Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RasterFont.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.HashMap;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.SortedMap;
  23. import java.util.TreeMap;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. /**
  27. * A font where each character is stored as an array of pixels (a bitmap). Such
  28. * fonts are not easily scalable, in contrast to vectored fonts. With this type
  29. * of font, the font metrics information is held in character set files (one for
  30. * each size and style). <p/>
  31. *
  32. */
  33. public class RasterFont extends AFPFont {
  34. /** Static logging instance */
  35. protected static final Log LOG = LogFactory.getLog("org.apache.fop.afp.fonts");
  36. private final SortedMap<Integer, CharacterSet> charSets = new TreeMap<Integer, CharacterSet>();
  37. private Map<Integer, CharacterSet> substitutionCharSets;
  38. private CharacterSet charSet = null;
  39. /**
  40. * Constructor for the raster font requires the name, weight and style
  41. * attribute to be available as this forms the key to the font.
  42. *
  43. * @param name
  44. * the name of the font
  45. */
  46. public RasterFont(String name, boolean embeddable) {
  47. super(name, embeddable);
  48. }
  49. /**
  50. * Adds the character set for the given point size
  51. * @param size point size (in mpt)
  52. * @param characterSet character set
  53. */
  54. public void addCharacterSet(int size, CharacterSet characterSet) {
  55. //TODO: replace with Integer.valueOf() once we switch to Java 5
  56. this.charSets.put(new Integer(size), characterSet);
  57. this.charSet = characterSet;
  58. }
  59. /**
  60. * Get the character set metrics for the specified point size.
  61. *
  62. * @param sizeInMpt the point size (in mpt)
  63. * @return the character set metrics
  64. */
  65. public CharacterSet getCharacterSet(int sizeInMpt) {
  66. Integer requestedSize = Integer.valueOf(sizeInMpt);
  67. CharacterSet csm = charSets.get(requestedSize);
  68. double sizeInPt = sizeInMpt / 1000.0;
  69. if (csm != null) {
  70. return csm;
  71. }
  72. if (substitutionCharSets != null) {
  73. //Check first if a substitution has already been added
  74. csm = substitutionCharSets.get(requestedSize);
  75. }
  76. if (csm == null && !charSets.isEmpty()) {
  77. // No match or substitution found, but there exist entries
  78. // for other sizes
  79. // Get char set with nearest, smallest font size
  80. SortedMap<Integer, CharacterSet> smallerSizes = charSets.headMap(requestedSize);
  81. SortedMap<Integer, CharacterSet> largerSizes = charSets.tailMap(requestedSize);
  82. int smallerSize = smallerSizes.isEmpty() ? 0
  83. : smallerSizes.lastKey().intValue();
  84. int largerSize = largerSizes.isEmpty() ? Integer.MAX_VALUE
  85. : largerSizes.firstKey().intValue();
  86. Integer fontSize;
  87. if (!smallerSizes.isEmpty()
  88. && (sizeInMpt - smallerSize) <= (largerSize - sizeInMpt)) {
  89. fontSize = Integer.valueOf(smallerSize);
  90. } else {
  91. fontSize = Integer.valueOf(largerSize);
  92. }
  93. csm = charSets.get(fontSize);
  94. if (csm != null) {
  95. // Add the substitute mapping, so subsequent calls will
  96. // find it immediately
  97. if (substitutionCharSets == null) {
  98. substitutionCharSets = new HashMap<Integer, CharacterSet>();
  99. }
  100. substitutionCharSets.put(requestedSize, csm);
  101. // do not output the warning if the font size is closer to an integer less than 0.1
  102. if (!(Math.abs(fontSize.intValue() / 1000.0 - sizeInPt) < 0.1)) {
  103. String msg = "No " + sizeInPt + "pt font " + getFontName()
  104. + " found, substituted with " + fontSize.intValue() / 1000f + "pt font";
  105. LOG.warn(msg);
  106. }
  107. }
  108. }
  109. if (csm == null) {
  110. // Still no match -> error
  111. String msg = "No font found for font " + getFontName() + " with point size " + sizeInPt;
  112. LOG.error(msg);
  113. throw new FontRuntimeException(msg);
  114. }
  115. return csm;
  116. }
  117. /**
  118. * Get the first character in this font.
  119. * @return the first character in this font.
  120. */
  121. public int getFirstChar() {
  122. Iterator<CharacterSet> it = charSets.values().iterator();
  123. if (it.hasNext()) {
  124. CharacterSet csm = it.next();
  125. return csm.getFirstChar();
  126. } else {
  127. String msg = "getFirstChar() - No character set found for font:" + getFontName();
  128. LOG.error(msg);
  129. throw new FontRuntimeException(msg);
  130. }
  131. }
  132. /**
  133. * Get the last character in this font.
  134. * @return the last character in this font.
  135. */
  136. public int getLastChar() {
  137. Iterator<CharacterSet> it = charSets.values().iterator();
  138. if (it.hasNext()) {
  139. CharacterSet csm = it.next();
  140. return csm.getLastChar();
  141. } else {
  142. String msg = "getLastChar() - No character set found for font:" + getFontName();
  143. LOG.error(msg);
  144. throw new FontRuntimeException(msg);
  145. }
  146. }
  147. private int metricsToAbsoluteSize(CharacterSet cs, int value, int givenSize) {
  148. int nominalVerticalSize = cs.getNominalVerticalSize();
  149. if (nominalVerticalSize != 0) {
  150. return value * nominalVerticalSize;
  151. } else {
  152. return value * givenSize;
  153. }
  154. }
  155. /**
  156. * The ascender is the part of a lowercase letter that extends above the
  157. * "x-height" (the height of the letter "x"), such as "d", "t", or "h". Also
  158. * used to denote the part of the letter extending above the x-height.
  159. *
  160. * @param size the font size (in mpt)
  161. * @return the ascender for the given point size
  162. */
  163. public int getAscender(int size) {
  164. CharacterSet cs = getCharacterSet(size);
  165. return metricsToAbsoluteSize(cs, cs.getAscender(), size);
  166. }
  167. /**
  168. * Obtains the height of capital letters for the specified point size.
  169. *
  170. * @param size the font size (in mpt)
  171. * @return the cap height for the specified point size
  172. */
  173. public int getCapHeight(int size) {
  174. CharacterSet cs = getCharacterSet(size);
  175. return metricsToAbsoluteSize(cs, cs.getCapHeight(), size);
  176. }
  177. /**
  178. * The descender is the part of a lowercase letter that extends below the
  179. * base line, such as "g", "j", or "p". Also used to denote the part of the
  180. * letter extending below the base line.
  181. *
  182. * @param size the font size (in mpt)
  183. * @return the descender for the specified point size
  184. */
  185. public int getDescender(int size) {
  186. CharacterSet cs = getCharacterSet(size);
  187. return metricsToAbsoluteSize(cs, cs.getDescender(), size);
  188. }
  189. /**
  190. * The "x-height" (the height of the letter "x").
  191. *
  192. * @param size the font size (in mpt)
  193. * @return the x height for the given point size
  194. */
  195. public int getXHeight(int size) {
  196. CharacterSet cs = getCharacterSet(size);
  197. return metricsToAbsoluteSize(cs, cs.getXHeight(), size);
  198. }
  199. /**
  200. * Obtain the width of the character for the specified point size.
  201. * @param character the character
  202. * @param size the font size (in mpt)
  203. * @return the width for the given point size
  204. */
  205. public int getWidth(int character, int size) {
  206. CharacterSet cs = getCharacterSet(size);
  207. return metricsToAbsoluteSize(cs, cs.getWidth(toUnicodeCodepoint(character)), size);
  208. }
  209. /**
  210. * Get the getWidth (in 1/1000ths of a point size) of all characters in this
  211. * character set.
  212. *
  213. * @param size the font size (in mpt)
  214. * @return the widths of all characters
  215. */
  216. public int[] getWidths(int size) {
  217. CharacterSet cs = getCharacterSet(size);
  218. int[] widths = cs.getWidths();
  219. for (int i = 0, c = widths.length; i < c; i++) {
  220. widths[i] = metricsToAbsoluteSize(cs, widths[i], size);
  221. }
  222. return widths;
  223. }
  224. /**
  225. * Get the getWidth (in 1/1000ths of a point size) of all characters in this
  226. * character set.
  227. *
  228. * @return the widths of all characters
  229. */
  230. public int[] getWidths() {
  231. return getWidths(1000);
  232. }
  233. /** {@inheritDoc} */
  234. public boolean hasChar(char c) {
  235. return charSet.hasChar(c);
  236. }
  237. /**
  238. * Map a Unicode character to a code point in the font.
  239. * @param c character to map
  240. * @return the mapped character
  241. */
  242. public char mapChar(char c) {
  243. return charSet.mapChar(c);
  244. }
  245. /** {@inheritDoc} */
  246. public String getEncodingName() {
  247. return charSet.getEncoding();
  248. }
  249. }