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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.HashMap;
  22. import java.util.LinkedHashMap;
  23. import java.util.Map;
  24. import org.apache.fop.util.CharUtilities;
  25. /**
  26. * Provides methods to get font information.
  27. * Naming:
  28. * glyph index: original index of the glyph in the non-subset font (!= unicode index)
  29. * character selector: index into a set of glyphs. For subset CID fonts, this starts at 0. For non-subset
  30. * fonts, this is the same as the glyph index.
  31. * Unicode index: The Unicode codepoint of a character.
  32. * Glyph name: the Adobe glyph name (as found in Glyphs.java)
  33. */
  34. public class CIDSubset implements CIDSet {
  35. /**
  36. * usedGlyphs contains orginal, new glyph index (glyph index -> char selector)
  37. */
  38. private Map<Integer, Integer> usedGlyphs = new LinkedHashMap<Integer, Integer>();
  39. /**
  40. * usedGlyphsIndex contains new glyph, original index (char selector -> glyph index)
  41. */
  42. private Map<Integer, Integer> usedGlyphsIndex = new HashMap<Integer, Integer>();
  43. private int usedGlyphsCount;
  44. /**
  45. * usedCharsIndex contains new glyph, original char (char selector -> Unicode)
  46. */
  47. private Map<Integer, Integer> usedCharsIndex = new HashMap<Integer, Integer>();
  48. /**
  49. * A map between the original character and it's GID in the original font.
  50. */
  51. private Map<Integer, Integer> charToGIDs = new HashMap<Integer, Integer>();
  52. private final MultiByteFont font;
  53. public CIDSubset(MultiByteFont mbf) {
  54. font = mbf;
  55. // The zeroth value is reserved for .notdef
  56. usedGlyphs.put(0, 0);
  57. usedGlyphsIndex.put(0, 0);
  58. usedGlyphsCount++;
  59. }
  60. /** {@inheritDoc} */
  61. public int getOriginalGlyphIndex(int index) {
  62. Integer glyphIndex = usedGlyphsIndex.get(index);
  63. if (glyphIndex != null) {
  64. return glyphIndex;
  65. } else {
  66. return -1;
  67. }
  68. }
  69. /** {@inheritDoc} */
  70. public int getUnicode(int index) {
  71. Integer mapValue = usedCharsIndex.get(index);
  72. if (mapValue != null) {
  73. return mapValue;
  74. } else {
  75. return CharUtilities.NOT_A_CHARACTER;
  76. }
  77. }
  78. /** {@inheritDoc} */
  79. public int mapChar(int glyphIndex, char unicode) {
  80. return mapCodePoint(glyphIndex, unicode);
  81. }
  82. /** {@inheritDoc} */
  83. public int mapCodePoint(int glyphIndex, int codePoint) {
  84. // Reencode to a new subset font or get the reencoded value
  85. // IOW, accumulate the accessed characters and build a character map for them
  86. Integer subsetCharSelector = usedGlyphs.get(glyphIndex);
  87. if (subsetCharSelector == null) {
  88. int selector = usedGlyphsCount;
  89. usedGlyphs.put(glyphIndex, selector);
  90. usedGlyphsIndex.put(selector, glyphIndex);
  91. usedCharsIndex.put(selector, codePoint);
  92. charToGIDs.put(codePoint, glyphIndex);
  93. usedGlyphsCount++;
  94. return selector;
  95. } else {
  96. return subsetCharSelector;
  97. }
  98. }
  99. /** {@inheritDoc} */
  100. public Map<Integer, Integer> getGlyphs() {
  101. return Collections.unmodifiableMap(this.usedGlyphs);
  102. }
  103. /** {@inheritDoc} */
  104. public char getUnicodeFromGID(int glyphIndex) {
  105. // TODO this method is never called in the MultiByte font path.
  106. // This is why we can safely cast the value of usedCharsIndex.get(selector)
  107. // to int . BTW is a question if it should be changed to int as getUnicode
  108. // or left like this.
  109. int selector = usedGlyphs.get(glyphIndex);
  110. return (char) usedCharsIndex.get(selector).intValue();
  111. }
  112. /** {@inheritDoc} */
  113. public int getGIDFromChar(char ch) {
  114. return charToGIDs.get((int) ch);
  115. }
  116. /** {@inheritDoc} */
  117. public char[] getChars() {
  118. StringBuilder buf = new StringBuilder();
  119. for (int i = 0; i < usedGlyphsCount; i++) {
  120. buf.appendCodePoint(getUnicode(i));
  121. }
  122. return buf.toString().toCharArray();
  123. }
  124. /** {@inheritDoc} */
  125. public int getNumberOfGlyphs() {
  126. return this.usedGlyphsCount;
  127. }
  128. /** {@inheritDoc} */
  129. public BitSet getGlyphIndices() {
  130. BitSet bitset = new BitSet();
  131. for (Integer cid : usedGlyphs.keySet()) {
  132. bitset.set(cid);
  133. }
  134. return bitset;
  135. }
  136. /** {@inheritDoc} */
  137. public int[] getWidths() {
  138. int[] widths = font.getWidths();
  139. int[] tmpWidth = new int[getNumberOfGlyphs()];
  140. for (int i = 0, c = getNumberOfGlyphs(); i < c; i++) {
  141. int nwx = Math.max(0, getOriginalGlyphIndex(i));
  142. tmpWidth[i] = widths[nwx];
  143. }
  144. return tmpWidth;
  145. }
  146. }