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.

MultiByteFont.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. //Java
  20. import java.text.DecimalFormat;
  21. import java.util.Map;
  22. /**
  23. * Generic MultiByte (CID) font
  24. */
  25. public class MultiByteFont extends CIDFont {
  26. private static int uniqueCounter = -1;
  27. private static final DecimalFormat COUNTER_FORMAT = new DecimalFormat("00000");
  28. private String ttcName = null;
  29. private String encoding = "Identity-H";
  30. private int defaultWidth = 0;
  31. private CIDFontType cidType = CIDFontType.CIDTYPE2;
  32. private String namePrefix = null; // Quasi unique prefix
  33. private CIDSubset subset = new CIDSubset();
  34. /** A map from Unicode indices to glyph indices */
  35. private BFEntry[] bfentries = null;
  36. /**
  37. * Default constructor
  38. */
  39. public MultiByteFont() {
  40. // Make sure that the 3 first glyphs are included
  41. subset.setupFirstThreeGlyphs();
  42. // Create a quasiunique prefix for fontname
  43. synchronized (this.getClass()) {
  44. uniqueCounter++;
  45. if (uniqueCounter > 99999 || uniqueCounter < 0) {
  46. uniqueCounter = 0; //We need maximum 5 character then we start again
  47. }
  48. }
  49. String cntString = COUNTER_FORMAT.format(uniqueCounter);
  50. //Subset prefix as described in chapter 5.5.3 of PDF 1.4
  51. StringBuffer sb = new StringBuffer("E");
  52. for (int i = 0, c = cntString.length(); i < c; i++) {
  53. //translate numbers to uppercase characters
  54. sb.append((char)(cntString.charAt(i) + (65 - 48)));
  55. }
  56. sb.append("+");
  57. namePrefix = sb.toString();
  58. setFontType(FontType.TYPE0);
  59. }
  60. /** {@inheritDoc} */
  61. public int getDefaultWidth() {
  62. return defaultWidth;
  63. }
  64. /** {@inheritDoc} */
  65. public String getRegistry() {
  66. return "Adobe";
  67. }
  68. /** {@inheritDoc} */
  69. public String getOrdering() {
  70. return "UCS";
  71. }
  72. /** {@inheritDoc} */
  73. public int getSupplement() {
  74. return 0;
  75. }
  76. /** {@inheritDoc} */
  77. public CIDFontType getCIDType() {
  78. return cidType;
  79. }
  80. /**
  81. * Sets the CIDType.
  82. * @param cidType The cidType to set
  83. */
  84. public void setCIDType(CIDFontType cidType) {
  85. this.cidType = cidType;
  86. }
  87. private String getPrefixedFontName() {
  88. return namePrefix + FontUtil.stripWhiteSpace(super.getFontName());
  89. }
  90. /** {@inheritDoc} */
  91. public String getEmbedFontName() {
  92. if (isEmbeddable()) {
  93. return getPrefixedFontName();
  94. } else {
  95. return super.getFontName();
  96. }
  97. }
  98. /** {@inheritDoc} */
  99. public boolean isEmbeddable() {
  100. return !(getEmbedFileName() == null && getEmbedResourceName() == null);
  101. }
  102. /** {@inheritDoc} */
  103. public CIDSubset getCIDSubset() {
  104. return this.subset;
  105. }
  106. /** {@inheritDoc} */
  107. public String getEncodingName() {
  108. return encoding;
  109. }
  110. /** {@inheritDoc} */
  111. public int getWidth(int i, int size) {
  112. if (isEmbeddable()) {
  113. int glyphIndex = subset.getGlyphIndexForSubsetIndex(i);
  114. return size * width[glyphIndex];
  115. } else {
  116. return size * width[i];
  117. }
  118. }
  119. /** {@inheritDoc} */
  120. public int[] getWidths() {
  121. int[] arr = new int[width.length];
  122. System.arraycopy(width, 0, arr, 0, width.length - 1);
  123. return arr;
  124. }
  125. /**
  126. * Returns the glyph index for a Unicode character. The method returns 0 if there's no
  127. * such glyph in the character map.
  128. * @param c the Unicode character index
  129. * @return the glyph index (or 0 if the glyph is not available)
  130. */
  131. private int findGlyphIndex(char c) {
  132. int idx = (int)c;
  133. int retIdx = 0; //.notdef
  134. for (int i = 0; (i < bfentries.length) && retIdx == 0; i++) {
  135. if (bfentries[i].getUnicodeStart() <= idx
  136. && bfentries[i].getUnicodeEnd() >= idx) {
  137. retIdx = bfentries[i].getGlyphStartIndex()
  138. + idx
  139. - bfentries[i].getUnicodeStart();
  140. }
  141. }
  142. return retIdx;
  143. }
  144. /** {@inheritDoc} */
  145. public char mapChar(char c) {
  146. notifyMapOperation();
  147. int glyphIndex = findGlyphIndex(c);
  148. if (isEmbeddable()) {
  149. glyphIndex = subset.mapSubsetChar(glyphIndex, c);
  150. }
  151. return (char)glyphIndex;
  152. }
  153. /** {@inheritDoc} */
  154. public boolean hasChar(char c) {
  155. return (findGlyphIndex(c) > 0);
  156. }
  157. /**
  158. * Sets the array of BFEntry instances which constitutes the Unicode to glyph index map for
  159. * a font. ("BF" means "base font")
  160. * @param entries the Unicode to glyph index map
  161. */
  162. public void setBFEntries(BFEntry[] entries) {
  163. this.bfentries = entries;
  164. }
  165. /**
  166. * Sets the defaultWidth.
  167. * @param defaultWidth The defaultWidth to set
  168. */
  169. public void setDefaultWidth(int defaultWidth) {
  170. this.defaultWidth = defaultWidth;
  171. }
  172. /**
  173. * Returns the TrueType Collection Name.
  174. * @return the TrueType Collection Name
  175. */
  176. public String getTTCName() {
  177. return ttcName;
  178. }
  179. /**
  180. * Sets the the TrueType Collection Name.
  181. * @param ttcName the TrueType Collection Name
  182. */
  183. public void setTTCName(String ttcName) {
  184. this.ttcName = ttcName;
  185. }
  186. /**
  187. * Sets the width array.
  188. * @param wds array of widths.
  189. */
  190. public void setWidthArray(int[] wds) {
  191. this.width = wds;
  192. }
  193. /**
  194. * Returns a Map of used Glyphs.
  195. * @return Map Map of used Glyphs
  196. */
  197. public Map getUsedGlyphs() {
  198. return subset.getSubsetGlyphs();
  199. }
  200. /** {@inheritDoc} */
  201. public char[] getCharsUsed() {
  202. if (!isEmbeddable()) {
  203. return null;
  204. }
  205. return subset.getSubsetChars();
  206. }
  207. }