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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Iterator;
  20. import java.util.Map;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. /**
  24. * A font where each character is stored as an array of pixels (a bitmap). Such
  25. * fonts are not easily scalable, in contrast to vectored fonts. With this type
  26. * of font, the font metrics information is held in character set files (one for
  27. * each size and style). <p/>
  28. *
  29. */
  30. public class RasterFont extends AFPFont {
  31. /** Static logging instance */
  32. protected static final Log log = LogFactory.getLog("org.apache.fop.afp.fonts");
  33. private final Map/*<String,CharacterSet>*/ charSets
  34. = new java.util.HashMap/*<String,CharacterSet>*/();
  35. private CharacterSet charSet = null;
  36. /**
  37. * Constructor for the raster font requires the name, weight and style
  38. * attribute to be available as this forms the key to the font.
  39. *
  40. * @param name
  41. * the name of the font
  42. */
  43. public RasterFont(String name) {
  44. super(name);
  45. }
  46. /**
  47. * Adds the character set for the given point size
  48. * @param size point size
  49. * @param characterSet character set
  50. */
  51. public void addCharacterSet(int size, CharacterSet characterSet) {
  52. this.charSets.put(String.valueOf(size), characterSet);
  53. this.charSet = characterSet;
  54. }
  55. /** Describes the unit millipoint. */
  56. public static final String MPT = "mpt";
  57. /**
  58. * Get the character set metrics for the specified point size.
  59. *
  60. * @param size the point size
  61. * @return the character set metrics
  62. */
  63. public CharacterSet getCharacterSet(int size) {
  64. String pointsize = String.valueOf(size / 1000);
  65. CharacterSet csm = (CharacterSet) charSets.get(pointsize);
  66. if (csm == null) {
  67. csm = (CharacterSet) charSets.get(size + MPT);
  68. }
  69. if (csm == null) {
  70. // Get char set with nearest font size
  71. int distance = Integer.MAX_VALUE;
  72. for (Iterator it = charSets.entrySet().iterator(); it.hasNext();) {
  73. Map.Entry me = (Map.Entry)it.next();
  74. String key = (String)me.getKey();
  75. if (!key.endsWith(MPT)) {
  76. int mpt = Integer.parseInt(key) * 1000;
  77. if (Math.abs(size - mpt) < distance) {
  78. distance = Math.abs(size - mpt);
  79. pointsize = (String)me.getKey();
  80. csm = (CharacterSet)me.getValue();
  81. }
  82. }
  83. }
  84. if (csm != null) {
  85. charSets.put(size + MPT, csm);
  86. String msg = "No " + (size / 1000) + "pt font " + getFontName()
  87. + " found, substituted with " + pointsize + "pt font";
  88. log.warn(msg);
  89. }
  90. }
  91. if (csm == null) {
  92. String msg = "No font found for font " + getFontName()
  93. + " with point size " + pointsize;
  94. log.error(msg);
  95. throw new FontRuntimeException(msg);
  96. }
  97. return csm;
  98. }
  99. /**
  100. * Get the first character in this font.
  101. * @return the first character in this font.
  102. */
  103. public int getFirstChar() {
  104. Iterator it = charSets.values().iterator();
  105. if (it.hasNext()) {
  106. CharacterSet csm = (CharacterSet) it.next();
  107. return csm.getFirstChar();
  108. } else {
  109. String msg = "getFirstChar() - No character set found for font:" + getFontName();
  110. log.error(msg);
  111. throw new FontRuntimeException(msg);
  112. }
  113. }
  114. /**
  115. * Get the last character in this font.
  116. * @return the last character in this font.
  117. */
  118. public int getLastChar() {
  119. Iterator it = charSets.values().iterator();
  120. if (it.hasNext()) {
  121. CharacterSet csm = (CharacterSet) it.next();
  122. return csm.getLastChar();
  123. } else {
  124. String msg = "getLastChar() - No character set found for font:" + getFontName();
  125. log.error(msg);
  126. throw new FontRuntimeException(msg);
  127. }
  128. }
  129. /**
  130. * The ascender is the part of a lowercase letter that extends above the
  131. * "x-height" (the height of the letter "x"), such as "d", "t", or "h". Also
  132. * used to denote the part of the letter extending above the x-height.
  133. *
  134. * @param size the point size
  135. * @return the ascender for the given point size
  136. */
  137. public int getAscender(int size) {
  138. return getCharacterSet(size).getAscender();
  139. }
  140. /**
  141. * Obtains the height of capital letters for the specified point size.
  142. *
  143. * @param size the point size
  144. * @return the cap height for the specified point size
  145. */
  146. public int getCapHeight(int size) {
  147. return getCharacterSet(size).getCapHeight();
  148. }
  149. /**
  150. * The descender is the part of a lowercase letter that extends below the
  151. * base line, such as "g", "j", or "p". Also used to denote the part of the
  152. * letter extending below the base line.
  153. *
  154. * @param size the point size
  155. * @return the descender for the specified point size
  156. */
  157. public int getDescender(int size) {
  158. return getCharacterSet(size).getDescender();
  159. }
  160. /**
  161. * The "x-height" (the height of the letter "x").
  162. *
  163. * @param size the point size
  164. * @return the x height for the given point size
  165. */
  166. public int getXHeight(int size) {
  167. return getCharacterSet(size).getXHeight();
  168. }
  169. /**
  170. * Obtain the width of the character for the specified point size.
  171. * @param character the character
  172. * @param size the point size
  173. * @return the width for the given point size
  174. */
  175. public int getWidth(int character, int size) {
  176. return getCharacterSet(size).getWidth(character);
  177. }
  178. /**
  179. * Get the getWidth (in 1/1000ths of a point size) of all characters in this
  180. * character set.
  181. *
  182. * @param size
  183. * the point size
  184. * @return the widths of all characters
  185. */
  186. public int[] getWidths(int size) {
  187. return getCharacterSet(size).getWidths();
  188. }
  189. /**
  190. * Get the getWidth (in 1/1000ths of a point size) of all characters in this
  191. * character set.
  192. *
  193. * @return the widths of all characters
  194. */
  195. public int[] getWidths() {
  196. return getWidths(1000);
  197. }
  198. /**
  199. * Map a Unicode character to a code point in the font.
  200. * @param c character to map
  201. * @return the mapped character
  202. */
  203. public char mapChar(char c) {
  204. return charSet.mapChar(c);
  205. }
  206. /** {@inheritDoc} */
  207. public String getEncodingName() {
  208. return charSet.getEncoding();
  209. }
  210. }