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.9KB

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