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

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