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ů.

LazyFont.java 6.5KB

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