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

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