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.

Font.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.fonts;
  18. import java.util.Map;
  19. /**
  20. * This class holds font state information and provides access to the font
  21. * metrics.
  22. */
  23. public class Font {
  24. /** Default fallback key */
  25. public static final String DEFAULT_FONT = "any,normal,400";
  26. /** Normal font weight */
  27. public static final int NORMAL = 400;
  28. /** Bold font weight */
  29. public static final int BOLD = 700;
  30. private String fontName;
  31. private int fontSize;
  32. //private String fontFamily;
  33. //private String fontStyle;
  34. //private int fontWeight;
  35. /**
  36. * normal or small-caps font
  37. */
  38. //private int fontVariant;
  39. private FontMetrics metric;
  40. /**
  41. * Main constructor
  42. * @param key key of the font
  43. * @param met font metrics
  44. * @param fontSize font size
  45. */
  46. public Font(String key, FontMetrics met, int fontSize) {
  47. this.fontName = key;
  48. this.metric = met;
  49. this.fontSize = fontSize;
  50. }
  51. /**
  52. * Returns the font's ascender.
  53. * @return the ascender
  54. */
  55. public int getAscender() {
  56. return metric.getAscender(fontSize) / 1000;
  57. }
  58. /**
  59. * Returns the font's CapHeight.
  60. * @return the capital height
  61. */
  62. public int getCapHeight() {
  63. return metric.getCapHeight(fontSize) / 1000;
  64. }
  65. /**
  66. * Returns the font's Descender.
  67. * @return the descender
  68. */
  69. public int getDescender() {
  70. return metric.getDescender(fontSize) / 1000;
  71. }
  72. /**
  73. * Returns the font's name.
  74. * @return the font name
  75. */
  76. public String getFontName() {
  77. return fontName;
  78. }
  79. /**
  80. * Returns the font size
  81. * @return the font size
  82. */
  83. public int getFontSize() {
  84. return fontSize;
  85. }
  86. /**
  87. * Returns the XHeight
  88. * @return the XHeight
  89. */
  90. public int getXHeight() {
  91. return metric.getXHeight(fontSize) / 1000;
  92. }
  93. /**
  94. * Returns the font's kerning table
  95. * @return the kerning table
  96. */
  97. public Map getKerning() {
  98. Map ret = metric.getKerningInfo();
  99. if (ret != null) {
  100. return ret;
  101. } else {
  102. return java.util.Collections.EMPTY_MAP;
  103. }
  104. }
  105. /**
  106. * Returns the width of a character
  107. * @param charnum character to look up
  108. * @return width of the character
  109. */
  110. public int getWidth(int charnum) {
  111. // returns width of given character number in millipoints
  112. return (metric.getWidth(charnum, fontSize) / 1000);
  113. }
  114. /**
  115. * Map a java character (unicode) to a font character.
  116. * Default uses CodePointMapping.
  117. * @param c character to map
  118. * @return the mapped character
  119. */
  120. public char mapChar(char c) {
  121. if (metric instanceof org.apache.fop.fonts.Typeface) {
  122. return ((org.apache.fop.fonts.Typeface)metric).mapChar(c);
  123. }
  124. // Use default CodePointMapping
  125. char d = CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c);
  126. if (d != 0) {
  127. c = d;
  128. } else {
  129. c = '#';
  130. }
  131. return c;
  132. }
  133. /**
  134. * @see java.lang.Object#toString()
  135. */
  136. public String toString() {
  137. StringBuffer sbuf = new StringBuffer();
  138. sbuf.append('(');
  139. /*
  140. sbuf.append(fontFamily);
  141. sbuf.append(',');*/
  142. sbuf.append(fontName);
  143. sbuf.append(',');
  144. sbuf.append(fontSize);
  145. /*
  146. sbuf.append(',');
  147. sbuf.append(fontStyle);
  148. sbuf.append(',');
  149. sbuf.append(fontWeight);*/
  150. sbuf.append(')');
  151. return sbuf.toString();
  152. }
  153. /**
  154. * Helper method for getting the width of a unicode char
  155. * from the current fontstate.
  156. * This also performs some guessing on widths on various
  157. * versions of space that might not exists in the font.
  158. * @param c character to inspect
  159. * @param fs FontState to use
  160. * @return the width of the character
  161. */
  162. public int getCharWidth(char c) {
  163. int width;
  164. if ((c == '\n') || (c == '\r') || (c == '\t') || (c == '\u00A0')) {
  165. width = getCharWidth(' ');
  166. } else {
  167. width = getWidth(mapChar(c));
  168. if (width <= 0) {
  169. // Estimate the width of spaces not represented in
  170. // the font
  171. int em = getWidth(mapChar('m'));
  172. int en = getWidth(mapChar('n'));
  173. if (em <= 0) {
  174. em = 500 * getFontSize();
  175. }
  176. if (en <= 0) {
  177. en = em - 10;
  178. }
  179. if (c == ' ') {
  180. width = em;
  181. }
  182. if (c == '\u2000') {
  183. width = en;
  184. }
  185. if (c == '\u2001') {
  186. width = em;
  187. }
  188. if (c == '\u2002') {
  189. width = em / 2;
  190. }
  191. if (c == '\u2003') {
  192. width = getFontSize();
  193. }
  194. if (c == '\u2004') {
  195. width = em / 3;
  196. }
  197. if (c == '\u2005') {
  198. width = em / 4;
  199. }
  200. if (c == '\u2006') {
  201. width = em / 6;
  202. }
  203. if (c == '\u2007') {
  204. width = getCharWidth(' ');
  205. }
  206. if (c == '\u2008') {
  207. width = getCharWidth('.');
  208. }
  209. if (c == '\u2009') {
  210. width = em / 5;
  211. }
  212. if (c == '\u200A') {
  213. width = 5;
  214. }
  215. if (c == '\u200B') {
  216. width = 100;
  217. }
  218. if (c == '\u202F') {
  219. width = getCharWidth(' ') / 2;
  220. }
  221. if (c == '\u3000') {
  222. width = getCharWidth(' ') * 2;
  223. }
  224. }
  225. }
  226. return width;
  227. }
  228. /**
  229. * Calculates the word width.
  230. */
  231. public int getWordWidth(String word) {
  232. if (word == null)
  233. return 0;
  234. int wordLength = word.length();
  235. int width = 0;
  236. char[] characters = new char[wordLength];
  237. word.getChars(0, wordLength, characters, 0);
  238. for (int i = 0; i < wordLength; i++) {
  239. width += getCharWidth(characters[i]);
  240. }
  241. return width;
  242. }
  243. }