Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CharacterSetOrientation.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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;
  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. /** The character set orientation */
  69. private final int orientation;
  70. /** space increment */
  71. private final int spaceIncrement;
  72. /** em space increment */
  73. private final int emSpaceIncrement;
  74. /** Nominal Character Increment */
  75. private final int nomCharIncrement;
  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, int spaceIncrement, int emSpaceIncrement,
  82. int nomCharIncrement) {
  83. this.orientation = orientation;
  84. this.spaceIncrement = spaceIncrement;
  85. this.emSpaceIncrement = emSpaceIncrement;
  86. this.nomCharIncrement = nomCharIncrement;
  87. charsWidths = new int[256];
  88. Arrays.fill(charsWidths, -1);
  89. }
  90. /**
  91. * Ascender height is the distance from the character baseline to the
  92. * top of the character box. A negative ascender height signifies that
  93. * all of the graphic character is below the character baseline. For
  94. * a character rotation other than 0, ascender height loses its
  95. * meaning when the character is lying on its side or is upside down
  96. * with respect to normal viewing orientation. For the general case,
  97. * Ascender Height is the character's most positive y-axis value.
  98. * For bounded character boxes, for a given character having an
  99. * ascender, ascender height and baseline offset are equal.
  100. * @return the ascender value in millipoints
  101. */
  102. public int getAscender() {
  103. return ascender;
  104. }
  105. /**
  106. * Cap height is the average height of the uppercase characters in
  107. * a font. This value is specified by the designer of a font and is
  108. * usually the height of the uppercase M.
  109. * @return the cap height value in millipoints
  110. */
  111. public int getCapHeight() {
  112. return capHeight;
  113. }
  114. /**
  115. * Descender depth is the distance from the character baseline to
  116. * the bottom of a character box. A negative descender depth signifies
  117. * that all of the graphic character is above the character baseline.
  118. * @return the descender value in millipoints
  119. */
  120. public int getDescender() {
  121. return descender;
  122. }
  123. /**
  124. * The first character in the character set
  125. * @return the first character (Unicode codepoint)
  126. */
  127. public char getFirstChar() {
  128. return firstChar;
  129. }
  130. /**
  131. * The last character in the character set
  132. * @return the last character (Unicode codepoint)
  133. */
  134. public char getLastChar() {
  135. return lastChar;
  136. }
  137. /**
  138. * The orientation for these metrics in the character set
  139. * @return the orientation
  140. */
  141. public int getOrientation() {
  142. return orientation;
  143. }
  144. /**
  145. * Get the width (in 1/1000ths of a point size) of all characters
  146. * in this character set.
  147. * @return the widths of all characters
  148. */
  149. public int[] getWidths() {
  150. int[] arr = new int[(getLastChar() - getFirstChar()) + 1];
  151. System.arraycopy(charsWidths, getFirstChar(), arr, 0, (getLastChar() - getFirstChar()) + 1);
  152. return arr;
  153. }
  154. /**
  155. * XHeight refers to the height of the lower case letters above
  156. * the baseline.
  157. * @return heightX the typical height of characters
  158. */
  159. public int getXHeight() {
  160. return xHeight;
  161. }
  162. /**
  163. * Get the width (in 1/1000ths of a point size) of the character
  164. * identified by the parameter passed.
  165. * @param character the Unicode character to evaluate
  166. * @return the widths of the character
  167. */
  168. public int getWidth(char character) {
  169. if (character >= charsWidths.length) {
  170. throw new IllegalArgumentException("Invalid character: "
  171. + character + " (" + Integer.toString(character)
  172. + "), maximum is " + (charsWidths.length - 1));
  173. }
  174. return charsWidths[character];
  175. }
  176. /**
  177. * Ascender height is the distance from the character baseline to the
  178. * top of the character box. A negative ascender height signifies that
  179. * all of the graphic character is below the character baseline. For
  180. * a character rotation other than 0, ascender height loses its
  181. * meaning when the character is lying on its side or is upside down
  182. * with respect to normal viewing orientation. For the general case,
  183. * Ascender Height is the character's most positive y-axis value.
  184. * For bounded character boxes, for a given character having an
  185. * ascender, ascender height and baseline offset are equal.
  186. * @param ascender the ascender to set
  187. */
  188. public void setAscender(int ascender) {
  189. this.ascender = ascender;
  190. }
  191. /**
  192. * Cap height is the average height of the uppercase characters in
  193. * a font. This value is specified by the designer of a font and is
  194. * usually the height of the uppercase M.
  195. * @param capHeight the cap height to set
  196. */
  197. public void setCapHeight(int capHeight) {
  198. this.capHeight = capHeight;
  199. }
  200. /**
  201. * Descender depth is the distance from the character baseline to
  202. * the bottom of a character box. A negative descender depth signifies
  203. * that all of the graphic character is above the character baseline.
  204. * @param descender the descender value in millipoints
  205. */
  206. public void setDescender(int descender) {
  207. this.descender = descender;
  208. }
  209. /**
  210. * The first character in the character set
  211. * @param firstChar the first character
  212. */
  213. public void setFirstChar(char firstChar) {
  214. this.firstChar = firstChar;
  215. }
  216. /**
  217. * The last character in the character set
  218. * @param lastChar the last character
  219. */
  220. public void setLastChar(char lastChar) {
  221. this.lastChar = lastChar;
  222. }
  223. /**
  224. * Set the width (in 1/1000ths of a point size) of the character
  225. * identified by the parameter passed.
  226. * @param character the Unicode character for which the width is being set
  227. * @param width the widths of the character
  228. */
  229. public void setWidth(char character, int width) {
  230. if (character >= charsWidths.length) {
  231. // Increase the size of the array if necessary
  232. // TODO Can we remove firstChar? surely firstChar==0 at this stage?
  233. int[] arr = new int[(character - firstChar) + 1];
  234. System.arraycopy(charsWidths, 0, arr, 0, charsWidths.length);
  235. Arrays.fill(arr, charsWidths.length, character - firstChar, -1);
  236. charsWidths = arr;
  237. }
  238. charsWidths[character] = width;
  239. }
  240. /**
  241. * XHeight refers to the height of the lower case letters above
  242. * the baseline.
  243. * @param xHeight the typical height of characters
  244. */
  245. public void setXHeight(int xHeight) {
  246. this.xHeight = xHeight;
  247. }
  248. /**
  249. * Returns the space increment.
  250. * @return the space increment
  251. */
  252. public int getSpaceIncrement() {
  253. return this.spaceIncrement;
  254. }
  255. /**
  256. * Returns the em space increment.
  257. * @return the em space increment
  258. */
  259. public int getEmSpaceIncrement() {
  260. return this.emSpaceIncrement;
  261. }
  262. /**
  263. * Returns the nominal character increment.
  264. * @return the nominal character increment
  265. */
  266. public int getNominalCharIncrement() {
  267. return this.nomCharIncrement;
  268. }
  269. }