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.

AFPFontConfig.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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.render.afp;
  19. import java.io.IOException;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.net.URI;
  22. import java.net.URISyntaxException;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.fop.afp.AFPEventProducer;
  29. import org.apache.fop.afp.fonts.AFPFont;
  30. import org.apache.fop.afp.fonts.AFPFontInfo;
  31. import org.apache.fop.afp.fonts.CharacterSet;
  32. import org.apache.fop.afp.fonts.CharacterSetBuilder;
  33. import org.apache.fop.afp.fonts.CharacterSetType;
  34. import org.apache.fop.afp.fonts.DoubleByteFont;
  35. import org.apache.fop.afp.fonts.OutlineFont;
  36. import org.apache.fop.afp.fonts.RasterFont;
  37. import org.apache.fop.afp.util.AFPResourceAccessor;
  38. import org.apache.fop.apps.FOPException;
  39. import org.apache.fop.apps.io.InternalResourceResolver;
  40. import org.apache.fop.configuration.Configuration;
  41. import org.apache.fop.configuration.ConfigurationException;
  42. import org.apache.fop.events.EventProducer;
  43. import org.apache.fop.fonts.EmbedFontInfo;
  44. import org.apache.fop.fonts.FontConfig;
  45. import org.apache.fop.fonts.FontManager;
  46. import org.apache.fop.fonts.FontManagerConfigurator;
  47. import org.apache.fop.fonts.FontTriplet;
  48. import org.apache.fop.fonts.FontTriplet.Matcher;
  49. import org.apache.fop.fonts.FontType;
  50. import org.apache.fop.fonts.FontUris;
  51. import org.apache.fop.fonts.FontUtil;
  52. import org.apache.fop.fonts.LazyFont;
  53. import org.apache.fop.fonts.Typeface;
  54. /**
  55. * The config object for AFP fonts, these differ from the the more generic fonts (TTF and Type1).
  56. */
  57. public final class AFPFontConfig implements FontConfig {
  58. private static final Log LOG = LogFactory.getLog(AFPFontConfig.class);
  59. private final List<AFPFontConfigData> fontsConfig;
  60. private AFPFontConfig() {
  61. fontsConfig = new ArrayList<AFPFontConfigData>();
  62. }
  63. /**
  64. * Returns a list of AFP font configuration data.
  65. * @return the AFP font config data
  66. */
  67. public List<AFPFontConfigData> getFontConfig() {
  68. return fontsConfig;
  69. }
  70. /**
  71. * The parser for AFP font data.
  72. */
  73. static final class AFPFontInfoConfigParser implements FontConfigParser {
  74. /** {@inheritDoc}} */
  75. public AFPFontConfig parse(Configuration cfg, FontManager fontManager, boolean strict,
  76. EventProducer eventProducer) throws FOPException {
  77. try {
  78. return new ParserHelper(cfg, fontManager, strict,
  79. (AFPEventProducer) eventProducer).fontConfig;
  80. } catch (ConfigurationException ce) {
  81. throw new FOPException(ce);
  82. }
  83. }
  84. AFPFontConfig getEmptyConfig() {
  85. return new AFPFontConfig();
  86. }
  87. }
  88. private static final class AggregateMatcher implements Matcher {
  89. private final List<Matcher> matchers;
  90. private AggregateMatcher(Matcher... matchers) {
  91. this.matchers = new ArrayList<Matcher>();
  92. for (Matcher matcher : matchers) {
  93. if (matcher != null) {
  94. this.matchers.add(matcher);
  95. }
  96. }
  97. }
  98. public boolean matches(FontTriplet triplet) {
  99. for (Matcher matcher : matchers) {
  100. if (matcher.matches(triplet)) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. }
  107. private static final class ParserHelper {
  108. private static final Log LOG = LogFactory.getLog(ParserHelper.class);
  109. private final AFPFontConfig fontConfig;
  110. private final Matcher matcher;
  111. private ParserHelper(Configuration cfg, FontManager fontManager, boolean strict,
  112. AFPEventProducer eventProducer) throws FOPException, ConfigurationException {
  113. Configuration fonts = cfg.getChild("fonts");
  114. Matcher localMatcher = null;
  115. Configuration referencedFontsCfg = fonts.getChild("referenced-fonts", false);
  116. if (referencedFontsCfg != null) {
  117. localMatcher = FontManagerConfigurator.createFontsMatcher(referencedFontsCfg, strict);
  118. }
  119. matcher = new AggregateMatcher(fontManager.getReferencedFontsMatcher(), localMatcher);
  120. fontConfig = new AFPFontConfig();
  121. for (Configuration font : fonts.getChildren("font")) {
  122. buildFont(font, eventProducer);
  123. }
  124. }
  125. private void buildFont(Configuration fontCfg, AFPEventProducer eventProducer)
  126. throws ConfigurationException {
  127. //FontManager fontManager = this.userAgent.getFontManager();
  128. Configuration[] triplets = fontCfg.getChildren("font-triplet");
  129. List<FontTriplet> tripletList = new ArrayList<FontTriplet>();
  130. if (triplets.length == 0) {
  131. eventProducer.fontConfigMissing(this, "<font-triplet...", fontCfg.getLocation());
  132. return;
  133. }
  134. for (Configuration triplet : triplets) {
  135. int weight = FontUtil.parseCSS2FontWeight(triplet.getAttribute("weight"));
  136. FontTriplet fontTriplet = new FontTriplet(triplet.getAttribute("name"),
  137. triplet.getAttribute("style"), weight);
  138. tripletList.add(fontTriplet);
  139. }
  140. String tturi = fontCfg.getAttribute("embed-url", null);
  141. if (tturi != null) {
  142. fontFromType(tripletList, "truetype", null, "UTF-16BE", fontCfg, eventProducer, tturi);
  143. return;
  144. }
  145. //build the fonts
  146. Configuration[] config = fontCfg.getChildren("afp-font");
  147. if (config.length == 0) {
  148. eventProducer.fontConfigMissing(this, "<afp-font...", fontCfg.getLocation());
  149. return;
  150. }
  151. Configuration afpFontCfg = config[0];
  152. String uri = afpFontCfg.getAttribute("base-uri", null);
  153. try {
  154. String type = afpFontCfg.getAttribute("type");
  155. if (type == null) {
  156. eventProducer.fontConfigMissing(this, "type attribute", fontCfg.getLocation());
  157. return;
  158. }
  159. String codepage = afpFontCfg.getAttribute("codepage");
  160. if (codepage == null) {
  161. eventProducer.fontConfigMissing(this, "codepage attribute",
  162. fontCfg.getLocation());
  163. return;
  164. }
  165. String encoding = afpFontCfg.getAttribute("encoding");
  166. if (encoding == null) {
  167. eventProducer.fontConfigMissing(this, "encoding attribute",
  168. fontCfg.getLocation());
  169. return;
  170. }
  171. fontFromType(tripletList, type, codepage, encoding, afpFontCfg, eventProducer, uri);
  172. } catch (ConfigurationException ce) {
  173. eventProducer.invalidConfiguration(this, ce);
  174. }
  175. }
  176. private void fontFromType(List<FontTriplet> fontTriplets, String type, String codepage,
  177. String encoding, Configuration cfg, AFPEventProducer eventProducer, String embedURI)
  178. throws ConfigurationException {
  179. AFPFontConfigData config = null;
  180. if ("raster".equalsIgnoreCase(type)) {
  181. config = getRasterFont(fontTriplets, type, codepage, encoding, cfg, eventProducer,
  182. embedURI);
  183. } else if ("outline".equalsIgnoreCase(type)) {
  184. config = getOutlineFont(fontTriplets, type, codepage, encoding, cfg, eventProducer,
  185. embedURI);
  186. } else if ("CIDKeyed".equalsIgnoreCase(type)) {
  187. config = getCIDKeyedFont(fontTriplets, type, codepage, encoding, cfg,
  188. eventProducer, embedURI);
  189. } else if ("truetype".equalsIgnoreCase(type)) {
  190. config = getTruetypeFont(fontTriplets, type, codepage, encoding, cfg, eventProducer, embedURI);
  191. } else {
  192. LOG.error("No or incorrect type attribute: " + type);
  193. }
  194. if (config != null) {
  195. fontConfig.fontsConfig.add(config);
  196. }
  197. }
  198. private CIDKeyedFontConfig getCIDKeyedFont(List<FontTriplet> fontTriplets, String type,
  199. String codepage, String encoding, Configuration cfg, AFPEventProducer eventProducer,
  200. String uri) throws ConfigurationException {
  201. String characterset = cfg.getAttribute("characterset");
  202. if (characterset == null) {
  203. eventProducer.fontConfigMissing(this, "characterset attribute",
  204. cfg.getLocation());
  205. return null;
  206. }
  207. String name = cfg.getAttribute("name", characterset);
  208. CharacterSetType charsetType = cfg.getAttributeAsBoolean("ebcdic-dbcs", false)
  209. ? CharacterSetType.DOUBLE_BYTE_LINE_DATA : CharacterSetType.DOUBLE_BYTE;
  210. return new CIDKeyedFontConfig(fontTriplets, type, codepage, encoding, characterset,
  211. name, charsetType, isEmbbedable(fontTriplets), uri);
  212. }
  213. private OutlineFontConfig getOutlineFont(List<FontTriplet> fontTriplets, String type,
  214. String codepage, String encoding, Configuration cfg,
  215. AFPEventProducer eventProducer, String uri) throws ConfigurationException {
  216. String characterset = cfg.getAttribute("characterset");
  217. if (characterset == null) {
  218. eventProducer.fontConfigMissing(this, "characterset attribute",
  219. cfg.getLocation());
  220. return null;
  221. }
  222. String name = cfg.getAttribute("name", characterset);
  223. String base14 = cfg.getAttribute("base14-font", null);
  224. return new OutlineFontConfig(fontTriplets, type, codepage, encoding, characterset,
  225. name, base14, isEmbbedable(fontTriplets), uri);
  226. }
  227. private TrueTypeFontConfig getTruetypeFont(List<FontTriplet> fontTriplets, String type, String codepage,
  228. String encoding, Configuration cfg, AFPEventProducer eventProducer,
  229. String uri) throws ConfigurationException {
  230. String name = cfg.getAttribute("name", null);
  231. if (name == null) {
  232. eventProducer.fontConfigMissing(this, "font name attribute", cfg.getLocation());
  233. return null;
  234. }
  235. String subfont = cfg.getAttribute("sub-font", null);
  236. return new TrueTypeFontConfig(fontTriplets, type, codepage, encoding, "",
  237. name, subfont, isEmbbedable(fontTriplets), uri);
  238. }
  239. private RasterFontConfig getRasterFont(List<FontTriplet> triplets, String type,
  240. String codepage, String encoding, Configuration cfg,
  241. AFPEventProducer eventProducer, String uri)
  242. throws ConfigurationException {
  243. String name = cfg.getAttribute("name", "Unknown");
  244. // Create a new font object
  245. Configuration[] rasters = cfg.getChildren("afp-raster-font");
  246. if (rasters.length == 0) {
  247. eventProducer.fontConfigMissing(this, "<afp-raster-font...",
  248. cfg.getLocation());
  249. return null;
  250. }
  251. List<RasterCharactersetData> charsetData = new ArrayList<RasterCharactersetData>();
  252. for (Configuration rasterCfg : rasters) {
  253. String characterset = rasterCfg.getAttribute("characterset");
  254. if (characterset == null) {
  255. eventProducer.fontConfigMissing(this, "characterset attribute",
  256. cfg.getLocation());
  257. return null;
  258. }
  259. float size = rasterCfg.getAttributeAsFloat("size");
  260. int sizeMpt = (int) (size * 1000);
  261. String base14 = rasterCfg.getAttribute("base14-font", null);
  262. charsetData.add(new RasterCharactersetData(characterset, sizeMpt, base14));
  263. }
  264. return new RasterFontConfig(triplets, type, codepage, encoding, null, name, uri, charsetData,
  265. isEmbbedable(triplets));
  266. }
  267. private boolean isEmbbedable(List<FontTriplet> triplets) {
  268. for (FontTriplet triplet : triplets) {
  269. if (matcher.matches(triplet)) {
  270. return false;
  271. }
  272. }
  273. return true;
  274. }
  275. }
  276. abstract static class AFPFontConfigData {
  277. protected final List<FontTriplet> triplets;
  278. private final String codePage;
  279. private final String encoding;
  280. private final String name;
  281. private final boolean embeddable;
  282. protected final String uri;
  283. AFPFontConfigData(List<FontTriplet> triplets, String type, String codePage,
  284. String encoding, String name, boolean embeddable, String uri) {
  285. this.triplets = Collections.unmodifiableList(triplets);
  286. this.codePage = codePage;
  287. this.encoding = encoding;
  288. this.name = name;
  289. this.embeddable = embeddable;
  290. this.uri = uri;
  291. }
  292. static AFPFontInfo getFontInfo(AFPFont font, AFPFontConfigData config) {
  293. return font != null ? new AFPFontInfo(font, config.triplets) : null;
  294. }
  295. abstract AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver,
  296. AFPEventProducer eventProducer) throws IOException;
  297. AFPResourceAccessor getAccessor(InternalResourceResolver resourceResolver) {
  298. return new AFPResourceAccessor(resourceResolver, uri);
  299. }
  300. }
  301. static final class CIDKeyedFontConfig extends AFPFontConfigData {
  302. private final CharacterSetType charsetType;
  303. private final String characterset;
  304. private CIDKeyedFontConfig(List<FontTriplet> triplets, String type, String codePage, String encoding,
  305. String characterset, String name, CharacterSetType charsetType, boolean embeddable, String uri) {
  306. super(triplets, type, codePage, encoding, name, embeddable, uri);
  307. this.characterset = characterset;
  308. this.charsetType = charsetType;
  309. }
  310. @Override
  311. AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver, AFPEventProducer eventProducer)
  312. throws IOException {
  313. AFPResourceAccessor accessor = getAccessor(resourceResolver);
  314. CharacterSet characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS(
  315. characterset, super.codePage, super.encoding, charsetType, accessor, eventProducer);
  316. return getFontInfo(new DoubleByteFont(super.codePage, super.embeddable, characterSet,
  317. eventProducer), this);
  318. }
  319. }
  320. static final class TrueTypeFontConfig extends AFPFontConfigData {
  321. private String characterset;
  322. private String subfont;
  323. private String fontUri;
  324. private TrueTypeFontConfig(List<FontTriplet> triplets, String type, String codePage,
  325. String encoding, String characterset, String name, String subfont,
  326. boolean embeddable, String uri) {
  327. super(triplets, type, codePage, encoding, name, embeddable, null);
  328. this.characterset = characterset;
  329. this.subfont = subfont;
  330. this.fontUri = uri;
  331. }
  332. @Override
  333. AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver, AFPEventProducer eventProducer)
  334. throws IOException {
  335. try {
  336. Typeface tf = new LazyFont(new EmbedFontInfo(
  337. new FontUris(new URI(fontUri), null)
  338. , false, true, null, subfont), resourceResolver, false).getRealFont();
  339. AFPResourceAccessor accessor = getAccessor(resourceResolver);
  340. CharacterSet characterSet = CharacterSetBuilder.getDoubleByteInstance().build(characterset,
  341. super.codePage, super.encoding, tf, accessor, eventProducer);
  342. OutlineFont font = new AFPTrueTypeFont(super.name, super.embeddable, characterSet,
  343. eventProducer, subfont, new URI(fontUri));
  344. return getFontInfo(font, this);
  345. } catch (URISyntaxException e) {
  346. throw new IOException(e);
  347. }
  348. }
  349. }
  350. public static class AFPTrueTypeFont extends OutlineFont {
  351. private String ttc;
  352. private URI uri;
  353. public AFPTrueTypeFont(String name, boolean embeddable, CharacterSet charSet, AFPEventProducer eventProducer,
  354. String ttc, URI uri) {
  355. super(name, embeddable, charSet, eventProducer);
  356. this.ttc = ttc;
  357. this.uri = uri;
  358. }
  359. public FontType getFontType() {
  360. return FontType.TRUETYPE;
  361. }
  362. public String getTTC() {
  363. return ttc;
  364. }
  365. public URI getUri() {
  366. return uri;
  367. }
  368. }
  369. static final class OutlineFontConfig extends AFPFontConfigData {
  370. private final String base14;
  371. private final String characterset;
  372. private OutlineFontConfig(List<FontTriplet> triplets, String type, String codePage,
  373. String encoding, String characterset, String name, String base14, boolean embeddable, String uri) {
  374. super(triplets, type, codePage, encoding, name, embeddable, uri);
  375. this.characterset = characterset;
  376. this.base14 = base14;
  377. }
  378. @Override
  379. AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver, AFPEventProducer eventProducer)
  380. throws IOException {
  381. CharacterSet characterSet = null;
  382. if (base14 != null) {
  383. try {
  384. Typeface tf = getTypeFace(base14);
  385. characterSet = CharacterSetBuilder.getSingleByteInstance()
  386. .build(characterset, super.codePage,
  387. super.encoding, tf, eventProducer);
  388. } catch (ClassNotFoundException cnfe) {
  389. String msg = "The base 14 font class for " + characterset
  390. + " could not be found";
  391. LOG.error(msg);
  392. }
  393. } else {
  394. AFPResourceAccessor accessor = getAccessor(resourceResolver);
  395. characterSet = CharacterSetBuilder.getSingleByteInstance().buildSBCS(
  396. characterset, super.codePage, super.encoding, accessor, eventProducer);
  397. }
  398. return getFontInfo(new OutlineFont(super.name, super.embeddable, characterSet,
  399. eventProducer), this);
  400. }
  401. }
  402. private static Typeface getTypeFace(String base14Name) throws ClassNotFoundException {
  403. try {
  404. Class<? extends Typeface> clazz = Class.forName("org.apache.fop.fonts.base14."
  405. + base14Name).asSubclass(Typeface.class);
  406. return clazz.getDeclaredConstructor().newInstance();
  407. } catch (IllegalAccessException iae) {
  408. LOG.error(iae.getMessage());
  409. } catch (ClassNotFoundException cnfe) {
  410. LOG.error(cnfe.getMessage());
  411. } catch (InstantiationException ie) {
  412. LOG.error(ie.getMessage());
  413. } catch (NoSuchMethodException e) {
  414. LOG.error(e.getMessage());
  415. } catch (InvocationTargetException e) {
  416. LOG.error(e.getMessage());
  417. }
  418. throw new ClassNotFoundException("Couldn't load file for AFP font with base14 name: "
  419. + base14Name);
  420. }
  421. static final class RasterFontConfig extends AFPFontConfigData {
  422. private final List<RasterCharactersetData> charsets;
  423. private RasterFontConfig(List<FontTriplet> triplets, String type, String codePage,
  424. String encoding, String characterset, String name, String uri,
  425. List<RasterCharactersetData> csetData, boolean embeddable) {
  426. super(triplets, type, codePage, encoding, name, embeddable, uri);
  427. this.charsets = Collections.unmodifiableList(csetData);
  428. }
  429. @Override
  430. AFPFontInfo getFontInfo(InternalResourceResolver resourceResolver, AFPEventProducer eventProducer)
  431. throws IOException {
  432. RasterFont rasterFont = new RasterFont(super.name, super.embeddable);
  433. for (RasterCharactersetData charset : charsets) {
  434. if (charset.base14 != null) {
  435. try {
  436. Typeface tf = getTypeFace(charset.base14);
  437. rasterFont.addCharacterSet(charset.size,
  438. CharacterSetBuilder.getSingleByteInstance().build(
  439. charset.characterset, super.codePage, super.encoding,
  440. tf, eventProducer));
  441. } catch (ClassNotFoundException cnfe) {
  442. String msg = "The base 14 font class for " + charset.characterset
  443. + " could not be found";
  444. LOG.error(msg);
  445. } catch (IOException ie) {
  446. String msg = "The base 14 font class " + charset.characterset
  447. + " could not be instantiated";
  448. LOG.error(msg);
  449. }
  450. } else {
  451. AFPResourceAccessor accessor = getAccessor(resourceResolver);
  452. rasterFont.addCharacterSet(charset.size,
  453. CharacterSetBuilder.getSingleByteInstance().buildSBCS(charset.characterset,
  454. super.codePage, super.encoding, accessor, eventProducer));
  455. }
  456. }
  457. return getFontInfo(rasterFont, this);
  458. }
  459. }
  460. static final class RasterCharactersetData {
  461. private final String characterset;
  462. private final int size;
  463. private final String base14;
  464. private RasterCharactersetData(String characterset, int size, String base14) {
  465. this.characterset = characterset;
  466. this.size = size;
  467. this.base14 = base14;
  468. }
  469. }
  470. }