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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. private final MultiByteFont font;
  48. public CIDSubset(MultiByteFont mbf) {
  49. font = mbf;
  50. // The zeroth value is reserved for .notdef
  51. usedGlyphs.put(0, 0);
  52. usedGlyphsIndex.put(0, 0);
  53. usedGlyphsCount++;
  54. }
  55. /** {@inheritDoc} */
  56. public int getOriginalGlyphIndex(int index) {
  57. Integer glyphIndex = usedGlyphsIndex.get(index);
  58. if (glyphIndex != null) {
  59. return glyphIndex;
  60. } else {
  61. return -1;
  62. }
  63. }
  64. /** {@inheritDoc} */
  65. public char getUnicode(int index) {
  66. Character mapValue = usedCharsIndex.get(index);
  67. if (mapValue != null) {
  68. return mapValue.charValue();
  69. } else {
  70. return CharUtilities.NOT_A_CHARACTER;
  71. }
  72. }
  73. /** {@inheritDoc} */
  74. public int mapChar(int glyphIndex, char unicode) {
  75. // Reencode to a new subset font or get the reencoded value
  76. // IOW, accumulate the accessed characters and build a character map for them
  77. Integer subsetCharSelector = usedGlyphs.get(glyphIndex);
  78. if (subsetCharSelector == null) {
  79. int selector = usedGlyphsCount;
  80. usedGlyphs.put(glyphIndex, selector);
  81. usedGlyphsIndex.put(selector, glyphIndex);
  82. usedCharsIndex.put(selector, unicode);
  83. usedGlyphsCount++;
  84. return selector;
  85. } else {
  86. return subsetCharSelector;
  87. }
  88. }
  89. /** {@inheritDoc} */
  90. public Map<Integer, Integer> getGlyphs() {
  91. return Collections.unmodifiableMap(this.usedGlyphs);
  92. }
  93. /** {@inheritDoc} */
  94. public char[] getChars() {
  95. char[] charArray = new char[usedGlyphsCount];
  96. for (int i = 0; i < usedGlyphsCount; i++) {
  97. charArray[i] = getUnicode(i);
  98. }
  99. return charArray;
  100. }
  101. /** {@inheritDoc} */
  102. public int getNumberOfGlyphs() {
  103. return this.usedGlyphsCount;
  104. }
  105. /** {@inheritDoc} */
  106. public BitSet getGlyphIndices() {
  107. BitSet bitset = new BitSet();
  108. for (Integer cid : usedGlyphs.keySet()) {
  109. bitset.set(cid);
  110. }
  111. return bitset;
  112. }
  113. /** {@inheritDoc} */
  114. public int[] getWidths() {
  115. int[] widths = font.getWidths();
  116. int[] tmpWidth = new int[getNumberOfGlyphs()];
  117. for (int i = 0, c = getNumberOfGlyphs(); i < c; i++) {
  118. int nwx = Math.max(0, getOriginalGlyphIndex(i));
  119. tmpWidth[i] = widths[nwx];
  120. }
  121. return tmpWidth;
  122. }
  123. }