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.

CustomFontMetricsMapper.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.render.java2d;
  19. import java.awt.Font;
  20. import java.awt.FontFormatException;
  21. import java.awt.Rectangle;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.net.URI;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import org.apache.fop.complexscripts.fonts.Positionable;
  29. import org.apache.fop.complexscripts.fonts.Substitutable;
  30. import org.apache.fop.fonts.CustomFont;
  31. import org.apache.fop.fonts.FontType;
  32. import org.apache.fop.fonts.LazyFont;
  33. import org.apache.fop.fonts.Typeface;
  34. /**
  35. * FontMetricsMapper that delegates most methods to an underlying
  36. * {@link org.apache.fop.fonts.FontMetrics} instance. This class was designed to allow
  37. * the underlying {@link Font} to be loaded from a
  38. * user-configured file not registered in the current graphics environment.
  39. */
  40. public class CustomFontMetricsMapper extends Typeface implements FontMetricsMapper, Substitutable,
  41. Positionable {
  42. /**
  43. * Font metrics for the font this class models.
  44. */
  45. private Typeface typeface;
  46. /**
  47. * The font required by the Java2D renderer.
  48. */
  49. private java.awt.Font font;
  50. /**
  51. * Maintains the most recently requested size.
  52. */
  53. private float size = 1;
  54. /**
  55. * Construction of this class results in the immediate construction
  56. * of the underlying {@link java.awt.Font}.
  57. * @param fontMetrics the metrics of the custom font
  58. * @throws FontFormatException if a bad font is loaded
  59. * @throws IOException if an I/O error occurs
  60. */
  61. public CustomFontMetricsMapper(final CustomFont fontMetrics)
  62. throws FontFormatException, IOException {
  63. this.typeface = fontMetrics;
  64. initialize(fontMetrics.getInputStream());
  65. }
  66. /**
  67. * Construction of this class results in the immediate construction
  68. * of the underlying {@link java.awt.Font}.
  69. * @param fontMetrics the font
  70. * @param fontSource the font source to access the font
  71. * @throws FontFormatException if a bad font is loaded
  72. * @throws IOException if an I/O error occurs
  73. */
  74. public CustomFontMetricsMapper(final LazyFont fontMetrics, final InputStream fontSource)
  75. throws FontFormatException, IOException {
  76. this.typeface = fontMetrics;
  77. initialize(fontSource);
  78. }
  79. private static final int TYPE1_FONT = 1; //Defined in Java 1.5
  80. /**
  81. * Loads the java.awt.Font
  82. * @param inStream
  83. * @throws FontFormatException
  84. * @throws IOException
  85. */
  86. private void initialize(final InputStream inStream)
  87. throws FontFormatException, IOException {
  88. int type = Font.TRUETYPE_FONT;
  89. if (FontType.TYPE1.equals(typeface.getFontType())) {
  90. type = TYPE1_FONT; //Font.TYPE1_FONT; only available in Java 1.5
  91. }
  92. this.font = Font.createFont(type, inStream);
  93. inStream.close();
  94. }
  95. /** {@inheritDoc} */
  96. public final String getEncodingName() {
  97. return null; //Not applicable to Java2D rendering
  98. }
  99. /** {@inheritDoc} */
  100. public final boolean hasChar(final char c) {
  101. return font.canDisplay(c);
  102. }
  103. /** {@inheritDoc} */
  104. public final char mapChar(final char c) {
  105. return typeface.mapChar(c);
  106. }
  107. /** {@inheritDoc} */
  108. public final Font getFont(final int size) {
  109. if (this.size == size) {
  110. return font;
  111. }
  112. this.size = size / 1000f;
  113. font = font.deriveFont(this.size);
  114. return font;
  115. }
  116. /** {@inheritDoc} */
  117. public final int getAscender(final int size) {
  118. return typeface.getAscender(size);
  119. }
  120. /** {@inheritDoc} */
  121. public final int getCapHeight(final int size) {
  122. return typeface.getCapHeight(size);
  123. }
  124. /** {@inheritDoc} */
  125. public final int getDescender(final int size) {
  126. return typeface.getDescender(size);
  127. }
  128. /** {@inheritDoc} */
  129. public final String getEmbedFontName() {
  130. return typeface.getEmbedFontName();
  131. }
  132. /** {@inheritDoc} */
  133. public final Set<String> getFamilyNames() {
  134. return typeface.getFamilyNames();
  135. }
  136. /** {@inheritDoc} */
  137. public final String getFontName() {
  138. return typeface.getFontName();
  139. }
  140. /** {@inheritDoc} */
  141. public final URI getFontURI() {
  142. return typeface.getFontURI();
  143. }
  144. /** {@inheritDoc} */
  145. public final FontType getFontType() {
  146. return typeface.getFontType();
  147. }
  148. /** {@inheritDoc} */
  149. public final String getFullName() {
  150. return typeface.getFullName();
  151. }
  152. /** {@inheritDoc} */
  153. public final Map getKerningInfo() {
  154. return typeface.getKerningInfo();
  155. }
  156. /** {@inheritDoc} */
  157. public final int getWidth(final int i, final int size) {
  158. return typeface.getWidth(i, size);
  159. }
  160. /** {@inheritDoc} */
  161. public final int[] getWidths() {
  162. return typeface.getWidths();
  163. }
  164. public Rectangle getBoundingBox(int glyphIndex, int size) {
  165. return typeface.getBoundingBox(glyphIndex, size);
  166. }
  167. /** {@inheritDoc} */
  168. public final int getXHeight(final int size) {
  169. return typeface.getXHeight(size);
  170. }
  171. public int getUnderlinePosition(int size) {
  172. return typeface.getUnderlinePosition(size);
  173. }
  174. public int getUnderlineThickness(int size) {
  175. return typeface.getUnderlineThickness(size);
  176. }
  177. public int getStrikeoutPosition(int size) {
  178. return typeface.getStrikeoutPosition(size);
  179. }
  180. public int getStrikeoutThickness(int size) {
  181. return typeface.getStrikeoutThickness(size);
  182. }
  183. /** {@inheritDoc} */
  184. public final boolean hasKerningInfo() {
  185. return typeface.hasKerningInfo();
  186. }
  187. /** {@inheritDoc} */
  188. public boolean isMultiByte() {
  189. return typeface.isMultiByte();
  190. }
  191. /**
  192. * {@inheritDoc}
  193. */
  194. public boolean performsPositioning() {
  195. if (typeface instanceof Positionable) {
  196. return ((Positionable) typeface).performsPositioning();
  197. } else {
  198. return false;
  199. }
  200. }
  201. /**
  202. * {@inheritDoc}
  203. */
  204. public int[][] performPositioning(CharSequence cs, String script, String language, int fontSize) {
  205. if (typeface instanceof Positionable) {
  206. return ((Positionable) typeface).performPositioning(cs, script, language, fontSize);
  207. } else {
  208. return null;
  209. }
  210. }
  211. /**
  212. * {@inheritDoc}
  213. */
  214. public int[][] performPositioning(CharSequence cs, String script, String language) {
  215. if (typeface instanceof Positionable) {
  216. return ((Positionable) typeface).performPositioning(cs, script, language);
  217. } else {
  218. return null;
  219. }
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. public boolean performsSubstitution() {
  225. if (typeface instanceof Substitutable) {
  226. return ((Substitutable) typeface).performsSubstitution();
  227. } else {
  228. return false;
  229. }
  230. }
  231. /**
  232. * {@inheritDoc}
  233. */
  234. public CharSequence performSubstitution(CharSequence cs, String script, String language, List associations,
  235. boolean retainControls) {
  236. if (typeface instanceof Substitutable) {
  237. return ((Substitutable) typeface).performSubstitution(cs, script, language, associations, retainControls);
  238. } else {
  239. return cs;
  240. }
  241. }
  242. /**
  243. * {@inheritDoc}
  244. */
  245. public CharSequence reorderCombiningMarks(CharSequence cs, int[][] gpa,
  246. String script, String language, List associations) {
  247. if (typeface instanceof Substitutable) {
  248. return ((Substitutable) typeface).reorderCombiningMarks(cs, gpa, script, language, associations);
  249. } else {
  250. return cs;
  251. }
  252. }
  253. public Typeface getRealFont() {
  254. return typeface;
  255. }
  256. }