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.

AFPRendererConfigurator.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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.File;
  20. import java.io.IOException;
  21. import java.net.URI;
  22. import java.net.URISyntaxException;
  23. import java.util.ArrayList;
  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.fop.afp.AFPConstants;
  28. import org.apache.fop.afp.AFPEventProducer;
  29. import org.apache.fop.afp.AFPResourceLevel;
  30. import org.apache.fop.afp.AFPResourceLevelDefaults;
  31. import org.apache.fop.afp.fonts.AFPFont;
  32. import org.apache.fop.afp.fonts.AFPFontCollection;
  33. import org.apache.fop.afp.fonts.AFPFontInfo;
  34. import org.apache.fop.afp.fonts.CharacterSet;
  35. import org.apache.fop.afp.fonts.CharacterSetBuilder;
  36. import org.apache.fop.afp.fonts.CharacterSetType;
  37. import org.apache.fop.afp.fonts.DoubleByteFont;
  38. import org.apache.fop.afp.fonts.OutlineFont;
  39. import org.apache.fop.afp.fonts.RasterFont;
  40. import org.apache.fop.afp.util.DefaultFOPResourceAccessor;
  41. import org.apache.fop.afp.util.ResourceAccessor;
  42. import org.apache.fop.apps.FOPException;
  43. import org.apache.fop.apps.FOUserAgent;
  44. import org.apache.fop.fonts.FontCollection;
  45. import org.apache.fop.fonts.FontInfo;
  46. import org.apache.fop.fonts.FontManager;
  47. import org.apache.fop.fonts.FontManagerConfigurator;
  48. import org.apache.fop.fonts.FontTriplet;
  49. import org.apache.fop.fonts.FontUtil;
  50. import org.apache.fop.fonts.Typeface;
  51. import org.apache.fop.render.PrintRendererConfigurator;
  52. import org.apache.fop.render.Renderer;
  53. import org.apache.fop.render.intermediate.IFDocumentHandler;
  54. import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
  55. import org.apache.fop.util.LogUtil;
  56. /**
  57. * AFP Renderer configurator
  58. */
  59. public class AFPRendererConfigurator extends PrintRendererConfigurator
  60. implements IFDocumentHandlerConfigurator {
  61. private final AFPEventProducer eventProducer;
  62. /**
  63. * Default constructor
  64. *
  65. * @param userAgent user agent
  66. */
  67. public AFPRendererConfigurator(FOUserAgent userAgent) {
  68. super(userAgent);
  69. eventProducer = AFPEventProducer.Provider.get(userAgent.getEventBroadcaster());
  70. }
  71. private AFPFontInfo buildFont(Configuration fontCfg, String fontPath)
  72. throws ConfigurationException {
  73. FontManager fontManager = this.userAgent.getFactory().getFontManager();
  74. Configuration[] triple = fontCfg.getChildren("font-triplet");
  75. List<FontTriplet> tripletList = new ArrayList<FontTriplet>();
  76. if (triple.length == 0) {
  77. eventProducer.fontConfigMissing(this, "<font-triplet...", fontCfg.getLocation());
  78. return null;
  79. }
  80. for (Configuration config : triple) {
  81. int weight = FontUtil.parseCSS2FontWeight(config.getAttribute("weight"));
  82. FontTriplet triplet = new FontTriplet(config.getAttribute("name"),
  83. config.getAttribute("style"),
  84. weight);
  85. tripletList.add(triplet);
  86. }
  87. //build the fonts
  88. Configuration[] config = fontCfg.getChildren("afp-font");
  89. if (config.length == 0) {
  90. eventProducer.fontConfigMissing(this, "<afp-font...", fontCfg.getLocation());
  91. return null;
  92. }
  93. Configuration afpFontCfg = config[0];
  94. URI baseURI = null;
  95. String uri = afpFontCfg.getAttribute("base-uri", fontPath);
  96. if (uri == null) {
  97. //Fallback for old attribute which only supports local filenames
  98. String path = afpFontCfg.getAttribute("path", fontPath);
  99. if (path != null) {
  100. File f = new File(path);
  101. baseURI = f.toURI();
  102. }
  103. } else {
  104. try {
  105. baseURI = new URI(uri);
  106. } catch (URISyntaxException e) {
  107. eventProducer.invalidConfiguration(this, e);
  108. return null;
  109. }
  110. }
  111. ResourceAccessor accessor = new DefaultFOPResourceAccessor(
  112. this.userAgent,
  113. fontManager.getFontBaseURL(),
  114. baseURI);
  115. AFPFont font = null;
  116. try {
  117. String type = afpFontCfg.getAttribute("type");
  118. if (type == null) {
  119. eventProducer.fontConfigMissing(this, "type attribute", fontCfg.getLocation());
  120. return null;
  121. }
  122. String codepage = afpFontCfg.getAttribute("codepage");
  123. if (codepage == null) {
  124. eventProducer.fontConfigMissing(this, "codepage attribute",
  125. fontCfg.getLocation());
  126. return null;
  127. }
  128. String encoding = afpFontCfg.getAttribute("encoding");
  129. if (encoding == null) {
  130. eventProducer.fontConfigMissing(this, "encoding attribute",
  131. fontCfg.getLocation());
  132. return null;
  133. }
  134. font = fontFromType(type, codepage, encoding, accessor, afpFontCfg);
  135. } catch (ConfigurationException ce) {
  136. eventProducer.invalidConfiguration(this, ce);
  137. } catch (IOException ioe) {
  138. eventProducer.invalidConfiguration(this, ioe);
  139. } catch (IllegalArgumentException iae) {
  140. eventProducer.invalidConfiguration(this, iae);
  141. }
  142. return font != null ? new AFPFontInfo(font, tripletList) : null;
  143. }
  144. /**
  145. * Create the AFPFont based on type and type-dependent configuration.
  146. *
  147. * @param type font type e.g. 'raster', 'outline'
  148. * @param codepage codepage file
  149. * @param encoding character encoding e.g. 'Cp500', 'UnicodeBigUnmarked'
  150. * @param accessor
  151. * @param afpFontCfg
  152. * @return the created AFPFont
  153. * @throws ConfigurationException
  154. */
  155. private AFPFont fontFromType(String type, String codepage, String encoding,
  156. ResourceAccessor accessor, Configuration afpFontCfg) throws ConfigurationException,
  157. IOException {
  158. if ("raster".equalsIgnoreCase(type)) {
  159. String name = afpFontCfg.getAttribute("name", "Unknown");
  160. // Create a new font object
  161. RasterFont font = new RasterFont(name);
  162. Configuration[] rasters = afpFontCfg.getChildren("afp-raster-font");
  163. if (rasters.length == 0) {
  164. eventProducer.fontConfigMissing(this, "<afp-raster-font...",
  165. afpFontCfg.getLocation());
  166. return null;
  167. }
  168. for (int j = 0; j < rasters.length; j++) {
  169. Configuration rasterCfg = rasters[j];
  170. String characterset = rasterCfg.getAttribute("characterset");
  171. if (characterset == null) {
  172. eventProducer.fontConfigMissing(this, "characterset attribute",
  173. afpFontCfg.getLocation());
  174. return null;
  175. }
  176. float size = rasterCfg.getAttributeAsFloat("size");
  177. int sizeMpt = (int) (size * 1000);
  178. String base14 = rasterCfg.getAttribute("base14-font", null);
  179. if (base14 != null) {
  180. try {
  181. Class<? extends Typeface> clazz = Class.forName(
  182. "org.apache.fop.fonts.base14." + base14).asSubclass(Typeface.class);
  183. try {
  184. Typeface tf = clazz.newInstance();
  185. font.addCharacterSet(sizeMpt,
  186. CharacterSetBuilder.getSingleByteInstance()
  187. .build(characterset, codepage, encoding, tf,
  188. eventProducer));
  189. } catch (Exception ie) {
  190. String msg = "The base 14 font class " + clazz.getName()
  191. + " could not be instantiated";
  192. log.error(msg);
  193. }
  194. } catch (ClassNotFoundException cnfe) {
  195. String msg = "The base 14 font class for " + characterset
  196. + " could not be found";
  197. log.error(msg);
  198. }
  199. } else {
  200. font.addCharacterSet(sizeMpt, CharacterSetBuilder.getSingleByteInstance()
  201. .buildSBCS(characterset, codepage, encoding, accessor, eventProducer));
  202. }
  203. }
  204. return font;
  205. } else if ("outline".equalsIgnoreCase(type)) {
  206. String characterset = afpFontCfg.getAttribute("characterset");
  207. if (characterset == null) {
  208. eventProducer.fontConfigMissing(this, "characterset attribute",
  209. afpFontCfg.getLocation());
  210. return null;
  211. }
  212. String name = afpFontCfg.getAttribute("name", characterset);
  213. CharacterSet characterSet = null;
  214. String base14 = afpFontCfg.getAttribute("base14-font", null);
  215. if (base14 != null) {
  216. try {
  217. Class<? extends Typeface> clazz = Class.forName("org.apache.fop.fonts.base14."
  218. + base14).asSubclass(Typeface.class);
  219. try {
  220. Typeface tf = clazz.newInstance();
  221. characterSet = CharacterSetBuilder.getSingleByteInstance()
  222. .build(characterset, codepage, encoding, tf, eventProducer);
  223. } catch (Exception ie) {
  224. String msg = "The base 14 font class " + clazz.getName()
  225. + " could not be instantiated";
  226. log.error(msg);
  227. }
  228. } catch (ClassNotFoundException cnfe) {
  229. String msg = "The base 14 font class for " + characterset
  230. + " could not be found";
  231. log.error(msg);
  232. }
  233. } else {
  234. characterSet = CharacterSetBuilder.getSingleByteInstance().buildSBCS(
  235. characterset, codepage, encoding, accessor, eventProducer);
  236. }
  237. // Return new font object
  238. return new OutlineFont(name, characterSet);
  239. } else if ("CIDKeyed".equalsIgnoreCase(type)) {
  240. String characterset = afpFontCfg.getAttribute("characterset");
  241. if (characterset == null) {
  242. eventProducer.fontConfigMissing(this, "characterset attribute",
  243. afpFontCfg.getLocation());
  244. return null;
  245. }
  246. String name = afpFontCfg.getAttribute("name", characterset);
  247. CharacterSet characterSet = null;
  248. CharacterSetType charsetType = afpFontCfg.getAttributeAsBoolean("ebcdic-dbcs", false)
  249. ? CharacterSetType.DOUBLE_BYTE_LINE_DATA : CharacterSetType.DOUBLE_BYTE;
  250. characterSet = CharacterSetBuilder.getDoubleByteInstance().buildDBCS(characterset,
  251. codepage, encoding, charsetType, accessor, eventProducer);
  252. // Create a new font object
  253. DoubleByteFont font = new DoubleByteFont(name, characterSet);
  254. return font;
  255. } else {
  256. log.error("No or incorrect type attribute: " + type);
  257. }
  258. return null;
  259. }
  260. /**
  261. * Builds a list of AFPFontInfo objects for use with the setup() method.
  262. *
  263. * @param cfg Configuration object
  264. * @param eventProducer for AFP font related events
  265. * @return List the newly created list of fonts
  266. * @throws ConfigurationException if something's wrong with the config data
  267. */
  268. private List<AFPFontInfo> buildFontListFromConfiguration(Configuration cfg,
  269. AFPEventProducer eventProducer) throws FOPException, ConfigurationException {
  270. Configuration fonts = cfg.getChild("fonts");
  271. FontManager fontManager = this.userAgent.getFactory().getFontManager();
  272. // General matcher
  273. FontTriplet.Matcher referencedFontsMatcher = fontManager.getReferencedFontsMatcher();
  274. // Renderer-specific matcher
  275. FontTriplet.Matcher localMatcher = null;
  276. // Renderer-specific referenced fonts
  277. Configuration referencedFontsCfg = fonts.getChild("referenced-fonts", false);
  278. if (referencedFontsCfg != null) {
  279. localMatcher = FontManagerConfigurator.createFontsMatcher(
  280. referencedFontsCfg, this.userAgent.getFactory().validateUserConfigStrictly());
  281. }
  282. List<AFPFontInfo> fontList = new java.util.ArrayList<AFPFontInfo>();
  283. Configuration[] font = fonts.getChildren("font");
  284. final String fontPath = null;
  285. for (int i = 0; i < font.length; i++) {
  286. AFPFontInfo afi = buildFont(font[i], fontPath);
  287. if (afi != null) {
  288. if (log.isDebugEnabled()) {
  289. log.debug("Adding font " + afi.getAFPFont().getFontName());
  290. }
  291. List<FontTriplet> fontTriplets = afi.getFontTriplets();
  292. for (int j = 0; j < fontTriplets.size(); ++j) {
  293. FontTriplet triplet = fontTriplets.get(j);
  294. if (log.isDebugEnabled()) {
  295. log.debug(" Font triplet "
  296. + triplet.getName() + ", "
  297. + triplet.getStyle() + ", "
  298. + triplet.getWeight());
  299. }
  300. if ((referencedFontsMatcher != null && referencedFontsMatcher.matches(triplet))
  301. || (localMatcher != null && localMatcher.matches(triplet))) {
  302. afi.getAFPFont().setEmbeddable(false);
  303. break;
  304. }
  305. }
  306. fontList.add(afi);
  307. }
  308. }
  309. return fontList;
  310. }
  311. /** images are converted to grayscale bitmapped IOCA */
  312. private static final String IMAGES_MODE_GRAYSCALE = "b+w";
  313. /** images are converted to color bitmapped IOCA */
  314. private static final String IMAGES_MODE_COLOR = "color";
  315. /**
  316. * Throws an UnsupportedOperationException.
  317. *
  318. * @param renderer not used
  319. */
  320. @Override
  321. public void configure(Renderer renderer) {
  322. throw new UnsupportedOperationException();
  323. }
  324. private void configure(AFPCustomizable customizable, Configuration cfg) throws FOPException {
  325. // image information
  326. Configuration imagesCfg = cfg.getChild("images");
  327. // default to grayscale images
  328. String imagesMode = imagesCfg.getAttribute("mode", IMAGES_MODE_GRAYSCALE);
  329. if (IMAGES_MODE_COLOR.equals(imagesMode)) {
  330. customizable.setColorImages(true);
  331. boolean cmyk = imagesCfg.getAttributeAsBoolean("cmyk", false);
  332. customizable.setCMYKImagesSupported(cmyk);
  333. } else {
  334. customizable.setColorImages(false);
  335. // default to 8 bits per pixel
  336. int bitsPerPixel = imagesCfg.getAttributeAsInteger("bits-per-pixel", 8);
  337. customizable.setBitsPerPixel(bitsPerPixel);
  338. }
  339. String dithering = imagesCfg.getAttribute("dithering-quality", "medium");
  340. float dq = 0.5f;
  341. if (dithering.startsWith("min")) {
  342. dq = 0.0f;
  343. } else if (dithering.startsWith("max")) {
  344. dq = 1.0f;
  345. } else {
  346. try {
  347. dq = Float.parseFloat(dithering);
  348. } catch (NumberFormatException nfe) {
  349. //ignore and leave the default above
  350. }
  351. }
  352. customizable.setDitheringQuality(dq);
  353. // native image support
  354. boolean nativeImageSupport = imagesCfg.getAttributeAsBoolean("native", false);
  355. customizable.setNativeImagesSupported(nativeImageSupport);
  356. Configuration jpegConfig = imagesCfg.getChild("jpeg");
  357. boolean allowEmbedding = false;
  358. float ieq = 1.0f;
  359. if (jpegConfig != null) {
  360. allowEmbedding = jpegConfig.getAttributeAsBoolean("allow-embedding", false);
  361. String bitmapEncodingQuality = jpegConfig.getAttribute("bitmap-encoding-quality", null);
  362. if (bitmapEncodingQuality != null) {
  363. try {
  364. ieq = Float.parseFloat(bitmapEncodingQuality);
  365. } catch (NumberFormatException nfe) {
  366. //ignore and leave the default above
  367. }
  368. }
  369. }
  370. customizable.canEmbedJpeg(allowEmbedding);
  371. customizable.setBitmapEncodingQuality(ieq);
  372. //FS11 and FS45 page segment wrapping
  373. boolean pSeg = imagesCfg.getAttributeAsBoolean("pseg", false);
  374. customizable.setWrapPSeg(pSeg);
  375. //FS45 image forcing
  376. boolean fs45 = imagesCfg.getAttributeAsBoolean("fs45", false);
  377. customizable.setFS45(fs45);
  378. // shading (filled rectangles)
  379. Configuration shadingCfg = cfg.getChild("shading");
  380. AFPShadingMode shadingMode = AFPShadingMode.valueOf(
  381. shadingCfg.getValue(AFPShadingMode.COLOR.getName()));
  382. customizable.setShadingMode(shadingMode);
  383. // GOCA Support
  384. Configuration gocaCfg = cfg.getChild("goca");
  385. boolean gocaEnabled = gocaCfg.getAttributeAsBoolean(
  386. "enabled", customizable.isGOCAEnabled());
  387. customizable.setGOCAEnabled(gocaEnabled);
  388. String gocaText = gocaCfg.getAttribute(
  389. "text", customizable.isStrokeGOCAText() ? "stroke" : "default");
  390. customizable.setStrokeGOCAText("stroke".equalsIgnoreCase(gocaText)
  391. || "shapes".equalsIgnoreCase(gocaText));
  392. // renderer resolution
  393. Configuration rendererResolutionCfg = cfg.getChild("renderer-resolution", false);
  394. if (rendererResolutionCfg != null) {
  395. customizable.setResolution(rendererResolutionCfg.getValueAsInteger(240));
  396. }
  397. // renderer resolution
  398. Configuration lineWidthCorrectionCfg = cfg.getChild("line-width-correction", false);
  399. if (lineWidthCorrectionCfg != null) {
  400. customizable.setLineWidthCorrection(lineWidthCorrectionCfg
  401. .getValueAsFloat(AFPConstants.LINE_WIDTH_CORRECTION));
  402. }
  403. // a default external resource group file setting
  404. Configuration resourceGroupFileCfg
  405. = cfg.getChild("resource-group-file", false);
  406. if (resourceGroupFileCfg != null) {
  407. String resourceGroupDest = null;
  408. try {
  409. resourceGroupDest = resourceGroupFileCfg.getValue();
  410. if (resourceGroupDest != null) {
  411. File resourceGroupFile = new File(resourceGroupDest);
  412. boolean created = resourceGroupFile.createNewFile();
  413. if (created && resourceGroupFile.canWrite()) {
  414. customizable.setDefaultResourceGroupFilePath(resourceGroupDest);
  415. } else {
  416. log.warn("Unable to write to default external resource group file '"
  417. + resourceGroupDest + "'");
  418. }
  419. }
  420. } catch (ConfigurationException e) {
  421. LogUtil.handleException(log, e,
  422. userAgent.getFactory().validateUserConfigStrictly());
  423. } catch (IOException ioe) {
  424. throw new FOPException("Could not create default external resource group file"
  425. , ioe);
  426. }
  427. }
  428. Configuration defaultResourceLevelCfg = cfg.getChild("default-resource-levels", false);
  429. if (defaultResourceLevelCfg != null) {
  430. AFPResourceLevelDefaults defaults = new AFPResourceLevelDefaults();
  431. String[] types = defaultResourceLevelCfg.getAttributeNames();
  432. for (int i = 0, c = types.length; i < c; i++) {
  433. String type = types[i];
  434. try {
  435. String level = defaultResourceLevelCfg.getAttribute(type);
  436. defaults.setDefaultResourceLevel(type, AFPResourceLevel.valueOf(level));
  437. } catch (IllegalArgumentException iae) {
  438. LogUtil.handleException(log, iae,
  439. userAgent.getFactory().validateUserConfigStrictly());
  440. } catch (ConfigurationException e) {
  441. LogUtil.handleException(log, e,
  442. userAgent.getFactory().validateUserConfigStrictly());
  443. }
  444. }
  445. customizable.setResourceLevelDefaults(defaults);
  446. }
  447. }
  448. /** {@inheritDoc} */
  449. @Override
  450. public void configure(IFDocumentHandler documentHandler) throws FOPException {
  451. Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
  452. if (cfg != null) {
  453. AFPDocumentHandler afpDocumentHandler = (AFPDocumentHandler) documentHandler;
  454. configure(afpDocumentHandler, cfg);
  455. }
  456. }
  457. /** {@inheritDoc} */
  458. @Override
  459. public void setupFontInfo(IFDocumentHandler documentHandler, FontInfo fontInfo)
  460. throws FOPException {
  461. FontManager fontManager = userAgent.getFactory().getFontManager();
  462. List<AFPFontCollection> fontCollections = new ArrayList<AFPFontCollection>();
  463. Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
  464. if (cfg != null) {
  465. try {
  466. List<AFPFontInfo> fontList = buildFontListFromConfiguration(cfg, eventProducer);
  467. fontCollections.add(new AFPFontCollection(
  468. userAgent.getEventBroadcaster(), fontList));
  469. } catch (ConfigurationException e) {
  470. eventProducer.invalidConfiguration(this, e);
  471. LogUtil.handleException(log, e,
  472. userAgent.getFactory().validateUserConfigStrictly());
  473. }
  474. } else {
  475. fontCollections.add(new AFPFontCollection(userAgent.getEventBroadcaster(), null));
  476. }
  477. fontManager.setup(fontInfo,
  478. fontCollections.toArray(
  479. new FontCollection[fontCollections.size()]));
  480. documentHandler.setFontInfo(fontInfo);
  481. }
  482. }