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.

LazyFont.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.fonts;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.URL;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.xml.sax.InputSource;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.fop.apps.FOPException;
  30. /**
  31. * This class is used to defer the loading of a font until it is really used.
  32. */
  33. public class LazyFont extends Typeface implements FontDescriptor {
  34. private static Log log = LogFactory.getLog(LazyFont.class);
  35. private String metricsFileName = null;
  36. private String fontEmbedPath = null;
  37. private boolean useKerning = false;
  38. private boolean isMetricsLoaded = false;
  39. private Typeface realFont = null;
  40. private FontDescriptor realFontDescriptor = null;
  41. private FontResolver resolver = null;
  42. /**
  43. * Main constructor
  44. * @param fontInfo the font info to embed
  45. * @param resolver the font resolver to handle font URIs
  46. */
  47. public LazyFont(EmbedFontInfo fontInfo, FontResolver resolver) {
  48. this.metricsFileName = fontInfo.getMetricsFile();
  49. this.fontEmbedPath = fontInfo.getEmbedFile();
  50. this.useKerning = fontInfo.getKerning();
  51. this.resolver = resolver;
  52. }
  53. /** {@inheritDoc} */
  54. public String toString() {
  55. return ( "metrics-url=" + metricsFileName + ", embed-url=" + fontEmbedPath
  56. + ", kerning=" + useKerning );
  57. }
  58. private void load(boolean fail) {
  59. if (!isMetricsLoaded) {
  60. try {
  61. if (metricsFileName != null) {
  62. /**@todo Possible thread problem here */
  63. FontReader reader = null;
  64. if (resolver != null) {
  65. Source source = resolver.resolve(metricsFileName);
  66. if (source == null) {
  67. String err
  68. = "Cannot load font: failed to create Source from metrics file "
  69. + metricsFileName;
  70. if (fail) {
  71. throw new RuntimeException(err);
  72. } else {
  73. log.error(err);
  74. }
  75. return;
  76. }
  77. InputStream in = null;
  78. if (source instanceof StreamSource) {
  79. in = ((StreamSource) source).getInputStream();
  80. }
  81. if (in == null && source.getSystemId() != null) {
  82. in = new java.net.URL(source.getSystemId()).openStream();
  83. }
  84. if (in == null) {
  85. String err = "Cannot load font: After URI resolution, the returned"
  86. + " Source object does not contain an InputStream"
  87. + " or a valid URL (system identifier) for metrics file: "
  88. + metricsFileName;
  89. if (fail) {
  90. throw new RuntimeException(err);
  91. } else {
  92. log.error(err);
  93. }
  94. return;
  95. }
  96. InputSource src = new InputSource(in);
  97. src.setSystemId(source.getSystemId());
  98. reader = new FontReader(src);
  99. } else {
  100. reader = new FontReader(new InputSource(
  101. new URL(metricsFileName).openStream()));
  102. }
  103. reader.setKerningEnabled(useKerning);
  104. reader.setFontEmbedPath(fontEmbedPath);
  105. reader.setResolver(resolver);
  106. realFont = reader.getFont();
  107. } else {
  108. if (fontEmbedPath == null) {
  109. throw new RuntimeException("Cannot load font. No font URIs available.");
  110. }
  111. realFont = FontLoader.loadFont(fontEmbedPath, resolver);
  112. }
  113. if (realFont instanceof FontDescriptor) {
  114. realFontDescriptor = (FontDescriptor) realFont;
  115. }
  116. } catch (FOPException fopex) {
  117. log.error("Failed to read font metrics file " + metricsFileName, fopex);
  118. if (fail) {
  119. throw new RuntimeException(fopex.getMessage());
  120. }
  121. } catch (IOException ioex) {
  122. log.error("Failed to read font metrics file " + metricsFileName, ioex);
  123. if (fail) {
  124. throw new RuntimeException(ioex.getMessage());
  125. }
  126. }
  127. realFont.setEventListener(this.eventListener);
  128. isMetricsLoaded = true;
  129. }
  130. }
  131. /**
  132. * Gets the real font.
  133. * @return the real font
  134. */
  135. public Typeface getRealFont() {
  136. load(false);
  137. return realFont;
  138. }
  139. // ---- Font ----
  140. /** {@inheritDoc} */
  141. public String getEncodingName() {
  142. load(true);
  143. return realFont.getEncodingName();
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. public char mapChar(char c) {
  149. load(true);
  150. return realFont.mapChar(c);
  151. }
  152. /**
  153. * {@inheritDoc}
  154. */
  155. public boolean hadMappingOperations() {
  156. load(true);
  157. return realFont.hadMappingOperations();
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. public boolean hasChar(char c) {
  163. load(true);
  164. return realFont.hasChar(c);
  165. }
  166. /**
  167. * {@inheritDoc}
  168. */
  169. public boolean isMultiByte() {
  170. load(true);
  171. return realFont.isMultiByte();
  172. }
  173. // ---- FontMetrics interface ----
  174. /** {@inheritDoc} */
  175. public String getFontName() {
  176. load(true);
  177. return realFont.getFontName();
  178. }
  179. /** {@inheritDoc} */
  180. public String getEmbedFontName() {
  181. load(true);
  182. return realFont.getEmbedFontName();
  183. }
  184. /** {@inheritDoc} */
  185. public String getFullName() {
  186. load(true);
  187. return realFont.getFullName();
  188. }
  189. /** {@inheritDoc} */
  190. public Set getFamilyNames() {
  191. load(true);
  192. return realFont.getFamilyNames();
  193. }
  194. /**
  195. * {@inheritDoc}
  196. */
  197. public int getMaxAscent(int size) {
  198. load(true);
  199. return realFont.getMaxAscent(size);
  200. }
  201. /**
  202. * {@inheritDoc}
  203. */
  204. public int getAscender(int size) {
  205. load(true);
  206. return realFont.getAscender(size);
  207. }
  208. /**
  209. * {@inheritDoc}
  210. */
  211. public int getCapHeight(int size) {
  212. load(true);
  213. return realFont.getCapHeight(size);
  214. }
  215. /**
  216. * {@inheritDoc}
  217. */
  218. public int getDescender(int size) {
  219. load(true);
  220. return realFont.getDescender(size);
  221. }
  222. /**
  223. * {@inheritDoc}
  224. */
  225. public int getXHeight(int size) {
  226. load(true);
  227. return realFont.getXHeight(size);
  228. }
  229. /**
  230. * {@inheritDoc}
  231. */
  232. public int getWidth(int i, int size) {
  233. load(true);
  234. return realFont.getWidth(i, size);
  235. }
  236. /**
  237. * {@inheritDoc}
  238. */
  239. public int[] getWidths() {
  240. load(true);
  241. return realFont.getWidths();
  242. }
  243. /**
  244. * {@inheritDoc}
  245. */
  246. public boolean hasKerningInfo() {
  247. load(true);
  248. return realFont.hasKerningInfo();
  249. }
  250. /**
  251. * {@inheritDoc}
  252. */
  253. public Map getKerningInfo() {
  254. load(true);
  255. return realFont.getKerningInfo();
  256. }
  257. // ---- FontDescriptor interface ----
  258. /**
  259. * {@inheritDoc}
  260. */
  261. public int getCapHeight() {
  262. load(true);
  263. return realFontDescriptor.getCapHeight();
  264. }
  265. /**
  266. * {@inheritDoc}
  267. */
  268. public int getDescender() {
  269. load(true);
  270. return realFontDescriptor.getDescender();
  271. }
  272. /**
  273. * {@inheritDoc}
  274. */
  275. public int getAscender() {
  276. load(true);
  277. return realFontDescriptor.getAscender();
  278. }
  279. /**
  280. * {@inheritDoc}
  281. */
  282. public int getFlags() {
  283. load(true);
  284. return realFontDescriptor.getFlags();
  285. }
  286. /**
  287. * {@inheritDoc}
  288. */
  289. public int[] getFontBBox() {
  290. load(true);
  291. return realFontDescriptor.getFontBBox();
  292. }
  293. /**
  294. * {@inheritDoc}
  295. */
  296. public int getItalicAngle() {
  297. load(true);
  298. return realFontDescriptor.getItalicAngle();
  299. }
  300. /**
  301. * {@inheritDoc}
  302. */
  303. public int getStemV() {
  304. load(true);
  305. return realFontDescriptor.getStemV();
  306. }
  307. /**
  308. * {@inheritDoc}
  309. */
  310. public FontType getFontType() {
  311. load(true);
  312. return realFontDescriptor.getFontType();
  313. }
  314. /**
  315. * {@inheritDoc}
  316. */
  317. public boolean isEmbeddable() {
  318. load(true);
  319. return realFontDescriptor.isEmbeddable();
  320. }
  321. }