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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.util.List;
  20. import org.apache.avalon.framework.configuration.Configuration;
  21. import org.apache.avalon.framework.configuration.ConfigurationException;
  22. import org.apache.fop.apps.FOPException;
  23. import org.apache.fop.apps.FOUserAgent;
  24. import org.apache.fop.fonts.FontTriplet;
  25. import org.apache.fop.fonts.FontUtil;
  26. import org.apache.fop.fonts.Typeface;
  27. import org.apache.fop.render.PrintRendererConfigurator;
  28. import org.apache.fop.render.Renderer;
  29. import org.apache.fop.render.afp.fonts.AFPFontInfo;
  30. import org.apache.fop.render.afp.fonts.CharacterSet;
  31. import org.apache.fop.render.afp.fonts.FopCharacterSet;
  32. import org.apache.fop.render.afp.fonts.OutlineFont;
  33. import org.apache.fop.render.afp.fonts.RasterFont;
  34. import org.apache.fop.util.LogUtil;
  35. /**
  36. * AFP Renderer configurator
  37. */
  38. public class AFPRendererConfigurator extends PrintRendererConfigurator {
  39. /**
  40. * Default constructor
  41. * @param userAgent user agent
  42. */
  43. public AFPRendererConfigurator(FOUserAgent userAgent) {
  44. super(userAgent);
  45. }
  46. private AFPFontInfo buildFont(Configuration fontCfg, String fontPath)
  47. throws ConfigurationException {
  48. Configuration[] triple = fontCfg.getChildren("font-triplet");
  49. List tripleList = new java.util.ArrayList();
  50. if (triple.length == 0) {
  51. log.error("Mandatory font configuration element '<font-triplet...' is missing");
  52. return null;
  53. }
  54. for (int j = 0; j < triple.length; j++) {
  55. int weight = FontUtil.parseCSS2FontWeight(triple[j].getAttribute("weight"));
  56. tripleList.add(new FontTriplet(triple[j].getAttribute("name"),
  57. triple[j].getAttribute("style"),
  58. weight));
  59. }
  60. //build the fonts
  61. Configuration afpFontCfg = fontCfg.getChild("afp-font");
  62. if (afpFontCfg == null) {
  63. log.error("Mandatory font configuration element '<afp-font...' is missing");
  64. return null;
  65. }
  66. String path = afpFontCfg.getAttribute("path", fontPath);
  67. String type = afpFontCfg.getAttribute("type");
  68. if (type == null) {
  69. log.error("Mandatory afp-font configuration attribute 'type=' is missing");
  70. return null;
  71. }
  72. String codepage = afpFontCfg.getAttribute("codepage");
  73. if (codepage == null) {
  74. log.error("Mandatory afp-font configuration attribute 'code=' is missing");
  75. return null;
  76. }
  77. String encoding = afpFontCfg.getAttribute("encoding");
  78. if (encoding == null) {
  79. log.error("Mandatory afp-font configuration attribute 'encoding=' is missing");
  80. return null;
  81. }
  82. if ("raster".equalsIgnoreCase(type)) {
  83. String name = afpFontCfg.getAttribute("name", "Unknown");
  84. // Create a new font object
  85. RasterFont font = new RasterFont(name);
  86. Configuration[] rasters = afpFontCfg.getChildren("afp-raster-font");
  87. if (rasters.length == 0) {
  88. log.error(
  89. "Mandatory font configuration elements '<afp-raster-font...' are missing");
  90. return null;
  91. }
  92. for (int j = 0; j < rasters.length; j++) {
  93. Configuration rasterCfg = rasters[j];
  94. String characterset = rasterCfg.getAttribute("characterset");
  95. if (characterset == null) {
  96. log.error(
  97. "Mandatory afp-raster-font configuration attribute 'characterset=' is missing");
  98. return null;
  99. }
  100. int size = rasterCfg.getAttributeAsInteger("size");
  101. String base14 = rasterCfg.getAttribute("base14-font", null);
  102. if (base14 != null) {
  103. try {
  104. Class clazz = Class.forName("org.apache.fop.fonts.base14."
  105. + base14);
  106. try {
  107. Typeface tf = (Typeface)clazz.newInstance();
  108. font.addCharacterSet(size, new FopCharacterSet(
  109. codepage, encoding, characterset, size, tf));
  110. } catch (Exception ie) {
  111. String msg = "The base 14 font class " + clazz.getName()
  112. + " could not be instantiated";
  113. log.error(msg);
  114. }
  115. } catch (ClassNotFoundException cnfe) {
  116. String msg = "The base 14 font class for " + characterset
  117. + " could not be found";
  118. log.error(msg);
  119. }
  120. } else {
  121. font.addCharacterSet(size, new CharacterSet(
  122. codepage, encoding, characterset, path));
  123. }
  124. }
  125. return new AFPFontInfo(font, tripleList);
  126. } else if ("outline".equalsIgnoreCase(type)) {
  127. String characterset = afpFontCfg.getAttribute("characterset");
  128. if (characterset == null) {
  129. log.error("Mandatory afp-font configuration attribute 'characterset=' is missing");
  130. return null;
  131. }
  132. String name = afpFontCfg.getAttribute("name", characterset);
  133. CharacterSet characterSet = null;
  134. String base14 = afpFontCfg.getAttribute("base14-font", null);
  135. if (base14 != null) {
  136. try {
  137. Class clazz = Class.forName("org.apache.fop.fonts.base14."
  138. + base14);
  139. try {
  140. Typeface tf = (Typeface)clazz.newInstance();
  141. characterSet = new FopCharacterSet(
  142. codepage, encoding, characterset, 1, tf);
  143. } catch (Exception ie) {
  144. String msg = "The base 14 font class " + clazz.getName()
  145. + " could not be instantiated";
  146. log.error(msg);
  147. }
  148. } catch (ClassNotFoundException cnfe) {
  149. String msg = "The base 14 font class for " + characterset
  150. + " could not be found";
  151. log.error(msg);
  152. }
  153. } else {
  154. characterSet = new CharacterSet(codepage, encoding, characterset, path);
  155. }
  156. // Create a new font object
  157. OutlineFont font = new OutlineFont(name, characterSet);
  158. return new AFPFontInfo(font, tripleList);
  159. } else {
  160. log.error("No or incorrect type attribute");
  161. }
  162. return null;
  163. }
  164. /**
  165. * Builds a list of AFPFontInfo objects for use with the setup() method.
  166. * @param cfg Configuration object
  167. * @return List the newly created list of fonts
  168. * @throws ConfigurationException if something's wrong with the config data
  169. */
  170. private List buildFontListFromConfiguration(Configuration cfg)
  171. throws ConfigurationException {
  172. List fontList = new java.util.ArrayList();
  173. Configuration[] font = cfg.getChild("fonts").getChildren("font");
  174. for (int i = 0; i < font.length; i++) {
  175. AFPFontInfo afi = buildFont(font[i], null);
  176. if (afi != null) {
  177. if (log.isDebugEnabled()) {
  178. log.debug("Adding font " + afi.getAFPFont().getFontName());
  179. for (int j = 0; j < afi.getFontTriplets().size(); ++j) {
  180. FontTriplet triplet = (FontTriplet) afi.getFontTriplets().get(j);
  181. log.debug(" Font triplet "
  182. + triplet.getName() + ", "
  183. + triplet.getStyle() + ", "
  184. + triplet.getWeight());
  185. }
  186. }
  187. fontList.add(afi);
  188. }
  189. }
  190. return fontList;
  191. }
  192. /**
  193. * Configure the AFP renderer.
  194. * @param renderer AFP renderer
  195. * @throws FOPException fop exception
  196. * @see org.apache.fop.render.PrintRendererConfigurator#configure(Renderer)
  197. */
  198. public void configure(Renderer renderer) throws FOPException {
  199. Configuration cfg = super.getRendererConfig(renderer);
  200. if (cfg != null) {
  201. AFPRenderer afpRenderer = (AFPRenderer)renderer;
  202. try {
  203. List fontList = buildFontListFromConfiguration(cfg);
  204. afpRenderer.setFontList(fontList);
  205. } catch (ConfigurationException e) {
  206. LogUtil.handleException(log, e,
  207. userAgent.getFactory().validateUserConfigStrictly());
  208. }
  209. Configuration imagesCfg = cfg.getChild("images");
  210. if (!"color".equalsIgnoreCase(imagesCfg.getAttribute("mode", "b+w"))) {
  211. afpRenderer.setBitsPerPixel(imagesCfg.getAttributeAsInteger("bits-per-pixel", 8));
  212. } else {
  213. afpRenderer.setColorImages(true);
  214. }
  215. Configuration rendererResolutionCfg = cfg.getChild("renderer-resolution", false);
  216. if (rendererResolutionCfg != null) {
  217. afpRenderer.setResolution(rendererResolutionCfg.getValueAsInteger(
  218. AFPRenderer.DPI_240_RESOLUTION));
  219. }
  220. }
  221. }
  222. }