選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LazyFont.java 10KB

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