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

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