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.

TTFFontLoader.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.truetype;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import org.apache.commons.io.IOUtils;
  24. import org.apache.fop.fonts.BFEntry;
  25. import org.apache.fop.fonts.CIDFontType;
  26. import org.apache.fop.fonts.EmbeddingMode;
  27. import org.apache.fop.fonts.EncodingMode;
  28. import org.apache.fop.fonts.FontLoader;
  29. import org.apache.fop.fonts.FontResolver;
  30. import org.apache.fop.fonts.FontType;
  31. import org.apache.fop.fonts.MultiByteFont;
  32. import org.apache.fop.fonts.NamedCharacter;
  33. import org.apache.fop.fonts.SingleByteFont;
  34. import org.apache.fop.fonts.truetype.TTFFile.PostScriptVersion;
  35. import org.apache.fop.util.HexEncoder;
  36. /**
  37. * Loads a TrueType font into memory directly from the original font file.
  38. */
  39. public class TTFFontLoader extends FontLoader {
  40. private MultiByteFont multiFont;
  41. private SingleByteFont singleFont;
  42. private final String subFontName;
  43. private EncodingMode encodingMode;
  44. private EmbeddingMode embeddingMode;
  45. /**
  46. * Default constructor
  47. * @param fontFileURI the URI representing the font file
  48. * @param resolver the FontResolver for font URI resolution
  49. */
  50. public TTFFontLoader(String fontFileURI, FontResolver resolver) {
  51. this(fontFileURI, null, true, EmbeddingMode.AUTO, EncodingMode.AUTO, true, resolver);
  52. }
  53. /**
  54. * Additional constructor for TrueType Collections.
  55. * @param fontFileURI the URI representing the font file
  56. * @param subFontName the sub-fontname of a font in a TrueType Collection (or null for normal
  57. * TrueType fonts)
  58. * @param embedded indicates whether the font is embedded or referenced
  59. * @param embeddingMode the embedding mode of the font
  60. * @param encodingMode the requested encoding mode
  61. * @param useKerning true to enable loading kerning info if available, false to disable
  62. * @param resolver the FontResolver for font URI resolution
  63. */
  64. public TTFFontLoader(String fontFileURI, String subFontName,
  65. boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode,
  66. boolean useKerning, FontResolver resolver) {
  67. super(fontFileURI, embedded, true, resolver);
  68. this.subFontName = subFontName;
  69. this.encodingMode = encodingMode;
  70. this.embeddingMode = embeddingMode;
  71. if (this.encodingMode == EncodingMode.AUTO) {
  72. this.encodingMode = EncodingMode.CID; //Default to CID mode for TrueType
  73. }
  74. if (this.embeddingMode == EmbeddingMode.AUTO) {
  75. this.embeddingMode = EmbeddingMode.SUBSET;
  76. }
  77. }
  78. /** {@inheritDoc} */
  79. protected void read() throws IOException {
  80. read(this.subFontName);
  81. }
  82. /**
  83. * Reads a TrueType font.
  84. * @param ttcFontName the TrueType sub-font name of TrueType Collection (may be null for
  85. * normal TrueType fonts)
  86. * @throws IOException if an I/O error occurs
  87. */
  88. private void read(String ttcFontName) throws IOException {
  89. InputStream in = openFontUri(resolver, this.fontFileURI);
  90. try {
  91. TTFFile ttf = new TTFFile();
  92. FontFileReader reader = new FontFileReader(in);
  93. boolean supported = ttf.readFont(reader, ttcFontName);
  94. if (!supported) {
  95. throw new IOException("TrueType font is not supported: " + fontFileURI);
  96. }
  97. buildFont(ttf, ttcFontName);
  98. loaded = true;
  99. } finally {
  100. IOUtils.closeQuietly(in);
  101. }
  102. }
  103. private void buildFont(TTFFile ttf, String ttcFontName) {
  104. if (ttf.isCFF()) {
  105. throw new UnsupportedOperationException(
  106. "OpenType fonts with CFF data are not supported, yet");
  107. }
  108. boolean isCid = this.embedded;
  109. if (this.encodingMode == EncodingMode.SINGLE_BYTE) {
  110. isCid = false;
  111. }
  112. if (isCid) {
  113. multiFont = new MultiByteFont();
  114. returnFont = multiFont;
  115. multiFont.setTTCName(ttcFontName);
  116. } else {
  117. singleFont = new SingleByteFont();
  118. returnFont = singleFont;
  119. }
  120. returnFont.setResolver(resolver);
  121. returnFont.setFontName(ttf.getPostScriptName());
  122. returnFont.setFullName(ttf.getFullName());
  123. returnFont.setFamilyNames(ttf.getFamilyNames());
  124. returnFont.setFontSubFamilyName(ttf.getSubFamilyName());
  125. returnFont.setCapHeight(ttf.getCapHeight());
  126. returnFont.setXHeight(ttf.getXHeight());
  127. returnFont.setAscender(ttf.getLowerCaseAscent());
  128. returnFont.setDescender(ttf.getLowerCaseDescent());
  129. returnFont.setFontBBox(ttf.getFontBBox());
  130. returnFont.setFlags(ttf.getFlags());
  131. returnFont.setStemV(Integer.parseInt(ttf.getStemV())); //not used for TTF
  132. returnFont.setItalicAngle(Integer.parseInt(ttf.getItalicAngle()));
  133. returnFont.setMissingWidth(0);
  134. returnFont.setWeight(ttf.getWeightClass());
  135. returnFont.setEmbeddingMode(this.embeddingMode);
  136. if (isCid) {
  137. multiFont.setCIDType(CIDFontType.CIDTYPE2);
  138. int[] wx = ttf.getWidths();
  139. multiFont.setWidthArray(wx);
  140. } else {
  141. singleFont.setFontType(FontType.TRUETYPE);
  142. singleFont.setEncoding(ttf.getCharSetName());
  143. returnFont.setFirstChar(ttf.getFirstChar());
  144. returnFont.setLastChar(ttf.getLastChar());
  145. singleFont.setTrueTypePostScriptVersion(ttf.getPostScriptVersion());
  146. copyWidthsSingleByte(ttf);
  147. }
  148. returnFont.setCMap(getCMap(ttf));
  149. if (useKerning) {
  150. copyKerning(ttf, isCid);
  151. }
  152. if (this.embedded && ttf.isEmbeddable()) {
  153. returnFont.setEmbedFileName(this.fontFileURI);
  154. }
  155. }
  156. private BFEntry[] getCMap(TTFFile ttf) {
  157. BFEntry[] array = new BFEntry[ttf.getCMaps().size()];
  158. return ttf.getCMaps().toArray(array);
  159. }
  160. private void copyWidthsSingleByte(TTFFile ttf) {
  161. int[] wx = ttf.getWidths();
  162. for (int i = singleFont.getFirstChar(); i <= singleFont.getLastChar(); i++) {
  163. singleFont.setWidth(i, ttf.getCharWidth(i));
  164. }
  165. for (BFEntry ce : ttf.getCMaps()) {
  166. if (ce.getUnicodeStart() < 0xFFFE) {
  167. for (char u = (char)ce.getUnicodeStart(); u <= ce.getUnicodeEnd(); u++) {
  168. int codePoint = singleFont.getEncoding().mapChar(u);
  169. if (codePoint <= 0) {
  170. int glyphIndex = ce.getGlyphStartIndex() + u - ce.getUnicodeStart();
  171. String glyphName = ttf.getGlyphName(glyphIndex);
  172. if (glyphName == "" && ttf.getPostScriptVersion() != PostScriptVersion.V2) {
  173. glyphName = "u" + HexEncoder.encode(u);
  174. }
  175. if (glyphName != "") {
  176. String unicode = Character.toString(u);
  177. NamedCharacter nc = new NamedCharacter(glyphName, unicode);
  178. singleFont.addUnencodedCharacter(nc, wx[glyphIndex]);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * Copy kerning information.
  187. */
  188. private void copyKerning(TTFFile ttf, boolean isCid) {
  189. // Get kerning
  190. Set<Integer> kerningSet;
  191. if (isCid) {
  192. kerningSet = ttf.getKerning().keySet();
  193. } else {
  194. kerningSet = ttf.getAnsiKerning().keySet();
  195. }
  196. for (Integer kpx1 : kerningSet) {
  197. Map<Integer, Integer> h2;
  198. if (isCid) {
  199. h2 = ttf.getKerning().get(kpx1);
  200. } else {
  201. h2 = ttf.getAnsiKerning().get(kpx1);
  202. }
  203. returnFont.putKerningEntry(kpx1, h2);
  204. }
  205. }
  206. }