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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. *
  121. * @return the underscore width
  122. */
  123. public int getUnderscoreWidth() {
  124. return underscoreWidth;
  125. }
  126. /**
  127. *
  128. * @return the underscore position
  129. */
  130. public int getUnderscorePosition() {
  131. return underscorePosition;
  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. * XHeight refers to the height of the lower case letters above
  142. * the baseline.
  143. * @return heightX the typical height of characters
  144. */
  145. public int getXHeight() {
  146. return xHeight;
  147. }
  148. /**
  149. * Get the width (in 1/1000ths of a point size) of the character
  150. * identified by the parameter passed.
  151. * @param character the Unicode character to evaluate
  152. * @param size the font size
  153. * @return the widths of the character
  154. */
  155. public int getWidth(char character, int size) {
  156. CharacterMetrics cm = getCharacterMetrics(character);
  157. return cm == null ? -1 : size * cm.width;
  158. }
  159. private CharacterMetrics getCharacterMetrics(char character) {
  160. return characterMetrics.get((int) character);
  161. }
  162. /**
  163. * Get the character box (rectangle with dimensions 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. * @param size the font size
  167. * @return the character box
  168. */
  169. public Rectangle getCharacterBox(char character, int size) {
  170. CharacterMetrics cm = getCharacterMetrics(character);
  171. return scale(cm == null ? getFallbackCharacterBox() : cm.characterBox, size);
  172. }
  173. private static Rectangle scale(Rectangle rectangle, int size) {
  174. if (rectangle == null) {
  175. return null;
  176. } else {
  177. return new Rectangle((int) (size * rectangle.getX()), (int) (size * rectangle.getY()),
  178. (int) (size * rectangle.getWidth()), (int) (size * rectangle.getHeight()));
  179. }
  180. }
  181. private Rectangle getFallbackCharacterBox() {
  182. // TODO replace with something sensible
  183. return new Rectangle(0, 0, 0, 0);
  184. }
  185. /**
  186. * Ascender height is the distance from the character baseline to the
  187. * top of the character box. A negative ascender height signifies that
  188. * all of the graphic character is below the character baseline. For
  189. * a character rotation other than 0, ascender height loses its
  190. * meaning when the character is lying on its side or is upside down
  191. * with respect to normal viewing orientation. For the general case,
  192. * Ascender Height is the character's most positive y-axis value.
  193. * For bounded character boxes, for a given character having an
  194. * ascender, ascender height and baseline offset are equal.
  195. * @param ascender the ascender to set
  196. */
  197. public void setAscender(int ascender) {
  198. this.ascender = ascender;
  199. }
  200. /**
  201. * Cap height is the average height of the uppercase characters in
  202. * a font. This value is specified by the designer of a font and is
  203. * usually the height of the uppercase M.
  204. * @param capHeight the cap height to set
  205. */
  206. public void setCapHeight(int capHeight) {
  207. this.capHeight = capHeight;
  208. }
  209. /**
  210. * Descender depth is the distance from the character baseline to
  211. * the bottom of a character box. A negative descender depth signifies
  212. * that all of the graphic character is above the character baseline.
  213. * @param descender the descender value in millipoints
  214. */
  215. public void setDescender(int descender) {
  216. this.descender = descender;
  217. }
  218. /**
  219. * TODO
  220. * @param underscoreWidth the underscore width value in millipoints
  221. */
  222. public void setUnderscoreWidth(int underscoreWidth) {
  223. this.underscoreWidth = underscoreWidth;
  224. }
  225. /**
  226. * TODO
  227. * @param underscorePosition the underscore position value in millipoints
  228. */
  229. public void setUnderscorePosition(int underscorePosition) {
  230. this.underscorePosition = underscorePosition;
  231. }
  232. /**
  233. * Set the width (in 1/1000ths of a point size) of the character
  234. * identified by the parameter passed.
  235. * @param character the Unicode character for which the width is being set
  236. * @param width the widths of the character
  237. * @param characterBox the character box
  238. */
  239. public void setCharacterMetrics(char character, int width, Rectangle characterBox) {
  240. characterMetrics.put((int) character, new CharacterMetrics(width, characterBox));
  241. }
  242. /**
  243. * XHeight refers to the height of the lower case letters above
  244. * the baseline.
  245. * @param xHeight the typical height of characters
  246. */
  247. public void setXHeight(int xHeight) {
  248. this.xHeight = xHeight;
  249. }
  250. /**
  251. * Returns the space increment.
  252. * @return the space increment
  253. */
  254. public int getSpaceIncrement() {
  255. return this.spaceIncrement;
  256. }
  257. /**
  258. * Returns the em space increment.
  259. * @return the em space increment
  260. */
  261. public int getEmSpaceIncrement() {
  262. return this.emSpaceIncrement;
  263. }
  264. /**
  265. * Returns the nominal character increment.
  266. * @return the nominal character increment
  267. */
  268. public int getNominalCharIncrement() {
  269. return this.nomCharIncrement;
  270. }
  271. private static class CharacterMetrics {
  272. public final int width;
  273. public final Rectangle characterBox;
  274. public CharacterMetrics(int width, Rectangle characterBox) {
  275. this.width = width;
  276. this.characterBox = characterBox;
  277. }
  278. }
  279. }