]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
more refactoring of duplicate code in renderers
authorKeiron Liddle <keiron@apache.org>
Tue, 18 Sep 2001 13:06:08 +0000 (13:06 +0000)
committerKeiron Liddle <keiron@apache.org>
Tue, 18 Sep 2001 13:06:08 +0000 (13:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194469 13f79535-47bb-0310-9956-ffa450edef68

src/org/apache/fop/render/AbstractRenderer.java
src/org/apache/fop/render/PrintRenderer.java
src/org/apache/fop/render/awt/AWTRenderer.java
src/org/apache/fop/render/mif/MIFRenderer.java
src/org/apache/fop/render/ps/PSRenderer.java
src/org/apache/fop/render/xml/XMLRenderer.java

index 23cf0ec7a4991c3d36c8b8c3de38ebfcedeab0a2..0c203753fe867e672ad53676c153d0acc8232163 100644 (file)
@@ -54,8 +54,7 @@ public abstract class AbstractRenderer implements Renderer {
     public void renderSpanArea(SpanArea area) {
         Enumeration e = area.getChildren().elements();
         while (e.hasMoreElements()) {
-            org.apache.fop.layout.Box b =
-                (org.apache.fop.layout.Box)e.nextElement();
+            Box b = (Box)e.nextElement();
             b.render(this);    // column areas
         }
 
@@ -63,6 +62,67 @@ public abstract class AbstractRenderer implements Renderer {
 
     protected abstract void doFrame(Area area);
 
+    /**
+     * Add a filled rectangle to the current stream
+     * This default implementation calls addRect
+     * using the same color for fill and border.
+     *
+     * @param x the x position of left edge in millipoints
+     * @param y the y position of top edge in millipoints
+     * @param w the width in millipoints
+     * @param h the height in millipoints
+     * @param fill the fill color/gradient
+     */
+    protected abstract void addFilledRect(int x, int y, int w, int h,
+                                 ColorType col);
+
+    public void renderBodyAreaContainer(BodyAreaContainer area) {
+        int saveY = this.currentYPosition;
+        int saveX = this.currentAreaContainerXPosition;
+
+        if (area.getPosition() == Position.ABSOLUTE) {
+            // Y position is computed assuming positive Y axis, adjust for negative postscript one
+            this.currentYPosition = area.getYPosition();
+            this.currentAreaContainerXPosition = area.getXPosition();
+        } else if (area.getPosition() == Position.RELATIVE) {
+            this.currentYPosition -= area.getYPosition();
+            this.currentAreaContainerXPosition += area.getXPosition();
+        }
+
+        this.currentXPosition = this.currentAreaContainerXPosition;
+        int w, h;
+        int rx = this.currentAreaContainerXPosition;
+        w = area.getContentWidth();
+        h = area.getContentHeight();
+        int ry = this.currentYPosition;
+        ColorType bg = area.getBackgroundColor();
+
+        // I'm not sure I should have to check for bg being null
+        // but I do
+        if ((bg != null) && (bg.alpha() == 0)) {
+            addFilledRect(rx, ry, w, -h, bg);
+        }
+
+        // floats & footnotes stuff
+        renderAreaContainer(area.getBeforeFloatReferenceArea());
+        renderAreaContainer(area.getFootnoteReferenceArea());
+
+        // main reference area
+        Enumeration e = area.getMainReferenceArea().getChildren().elements();
+        while (e.hasMoreElements()) {
+            Box b = (Box)e.nextElement();
+            b.render(this);    // span areas
+        }
+
+
+        if (area.getPosition() != Position.STATIC) {
+            this.currentYPosition = saveY;
+            this.currentAreaContainerXPosition = saveX;
+        } else
+            this.currentYPosition -= area.getHeight();
+
+    }
+
     /**
      * render area container
      *
index 02fe863072b012740970ed4fcbbb3fcff77c80a1..e6f4c6101bc1f7efc7ee4f0bb73e2abc3f984633 100644 (file)
@@ -177,51 +177,10 @@ public abstract class PrintRenderer extends AbstractRenderer {
         addRect(x, y, w, h, fill, fill);
     }
 
-    public void renderBodyAreaContainer(BodyAreaContainer area) {
-        int saveY = this.currentYPosition;
-        int saveX = this.currentAreaContainerXPosition;
-
-        if (area.getPosition() == Position.ABSOLUTE) {
-            // Y position is computed assuming positive Y axis, adjust for negative postscript one
-            this.currentYPosition = area.getYPosition();
-            this.currentAreaContainerXPosition = area.getXPosition();
-        } else if (area.getPosition() == Position.RELATIVE) {
-            this.currentYPosition -= area.getYPosition();
-            this.currentAreaContainerXPosition += area.getXPosition();
-        }
-
-        this.currentXPosition = this.currentAreaContainerXPosition;
-        int w, h;
-        int rx = this.currentAreaContainerXPosition;
-        w = area.getContentWidth();
-        h = area.getContentHeight();
-        int ry = this.currentYPosition;
-        ColorType bg = area.getBackgroundColor();
-
-        // I'm not sure I should have to check for bg being null
-        // but I do
-        if ((bg != null) && (bg.alpha() == 0)) {
-            this.addRect(rx, ry, w, -h, new PDFColor(bg), new PDFColor(bg));
-        }
-
-        // floats & footnotes stuff
-        renderAreaContainer(area.getBeforeFloatReferenceArea());
-        renderAreaContainer(area.getFootnoteReferenceArea());
-
-        // main reference area
-        Enumeration e = area.getMainReferenceArea().getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);    // span areas
-        }
-
-
-        if (area.getPosition() != Position.STATIC) {
-            this.currentYPosition = saveY;
-            this.currentAreaContainerXPosition = saveX;
-        } else
-            this.currentYPosition -= area.getHeight();
-
+    protected void addFilledRect(int x, int y, int w, int h,
+                                 ColorType col) {
+        PDFColor pdfcol = new PDFColor(col);
+        addRect(x, y, w, h, pdfcol, pdfcol);
     }
 
     protected void doFrame(Area area) {
index 9a664b5a2d952a28b75c3157a98d8d7189e06727..aad6001b2d70ffa0d05d0e2d821bb21dc45e65dc 100644 (file)
@@ -219,11 +219,8 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable
      * @param g the green component
      * @param b the blue component
      */
-
     // changed by aml/rlc to use helper function that
     // corrects for integer roundoff, and to remove 3D effect
-
-
     protected void addRect(int x, int y, int w, int h, float r, float g,
                            float b) {
         graphics.setColor(new Color(r, g, b));
@@ -280,6 +277,14 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable
             graphics.fillRect(startx, starty, endx - startx, endy - starty);
     }
 
+    protected void addFilledRect(int x, int y, int w, int h,
+                                 ColorType col) {
+        float r = col.red();
+        float g = col.green();
+        float b = col.blue();
+        addRect(x, y, w, h, r, g, b, r, g, b);
+    }
+
     /**
      * To configure before print.
      *
@@ -392,20 +397,6 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable
          */
     }
 
-    public void renderBodyAreaContainer(BodyAreaContainer area) {
-        renderAreaContainer(area.getBeforeFloatReferenceArea());
-        renderAreaContainer(area.getFootnoteReferenceArea());
-
-        // main reference area
-        Enumeration e = area.getMainReferenceArea().getChildren().elements();
-        while (e.hasMoreElements()) {
-            org.apache.fop.layout.Box b =
-                (org.apache.fop.layout.Box)e.nextElement();
-            b.render(this);    // span areas
-        }
-
-    }
-
     protected void doFrame(org.apache.fop.layout.Area area) {
         int w, h;
         int rx = this.currentAreaContainerXPosition;
index 6dc9724aa8db4db19054d7d1f83f7500f1f1e6f4..3d4a990d18b203039f425bbc5de4572254c09ba3 100644 (file)
@@ -149,93 +149,11 @@ public class MIFRenderer extends AbstractRenderer {
             this.mifDoc.endTable();
 
         }
-        int saveY = this.currentYPosition;
-        int saveX = this.currentAreaContainerXPosition;
-
-        if (area.getPosition() == Position.ABSOLUTE) {
-            // Y position is computed assuming positive Y axis, adjust for negative postscript one
-            this.currentYPosition = area.getYPosition()
-                                    - 2 * area.getPaddingTop()
-                                    - 2 * area.getBorderTopWidth();
-
-            this.currentAreaContainerXPosition = area.getXPosition();
-        } else if (area.getPosition() == Position.RELATIVE) {
-
-            this.currentYPosition -= area.getYPosition();
-            this.currentAreaContainerXPosition += area.getXPosition();
-
-        } else if (area.getPosition() == Position.STATIC) {
-
-            this.currentYPosition -= area.getPaddingTop()
-                                     + area.getBorderTopWidth();
-            this.currentAreaContainerXPosition += area.getPaddingLeft()
-                                                  + area.getBorderLeftWidth();
-        }
-
-        this.currentXPosition = this.currentAreaContainerXPosition;
-        doFrame(area);
-
-        Enumeration e = area.getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);
-        }
-        if (area.getPosition() != Position.STATIC) {
-            this.currentYPosition = saveY;
-            this.currentAreaContainerXPosition = saveX;
-        } else
-            this.currentYPosition -= area.getHeight();
+        super.renderAreaContainer(area);
     }
 
-    public void renderBodyAreaContainer(BodyAreaContainer area) {
-
-
-        int saveY = this.currentYPosition;
-        int saveX = this.currentAreaContainerXPosition;
-
-        if (area.getPosition() == Position.ABSOLUTE) {
-            // Y position is computed assuming positive Y axis, adjust for negative postscript one
-            this.currentYPosition = area.getYPosition();
-            this.currentAreaContainerXPosition = area.getXPosition();
-        } else if (area.getPosition() == Position.RELATIVE) {
-            this.currentYPosition -= area.getYPosition();
-            this.currentAreaContainerXPosition += area.getXPosition();
-        }
-
-        this.currentXPosition = this.currentAreaContainerXPosition;
-        int w, h;
-        int rx = this.currentAreaContainerXPosition;
-        w = area.getContentWidth();
-        h = area.getContentHeight();
-        int ry = this.currentYPosition;
-        ColorType bg = area.getBackgroundColor();
-
-        /*
-         * // I'm not sure I should have to check for bg being null
-         * // but I do
-         * if ((bg != null) && (bg.alpha() == 0)) {
-         * this.addRect(rx, ry, w, -h, new PDFColor(bg), new PDFColor(bg));
-         * }
-         */
-        /*
-         * // floats & footnotes stuff
-         * renderAreaContainer(area.getBeforeFloatReferenceArea());
-         * renderAreaContainer(area.getFootnoteReferenceArea());
-         */
-        // main reference area
-        Enumeration e = area.getMainReferenceArea().getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);    // span areas
-        }
-
-
-        if (area.getPosition() != Position.STATIC) {
-            this.currentYPosition = saveY;
-            this.currentAreaContainerXPosition = saveX;
-        } else
-            this.currentYPosition -= area.getHeight();
-
+    protected void addFilledRect(int x, int y, int w, int h,
+                                 ColorType col) {
     }
 
     protected void doFrame(Area area) {
@@ -293,42 +211,25 @@ public class MIFRenderer extends AbstractRenderer {
     }
 
     public void renderSpanArea(SpanArea area) {
-
         // A span maps to a textframe
-
-
         this.mifDoc.createTextRect(area.getColumnCount());
-
-        Enumeration e = area.getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);    // column areas
-        }
-
+        super.renderSpanArea(area);
     }
 
     /**
      * render the given block area
      */
     public void renderBlockArea(BlockArea area) {
-
         this.mifDoc.setBlockProp(area.getStartIndent(), area.getEndIndent());
-        Enumeration e = area.getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);
-        }
-
+        super.renderBlockArea(area);
     }
 
     /**
      * render the given display space
      */
     public void renderDisplaySpace(DisplaySpace space) {
-
         int d = space.getSize();
         this.currentYPosition -= d;
-
     }
 
     /**
@@ -341,7 +242,6 @@ public class MIFRenderer extends AbstractRenderer {
      */
     public void renderForeignObjectArea(ForeignObjectArea area) {}
 
-
     public void renderWordArea(WordArea area) {
         String s;
         s = area.getText();
@@ -407,32 +307,9 @@ public class MIFRenderer extends AbstractRenderer {
      * render the given line area
      */
     public void renderLineArea(LineArea area) {
-
-        int rx = this.currentAreaContainerXPosition + area.getStartIndent();
-        int ry = this.currentYPosition;
-        int w = area.getContentWidth();
-        int h = area.getHeight();
-
-        this.currentYPosition -= area.getPlacementOffset();
-        this.currentXPosition = rx;
-
-        int bl = this.currentYPosition;
-
         // The start of a new linearea corresponds to a new para in FM
-
         this.mifDoc.startLine();
-
-        Enumeration e = area.getChildren().elements();
-        while (e.hasMoreElements()) {
-
-            Box b = (Box)e.nextElement();
-            this.currentYPosition = ry - area.getPlacementOffset();
-            b.render(this);
-
-        }
-        this.currentYPosition = ry - h;
-        this.currentXPosition = rx;
-
+        super.renderLineArea(area);
     }
 
     /**
index e3cd72039fa66f8a5ca4d8ce262a97b86d42fceb..6785c562b5941b1880c56fea431d20fb65450fec 100644 (file)
@@ -143,7 +143,6 @@ public class PSRenderer extends AbstractRenderer {
         }
     }
 
-
     /**
      * write out a comment
      */
@@ -152,7 +151,6 @@ public class PSRenderer extends AbstractRenderer {
             write(comment);
     }
 
-
     protected void writeFontDict(FontInfo fontInfo) {
         write("%%BeginResource: procset FOPFonts");
         write("%%Title: Font setup (shortcuts) for this file");
@@ -259,122 +257,17 @@ public class PSRenderer extends AbstractRenderer {
         this.fontInfo = fontInfo;
     }
 
-    /**
-     * render an area container to PostScript
-     *
-     * @param area the area container to render
-     */
-    public void renderAreaContainer(AreaContainer area) {
-        int saveY = this.currentYPosition;
-        int saveX = this.currentAreaContainerXPosition;
-        if (area.getPosition() == Position.ABSOLUTE) {
-            // Y position is computed assuming positive Y axis, adjust for negative postscript one
-            this.currentYPosition = area.getYPosition()
-                                    - 2 * area.getPaddingTop()
-                                    - 2 * area.getBorderTopWidth();
-            this.currentAreaContainerXPosition = area.getXPosition();
-        } else if (area.getPosition() == Position.RELATIVE) {
-            this.currentYPosition -= area.getYPosition();
-            this.currentAreaContainerXPosition += area.getXPosition();
-        } else if (area.getPosition() == Position.STATIC) {
-            this.currentYPosition -= area.getPaddingTop()
-                                     + area.getBorderTopWidth();
-            this.currentAreaContainerXPosition += area.getPaddingLeft()
-                                                  + area.getBorderLeftWidth();
-        }
-
-        this.currentXPosition = this.currentAreaContainerXPosition;
-
-        // comment("% --- AreaContainer begin");
-        doFrame(area);
-        Enumeration e = area.getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);
-        }
-        // comment("% --- AreaContainer end");
-
-        if (area.getPosition() != Position.STATIC) {
-            this.currentYPosition = saveY;
-            this.currentAreaContainerXPosition = saveX;
-        } else {
-            this.currentYPosition -= area.getHeight();
-        }
-    }
-
-    /**
-     * render a body area container to PostScript
-     *
-     * @param area the body area container to render
-     */
-    public void renderBodyAreaContainer(BodyAreaContainer area) {
-        int saveY = this.currentYPosition;
-        int saveX = this.currentAreaContainerXPosition;
-
-        if (area.getPosition() == Position.ABSOLUTE) {
-            // Y position is computed assuming positive Y axis, adjust for negative postscript one
-            this.currentYPosition = area.getYPosition();
-            this.currentAreaContainerXPosition = area.getXPosition();
-        } else if (area.getPosition() == Position.RELATIVE) {
-            this.currentYPosition -= area.getYPosition();
-            this.currentAreaContainerXPosition += area.getXPosition();
-        }
-
-        this.currentXPosition = this.currentAreaContainerXPosition;
-        int w, h;
-        int rx = this.currentAreaContainerXPosition;
-        w = area.getContentWidth();
-        h = area.getContentHeight();
-        int ry = this.currentYPosition;
-
-        // comment("% --- BodyAreaContainer begin");
-        doFrame(area);
-        // movetoCurrPosition();
-
-        Enumeration e = area.getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);
-        }
-        // comment("% --- BodyAreaContainer end");
-
-        if (area.getPosition() != Position.STATIC) {
-            this.currentYPosition = saveY;
-            this.currentAreaContainerXPosition = saveX;
-        } else {
-            this.currentYPosition -= area.getHeight();
-        }
-    }
-
-    /**
-     * render a span area to PostScript
-     *
-     * @param area the span area to render
-     */
-    public void renderSpanArea(SpanArea area) {
-        // comment("% --- SpanArea begin");
-        Enumeration e = area.getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);
-        }
-        // comment("% --- SpanArea end");
-    }
-
-    /**
-     * render a block area to PostScript
-     *
-     * @param area the block area to render
-     */
-    public void renderBlockArea(BlockArea area) {
-        // comment("% --- BlockArea begin");
-        doFrame(area);
-        Enumeration e = area.getChildren().elements();
-        while (e.hasMoreElements()) {
-            Box b = (Box)e.nextElement();
-            b.render(this);
-        }
-        // comment("% --- BlockArea end");
+    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");
     }
 
     /**
@@ -699,6 +592,7 @@ public class PSRenderer extends AbstractRenderer {
         this.currentXPosition = rx;
 
         int bl = this.currentYPosition;
+        // method is identical to super method except next line
         movetoCurrPosition();
 
         String fontWeight = area.getFontState().getFontWeight();
index 8157060914a4cb27f7e829415fbb9baceaaac026..456be9a1fedbb69e529dc8d33db042ac23e6c715 100644 (file)
@@ -17,6 +17,8 @@ import org.apache.fop.layout.inline.*;
 import org.apache.fop.pdf.*;
 import org.apache.fop.fo.properties.LeaderPattern;
 
+import org.apache.log.Logger;
+
 // Java
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -30,7 +32,13 @@ import java.util.Hashtable;
  * Modified by Mark Lillywhite mark-fop@inomial.com to use the
  * new renderer interface. Not 100% certain that this is correct.
  */
-public class XMLRenderer extends AbstractRenderer {
+public class XMLRenderer implements Renderer {
+
+    protected Logger log;
+
+    public void setLogger(Logger logger) {
+        log = logger;
+    }
 
     /**
      * indentation to use for pretty-printing the XML
@@ -55,8 +63,6 @@ public class XMLRenderer extends AbstractRenderer {
 
     public XMLRenderer() {}
 
-    protected void doFrame(Area area) {}
-
     /**
      * set up renderer options
      */
@@ -444,7 +450,7 @@ public class XMLRenderer extends AbstractRenderer {
     */
     public void startRenderer(OutputStream outputStream)
     throws IOException {
-        log.info("rendering areas to XML");
+        log.debug("rendering areas to XML");
         this.writer = new PrintWriter(outputStream);
         this.writer.write( "<?xml version=\"1.0\"?>\n<!-- produced by " +
                            this.producer + " -->\n");
@@ -459,6 +465,6 @@ public class XMLRenderer extends AbstractRenderer {
     throws IOException {
         writeEndTag("</AreaTree>");
         this.writer.flush();
-        log.error("written out XML");
+        log.debug("written out XML");
     }
 }