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

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