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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.awt.Rectangle;
  20. import java.io.UnsupportedEncodingException;
  21. import java.nio.charset.CharacterCodingException;
  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.AFPEventProducer;
  26. import org.apache.fop.afp.fonts.CharactersetEncoder.EncodedChars;
  27. import org.apache.fop.afp.util.AFPResourceAccessor;
  28. import org.apache.fop.afp.util.StringUtils;
  29. /**
  30. * The IBM Font Object Content Architecture (FOCA) supports presentation
  31. * of character shapes by defining their characteristics, which include
  32. * font description information for identifying the characters, font metric
  33. * information for positioning the characters, and character shape information
  34. * for presenting the character images.
  35. * <br>
  36. * Presenting a graphic character on a presentation surface requires
  37. * information on the rotation and position of character on the physical
  38. * or logical page.
  39. * <br>
  40. * This class proivdes font metric information for a particular font
  41. * as identified by the character set name. This information is obtained
  42. * directly from the AFP font files which must be installed in the path
  43. * specified in the afp-fonts xml definition file.
  44. * <br>
  45. */
  46. public class CharacterSet {
  47. /** Static logging instance */
  48. protected static final Log LOG = LogFactory.getLog(CharacterSet.class.getName());
  49. /** default codepage */
  50. public static final String DEFAULT_CODEPAGE = "T1V10500";
  51. /** default encoding */
  52. public static final String DEFAULT_ENCODING = "Cp500";
  53. private static final int MAX_NAME_LEN = 8;
  54. /** The current orientation (currently only 0 is supported by FOP) */
  55. public static final int SUPPORTED_ORIENTATION = 0;
  56. /** The code page to which the character set relates */
  57. protected final String codePage;
  58. /** The encoding used for the code page */
  59. protected final String encoding;
  60. /** The characterset encoder corresponding to this encoding */
  61. private final CharactersetEncoder encoder;
  62. /** The character set relating to the font */
  63. protected final String name;
  64. /** The path to the installed fonts */
  65. private final AFPResourceAccessor accessor;
  66. /** The collection of objects for each orientation */
  67. private CharacterSetOrientation characterSetOrientation;
  68. /** The nominal vertical size (in millipoints) for bitmap fonts. 0 for outline fonts. */
  69. private int nominalVerticalSize;
  70. /**
  71. * Constructor for the CharacterSetMetric object, the character set is used to load the font
  72. * information from the actual AFP font.
  73. *
  74. * @param codePage the code page identifier
  75. * @param encoding the encoding of the font
  76. * @param charsetType the type of the characterset
  77. * @param name the character set name
  78. * @param accessor the resource accessor to load resource with
  79. * @param eventProducer for handling AFP related events
  80. */
  81. CharacterSet(String codePage, String encoding, CharacterSetType charsetType, String name,
  82. AFPResourceAccessor accessor, AFPEventProducer eventProducer) {
  83. if (name.length() > MAX_NAME_LEN) {
  84. String msg = "Character set name '" + name + "' must be a maximum of "
  85. + MAX_NAME_LEN + " characters";
  86. eventProducer.characterSetNameInvalid(this, msg);
  87. throw new IllegalArgumentException(msg);
  88. }
  89. // the character set name must be 8 chars long
  90. this.name = padName(name);
  91. if (codePage == null) {
  92. this.codePage = null;
  93. } else {
  94. // the code page name must be 8 chars long
  95. this.codePage = padName(codePage);
  96. }
  97. this.encoding = encoding;
  98. this.encoder = charsetType.getEncoder(encoding);
  99. this.accessor = accessor;
  100. }
  101. // right pad short names with space
  102. private String padName(String name) {
  103. return name.length() < MAX_NAME_LEN ? StringUtils.rpad(name, ' ', MAX_NAME_LEN) : name;
  104. }
  105. /**
  106. * Add character set metric information for the different orientations
  107. *
  108. * @param cso the metrics for the orientation
  109. */
  110. public void addCharacterSetOrientation(CharacterSetOrientation cso) {
  111. if (cso.getOrientation() == SUPPORTED_ORIENTATION) {
  112. characterSetOrientation = cso;
  113. }
  114. }
  115. /**
  116. * Sets the nominal vertical size of the font in the case of bitmap fonts.
  117. * @param nominalVerticalSize the nominal vertical size (in millipoints)
  118. */
  119. public void setNominalVerticalSize(int nominalVerticalSize) {
  120. this.nominalVerticalSize = nominalVerticalSize;
  121. }
  122. /**
  123. * Returns the nominal vertical size of the font in the case of bitmap fonts. For outline fonts,
  124. * zero is returned, because these are scalable fonts.
  125. * @return the nominal vertical size (in millipoints) for bitmap fonts, or 0 for outline fonts.
  126. */
  127. public int getNominalVerticalSize() {
  128. return this.nominalVerticalSize;
  129. }
  130. /**
  131. * Ascender height is the distance from the character baseline to the
  132. * top of the character box. A negative ascender height signifies that
  133. * all of the graphic character is below the character baseline. For
  134. * a character rotation other than 0, ascender height loses its
  135. * meaning when the character is lying on its side or is upside down
  136. * with respect to normal viewing orientation. For the general case,
  137. * Ascender Height is the characters most positive y-axis value.
  138. * For bounded character boxes, for a given character having an
  139. * ascender, ascender height and baseline offset are equal.
  140. *
  141. * @return the ascender value in millipoints
  142. */
  143. public int getAscender() {
  144. return getCharacterSetOrientation().getAscender();
  145. }
  146. /**
  147. * Return the width to use for an underscore (_) character.
  148. *
  149. * @return the width of an underscore character
  150. */
  151. public int getUnderscoreWidth() {
  152. return getCharacterSetOrientation().getUnderscoreWidth();
  153. }
  154. /**
  155. * Return the position for an underscore (_) character.
  156. *
  157. * @return the position of an underscore character
  158. */
  159. public int getUnderscorePosition() {
  160. return getCharacterSetOrientation().getUnderscorePosition();
  161. }
  162. /**
  163. * Cap height is the average height of the uppercase characters in
  164. * a font. This value is specified by the designer of a font and is
  165. * usually the height of the uppercase M.
  166. *
  167. * @return the cap height value in millipoints
  168. */
  169. public int getCapHeight() {
  170. return getCharacterSetOrientation().getCapHeight();
  171. }
  172. /**
  173. * Descender depth is the distance from the character baseline to
  174. * the bottom of a character box. A negative descender depth signifies
  175. * that all of the graphic character is above the character baseline.
  176. *
  177. * @return the descender value in millipoints
  178. */
  179. public int getDescender() {
  180. return getCharacterSetOrientation().getDescender();
  181. }
  182. /**
  183. * Returns the resource accessor to load the font resources with.
  184. * @return the resource accessor to load the font resources with
  185. */
  186. public AFPResourceAccessor getResourceAccessor() {
  187. return this.accessor;
  188. }
  189. /**
  190. * XHeight refers to the height of the lower case letters above the baseline.
  191. *
  192. * @return the typical height of characters
  193. */
  194. public int getXHeight() {
  195. return getCharacterSetOrientation().getXHeight();
  196. }
  197. /**
  198. * Get the width (in 1/1000ths of a point size) of the character
  199. * identified by the parameter passed.
  200. *
  201. * @param character the Unicode character from which the width will be calculated
  202. * @param size the font size
  203. * @return the width of the character
  204. */
  205. public int getWidth(char character, int size) {
  206. return getCharacterSetOrientation().getWidth(character, size);
  207. }
  208. public Rectangle getCharacterBox(char character, int size) {
  209. return getCharacterSetOrientation().getCharacterBox(character, size);
  210. }
  211. /**
  212. * Returns the AFP character set identifier
  213. *
  214. * @return the AFP character set identifier
  215. */
  216. public String getName() {
  217. return name;
  218. }
  219. /**
  220. * Returns the AFP character set identifier as a byte array
  221. *
  222. * @return the AFP character set identifier as a byte array
  223. */
  224. public byte[] getNameBytes() {
  225. byte[] nameBytes = null;
  226. try {
  227. nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
  228. } catch (UnsupportedEncodingException usee) {
  229. // @SuppressFBWarnings("DM_DEFAULT_ENCODING")
  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. return characterSetOrientation;
  266. }
  267. /**
  268. * Indicates whether the given char in the character set.
  269. * @param c the character to check
  270. * @return true if the character is in the character set
  271. */
  272. public boolean hasChar(char c) {
  273. if (encoder != null) {
  274. return encoder.canEncode(c);
  275. } else {
  276. //Sun Java 1.4.2 compatibility
  277. return true;
  278. }
  279. }
  280. /**
  281. * Encodes a character sequence to a byte array.
  282. * @param chars the characters
  283. * @return the encoded characters
  284. * @throws CharacterCodingException if the encoding operation fails
  285. */
  286. public EncodedChars encodeChars(CharSequence chars) throws CharacterCodingException {
  287. return encoder.encode(chars);
  288. }
  289. /**
  290. * Map a Unicode character to a code point in the font.
  291. * The code tables are already converted to Unicode therefore
  292. * we can use the identity mapping.
  293. *
  294. * @param c the Unicode character to map
  295. * @return the mapped character
  296. */
  297. public char mapChar(char c) {
  298. //TODO This is not strictly correct but we'll let it be for the moment
  299. return c;
  300. }
  301. /**
  302. * Returns the increment for an space.
  303. * @return the space increment
  304. */
  305. public int getSpaceIncrement() {
  306. return getCharacterSetOrientation().getSpaceIncrement();
  307. }
  308. /**
  309. * Returns the increment for an em space.
  310. * @return the em space increment
  311. */
  312. public int getEmSpaceIncrement() {
  313. return getCharacterSetOrientation().getEmSpaceIncrement();
  314. }
  315. /**
  316. * Returns the nominal character increment.
  317. * @return the nominal character increment
  318. */
  319. public int getNominalCharIncrement() {
  320. return getCharacterSetOrientation().getNominalCharIncrement();
  321. }
  322. }