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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. isMetricsLoaded = true;
  128. }
  129. }
  130. /**
  131. * Gets the real font.
  132. * @return the real font
  133. */
  134. public Typeface getRealFont() {
  135. load(false);
  136. return realFont;
  137. }
  138. // ---- Font ----
  139. /** {@inheritDoc} */
  140. public String getEncodingName() {
  141. load(true);
  142. return realFont.getEncodingName();
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public char mapChar(char c) {
  148. load(true);
  149. return realFont.mapChar(c);
  150. }
  151. /**
  152. * {@inheritDoc}
  153. */
  154. public boolean hadMappingOperations() {
  155. load(true);
  156. return realFont.hadMappingOperations();
  157. }
  158. /**
  159. * {@inheritDoc}
  160. */
  161. public boolean hasChar(char c) {
  162. load(true);
  163. return realFont.hasChar(c);
  164. }
  165. /**
  166. * {@inheritDoc}
  167. */
  168. public boolean isMultiByte() {
  169. load(true);
  170. return realFont.isMultiByte();
  171. }
  172. // ---- FontMetrics interface ----
  173. /** {@inheritDoc} */
  174. public String getFontName() {
  175. load(true);
  176. return realFont.getFontName();
  177. }
  178. /** {@inheritDoc} */
  179. public String getEmbedFontName() {
  180. load(true);
  181. return realFont.getEmbedFontName();
  182. }
  183. /** {@inheritDoc} */
  184. public String getFullName() {
  185. load(true);
  186. return realFont.getFullName();
  187. }
  188. /** {@inheritDoc} */
  189. public Set getFamilyNames() {
  190. load(true);
  191. return realFont.getFamilyNames();
  192. }
  193. /**
  194. * {@inheritDoc}
  195. */
  196. public int getMaxAscent(int size) {
  197. load(true);
  198. return realFont.getMaxAscent(size);
  199. }
  200. /**
  201. * {@inheritDoc}
  202. */
  203. public int getAscender(int size) {
  204. load(true);
  205. return realFont.getAscender(size);
  206. }
  207. /**
  208. * {@inheritDoc}
  209. */
  210. public int getCapHeight(int size) {
  211. load(true);
  212. return realFont.getCapHeight(size);
  213. }
  214. /**
  215. * {@inheritDoc}
  216. */
  217. public int getDescender(int size) {
  218. load(true);
  219. return realFont.getDescender(size);
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. public int getXHeight(int size) {
  225. load(true);
  226. return realFont.getXHeight(size);
  227. }
  228. /**
  229. * {@inheritDoc}
  230. */
  231. public int getWidth(int i, int size) {
  232. load(true);
  233. return realFont.getWidth(i, size);
  234. }
  235. /**
  236. * {@inheritDoc}
  237. */
  238. public int[] getWidths() {
  239. load(true);
  240. return realFont.getWidths();
  241. }
  242. /**
  243. * {@inheritDoc}
  244. */
  245. public boolean hasKerningInfo() {
  246. load(true);
  247. return realFont.hasKerningInfo();
  248. }
  249. /**
  250. * {@inheritDoc}
  251. */
  252. public Map getKerningInfo() {
  253. load(true);
  254. return realFont.getKerningInfo();
  255. }
  256. // ---- FontDescriptor interface ----
  257. /**
  258. * {@inheritDoc}
  259. */
  260. public int getCapHeight() {
  261. load(true);
  262. return realFontDescriptor.getCapHeight();
  263. }
  264. /**
  265. * {@inheritDoc}
  266. */
  267. public int getDescender() {
  268. load(true);
  269. return realFontDescriptor.getDescender();
  270. }
  271. /**
  272. * {@inheritDoc}
  273. */
  274. public int getAscender() {
  275. load(true);
  276. return realFontDescriptor.getAscender();
  277. }
  278. /**
  279. * {@inheritDoc}
  280. */
  281. public int getFlags() {
  282. load(true);
  283. return realFontDescriptor.getFlags();
  284. }
  285. /**
  286. * {@inheritDoc}
  287. */
  288. public int[] getFontBBox() {
  289. load(true);
  290. return realFontDescriptor.getFontBBox();
  291. }
  292. /**
  293. * {@inheritDoc}
  294. */
  295. public int getItalicAngle() {
  296. load(true);
  297. return realFontDescriptor.getItalicAngle();
  298. }
  299. /**
  300. * {@inheritDoc}
  301. */
  302. public int getStemV() {
  303. load(true);
  304. return realFontDescriptor.getStemV();
  305. }
  306. /**
  307. * {@inheritDoc}
  308. */
  309. public FontType getFontType() {
  310. load(true);
  311. return realFontDescriptor.getFontType();
  312. }
  313. /**
  314. * {@inheritDoc}
  315. */
  316. public boolean isEmbeddable() {
  317. load(true);
  318. return realFontDescriptor.isEmbeddable();
  319. }
  320. }