From 81812e2f472d4dd50714ad0a36a981c46705130e Mon Sep 17 00:00:00 2001 From: Keiron Liddle Date: Tue, 23 Jul 2002 10:57:57 +0000 Subject: [PATCH] spearated GraphicsConfiguration classes handles the text painting better using the stroke painter as a proxy for bounds etc. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@195020 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/svg/PDFDocumentGraphics2D.java | 15 +- src/org/apache/fop/svg/PDFGraphics2D.java | 276 ++++++------------ .../fop/svg/PDFGraphicsConfiguration.java | 113 +++++++ src/org/apache/fop/svg/PDFGraphicsDevice.java | 72 +++++ .../apache/fop/svg/PDFTextElementBridge.java | 6 +- src/org/apache/fop/svg/PDFTextPainter.java | 89 +++--- src/org/apache/fop/svg/PDFTranscoder.java | 6 +- 7 files changed, 336 insertions(+), 241 deletions(-) create mode 100644 src/org/apache/fop/svg/PDFGraphicsConfiguration.java create mode 100644 src/org/apache/fop/svg/PDFGraphicsDevice.java diff --git a/src/org/apache/fop/svg/PDFDocumentGraphics2D.java b/src/org/apache/fop/svg/PDFDocumentGraphics2D.java index 3894d1140..2b67d0f83 100644 --- a/src/org/apache/fop/svg/PDFDocumentGraphics2D.java +++ b/src/org/apache/fop/svg/PDFDocumentGraphics2D.java @@ -11,7 +11,6 @@ import org.apache.fop.pdf.*; import org.apache.fop.fonts.*; import org.apache.fop.render.pdf.FontSetup; import org.apache.fop.layout.*; -import org.apache.fop.apps.FOPException; import java.awt.Graphics; import java.awt.Font; @@ -44,8 +43,6 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { int width; int height; - FontInfo fontInfo = null; - /** * Create a new PDFDocumentGraphics2D. * This is used to create a new pdf document, the height, @@ -63,10 +60,8 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { if(!textAsShapes) { fontInfo = new FontInfo(); FontSetup.setup(fontInfo); - try { - fontState = new FontState(fontInfo, "Helvetica", "normal", - "normal", 12, 0); - } catch (FOPException e) {} + //FontState fontState = new FontState("Helvetica", "normal", + // FontInfo.NORMAL, 12, 0); } standalone = true; @@ -117,8 +112,8 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { setupDocument(stream, width, height); } - public FontState getFontState() { - return fontState; + public FontInfo getFontInfo() { + return fontInfo; } public PDFDocument getPDFDocument() { @@ -160,6 +155,8 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { * This will then write the results to the output stream. */ public void finish() throws IOException { + // restorePDFState(); + pdfStream.add(getString()); this.pdfDoc.addStream(pdfStream); currentPage.setContents(pdfStream); diff --git a/src/org/apache/fop/svg/PDFGraphics2D.java b/src/org/apache/fop/svg/PDFGraphics2D.java index b9f20737f..ec5bcb327 100644 --- a/src/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/org/apache/fop/svg/PDFGraphics2D.java @@ -65,8 +65,9 @@ public class PDFGraphics2D extends AbstractGraphics2D { * the current state of the pdf graphics */ PDFState graphicsState; + int baseLevel = 0; - protected FontState fontState; + protected FontInfo fontInfo; protected FontState ovFontState = null; /** @@ -107,7 +108,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { * This is used to create a Graphics object for use inside an already * existing document. */ - public PDFGraphics2D(boolean textAsShapes, FontState fs, PDFDocument doc, + public PDFGraphics2D(boolean textAsShapes, FontInfo fi, PDFDocument doc, PDFResourceContext page, String pref, String font, float size, int xpos, int ypos) { super(textAsShapes); pdfDoc = doc; @@ -116,7 +117,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { currentFontSize = size; currentYPosition = ypos; currentXPosition = xpos; - fontState = fs; + fontInfo = fi; pageRef = pref; graphicsState = new PDFState(); } @@ -127,6 +128,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { public void setPDFState(PDFState state) { graphicsState = state; + baseLevel = graphicsState.getStackLevel(); } public void setOutputStream(OutputStream os) { @@ -162,6 +164,13 @@ public class PDFGraphics2D extends AbstractGraphics2D { return new PDFGraphics2D(this); } + public void restorePDFState() { + for(int count = graphicsState.getStackLevel(); count > baseLevel; count--) { + currentStream.write("Q\n"); + } + graphicsState.restoreLevel(baseLevel); + } + /** * This is a pdf specific method used to add a link to the * pdf document. @@ -192,9 +201,11 @@ public class PDFGraphics2D extends AbstractGraphics2D { currentStream.write("q\n"); Shape imclip = getClip(); writeClip(imclip); - currentStream.write("" + matrix[0] + " " + matrix[1] + " " - + matrix[2] + " " + matrix[3] + " " - + matrix[4] + " " + matrix[5] + " cm\n"); + if(!at.isIdentity()) { + currentStream.write("" + matrix[0] + " " + matrix[1] + " " + + matrix[2] + " " + matrix[3] + " " + + matrix[4] + " " + matrix[5] + " cm\n"); + } currentStream.write("" + width + " 0 0 " + (-height) + " " @@ -365,9 +376,11 @@ public class PDFGraphics2D extends AbstractGraphics2D { currentStream.write("q\n"); Shape imclip = getClip(); writeClip(imclip); - currentStream.write("" + matrix[0] + " " + matrix[1] + " " - + matrix[2] + " " + matrix[3] + " " - + matrix[4] + " " + matrix[5] + " cm\n"); + if(!at.isIdentity()) { + currentStream.write("" + matrix[0] + " " + matrix[1] + " " + + matrix[2] + " " + matrix[3] + " " + + matrix[4] + " " + matrix[5] + " cm\n"); + } currentStream.write("" + width + " 0 0 " + (-height) + " " + x + " " + (y + height) + " cm\n" + "/Im" + imageInfo.getXNumber() + " Do\nQ\n"); @@ -448,7 +461,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { public void dispose() { // System.out.println("dispose"); pdfDoc = null; - fontState = null; + fontInfo = null; currentStream = null; currentFontName = null; } @@ -558,6 +571,43 @@ public class PDFGraphics2D extends AbstractGraphics2D { } } +/* + // in theory we could set the clip using these methods + // it doesn't seem to improve the file sizes much + // and makes everything more complicated + + Shape lastClip = null; + + public void clip(Shape cl) { + super.clip(cl); + Shape newClip = getClip(); + if(newClip == null || lastClip == null || !(new Area(newClip).equals(new Area(lastClip)))) { + graphicsState.setClip(newClip); + writeClip(newClip); + } + + lastClip = newClip; + } + + public void setClip(Shape cl) { + super.setClip(cl); + Shape newClip = getClip(); + if(newClip == null || lastClip == null || !(new Area(newClip).equals(new Area(lastClip)))) { + for(int count = graphicsState.getStackLevel(); count > baseLevel; count--) { + currentStream.write("Q\n"); + } + graphicsState.restoreLevel(baseLevel); + currentStream.write("q\n"); + graphicsState.push(); + if(newClip != null) { + graphicsState.setClip(newClip); + } + writeClip(newClip); + } + + lastClip = newClip; + } +*/ protected void writeClip(Shape s) { if (s == null) { return; @@ -742,16 +792,10 @@ public class PDFGraphics2D extends AbstractGraphics2D { FontInfo fi = new FontInfo(); FontSetup.setup(fi); - FontState fs = null; - try { - fs = new FontState(fi, "sans-serif", "normal", - "normal", 1, 0); - } catch (org.apache.fop.apps.FOPException fope) { - fope.printStackTrace(); - } + PDFResources res = pdfDoc.makeResources(); PDFResourceContext context = new PDFResourceContext(0, pdfDoc, res); - PDFGraphics2D pattGraphic = new PDFGraphics2D(textAsShapes, fs, + PDFGraphics2D pattGraphic = new PDFGraphics2D(textAsShapes, fi, pdfDoc, context, pageRef, currentFontName, currentFontSize, currentYPosition, currentXPosition); @@ -950,6 +994,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { public void drawString(String s, float x, float y) { // System.out.println("drawString(String)"); + FontState fontState; if(ovFontState == null) { Font gFont = getFont(); String n = gFont.getFamily(); @@ -958,13 +1003,10 @@ public class PDFGraphics2D extends AbstractGraphics2D { } int siz = gFont.getSize(); String style = gFont.isItalic() ? "italic" : "normal"; - String weight = gFont.isBold() ? "bold" : "normal"; - try { - fontState = new FontState(fontState.getFontInfo(), n, style, - weight, siz * 1000, 0); - } catch (org.apache.fop.apps.FOPException fope) { - fope.printStackTrace(); - } + int weight = gFont.isBold() ? FontInfo.BOLD : FontInfo.NORMAL; + String fname = fontInfo.fontLookup(n, style, weight); + FontMetric metrics = fontInfo.getMetricsFor(fname); + fontState = new FontState(fname, metrics, siz * 1000); } else { fontState = ovFontState; ovFontState = null; @@ -990,12 +1032,9 @@ public class PDFGraphics2D extends AbstractGraphics2D { applyColor(c, true); int salpha = c.getAlpha(); - c = getBackground(); - applyColor(c, false); - if(salpha != 255/* || c.getAlpha() != 255*/) { + if(salpha != 255) { HashMap vals = new HashMap(); vals.put(PDFGState.ca, new Float(salpha / 255f)); - //vals.put(PDFGState.CA, new Float(c.getAlpha() / 255f)); PDFGState gstate = pdfDoc.makeGState(vals, graphicsState.getGState()); resourceContext.addGState(gstate); currentStream.write("/" + gstate.getName() + " gs\n"); @@ -1014,7 +1053,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { // This assumes that *all* CIDFonts use a /ToUnicode mapping boolean useMultiByte = false; org.apache.fop.render.pdf.Font f = - (org.apache.fop.render.pdf.Font)fontState.getFontInfo().getFonts().get(name); + (org.apache.fop.render.pdf.Font)fontInfo.getFonts().get(name); if (f instanceof LazyFont){ if(((LazyFont) f).getRealFont() instanceof CIDFont){ useMultiByte = true; @@ -1149,15 +1188,34 @@ public class PDFGraphics2D extends AbstractGraphics2D { float y) { System.err.println("drawString(AttributedCharacterIterator)"); + FontState fontState = null; + Shape imclip = getClip(); writeClip(imclip); Color c = getColor(); applyColor(c, true); - c = getBackground(); - applyColor(c, false); + + boolean fill = true; + boolean stroke = false; + if(true) { + Stroke currentStroke = getStroke(); + stroke = true; + applyStroke(currentStroke); + applyColor(c, false); + } currentStream.write("BT\n"); + // set text rendering mode: + // 0 - fill, 1 - stroke, 2 - fill then stroke + int textr = 0; + if(fill && stroke) { + textr = 2; + } else if(stroke) { + textr = 1; + } + currentStream.write(textr + " Tr\n"); + AffineTransform trans = getTransform(); trans.translate(x, y); double[] vals = new double[6]; @@ -1316,160 +1374,6 @@ public class PDFGraphics2D extends AbstractGraphics2D { return new PDFGraphicsConfiguration(); } - /** - * Our implementation of the class that returns information about - * roughly what we can handle and want to see (alpha for example). - */ - static class PDFGraphicsConfiguration extends GraphicsConfiguration { - // We use this to get a good colormodel.. - static BufferedImage BIWithAlpha = new BufferedImage(1, 1, - BufferedImage.TYPE_INT_ARGB); - // We use this to get a good colormodel.. - static BufferedImage BIWithOutAlpha = new BufferedImage(1, 1, - BufferedImage.TYPE_INT_RGB); - - /** - * Construct a buffered image with an alpha channel, unless - * transparencty is OPAQUE (no alpha at all). - */ - public BufferedImage createCompatibleImage(int width, int height, - int transparency) { - if (transparency == Transparency.OPAQUE) - return new BufferedImage(width, height, - BufferedImage.TYPE_INT_RGB); - else - return new BufferedImage(width, height, - BufferedImage.TYPE_INT_ARGB); - } - - /** - * Construct a buffered image with an alpha channel. - */ - public BufferedImage createCompatibleImage(int width, int height) { - return new BufferedImage(width, height, - BufferedImage.TYPE_INT_ARGB); - } - - /** - * FIXX ME: This should return the page bounds in Pts, - * I couldn't figure out how to get this for the current - * page from the PDFDocument (this still works for now, - * but it should be fixed...). - */ - public Rectangle getBounds() { -System.out.println("getting getBounds"); - return null; - } - - /** - * Return a good default color model for this 'device'. - */ - public ColorModel getColorModel() { - return BIWithAlpha.getColorModel(); - } - - /** - * Return a good color model given transparency - */ - public ColorModel getColorModel(int transparency) { - if (transparency == Transparency.OPAQUE) - return BIWithOutAlpha.getColorModel(); - else - return BIWithAlpha.getColorModel(); - } - - /** - * The default transform (1:1). - */ - public AffineTransform getDefaultTransform() { -System.out.println("getting getDefaultTransform"); - return new AffineTransform(); - } - - /** - * The normalizing transform (1:1) (since we currently - * render images at 72dpi, which we might want to change - * in the future). - */ - public AffineTransform getNormalizingTransform() { -System.out.println("getting getNormalizingTransform"); - return new AffineTransform(2, 0, 0, 2, 0, 0); - } - - /** - * Return our dummy instance of GraphicsDevice - */ - public GraphicsDevice getDevice() { - return new PDFGraphicsDevice(this); - } - -/* -// for jdk1.4 -public java.awt.image.VolatileImage createCompatibleVolatileImage(int width, int height) { -return null; -} -*/ - } - - /** - * This implements the GraphicsDevice interface as appropriate for - * a PDFGraphics2D. This is quite simple since we only have one - * GraphicsConfiguration for now (this might change in the future - * I suppose). - */ - static class PDFGraphicsDevice extends GraphicsDevice { - - /** - * The Graphics Config that created us... - */ - GraphicsConfiguration gc; - - /** - * @param The gc we should reference - */ - PDFGraphicsDevice(PDFGraphicsConfiguration gc) { - this.gc = gc; - } - - /** - * Ignore template and return the only config we have - */ - public GraphicsConfiguration getBestConfiguration(GraphicsConfigTemplate gct) { - return gc; - } - - /** - * Return an array of our one GraphicsConfig - */ - public GraphicsConfiguration[] getConfigurations() { - return new GraphicsConfiguration[] { - gc - }; - } - - /** - * Return out sole GraphicsConfig. - */ - public GraphicsConfiguration getDefaultConfiguration() { - return gc; - } - - /** - * Generate an IdString.. - */ - public String getIDstring() { - return toString(); - } - - /** - * Let the caller know that we are "a printer" - */ - public int getType() { - return GraphicsDevice.TYPE_PRINTER; - } - - } - /** * Used to create proper font metrics */ diff --git a/src/org/apache/fop/svg/PDFGraphicsConfiguration.java b/src/org/apache/fop/svg/PDFGraphicsConfiguration.java new file mode 100644 index 000000000..1392101ed --- /dev/null +++ b/src/org/apache/fop/svg/PDFGraphicsConfiguration.java @@ -0,0 +1,113 @@ +/* + * $Id$ + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources. + */ + +package org.apache.fop.svg; + +import java.awt.*; +import java.awt.Font; +import java.awt.Image; +import java.awt.image.*; +import java.awt.font.*; +import java.awt.geom.*; +import java.awt.color.ColorSpace; +import java.awt.image.renderable.*; + +/** + * Our implementation of the class that returns information about + * roughly what we can handle and want to see (alpha for example). + */ +class PDFGraphicsConfiguration extends GraphicsConfiguration { + // We use this to get a good colormodel.. + static BufferedImage BIWithAlpha = + new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); + // We use this to get a good colormodel.. + static BufferedImage BIWithOutAlpha = + new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); + + /** + * Construct a buffered image with an alpha channel, unless + * transparencty is OPAQUE (no alpha at all). + */ + public BufferedImage createCompatibleImage(int width, int height, + int transparency) { + if (transparency == Transparency.OPAQUE) + return new BufferedImage(width, height, + BufferedImage.TYPE_INT_RGB); + else + return new BufferedImage(width, height, + BufferedImage.TYPE_INT_ARGB); + } + + /** + * Construct a buffered image with an alpha channel. + */ + public BufferedImage createCompatibleImage(int width, int height) { + return new BufferedImage(width, height, + BufferedImage.TYPE_INT_ARGB); + } + + /** + * FIXX ME: This should return the page bounds in Pts, + * I couldn't figure out how to get this for the current + * page from the PDFDocument (this still works for now, + * but it should be fixed...). + */ + public Rectangle getBounds() { + System.out.println("getting getBounds"); + return null; + } + + /** + * Return a good default color model for this 'device'. + */ + public ColorModel getColorModel() { + return BIWithAlpha.getColorModel(); + } + + /** + * Return a good color model given transparency + */ + public ColorModel getColorModel(int transparency) { + if (transparency == Transparency.OPAQUE) + return BIWithOutAlpha.getColorModel(); + else + return BIWithAlpha.getColorModel(); + } + + /** + * The default transform (1:1). + */ + public AffineTransform getDefaultTransform() { + System.out.println("getting getDefaultTransform"); + return new AffineTransform(); + } + + /** + * The normalizing transform (1:1) (since we currently + * render images at 72dpi, which we might want to change + * in the future). + */ + public AffineTransform getNormalizingTransform() { + System.out.println("getting getNormalizingTransform"); + return new AffineTransform(2, 0, 0, 2, 0, 0); + } + + /** + * Return our dummy instance of GraphicsDevice + */ + public GraphicsDevice getDevice() { + return new PDFGraphicsDevice(this); + } + + /* + // for jdk1.4 + public java.awt.image.VolatileImage createCompatibleVolatileImage(int width, int height) { + return null; + } + */ +} + diff --git a/src/org/apache/fop/svg/PDFGraphicsDevice.java b/src/org/apache/fop/svg/PDFGraphicsDevice.java new file mode 100644 index 000000000..ff2fa430f --- /dev/null +++ b/src/org/apache/fop/svg/PDFGraphicsDevice.java @@ -0,0 +1,72 @@ +/* + * $Id$ + * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * For details on use and redistribution please refer to the + * LICENSE file included with these sources. + */ + +package org.apache.fop.svg; + +import java.awt.*; +import java.awt.Image; +import java.awt.image.*; +import java.awt.image.renderable.*; + +/** + * This implements the GraphicsDevice interface as appropriate for + * a PDFGraphics2D. This is quite simple since we only have one + * GraphicsConfiguration for now (this might change in the future + * I suppose). + */ +class PDFGraphicsDevice extends GraphicsDevice { + + /** + * The Graphics Config that created us... + */ + GraphicsConfiguration gc; + + /** + * @param The gc we should reference + */ + PDFGraphicsDevice(PDFGraphicsConfiguration gc) { + this.gc = gc; + } + + /** + * Ignore template and return the only config we have + */ + public GraphicsConfiguration getBestConfiguration( + GraphicsConfigTemplate gct) { + return gc; + } + + /** + * Return an array of our one GraphicsConfig + */ + public GraphicsConfiguration[] getConfigurations() { + return new GraphicsConfiguration[]{ gc }; + } + + /** + * Return out sole GraphicsConfig. + */ + public GraphicsConfiguration getDefaultConfiguration() { + return gc; + } + + /** + * Generate an IdString.. + */ + public String getIDstring() { + return toString(); + } + + /** + * Let the caller know that we are "a printer" + */ + public int getType() { + return GraphicsDevice.TYPE_PRINTER; + } + +} + diff --git a/src/org/apache/fop/svg/PDFTextElementBridge.java b/src/org/apache/fop/svg/PDFTextElementBridge.java index b44c9951c..e01026472 100644 --- a/src/org/apache/fop/svg/PDFTextElementBridge.java +++ b/src/org/apache/fop/svg/PDFTextElementBridge.java @@ -13,7 +13,7 @@ import org.apache.batik.bridge.*; import org.apache.batik.gvt.CompositeGraphicsNode; import org.apache.batik.gvt.GraphicsNode; -import org.apache.fop.layout.FontState; +import org.apache.fop.layout.FontInfo; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -33,8 +33,8 @@ public class PDFTextElementBridge extends SVGTextElementBridge { /** * Constructs a new bridge for the <text> element. */ - public PDFTextElementBridge(FontState fs) { - pdfTextPainter = new PDFTextPainter(fs); + public PDFTextElementBridge(FontInfo fi) { + pdfTextPainter = new PDFTextPainter(fi); } public GraphicsNode createGraphicsNode(BridgeContext ctx, Element e) { diff --git a/src/org/apache/fop/svg/PDFTextPainter.java b/src/org/apache/fop/svg/PDFTextPainter.java index 61173a569..6107cf208 100644 --- a/src/org/apache/fop/svg/PDFTextPainter.java +++ b/src/org/apache/fop/svg/PDFTextPainter.java @@ -32,8 +32,6 @@ import org.apache.batik.gvt.font.*; import org.apache.fop.layout.*; -// TODO properly calculate bounds for links etc. - /** * Renders the attributed character iterator of a TextNode. * @@ -41,10 +39,12 @@ import org.apache.fop.layout.*; * @version $Id$ */ public class PDFTextPainter implements TextPainter { - FontState fontState; + FontInfo fontInfo; + protected final static TextPainter proxyPainter + = StrokingTextPainter.getInstance(); - public PDFTextPainter(FontState fs) { - fontState = fs; + public PDFTextPainter(FontInfo fi) { + fontInfo = fi; } /** @@ -75,6 +75,7 @@ public class PDFTextPainter implements TextPainter { Vector gvtFonts = (Vector)aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.GVT_FONT_FAMILIES); Paint forg = (Paint)aci.getAttribute(TextAttribute.FOREGROUND); + Paint strokePaint = (Paint)aci.getAttribute(GVTAttributedCharacterIterator.TextAttribute.STROKE_PAINT); Float size = (Float)aci.getAttribute(TextAttribute.SIZE); if(size == null) { return; @@ -89,71 +90,81 @@ public class PDFTextPainter implements TextPainter { Float posture = (Float)aci.getAttribute(TextAttribute.POSTURE); Float taWeight = (Float)aci.getAttribute(TextAttribute.WEIGHT); + boolean useStrokePainter = false; + if (forg instanceof Color) { - g2d.setColor((Color)forg); + Color col = (Color)forg; + if(col.getAlpha() != 255) { + useStrokePainter = true; + } + g2d.setColor(col); } g2d.setPaint(forg); g2d.setStroke(stroke); + if(strokePaint != null) { + // need to draw using AttributedCharacterIterator + useStrokePainter = true; + } + + if(useStrokePainter) { + proxyPainter.paint(node, g2d); + return; + } + String style = ((posture != null) && (posture.floatValue() > 0.0)) ? "italic" : "normal"; - String weight = ((taWeight != null) && (taWeight.floatValue() > 1.0)) - ? "bold" : "normal"; + int weight = ((taWeight != null) && (taWeight.floatValue() > 1.0)) + ? FontInfo.BOLD : FontInfo.NORMAL; - FontInfo fi = fontState.getFontInfo(); + FontState fontState = null; + FontInfo fi = fontInfo; boolean found = false; + String fontFamily = null; if (gvtFonts != null) { for (Enumeration e = gvtFonts.elements(); e.hasMoreElements(); ) { GVTFontFamily fam = (GVTFontFamily)e.nextElement(); - String name = fam.getFamilyName(); - if (fi.hasFont(name, weight, style)) { - try { - int fsize = (int)(size.floatValue() * 1000); - fontState = new FontState(fontState.getFontInfo(), - name, style, weight, - fsize, 0); - } catch (org.apache.fop.apps.FOPException fope) { - fope.printStackTrace(); - } + fontFamily = fam.getFamilyName(); + if (fi.hasFont(fontFamily, style, weight)) { + String fname = fontInfo.fontLookup(fontFamily, style, weight); + FontMetric metrics = fontInfo.getMetricsFor(fname); + int fsize = (int)(size.floatValue() * 1000); + fontState = new FontState(fname, metrics, fsize); found = true; break; } } } if (!found) { - try { - int fsize = (int)(size.floatValue() * 1000); - fontState = new FontState(fontState.getFontInfo(), "any", - style, weight, fsize, 0); - } catch (org.apache.fop.apps.FOPException fope) { - fope.printStackTrace(); - } + String fname = fontInfo.fontLookup("any", style, FontInfo.NORMAL); + FontMetric metrics = fontInfo.getMetricsFor(fname); + int fsize = (int)(size.floatValue() * 1000); + fontState = new FontState(fname, metrics, fsize); } else { if(g2d instanceof PDFGraphics2D) { ((PDFGraphics2D)g2d).setOverrideFontState(fontState); } } int fStyle = Font.PLAIN; - if (fontState.getFontWeight().equals("bold")) { - if (fontState.getFontStyle().equals("italic")) { + if (weight == FontInfo.BOLD) { + if (style.equals("italic")) { fStyle = Font.BOLD | Font.ITALIC; } else { fStyle = Font.BOLD; } } else { - if (fontState.getFontStyle().equals("italic")) { + if (style.equals("italic")) { fStyle = Font.ITALIC; } else { fStyle = Font.PLAIN; } } - Font font = new Font(fontState.getFontFamily(), fStyle, + Font font = new Font(fontFamily, fStyle, (int)(fontState.getFontSize() / 1000)); g2d.setFont(font); - - float advance = getStringWidth(txt); + float advance = getStringWidth(txt, fontState); float tx = 0; if (anchor != null) { switch (anchor.getType()) { @@ -167,7 +178,7 @@ public class PDFTextPainter implements TextPainter { g2d.drawString(txt, (float)(loc.getX() + tx), (float)(loc.getY())); } - public float getStringWidth(String str) { + public float getStringWidth(String str, FontState fontState) { float wordWidth = 0; float whitespaceWidth = fontState.width(fontState.mapChar(' ')); @@ -231,29 +242,27 @@ public class PDFTextPainter implements TextPainter { public Shape getShape(TextNode node) { System.out.println("PDFText getShape"); - return null; + return proxyPainter.getShape(node); } public Shape getDecoratedShape(TextNode node) { //System.out.println("PDFText getDecoratedShape"); - return new Rectangle(1, 1); + return proxyPainter.getDecoratedShape(node); } public Rectangle2D getBounds(TextNode node) { //System.out.println("PDFText getBounds"); - Rectangle2D bounds = new Rectangle2D.Float(0, 0, 100, 12); - return bounds; + return proxyPainter.getBounds(node); } public Rectangle2D getDecoratedBounds(TextNode node) { System.out.println("PDFText getDecoratedBounds"); - return null; + return proxyPainter.getDecoratedBounds(node); } public Rectangle2D getPaintedBounds(TextNode node) { // System.out.println("PDFText getPaintedBounds"); - Point2D loc = node.getLocation(); - return new Rectangle2D.Double(loc.getX(), loc.getY(), 100, 12); + return proxyPainter.getPaintedBounds(node); } } diff --git a/src/org/apache/fop/svg/PDFTranscoder.java b/src/org/apache/fop/svg/PDFTranscoder.java index 687267987..ecd19c1a5 100644 --- a/src/org/apache/fop/svg/PDFTranscoder.java +++ b/src/org/apache/fop/svg/PDFTranscoder.java @@ -88,7 +88,7 @@ import org.w3c.dom.svg.*; import org.w3c.dom.css.*; import org.w3c.dom.svg.SVGLength; -import org.apache.fop.svg.*; +//import org.apache.fop.layout.FontInfo; import org.apache.fop.pdf.*; import org.w3c.dom.DOMException; @@ -180,7 +180,7 @@ public class PDFTranscoder extends XMLAbstractTranscoder { textPainter = new StrokingTextPainter(); ctx.setTextPainter(textPainter); - PDFTextElementBridge pdfTextElementBridge = new PDFTextElementBridge(graphics.getFontState()); + PDFTextElementBridge pdfTextElementBridge = new PDFTextElementBridge(graphics.getFontInfo()); ctx.putBridge(pdfTextElementBridge); PDFAElementBridge pdfAElementBridge = new PDFAElementBridge(); @@ -276,7 +276,7 @@ public class PDFTranscoder extends XMLAbstractTranscoder { graphics.setSVGDimension(docWidth, docHeight); currentTransform.setTransform(1, 0, 0, -1, 0, height); /*if (!stroke) { - textPainter = new PDFTextPainter(graphics.getFontState()); + textPainter = new PDFTextPainter(graphics.getFontInfo()); ctx.setTextPainter(textPainter); }*/ -- 2.39.5