From: Keiron Liddle Date: Tue, 10 Jul 2001 11:59:24 +0000 (+0000) Subject: should handle colour space a bit better X-Git-Tag: PRE_CODEFORMATTING~36 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=980ee9066893c07cc9cccd6fde4f025b1f219332;p=xmlgraphics-fop.git should handle colour space a bit better writes fonts to resources in PDFDocument cleaned up a bit git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194343 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/org/apache/fop/svg/PDFDocumentGraphics2D.java b/src/org/apache/fop/svg/PDFDocumentGraphics2D.java index f0bc9f9cd..04b4008c6 100644 --- a/src/org/apache/fop/svg/PDFDocumentGraphics2D.java +++ b/src/org/apache/fop/svg/PDFDocumentGraphics2D.java @@ -37,6 +37,8 @@ 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 of the given height @@ -54,7 +56,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { super(textAsShapes); if(!textAsShapes) { - FontInfo fontInfo = new FontInfo(); + fontInfo = new FontInfo(); FontSetup.setup(fontInfo); try { fontState = new FontState(fontInfo, "Helvetica", "normal", "normal", 12, 0); @@ -73,15 +75,9 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { currentFontSize = 0; currentYPosition = 0; currentXPosition = 0; - // fontState = fs; currentStream.write("1 0 0 -1 0 " + height + " cm\n"); - // end part - /* - FontSetup.addToResources(this.pdfDoc, fontInfo); - */ - } /** @@ -123,6 +119,9 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { PDFPage currentPage = this.pdfDoc.makePage(pdfResources, pdfStream, width, height, null); + if(fontInfo != null) { + FontSetup.addToResources(this.pdfDoc, fontInfo); + } this.pdfDoc.output(stream); } diff --git a/src/org/apache/fop/svg/PDFGraphics2D.java b/src/org/apache/fop/svg/PDFGraphics2D.java index 7ab314f57..15ca52780 100644 --- a/src/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/org/apache/fop/svg/PDFGraphics2D.java @@ -163,8 +163,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { final byte[] result = new byte[buf.getWidth() * buf.getHeight() * 3]; - final byte[] mask = - new byte[buf.getWidth() * buf.getHeight()]; + final byte[] mask = new byte[buf.getWidth() * buf.getHeight()]; Raster raster = buf.getData(); DataBuffer bd = raster.getDataBuffer(); @@ -177,10 +176,10 @@ public class PDFGraphics2D extends AbstractGraphics2D { for (int i = 0; i < idata.length; i++) { for (int j = 0; j < idata[i].length; j++) { //mask[maskpos++] = (byte)((idata[i][j] >> 24) & 0xFF); - if(((idata[i][j] >> 24) & 0xFF) != 255) { - result[count++] = (byte)0xFF; - result[count++] = (byte)0xFF; - result[count++] = (byte)0xFF; + if (((idata[i][j] >> 24) & 0xFF) != 255) { + result[count++] = (byte) 0xFF; + result[count++] = (byte) 0xFF; + result[count++] = (byte) 0xFF; } else { result[count++] = (byte)((idata[i][j] >> 16) & 0xFF); @@ -199,17 +198,12 @@ public class PDFGraphics2D extends AbstractGraphics2D { try { FopImage fopimg = new TempImage(width, height, result, mask); int xObjectNum = this.pdfDoc.addImage(fopimg); - /*currentStream.write("q\n" + (((float) width)) + - " 0 0 " + (((float) height)) + " " + - x + " " + - ((float)(y - height)) + " cm\n" + "/Im" + - xObjectNum + " Do\nQ\n");*/ AffineTransform at = getTransform(); double[] matrix = new double[6]; at.getMatrix(matrix); currentStream.write("q\n"); - Shape imclip = getClip(); - writeClip(imclip); + Shape imclip = getClip(); + writeClip(imclip); currentStream.write("" + matrix[0] + " " + matrix[1] + " " + matrix[2] + " " + matrix[3] + " " + matrix[4] + " " + matrix[5] + " cm\n"); @@ -237,8 +231,8 @@ public class PDFGraphics2D extends AbstractGraphics2D { byte[] m_mask; PDFColor transparent = new PDFColor(255, 255, 255); - TempImage(int width, int height, - byte[] result, byte[] mask) throws FopImageException { + TempImage(int width, int height, byte[] result, + byte[] mask) throws FopImageException { this.m_height = height; this.m_width = width; this.m_bitsPerPixel = 8; @@ -279,7 +273,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { public PDFColor getTransparentColor() throws FopImageException { return transparent; } - public byte[] getMask() throws FopImageException { + public byte[] getMask() throws FopImageException { return m_mask; } // get the image bytes, and bytes properties @@ -406,12 +400,11 @@ public class PDFGraphics2D extends AbstractGraphics2D { */ public void draw(Shape s) { //System.out.println("draw(Shape)"); - currentStream.write("q\n"); - Shape imclip = getClip(); - writeClip(imclip); + currentStream.write("q\n"); + Shape imclip = getClip(); + writeClip(imclip); Color c = getColor(); - currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); - currentStream.write(currentColour.getColorSpaceOut(false)); + applyColor(c, false); applyPaint(getPaint(), false); applyStroke(getStroke()); @@ -452,10 +445,13 @@ public class PDFGraphics2D extends AbstractGraphics2D { iter.next(); } doDrawing(false, true, false); - currentStream.write("Q\n"); + currentStream.write("Q\n"); } protected void writeClip(Shape s) { + if (s == null) { + return; + } PathIterator iter = s.getPathIterator(getTransform()); while (!iter.isDone()) { double vals[] = new double[6]; @@ -492,110 +488,149 @@ public class PDFGraphics2D extends AbstractGraphics2D { iter.next(); } // clip area - currentStream.write("W\n"); - currentStream.write("n\n"); + currentStream.write("W\n"); + currentStream.write("n\n"); + } + + protected void applyColor(Color col, boolean fill) { + Color c = col; + if (c.getColorSpace().getType() == + java.awt.color.ColorSpace.TYPE_RGB) { + currentColour = + new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); + currentStream.write(currentColour.getColorSpaceOut(fill)); + } else if (c.getColorSpace().getType() == + java.awt.color.ColorSpace.TYPE_CMYK) { + float[] cComps = c.getColorComponents(new float[3]); + double[] cmyk = new double[3]; + for (int i = 0; i < 3; i++) { + // convert the float elements to doubles for pdf + cmyk[i] = cComps[i]; + } + currentColour = + new PDFColor(cmyk[0], cmyk[1], cmyk[2], cmyk[3]); + currentStream.write(currentColour.getColorSpaceOut(fill)); + } else if (c.getColorSpace().getType() == + java.awt.color.ColorSpace.TYPE_2CLR) { + // used for black/magenta + float[] cComps = c.getColorComponents(new float[1]); + double[] blackMagenta = new double[1]; + for (int i = 0; i < 1; i++) { + blackMagenta[i] = cComps[i]; + } + //currentColour = new PDFColor(blackMagenta[0], blackMagenta[1]); + currentStream.write(currentColour.getColorSpaceOut(fill)); + } + else { + System.err.println("Color Space not supported by PDFGraphics2D"); + } } protected void applyPaint(Paint paint, boolean fill) { - if(paint instanceof GradientPaint) { - GradientPaint gp = (GradientPaint)paint; + if (paint instanceof GradientPaint) { + GradientPaint gp = (GradientPaint) paint; Color c1 = gp.getColor1(); Color c2 = gp.getColor2(); Point2D p1 = gp.getPoint1(); Point2D p2 = gp.getPoint2(); boolean cyclic = gp.isCyclic(); - Vector theCoords = new Vector(); - theCoords.addElement( new Double(p1.getX())); - theCoords.addElement( new Double(p1.getY())); - theCoords.addElement( new Double(p2.getX())); - theCoords.addElement( new Double(p2.getY())); + Vector theCoords = new Vector(); + theCoords.addElement(new Double(p1.getX())); + theCoords.addElement(new Double(p1.getY())); + theCoords.addElement(new Double(p2.getX())); + theCoords.addElement(new Double(p2.getY())); - Vector theExtend = new Vector(); - theExtend.addElement(new Boolean(true)); - theExtend.addElement(new Boolean(true)); + Vector theExtend = new Vector(); + theExtend.addElement(new Boolean(true)); + theExtend.addElement(new Boolean(true)); - Vector theDomain = new Vector(); - theDomain.addElement(new Double(0)); - theDomain.addElement(new Double(1)); + Vector theDomain = new Vector(); + theDomain.addElement(new Double(0)); + theDomain.addElement(new Double(1)); - Vector theEncode = new Vector(); - theEncode.addElement(new Double(0)); - theEncode.addElement(new Double(1)); - theEncode.addElement(new Double(0)); - theEncode.addElement(new Double(1)); + Vector theEncode = new Vector(); + theEncode.addElement(new Double(0)); + theEncode.addElement(new Double(1)); + theEncode.addElement(new Double(0)); + theEncode.addElement(new Double(1)); - Vector theBounds = new Vector(); - theBounds.addElement(new Double(0)); - theBounds.addElement(new Double(1)); + Vector theBounds = new Vector(); + theBounds.addElement(new Double(0)); + theBounds.addElement(new Double(1)); - Vector theFunctions = new Vector(); + Vector theFunctions = new Vector(); Vector someColors = new Vector(); - PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(), c1.getBlue()); + PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(), + c1.getBlue()); someColors.addElement(color1); - PDFColor color2 = new PDFColor(c2.getRed(), c2.getGreen(), c2.getBlue()); + PDFColor color2 = new PDFColor(c2.getRed(), c2.getGreen(), + c2.getBlue()); someColors.addElement(color2); - PDFFunction myfunc = this.pdfDoc.makeFunction(2, theDomain, null, color1.getVector(), color2.getVector(), 1.0); + PDFFunction myfunc = + this.pdfDoc.makeFunction(2, theDomain, null, + color1.getVector(), color2.getVector(), 1.0); ColorSpace aColorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); - PDFPattern myPat = this.pdfDoc.createGradient(false, aColorSpace, - someColors, null, theCoords); - currentStream.write(myPat.getColorSpaceOut(fill)); + PDFPattern myPat = + this.pdfDoc.createGradient(false, aColorSpace, + someColors, null, theCoords); + currentStream.write(myPat.getColorSpaceOut(fill)); - } else if(paint instanceof TexturePaint) { + } else if (paint instanceof TexturePaint) { } } protected void applyStroke(Stroke stroke) { - if(stroke instanceof BasicStroke) { - BasicStroke bs = (BasicStroke)stroke; + if (stroke instanceof BasicStroke) { + BasicStroke bs = (BasicStroke) stroke; float[] da = bs.getDashArray(); - if(da != null) { + if (da != null) { currentStream.write("["); - for(int count = 0; count < da.length; count++) { - currentStream.write("" + ((int)da[count])); - if(count < da.length - 1) { + for (int count = 0; count < da.length; count++) { + currentStream.write("" + ((int) da[count])); + if (count < da.length - 1) { currentStream.write(" "); } } currentStream.write("] "); float offset = bs.getDashPhase(); - currentStream.write(((int)offset) + " d\n"); + currentStream.write(((int) offset) + " d\n"); } int ec = bs.getEndCap(); - switch(ec) { + switch (ec) { case BasicStroke.CAP_BUTT: currentStream.write(0 + " J\n"); - break; + break; case BasicStroke.CAP_ROUND: currentStream.write(1 + " J\n"); - break; + break; case BasicStroke.CAP_SQUARE: currentStream.write(2 + " J\n"); - break; + break; } int lj = bs.getLineJoin(); - switch(lj) { + switch (lj) { case BasicStroke.JOIN_MITER: currentStream.write(0 + " j\n"); - break; + break; case BasicStroke.JOIN_ROUND: currentStream.write(1 + " j\n"); - break; + break; case BasicStroke.JOIN_BEVEL: currentStream.write(2 + " j\n"); - break; + break; } float lw = bs.getLineWidth(); currentStream.write(PDFNumber.doubleOut(lw) + " w\n"); float ml = bs.getMiterLimit(); - currentStream.write(PDFNumber.doubleOut(ml) + " M\n"); + currentStream.write(PDFNumber.doubleOut(ml) + " M\n"); } } @@ -686,18 +721,16 @@ public class PDFGraphics2D extends AbstractGraphics2D { //System.out.println("drawString(String)"); currentStream.write("BT\n"); - Shape imclip = getClip(); - writeClip(imclip); + Shape imclip = getClip(); + writeClip(imclip); Color c = getColor(); - currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); - currentStream.write(currentColour.getColorSpaceOut(true)); + applyColor(c, true); c = getBackground(); - PDFColor col = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); - currentStream.write(col.getColorSpaceOut(false)); + applyColor(c, false); Font gFont = getFont(); String name = gFont.getName(); - if(name.equals("sanserif")) { + if (name.equals("sanserif")) { name = "sans-serif"; } int size = gFont.getSize(); @@ -706,29 +739,32 @@ public class PDFGraphics2D extends AbstractGraphics2D { try { fontState = new FontState(fontState.getFontInfo(), name, style, weight, size * 1000, 0); - } catch(org.apache.fop.apps.FOPException fope) { + } catch (org.apache.fop.apps.FOPException fope) { fope.printStackTrace(); } name = fontState.getFontName(); size = fontState.getFontSize() / 1000; -//System.out.println("ffn:" + gFont.getFontName() + "fn:" + gFont.getName() + " ff:" + gFont.getFamily() + " fs:" + fontState.getFontName()); + //System.out.println("ffn:" + gFont.getFontName() + "fn:" + gFont.getName() + " ff:" + gFont.getFamily() + " fs:" + fontState.getFontName()); - if ((!name.equals(this.currentFontName)) || - (size != this.currentFontSize)) { - this.currentFontName = name; - this.currentFontSize = size; - currentStream.write("/" + name + " " + size + " Tf\n"); + if ((!name.equals(this.currentFontName)) || + (size != this.currentFontSize)) { + this.currentFontName = name; + this.currentFontSize = size; + currentStream.write("/" + name + " " + size + " Tf\n"); - } + } AffineTransform trans = getTransform(); trans.translate(x, y); double[] vals = new double[6]; trans.getMatrix(vals); - currentStream.write(PDFNumber.doubleOut(vals[0]) + " " + PDFNumber.doubleOut(vals[1]) + " " -+ PDFNumber.doubleOut(vals[2]) + " " + PDFNumber.doubleOut(-vals[3]) + " " + PDFNumber.doubleOut(vals[4]) -+ " " + PDFNumber.doubleOut(vals[5]) + " Tm (" + s + ") Tj\n"); + currentStream.write(PDFNumber.doubleOut(vals[0]) + " " + + PDFNumber.doubleOut(vals[1]) + " " + + PDFNumber.doubleOut(vals[2]) + " " + + PDFNumber.doubleOut(-vals[3]) + " " + + PDFNumber.doubleOut(vals[4]) + " " + + PDFNumber.doubleOut(vals[5]) + " Tm (" + s + ") Tj\n"); currentStream.write("ET\n"); } @@ -761,21 +797,20 @@ public class PDFGraphics2D extends AbstractGraphics2D { System.err.println("drawString(AttributedCharacterIterator)"); currentStream.write("BT\n"); - Shape imclip = getClip(); - writeClip(imclip); + Shape imclip = getClip(); + writeClip(imclip); Color c = getColor(); - currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); - currentStream.write(currentColour.getColorSpaceOut(true)); + applyColor(c, true); c = getBackground(); - PDFColor col = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); - currentStream.write(col.getColorSpaceOut(false)); + applyColor(c, false); AffineTransform trans = getTransform(); trans.translate(x, y); double[] vals = new double[6]; trans.getMatrix(vals); - for(char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) { + for (char ch = iterator.first(); ch != CharacterIterator.DONE; + ch = iterator.next()) { Map attr = iterator.getAttributes(); String name = fontState.getFontName(); @@ -784,13 +819,17 @@ public class PDFGraphics2D extends AbstractGraphics2D { (size != this.currentFontSize)) { this.currentFontName = name; this.currentFontSize = size; - currentStream.write("/" + name + " " + (size / 1000) + " Tf\n"); + currentStream.write("/" + name + " " + (size / 1000) + + " Tf\n"); } - currentStream.write(PDFNumber.doubleOut(vals[0]) + " " + PDFNumber.doubleOut(vals[1]) + " " -+ PDFNumber.doubleOut(vals[2]) + " " + PDFNumber.doubleOut(vals[3]) + " " + PDFNumber.doubleOut(vals[4]) -+ " " + PDFNumber.doubleOut(vals[5]) + " Tm (" + ch + ") Tj\n"); + currentStream.write(PDFNumber.doubleOut(vals[0]) + " " + + PDFNumber.doubleOut(vals[1]) + " " + + PDFNumber.doubleOut(vals[2]) + " " + + PDFNumber.doubleOut(vals[3]) + " " + + PDFNumber.doubleOut(vals[4]) + " " + + PDFNumber.doubleOut(vals[5]) + " Tm (" + ch + ") Tj\n"); } currentStream.write("ET\n"); @@ -812,15 +851,13 @@ public class PDFGraphics2D extends AbstractGraphics2D { */ public void fill(Shape s) { //System.err.println("fill"); - currentStream.write("q\n"); - Shape imclip = getClip(); - writeClip(imclip); + currentStream.write("q\n"); + Shape imclip = getClip(); + writeClip(imclip); Color c = getColor(); - currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); - currentStream.write(currentColour.getColorSpaceOut(true)); + applyColor(c, true); c = getBackground(); - PDFColor col = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); - currentStream.write(col.getColorSpaceOut(false)); + applyColor(c, false); applyPaint(getPaint(), true); @@ -861,7 +898,7 @@ public class PDFGraphics2D extends AbstractGraphics2D { } doDrawing(true, false, iter.getWindingRule() == PathIterator.WIND_EVEN_ODD); - currentStream.write("Q\n"); + currentStream.write("Q\n"); } protected void doDrawing(boolean fill, boolean stroke, diff --git a/src/org/apache/fop/svg/PDFTextPainter.java b/src/org/apache/fop/svg/PDFTextPainter.java index 8c89a9a1e..a7435cb0f 100644 --- a/src/org/apache/fop/svg/PDFTextPainter.java +++ b/src/org/apache/fop/svg/PDFTextPainter.java @@ -56,14 +56,20 @@ public class PDFTextPainter implements TextPainter { */ public void paint(TextNode node, Graphics2D g2d, GraphicsNodeRenderContext context) { - System.out.println("PDFText paint"); + //System.out.println("PDFText paint"); String txt = node.getText(); Point2D loc = node.getLocation(); AttributedCharacterIterator aci = node.getAttributedCharacterIterator(); // reset position to start of char iterator - aci.first(); + if (aci.getBeginIndex() == aci.getEndIndex()) { + return; + } + char ch = aci.first(); + if (ch == AttributedCharacterIterator.DONE) { + return; + } TextNode.Anchor anchor = (TextNode.Anchor) aci.getAttribute( GVTAttributedCharacterIterator.TextAttribute.ANCHOR_TYPE); @@ -81,36 +87,35 @@ public class PDFTextPainter implements TextPainter { Float posture = (Float) aci.getAttribute(TextAttribute.POSTURE); Float taWeight = (Float) aci.getAttribute(TextAttribute.WEIGHT); - Set set = aci.getAllAttributeKeys(); - for (Iterator iter = set.iterator(); iter.hasNext();) { - System.out.println(iter.next()); - } - if (forg instanceof Color) { g2d.setColor((Color) forg); } g2d.setPaint(forg); g2d.setStroke(stroke); - String style = posture.floatValue() > 0.0 ? "italic" : "normal"; - String weight = taWeight.floatValue() > 1.0 ? "bold" : "normal"; + String style = ((posture != null) && (posture.floatValue() > 0.0)) ? + "italic" : "normal"; + String weight = ((taWeight != null) && + (taWeight.floatValue() > 1.0)) ? "bold" : "normal"; FontInfo fi = fontState.getFontInfo(); boolean found = false; - for (Enumeration e = gvtFonts.elements(); e.hasMoreElements();) { - GVTFontFamily fam = (GVTFontFamily) e.nextElement(); - String name = fam.getFamilyName(); - System.out.println(name); - if (fi.hasFont(name, weight, style)) { - try { - int fsize = (int) size.floatValue(); - fontState = new FontState(fontState.getFontInfo(), - name, style, weight, fsize * 1000, 0); - } catch (org.apache.fop.apps.FOPException fope) { - fope.printStackTrace(); + 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(); + fontState = new FontState(fontState.getFontInfo(), + name, style, weight, fsize * 1000, 0); + } catch (org.apache.fop.apps.FOPException fope) { + fope.printStackTrace(); + } + found = true; + break; } - found = true; - break; } } if (!found) { @@ -143,8 +148,6 @@ public class PDFTextPainter implements TextPainter { float advance = getStringWidth(txt); - System.out.println("ad:" + advance + " a:" + anchor + " ind:" + - aci.getIndex()); float tx = 0; if (anchor != null) { switch (anchor.getType()) { @@ -350,7 +353,7 @@ public class PDFTextPainter implements TextPainter { */ public Rectangle2D getPaintedBounds(TextNode node, FontRenderContext frc) { - System.out.println("PDFText getPaintedBounds"); + //System.out.println("PDFText getPaintedBounds"); return null; } }