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.5KB

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.net.URI;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import org.apache.commons.io.IOUtils;
  25. import org.apache.fop.apps.io.InternalResourceResolver;
  26. import org.apache.fop.fonts.CIDFontType;
  27. import org.apache.fop.fonts.CMapSegment;
  28. import org.apache.fop.fonts.EmbeddingMode;
  29. import org.apache.fop.fonts.EncodingMode;
  30. import org.apache.fop.fonts.FontLoader;
  31. import org.apache.fop.fonts.FontType;
  32. import org.apache.fop.fonts.MultiByteFont;
  33. import org.apache.fop.fonts.NamedCharacter;
  34. import org.apache.fop.fonts.SingleByteFont;
  35. import org.apache.fop.fonts.truetype.TTFFile.PostScriptVersion;
  36. import org.apache.fop.util.HexEncoder;
  37. /**
  38. * Loads a TrueType font into memory directly from the original font file.
  39. */
  40. public class TTFFontLoader extends FontLoader {
  41. private MultiByteFont multiFont;
  42. private SingleByteFont singleFont;
  43. private final String subFontName;
  44. private EncodingMode encodingMode;
  45. private EmbeddingMode embeddingMode;
  46. /**
  47. * Default constructor
  48. * @param fontFileURI the URI representing the font file
  49. * @param resourceResolver the resource resolver for font URI resolution
  50. */
  51. public TTFFontLoader(URI fontFileURI, InternalResourceResolver resourceResolver) {
  52. this(fontFileURI, null, true, EmbeddingMode.AUTO, EncodingMode.AUTO, true, true, resourceResolver);
  53. }
  54. /**
  55. * Additional constructor for TrueType Collections.
  56. * @param fontFileURI the URI representing the font file
  57. * @param subFontName the sub-fontname of a font in a TrueType Collection (or null for normal
  58. * TrueType fonts)
  59. * @param embedded indicates whether the font is embedded or referenced
  60. * @param embeddingMode the embedding mode of the font
  61. * @param encodingMode the requested encoding mode
  62. * @param useKerning true to enable loading kerning info if available, false to disable
  63. * @param useAdvanced true to enable loading advanced info if available, false to disable
  64. * @param resolver the FontResolver for font URI resolution
  65. */
  66. public TTFFontLoader(URI fontFileURI, String subFontName, boolean embedded,
  67. EmbeddingMode embeddingMode, EncodingMode encodingMode, boolean useKerning,
  68. boolean useAdvanced, InternalResourceResolver resolver) {
  69. super(fontFileURI, embedded, useKerning, useAdvanced, resolver);
  70. this.subFontName = subFontName;
  71. this.encodingMode = encodingMode;
  72. this.embeddingMode = embeddingMode;
  73. if (this.encodingMode == EncodingMode.AUTO) {
  74. this.encodingMode = EncodingMode.CID; //Default to CID mode for TrueType
  75. }
  76. if (this.embeddingMode == EmbeddingMode.AUTO) {
  77. this.embeddingMode = EmbeddingMode.SUBSET;
  78. }
  79. }
  80. /** {@inheritDoc} */
  81. protected void read() throws IOException {
  82. read(this.subFontName);
  83. }
  84. /**
  85. * Reads a TrueType font.
  86. * @param ttcFontName the TrueType sub-font name of TrueType Collection (may be null for
  87. * normal TrueType fonts)
  88. * @throws IOException if an I/O error occurs
  89. */
  90. private void read(String ttcFontName) throws IOException {
  91. InputStream in = resourceResolver.getResource(this.fontFileURI);
  92. try {
  93. TTFFile ttf = new TTFFile(useKerning, useAdvanced);
  94. FontFileReader reader = new FontFileReader(in);
  95. boolean supported = ttf.readFont(reader, ttcFontName);
  96. if (!supported) {
  97. throw new IOException("TrueType font is not supported: " + fontFileURI);
  98. }
  99. buildFont(ttf, ttcFontName);
  100. loaded = true;
  101. } finally {
  102. IOUtils.closeQuietly(in);
  103. }
  104. }
  105. private void buildFont(TTFFile ttf, String ttcFontName) {
  106. if (ttf.isCFF()) {
  107. throw new UnsupportedOperationException(
  108. "OpenType fonts with CFF data are not supported, yet");
  109. }
  110. boolean isCid = this.embedded;
  111. if (this.encodingMode == EncodingMode.SINGLE_BYTE) {
  112. isCid = false;
  113. }
  114. if (isCid) {
  115. multiFont = new MultiByteFont(resourceResolver, embeddingMode);
  116. returnFont = multiFont;
  117. multiFont.setTTCName(ttcFontName);
  118. } else {
  119. singleFont = new SingleByteFont(resourceResolver);
  120. returnFont = singleFont;
  121. }
  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.setEmbedURI(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. }