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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.afp.fonts;
  18. import java.util.HashMap;
  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. import org.apache.fop.render.afp.exceptions.FontRuntimeException;
  24. /**
  25. * A font where each character is stored as an array of pixels (a bitmap). Such
  26. * fonts are not easily scalable, in contrast to vectored fonts. With this type
  27. * of font, the font metrics information is held in character set files (one for
  28. * each size and style). <p/>
  29. *
  30. */
  31. public class RasterFont extends AFPFont {
  32. /** Static logging instance */
  33. protected static final Log log = LogFactory.getLog("org.apache.fop.render.afp.fonts");
  34. private HashMap _characterSets = new HashMap();
  35. private CharacterSet _characterSet = 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. public void addCharacterSet(int size, CharacterSet characterSet) {
  47. _characterSets.put(String.valueOf(size), characterSet);
  48. _characterSet = characterSet;
  49. }
  50. /**
  51. * Get the character set metrics for the specified point size.
  52. *
  53. * @param size the point size
  54. * @return the character set metrics
  55. */
  56. public CharacterSet getCharacterSet(int size) {
  57. String pointsize = String.valueOf(size / 1000);
  58. CharacterSet csm = (CharacterSet) _characterSets.get(pointsize);
  59. if (csm == null) {
  60. csm = (CharacterSet) _characterSets.get(size + "mpt");
  61. }
  62. if (csm == null) {
  63. // Get char set with nearest font size
  64. int distance = Integer.MAX_VALUE;
  65. for (Iterator it = _characterSets.entrySet().iterator(); it.hasNext(); ) {
  66. Map.Entry me = (Map.Entry)it.next();
  67. String key = (String)me.getKey();
  68. if (!key.endsWith("mpt")) {
  69. int mpt = Integer.parseInt(key) * 1000;
  70. if (Math.abs(size - mpt) < distance) {
  71. distance = Math.abs(size - mpt);
  72. pointsize = (String)me.getKey();
  73. csm = (CharacterSet)me.getValue();
  74. }
  75. }
  76. }
  77. if (csm != null) {
  78. _characterSets.put(size + "mpt", csm);
  79. String msg = "No " + (size / 1000) + "pt font " + _name
  80. + " found, substituted with " + pointsize + "pt font";
  81. log.warn(msg);
  82. }
  83. }
  84. if (csm == null) {
  85. String msg = "No font found for font " + _name
  86. + " with point size " + pointsize;
  87. log.error(msg);
  88. throw new FontRuntimeException(msg);
  89. }
  90. return csm;
  91. }
  92. /**
  93. * Get the first character in this font.
  94. */
  95. public int getFirstChar() {
  96. Iterator i = _characterSets.values().iterator();
  97. if (i.hasNext()) {
  98. CharacterSet csm = (CharacterSet) i.next();
  99. return csm.getFirstChar();
  100. } else {
  101. String msg = "getFirstChar() - No character set found for font:" + _name;
  102. log.error(msg);
  103. throw new FontRuntimeException(msg);
  104. }
  105. }
  106. /**
  107. * Get the last character in this font.
  108. */
  109. public int getLastChar() {
  110. Iterator i = _characterSets.values().iterator();
  111. if (i.hasNext()) {
  112. CharacterSet csm = (CharacterSet) i.next();
  113. return csm.getLastChar();
  114. } else {
  115. String msg = "getLastChar() - No character set found for font:" + _name;
  116. log.error(msg);
  117. throw new FontRuntimeException(msg);
  118. }
  119. }
  120. /**
  121. * The ascender is the part of a lowercase letter that extends above the
  122. * "x-height" (the height of the letter "x"), such as "d", "t", or "h". Also
  123. * used to denote the part of the letter extending above the x-height.
  124. *
  125. * @param size the point size
  126. */
  127. public int getAscender(int size) {
  128. return getCharacterSet(size).getAscender();
  129. }
  130. /**
  131. * Obtains the height of capital letters for the specified point size.
  132. *
  133. * @param size the point size
  134. */
  135. public int getCapHeight(int size) {
  136. return getCharacterSet(size).getCapHeight();
  137. }
  138. /**
  139. * The descender is the part of a lowercase letter that extends below the
  140. * base line, such as "g", "j", or "p". Also used to denote the part of the
  141. * letter extending below the base line.
  142. *
  143. * @param size the point size
  144. */
  145. public int getDescender(int size) {
  146. return getCharacterSet(size).getDescender();
  147. }
  148. /**
  149. * The "x-height" (the height of the letter "x").
  150. *
  151. * @param size the point size
  152. */
  153. public int getXHeight(int size) {
  154. return getCharacterSet(size).getXHeight();
  155. }
  156. /**
  157. * Obtain the width of the character for the specified point size.
  158. */
  159. public int getWidth(int character, int size) {
  160. return getCharacterSet(size).width(character);
  161. }
  162. /**
  163. * Get the getWidth (in 1/1000ths of a point size) of all characters in this
  164. * character set.
  165. *
  166. * @param size
  167. * the point size
  168. * @return the widths of all characters
  169. */
  170. public int[] getWidths(int size) {
  171. return getCharacterSet(size).getWidths();
  172. }
  173. /**
  174. * Get the getWidth (in 1/1000ths of a point size) of all characters in this
  175. * character set.
  176. *
  177. * @return the widths of all characters
  178. */
  179. public int[] getWidths() {
  180. return getWidths(1000);
  181. }
  182. /**
  183. * Map a Unicode character to a code point in the font.
  184. * @param c character to map
  185. * @return the mapped character
  186. */
  187. public char mapChar(char c) {
  188. return _characterSet.mapChar(c);
  189. }
  190. /**
  191. * Get the encoding of the font.
  192. * @return the encoding
  193. */
  194. public String getEncoding() {
  195. return _characterSet.getEncoding();
  196. }
  197. }