You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SingleByteFont.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts;
  19. import java.awt.Rectangle;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.LinkedHashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import java.util.TreeSet;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.apache.xmlgraphics.fonts.Glyphs;
  31. import org.apache.fop.apps.io.InternalResourceResolver;
  32. import org.apache.fop.fonts.truetype.OpenFont.PostScriptVersion;
  33. import org.apache.fop.util.CharUtilities;
  34. /**
  35. * Generic SingleByte font
  36. */
  37. public class SingleByteFont extends CustomFont {
  38. /** logger */
  39. private static Log log = LogFactory.getLog(SingleByteFont.class);
  40. protected SingleByteEncoding mapping;
  41. private boolean useNativeEncoding;
  42. protected int[] width;
  43. private Rectangle[] boundingBoxes;
  44. private Map<Character, UnencodedCharacter> unencodedCharacters;
  45. private List<SimpleSingleByteEncoding> additionalEncodings;
  46. private Map<Character, Character> alternativeCodes;
  47. private PostScriptVersion ttPostScriptVersion;
  48. private int usedGlyphsCount;
  49. private LinkedHashMap<Integer, String> usedGlyphNames;
  50. private Map<Integer, Integer> usedGlyphs;
  51. private Map<Integer, Character> usedCharsIndex;
  52. private Map<Character, Integer> charGIDMappings;
  53. public SingleByteFont(InternalResourceResolver resourceResolver) {
  54. super(resourceResolver);
  55. setEncoding(CodePointMapping.WIN_ANSI_ENCODING);
  56. }
  57. public SingleByteFont(InternalResourceResolver resourceResolver, EmbeddingMode embeddingMode) {
  58. this(resourceResolver);
  59. setEmbeddingMode(embeddingMode);
  60. if (embeddingMode != EmbeddingMode.FULL) {
  61. usedGlyphNames = new LinkedHashMap<Integer, String>();
  62. usedGlyphs = new HashMap<Integer, Integer>();
  63. usedCharsIndex = new HashMap<Integer, Character>();
  64. charGIDMappings = new HashMap<Character, Integer>();
  65. // The zeroth value is reserved for .notdef
  66. usedGlyphs.put(0, 0);
  67. usedGlyphsCount++;
  68. }
  69. }
  70. /** {@inheritDoc} */
  71. public boolean isEmbeddable() {
  72. return (!(getEmbedFileURI() == null
  73. && getEmbedResourceName() == null));
  74. }
  75. /** {@inheritDoc} */
  76. public boolean isSubsetEmbedded() {
  77. return getEmbeddingMode() != EmbeddingMode.FULL;
  78. }
  79. /** {@inheritDoc} */
  80. public String getEncodingName() {
  81. return this.mapping.getName();
  82. }
  83. /**
  84. * Returns the code point mapping (encoding) of this font.
  85. * @return the code point mapping
  86. */
  87. public SingleByteEncoding getEncoding() {
  88. return this.mapping;
  89. }
  90. /** {@inheritDoc} */
  91. public int getWidth(int i, int size) {
  92. if (i < 256) {
  93. int idx = i - getFirstChar();
  94. if (idx >= 0 && idx < width.length) {
  95. return size * width[idx];
  96. }
  97. } else if (this.additionalEncodings != null) {
  98. int encodingIndex = (i / 256) - 1;
  99. SimpleSingleByteEncoding encoding = getAdditionalEncoding(encodingIndex);
  100. int codePoint = i % 256;
  101. NamedCharacter nc = encoding.getCharacterForIndex(codePoint);
  102. UnencodedCharacter uc
  103. = this.unencodedCharacters.get(nc.getSingleUnicodeValue());
  104. return size * uc.getWidth();
  105. }
  106. return 0;
  107. }
  108. /** {@inheritDoc} */
  109. public int[] getWidths() {
  110. int[] arr = new int[width.length];
  111. System.arraycopy(width, 0, arr, 0, width.length);
  112. return arr;
  113. }
  114. public Rectangle getBoundingBox(int glyphIndex, int size) {
  115. Rectangle bbox = null;
  116. if (glyphIndex < 256) {
  117. int idx = glyphIndex - getFirstChar();
  118. if (idx >= 0 && idx < boundingBoxes.length) {
  119. bbox = boundingBoxes[idx];
  120. }
  121. } else if (this.additionalEncodings != null) {
  122. int encodingIndex = (glyphIndex / 256) - 1;
  123. SimpleSingleByteEncoding encoding = getAdditionalEncoding(encodingIndex);
  124. int codePoint = glyphIndex % 256;
  125. NamedCharacter nc = encoding.getCharacterForIndex(codePoint);
  126. UnencodedCharacter uc = this.unencodedCharacters.get(nc.getSingleUnicodeValue());
  127. bbox = uc.getBBox();
  128. }
  129. return bbox == null ? null : new Rectangle(bbox.x * size, bbox.y * size, bbox.width * size, bbox.height * size);
  130. }
  131. /**
  132. * Lookup a character using its alternative names. If found, cache it so we
  133. * can speed up lookups.
  134. * @param c the character
  135. * @return the suggested alternative character present in the font
  136. */
  137. private char findAlternative(char c) {
  138. char d;
  139. if (alternativeCodes == null) {
  140. alternativeCodes = new java.util.HashMap<Character, Character>();
  141. } else {
  142. Character alternative = alternativeCodes.get(c);
  143. if (alternative != null) {
  144. return alternative;
  145. }
  146. }
  147. String charName = Glyphs.charToGlyphName(c);
  148. String[] charNameAlternatives = Glyphs.getCharNameAlternativesFor(charName);
  149. if (charNameAlternatives != null && charNameAlternatives.length > 0) {
  150. for (int i = 0; i < charNameAlternatives.length; i++) {
  151. if (log.isDebugEnabled()) {
  152. log.debug("Checking alternative for char " + c + " (charname="
  153. + charName + "): " + charNameAlternatives[i]);
  154. }
  155. String s = Glyphs.getUnicodeSequenceForGlyphName(charNameAlternatives[i]);
  156. if (s != null) {
  157. d = lookupChar(s.charAt(0));
  158. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  159. alternativeCodes.put(c, d);
  160. return d;
  161. }
  162. }
  163. }
  164. }
  165. return SingleByteEncoding.NOT_FOUND_CODE_POINT;
  166. }
  167. private char lookupChar(char c) {
  168. char d = mapping.mapChar(c);
  169. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  170. return d;
  171. }
  172. // Check unencoded characters which are available in the font by
  173. // character name
  174. d = mapUnencodedChar(c);
  175. return d;
  176. }
  177. private boolean isSubset() {
  178. return getEmbeddingMode() == EmbeddingMode.SUBSET;
  179. }
  180. /** {@inheritDoc} */
  181. @Override
  182. public char mapChar(char c) {
  183. notifyMapOperation();
  184. char d = lookupChar(c);
  185. if (d == SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  186. // Check for alternative
  187. d = findAlternative(c);
  188. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  189. return d;
  190. } else {
  191. this.warnMissingGlyph(c);
  192. return Typeface.NOT_FOUND;
  193. }
  194. }
  195. if (isEmbeddable() && isSubset()) {
  196. mapChar(d, c);
  197. }
  198. return d;
  199. }
  200. private int mapChar(int glyphIndex, char unicode) {
  201. // Reencode to a new subset font or get the reencoded value
  202. // IOW, accumulate the accessed characters and build a character map for them
  203. Integer subsetCharSelector = usedGlyphs.get(glyphIndex);
  204. if (subsetCharSelector == null) {
  205. int selector = usedGlyphsCount;
  206. usedGlyphs.put(glyphIndex, selector);
  207. usedCharsIndex.put(selector, unicode);
  208. charGIDMappings.put(unicode, glyphIndex);
  209. usedGlyphsCount++;
  210. return selector;
  211. } else {
  212. return subsetCharSelector;
  213. }
  214. }
  215. private char getUnicode(int index) {
  216. Character mapValue = usedCharsIndex.get(index);
  217. if (mapValue != null) {
  218. return mapValue.charValue();
  219. } else {
  220. return CharUtilities.NOT_A_CHARACTER;
  221. }
  222. }
  223. private char mapUnencodedChar(char ch) {
  224. if (this.unencodedCharacters != null) {
  225. UnencodedCharacter unencoded = this.unencodedCharacters.get(ch);
  226. if (unencoded != null) {
  227. if (this.additionalEncodings == null) {
  228. this.additionalEncodings = new ArrayList<SimpleSingleByteEncoding>();
  229. }
  230. SimpleSingleByteEncoding encoding = null;
  231. char mappedStart = 0;
  232. int additionalsCount = this.additionalEncodings.size();
  233. for (int i = 0; i < additionalsCount; i++) {
  234. mappedStart += 256;
  235. encoding = getAdditionalEncoding(i);
  236. char alt = encoding.mapChar(ch);
  237. if (alt != 0) {
  238. return (char)(mappedStart + alt);
  239. }
  240. }
  241. if (encoding != null && encoding.isFull()) {
  242. encoding = null;
  243. }
  244. if (encoding == null) {
  245. encoding = new SimpleSingleByteEncoding(
  246. getFontName() + "EncodingSupp" + (additionalsCount + 1));
  247. this.additionalEncodings.add(encoding);
  248. mappedStart += 256;
  249. }
  250. return (char)(mappedStart + encoding.addCharacter(unencoded.getCharacter()));
  251. }
  252. }
  253. return 0;
  254. }
  255. /** {@inheritDoc} */
  256. @Override
  257. public boolean hasChar(char c) {
  258. char d = mapping.mapChar(c);
  259. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  260. return true;
  261. }
  262. //Check unencoded characters which are available in the font by character name
  263. d = mapUnencodedChar(c);
  264. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  265. return true;
  266. }
  267. // Check if an alternative exists
  268. d = findAlternative(c);
  269. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  270. return true;
  271. }
  272. return false;
  273. }
  274. /* ---- single byte font specific setters --- */
  275. /**
  276. * Updates the mapping variable based on the encoding.
  277. * @param encoding the name of the encoding
  278. */
  279. protected void updateMapping(String encoding) {
  280. try {
  281. this.mapping = CodePointMapping.getMapping(encoding);
  282. } catch (UnsupportedOperationException e) {
  283. log.error("Font '" + super.getFontName() + "': " + e.getMessage());
  284. }
  285. }
  286. /**
  287. * Sets the encoding of the font.
  288. * @param encoding the encoding (ex. "WinAnsiEncoding" or "SymbolEncoding")
  289. */
  290. public void setEncoding(String encoding) {
  291. updateMapping(encoding);
  292. }
  293. /**
  294. * Sets the encoding of the font.
  295. * @param encoding the encoding information
  296. */
  297. public void setEncoding(CodePointMapping encoding) {
  298. this.mapping = encoding;
  299. }
  300. /**
  301. * Controls whether the font is configured to use its native encoding or if it
  302. * may need to be re-encoded for the target format.
  303. * @param value true indicates that the configured encoding is the font's native encoding
  304. */
  305. public void setUseNativeEncoding(boolean value) {
  306. this.useNativeEncoding = value;
  307. }
  308. /**
  309. * Indicates whether this font is configured to use its native encoding. This
  310. * method is used to determine whether the font needs to be re-encoded.
  311. * @return true if the font uses its native encoding.
  312. */
  313. public boolean isUsingNativeEncoding() {
  314. return this.useNativeEncoding;
  315. }
  316. /**
  317. * Sets a width for a character.
  318. * @param index index of the character
  319. * @param w the width of the character
  320. */
  321. public void setWidth(int index, int w) {
  322. if (this.width == null) {
  323. this.width = new int[getLastChar() - getFirstChar() + 1];
  324. }
  325. this.width[index - getFirstChar()] = w;
  326. }
  327. public void setBoundingBox(int index, Rectangle bbox) {
  328. if (this.boundingBoxes == null) {
  329. this.boundingBoxes = new Rectangle[getLastChar() - getFirstChar() + 1];
  330. }
  331. this.boundingBoxes[index - getFirstChar()] = bbox;
  332. }
  333. /**
  334. * Adds an unencoded character (one that is not supported by the primary encoding).
  335. * @param ch the named character
  336. * @param width the width of the character
  337. */
  338. public void addUnencodedCharacter(NamedCharacter ch, int width, Rectangle bbox) {
  339. if (this.unencodedCharacters == null) {
  340. this.unencodedCharacters = new HashMap<Character, UnencodedCharacter>();
  341. }
  342. if (ch.hasSingleUnicodeValue()) {
  343. UnencodedCharacter uc = new UnencodedCharacter(ch, width, bbox);
  344. this.unencodedCharacters.put(ch.getSingleUnicodeValue(), uc);
  345. } else {
  346. //Cannot deal with unicode sequences, so ignore this character
  347. }
  348. }
  349. /**
  350. * Makes all unencoded characters available through additional encodings. This method
  351. * is used in cases where the fonts need to be encoded in the target format before
  352. * all text of the document is processed (for example in PostScript when resource optimization
  353. * is disabled).
  354. */
  355. public void encodeAllUnencodedCharacters() {
  356. if (this.unencodedCharacters != null) {
  357. Set<Character> sortedKeys = new TreeSet<Character>(this.unencodedCharacters.keySet());
  358. for (Character ch : sortedKeys) {
  359. char mapped = mapChar(ch.charValue());
  360. assert mapped != Typeface.NOT_FOUND;
  361. }
  362. }
  363. }
  364. /**
  365. * Indicates whether the encoding has additional encodings besides the primary encoding.
  366. * @return true if there are additional encodings.
  367. */
  368. public boolean hasAdditionalEncodings() {
  369. return (this.additionalEncodings != null) && (this.additionalEncodings.size() > 0);
  370. }
  371. /**
  372. * Returns the number of additional encodings this single-byte font maintains.
  373. * @return the number of additional encodings
  374. */
  375. public int getAdditionalEncodingCount() {
  376. if (hasAdditionalEncodings()) {
  377. return this.additionalEncodings.size();
  378. } else {
  379. return 0;
  380. }
  381. }
  382. /**
  383. * Returns an additional encoding.
  384. * @param index the index of the additional encoding
  385. * @return the additional encoding
  386. * @throws IndexOutOfBoundsException if the index is out of bounds
  387. */
  388. public SimpleSingleByteEncoding getAdditionalEncoding(int index)
  389. throws IndexOutOfBoundsException {
  390. if (hasAdditionalEncodings()) {
  391. return this.additionalEncodings.get(index);
  392. } else {
  393. throw new IndexOutOfBoundsException("No additional encodings available");
  394. }
  395. }
  396. /**
  397. * Returns an array with the widths for an additional encoding.
  398. * @param index the index of the additional encoding
  399. * @return the width array
  400. */
  401. public int[] getAdditionalWidths(int index) {
  402. SimpleSingleByteEncoding enc = getAdditionalEncoding(index);
  403. int[] arr = new int[enc.getLastChar() - enc.getFirstChar() + 1];
  404. for (int i = 0, c = arr.length; i < c; i++) {
  405. NamedCharacter nc = enc.getCharacterForIndex(enc.getFirstChar() + i);
  406. UnencodedCharacter uc = this.unencodedCharacters.get(
  407. nc.getSingleUnicodeValue());
  408. arr[i] = uc.getWidth();
  409. }
  410. return arr;
  411. }
  412. private static final class UnencodedCharacter {
  413. private final NamedCharacter character;
  414. private final int width;
  415. private final Rectangle bbox;
  416. public UnencodedCharacter(NamedCharacter character, int width, Rectangle bbox) {
  417. this.character = character;
  418. this.width = width;
  419. this.bbox = bbox;
  420. }
  421. public NamedCharacter getCharacter() {
  422. return this.character;
  423. }
  424. public int getWidth() {
  425. return this.width;
  426. }
  427. public Rectangle getBBox() {
  428. return bbox;
  429. }
  430. /** {@inheritDoc} */
  431. @Override
  432. public String toString() {
  433. return getCharacter().toString();
  434. }
  435. }
  436. /**
  437. * Sets the version of the PostScript table stored in the TrueType font represented by
  438. * this instance.
  439. *
  440. * @param version version of the <q>post</q> table
  441. */
  442. public void setTrueTypePostScriptVersion(PostScriptVersion version) {
  443. ttPostScriptVersion = version;
  444. }
  445. /**
  446. * Returns the version of the PostScript table stored in the TrueType font represented by
  447. * this instance.
  448. *
  449. * @return the version of the <q>post</q> table
  450. */
  451. public PostScriptVersion getTrueTypePostScriptVersion() {
  452. assert getFontType() == FontType.TRUETYPE;
  453. return ttPostScriptVersion;
  454. }
  455. /**
  456. * Returns a Map of used Glyphs.
  457. * @return Map Map of used Glyphs
  458. */
  459. public Map<Integer, Integer> getUsedGlyphs() {
  460. return Collections.unmodifiableMap(usedGlyphs);
  461. }
  462. public char getUnicodeFromSelector(int selector) {
  463. return getUnicode(selector);
  464. }
  465. public int getGIDFromChar(char ch) {
  466. return charGIDMappings.get(ch);
  467. }
  468. public char getUnicodeFromGID(int glyphIndex) {
  469. int selector = usedGlyphs.get(glyphIndex);
  470. return usedCharsIndex.get(selector);
  471. }
  472. public void mapUsedGlyphName(int gid, String value) {
  473. usedGlyphNames.put(gid, value);
  474. }
  475. public Map<Integer, String> getUsedGlyphNames() {
  476. return usedGlyphNames;
  477. }
  478. public String getGlyphName(int idx) {
  479. if (idx < mapping.getCharNameMap().length) {
  480. return mapping.getCharNameMap()[idx];
  481. } else {
  482. int selector = usedGlyphs.get(idx);
  483. char theChar = usedCharsIndex.get(selector);
  484. return unencodedCharacters.get(theChar).getCharacter().getName();
  485. }
  486. }
  487. }