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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.fop.fonts.BFEntry;
  25. import org.apache.fop.fonts.CIDFontType;
  26. import org.apache.fop.fonts.FontLoader;
  27. import org.apache.fop.fonts.FontResolver;
  28. import org.apache.fop.fonts.MultiByteFont;
  29. /**
  30. * Loads a font into memory directly from the original font file.
  31. */
  32. public class TTFFontLoader extends FontLoader {
  33. private MultiByteFont multiFont;
  34. /**
  35. * Default constructor
  36. * @param fontFileURI the URI representing the font file
  37. * @param in the InputStream to load the font from
  38. * @param resolver the FontResolver for font URI resolution
  39. */
  40. public TTFFontLoader(String fontFileURI, InputStream in, FontResolver resolver) {
  41. super(fontFileURI, in, resolver);
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. protected void read() throws IOException {
  47. TTFFile ttf = new TTFFile();
  48. FontFileReader reader = new FontFileReader(in);
  49. boolean supported = ttf.readFont(reader, null);
  50. if (!supported) {
  51. throw new IOException("Could not load TrueType font: " + fontFileURI);
  52. }
  53. if (ttf.isCFF()) {
  54. throw new UnsupportedOperationException(
  55. "OpenType fonts with CFF data are not supported, yet");
  56. }
  57. multiFont = new MultiByteFont();
  58. multiFont.setResolver(this.resolver);
  59. returnFont = multiFont;
  60. returnFont.setFontName(ttf.getPostScriptName());
  61. returnFont.setFullName(ttf.getFullName());
  62. returnFont.setFamilyNames(ttf.getFamilyNames());
  63. returnFont.setFontSubFamilyName(ttf.getSubFamilyName());
  64. //multiFont.setTTCName(ttcName)
  65. returnFont.setCapHeight(ttf.getCapHeight());
  66. returnFont.setXHeight(ttf.getXHeight());
  67. returnFont.setAscender(ttf.getLowerCaseAscent());
  68. returnFont.setDescender(ttf.getLowerCaseDescent());
  69. returnFont.setFontBBox(ttf.getFontBBox());
  70. //returnFont.setFirstChar(ttf.getFirstChar();)
  71. returnFont.setFlags(ttf.getFlags());
  72. returnFont.setStemV(Integer.parseInt(ttf.getStemV())); //not used for TTF
  73. returnFont.setItalicAngle(Integer.parseInt(ttf.getItalicAngle()));
  74. returnFont.setMissingWidth(0);
  75. multiFont.setCIDType(CIDFontType.CIDTYPE2);
  76. int[] wx = ttf.getWidths();
  77. multiFont.setWidthArray(wx);
  78. List entries = ttf.getCMaps();
  79. BFEntry[] bfentries = new BFEntry[entries.size()];
  80. int pos = 0;
  81. Iterator iter = ttf.getCMaps().listIterator();
  82. while (iter.hasNext()) {
  83. TTFCmapEntry ce = (TTFCmapEntry)iter.next();
  84. bfentries[pos] = new BFEntry(ce.getUnicodeStart(), ce.getUnicodeEnd(),
  85. ce.getGlyphStartIndex());
  86. pos++;
  87. }
  88. multiFont.setBFEntries(bfentries);
  89. copyKerning(ttf, true);
  90. multiFont.setEmbedFileName(this.fontFileURI);
  91. loaded = true;
  92. }
  93. /**
  94. * Copy kerning information.
  95. */
  96. private void copyKerning(TTFFile ttf, boolean isCid) {
  97. // Get kerning
  98. Iterator iter;
  99. if (isCid) {
  100. iter = ttf.getKerning().keySet().iterator();
  101. } else {
  102. iter = ttf.getAnsiKerning().keySet().iterator();
  103. }
  104. while (iter.hasNext()) {
  105. Integer kpx1 = (Integer)iter.next();
  106. Map h2;
  107. if (isCid) {
  108. h2 = (Map)ttf.getKerning().get(kpx1);
  109. } else {
  110. h2 = (Map)ttf.getAnsiKerning().get(kpx1);
  111. }
  112. returnFont.putKerningEntry(kpx1, h2);
  113. }
  114. }
  115. }