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.

CIDSubset.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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;
  19. import java.util.BitSet;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.Map;
  23. import org.apache.fop.util.CharUtilities;
  24. //Naming:
  25. //glyph index: original index of the glyph in the non-subset font (!= unicode index)
  26. //character selector: index into a set of glyphs. For subset CID fonts, this starts at 0. For
  27. // non-subset fonts, this is the same as the glyph index.
  28. //Unicode index: The Unicode codepoint of a character.
  29. //Glyph name: the Adobe glyph name (as found in Glyphs.java)
  30. /**
  31. * Keeps track of the glyphs used in a document. This information is later used to build
  32. * a subset of a font.
  33. */
  34. public class CIDSubset {
  35. /**
  36. * usedGlyphs contains orginal, new glyph index (glyph index -> char selector)
  37. */
  38. private Map/*<Integer, Integer>*/ usedGlyphs = new java.util.HashMap();
  39. /**
  40. * usedGlyphsIndex contains new glyph, original index (char selector -> glyph index)
  41. */
  42. private Map/*<Integer, Integer>*/ usedGlyphsIndex = new java.util.HashMap();
  43. private int usedGlyphsCount = 0;
  44. /**
  45. * usedCharsIndex contains new glyph, original char (char selector -> Unicode)
  46. */
  47. private Map/*<Integer, Character>*/ usedCharsIndex = new java.util.HashMap();
  48. public CIDSubset() {
  49. }
  50. /**
  51. * Adds the initial 3 glyphs which are the same for all CID subsets.
  52. */
  53. public void setupFirstThreeGlyphs() {
  54. // Make sure that the 3 first glyphs are included
  55. usedGlyphs.put(new Integer(0), new Integer(0));
  56. usedGlyphsIndex.put(new Integer(0), new Integer(0));
  57. usedGlyphsCount++;
  58. usedGlyphs.put(new Integer(1), new Integer(1));
  59. usedGlyphsIndex.put(new Integer(1), new Integer(1));
  60. usedGlyphsCount++;
  61. usedGlyphs.put(new Integer(2), new Integer(2));
  62. usedGlyphsIndex.put(new Integer(2), new Integer(2));
  63. usedGlyphsCount++;
  64. }
  65. /**
  66. * Returns the original index of the glyph inside the (non-subset) font's glyph list. This
  67. * index can be used to access the character width information, for example.
  68. * @param subsetIndex the subset index (character selector) to access the glyph
  69. * @return the original index (or -1 if no glyph index is available for the subset index)
  70. */
  71. public int getGlyphIndexForSubsetIndex(int subsetIndex) {
  72. Integer glyphIndex = (Integer)usedGlyphsIndex.get(new Integer(subsetIndex));
  73. if (glyphIndex != null) {
  74. return glyphIndex.intValue();
  75. } else {
  76. return -1;
  77. }
  78. }
  79. /**
  80. * Returns the Unicode value for a subset index (character selector). If there's no such
  81. * Unicode value, the "NOT A CHARACTER" (0xFFFF) is returned.
  82. * @param subsetIndex the subset index (character selector)
  83. * @return the Unicode value or "NOT A CHARACTER" (0xFFFF)
  84. */
  85. public char getUnicodeForSubsetIndex(int subsetIndex) {
  86. Character mapValue = (Character)usedCharsIndex.get(new Integer(subsetIndex));
  87. if (mapValue != null) {
  88. return mapValue.charValue();
  89. } else {
  90. return CharUtilities.NOT_A_CHARACTER;
  91. }
  92. }
  93. /**
  94. * Maps a character to a character selector for a font subset. If the character isn't in the
  95. * subset, yet, it is added and a new character selector returned. Otherwise, the already
  96. * allocated character selector is returned from the existing map/subset.
  97. * @param glyphIndex the glyph index of the character
  98. * @param unicode the Unicode index of the character
  99. * @return the subset index
  100. */
  101. public int mapSubsetChar(int glyphIndex, char unicode) {
  102. // Reencode to a new subset font or get the reencoded value
  103. // IOW, accumulate the accessed characters and build a character map for them
  104. Integer subsetCharSelector = (Integer)usedGlyphs.get(new Integer(glyphIndex));
  105. if (subsetCharSelector == null) {
  106. int selector = usedGlyphsCount;
  107. usedGlyphs.put(new Integer(glyphIndex),
  108. new Integer(selector));
  109. usedGlyphsIndex.put(new Integer(selector),
  110. new Integer(glyphIndex));
  111. usedCharsIndex.put(new Integer(selector),
  112. new Character(unicode));
  113. usedGlyphsCount++;
  114. return selector;
  115. } else {
  116. return subsetCharSelector.intValue();
  117. }
  118. }
  119. /**
  120. * Returns an unmodifiable Map of the font subset. It maps from glyph index to
  121. * character selector (i.e. the subset index in this case).
  122. * @return Map Map&lt;Integer, Integer&gt; of the font subset
  123. */
  124. public Map/*<Integer, Integer>*/ getSubsetGlyphs() {
  125. return Collections.unmodifiableMap(this.usedGlyphs);
  126. }
  127. /**
  128. * Returns a char array containing all Unicode characters that are in the subset.
  129. * @return a char array with all used Unicode characters
  130. */
  131. public char[] getSubsetChars() {
  132. char[] charArray = new char[usedGlyphsCount];
  133. for (int i = 0; i < usedGlyphsCount; i++) {
  134. charArray[i] = getUnicodeForSubsetIndex(i);
  135. }
  136. return charArray;
  137. }
  138. /**
  139. * Returns the number of glyphs in the subset.
  140. * @return the number of glyphs in the subset
  141. */
  142. public int getSubsetSize() {
  143. return this.usedGlyphsCount;
  144. }
  145. /**
  146. * Returns a BitSet with bits set for each available glyph index in the subset.
  147. * @return a BitSet indicating available glyph indices
  148. */
  149. public BitSet getGlyphIndexBitSet() {
  150. BitSet bitset = new BitSet();
  151. Iterator iter = usedGlyphsIndex.keySet().iterator();
  152. while (iter.hasNext()) {
  153. Integer cid = (Integer)iter.next();
  154. bitset.set(cid.intValue());
  155. }
  156. return bitset;
  157. }
  158. }