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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.UnsupportedEncodingException;
  20. import java.nio.charset.CharacterCodingException;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.afp.AFPConstants;
  26. import org.apache.fop.afp.AFPEventProducer;
  27. import org.apache.fop.afp.fonts.CharactersetEncoder.EncodedChars;
  28. import org.apache.fop.afp.util.AFPResourceAccessor;
  29. import org.apache.fop.afp.util.StringUtils;
  30. /**
  31. * The IBM Font Object Content Architecture (FOCA) supports presentation
  32. * of character shapes by defining their characteristics, which include
  33. * font description information for identifying the characters, font metric
  34. * information for positioning the characters, and character shape information
  35. * for presenting the character images.
  36. * <p/>
  37. * Presenting a graphic character on a presentation surface requires
  38. * information on the rotation and position of character on the physical
  39. * or logical page.
  40. * <p/>
  41. * This class proivdes font metric information for a particular font
  42. * as identified by the character set name. This information is obtained
  43. * directly from the AFP font files which must be installed in the path
  44. * specified in the afp-fonts xml definition file.
  45. * <p/>
  46. */
  47. public class CharacterSet {
  48. /** Static logging instance */
  49. protected static final Log LOG = LogFactory.getLog(CharacterSet.class.getName());
  50. /** default codepage */
  51. public static final String DEFAULT_CODEPAGE = "T1V10500";
  52. /** default encoding */
  53. public static final String DEFAULT_ENCODING = "Cp500";
  54. private static final int MAX_NAME_LEN = 8;
  55. /** The code page to which the character set relates */
  56. protected final String codePage;
  57. /** The encoding used for the code page */
  58. protected final String encoding;
  59. /** The characterset encoder corresponding to this encoding */
  60. private final CharactersetEncoder encoder;
  61. /** The character set relating to the font */
  62. protected final String name;
  63. /** The path to the installed fonts */
  64. private final AFPResourceAccessor accessor;
  65. /** The current orientation (currently only 0 is supported by FOP) */
  66. private final String currentOrientation = "0";
  67. /** The collection of objects for each orientation */
  68. private final Map<String, CharacterSetOrientation> characterSetOrientations;
  69. /** The nominal vertical size (in millipoints) for bitmap fonts. 0 for outline fonts. */
  70. private int nominalVerticalSize;
  71. /**
  72. * Constructor for the CharacterSetMetric object, the character set is used to load the font
  73. * information from the actual AFP font.
  74. *
  75. * @param codePage the code page identifier
  76. * @param encoding the encoding of the font
  77. * @param charsetType the type of the characterset
  78. * @param name the character set name
  79. * @param accessor the resource accessor to load resource with
  80. * @param eventProducer for handling AFP related events
  81. */
  82. CharacterSet(String codePage, String encoding, CharacterSetType charsetType, String name,
  83. AFPResourceAccessor accessor, AFPEventProducer eventProducer) {
  84. if (name.length() > MAX_NAME_LEN) {
  85. String msg = "Character set name '" + name + "' must be a maximum of "
  86. + MAX_NAME_LEN + " characters";
  87. eventProducer.characterSetNameInvalid(this, msg);
  88. throw new IllegalArgumentException(msg);
  89. }
  90. if (name.length() < MAX_NAME_LEN) {
  91. this.name = StringUtils.rpad(name, ' ', MAX_NAME_LEN);
  92. } else {
  93. this.name = name;
  94. }
  95. this.codePage = codePage;
  96. this.encoding = encoding;
  97. this.encoder = charsetType.getEncoder(encoding);
  98. this.accessor = accessor;
  99. this.characterSetOrientations = new HashMap<String, CharacterSetOrientation>(4);
  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. characterSetOrientations.put(String.valueOf(cso.getOrientation()), cso);
  108. }
  109. /**
  110. * Sets the nominal vertical size of the font in the case of bitmap fonts.
  111. * @param nominalVerticalSize the nominal vertical size (in millipoints)
  112. */
  113. public void setNominalVerticalSize(int nominalVerticalSize) {
  114. this.nominalVerticalSize = nominalVerticalSize;
  115. }
  116. /**
  117. * Returns the nominal vertical size of the font in the case of bitmap fonts. For outline fonts,
  118. * zero is returned, because these are scalable fonts.
  119. * @return the nominal vertical size (in millipoints) for bitmap fonts, or 0 for outline fonts.
  120. */
  121. public int getNominalVerticalSize() {
  122. return this.nominalVerticalSize;
  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 characters 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. *
  135. * @return the ascender value in millipoints
  136. */
  137. public int getAscender() {
  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. *
  145. * @return the cap height value in millipoints
  146. */
  147. public int getCapHeight() {
  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. *
  155. * @return the descender value in millipoints
  156. */
  157. public int getDescender() {
  158. return getCharacterSetOrientation().getDescender();
  159. }
  160. /**
  161. * Returns the first character in the character set
  162. *
  163. * @return the first character in the character set (Unicode codepoint)
  164. */
  165. public char getFirstChar() {
  166. return getCharacterSetOrientation().getFirstChar();
  167. }
  168. /**
  169. * Returns the last character in the character set
  170. *
  171. * @return the last character in the character set (Unicode codepoint)
  172. */
  173. public char getLastChar() {
  174. return getCharacterSetOrientation().getLastChar();
  175. }
  176. /**
  177. * Returns the resource accessor to load the font resources with.
  178. * @return the resource accessor to load the font resources with
  179. */
  180. public AFPResourceAccessor getResourceAccessor() {
  181. return this.accessor;
  182. }
  183. /**
  184. * Get the width (in 1/1000ths of a point size) of all characters
  185. *
  186. * @return the widths of all characters
  187. */
  188. public int[] getWidths() {
  189. return getCharacterSetOrientation().getWidths();
  190. }
  191. /**
  192. * XHeight refers to the height of the lower case letters above the baseline.
  193. *
  194. * @return the typical height of characters
  195. */
  196. public int getXHeight() {
  197. return getCharacterSetOrientation().getXHeight();
  198. }
  199. /**
  200. * Get the width (in 1/1000ths of a point size) of the character
  201. * identified by the parameter passed.
  202. *
  203. * @param character the Unicode character from which the width will be calculated
  204. * @return the width of the character
  205. */
  206. public int getWidth(char character) {
  207. return getCharacterSetOrientation().getWidth(character);
  208. }
  209. /**
  210. * Returns the AFP character set identifier
  211. *
  212. * @return the AFP character set identifier
  213. */
  214. public String getName() {
  215. return name;
  216. }
  217. /**
  218. * Returns the AFP character set identifier as a byte array
  219. *
  220. * @return the AFP character set identifier as a byte array
  221. */
  222. public byte[] getNameBytes() {
  223. byte[] nameBytes = null;
  224. try {
  225. nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
  226. } catch (UnsupportedEncodingException usee) {
  227. nameBytes = name.getBytes();
  228. LOG.warn(
  229. "UnsupportedEncodingException translating the name " + name);
  230. }
  231. return nameBytes;
  232. }
  233. /**
  234. * Returns the AFP code page identifier
  235. *
  236. * @return the AFP code page identifier
  237. */
  238. public String getCodePage() {
  239. return codePage;
  240. }
  241. /**
  242. * Returns the AFP code page encoding
  243. *
  244. * @return the AFP code page encoding
  245. */
  246. public String getEncoding() {
  247. return encoding;
  248. }
  249. /**
  250. * Helper method to return the current CharacterSetOrientation, note
  251. * that FOP does not yet implement the "reference-orientation"
  252. * attribute therefore we always use the orientation zero degrees,
  253. * Other orientation information is captured for use by a future
  254. * implementation (whenever FOP implement the mechanism). This is also
  255. * the case for landscape prints which use an orientation of 270 degrees,
  256. * in 99.9% of cases the font metrics will be the same as the 0 degrees
  257. * therefore the implementation currently will always use 0 degrees.
  258. *
  259. * @return characterSetOrentation The current orientation metrics.
  260. */
  261. private CharacterSetOrientation getCharacterSetOrientation() {
  262. CharacterSetOrientation c
  263. = characterSetOrientations.get(currentOrientation);
  264. return c;
  265. }
  266. /**
  267. * Indicates whether the given char in the character set.
  268. * @param c the character to check
  269. * @return true if the character is in the character set
  270. */
  271. public boolean hasChar(char c) {
  272. if (encoder != null) {
  273. return encoder.canEncode(c);
  274. } else {
  275. //Sun Java 1.4.2 compatibility
  276. return true;
  277. }
  278. }
  279. /**
  280. * Encodes a character sequence to a byte array.
  281. * @param chars the characters
  282. * @return the encoded characters
  283. * @throws CharacterCodingException if the encoding operation fails
  284. */
  285. public EncodedChars encodeChars(CharSequence chars) throws CharacterCodingException {
  286. return encoder.encode(chars);
  287. }
  288. /**
  289. * Map a Unicode character to a code point in the font.
  290. * The code tables are already converted to Unicode therefore
  291. * we can use the identity mapping.
  292. *
  293. * @param c the Unicode character to map
  294. * @return the mapped character
  295. */
  296. public char mapChar(char c) {
  297. //TODO This is not strictly correct but we'll let it be for the moment
  298. return c;
  299. }
  300. /**
  301. * Returns the increment for an space.
  302. * @return the space increment
  303. */
  304. public int getSpaceIncrement() {
  305. return getCharacterSetOrientation().getSpaceIncrement();
  306. }
  307. /**
  308. * Returns the increment for an em space.
  309. * @return the em space increment
  310. */
  311. public int getEmSpaceIncrement() {
  312. return getCharacterSetOrientation().getEmSpaceIncrement();
  313. }
  314. }