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

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