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.

RasterFont.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. import java.util.HashMap;
  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;
  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. private int metricsToAbsoluteSize(CharacterSet cs, int value, int givenSize) {
  118. int nominalVerticalSize = cs.getNominalVerticalSize();
  119. if (nominalVerticalSize != 0) {
  120. return value * nominalVerticalSize;
  121. } else {
  122. return value * givenSize;
  123. }
  124. }
  125. private int metricsToAbsoluteSize(CharacterSet cs, double value, int givenSize) {
  126. int nominalVerticalSize = cs.getNominalVerticalSize();
  127. if (nominalVerticalSize != 0) {
  128. return (int) (value * nominalVerticalSize);
  129. } else {
  130. return (int) (value * givenSize);
  131. }
  132. }
  133. /**
  134. * The ascender is the part of a lowercase letter that extends above the
  135. * "x-height" (the height of the letter "x"), such as "d", "t", or "h". Also
  136. * used to denote the part of the letter extending above the x-height.
  137. *
  138. * @param size the font size (in mpt)
  139. * @return the ascender for the given point size
  140. */
  141. public int getAscender(int size) {
  142. CharacterSet cs = getCharacterSet(size);
  143. return metricsToAbsoluteSize(cs, cs.getAscender(), size);
  144. }
  145. /** {@inheritDoc} */
  146. public int getUnderlinePosition(int size) {
  147. CharacterSet cs = getCharacterSet(size);
  148. return metricsToAbsoluteSize(cs, cs.getUnderscorePosition(), size);
  149. }
  150. @Override
  151. public int getUnderlineThickness(int size) {
  152. CharacterSet cs = getCharacterSet(size);
  153. int underscoreWidth = cs.getUnderscoreWidth();
  154. return underscoreWidth == 0 ? super.getUnderlineThickness(size)
  155. : metricsToAbsoluteSize(cs, underscoreWidth, size);
  156. }
  157. /**
  158. * Obtains the height of capital letters for the specified point size.
  159. *
  160. * @param size the font size (in mpt)
  161. * @return the cap height for the specified point size
  162. */
  163. public int getCapHeight(int size) {
  164. CharacterSet cs = getCharacterSet(size);
  165. return metricsToAbsoluteSize(cs, cs.getCapHeight(), size);
  166. }
  167. /**
  168. * The descender is the part of a lowercase letter that extends below the
  169. * base line, such as "g", "j", or "p". Also used to denote the part of the
  170. * letter extending below the base line.
  171. *
  172. * @param size the font size (in mpt)
  173. * @return the descender for the specified point size
  174. */
  175. public int getDescender(int size) {
  176. CharacterSet cs = getCharacterSet(size);
  177. return metricsToAbsoluteSize(cs, cs.getDescender(), size);
  178. }
  179. /**
  180. * The "x-height" (the height of the letter "x").
  181. *
  182. * @param size the font size (in mpt)
  183. * @return the x height for the given point size
  184. */
  185. public int getXHeight(int size) {
  186. CharacterSet cs = getCharacterSet(size);
  187. return metricsToAbsoluteSize(cs, cs.getXHeight(), size);
  188. }
  189. /**
  190. * Obtain the width of the character for the specified point size.
  191. * @param character the character
  192. * @param size the font size (in mpt)
  193. * @return the width for the given point size
  194. */
  195. public int getWidth(int character, int size) {
  196. CharacterSet cs = getCharacterSet(size);
  197. return metricsToAbsoluteSize(cs, cs.getWidth(toUnicodeCodepoint(character), 1), size);
  198. }
  199. /**
  200. * TODO
  201. */
  202. public Rectangle getBoundingBox(int character, int size) {
  203. CharacterSet cs = getCharacterSet(size);
  204. Rectangle characterBox = cs.getCharacterBox(toUnicodeCodepoint(character), 1);
  205. int x = metricsToAbsoluteSize(cs, characterBox.getX(), size);
  206. int y = metricsToAbsoluteSize(cs, characterBox.getY(), size);
  207. int w = metricsToAbsoluteSize(cs, characterBox.getWidth(), size);
  208. int h = metricsToAbsoluteSize(cs, characterBox.getHeight(), size);
  209. return new Rectangle(x, y, w, h);
  210. }
  211. /** {@inheritDoc} */
  212. public boolean hasChar(char c) {
  213. return charSet.hasChar(c);
  214. }
  215. /**
  216. * Map a Unicode character to a code point in the font.
  217. * @param c character to map
  218. * @return the mapped character
  219. */
  220. public char mapChar(char c) {
  221. return charSet.mapChar(c);
  222. }
  223. /** {@inheritDoc} */
  224. public String getEncodingName() {
  225. return charSet.getEncoding();
  226. }
  227. }