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.1KB

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