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

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