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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.fonts;
  19. import java.util.Map;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. /**
  23. * This class holds font state information and provides access to the font
  24. * metrics.
  25. */
  26. public class Font {
  27. /** Extra Bold font weight */
  28. public static final int WEIGHT_EXTRA_BOLD = 800;
  29. /** Bold font weight */
  30. public static final int WEIGHT_BOLD = 700;
  31. /** Normal font weight */
  32. public static final int WEIGHT_NORMAL = 400;
  33. /** Light font weight */
  34. public static final int WEIGHT_LIGHT = 200;
  35. /** Normal font style */
  36. public static final String STYLE_NORMAL = "normal";
  37. /** Italic font style */
  38. public static final String STYLE_ITALIC = "italic";
  39. /** Default fallback key */
  40. public static final FontTriplet DEFAULT_FONT = new FontTriplet(
  41. "any", STYLE_NORMAL, WEIGHT_NORMAL);
  42. /** logger */
  43. private static Log log = LogFactory.getLog(Font.class);
  44. private String fontName;
  45. private FontTriplet triplet;
  46. private int fontSize;
  47. /**
  48. * normal or small-caps font
  49. */
  50. //private int fontVariant;
  51. private FontMetrics metric;
  52. /**
  53. * Main constructor
  54. * @param key key of the font
  55. * @param triplet the font triplet that was used to lookup this font (may be null)
  56. * @param met font metrics
  57. * @param fontSize font size
  58. */
  59. public Font(String key, FontTriplet triplet, FontMetrics met, int fontSize) {
  60. this.fontName = key;
  61. this.triplet = triplet;
  62. this.metric = met;
  63. this.fontSize = fontSize;
  64. }
  65. /**
  66. * Returns the associated font metrics object.
  67. * @return the font metrics
  68. */
  69. public FontMetrics getFontMetrics() {
  70. return this.metric;
  71. }
  72. /**
  73. * Returns the font's ascender.
  74. * @return the ascender
  75. */
  76. public int getAscender() {
  77. return metric.getAscender(fontSize) / 1000;
  78. }
  79. /**
  80. * Returns the font's CapHeight.
  81. * @return the capital height
  82. */
  83. public int getCapHeight() {
  84. return metric.getCapHeight(fontSize) / 1000;
  85. }
  86. /**
  87. * Returns the font's Descender.
  88. * @return the descender
  89. */
  90. public int getDescender() {
  91. return metric.getDescender(fontSize) / 1000;
  92. }
  93. /**
  94. * Returns the font's name.
  95. * @return the font name
  96. */
  97. public String getFontName() {
  98. return fontName;
  99. }
  100. /** @return the font triplet that selected this font */
  101. public FontTriplet getFontTriplet() {
  102. return this.triplet;
  103. }
  104. /**
  105. * Returns the font size
  106. * @return the font size
  107. */
  108. public int getFontSize() {
  109. return fontSize;
  110. }
  111. /**
  112. * Returns the XHeight
  113. * @return the XHeight
  114. */
  115. public int getXHeight() {
  116. return metric.getXHeight(fontSize) / 1000;
  117. }
  118. /** @return true if the font has kerning info */
  119. public boolean hasKerning() {
  120. return metric.hasKerningInfo();
  121. }
  122. /**
  123. * Returns the font's kerning table
  124. * @return the kerning table
  125. */
  126. public Map getKerning() {
  127. if (metric.hasKerningInfo()) {
  128. return metric.getKerningInfo();
  129. } else {
  130. return java.util.Collections.EMPTY_MAP;
  131. }
  132. }
  133. /**
  134. * Returns the amount of kerning between two characters.
  135. * @param ch1 first character
  136. * @param ch2 second character
  137. * @return the distance to adjust for kerning, 0 if there's no kerning
  138. */
  139. public int getKernValue(char ch1, char ch2) {
  140. Map kernPair = (Map)getKerning().get(new Integer(ch1));
  141. if (kernPair != null) {
  142. Integer width = (Integer)kernPair.get(new Integer(ch2));
  143. if (width != null) {
  144. return width.intValue();
  145. }
  146. }
  147. return 0;
  148. }
  149. /**
  150. * Returns the width of a character
  151. * @param charnum character to look up
  152. * @return width of the character
  153. */
  154. public int getWidth(int charnum) {
  155. // returns width of given character number in millipoints
  156. return (metric.getWidth(charnum, fontSize) / 1000);
  157. }
  158. /**
  159. * Map a java character (unicode) to a font character.
  160. * Default uses CodePointMapping.
  161. * @param c character to map
  162. * @return the mapped character
  163. */
  164. public char mapChar(char c) {
  165. if (metric instanceof org.apache.fop.fonts.Typeface) {
  166. return ((org.apache.fop.fonts.Typeface)metric).mapChar(c);
  167. }
  168. // Use default CodePointMapping
  169. char d = CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c);
  170. if (d != SingleByteEncoding.NOT_FOUND_CODE_POINT) {
  171. c = d;
  172. } else {
  173. log.warn("Glyph " + (int) c + " not available in font " + fontName);
  174. c = '#';
  175. }
  176. return c;
  177. }
  178. /**
  179. * Determines whether this font contains a particular character/glyph.
  180. * @param c character to check
  181. * @return True if the character is supported, Falso otherwise
  182. */
  183. public boolean hasChar(char c) {
  184. if (metric instanceof org.apache.fop.fonts.Typeface) {
  185. return ((org.apache.fop.fonts.Typeface)metric).hasChar(c);
  186. } else {
  187. // Use default CodePointMapping
  188. return (CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c) > 0);
  189. }
  190. }
  191. /**
  192. * {@inheritDoc}
  193. */
  194. public String toString() {
  195. StringBuffer sbuf = new StringBuffer();
  196. sbuf.append('(');
  197. /*
  198. sbuf.append(fontFamily);
  199. sbuf.append(',');*/
  200. sbuf.append(fontName);
  201. sbuf.append(',');
  202. sbuf.append(fontSize);
  203. /*
  204. sbuf.append(',');
  205. sbuf.append(fontStyle);
  206. sbuf.append(',');
  207. sbuf.append(fontWeight);*/
  208. sbuf.append(')');
  209. return sbuf.toString();
  210. }
  211. /**
  212. * Helper method for getting the width of a unicode char
  213. * from the current fontstate.
  214. * This also performs some guessing on widths on various
  215. * versions of space that might not exists in the font.
  216. * @param c character to inspect
  217. * @return the width of the character
  218. */
  219. public int getCharWidth(char c) {
  220. int width;
  221. if ((c == '\n') || (c == '\r') || (c == '\t') || (c == '\u00A0')) {
  222. width = getCharWidth(' ');
  223. } else {
  224. if (hasChar(c)) {
  225. width = getWidth(mapChar(c));
  226. } else {
  227. width = -1;
  228. }
  229. if (width <= 0) {
  230. // Estimate the width of spaces not represented in
  231. // the font
  232. int em = getFontSize(); //http://en.wikipedia.org/wiki/Em_(typography)
  233. int en = em / 2; //http://en.wikipedia.org/wiki/En_(typography)
  234. if (c == ' ') {
  235. width = em;
  236. } else if (c == '\u2000') {
  237. width = en;
  238. } else if (c == '\u2001') {
  239. width = em;
  240. } else if (c == '\u2002') {
  241. width = em / 2;
  242. } else if (c == '\u2003') {
  243. width = getFontSize();
  244. } else if (c == '\u2004') {
  245. width = em / 3;
  246. } else if (c == '\u2005') {
  247. width = em / 4;
  248. } else if (c == '\u2006') {
  249. width = em / 6;
  250. } else if (c == '\u2007') {
  251. width = getCharWidth('0');
  252. } else if (c == '\u2008') {
  253. width = getCharWidth('.');
  254. } else if (c == '\u2009') {
  255. width = em / 5;
  256. } else if (c == '\u200A') {
  257. width = em / 10;
  258. } else if (c == '\u200B') {
  259. width = 0;
  260. } else if (c == '\u202F') {
  261. width = getCharWidth(' ') / 2;
  262. } else if (c == '\u2060') {
  263. width = 0;
  264. } else if (c == '\u3000') {
  265. width = getCharWidth(' ') * 2;
  266. } else if (c == '\ufeff') {
  267. width = 0;
  268. } else {
  269. //Will be internally replaced by "#" if not found
  270. width = getWidth(mapChar(c));
  271. }
  272. }
  273. }
  274. return width;
  275. }
  276. /**
  277. * Calculates the word width.
  278. * @param word text to get width for
  279. * @return the width of the text
  280. */
  281. public int getWordWidth(String word) {
  282. if (word == null) {
  283. return 0;
  284. }
  285. int wordLength = word.length();
  286. int width = 0;
  287. char[] characters = new char[wordLength];
  288. word.getChars(0, wordLength, characters, 0);
  289. for (int i = 0; i < wordLength; i++) {
  290. width += getCharWidth(characters[i]);
  291. }
  292. return width;
  293. }
  294. }