From 1867bcc73af7bbb10a7729ff01184a1909abbb4d Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Thu, 17 Jul 2008 09:11:09 +0000 Subject: [PATCH] fonts package must not depend on the render package. Refactored to restore that rule. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@677543 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/fonts/CustomFontCollection.java | 37 ++++++------ .../org/apache/fop/fonts/FontManager.java | 60 ++++--------------- .../org/apache/fop/render/PrintRenderer.java | 29 +++++---- .../java2d/ConfiguredFontCollection.java | 45 +++++++------- .../fop/render/java2d/Java2DRenderer.java | 9 ++- .../apache/fop/render/pcl/PCLRenderer.java | 34 +++++++---- .../fop/config/FontsSubstitutionTestCase.java | 13 +++- 7 files changed, 108 insertions(+), 119 deletions(-) diff --git a/src/java/org/apache/fop/fonts/CustomFontCollection.java b/src/java/org/apache/fop/fonts/CustomFontCollection.java index 5e5a61189..5a0bba782 100644 --- a/src/java/org/apache/fop/fonts/CustomFontCollection.java +++ b/src/java/org/apache/fop/fonts/CustomFontCollection.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,38 +21,35 @@ package org.apache.fop.fonts; import java.util.List; -import org.apache.fop.render.PrintRenderer; - /** * Sets up a set of custom (embedded) fonts */ public class CustomFontCollection implements FontCollection { - private PrintRenderer renderer = null; + private FontResolver fontResolver; + private List/**/ embedFontInfoList; /** - * A print renderer to configure - * @param renderer a print renderer + * Main constructor. + * @param fontResolver a font resolver + * @param customFonts the list of custom fonts */ - public CustomFontCollection(PrintRenderer renderer) { - this.renderer = renderer; + public CustomFontCollection(FontResolver fontResolver, + List/**/ customFonts) { + this.fontResolver = fontResolver; + if (this.fontResolver == null) { + //Ensure that we have minimal font resolution capabilities + this.fontResolver = FontManager.createMinimalFontResolver(); + } + this.embedFontInfoList = customFonts; } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public int setup(int num, FontInfo fontInfo) { - List/**/ embedFontInfoList = renderer.getFontList(); if (embedFontInfoList == null) { return num; //No fonts to process } - FontResolver resolver = renderer.getFontResolver(); - if (resolver == null) { - //Ensure that we have minimal font resolution capabilities - resolver = FontManager.createMinimalFontResolver(); - } - String internalName = null; //FontReader reader = null; @@ -69,7 +66,7 @@ public class CustomFontCollection implements FontCollection { fontInfo.addMetrics(internalName, reader.getFont()); */ - LazyFont font = new LazyFont(embedFontInfo, resolver); + LazyFont font = new LazyFont(embedFontInfo, this.fontResolver); fontInfo.addMetrics(internalName, font); List triplets = embedFontInfo.getFontTriplets(); diff --git a/src/java/org/apache/fop/fonts/FontManager.java b/src/java/org/apache/fop/fonts/FontManager.java index c180fbcc9..b3833cf50 100644 --- a/src/java/org/apache/fop/fonts/FontManager.java +++ b/src/java/org/apache/fop/fonts/FontManager.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,7 +19,6 @@ package org.apache.fop.fonts; -import java.awt.Graphics2D; import java.net.MalformedURLException; import javax.xml.transform.Source; @@ -27,7 +26,6 @@ import javax.xml.transform.stream.StreamSource; import org.apache.fop.fonts.FontTriplet.Matcher; import org.apache.fop.fonts.substitute.FontSubstitutions; -import org.apache.fop.render.PrintRenderer; // TODO: Refactor fonts package so major font activities (autodetection etc) // are all centrally managed and delegated from this class, also remove dependency on FopFactory @@ -144,55 +142,17 @@ public class FontManager { } /** - * Sets up the fonts on a given PrintRenderer - * @param renderer a print renderer + * Sets up the fonts on a given FontInfo object. The fonts to setup are defined by an + * array of {@code FontCollection} objects. + * @param fontInfo the FontInfo object to set up + * @param fontCollections the array of font collections/sources */ - public void setupRenderer(PrintRenderer renderer) { - FontInfo fontInfo = renderer.getFontInfo(); - + public void setup(FontInfo fontInfo, FontCollection[] fontCollections) { int startNum = 1; - // Configure base 14 fonts - org.apache.fop.fonts.base14.Base14FontCollection base14FontCollection - = new org.apache.fop.fonts.base14.Base14FontCollection(this.enableBase14Kerning); - startNum = base14FontCollection.setup(startNum, fontInfo); - - // Configure any custom font collection - org.apache.fop.fonts.CustomFontCollection customFontCollection - = new org.apache.fop.fonts.CustomFontCollection(renderer); - startNum = customFontCollection.setup(startNum, fontInfo); - - // Make any defined substitutions in the font info - getFontSubstitutions().adjustFontInfo(fontInfo); - } - - /** - * Sets up the fonts on a given PrintRenderer with Graphics2D - * @param renderer a print renderer - * @param graphics2D a graphics 2D - */ - public void setupRenderer(PrintRenderer renderer, Graphics2D graphics2D) { - FontInfo fontInfo = renderer.getFontInfo(); - - int startNum = 1; - - // setup base 14 fonts - org.apache.fop.render.java2d.Base14FontCollection base14FontCollection - = new org.apache.fop.render.java2d.Base14FontCollection(graphics2D); - - // setup any custom font collection - startNum = base14FontCollection.setup(startNum, fontInfo); - - // setup any installed fonts - org.apache.fop.render.java2d.InstalledFontCollection installedFontCollection - = new org.apache.fop.render.java2d.InstalledFontCollection(graphics2D); - startNum = installedFontCollection.setup(startNum, fontInfo); - - // setup any configured fonts - org.apache.fop.render.java2d.ConfiguredFontCollection configuredFontCollection - = new org.apache.fop.render.java2d.ConfiguredFontCollection(renderer); - startNum = configuredFontCollection.setup(startNum, fontInfo); - + for (int i = 0, c = fontCollections.length; i < c; i++) { + startNum = fontCollections[i].setup(startNum, fontInfo); + } // Make any defined substitutions in the font info getFontSubstitutions().adjustFontInfo(fontInfo); } diff --git a/src/java/org/apache/fop/render/PrintRenderer.java b/src/java/org/apache/fop/render/PrintRenderer.java index 35630d628..44b0a211d 100644 --- a/src/java/org/apache/fop/render/PrintRenderer.java +++ b/src/java/org/apache/fop/render/PrintRenderer.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,19 +20,23 @@ package org.apache.fop.render; // FOP +import java.awt.Color; +import java.awt.geom.Rectangle2D; +import java.util.List; +import java.util.Map; + +import org.w3c.dom.Document; + import org.apache.fop.area.Area; import org.apache.fop.area.Trait; +import org.apache.fop.fonts.CustomFontCollection; import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontCollection; import org.apache.fop.fonts.FontInfo; +import org.apache.fop.fonts.FontManager; import org.apache.fop.fonts.FontResolver; import org.apache.fop.fonts.FontTriplet; -import org.w3c.dom.Document; - -// Java -import java.awt.Color; -import java.awt.geom.Rectangle2D; -import java.util.List; -import java.util.Map; +import org.apache.fop.fonts.base14.Base14FontCollection; /** Abstract base class of "Print" type renderers. */ public abstract class PrintRenderer extends AbstractRenderer { @@ -79,7 +83,12 @@ public abstract class PrintRenderer extends AbstractRenderer { */ public void setupFontInfo(FontInfo inFontInfo) { this.fontInfo = inFontInfo; - userAgent.getFactory().getFontManager().setupRenderer(this); + FontManager fontManager = userAgent.getFactory().getFontManager(); + FontCollection[] fontCollections = new FontCollection[] { + new Base14FontCollection(fontManager.isBase14KerningEnabled()), + new CustomFontCollection(getFontResolver(), getFontList()) + }; + fontManager.setup(getFontInfo(), fontCollections); } /** diff --git a/src/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java b/src/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java index 843ab8413..26d64af74 100644 --- a/src/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java +++ b/src/java/org/apache/fop/render/java2d/ConfiguredFontCollection.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -35,7 +35,6 @@ import org.apache.fop.fonts.FontManager; import org.apache.fop.fonts.FontResolver; import org.apache.fop.fonts.FontTriplet; import org.apache.fop.fonts.LazyFont; -import org.apache.fop.render.PrintRenderer; /** * A java2d configured font collection @@ -44,37 +43,36 @@ public class ConfiguredFontCollection implements FontCollection { private static Log log = LogFactory.getLog(ConfiguredFontCollection.class); - private PrintRenderer renderer = null; + private FontResolver fontResolver; + private List/**/ embedFontInfoList; /** * Main constructor - * - * @param renderer a print renderer + * @param fontResolver a font resolver + * @param customFonts the list of custom fonts */ - public ConfiguredFontCollection(PrintRenderer renderer) { - this.renderer = renderer; + public ConfiguredFontCollection(FontResolver fontResolver, + List/**/ customFonts) { + this.fontResolver = fontResolver; + if (this.fontResolver == null) { + //Ensure that we have minimal font resolution capabilities + this.fontResolver = FontManager.createMinimalFontResolver(); + } + this.embedFontInfoList = customFonts; } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ public int setup(int start, FontInfo fontInfo) { - List/**/ fontList = renderer.getFontList(); - FontResolver resolver = renderer.getFontResolver(); int num = start; - if (fontList == null || fontList.size() < 1) { + if (embedFontInfoList == null || embedFontInfoList.size() < 1) { log.debug("No user configured fonts found."); return num; } - if (resolver == null) { - // Ensure that we have minimal font resolution capabilities - resolver = FontManager.createMinimalFontResolver(); - } String internalName = null; - for (int i = 0; i < fontList.size(); i++) { + for (int i = 0; i < embedFontInfoList.size(); i++) { - EmbedFontInfo configFontInfo = (EmbedFontInfo) fontList.get(i); + EmbedFontInfo configFontInfo = (EmbedFontInfo) embedFontInfoList.get(i); String fontFile = configFontInfo.getEmbedFile(); internalName = "F" + num; num++; @@ -84,11 +82,12 @@ public class ConfiguredFontCollection implements FontCollection { // If the user specified an XML-based metrics file, we'll use it // Otherwise, calculate metrics directly from the font file. if (metricsUrl != null) { - LazyFont fontMetrics = new LazyFont(configFontInfo, resolver); - Source fontSource = resolver.resolve(configFontInfo.getEmbedFile()); + LazyFont fontMetrics = new LazyFont(configFontInfo, fontResolver); + Source fontSource = fontResolver.resolve(configFontInfo.getEmbedFile()); font = new CustomFontMetricsMapper(fontMetrics, fontSource); } else { - CustomFont fontMetrics = FontLoader.loadFont(fontFile, null, true, resolver); + CustomFont fontMetrics = FontLoader.loadFont( + fontFile, null, true, fontResolver); font = new CustomFontMetricsMapper(fontMetrics); } diff --git a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java index ed6f29ab9..1dd353b3b 100644 --- a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java +++ b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java @@ -69,6 +69,7 @@ import org.apache.fop.datatypes.URISpecification; import org.apache.fop.events.ResourceEventProducer; import org.apache.fop.fo.Constants; import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontCollection; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.Typeface; import org.apache.fop.render.AbstractPathOrientedRenderer; @@ -176,7 +177,13 @@ public abstract class Java2DRenderer extends AbstractPathOrientedRenderer implem graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); - userAgent.getFactory().getFontManager().setupRenderer(this, graphics2D); + FontCollection[] fontCollections = new FontCollection[] { + new Base14FontCollection(graphics2D), + new InstalledFontCollection(graphics2D), + new ConfiguredFontCollection(getFontResolver(), getFontList()) + }; + userAgent.getFactory().getFontManager().setup( + getFontInfo(), fontCollections); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/render/pcl/PCLRenderer.java b/src/java/org/apache/fop/render/pcl/PCLRenderer.java index 7ab46c24e..11366f985 100644 --- a/src/java/org/apache/fop/render/pcl/PCLRenderer.java +++ b/src/java/org/apache/fop/render/pcl/PCLRenderer.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -82,6 +82,7 @@ import org.apache.fop.datatypes.URISpecification; import org.apache.fop.events.ResourceEventProducer; import org.apache.fop.fo.extensions.ExtensionElementMapping; import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontCollection; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontMetrics; import org.apache.fop.render.Graphics2DAdapter; @@ -89,7 +90,10 @@ import org.apache.fop.render.PrintRenderer; import org.apache.fop.render.RendererContext; import org.apache.fop.render.RendererContextConstants; import org.apache.fop.render.RendererEventProducer; +import org.apache.fop.render.java2d.Base14FontCollection; +import org.apache.fop.render.java2d.ConfiguredFontCollection; import org.apache.fop.render.java2d.FontMetricsMapper; +import org.apache.fop.render.java2d.InstalledFontCollection; import org.apache.fop.render.java2d.Java2DRenderer; import org.apache.fop.render.pcl.extensions.PCLElementMapping; import org.apache.fop.traits.BorderProps; @@ -207,7 +211,13 @@ public class PCLRenderer extends PrintRenderer { graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); - userAgent.getFactory().getFontManager().setupRenderer(this, graphics2D); + FontCollection[] fontCollections = new FontCollection[] { + new Base14FontCollection(graphics2D), + new InstalledFontCollection(graphics2D), + new ConfiguredFontCollection(getFontResolver(), getFontList()) + }; + userAgent.getFactory().getFontManager().setup( + getFontInfo(), fontCollections); } /** @@ -1035,7 +1045,7 @@ public class PCLRenderer extends PrintRenderer { //So there's some optimization potential but not otherwise PCLRenderer is a little //difficult to derive from AbstractPathOrientedRenderer. Maybe an additional layer //between PrintRenderer and AbstractPathOrientedRenderer is necessary. - + // save position and offset int saveIP = currentIPPosition; int saveBP = currentBPPosition; @@ -1045,7 +1055,7 @@ public class PCLRenderer extends PrintRenderer { at.translate(currentIPPosition, currentBPPosition); at.translate(block.getXOffset(), block.getYOffset()); at.translate(0, block.getSpaceBefore()); - + if (!at.isIdentity()) { saveGraphicsState(); concatenateTransformationMatrix(mptToPt(at)); @@ -1063,12 +1073,12 @@ public class PCLRenderer extends PrintRenderer { if (!at.isIdentity()) { restoreGraphicsState(); } - + // stacked and relative blocks effect stacking currentIPPosition = saveIP; currentBPPosition = saveBP; } - + /** {@inheritDoc} */ protected void renderFlow(NormalFlow flow) { //TODO This is the same code as in AbstractPathOrientedRenderer @@ -1083,7 +1093,7 @@ public class PCLRenderer extends PrintRenderer { //Establish a new coordinate system AffineTransform at = new AffineTransform(); at.translate(currentIPPosition, currentBPPosition); - + if (!at.isIdentity()) { saveGraphicsState(); concatenateTransformationMatrix(mptToPt(at)); @@ -1092,16 +1102,16 @@ public class PCLRenderer extends PrintRenderer { currentIPPosition = 0; currentBPPosition = 0; super.renderFlow(flow); - + if (!at.isIdentity()) { restoreGraphicsState(); } - + // stacked and relative blocks effect stacking currentIPPosition = saveIP; currentBPPosition = saveBP; } - + /** * Concatenates the current transformation matrix with the given one, therefore establishing * a new coordinate system. @@ -1225,7 +1235,7 @@ public class PCLRenderer extends PrintRenderer { renderDocument(doc, ns, pos, fo.getForeignAttributes()); } - /** + /** * Common method to render the background and borders for any inline area. * The all borders and padding are drawn outside the specified area. * @param area the inline area for which the background, border and padding is to be diff --git a/test/java/org/apache/fop/config/FontsSubstitutionTestCase.java b/test/java/org/apache/fop/config/FontsSubstitutionTestCase.java index 6f286a3fb..725f0d4d7 100644 --- a/test/java/org/apache/fop/config/FontsSubstitutionTestCase.java +++ b/test/java/org/apache/fop/config/FontsSubstitutionTestCase.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,10 +23,13 @@ import java.io.File; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.MimeConstants; +import org.apache.fop.fonts.CustomFontCollection; import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontCollection; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontManager; import org.apache.fop.fonts.FontTriplet; +import org.apache.fop.fonts.base14.Base14FontCollection; import org.apache.fop.render.PrintRenderer; /** @@ -53,7 +56,11 @@ public class FontsSubstitutionTestCase extends FontInfo fontInfo = new FontInfo(); renderer.setupFontInfo(fontInfo); FontManager fontManager = ua.getFactory().getFontManager(); - fontManager.setupRenderer(renderer); + FontCollection[] fontCollections = new FontCollection[] { + new Base14FontCollection(fontManager.isBase14KerningEnabled()), + new CustomFontCollection(renderer.getFontResolver(), renderer.getFontList()) + }; + fontManager.setup(fontInfo, fontCollections); FontTriplet triplet = new FontTriplet("Times", "italic", Font.WEIGHT_NORMAL); String internalFontKey = fontInfo.getInternalFontKey(triplet); -- 2.39.5