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.

CharacterSet.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.afp.fonts;
  19. import java.io.IOException;
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.Map;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.afp.AFPConstants;
  25. import org.apache.fop.afp.util.StringUtils;
  26. /**
  27. * The IBM Font Object Content Architecture (FOCA) supports presentation
  28. * of character shapes by defining their characteristics, which include
  29. * font description information for identifying the characters, font metric
  30. * information for positioning the characters, and character shape information
  31. * for presenting the character images.
  32. * <p/>
  33. * Presenting a graphic character on a presentation surface requires
  34. * information on the rotation and position of character on the physical
  35. * or logical page.
  36. * <p/>
  37. * This class proivdes font metric information for a particular font
  38. * as identified by the character set name. This information is obtained
  39. * directly from the AFP font files which must be installed in the path
  40. * specified in the afp-fonts xml definition file.
  41. * <p/>
  42. */
  43. public class CharacterSet {
  44. /** Static logging instance */
  45. protected static final Log log = LogFactory.getLog(CharacterSet.class.getName());
  46. /** default codepage */
  47. public static final String DEFAULT_CODEPAGE = "T1V10500";
  48. /** default encoding */
  49. public static final String DEFAULT_ENCODING = "Cp500";
  50. private static final int MAX_NAME_LEN = 8;
  51. /** The code page to which the character set relates */
  52. protected String codePage;
  53. /** The encoding used for the code page */
  54. protected String encoding;
  55. /** The character set relating to the font */
  56. protected String name;
  57. /** The path to the installed fonts */
  58. protected String path;
  59. /** Indicator as to whether to metrics have been loaded */
  60. private boolean isMetricsLoaded = false;
  61. /** The current orientation (currently only 0 is supported by FOP) */
  62. private final String currentOrientation = "0";
  63. /** The collection of objects for each orientation */
  64. private Map characterSetOrientations = null;
  65. /**
  66. * Constructor for the CharacterSetMetric object, the character set is used
  67. * to load the font information from the actual AFP font.
  68. *
  69. * @param codePage the code page identifier
  70. * @param encoding the encoding of the font
  71. * @param name the character set name
  72. * @param path the path to the installed afp fonts
  73. */
  74. public CharacterSet(String codePage, String encoding, String name, String path) {
  75. if (name.length() > MAX_NAME_LEN) {
  76. String msg = "Character set name '" + name + "' must be a maximum of "
  77. + MAX_NAME_LEN + " characters";
  78. log.error("Constructor:: " + msg);
  79. throw new IllegalArgumentException(msg);
  80. }
  81. if (name.length() < MAX_NAME_LEN) {
  82. this.name = StringUtils.rpad(name, ' ', MAX_NAME_LEN);
  83. } else {
  84. this.name = name;
  85. }
  86. this.codePage = codePage;
  87. this.encoding = encoding;
  88. this.path = path;
  89. this.characterSetOrientations = new java.util.HashMap(4);
  90. }
  91. /**
  92. * Add character set metric information for the different orientations
  93. *
  94. * @param cso the metrics for the orientation
  95. */
  96. public void addCharacterSetOrientation(CharacterSetOrientation cso) {
  97. characterSetOrientations.put(
  98. String.valueOf(cso.getOrientation()),
  99. cso);
  100. }
  101. /**
  102. * Ascender height is the distance from the character baseline to the
  103. * top of the character box. A negative ascender height signifies that
  104. * all of the graphic character is below the character baseline. For
  105. * a character rotation other than 0, ascender height loses its
  106. * meaning when the character is lying on its side or is upside down
  107. * with respect to normal viewing orientation. For the general case,
  108. * Ascender Height is the characters most positive y-axis value.
  109. * For bounded character boxes, for a given character having an
  110. * ascender, ascender height and baseline offset are equal.
  111. *
  112. * @return the ascender value in millipoints
  113. */
  114. public int getAscender() {
  115. load();
  116. return getCharacterSetOrientation().getAscender();
  117. }
  118. /**
  119. * Cap height is the average height of the uppercase characters in
  120. * a font. This value is specified by the designer of a font and is
  121. * usually the height of the uppercase M.
  122. *
  123. * @return the cap height value in millipoints
  124. */
  125. public int getCapHeight() {
  126. load();
  127. return getCharacterSetOrientation().getCapHeight();
  128. }
  129. /**
  130. * Descender depth is the distance from the character baseline to
  131. * the bottom of a character box. A negative descender depth signifies
  132. * that all of the graphic character is above the character baseline.
  133. *
  134. * @return the descender value in millipoints
  135. */
  136. public int getDescender() {
  137. load();
  138. return getCharacterSetOrientation().getDescender();
  139. }
  140. /**
  141. * Returns the first character in the character set
  142. *
  143. * @return the first character in the character set
  144. */
  145. public int getFirstChar() {
  146. load();
  147. return getCharacterSetOrientation().getFirstChar();
  148. }
  149. /**
  150. * Returns the last character in the character set
  151. *
  152. * @return the last character in the character set
  153. */
  154. public int getLastChar() {
  155. load();
  156. return getCharacterSetOrientation().getLastChar();
  157. }
  158. /**
  159. * Returns the path where the font resources are installed
  160. *
  161. * @return the path where the font resources are installed
  162. */
  163. public String getPath() {
  164. return path;
  165. }
  166. /**
  167. * Get the width (in 1/1000ths of a point size) of all characters
  168. *
  169. * @return the widths of all characters
  170. */
  171. public int[] getWidths() {
  172. load();
  173. return getCharacterSetOrientation().getWidths();
  174. }
  175. /**
  176. * XHeight refers to the height of the lower case letters above the baseline.
  177. *
  178. * @return the typical height of characters
  179. */
  180. public int getXHeight() {
  181. load();
  182. return getCharacterSetOrientation().getXHeight();
  183. }
  184. /**
  185. * Get the width (in 1/1000ths of a point size) of the character
  186. * identified by the parameter passed.
  187. *
  188. * @param character the character from which the width will be calculated
  189. * @return the width of the character
  190. */
  191. public int getWidth(int character) {
  192. load();
  193. return getCharacterSetOrientation().getWidth(character);
  194. }
  195. /**
  196. * Lazy creation of the character metrics, the afp font file will only
  197. * be processed on a method call requiring the metric information.
  198. */
  199. private void load() {
  200. if (!isMetricsLoaded) {
  201. AFPFontReader afpFontReader = new AFPFontReader();
  202. try {
  203. afpFontReader.loadCharacterSetMetric(this);
  204. isMetricsLoaded = true;
  205. } catch (IOException e) {
  206. String msg = "Failed to load the character set metrics for code page " + codePage;
  207. log.error(msg);
  208. throw new RuntimeException(e.getMessage());
  209. }
  210. }
  211. }
  212. /**
  213. * Returns the AFP character set identifier
  214. *
  215. * @return the AFP character set identifier
  216. */
  217. public String getName() {
  218. return name;
  219. }
  220. /**
  221. * Returns the AFP character set identifier as a byte array
  222. *
  223. * @return the AFP character set identifier as a byte array
  224. */
  225. public byte[] getNameBytes() {
  226. byte[] nameBytes = null;
  227. try {
  228. nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
  229. } catch (UnsupportedEncodingException usee) {
  230. nameBytes = name.getBytes();
  231. log.warn(
  232. "UnsupportedEncodingException translating the name " + name);
  233. }
  234. return nameBytes;
  235. }
  236. /**
  237. * Returns the AFP code page identifier
  238. *
  239. * @return the AFP code page identifier
  240. */
  241. public String getCodePage() {
  242. return codePage;
  243. }
  244. /**
  245. * Returns the AFP code page encoding
  246. *
  247. * @return the AFP code page encoding
  248. */
  249. public String getEncoding() {
  250. return encoding;
  251. }
  252. /**
  253. * Helper method to return the current CharacterSetOrientation, note
  254. * that FOP does not yet implement the "reference-orientation"
  255. * attribute therefore we always use the orientation zero degrees,
  256. * Other orientation information is captured for use by a future
  257. * implementation (whenever FOP implement the mechanism). This is also
  258. * the case for landscape prints which use an orientation of 270 degrees,
  259. * in 99.9% of cases the font metrics will be the same as the 0 degrees
  260. * therefore the implementation currently will always use 0 degrees.
  261. *
  262. * @return characterSetOrentation The current orientation metrics.
  263. */
  264. private CharacterSetOrientation getCharacterSetOrientation() {
  265. CharacterSetOrientation c
  266. = (CharacterSetOrientation) characterSetOrientations.get(currentOrientation);
  267. return c;
  268. }
  269. /**
  270. * Map a Unicode character to a code point in the font.
  271. * The code tables are already converted to Unicode therefore
  272. * we can use the identity mapping.
  273. *
  274. * @param c character to map
  275. * @return the mapped character
  276. */
  277. public char mapChar(char c) {
  278. return c;
  279. }
  280. }