You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2003/01/08 15:03:35 UTC

cvs commit: xml-fop/src/org/apache/fop/render/ps PSGraphics2D.java PSRenderer.java

jeremias    2003/01/08 06:03:35

  Modified:    src/org/apache/fop/render/ps PSGraphics2D.java
                        PSRenderer.java
  Log:
  Adjustments for font refactoring
  Lots of Javadocs
  Fixed Checkstyle errors
  
  Revision  Changes    Path
  1.7       +125 -52   xml-fop/src/org/apache/fop/render/ps/PSGraphics2D.java
  
  Index: PSGraphics2D.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/render/ps/PSGraphics2D.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PSGraphics2D.java	27 Jun 2002 11:45:55 -0000	1.6
  +++ PSGraphics2D.java	8 Jan 2003 14:03:35 -0000	1.7
  @@ -1,34 +1,54 @@
   /*
    * $Id$
  - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  + * Copyright (C) 2001-2003 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.render.ps;
   
  -import org.apache.fop.pdf.*;
  -import org.apache.fop.layout.*;
  -import org.apache.fop.fonts.*;
  -import org.apache.fop.render.pdf.*;
  -import org.apache.fop.image.*;
  -import org.apache.fop.fo.FOUserAgent;
  -
  -import org.apache.batik.ext.awt.g2d.*;
  -
  +//Java
  +import java.util.List;
   import java.text.AttributedCharacterIterator;
   import java.text.CharacterIterator;
  -import java.awt.*;
  +import java.awt.AlphaComposite;
  +import java.awt.BasicStroke;
  +import java.awt.Color;
  +import java.awt.Dimension;
   import java.awt.Font;
  +import java.awt.GradientPaint;
  +import java.awt.Graphics;
  +import java.awt.Graphics2D;
  +import java.awt.GraphicsConfiguration;
  +import java.awt.GraphicsEnvironment;
   import java.awt.Image;
  -import java.awt.image.*;
  -import java.awt.font.*;
  -import java.awt.geom.*;
  -import java.awt.image.renderable.*;
  -import java.io.*;
  +import java.awt.Paint;
  +import java.awt.Rectangle;
  +import java.awt.Shape;
  +import java.awt.Stroke;
  +import java.awt.TexturePaint;
  +import java.awt.geom.AffineTransform;
  +import java.awt.geom.PathIterator;
  +import java.awt.geom.Point2D;
  +import java.awt.image.BufferedImage;
  +import java.awt.image.DataBuffer;
  +import java.awt.image.DataBufferInt;
  +import java.awt.image.ImageObserver;
  +import java.awt.image.Raster;
  +import java.awt.image.RenderedImage;
  +import java.awt.image.renderable.RenderableImage;
  +
  +// FOP
  +import org.apache.fop.layout.FontInfo;
  +import org.apache.fop.layout.FontState;
  +import org.apache.fop.pdf.PDFColor;
  +//import org.apache.fop.pdf.PDFColorSpace;
  +import org.apache.fop.pdf.PDFNumber;
  +
  +// Batik
  +import org.apache.batik.ext.awt.g2d.AbstractGraphics2D;
  +import org.apache.batik.ext.awt.g2d.GraphicContext;
   
  -import java.util.Map;
  -import java.util.ArrayList;
   
   /**
    * This concrete implementation of <tt>AbstractGraphics2D</tt> is a
  @@ -43,13 +63,15 @@
    * @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D
    */
   public class PSGraphics2D extends AbstractGraphics2D {
  -    boolean standalone = false;
  +
  +    private boolean standalone = false;
   
       /**
        * the PDF Document being created
        */
       protected PSRenderer psRenderer;
   
  +    /** Currently valid FontState */
       protected FontState fontState;
   
       /**
  @@ -75,14 +97,21 @@
       /**
        * the current colour for use in svg
        */
  -    PDFColor currentColour = new PDFColor(0, 0, 0);
  +    protected PDFColor currentColour = new PDFColor(0, 0, 0);
   
  -    FontInfo fontInfo;
  +    /** FontInfo containing all available fonts */
  +    protected FontInfo fontInfo;
   
       /**
  -     * Create a new PDFGraphics2D with the given pdf document info.
  -     * This is used to create a Graphics object for use inside an already
  -     * existing document.
  +     * Create a new Graphics2D that generates PostScript code.
  +     * @param textAsShapes True if text should be rendered as graphics
  +     * @param fs currently valid FontState object
  +     * @param ren PostScript renderer
  +     * @param font current font name
  +     * @param size current font size
  +     * @param xpos current x pos
  +     * @param ypos current y pos
  +     * @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D#AbstractGraphics2D(boolean)
        */
       public PSGraphics2D(boolean textAsShapes, FontState fs, PSRenderer ren,
                           String font, int size, int xpos, int ypos) {
  @@ -95,22 +124,31 @@
           fontState = fs;
       }
   
  +    /**
  +     * Create a new Graphics2D that generates PostScript code.
  +     * @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D#AbstractGraphics2D(boolean)
  +     */
       public PSGraphics2D(boolean textAsShapes) {
           super(textAsShapes);
       }
   
  -    public void setGraphicContext(GraphicContext c) {
  -        gc = c;
  -    }
  -
       /**
  -     * This constructor supports the create method
  +     * Constructor for creating copies
  +     * @param g parent PostScript Graphics2D
        */
       public PSGraphics2D(PSGraphics2D g) {
           super(g);
       }
   
       /**
  +     * Sets the GraphicContext
  +     * @param c GraphicContext to use
  +     */
  +    public void setGraphicContext(GraphicContext c) {
  +        gc = c;
  +    }
  +
  +    /**
        * Creates a new <code>Graphics</code> object that is
        * a copy of this <code>Graphics</code> object.
        * @return     a new graphics context that is a copy of
  @@ -140,6 +178,7 @@
        * @param    y   the <i>y</i> coordinate.
        * @param    observer    object to be notified as more of
        * the image is converted.
  +     * @return True if the image has been fully drawn/loaded
        * @see      java.awt.Image
        * @see      java.awt.image.ImageObserver
        * @see      java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  @@ -170,13 +209,13 @@
           g.dispose();
   
           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();
   
           int count = 0;
  -        int maskpos = 0;
  +        //int maskpos = 0;
           switch (bd.getDataType()) {
           case DataBuffer.TYPE_INT:
               int[][] idata = ((DataBufferInt)bd).getBankData();
  @@ -219,6 +258,11 @@
           return true;
       }
   
  +    /**
  +     * Creates a buffered image.
  +     * @param size dimensions of the image to be created
  +     * @return the buffered image
  +     */
       public BufferedImage buildBufferedImage(Dimension size) {
           return new BufferedImage(size.width, size.height,
                                    BufferedImage.TYPE_INT_ARGB);
  @@ -357,6 +401,7 @@
        * @param    height the height of the rectangle.
        * @param    observer    object to be notified as more of
        * the image is converted.
  +     * @return   True if the image has been fully loaded/drawn
        * @see      java.awt.Image
        * @see      java.awt.image.ImageObserver
        * @see      java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
  @@ -474,6 +519,10 @@
           psRenderer.write("grestore");
       }
   
  +    /**
  +     * Establishes a clipping region
  +     * @param s Shape defining the clipping region
  +     */
       protected void writeClip(Shape s) {
           PathIterator iter = s.getPathIterator(getTransform());
           psRenderer.write("newpath");
  @@ -518,6 +567,11 @@
           psRenderer.write("clippath");
       }
   
  +    /**
  +     * Applies a new Paint object.
  +     * @param paint Paint object to use
  +     * @param fill True if to be applied for filling
  +     */
       protected void applyPaint(Paint paint, boolean fill) {
           if (paint instanceof GradientPaint) {
               GradientPaint gp = (GradientPaint)paint;
  @@ -525,35 +579,35 @@
               Color c2 = gp.getColor2();
               Point2D p1 = gp.getPoint1();
               Point2D p2 = gp.getPoint2();
  -            boolean cyclic = gp.isCyclic();
  +            //boolean cyclic = gp.isCyclic();
   
  -            ArrayList theCoords = new ArrayList();
  +            List theCoords = new java.util.ArrayList();
               theCoords.add(new Double(p1.getX()));
               theCoords.add(new Double(p1.getY()));
               theCoords.add(new Double(p2.getX()));
               theCoords.add(new Double(p2.getY()));
   
  -            ArrayList theExtend = new ArrayList();
  +            List theExtend = new java.util.ArrayList();
               theExtend.add(new Boolean(true));
               theExtend.add(new Boolean(true));
   
  -            ArrayList theDomain = new ArrayList();
  +            List theDomain = new java.util.ArrayList();
               theDomain.add(new Double(0));
               theDomain.add(new Double(1));
   
  -            ArrayList theEncode = new ArrayList();
  +            List theEncode = new java.util.ArrayList();
               theEncode.add(new Double(0));
               theEncode.add(new Double(1));
               theEncode.add(new Double(0));
               theEncode.add(new Double(1));
   
  -            ArrayList theBounds = new ArrayList();
  +            List theBounds = new java.util.ArrayList();
               theBounds.add(new Double(0));
               theBounds.add(new Double(1));
   
  -            ArrayList theFunctions = new ArrayList();
  +            //List theFunctions = new java.util.ArrayList();
   
  -            ArrayList someColors = new ArrayList();
  +            List someColors = new java.util.ArrayList();
   
               PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(),
                                              c1.getBlue());
  @@ -562,10 +616,16 @@
                                              c2.getBlue());
               someColors.add(color2);
   
  -            PDFColorSpace aColorSpace = new PDFColorSpace(PDFColorSpace.DEVICE_RGB);
  -        } else if (paint instanceof TexturePaint) {}
  +            //PDFColorSpace aColorSpace = new PDFColorSpace(PDFColorSpace.DEVICE_RGB);
  +        } else if (paint instanceof TexturePaint) {
  +            //nop
  +        }
       }
   
  +    /**
  +     * Applies a new Stroke object.
  +     * @param stroke Stroke object to use
  +     */
       protected void applyStroke(Stroke stroke) {
           if (stroke instanceof BasicStroke) {
               BasicStroke bs = (BasicStroke)stroke;
  @@ -691,7 +751,9 @@
        * left, in which case the coordinate supplied is the location of the
        * leftmost character on the baseline.
        * @param s the <code>String</code> to be rendered
  -     * @param x,&nbsp;y the coordinates where the <code>String</code>
  +     * @param x the x-coordinate where the <code>String</code>
  +     * should be rendered
  +     * @param y the y-coordinate where the <code>String</code>
        * should be rendered
        * @see #setPaint
        * @see java.awt.Graphics#setColor
  @@ -740,7 +802,9 @@
        * coordinate supplied is the location of the leftmost character
        * on the baseline.
        * @param iterator the iterator whose text is to be rendered
  -     * @param x,&nbsp;y the coordinates where the iterator's text is to be
  +     * @param x the x-coordinate where the iterator's text is to be
  +     * rendered
  +     * @param y the y-coordinate where the iterator's text is to be
        * rendered
        * @see #setPaint
        * @see java.awt.Graphics#setColor
  @@ -769,7 +833,7 @@
   
           for (char ch = iterator.first(); ch != CharacterIterator.DONE;
                   ch = iterator.next()) {
  -            Map attr = iterator.getAttributes();
  +            //Map attr = iterator.getAttributes();
   
               psRenderer.write(PDFNumber.doubleOut(vals[0]) + " "
                                + PDFNumber.doubleOut(vals[1]) + " "
  @@ -853,18 +917,25 @@
           psRenderer.write("grestore");
       }
   
  +    /**
  +     * Commits a painting operation.
  +     * @param fill filling
  +     * @param stroke stroking
  +     */
       protected void doDrawing(boolean fill, boolean stroke, boolean nonzero) {
           if (fill) {
               if (stroke) {
  -                if (!nonzero)
  +                if (!nonzero) {
                       psRenderer.write("stroke");
  -                else
  +                } else {
                       psRenderer.write("stroke");
  +                }
               } else {
  -                if (!nonzero)
  +                if (!nonzero) {
                       psRenderer.write("fill");
  -                else
  +                } else {
                       psRenderer.write("fill");
  +                }
               }
           } else {
               // if(stroke)
  @@ -875,10 +946,12 @@
       /**
        * Returns the device configuration associated with this
        * <code>Graphics2D</code>.
  +     * @return the device configuration
        */
       public GraphicsConfiguration getDeviceConfiguration() {
           // System.out.println("getDeviceConviguration");
  -        return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
  +        return GraphicsEnvironment.getLocalGraphicsEnvironment().
  +                getDefaultScreenDevice().getDefaultConfiguration();
       }
   
       /**
  @@ -901,7 +974,7 @@
        * @see       java.awt.FontMetrics
        * @see       java.awt.Graphics#getFontMetrics()
        */
  -    public FontMetrics getFontMetrics(Font f) {
  +    public java.awt.FontMetrics getFontMetrics(Font f) {
           return fmg.getFontMetrics(f);
       }
   
  
  
  
  1.27      +64 -94    xml-fop/src/org/apache/fop/render/ps/PSRenderer.java
  
  Index: PSRenderer.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/org/apache/fop/render/ps/PSRenderer.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- PSRenderer.java	4 Sep 2002 08:36:36 -0000	1.26
  +++ PSRenderer.java	8 Jan 2003 14:03:35 -0000	1.27
  @@ -1,48 +1,24 @@
   /*
    * $Id$
  - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  + * Copyright (C) 2001-2003 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.render.ps;
   
  -// FOP
  -import org.apache.fop.render.AbstractRenderer;
  -import org.apache.fop.render.Renderer;
  -import org.apache.fop.image.FopImage;
  -import org.apache.fop.layout.*;
  -import org.apache.fop.datatypes.*;
  -import org.apache.fop.fo.properties.*;
  -import org.apache.fop.render.pdf.Font;
  -import org.apache.fop.image.*;
  -
  -import org.apache.batik.bridge.*;
  -import org.apache.batik.swing.svg.*;
  -import org.apache.batik.swing.gvt.*;
  -import org.apache.batik.gvt.*;
  -import org.apache.batik.gvt.renderer.*;
  -import org.apache.batik.gvt.filter.*;
  -import org.apache.batik.gvt.event.*;
  -
  -// SVG
  -import org.w3c.dom.svg.SVGSVGElement;
  -import org.w3c.dom.svg.SVGDocument;
  -import org.w3c.dom.*;
  -import org.w3c.dom.svg.*;
  -
   // Java
  -import java.io.*;
  -import java.util.*;
   import java.io.IOException;
   import java.io.OutputStream;
   import java.util.Iterator;
  -import java.util.HashMap;
  -import java.awt.geom.AffineTransform;
  -import java.awt.geom.Dimension2D;
  -import java.awt.Point;
  -import java.awt.RenderingHints;
  -import java.awt.Dimension;
  +import java.util.Map;
  +
  +// FOP
  +import org.apache.fop.datatypes.ColorType;
  +import org.apache.fop.fonts.Font;
  +import org.apache.fop.layout.FontInfo;
  +import org.apache.fop.render.AbstractRenderer;
  +
   
   /**
    * Renderer that renders to PostScript.
  @@ -51,24 +27,12 @@
    * is the FlateEncode filter which is a Level 3 feature. The filters in use
    * are hardcoded at the moment.
    * <br>
  - * This class follows the Document Structuring Conventions (DSC) version 3.0
  - * (If I did everything right). If anyone modifies this renderer please make
  + * This class follows the Document Structuring Conventions (DSC) version 3.0.
  + * If anyone modifies this renderer please make
    * sure to also follow the DSC to make it simpler to programmatically modify
    * the generated Postscript files (ex. extract pages etc.).
    * <br>
  - * TODO: Character size/spacing, SVG Transcoder for Batik, configuration, move
  - * to PrintRenderer, maybe improve filters (I'm not very proud of them), add a
  - * RunLengthEncode filter (useful for Level 2 Postscript), Improve
  - * DocumentProcessColors stuff (probably needs to be configurable, then maybe
  - * add a color to grayscale conversion for bitmaps to make output smaller (See
  - * PCLRenderer), font embedding, support different character encodings, try to
  - * implement image transparency, positioning of images is wrong etc. <P>
  - *
  - * Modified by Mark Lillywhite mark-fop@inomial.com, to use the new
  - * Renderer interface. This PostScript renderer appears to be the
  - * most efficient at producing output.
  - *
  - * @author Jeremias M�rki
  + * @todo Rebuild the PostScript renderer
    */
   public class PSRenderer extends AbstractRenderer {
   
  @@ -77,8 +41,6 @@
        */
       protected String producer;
   
  -    int imagecount = 0;    // DEBUG
  -
       private boolean enableComments = true;
   
       /**
  @@ -98,7 +60,7 @@
       private FontInfo fontInfo;
   
       /**
  -     * set the document's producer
  +     * Set the document's producer
        *
        * @param producer string indicating application producing the PostScript
        */
  @@ -107,26 +69,34 @@
       }
   
       /**
  -     * write out a command
  +     * Write out a command
  +     * @param cmd PostScript command
        */
       protected void write(String cmd) {
           try {
               out.write(cmd);
           } catch (IOException e) {
  -            if (!ioTrouble)
  +            if (!ioTrouble) {
                   e.printStackTrace();
  +            }
               ioTrouble = true;
           }
       }
   
       /**
  -     * write out a comment
  +     * Write out a comment
  +     * @param comment Comment to write
        */
       protected void comment(String comment) {
  -        if (this.enableComments)
  +        if (this.enableComments) {
               write(comment);
  +        }
       }
   
  +    /**
  +     * Generates the PostScript code for the font dictionary.
  +     * @param fontInfo available fonts
  +     */
       protected void writeFontDict(FontInfo fontInfo) {
           write("%%BeginResource: procset FOPFonts");
           write("%%Title: Font setup (shortcuts) for this file");
  @@ -186,7 +156,8 @@
           write("  /FontMatrix get 0 get /Ts exch def /FontInfo get dup");
           write("  /UnderlinePosition get Ts mul /To exch def");
           write("  /UnderlineThickness get Ts mul /Tt exch def");
  -        write("  ux uy To add cs 10 mul 26 idiv add moveto Tcx uy To add cs 10 mul 26 idiv add lineto");
  +        write("  ux uy To add cs 10 mul 26 idiv add moveto "
  +                    + "Tcx uy To add cs 10 mul 26 idiv add lineto");
           write("  Tt setlinewidth stroke");
           write("  grestore");
           write("} bd");
  @@ -195,12 +166,12 @@
   
           // write("/gfF1{/Helvetica findfont} bd");
           // write("/gfF3{/Helvetica-Bold findfont} bd");
  -        HashMap fonts = fontInfo.getFonts();
  +        Map fonts = fontInfo.getFonts();
           Iterator enum = fonts.keySet().iterator();
           while (enum.hasNext()) {
               String key = (String)enum.next();
               Font fm = (Font)fonts.get(key);
  -            write("/" + key + " /" + fm.fontName() + " def");
  +            write("/" + key + " /" + fm.getFontName() + " def");
           }
           write("end def");
           write("%%EndResource");
  @@ -208,22 +179,25 @@
           while (enum.hasNext()) {
               String key = (String)enum.next();
               Font fm = (Font)fonts.get(key);
  -            write("/" + fm.fontName() + " findfont");
  +            write("/" + fm.getFontName() + " findfont");
               write("dup length dict begin");
               write("  {1 index /FID ne {def} {pop pop} ifelse} forall");
               write("  /Encoding ISOLatin1Encoding def");
               write("  currentdict");
               write("end");
  -            write("/" + fm.fontName() + " exch definefont pop");
  +            write("/" + fm.getFontName() + " exch definefont pop");
           }
       }
   
  +    /**
  +     * Make sure the cursor is in the right place.
  +     */
       protected void movetoCurrPosition() {
           write(this.currentIPPosition + " " + this.currentBPPosition + " M");
       }
   
       /**
  -     * set up the font info
  +     * Set up the font info
        *
        * @param fontInfo the font info object to set up
        */
  @@ -233,37 +207,32 @@
           this.fontInfo = fontInfo;
       }
   
  +    /**
  +     * Draws a filled rectangle.
  +     * @param x x-coordinate
  +     * @param y y-coordinate
  +     * @param w width
  +     * @param h height
  +     * @param col color to fill with
  +     */
       protected void addFilledRect(int x, int y, int w, int h,
                                    ColorType col) {
  -            write("newpath");
  -            write(x + " " + y + " M");
  -            write(w + " 0 rlineto");
  -            write("0 " + (-h) + " rlineto");
  -            write((-w) + " 0 rlineto");
  -            write("0 " + h + " rlineto");
  -            write("closepath");
  -            useColor(col);
  -            write("fill");
  -    }
  -
  -    private long copyStream(InputStream in, OutputStream out,
  -                            int bufferSize) throws IOException {
  -        long bytes_total = 0;
  -        byte[] buf = new byte[bufferSize];
  -        int bytes_read;
  -        while ((bytes_read = in.read(buf)) != -1) {
  -            bytes_total += bytes_read;
  -            out.write(buf, 0, bytes_read);
  -        }
  -        return bytes_total;
  -    }
  -
  -
  -    private long copyStream(InputStream in,
  -                            OutputStream out) throws IOException {
  -        return copyStream(in, out, 4096);
  +        write("newpath");
  +        write(x + " " + y + " M");
  +        write(w + " 0 rlineto");
  +        write("0 " + (-h) + " rlineto");
  +        write((-w) + " 0 rlineto");
  +        write("0 " + h + " rlineto");
  +        write("closepath");
  +        useColor(col);
  +        write("fill");
       }
   
  +    /**
  +     * Changes the currently used font.
  +     * @param name name of the font
  +     * @param size font size
  +     */
       public void useFont(String name, int size) {
           if ((currentFontName != name) || (currentFontSize != size)) {
               write(name + " " + size + " F");
  @@ -286,14 +255,15 @@
       }
   
       /**
  -    */
  +     * @see org.apache.fop.render.Renderer#startRenderer(OutputStream)
  +     */
       public void startRenderer(OutputStream outputStream)
       throws IOException {
           getLogger().debug("rendering areas to PostScript");
   
           this.out = new PSStream(outputStream);
           write("%!PS-Adobe-3.0");
  -        write("%%Creator: "+this.producer);
  +        write("%%Creator: " + this.producer);
           write("%%DocumentProcessColors: Black");
           write("%%DocumentSuppliedResources: procset FOPFonts");
           write("%%EndComments");
  @@ -337,9 +307,9 @@
       }
   
       /**
  -    */
  -    public void stopRenderer()
  -    throws IOException {
  +     * @see org.apache.fop.render.Renderer#stopRenderer()
  +     */
  +    public void stopRenderer() throws IOException {
           write("%%Trailer");
           write("%%EOF");
           this.out.flush();
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-cvs-help@xml.apache.org