選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LazyFont.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 1999-2004 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.fonts;
  18. //Java
  19. import java.util.Map;
  20. //FOP
  21. /**
  22. * This class is used to defer the loading of a font until it is really used.
  23. */
  24. public class LazyFont extends Typeface implements FontDescriptor {
  25. private String metricsFileName = null;
  26. private String fontEmbedPath = null;
  27. private boolean useKerning = false;
  28. private boolean isMetricsLoaded = false;
  29. private Typeface realFont = null;
  30. private FontDescriptor realFontDescriptor = null;
  31. /**
  32. * Main constructor
  33. * @param fontEmbedPath path to embeddable file (may be null)
  34. * @param metricsFileName path to the metrics XML file
  35. * @param useKerning True, if kerning should be enabled
  36. */
  37. public LazyFont(String fontEmbedPath, String metricsFileName, boolean useKerning) {
  38. this.metricsFileName = metricsFileName;
  39. this.fontEmbedPath = fontEmbedPath;
  40. this.useKerning = useKerning;
  41. }
  42. private void load() {
  43. if (!isMetricsLoaded) {
  44. isMetricsLoaded = true;
  45. try {
  46. /**@todo Possible thread problem here */
  47. FontReader reader = new FontReader(metricsFileName);
  48. reader.setKerningEnabled(useKerning);
  49. reader.setFontEmbedPath(fontEmbedPath);
  50. realFont = reader.getFont();
  51. if (realFont instanceof FontDescriptor) {
  52. realFontDescriptor = (FontDescriptor) realFont;
  53. }
  54. // System.out.println("Metrics " + metricsFileName + " loaded.");
  55. } catch (Exception ex) {
  56. ex.printStackTrace();
  57. /**@todo Log this exception */
  58. //log.error("Failed to read font metrics file "
  59. // + metricsFileName
  60. // + " : " + ex.getMessage());
  61. }
  62. }
  63. }
  64. /**
  65. * Gets the real font.
  66. * @return the real font
  67. */
  68. public Typeface getRealFont() {
  69. load();
  70. return realFont;
  71. }
  72. // ---- Font ----
  73. /**
  74. * @see org.apache.fop.fonts.Typeface#getEncoding()
  75. */
  76. public String getEncoding() {
  77. load();
  78. return realFont.getEncoding();
  79. }
  80. /**
  81. * @see org.apache.fop.fonts.Font#mapChar(char)
  82. */
  83. public char mapChar(char c) {
  84. load();
  85. return realFont.mapChar(c);
  86. }
  87. /**
  88. * @see org.apache.fop.fonts.Typeface#isMultiByte()
  89. */
  90. public boolean isMultiByte() {
  91. return realFont.isMultiByte();
  92. }
  93. // ---- FontMetrics interface ----
  94. /**
  95. * @see org.apache.fop.fonts.FontMetrics#getFontName()
  96. */
  97. public String getFontName() {
  98. load();
  99. return realFont.getFontName();
  100. }
  101. /**
  102. * @see org.apache.fop.fonts.FontMetrics#getAscender(int)
  103. */
  104. public int getAscender(int size) {
  105. load();
  106. return realFont.getAscender(size);
  107. }
  108. /**
  109. * @see org.apache.fop.fonts.FontMetrics#getCapHeight(int)
  110. */
  111. public int getCapHeight(int size) {
  112. load();
  113. return realFont.getCapHeight(size);
  114. }
  115. /**
  116. * @see org.apache.fop.fonts.FontMetrics#getDescender(int)
  117. */
  118. public int getDescender(int size) {
  119. load();
  120. return realFont.getDescender(size);
  121. }
  122. /**
  123. * @see org.apache.fop.fonts.FontMetrics#getXHeight(int)
  124. */
  125. public int getXHeight(int size) {
  126. load();
  127. return realFont.getXHeight(size);
  128. }
  129. /**
  130. * @see org.apache.fop.fonts.FontMetrics#getWidth(int, int)
  131. */
  132. public int getWidth(int i, int size) {
  133. load();
  134. return realFont.getWidth(i, size);
  135. }
  136. /**
  137. * @see org.apache.fop.fonts.FontMetrics#getWidths()
  138. */
  139. public int[] getWidths() {
  140. load();
  141. return realFont.getWidths();
  142. }
  143. /**
  144. * @see org.apache.fop.fonts.FontMetrics#hasKerningInfo()
  145. */
  146. public boolean hasKerningInfo() {
  147. load();
  148. return realFont.hasKerningInfo();
  149. }
  150. /**
  151. * @see org.apache.fop.fonts.FontMetrics#getKerningInfo()
  152. */
  153. public Map getKerningInfo() {
  154. load();
  155. return realFont.getKerningInfo();
  156. }
  157. // ---- FontDescriptor interface ----
  158. /**
  159. * @see org.apache.fop.fonts.FontDescriptor#getCapHeight()
  160. */
  161. public int getCapHeight() {
  162. load();
  163. return realFontDescriptor.getCapHeight();
  164. }
  165. /**
  166. * @see org.apache.fop.fonts.FontDescriptor#getDescender()
  167. */
  168. public int getDescender() {
  169. load();
  170. return realFontDescriptor.getDescender();
  171. }
  172. /**
  173. * @see org.apache.fop.fonts.FontDescriptor#getAscender()
  174. */
  175. public int getAscender() {
  176. load();
  177. return realFontDescriptor.getAscender();
  178. }
  179. /**
  180. * @see org.apache.fop.fonts.FontDescriptor#getFlags()
  181. */
  182. public int getFlags() {
  183. load();
  184. return realFontDescriptor.getFlags();
  185. }
  186. /**
  187. * @see org.apache.fop.fonts.FontDescriptor#getFontBBox()
  188. */
  189. public int[] getFontBBox() {
  190. load();
  191. return realFontDescriptor.getFontBBox();
  192. }
  193. /**
  194. * @see org.apache.fop.fonts.FontDescriptor#getItalicAngle()
  195. */
  196. public int getItalicAngle() {
  197. load();
  198. return realFontDescriptor.getItalicAngle();
  199. }
  200. /**
  201. * @see org.apache.fop.fonts.FontDescriptor#getStemV()
  202. */
  203. public int getStemV() {
  204. load();
  205. return realFontDescriptor.getStemV();
  206. }
  207. /**
  208. * @see org.apache.fop.fonts.FontDescriptor#getFontType()
  209. */
  210. public FontType getFontType() {
  211. load();
  212. return realFontDescriptor.getFontType();
  213. }
  214. /**
  215. * @see org.apache.fop.fonts.FontDescriptor#isEmbeddable()
  216. */
  217. public boolean isEmbeddable() {
  218. load();
  219. return realFontDescriptor.isEmbeddable();
  220. }
  221. }