Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CustomFontMetricsMapper.java 8.1KB

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