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

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