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.9KB

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