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

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