Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AFPFontConfig.java 23KB

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