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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.CIDFontType;
  25. import org.apache.fop.fonts.CMapSegment;
  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, 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 useAdvanced true to enable loading advanced info if available, false to disable
  63. * @param resolver the FontResolver for font URI resolution
  64. */
  65. public TTFFontLoader(String fontFileURI, String subFontName,
  66. boolean embedded, EmbeddingMode embeddingMode, EncodingMode encodingMode,
  67. boolean useKerning, boolean useAdvanced, FontResolver resolver) {
  68. super(fontFileURI, embedded, useKerning, useAdvanced, resolver);
  69. this.subFontName = subFontName;
  70. this.encodingMode = encodingMode;
  71. this.embeddingMode = embeddingMode;
  72. if (this.encodingMode == EncodingMode.AUTO) {
  73. this.encodingMode = EncodingMode.CID; //Default to CID mode for TrueType
  74. }
  75. if (this.embeddingMode == EmbeddingMode.AUTO) {
  76. this.embeddingMode = EmbeddingMode.SUBSET;
  77. }
  78. }
  79. /** {@inheritDoc} */
  80. protected void read() throws IOException {
  81. read(this.subFontName);
  82. }
  83. /**
  84. * Reads a TrueType font.
  85. * @param ttcFontName the TrueType sub-font name of TrueType Collection (may be null for
  86. * normal TrueType fonts)
  87. * @throws IOException if an I/O error occurs
  88. */
  89. private void read(String ttcFontName) throws IOException {
  90. InputStream in = openFontUri(resolver, this.fontFileURI);
  91. try {
  92. TTFFile ttf = new TTFFile(useKerning, useAdvanced);
  93. FontFileReader reader = new FontFileReader(in);
  94. boolean supported = ttf.readFont(reader, ttcFontName);
  95. if (!supported) {
  96. throw new IOException("TrueType font is not supported: " + fontFileURI);
  97. }
  98. buildFont(ttf, ttcFontName);
  99. loaded = true;
  100. } finally {
  101. IOUtils.closeQuietly(in);
  102. }
  103. }
  104. private void buildFont(TTFFile ttf, String ttcFontName) {
  105. if (ttf.isCFF()) {
  106. throw new UnsupportedOperationException(
  107. "OpenType fonts with CFF data are not supported, yet");
  108. }
  109. boolean isCid = this.embedded;
  110. if (this.encodingMode == EncodingMode.SINGLE_BYTE) {
  111. isCid = false;
  112. }
  113. if (isCid) {
  114. multiFont = new MultiByteFont();
  115. returnFont = multiFont;
  116. multiFont.setTTCName(ttcFontName);
  117. } else {
  118. singleFont = new SingleByteFont();
  119. returnFont = singleFont;
  120. }
  121. returnFont.setResolver(resolver);
  122. returnFont.setFontName(ttf.getPostScriptName());
  123. returnFont.setFullName(ttf.getFullName());
  124. returnFont.setFamilyNames(ttf.getFamilyNames());
  125. returnFont.setFontSubFamilyName(ttf.getSubFamilyName());
  126. returnFont.setCapHeight(ttf.getCapHeight());
  127. returnFont.setXHeight(ttf.getXHeight());
  128. returnFont.setAscender(ttf.getLowerCaseAscent());
  129. returnFont.setDescender(ttf.getLowerCaseDescent());
  130. returnFont.setFontBBox(ttf.getFontBBox());
  131. returnFont.setFlags(ttf.getFlags());
  132. returnFont.setStemV(Integer.parseInt(ttf.getStemV())); //not used for TTF
  133. returnFont.setItalicAngle(Integer.parseInt(ttf.getItalicAngle()));
  134. returnFont.setMissingWidth(0);
  135. returnFont.setWeight(ttf.getWeightClass());
  136. returnFont.setEmbeddingMode(this.embeddingMode);
  137. if (isCid) {
  138. multiFont.setCIDType(CIDFontType.CIDTYPE2);
  139. int[] wx = ttf.getWidths();
  140. multiFont.setWidthArray(wx);
  141. } else {
  142. singleFont.setFontType(FontType.TRUETYPE);
  143. singleFont.setEncoding(ttf.getCharSetName());
  144. returnFont.setFirstChar(ttf.getFirstChar());
  145. returnFont.setLastChar(ttf.getLastChar());
  146. singleFont.setTrueTypePostScriptVersion(ttf.getPostScriptVersion());
  147. copyWidthsSingleByte(ttf);
  148. }
  149. returnFont.setCMap(getCMap(ttf));
  150. if (useKerning) {
  151. copyKerning(ttf, isCid);
  152. }
  153. if (useAdvanced) {
  154. copyAdvanced(ttf);
  155. }
  156. if (this.embedded) {
  157. if (ttf.isEmbeddable()) {
  158. returnFont.setEmbedFileName(this.fontFileURI);
  159. } else {
  160. String msg = "The font " + this.fontFileURI + " is not embeddable due to a"
  161. + " licensing restriction.";
  162. throw new RuntimeException(msg);
  163. }
  164. }
  165. }
  166. private CMapSegment[] getCMap(TTFFile ttf) {
  167. CMapSegment[] array = new CMapSegment[ttf.getCMaps().size()];
  168. return ttf.getCMaps().toArray(array);
  169. }
  170. private void copyWidthsSingleByte(TTFFile ttf) {
  171. int[] wx = ttf.getWidths();
  172. for (int i = singleFont.getFirstChar(); i <= singleFont.getLastChar(); i++) {
  173. singleFont.setWidth(i, ttf.getCharWidth(i));
  174. }
  175. for (CMapSegment segment : ttf.getCMaps()) {
  176. if (segment.getUnicodeStart() < 0xFFFE) {
  177. for (char u = (char)segment.getUnicodeStart(); u <= segment.getUnicodeEnd(); u++) {
  178. int codePoint = singleFont.getEncoding().mapChar(u);
  179. if (codePoint <= 0) {
  180. int glyphIndex = segment.getGlyphStartIndex() + u - segment.getUnicodeStart();
  181. String glyphName = ttf.getGlyphName(glyphIndex);
  182. if (glyphName.length() == 0 && ttf.getPostScriptVersion() != PostScriptVersion.V2) {
  183. glyphName = "u" + HexEncoder.encode(u);
  184. }
  185. if (glyphName.length() > 0) {
  186. String unicode = Character.toString(u);
  187. NamedCharacter nc = new NamedCharacter(glyphName, unicode);
  188. singleFont.addUnencodedCharacter(nc, wx[glyphIndex]);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * Copy kerning information.
  197. */
  198. private void copyKerning(TTFFile ttf, boolean isCid) {
  199. // Get kerning
  200. Set<Integer> kerningSet;
  201. if (isCid) {
  202. kerningSet = ttf.getKerning().keySet();
  203. } else {
  204. kerningSet = ttf.getAnsiKerning().keySet();
  205. }
  206. for (Integer kpx1 : kerningSet) {
  207. Map<Integer, Integer> h2;
  208. if (isCid) {
  209. h2 = ttf.getKerning().get(kpx1);
  210. } else {
  211. h2 = ttf.getAnsiKerning().get(kpx1);
  212. }
  213. returnFont.putKerningEntry(kpx1, h2);
  214. }
  215. }
  216. /**
  217. * Copy advanced typographic information.
  218. */
  219. private void copyAdvanced ( TTFFile ttf ) {
  220. if ( returnFont instanceof MultiByteFont ) {
  221. MultiByteFont mbf = (MultiByteFont) returnFont;
  222. mbf.setGDEF ( ttf.getGDEF() );
  223. mbf.setGSUB ( ttf.getGSUB() );
  224. mbf.setGPOS ( ttf.getGPOS() );
  225. }
  226. }
  227. }