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

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