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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.util.List;
  21. import org.apache.avalon.framework.configuration.Configuration;
  22. import org.apache.avalon.framework.configuration.ConfigurationException;
  23. import org.apache.fop.afp.AFPResourceLevel;
  24. import org.apache.fop.afp.AFPResourceLevelDefaults;
  25. import org.apache.fop.afp.fonts.AFPFontInfo;
  26. import org.apache.fop.afp.fonts.CharacterSet;
  27. import org.apache.fop.afp.fonts.FopCharacterSet;
  28. import org.apache.fop.afp.fonts.OutlineFont;
  29. import org.apache.fop.afp.fonts.RasterFont;
  30. import org.apache.fop.apps.FOPException;
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.fonts.FontTriplet;
  33. import org.apache.fop.fonts.FontUtil;
  34. import org.apache.fop.fonts.Typeface;
  35. import org.apache.fop.render.PrintRendererConfigurator;
  36. import org.apache.fop.render.Renderer;
  37. import org.apache.fop.util.LogUtil;
  38. /**
  39. * AFP Renderer configurator
  40. */
  41. public class AFPRendererConfigurator extends PrintRendererConfigurator {
  42. /**
  43. * Default constructor
  44. *
  45. * @param userAgent user agent
  46. */
  47. public AFPRendererConfigurator(FOUserAgent userAgent) {
  48. super(userAgent);
  49. }
  50. private AFPFontInfo buildFont(Configuration fontCfg, String fontPath)
  51. throws ConfigurationException {
  52. Configuration[] triple = fontCfg.getChildren("font-triplet");
  53. List/*<FontTriplet>*/ tripletList = new java.util.ArrayList/*<FontTriplet>*/();
  54. if (triple.length == 0) {
  55. log.error("Mandatory font configuration element '<font-triplet...' is missing");
  56. return null;
  57. }
  58. for (int j = 0; j < triple.length; j++) {
  59. int weight = FontUtil.parseCSS2FontWeight(triple[j].getAttribute("weight"));
  60. FontTriplet triplet = new FontTriplet(triple[j].getAttribute("name"),
  61. triple[j].getAttribute("style"),
  62. weight);
  63. tripletList.add(triplet);
  64. }
  65. //build the fonts
  66. Configuration afpFontCfg = fontCfg.getChild("afp-font");
  67. if (afpFontCfg == null) {
  68. log.error("Mandatory font configuration element '<afp-font...' is missing");
  69. return null;
  70. }
  71. String path = afpFontCfg.getAttribute("path", fontPath);
  72. String type = afpFontCfg.getAttribute("type");
  73. if (type == null) {
  74. log.error("Mandatory afp-font configuration attribute 'type=' is missing");
  75. return null;
  76. }
  77. String codepage = afpFontCfg.getAttribute("codepage");
  78. if (codepage == null) {
  79. log.error("Mandatory afp-font configuration attribute 'code=' is missing");
  80. return null;
  81. }
  82. String encoding = afpFontCfg.getAttribute("encoding");
  83. if (encoding == null) {
  84. log.error("Mandatory afp-font configuration attribute 'encoding=' is missing");
  85. return null;
  86. }
  87. if ("raster".equalsIgnoreCase(type)) {
  88. String name = afpFontCfg.getAttribute("name", "Unknown");
  89. // Create a new font object
  90. RasterFont font = new RasterFont(name);
  91. Configuration[] rasters = afpFontCfg.getChildren("afp-raster-font");
  92. if (rasters.length == 0) {
  93. log.error(
  94. "Mandatory font configuration elements '<afp-raster-font...' are missing");
  95. return null;
  96. }
  97. for (int j = 0; j < rasters.length; j++) {
  98. Configuration rasterCfg = rasters[j];
  99. String characterset = rasterCfg.getAttribute("characterset");
  100. if (characterset == null) {
  101. log.error(
  102. "Mandatory afp-raster-font configuration attribute 'characterset=' is missing");
  103. return null;
  104. }
  105. int size = rasterCfg.getAttributeAsInteger("size");
  106. String base14 = rasterCfg.getAttribute("base14-font", null);
  107. if (base14 != null) {
  108. try {
  109. Class clazz = Class.forName("org.apache.fop.fonts.base14."
  110. + base14);
  111. try {
  112. Typeface tf = (Typeface)clazz.newInstance();
  113. font.addCharacterSet(size, new FopCharacterSet(
  114. codepage, encoding, characterset, tf));
  115. } catch (Exception ie) {
  116. String msg = "The base 14 font class " + clazz.getName()
  117. + " could not be instantiated";
  118. log.error(msg);
  119. }
  120. } catch (ClassNotFoundException cnfe) {
  121. String msg = "The base 14 font class for " + characterset
  122. + " could not be found";
  123. log.error(msg);
  124. }
  125. } else {
  126. font.addCharacterSet(size, new CharacterSet(
  127. codepage, encoding, characterset, path));
  128. }
  129. }
  130. return new AFPFontInfo(font, tripletList);
  131. } else if ("outline".equalsIgnoreCase(type)) {
  132. String characterset = afpFontCfg.getAttribute("characterset");
  133. if (characterset == null) {
  134. log.error("Mandatory afp-font configuration attribute 'characterset=' is missing");
  135. return null;
  136. }
  137. String name = afpFontCfg.getAttribute("name", characterset);
  138. CharacterSet characterSet = null;
  139. String base14 = afpFontCfg.getAttribute("base14-font", null);
  140. if (base14 != null) {
  141. try {
  142. Class clazz = Class.forName("org.apache.fop.fonts.base14."
  143. + base14);
  144. try {
  145. Typeface tf = (Typeface)clazz.newInstance();
  146. characterSet = new FopCharacterSet(
  147. codepage, encoding, characterset, tf);
  148. } catch (Exception ie) {
  149. String msg = "The base 14 font class " + clazz.getName()
  150. + " could not be instantiated";
  151. log.error(msg);
  152. }
  153. } catch (ClassNotFoundException cnfe) {
  154. String msg = "The base 14 font class for " + characterset
  155. + " could not be found";
  156. log.error(msg);
  157. }
  158. } else {
  159. characterSet = new CharacterSet(codepage, encoding, characterset, path);
  160. }
  161. // Create a new font object
  162. OutlineFont font = new OutlineFont(name, characterSet);
  163. return new AFPFontInfo(font, tripletList);
  164. } else {
  165. log.error("No or incorrect type attribute");
  166. }
  167. return null;
  168. }
  169. /**
  170. * Builds a list of AFPFontInfo objects for use with the setup() method.
  171. *
  172. * @param cfg Configuration object
  173. * @return List the newly created list of fonts
  174. * @throws ConfigurationException if something's wrong with the config data
  175. */
  176. private List/*<AFPFontInfo>*/ buildFontListFromConfiguration(Configuration cfg)
  177. throws ConfigurationException {
  178. List/*<AFPFontInfo>*/ fontList = new java.util.ArrayList();
  179. Configuration[] font = cfg.getChild("fonts").getChildren("font");
  180. final String fontPath = null;
  181. for (int i = 0; i < font.length; i++) {
  182. AFPFontInfo afi = buildFont(font[i], fontPath);
  183. if (afi != null) {
  184. if (log.isDebugEnabled()) {
  185. log.debug("Adding font " + afi.getAFPFont().getFontName());
  186. List/*<FontTriplet>*/ fontTriplets = afi.getFontTriplets();
  187. for (int j = 0; j < fontTriplets.size(); ++j) {
  188. FontTriplet triplet = (FontTriplet) fontTriplets.get(j);
  189. log.debug(" Font triplet "
  190. + triplet.getName() + ", "
  191. + triplet.getStyle() + ", "
  192. + triplet.getWeight());
  193. }
  194. }
  195. fontList.add(afi);
  196. }
  197. }
  198. return fontList;
  199. }
  200. /** images are converted to grayscale bitmapped IOCA */
  201. private static final String IMAGES_MODE_GRAYSCALE = "b+w";
  202. /** images are converted to color bitmapped IOCA */
  203. private static final String IMAGES_MODE_COLOR = "color";
  204. /**
  205. * Configure the AFP renderer.
  206. *
  207. * @param renderer AFP renderer
  208. * @throws FOPException fop exception
  209. * @see org.apache.fop.render.PrintRendererConfigurator#configure(Renderer)
  210. */
  211. public void configure(Renderer renderer) throws FOPException {
  212. Configuration cfg = super.getRendererConfig(renderer);
  213. if (cfg != null) {
  214. AFPRenderer afpRenderer = (AFPRenderer)renderer;
  215. try {
  216. List/*<AFPFontInfo>*/ fontList = buildFontListFromConfiguration(cfg);
  217. afpRenderer.setFontList(fontList);
  218. } catch (ConfigurationException e) {
  219. LogUtil.handleException(log, e,
  220. userAgent.getFactory().validateUserConfigStrictly());
  221. }
  222. // image information
  223. Configuration imagesCfg = cfg.getChild("images");
  224. // default to grayscale images
  225. String imagesMode = imagesCfg.getAttribute("mode", IMAGES_MODE_GRAYSCALE);
  226. if (IMAGES_MODE_COLOR.equals(imagesMode)) {
  227. afpRenderer.setColorImages(true);
  228. } else {
  229. afpRenderer.setColorImages(false);
  230. // default to 8 bits per pixel
  231. int bitsPerPixel = imagesCfg.getAttributeAsInteger("bits-per-pixel", 8);
  232. afpRenderer.setBitsPerPixel(bitsPerPixel);
  233. }
  234. // native image support
  235. boolean nativeImageSupport = imagesCfg.getAttributeAsBoolean("native", false);
  236. afpRenderer.setNativeImagesSupported(nativeImageSupport);
  237. // renderer resolution
  238. Configuration rendererResolutionCfg = cfg.getChild("renderer-resolution", false);
  239. if (rendererResolutionCfg != null) {
  240. afpRenderer.setResolution(rendererResolutionCfg.getValueAsInteger(240));
  241. }
  242. // a default external resource group file setting
  243. Configuration resourceGroupFileCfg
  244. = cfg.getChild("resource-group-file", false);
  245. if (resourceGroupFileCfg != null) {
  246. String resourceGroupDest = null;
  247. try {
  248. resourceGroupDest = resourceGroupFileCfg.getValue();
  249. } catch (ConfigurationException e) {
  250. LogUtil.handleException(log, e,
  251. userAgent.getFactory().validateUserConfigStrictly());
  252. }
  253. File resourceGroupFile = new File(resourceGroupDest);
  254. if (resourceGroupFile.canWrite()) {
  255. afpRenderer.setDefaultResourceGroupFilePath(resourceGroupDest);
  256. } else {
  257. log.warn("Unable to write to default external resource group file '"
  258. + resourceGroupDest + "'");
  259. }
  260. }
  261. Configuration defaultResourceLevelCfg = cfg.getChild("default-resource-levels", false);
  262. if (defaultResourceLevelCfg != null) {
  263. AFPResourceLevelDefaults defaults = new AFPResourceLevelDefaults();
  264. String[] types = defaultResourceLevelCfg.getAttributeNames();
  265. for (int i = 0, c = types.length; i < c; i++) {
  266. String type = types[i];
  267. try {
  268. String level = defaultResourceLevelCfg.getAttribute(type);
  269. defaults.setDefaultResourceLevel(type, AFPResourceLevel.valueOf(level));
  270. } catch (IllegalArgumentException iae) {
  271. LogUtil.handleException(log, iae,
  272. userAgent.getFactory().validateUserConfigStrictly());
  273. } catch (ConfigurationException e) {
  274. LogUtil.handleException(log, e,
  275. userAgent.getFactory().validateUserConfigStrictly());
  276. }
  277. }
  278. afpRenderer.setResourceLevelDefaults(defaults);
  279. }
  280. }
  281. }
  282. }