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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 String ttcName = null;
  28. private String encoding = "Identity-H";
  29. private int defaultWidth = 0;
  30. private CIDFontType cidType = CIDFontType.CIDTYPE2;
  31. private String namePrefix = null; // Quasi unique prefix
  32. private CIDSubset subset = new CIDSubset();
  33. /** A map from Unicode indices to glyph indices */
  34. private BFEntry[] bfentries = null;
  35. /**
  36. * Default constructor
  37. */
  38. public MultiByteFont() {
  39. // Make sure that the 3 first glyphs are included
  40. subset.setupFirstThreeGlyphs();
  41. // Create a quasiunique prefix for fontname
  42. synchronized (this.getClass()) {
  43. uniqueCounter++;
  44. if (uniqueCounter > 99999 || uniqueCounter < 0) {
  45. uniqueCounter = 0; //We need maximum 5 character then we start again
  46. }
  47. }
  48. DecimalFormat counterFormat = new DecimalFormat("00000");
  49. String cntString = counterFormat.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 = SingleByteEncoding.NOT_FOUND_CODE_POINT;
  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 (glyphIndex == SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  149. warnMissingGlyph(c);
  150. glyphIndex = findGlyphIndex(Typeface.NOT_FOUND);
  151. }
  152. if (isEmbeddable()) {
  153. glyphIndex = subset.mapSubsetChar(glyphIndex, c);
  154. }
  155. return (char)glyphIndex;
  156. }
  157. /** {@inheritDoc} */
  158. public boolean hasChar(char c) {
  159. return (findGlyphIndex(c) != SingleByteEncoding.NOT_FOUND_CODE_POINT);
  160. }
  161. /**
  162. * Sets the array of BFEntry instances which constitutes the Unicode to glyph index map for
  163. * a font. ("BF" means "base font")
  164. * @param entries the Unicode to glyph index map
  165. */
  166. public void setBFEntries(BFEntry[] entries) {
  167. this.bfentries = entries;
  168. }
  169. /**
  170. * Sets the defaultWidth.
  171. * @param defaultWidth The defaultWidth to set
  172. */
  173. public void setDefaultWidth(int defaultWidth) {
  174. this.defaultWidth = defaultWidth;
  175. }
  176. /**
  177. * Returns the TrueType Collection Name.
  178. * @return the TrueType Collection Name
  179. */
  180. public String getTTCName() {
  181. return ttcName;
  182. }
  183. /**
  184. * Sets the the TrueType Collection Name.
  185. * @param ttcName the TrueType Collection Name
  186. */
  187. public void setTTCName(String ttcName) {
  188. this.ttcName = ttcName;
  189. }
  190. /**
  191. * Sets the width array.
  192. * @param wds array of widths.
  193. */
  194. public void setWidthArray(int[] wds) {
  195. this.width = wds;
  196. }
  197. /**
  198. * Returns a Map of used Glyphs.
  199. * @return Map Map of used Glyphs
  200. */
  201. public Map getUsedGlyphs() {
  202. return subset.getSubsetGlyphs();
  203. }
  204. /** {@inheritDoc} */
  205. public char[] getCharsUsed() {
  206. if (!isEmbeddable()) {
  207. return null;
  208. }
  209. return subset.getSubsetChars();
  210. }
  211. }