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.

GlyfTable.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.util.HashSet;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import java.util.TreeSet;
  24. /**
  25. * This "glyf" table in a TrueType font file contains information that describes the glyphs. This
  26. * class is responsible for creating a subset of the "glyf" table given a set of glyph indices.
  27. */
  28. public class GlyfTable {
  29. private final OFMtxEntry[] mtxTab;
  30. private final long tableOffset;
  31. private final Set<Long> remappedComposites;
  32. protected final Map<Integer, Integer> subset;
  33. private final FontFileReader in;
  34. /** All the composite glyphs that appear in the subset. */
  35. private Set<Integer> compositeGlyphs = new TreeSet<Integer>();
  36. /** All the glyphs that are composed, but do not appear in the subset. */
  37. protected Set<Integer> composedGlyphs = new TreeSet<Integer>();
  38. protected GlyfTable(FontFileReader in, OFMtxEntry[] metrics, OFDirTabEntry dirTableEntry,
  39. Map<Integer, Integer> glyphs) throws IOException {
  40. mtxTab = metrics;
  41. tableOffset = dirTableEntry.getOffset();
  42. remappedComposites = new HashSet<Long>();
  43. this.subset = glyphs;
  44. this.in = in;
  45. }
  46. private static enum GlyfFlags {
  47. ARG_1_AND_2_ARE_WORDS(4, 2),
  48. ARGS_ARE_XY_VALUES,
  49. ROUND_XY_TO_GRID,
  50. WE_HAVE_A_SCALE(2),
  51. RESERVED,
  52. MORE_COMPONENTS,
  53. WE_HAVE_AN_X_AND_Y_SCALE(4),
  54. WE_HAVE_A_TWO_BY_TWO(8),
  55. WE_HAVE_INSTRUCTIONS,
  56. USE_MY_METRICS,
  57. OVERLAP_COMPOUND,
  58. SCALED_COMPONENT_OFFSET,
  59. UNSCALED_COMPONENT_OFFSET;
  60. private final int bitMask;
  61. private final int argsCountIfSet;
  62. private final int argsCountIfNotSet;
  63. private GlyfFlags(int argsCountIfSet, int argsCountIfNotSet) {
  64. this.bitMask = 1 << this.ordinal();
  65. this.argsCountIfSet = argsCountIfSet;
  66. this.argsCountIfNotSet = argsCountIfNotSet;
  67. }
  68. private GlyfFlags(int argsCountIfSet) {
  69. this(argsCountIfSet, 0);
  70. }
  71. private GlyfFlags() {
  72. this(0, 0);
  73. }
  74. /**
  75. * Calculates, from the given flags, the offset to the next glyph index.
  76. *
  77. * @param flags the glyph data flags
  78. * @return offset to the next glyph if any, or 0
  79. */
  80. static int getOffsetToNextComposedGlyf(int flags) {
  81. int offset = 0;
  82. for (GlyfFlags flag : GlyfFlags.values()) {
  83. offset += (flags & flag.bitMask) > 0 ? flag.argsCountIfSet : flag.argsCountIfNotSet;
  84. }
  85. return offset;
  86. }
  87. /**
  88. * Checks the given flags to see if there is another composed glyph.
  89. *
  90. * @param flags the glyph data flags
  91. * @return true if there is another composed glyph, otherwise false.
  92. */
  93. static boolean hasMoreComposites(int flags) {
  94. return (flags & MORE_COMPONENTS.bitMask) > 0;
  95. }
  96. }
  97. /**
  98. * Populates the map of subset glyphs with all the glyphs that compose the glyphs in the subset.
  99. * This also re-maps the indices of composed glyphs to their new index in the subset font.
  100. *
  101. * @throws IOException an I/O error
  102. */
  103. protected void populateGlyphsWithComposites() throws IOException {
  104. for (int indexInOriginal : subset.keySet()) {
  105. scanGlyphsRecursively(indexInOriginal);
  106. }
  107. addAllComposedGlyphsToSubset();
  108. for (int compositeGlyph : compositeGlyphs) {
  109. long offset = tableOffset + mtxTab[compositeGlyph].getOffset() + 10;
  110. if (!remappedComposites.contains(offset)) {
  111. remapComposite(offset);
  112. }
  113. }
  114. }
  115. /**
  116. * Scans each glyph for any composed glyphs. This populates <code>compositeGlyphs</code> with
  117. * all the composite glyphs being used in the subset. This also populates <code>newGlyphs</code>
  118. * with any new glyphs that are composed and do not appear in the subset of glyphs.
  119. *
  120. * For example the double quote mark (") is often composed of two apostrophes ('), if an
  121. * apostrophe doesn't appear in the glyphs in the subset, it will be included and will be added
  122. * to newGlyphs.
  123. *
  124. * @param indexInOriginal the index of the glyph to test from the original font
  125. * @throws IOException an I/O error
  126. */
  127. private void scanGlyphsRecursively(int indexInOriginal) throws IOException {
  128. if (!subset.containsKey(indexInOriginal)) {
  129. composedGlyphs.add(indexInOriginal);
  130. }
  131. if (isComposite(indexInOriginal)) {
  132. compositeGlyphs.add(indexInOriginal);
  133. Set<Integer> composedGlyphs = retrieveComposedGlyphs(indexInOriginal);
  134. for (Integer composedGlyph : composedGlyphs) {
  135. scanGlyphsRecursively(composedGlyph);
  136. }
  137. }
  138. }
  139. /**
  140. * Adds to the subset, all the glyphs that are composed by a glyph, but do not appear themselves
  141. * in the subset.
  142. */
  143. protected void addAllComposedGlyphsToSubset() {
  144. int newIndex = subset.size();
  145. for (int composedGlyph : composedGlyphs) {
  146. subset.put(composedGlyph, newIndex++);
  147. }
  148. }
  149. /**
  150. * Re-maps the index of composed glyphs in the original font to the index of the same glyph in
  151. * the subset font.
  152. *
  153. * @param glyphOffset the offset of the composite glyph
  154. * @throws IOException an I/O error
  155. */
  156. private void remapComposite(long glyphOffset) throws IOException {
  157. long currentGlyphOffset = glyphOffset;
  158. remappedComposites.add(currentGlyphOffset);
  159. int flags = 0;
  160. do {
  161. flags = in.readTTFUShort(currentGlyphOffset);
  162. int glyphIndex = in.readTTFUShort(currentGlyphOffset + 2);
  163. Integer indexInSubset = subset.get(glyphIndex);
  164. assert indexInSubset != null;
  165. /*
  166. * TODO: this should not be done here!! We're writing to the stream we're reading from,
  167. * this is asking for trouble! What should happen is when the glyph data is copied from
  168. * subset, the remapping should be done there. So the original stream is left untouched.
  169. */
  170. in.writeTTFUShort(currentGlyphOffset + 2, indexInSubset);
  171. currentGlyphOffset += 4 + GlyfFlags.getOffsetToNextComposedGlyf(flags);
  172. } while (GlyfFlags.hasMoreComposites(flags));
  173. }
  174. private boolean isComposite(int indexInOriginal) throws IOException {
  175. int numberOfContours = in.readTTFShort(tableOffset + mtxTab[indexInOriginal].getOffset());
  176. return numberOfContours < 0;
  177. }
  178. /**
  179. * Reads a composite glyph at a given index and retrieves all the glyph indices of contingent
  180. * composed glyphs.
  181. *
  182. * @param indexInOriginal the glyph index of the composite glyph
  183. * @return the set of glyph indices this glyph composes
  184. * @throws IOException an I/O error
  185. */
  186. private Set<Integer> retrieveComposedGlyphs(int indexInOriginal)
  187. throws IOException {
  188. Set<Integer> composedGlyphs = new HashSet<Integer>();
  189. long offset = tableOffset + mtxTab[indexInOriginal].getOffset() + 10;
  190. int flags = 0;
  191. do {
  192. flags = in.readTTFUShort(offset);
  193. composedGlyphs.add(in.readTTFUShort(offset + 2));
  194. offset += 4 + GlyfFlags.getOffsetToNextComposedGlyf(flags);
  195. } while (GlyfFlags.hasMoreComposites(flags));
  196. return composedGlyphs;
  197. }
  198. }