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.

PDFDocumentGraphics2DConfigurator.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.svg;
  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.io.ResourceResolverFactory;
  24. import org.apache.fop.apps.io.URIResolverWrapper;
  25. import org.apache.fop.fonts.DefaultFontConfig;
  26. import org.apache.fop.fonts.DefaultFontConfigurator;
  27. import org.apache.fop.fonts.EmbedFontInfo;
  28. import org.apache.fop.fonts.FontCacheManagerFactory;
  29. import org.apache.fop.fonts.FontDetectorFactory;
  30. import org.apache.fop.fonts.FontEventListener;
  31. import org.apache.fop.fonts.FontInfo;
  32. import org.apache.fop.fonts.FontManager;
  33. import org.apache.fop.fonts.FontSetup;
  34. import org.apache.fop.pdf.PDFDocument;
  35. import org.apache.fop.render.pdf.PDFRendererConfig;
  36. import org.apache.fop.render.pdf.PDFRendererConfig.PDFRendererConfigParser;
  37. /**
  38. * Configurator class for PDFDocumentGraphics2D.
  39. */
  40. public class PDFDocumentGraphics2DConfigurator {
  41. /**
  42. * Configures a PDFDocumentGraphics2D instance using an Avalon Configuration object.
  43. * @param graphics the PDFDocumentGraphics2D instance
  44. * @param cfg the configuration
  45. * @param useComplexScriptFeatures true if complex script features enabled
  46. * @throws ConfigurationException if an error occurs while configuring the object
  47. */
  48. public void configure(PDFDocumentGraphics2D graphics, Configuration cfg,
  49. boolean useComplexScriptFeatures )
  50. throws ConfigurationException {
  51. PDFDocument pdfDoc = graphics.getPDFDocument();
  52. try {
  53. //Filter map
  54. PDFRendererConfig pdfConfig = new PDFRendererConfigParser().build(null, cfg);
  55. pdfDoc.setFilterMap(pdfConfig.getFilterMap());
  56. } catch (FOPException e) {
  57. throw new RuntimeException(e);
  58. }
  59. //Fonts
  60. try {
  61. FontInfo fontInfo = createFontInfo(cfg, useComplexScriptFeatures);
  62. graphics.setFontInfo(fontInfo);
  63. } catch (FOPException e) {
  64. throw new ConfigurationException("Error while setting up fonts", e);
  65. }
  66. }
  67. /**
  68. * Creates the {@link FontInfo} instance for the given configuration.
  69. * @param cfg the configuration
  70. * @param useComplexScriptFeatures true if complex script features enabled
  71. * @return the font collection
  72. * @throws FOPException if an error occurs while setting up the fonts
  73. */
  74. public static FontInfo createFontInfo(Configuration cfg, boolean useComplexScriptFeatures)
  75. throws FOPException {
  76. FontInfo fontInfo = new FontInfo();
  77. final boolean strict = false;
  78. if (cfg != null) {
  79. URIResolverWrapper resolver = ResourceResolverFactory.createDefaultWrapper();
  80. //TODO The following could be optimized by retaining the FontManager somewhere
  81. FontManager fontManager = new FontManager(resolver, FontDetectorFactory.createDefault(),
  82. FontCacheManagerFactory.createDefault());
  83. //TODO Make use of fontBaseURL, font substitution and referencing configuration
  84. //Requires a change to the expected configuration layout
  85. final FontEventListener listener = null;
  86. DefaultFontConfig.DefaultFontConfigParser parser
  87. = new DefaultFontConfig.DefaultFontConfigParser();
  88. DefaultFontConfig fontInfoConfig = parser.parse(cfg, strict);
  89. DefaultFontConfigurator fontInfoConfigurator
  90. = new DefaultFontConfigurator(fontManager, listener, strict);
  91. List<EmbedFontInfo> fontInfoList = fontInfoConfigurator.configure(fontInfoConfig);
  92. fontManager.saveCache();
  93. FontSetup.setup(fontInfo, fontInfoList, resolver, useComplexScriptFeatures);
  94. } else {
  95. FontSetup.setup(fontInfo, useComplexScriptFeatures);
  96. }
  97. return fontInfo;
  98. }
  99. }