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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /**
  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.<br>
  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.<br>
  30. *
  31. * This class provides font metric information for a particular font
  32. * as by the orientation.<br>
  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. */
  38. public class CharacterSetOrientation {
  39. /**
  40. * The ascender height for the character set
  41. */
  42. private int ascender;
  43. /**
  44. * The descender depth for the character set
  45. */
  46. private int descender;
  47. /**
  48. * The height of capital letters
  49. */
  50. private int capHeight;
  51. /**
  52. * The character widths in the character set (indexed using Unicode codepoints)
  53. */
  54. private IntegerKeyStore<CharacterMetrics> characterMetrics;
  55. /**
  56. * The height of lowercase letters
  57. */
  58. private int xHeight;
  59. /** The character set orientation */
  60. private final int orientation;
  61. /** space increment */
  62. private final int spaceIncrement;
  63. /** em space increment */
  64. private final int emSpaceIncrement;
  65. /** Nominal Character Increment */
  66. private final int nomCharIncrement;
  67. private int underscoreWidth;
  68. private int underscorePosition;
  69. /**
  70. * Constructor for the CharacterSetOrientation, the orientation is
  71. * expressed as the degrees rotation (i.e 0, 90, 180, 270)
  72. *
  73. * @param orientation the character set orientation
  74. * @param spaceIncrement the space increment
  75. * @param emSpaceIncrement the em space increment
  76. * @param nomCharIncrement the nominal character increment
  77. */
  78. public CharacterSetOrientation(int orientation, int spaceIncrement, int emSpaceIncrement,
  79. int nomCharIncrement) {
  80. this.orientation = orientation;
  81. this.spaceIncrement = spaceIncrement;
  82. this.emSpaceIncrement = emSpaceIncrement;
  83. this.nomCharIncrement = nomCharIncrement;
  84. this.characterMetrics = new IntegerKeyStore<CharacterMetrics>();
  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. * {@asf.todo}
  121. *
  122. * @return the underscore width
  123. */
  124. public int getUnderscoreWidth() {
  125. return underscoreWidth;
  126. }
  127. /**
  128. * {@asf.todo}
  129. *
  130. * @return the underscore position
  131. */
  132. public int getUnderscorePosition() {
  133. return underscorePosition;
  134. }
  135. /**
  136. * The orientation for these metrics in the character set
  137. * @return the orientation
  138. */
  139. public int getOrientation() {
  140. return orientation;
  141. }
  142. /**
  143. * XHeight refers to the height of the lower case letters above
  144. * the baseline.
  145. * @return heightX the typical height of characters
  146. */
  147. public int getXHeight() {
  148. return xHeight;
  149. }
  150. /**
  151. * Get the width (in 1/1000ths of a point size) of the character
  152. * identified by the parameter passed.
  153. * @param character the Unicode character to evaluate
  154. * @param size the font size
  155. * @return the widths of the character
  156. */
  157. public int getWidth(char character, int size) {
  158. CharacterMetrics cm = getCharacterMetrics(character);
  159. return cm == null ? -1 : size * cm.width;
  160. }
  161. private CharacterMetrics getCharacterMetrics(char character) {
  162. return characterMetrics.get((int) character);
  163. }
  164. /**
  165. * Get the character box (rectangle with dimensions in 1/1000ths of a point size) of the character
  166. * identified by the parameter passed.
  167. * @param character the Unicode character to evaluate
  168. * @param size the font size
  169. * @return the character box
  170. */
  171. public Rectangle getCharacterBox(char character, int size) {
  172. CharacterMetrics cm = getCharacterMetrics(character);
  173. return scale(cm == null ? getFallbackCharacterBox() : cm.characterBox, size);
  174. }
  175. private static Rectangle scale(Rectangle rectangle, int size) {
  176. if (rectangle == null) {
  177. return null;
  178. } else {
  179. return new Rectangle((int) (size * rectangle.getX()), (int) (size * rectangle.getY()),
  180. (int) (size * rectangle.getWidth()), (int) (size * rectangle.getHeight()));
  181. }
  182. }
  183. private Rectangle getFallbackCharacterBox() {
  184. // TODO replace with something sensible
  185. return new Rectangle(0, 0, 0, 0);
  186. }
  187. /**
  188. * Ascender height is the distance from the character baseline to the
  189. * top of the character box. A negative ascender height signifies that
  190. * all of the graphic character is below the character baseline. For
  191. * a character rotation other than 0, ascender height loses its
  192. * meaning when the character is lying on its side or is upside down
  193. * with respect to normal viewing orientation. For the general case,
  194. * Ascender Height is the character's most positive y-axis value.
  195. * For bounded character boxes, for a given character having an
  196. * ascender, ascender height and baseline offset are equal.
  197. * @param ascender the ascender to set
  198. */
  199. public void setAscender(int ascender) {
  200. this.ascender = ascender;
  201. }
  202. /**
  203. * Cap height is the average height of the uppercase characters in
  204. * a font. This value is specified by the designer of a font and is
  205. * usually the height of the uppercase M.
  206. * @param capHeight the cap height to set
  207. */
  208. public void setCapHeight(int capHeight) {
  209. this.capHeight = capHeight;
  210. }
  211. /**
  212. * Descender depth is the distance from the character baseline to
  213. * the bottom of a character box. A negative descender depth signifies
  214. * that all of the graphic character is above the character baseline.
  215. * @param descender the descender value in millipoints
  216. */
  217. public void setDescender(int descender) {
  218. this.descender = descender;
  219. }
  220. /**
  221. * TODO
  222. * @param underscoreWidth the underscore width value in millipoints
  223. */
  224. public void setUnderscoreWidth(int underscoreWidth) {
  225. this.underscoreWidth = underscoreWidth;
  226. }
  227. /**
  228. * TODO
  229. * @param underscorePosition the underscore position value in millipoints
  230. */
  231. public void setUnderscorePosition(int underscorePosition) {
  232. this.underscorePosition = underscorePosition;
  233. }
  234. /**
  235. * Set the width (in 1/1000ths of a point size) of the character
  236. * identified by the parameter passed.
  237. * @param character the Unicode character for which the width is being set
  238. * @param width the widths of the character
  239. * @param characterBox the character box
  240. */
  241. public void setCharacterMetrics(char character, int width, Rectangle characterBox) {
  242. characterMetrics.put((int) character, new CharacterMetrics(width, characterBox));
  243. }
  244. /**
  245. * XHeight refers to the height of the lower case letters above
  246. * the baseline.
  247. * @param xHeight the typical height of characters
  248. */
  249. public void setXHeight(int xHeight) {
  250. this.xHeight = xHeight;
  251. }
  252. /**
  253. * Returns the space increment.
  254. * @return the space increment
  255. */
  256. public int getSpaceIncrement() {
  257. return this.spaceIncrement;
  258. }
  259. /**
  260. * Returns the em space increment.
  261. * @return the em space increment
  262. */
  263. public int getEmSpaceIncrement() {
  264. return this.emSpaceIncrement;
  265. }
  266. /**
  267. * Returns the nominal character increment.
  268. * @return the nominal character increment
  269. */
  270. public int getNominalCharIncrement() {
  271. return this.nomCharIncrement;
  272. }
  273. private static class CharacterMetrics {
  274. public final int width;
  275. public final Rectangle characterBox;
  276. public CharacterMetrics(int width, Rectangle characterBox) {
  277. this.width = width;
  278. this.characterBox = characterBox;
  279. }
  280. }
  281. }