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

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