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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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.URI;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import org.xml.sax.InputSource;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.fop.apps.FOPException;
  28. import org.apache.fop.apps.io.InternalResourceResolver;
  29. import org.apache.fop.complexscripts.fonts.Positionable;
  30. import org.apache.fop.complexscripts.fonts.Substitutable;
  31. /**
  32. * This class is used to defer the loading of a font until it is really used.
  33. */
  34. public class LazyFont extends Typeface implements FontDescriptor, Substitutable, Positionable {
  35. private static Log log = LogFactory.getLog(LazyFont.class);
  36. private final URI metricsURI;
  37. private final URI fontEmbedURI;
  38. private final boolean useKerning;
  39. private final boolean useAdvanced;
  40. private final EncodingMode encodingMode;
  41. private final boolean embedded;
  42. private final String subFontName;
  43. private final InternalResourceResolver resourceResolver;
  44. private boolean isMetricsLoaded;
  45. private Typeface realFont;
  46. private FontDescriptor realFontDescriptor;
  47. /**
  48. * Main constructor
  49. * @param fontInfo the font info to embed
  50. * @param resourceResolver the font resolver to handle font URIs
  51. */
  52. public LazyFont(EmbedFontInfo fontInfo, InternalResourceResolver resourceResolver,
  53. boolean useComplexScripts) {
  54. this.metricsURI = fontInfo.getMetricsURI();
  55. this.fontEmbedURI = fontInfo.getEmbedURI();
  56. this.useKerning = fontInfo.getKerning();
  57. if (resourceResolver != null) {
  58. this.useAdvanced = useComplexScripts;
  59. } else {
  60. this.useAdvanced = fontInfo.getAdvanced();
  61. }
  62. this.encodingMode = fontInfo.getEncodingMode() != null ? fontInfo.getEncodingMode()
  63. : EncodingMode.AUTO;
  64. this.subFontName = fontInfo.getSubFontName();
  65. this.embedded = fontInfo.isEmbedded();
  66. this.resourceResolver = resourceResolver;
  67. }
  68. /** {@inheritDoc} */
  69. public String toString() {
  70. StringBuffer sbuf = new StringBuffer(super.toString());
  71. sbuf.append('{');
  72. sbuf.append("metrics-url=" + metricsURI);
  73. sbuf.append(",embed-url=" + fontEmbedURI);
  74. sbuf.append(",kerning=" + useKerning);
  75. sbuf.append(",advanced=" + useAdvanced);
  76. sbuf.append('}');
  77. return sbuf.toString();
  78. }
  79. private void load(boolean fail) {
  80. if (!isMetricsLoaded) {
  81. try {
  82. if (metricsURI != null) {
  83. /**@todo Possible thread problem here */
  84. FontReader reader = null;
  85. InputStream in = resourceResolver.getResource(metricsURI);
  86. InputSource src = new InputSource(in);
  87. src.setSystemId(metricsURI.toASCIIString());
  88. reader = new FontReader(src, resourceResolver);
  89. reader.setKerningEnabled(useKerning);
  90. reader.setAdvancedEnabled(useAdvanced);
  91. if (this.embedded) {
  92. reader.setFontEmbedURI(fontEmbedURI);
  93. }
  94. realFont = reader.getFont();
  95. } else {
  96. if (fontEmbedURI == null) {
  97. throw new RuntimeException("Cannot load font. No font URIs available.");
  98. }
  99. realFont = FontLoader.loadFont(fontEmbedURI, this.subFontName,
  100. this.embedded, this.encodingMode, useKerning, useAdvanced, resourceResolver);
  101. }
  102. if (realFont instanceof FontDescriptor) {
  103. realFontDescriptor = (FontDescriptor) realFont;
  104. }
  105. } catch (FOPException fopex) {
  106. log.error("Failed to read font metrics file " + metricsURI, fopex);
  107. if (fail) {
  108. throw new RuntimeException(fopex);
  109. }
  110. } catch (IOException ioex) {
  111. log.error("Failed to read font metrics file " + metricsURI, ioex);
  112. if (fail) {
  113. throw new RuntimeException(ioex);
  114. }
  115. }
  116. realFont.setEventListener(this.eventListener);
  117. isMetricsLoaded = true;
  118. }
  119. }
  120. /**
  121. * Gets the real font.
  122. * @return the real font
  123. */
  124. public Typeface getRealFont() {
  125. load(false);
  126. return realFont;
  127. }
  128. // ---- Font ----
  129. /** {@inheritDoc} */
  130. public String getEncodingName() {
  131. load(true);
  132. return realFont.getEncodingName();
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. public char mapChar(char c) {
  138. load(true);
  139. return realFont.mapChar(c);
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public boolean hadMappingOperations() {
  145. load(true);
  146. return realFont.hadMappingOperations();
  147. }
  148. /**
  149. * {@inheritDoc}
  150. */
  151. public boolean hasChar(char c) {
  152. load(true);
  153. return realFont.hasChar(c);
  154. }
  155. /**
  156. * {@inheritDoc}
  157. */
  158. public boolean isMultiByte() {
  159. load(true);
  160. return realFont.isMultiByte();
  161. }
  162. // ---- FontMetrics interface ----
  163. /** {@inheritDoc} */
  164. public String getFontName() {
  165. load(true);
  166. return realFont.getFontName();
  167. }
  168. /** {@inheritDoc} */
  169. public String getEmbedFontName() {
  170. load(true);
  171. return realFont.getEmbedFontName();
  172. }
  173. /** {@inheritDoc} */
  174. public String getFullName() {
  175. load(true);
  176. return realFont.getFullName();
  177. }
  178. /** {@inheritDoc} */
  179. public Set<String> getFamilyNames() {
  180. load(true);
  181. return realFont.getFamilyNames();
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public int getMaxAscent(int size) {
  187. load(true);
  188. return realFont.getMaxAscent(size);
  189. }
  190. /**
  191. * {@inheritDoc}
  192. */
  193. public int getAscender(int size) {
  194. load(true);
  195. return realFont.getAscender(size);
  196. }
  197. /**
  198. * {@inheritDoc}
  199. */
  200. public int getCapHeight(int size) {
  201. load(true);
  202. return realFont.getCapHeight(size);
  203. }
  204. /**
  205. * {@inheritDoc}
  206. */
  207. public int getDescender(int size) {
  208. load(true);
  209. return realFont.getDescender(size);
  210. }
  211. /**
  212. * {@inheritDoc}
  213. */
  214. public int getXHeight(int size) {
  215. load(true);
  216. return realFont.getXHeight(size);
  217. }
  218. /**
  219. * {@inheritDoc}
  220. */
  221. public int getWidth(int i, int size) {
  222. load(true);
  223. return realFont.getWidth(i, size);
  224. }
  225. /**
  226. * {@inheritDoc}
  227. */
  228. public int[] getWidths() {
  229. load(true);
  230. return realFont.getWidths();
  231. }
  232. /**
  233. * {@inheritDoc}
  234. */
  235. public boolean hasKerningInfo() {
  236. load(true);
  237. return realFont.hasKerningInfo();
  238. }
  239. /**
  240. * {@inheritDoc}
  241. */
  242. public Map<Integer, Map<Integer, Integer>> getKerningInfo() {
  243. load(true);
  244. return realFont.getKerningInfo();
  245. }
  246. // ---- FontDescriptor interface ----
  247. /**
  248. * {@inheritDoc}
  249. */
  250. public int getCapHeight() {
  251. load(true);
  252. return realFontDescriptor.getCapHeight();
  253. }
  254. /**
  255. * {@inheritDoc}
  256. */
  257. public int getDescender() {
  258. load(true);
  259. return realFontDescriptor.getDescender();
  260. }
  261. /**
  262. * {@inheritDoc}
  263. */
  264. public int getAscender() {
  265. load(true);
  266. return realFontDescriptor.getAscender();
  267. }
  268. /** {@inheritDoc} */
  269. public int getFlags() {
  270. load(true);
  271. return realFontDescriptor.getFlags();
  272. }
  273. /** {@inheritDoc} */
  274. public boolean isSymbolicFont() {
  275. load(true);
  276. return realFontDescriptor.isSymbolicFont();
  277. }
  278. /**
  279. * {@inheritDoc}
  280. */
  281. public int[] getFontBBox() {
  282. load(true);
  283. return realFontDescriptor.getFontBBox();
  284. }
  285. /**
  286. * {@inheritDoc}
  287. */
  288. public int getItalicAngle() {
  289. load(true);
  290. return realFontDescriptor.getItalicAngle();
  291. }
  292. /**
  293. * {@inheritDoc}
  294. */
  295. public int getStemV() {
  296. load(true);
  297. return realFontDescriptor.getStemV();
  298. }
  299. /**
  300. * {@inheritDoc}
  301. */
  302. public FontType getFontType() {
  303. load(true);
  304. return realFontDescriptor.getFontType();
  305. }
  306. /**
  307. * {@inheritDoc}
  308. */
  309. public boolean isEmbeddable() {
  310. load(true);
  311. return realFontDescriptor.isEmbeddable();
  312. }
  313. /**
  314. * {@inheritDoc}
  315. */
  316. public boolean performsSubstitution() {
  317. load(true);
  318. if ( realFontDescriptor instanceof Substitutable ) {
  319. return ((Substitutable)realFontDescriptor).performsSubstitution();
  320. } else {
  321. return false;
  322. }
  323. }
  324. /**
  325. * {@inheritDoc}
  326. */
  327. public CharSequence performSubstitution ( CharSequence cs, String script, String language ) {
  328. load(true);
  329. if ( realFontDescriptor instanceof Substitutable ) {
  330. return ((Substitutable)realFontDescriptor).performSubstitution(cs, script, language);
  331. } else {
  332. return cs;
  333. }
  334. }
  335. /**
  336. * {@inheritDoc}
  337. */
  338. public CharSequence reorderCombiningMarks
  339. ( CharSequence cs, int[][] gpa, String script, String language ) {
  340. load(true);
  341. if ( realFontDescriptor instanceof Substitutable ) {
  342. return ((Substitutable)realFontDescriptor)
  343. .reorderCombiningMarks(cs, gpa, script, language);
  344. } else {
  345. return cs;
  346. }
  347. }
  348. /**
  349. * {@inheritDoc}
  350. */
  351. public boolean performsPositioning() {
  352. load(true);
  353. if ( realFontDescriptor instanceof Positionable ) {
  354. return ((Positionable)realFontDescriptor).performsPositioning();
  355. } else {
  356. return false;
  357. }
  358. }
  359. /**
  360. * {@inheritDoc}
  361. */
  362. public int[][]
  363. performPositioning ( CharSequence cs, String script, String language, int fontSize ) {
  364. load(true);
  365. if ( realFontDescriptor instanceof Positionable ) {
  366. return ((Positionable)realFontDescriptor)
  367. .performPositioning(cs, script, language, fontSize);
  368. } else {
  369. return null;
  370. }
  371. }
  372. /**
  373. * {@inheritDoc}
  374. */
  375. public int[][]
  376. performPositioning ( CharSequence cs, String script, String language ) {
  377. load(true);
  378. if ( realFontDescriptor instanceof Positionable ) {
  379. return ((Positionable)realFontDescriptor)
  380. .performPositioning(cs, script, language);
  381. } else {
  382. return null;
  383. }
  384. }
  385. /**
  386. * {@inheritDoc}
  387. */
  388. public boolean isSubsetEmbedded() {
  389. load(true);
  390. return realFont.isMultiByte();
  391. }
  392. }