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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * <p/>
  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. * <p/>
  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. * <p/>
  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. // the code page name must be 8 chars long
  92. this.codePage = padName(codePage);
  93. this.encoding = encoding;
  94. this.encoder = charsetType.getEncoder(encoding);
  95. this.accessor = accessor;
  96. }
  97. // right pad short names with space
  98. private String padName(String name) {
  99. return name.length() < MAX_NAME_LEN ? StringUtils.rpad(name, ' ', MAX_NAME_LEN) : name;
  100. }
  101. /**
  102. * Add character set metric information for the different orientations
  103. *
  104. * @param cso the metrics for the orientation
  105. */
  106. public void addCharacterSetOrientation(CharacterSetOrientation cso) {
  107. if (cso.getOrientation() == SUPPORTED_ORIENTATION) {
  108. characterSetOrientation = cso;
  109. }
  110. }
  111. /**
  112. * Sets the nominal vertical size of the font in the case of bitmap fonts.
  113. * @param nominalVerticalSize the nominal vertical size (in millipoints)
  114. */
  115. public void setNominalVerticalSize(int nominalVerticalSize) {
  116. this.nominalVerticalSize = nominalVerticalSize;
  117. }
  118. /**
  119. * Returns the nominal vertical size of the font in the case of bitmap fonts. For outline fonts,
  120. * zero is returned, because these are scalable fonts.
  121. * @return the nominal vertical size (in millipoints) for bitmap fonts, or 0 for outline fonts.
  122. */
  123. public int getNominalVerticalSize() {
  124. return this.nominalVerticalSize;
  125. }
  126. /**
  127. * Ascender height is the distance from the character baseline to the
  128. * top of the character box. A negative ascender height signifies that
  129. * all of the graphic character is below the character baseline. For
  130. * a character rotation other than 0, ascender height loses its
  131. * meaning when the character is lying on its side or is upside down
  132. * with respect to normal viewing orientation. For the general case,
  133. * Ascender Height is the characters most positive y-axis value.
  134. * For bounded character boxes, for a given character having an
  135. * ascender, ascender height and baseline offset are equal.
  136. *
  137. * @return the ascender value in millipoints
  138. */
  139. public int getAscender() {
  140. return getCharacterSetOrientation().getAscender();
  141. }
  142. /**
  143. * TODO
  144. */
  145. public int getUnderscoreWidth() {
  146. return getCharacterSetOrientation().getUnderscoreWidth();
  147. }
  148. /**
  149. * TODO
  150. */
  151. public int getUnderscorePosition() {
  152. return getCharacterSetOrientation().getUnderscorePosition();
  153. }
  154. /**
  155. * Cap height is the average height of the uppercase characters in
  156. * a font. This value is specified by the designer of a font and is
  157. * usually the height of the uppercase M.
  158. *
  159. * @return the cap height value in millipoints
  160. */
  161. public int getCapHeight() {
  162. return getCharacterSetOrientation().getCapHeight();
  163. }
  164. /**
  165. * Descender depth is the distance from the character baseline to
  166. * the bottom of a character box. A negative descender depth signifies
  167. * that all of the graphic character is above the character baseline.
  168. *
  169. * @return the descender value in millipoints
  170. */
  171. public int getDescender() {
  172. return getCharacterSetOrientation().getDescender();
  173. }
  174. /**
  175. * Returns the resource accessor to load the font resources with.
  176. * @return the resource accessor to load the font resources with
  177. */
  178. public AFPResourceAccessor getResourceAccessor() {
  179. return this.accessor;
  180. }
  181. /**
  182. * XHeight refers to the height of the lower case letters above the baseline.
  183. *
  184. * @return the typical height of characters
  185. */
  186. public int getXHeight() {
  187. return getCharacterSetOrientation().getXHeight();
  188. }
  189. /**
  190. * Get the width (in 1/1000ths of a point size) of the character
  191. * identified by the parameter passed.
  192. *
  193. * @param character the Unicode character from which the width will be calculated
  194. * @return the width of the character
  195. */
  196. public int getWidth(char character, int size) {
  197. return getCharacterSetOrientation().getWidth(character, size);
  198. }
  199. public Rectangle getCharacterBox(char character, int size) {
  200. return getCharacterSetOrientation().getCharacterBox(character, size);
  201. }
  202. /**
  203. * Returns the AFP character set identifier
  204. *
  205. * @return the AFP character set identifier
  206. */
  207. public String getName() {
  208. return name;
  209. }
  210. /**
  211. * Returns the AFP character set identifier as a byte array
  212. *
  213. * @return the AFP character set identifier as a byte array
  214. */
  215. public byte[] getNameBytes() {
  216. byte[] nameBytes = null;
  217. try {
  218. nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
  219. } catch (UnsupportedEncodingException usee) {
  220. nameBytes = name.getBytes();
  221. LOG.warn(
  222. "UnsupportedEncodingException translating the name " + name);
  223. }
  224. return nameBytes;
  225. }
  226. /**
  227. * Returns the AFP code page identifier
  228. *
  229. * @return the AFP code page identifier
  230. */
  231. public String getCodePage() {
  232. return codePage;
  233. }
  234. /**
  235. * Returns the AFP code page encoding
  236. *
  237. * @return the AFP code page encoding
  238. */
  239. public String getEncoding() {
  240. return encoding;
  241. }
  242. /**
  243. * Helper method to return the current CharacterSetOrientation, note
  244. * that FOP does not yet implement the "reference-orientation"
  245. * attribute therefore we always use the orientation zero degrees,
  246. * Other orientation information is captured for use by a future
  247. * implementation (whenever FOP implement the mechanism). This is also
  248. * the case for landscape prints which use an orientation of 270 degrees,
  249. * in 99.9% of cases the font metrics will be the same as the 0 degrees
  250. * therefore the implementation currently will always use 0 degrees.
  251. *
  252. * @return characterSetOrentation The current orientation metrics.
  253. */
  254. private CharacterSetOrientation getCharacterSetOrientation() {
  255. return characterSetOrientation;
  256. }
  257. /**
  258. * Indicates whether the given char in the character set.
  259. * @param c the character to check
  260. * @return true if the character is in the character set
  261. */
  262. public boolean hasChar(char c) {
  263. if (encoder != null) {
  264. return encoder.canEncode(c);
  265. } else {
  266. //Sun Java 1.4.2 compatibility
  267. return true;
  268. }
  269. }
  270. /**
  271. * Encodes a character sequence to a byte array.
  272. * @param chars the characters
  273. * @return the encoded characters
  274. * @throws CharacterCodingException if the encoding operation fails
  275. */
  276. public EncodedChars encodeChars(CharSequence chars) throws CharacterCodingException {
  277. return encoder.encode(chars);
  278. }
  279. /**
  280. * Map a Unicode character to a code point in the font.
  281. * The code tables are already converted to Unicode therefore
  282. * we can use the identity mapping.
  283. *
  284. * @param c the Unicode character to map
  285. * @return the mapped character
  286. */
  287. public char mapChar(char c) {
  288. //TODO This is not strictly correct but we'll let it be for the moment
  289. return c;
  290. }
  291. /**
  292. * Returns the increment for an space.
  293. * @return the space increment
  294. */
  295. public int getSpaceIncrement() {
  296. return getCharacterSetOrientation().getSpaceIncrement();
  297. }
  298. /**
  299. * Returns the increment for an em space.
  300. * @return the em space increment
  301. */
  302. public int getEmSpaceIncrement() {
  303. return getCharacterSetOrientation().getEmSpaceIncrement();
  304. }
  305. /**
  306. * Returns the nominal character increment.
  307. * @return the nominal character increment
  308. */
  309. public int getNominalCharIncrement() {
  310. return getCharacterSetOrientation().getNominalCharIncrement();
  311. }
  312. }