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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.util.Map;
  21. /**
  22. * Generic MultiByte (CID) font
  23. */
  24. public class MultiByteFont extends CIDFont {
  25. private String ttcName = null;
  26. private String encoding = "Identity-H";
  27. private int defaultWidth = 0;
  28. private CIDFontType cidType = CIDFontType.CIDTYPE2;
  29. private CIDSubset subset = new CIDSubset();
  30. /** A map from Unicode indices to glyph indices */
  31. private BFEntry[] bfentries = null;
  32. /**
  33. * Default constructor
  34. */
  35. public MultiByteFont() {
  36. subset.setupFirstGlyph();
  37. setFontType(FontType.TYPE0);
  38. }
  39. /** {@inheritDoc} */
  40. public int getDefaultWidth() {
  41. return defaultWidth;
  42. }
  43. /** {@inheritDoc} */
  44. public String getRegistry() {
  45. return "Adobe";
  46. }
  47. /** {@inheritDoc} */
  48. public String getOrdering() {
  49. return "UCS";
  50. }
  51. /** {@inheritDoc} */
  52. public int getSupplement() {
  53. return 0;
  54. }
  55. /** {@inheritDoc} */
  56. public CIDFontType getCIDType() {
  57. return cidType;
  58. }
  59. /**
  60. * Sets the CIDType.
  61. * @param cidType The cidType to set
  62. */
  63. public void setCIDType(CIDFontType cidType) {
  64. this.cidType = cidType;
  65. }
  66. /** {@inheritDoc} */
  67. public String getEmbedFontName() {
  68. if (isEmbeddable()) {
  69. return FontUtil.stripWhiteSpace(super.getFontName());
  70. } else {
  71. return super.getFontName();
  72. }
  73. }
  74. /** {@inheritDoc} */
  75. public boolean isEmbeddable() {
  76. return !(getEmbedFileName() == null && getEmbedResourceName() == null);
  77. }
  78. /** {@inheritDoc} */
  79. public boolean isSubsetEmbedded() {
  80. return true;
  81. }
  82. /** {@inheritDoc} */
  83. public CIDSubset getCIDSubset() {
  84. return this.subset;
  85. }
  86. /** {@inheritDoc} */
  87. public String getEncodingName() {
  88. return encoding;
  89. }
  90. /** {@inheritDoc} */
  91. public int getWidth(int i, int size) {
  92. if (isEmbeddable()) {
  93. int glyphIndex = subset.getGlyphIndexForSubsetIndex(i);
  94. return size * width[glyphIndex];
  95. } else {
  96. return size * width[i];
  97. }
  98. }
  99. /** {@inheritDoc} */
  100. public int[] getWidths() {
  101. int[] arr = new int[width.length];
  102. System.arraycopy(width, 0, arr, 0, width.length);
  103. return arr;
  104. }
  105. /**
  106. * Returns the glyph index for a Unicode character. The method returns 0 if there's no
  107. * such glyph in the character map.
  108. * @param c the Unicode character index
  109. * @return the glyph index (or 0 if the glyph is not available)
  110. */
  111. private int findGlyphIndex(char c) {
  112. int idx = (int)c;
  113. int retIdx = SingleByteEncoding.NOT_FOUND_CODE_POINT;
  114. for (int i = 0; (i < bfentries.length) && retIdx == 0; i++) {
  115. if (bfentries[i].getUnicodeStart() <= idx
  116. && bfentries[i].getUnicodeEnd() >= idx) {
  117. retIdx = bfentries[i].getGlyphStartIndex()
  118. + idx
  119. - bfentries[i].getUnicodeStart();
  120. }
  121. }
  122. return retIdx;
  123. }
  124. /** {@inheritDoc} */
  125. public char mapChar(char c) {
  126. notifyMapOperation();
  127. int glyphIndex = findGlyphIndex(c);
  128. if (glyphIndex == SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  129. warnMissingGlyph(c);
  130. glyphIndex = findGlyphIndex(Typeface.NOT_FOUND);
  131. }
  132. if (isEmbeddable()) {
  133. glyphIndex = subset.mapSubsetChar(glyphIndex, c);
  134. }
  135. return (char)glyphIndex;
  136. }
  137. /** {@inheritDoc} */
  138. public boolean hasChar(char c) {
  139. return (findGlyphIndex(c) != SingleByteEncoding.NOT_FOUND_CODE_POINT);
  140. }
  141. /**
  142. * Sets the array of BFEntry instances which constitutes the Unicode to glyph index map for
  143. * a font. ("BF" means "base font")
  144. * @param entries the Unicode to glyph index map
  145. */
  146. public void setBFEntries(BFEntry[] entries) {
  147. this.bfentries = entries;
  148. }
  149. /**
  150. * Sets the defaultWidth.
  151. * @param defaultWidth The defaultWidth to set
  152. */
  153. public void setDefaultWidth(int defaultWidth) {
  154. this.defaultWidth = defaultWidth;
  155. }
  156. /**
  157. * Returns the TrueType Collection Name.
  158. * @return the TrueType Collection Name
  159. */
  160. public String getTTCName() {
  161. return ttcName;
  162. }
  163. /**
  164. * Sets the the TrueType Collection Name.
  165. * @param ttcName the TrueType Collection Name
  166. */
  167. public void setTTCName(String ttcName) {
  168. this.ttcName = ttcName;
  169. }
  170. /**
  171. * Sets the width array.
  172. * @param wds array of widths.
  173. */
  174. public void setWidthArray(int[] wds) {
  175. this.width = wds;
  176. }
  177. /**
  178. * Returns a Map of used Glyphs.
  179. * @return Map Map of used Glyphs
  180. */
  181. public Map<Integer, Integer> getUsedGlyphs() {
  182. return subset.getSubsetGlyphs();
  183. }
  184. /** @return an array of the chars used */
  185. public char[] getCharsUsed() {
  186. if (!isEmbeddable()) {
  187. return null;
  188. }
  189. return subset.getSubsetChars();
  190. }
  191. }