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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.TreeSet;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.xmlgraphics.fonts.Glyphs;
  28. import org.apache.fop.fonts.truetype.TTFFile.PostScriptVersion;
  29. /**
  30. * Generic SingleByte font
  31. */
  32. public class SingleByteFont extends CustomFont {
  33. /** logger */
  34. private static Log log = LogFactory.getLog(SingleByteFont.class);
  35. private SingleByteEncoding mapping;
  36. private boolean useNativeEncoding = false;
  37. private int[] width = null;
  38. private Map<Character, UnencodedCharacter> unencodedCharacters;
  39. private List<SimpleSingleByteEncoding> additionalEncodings;
  40. private Map<Character, Character> alternativeCodes;
  41. private PostScriptVersion ttPostScriptVersion;
  42. /**
  43. * Main constructor.
  44. */
  45. public SingleByteFont() {
  46. setEncoding(CodePointMapping.WIN_ANSI_ENCODING);
  47. }
  48. /** {@inheritDoc} */
  49. public boolean isEmbeddable() {
  50. return (!(getEmbedFileName() == null
  51. && getEmbedResourceName() == null));
  52. }
  53. /** {@inheritDoc} */
  54. public boolean isSubsetEmbedded() {
  55. return false;
  56. }
  57. /** {@inheritDoc} */
  58. public String getEncodingName() {
  59. return this.mapping.getName();
  60. }
  61. /**
  62. * Returns the code point mapping (encoding) of this font.
  63. * @return the code point mapping
  64. */
  65. public SingleByteEncoding getEncoding() {
  66. return this.mapping;
  67. }
  68. /** {@inheritDoc} */
  69. public int getWidth(int i, int size) {
  70. if (i < 256) {
  71. int idx = i - getFirstChar();
  72. if (idx >= 0 && idx < width.length) {
  73. return size * width[idx];
  74. }
  75. } else if (this.additionalEncodings != null) {
  76. int encodingIndex = (i / 256) - 1;
  77. SimpleSingleByteEncoding encoding = getAdditionalEncoding(encodingIndex);
  78. int codePoint = i % 256;
  79. NamedCharacter nc = encoding.getCharacterForIndex(codePoint);
  80. UnencodedCharacter uc
  81. = this.unencodedCharacters.get(Character.valueOf(nc.getSingleUnicodeValue()));
  82. return size * uc.getWidth();
  83. }
  84. return 0;
  85. }
  86. /** {@inheritDoc} */
  87. public int[] getWidths() {
  88. int[] arr = new int[width.length];
  89. System.arraycopy(width, 0, arr, 0, width.length);
  90. return arr;
  91. }
  92. /**
  93. * Lookup a character using its alternative names. If found, cache it so we
  94. * can speed up lookups.
  95. * @param c the character
  96. * @return the suggested alternative character present in the font
  97. */
  98. private char findAlternative(char c) {
  99. char d;
  100. if (alternativeCodes == null) {
  101. alternativeCodes = new java.util.HashMap<Character, Character>();
  102. } else {
  103. Character alternative = alternativeCodes.get(c);
  104. if (alternative != null) {
  105. return alternative;
  106. }
  107. }
  108. String charName = Glyphs.charToGlyphName(c);
  109. String[] charNameAlternatives = Glyphs.getCharNameAlternativesFor(charName);
  110. if (charNameAlternatives != null && charNameAlternatives.length > 0) {
  111. for (int i = 0; i < charNameAlternatives.length; i++) {
  112. if (log.isDebugEnabled()) {
  113. log.debug("Checking alternative for char " + c + " (charname="
  114. + charName + "): " + charNameAlternatives[i]);
  115. }
  116. String s = Glyphs.getUnicodeSequenceForGlyphName(charNameAlternatives[i]);
  117. if (s != null) {
  118. d = lookupChar(s.charAt(0));
  119. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  120. alternativeCodes.put(c, d);
  121. return d;
  122. }
  123. }
  124. }
  125. }
  126. return SingleByteEncoding.NOT_FOUND_CODE_POINT;
  127. }
  128. private char lookupChar(char c) {
  129. char d = mapping.mapChar(c);
  130. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  131. return d;
  132. }
  133. // Check unencoded characters which are available in the font by
  134. // character name
  135. d = mapUnencodedChar(c);
  136. return d;
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public char mapChar(char c) {
  141. notifyMapOperation();
  142. char d = lookupChar(c);
  143. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  144. return d;
  145. } else {
  146. // Check for alternative
  147. d = findAlternative(c);
  148. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  149. return d;
  150. }
  151. }
  152. this.warnMissingGlyph(c);
  153. return Typeface.NOT_FOUND;
  154. }
  155. private char mapUnencodedChar(char ch) {
  156. if (this.unencodedCharacters != null) {
  157. UnencodedCharacter unencoded = this.unencodedCharacters.get(Character.valueOf(ch));
  158. if (unencoded != null) {
  159. if (this.additionalEncodings == null) {
  160. this.additionalEncodings = new ArrayList<SimpleSingleByteEncoding>();
  161. }
  162. SimpleSingleByteEncoding encoding = null;
  163. char mappedStart = 0;
  164. int additionalsCount = this.additionalEncodings.size();
  165. for (int i = 0; i < additionalsCount; i++) {
  166. mappedStart += 256;
  167. encoding = getAdditionalEncoding(i);
  168. char alt = encoding.mapChar(ch);
  169. if (alt != 0) {
  170. return (char)(mappedStart + alt);
  171. }
  172. }
  173. if (encoding != null && encoding.isFull()) {
  174. encoding = null;
  175. }
  176. if (encoding == null) {
  177. encoding = new SimpleSingleByteEncoding(
  178. getFontName() + "EncodingSupp" + (additionalsCount + 1));
  179. this.additionalEncodings.add(encoding);
  180. mappedStart += 256;
  181. }
  182. return (char)(mappedStart + encoding.addCharacter(unencoded.getCharacter()));
  183. }
  184. }
  185. return 0;
  186. }
  187. /** {@inheritDoc} */
  188. @Override
  189. public boolean hasChar(char c) {
  190. char d = mapping.mapChar(c);
  191. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  192. return true;
  193. }
  194. //Check unencoded characters which are available in the font by character name
  195. d = mapUnencodedChar(c);
  196. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  197. return true;
  198. }
  199. // Check if an alternative exists
  200. d = findAlternative(c);
  201. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  202. return true;
  203. }
  204. return false;
  205. }
  206. /* ---- single byte font specific setters --- */
  207. /**
  208. * Updates the mapping variable based on the encoding.
  209. * @param encoding the name of the encoding
  210. */
  211. protected void updateMapping(String encoding) {
  212. try {
  213. this.mapping = CodePointMapping.getMapping(encoding);
  214. } catch (UnsupportedOperationException e) {
  215. log.error("Font '" + super.getFontName() + "': " + e.getMessage());
  216. }
  217. }
  218. /**
  219. * Sets the encoding of the font.
  220. * @param encoding the encoding (ex. "WinAnsiEncoding" or "SymbolEncoding")
  221. */
  222. public void setEncoding(String encoding) {
  223. updateMapping(encoding);
  224. }
  225. /**
  226. * Sets the encoding of the font.
  227. * @param encoding the encoding information
  228. */
  229. public void setEncoding(CodePointMapping encoding) {
  230. this.mapping = encoding;
  231. }
  232. /**
  233. * Controls whether the font is configured to use its native encoding or if it
  234. * may need to be re-encoded for the target format.
  235. * @param value true indicates that the configured encoding is the font's native encoding
  236. */
  237. public void setUseNativeEncoding(boolean value) {
  238. this.useNativeEncoding = value;
  239. }
  240. /**
  241. * Indicates whether this font is configured to use its native encoding. This
  242. * method is used to determine whether the font needs to be re-encoded.
  243. * @return true if the font uses its native encoding.
  244. */
  245. public boolean isUsingNativeEncoding() {
  246. return this.useNativeEncoding;
  247. }
  248. /**
  249. * Sets a width for a character.
  250. * @param index index of the character
  251. * @param w the width of the character
  252. */
  253. public void setWidth(int index, int w) {
  254. if (this.width == null) {
  255. this.width = new int[getLastChar() - getFirstChar() + 1];
  256. }
  257. this.width[index - getFirstChar()] = w;
  258. }
  259. /**
  260. * Adds an unencoded character (one that is not supported by the primary encoding).
  261. * @param ch the named character
  262. * @param width the width of the character
  263. */
  264. public void addUnencodedCharacter(NamedCharacter ch, int width) {
  265. if (this.unencodedCharacters == null) {
  266. this.unencodedCharacters = new HashMap<Character, UnencodedCharacter>();
  267. }
  268. if (ch.hasSingleUnicodeValue()) {
  269. UnencodedCharacter uc = new UnencodedCharacter(ch, width);
  270. this.unencodedCharacters.put(Character.valueOf(ch.getSingleUnicodeValue()), uc);
  271. } else {
  272. //Cannot deal with unicode sequences, so ignore this character
  273. }
  274. }
  275. /**
  276. * Makes all unencoded characters available through additional encodings. This method
  277. * is used in cases where the fonts need to be encoded in the target format before
  278. * all text of the document is processed (for example in PostScript when resource optimization
  279. * is disabled).
  280. */
  281. public void encodeAllUnencodedCharacters() {
  282. if (this.unencodedCharacters != null) {
  283. Set<Character> sortedKeys = new TreeSet<Character>(this.unencodedCharacters.keySet());
  284. for (Character ch : sortedKeys) {
  285. char mapped = mapChar(ch.charValue());
  286. assert mapped != Typeface.NOT_FOUND;
  287. }
  288. }
  289. }
  290. /**
  291. * Indicates whether the encoding has additional encodings besides the primary encoding.
  292. * @return true if there are additional encodings.
  293. */
  294. public boolean hasAdditionalEncodings() {
  295. return (this.additionalEncodings != null) && (this.additionalEncodings.size() > 0);
  296. }
  297. /**
  298. * Returns the number of additional encodings this single-byte font maintains.
  299. * @return the number of additional encodings
  300. */
  301. public int getAdditionalEncodingCount() {
  302. if (hasAdditionalEncodings()) {
  303. return this.additionalEncodings.size();
  304. } else {
  305. return 0;
  306. }
  307. }
  308. /**
  309. * Returns an additional encoding.
  310. * @param index the index of the additional encoding
  311. * @return the additional encoding
  312. * @throws IndexOutOfBoundsException if the index is out of bounds
  313. */
  314. public SimpleSingleByteEncoding getAdditionalEncoding(int index)
  315. throws IndexOutOfBoundsException {
  316. if (hasAdditionalEncodings()) {
  317. return this.additionalEncodings.get(index);
  318. } else {
  319. throw new IndexOutOfBoundsException("No additional encodings available");
  320. }
  321. }
  322. /**
  323. * Returns an array with the widths for an additional encoding.
  324. * @param index the index of the additional encoding
  325. * @return the width array
  326. */
  327. public int[] getAdditionalWidths(int index) {
  328. SimpleSingleByteEncoding enc = getAdditionalEncoding(index);
  329. int[] arr = new int[enc.getLastChar() - enc.getFirstChar() + 1];
  330. for (int i = 0, c = arr.length; i < c; i++) {
  331. NamedCharacter nc = enc.getCharacterForIndex(enc.getFirstChar() + i);
  332. UnencodedCharacter uc = this.unencodedCharacters.get(
  333. Character.valueOf(nc.getSingleUnicodeValue()));
  334. arr[i] = uc.getWidth();
  335. }
  336. return arr;
  337. }
  338. private static final class UnencodedCharacter {
  339. private final NamedCharacter character;
  340. private final int width;
  341. public UnencodedCharacter(NamedCharacter character, int width) {
  342. this.character = character;
  343. this.width = width;
  344. }
  345. public NamedCharacter getCharacter() {
  346. return this.character;
  347. }
  348. public int getWidth() {
  349. return this.width;
  350. }
  351. /** {@inheritDoc} */
  352. @Override
  353. public String toString() {
  354. return getCharacter().toString();
  355. }
  356. }
  357. /**
  358. * Sets the version of the PostScript table stored in the TrueType font represented by
  359. * this instance.
  360. *
  361. * @param version version of the <q>post</q> table
  362. */
  363. public void setTrueTypePostScriptVersion(PostScriptVersion version) {
  364. ttPostScriptVersion = version;
  365. }
  366. /**
  367. * Returns the version of the PostScript table stored in the TrueType font represented by
  368. * this instance.
  369. *
  370. * @return the version of the <q>post</q> table
  371. */
  372. public PostScriptVersion getTrueTypePostScriptVersion() {
  373. assert getFontType() == FontType.TRUETYPE;
  374. return ttPostScriptVersion;
  375. }
  376. }