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.

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