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.

CharacterSetOrientation.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.util.Arrays;
  20. /**
  21. * The IBM Font Object Content Architecture (FOCA) supports presentation
  22. * of character shapes by defining their characteristics, which include
  23. * Font-Description information for identifying the characters, Font-Metric
  24. * information for positioning the characters, and Character-Shape
  25. * information for presenting the character images.
  26. *
  27. * Presenting a graphic character on a presentation surface requires
  28. * that you communicate this information clearly to rotate and position
  29. * characters correctly on the physical or logical page.
  30. *
  31. * This class provides font metric information for a particular font
  32. * as by the orientation.
  33. *
  34. * This information is obtained directly from the AFP font files which must
  35. * be installed in the classpath under in the location specified by the path
  36. * attribute in the afp-font.xml file.
  37. * <p/>
  38. */
  39. public class CharacterSetOrientation {
  40. /**
  41. * The ascender height for the character set
  42. */
  43. private int ascender;
  44. /**
  45. * The descender depth for the character set
  46. */
  47. private int descender;
  48. /**
  49. * The height of capital letters
  50. */
  51. private int capHeight;
  52. /**
  53. * The character widths in the character set (indexed using Unicode codepoints)
  54. */
  55. private int[] charsWidths = null;
  56. /**
  57. * The height of lowercase letters
  58. */
  59. private int xHeight;
  60. /**
  61. * The first character (Unicode codepoint)
  62. */
  63. private char firstChar;
  64. /**
  65. * The last character (Unicode codepoint)
  66. */
  67. private char lastChar;
  68. /**
  69. * The character set orientation
  70. */
  71. private int orientation = 0;
  72. /** space increment */
  73. private int spaceIncrement;
  74. /** em space increment */
  75. private int emSpaceIncrement = -1;
  76. /**
  77. * Constructor for the CharacterSetOrientation, the orientation is
  78. * expressed as the degrees rotation (i.e 0, 90, 180, 270)
  79. * @param orientation the character set orientation
  80. */
  81. public CharacterSetOrientation(int orientation) {
  82. this.orientation = orientation;
  83. charsWidths = new int[256];
  84. Arrays.fill(charsWidths, -1);
  85. }
  86. /**
  87. * Ascender height is the distance from the character baseline to the
  88. * top of the character box. A negative ascender height signifies that
  89. * all of the graphic character is below the character baseline. For
  90. * a character rotation other than 0, ascender height loses its
  91. * meaning when the character is lying on its side or is upside down
  92. * with respect to normal viewing orientation. For the general case,
  93. * Ascender Height is the character�s most positive y-axis value.
  94. * For bounded character boxes, for a given character having an
  95. * ascender, ascender height and baseline offset are equal.
  96. * @return the ascender value in millipoints
  97. */
  98. public int getAscender() {
  99. return ascender;
  100. }
  101. /**
  102. * Cap height is the average height of the uppercase characters in
  103. * a font. This value is specified by the designer of a font and is
  104. * usually the height of the uppercase M.
  105. * @return the cap height value in millipoints
  106. */
  107. public int getCapHeight() {
  108. return capHeight;
  109. }
  110. /**
  111. * Descender depth is the distance from the character baseline to
  112. * the bottom of a character box. A negative descender depth signifies
  113. * that all of the graphic character is above the character baseline.
  114. * @return the descender value in millipoints
  115. */
  116. public int getDescender() {
  117. return descender;
  118. }
  119. /**
  120. * The first character in the character set
  121. * @return the first character (Unicode codepoint)
  122. */
  123. public char getFirstChar() {
  124. return firstChar;
  125. }
  126. /**
  127. * The last character in the character set
  128. * @return the last character (Unicode codepoint)
  129. */
  130. public char getLastChar() {
  131. return lastChar;
  132. }
  133. /**
  134. * The orientation for these metrics in the character set
  135. * @return the orientation
  136. */
  137. public int getOrientation() {
  138. return orientation;
  139. }
  140. /**
  141. * Get the width (in 1/1000ths of a point size) of all characters
  142. * in this character set.
  143. * @return the widths of all characters
  144. */
  145. public int[] getWidths() {
  146. int[] arr = new int[(getLastChar() - getFirstChar()) + 1];
  147. System.arraycopy(charsWidths, getFirstChar(), arr, 0, (getLastChar() - getFirstChar()) + 1);
  148. return arr;
  149. }
  150. /**
  151. * XHeight refers to the height of the lower case letters above
  152. * the baseline.
  153. * @return heightX the typical height of characters
  154. */
  155. public int getXHeight() {
  156. return xHeight;
  157. }
  158. /**
  159. * Get the width (in 1/1000ths of a point size) of the character
  160. * identified by the parameter passed.
  161. * @param character the Unicode character to evaluate
  162. * @return the widths of the character
  163. */
  164. public int getWidth(char character) {
  165. if (character >= charsWidths.length) {
  166. throw new IllegalArgumentException("Invalid character: "
  167. + character + " (" + Integer.toString(character)
  168. + "), maximum is " + (charsWidths.length - 1));
  169. }
  170. return charsWidths[character];
  171. }
  172. /**
  173. * Ascender height is the distance from the character baseline to the
  174. * top of the character box. A negative ascender height signifies that
  175. * all of the graphic character is below the character baseline. For
  176. * a character rotation other than 0, ascender height loses its
  177. * meaning when the character is lying on its side or is upside down
  178. * with respect to normal viewing orientation. For the general case,
  179. * Ascender Height is the character's most positive y-axis value.
  180. * For bounded character boxes, for a given character having an
  181. * ascender, ascender height and baseline offset are equal.
  182. * @param ascender the ascender to set
  183. */
  184. public void setAscender(int ascender) {
  185. this.ascender = ascender;
  186. }
  187. /**
  188. * Cap height is the average height of the uppercase characters in
  189. * a font. This value is specified by the designer of a font and is
  190. * usually the height of the uppercase M.
  191. * @param capHeight the cap height to set
  192. */
  193. public void setCapHeight(int capHeight) {
  194. this.capHeight = capHeight;
  195. }
  196. /**
  197. * Descender depth is the distance from the character baseline to
  198. * the bottom of a character box. A negative descender depth signifies
  199. * that all of the graphic character is above the character baseline.
  200. * @param descender the descender value in millipoints
  201. */
  202. public void setDescender(int descender) {
  203. this.descender = descender;
  204. }
  205. /**
  206. * The first character in the character set
  207. * @param firstChar the first character
  208. */
  209. public void setFirstChar(char firstChar) {
  210. this.firstChar = firstChar;
  211. }
  212. /**
  213. * The last character in the character set
  214. * @param lastChar the last character
  215. */
  216. public void setLastChar(char lastChar) {
  217. this.lastChar = lastChar;
  218. }
  219. /**
  220. * Set the width (in 1/1000ths of a point size) of the character
  221. * identified by the parameter passed.
  222. * @param character the Unicode character for which the width is being set
  223. * @param width the widths of the character
  224. */
  225. public void setWidth(char character, int width) {
  226. if (character >= charsWidths.length) {
  227. // Increase the size of the array if necessary
  228. // TODO Can we remove firstChar? surely firstChar==0 at this stage?
  229. int[] arr = new int[(character - firstChar) + 1];
  230. System.arraycopy(charsWidths, 0, arr, 0, charsWidths.length);
  231. Arrays.fill(arr, charsWidths.length, character - firstChar, -1);
  232. charsWidths = arr;
  233. }
  234. charsWidths[character] = width;
  235. }
  236. /**
  237. * XHeight refers to the height of the lower case letters above
  238. * the baseline.
  239. * @param xHeight the typical height of characters
  240. */
  241. public void setXHeight(int xHeight) {
  242. this.xHeight = xHeight;
  243. }
  244. /**
  245. * Returns the space increment.
  246. * @return the space increment
  247. */
  248. public int getSpaceIncrement() {
  249. return this.spaceIncrement;
  250. }
  251. /**
  252. * Sets the space increment.
  253. * @param value the space increment
  254. */
  255. public void setSpaceIncrement(int value) {
  256. this.spaceIncrement = value;
  257. }
  258. /**
  259. * Returns the em space increment.
  260. * @return the em space increment
  261. */
  262. public int getEmSpaceIncrement() {
  263. return this.emSpaceIncrement;
  264. }
  265. /**
  266. * Sets the em space increment.
  267. * @param value the em space increment
  268. */
  269. public void setEmSpaceIncrement(int value) {
  270. this.emSpaceIncrement = value;
  271. }
  272. }