]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Bugzilla#45822: Changed the dashed borders to appear more like dashes and less like...
authorMehdi Houshmand <mehdi@apache.org>
Tue, 30 Oct 2012 09:26:59 +0000 (09:26 +0000)
committerMehdi Houshmand <mehdi@apache.org>
Tue, 30 Oct 2012 09:26:59 +0000 (09:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1403643 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/afp/AFPBorderPainter.java
src/java/org/apache/fop/render/intermediate/BorderPainter.java
src/java/org/apache/fop/render/pdf/PDFGraphicsPainter.java
src/java/org/apache/fop/render/ps/PSGraphicsPainter.java
status.xml
test/java/org/apache/fop/render/intermediate/BorderPainterTestCase.java
test/java/org/apache/fop/render/pdf/PDFBorderPainterTestCase.java [new file with mode: 0644]
test/java/org/apache/fop/render/ps/PSBorderPainterTestCase.java [new file with mode: 0644]

index 42c58aa7df0cf03d0267947039974535732742e7..06f1ecd11639e4a46c5e7904ab56b7ee3d64dc49 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.fop.afp;
 import java.awt.geom.AffineTransform;
 
 import org.apache.fop.fo.Constants;
+import org.apache.fop.render.intermediate.BorderPainter;
 import org.apache.fop.util.ColorUtil;
 
 /**
@@ -126,24 +127,27 @@ public class AFPBorderPainter extends AbstractAFPPainter {
             }
             break;
         case Constants.EN_DASHED:
-            int thick = lineDataInfo.getThickness() * 3;
             if (borderPaintInfo.isHorizontal()) {
-                lineDataInfo.setX2 ( lineDataInfo.getX1() + thick );
+                int dashWidth = (int) (BorderPainter.dashWidthCalculator(x2 - x1, thickness));
+                lineDataInfo.setX2 ( lineDataInfo.getX1() + dashWidth );
                 lineDataInfo.setY2 ( lineDataInfo.getY1() );
                 int ex2 = Math.round(x2);
-                while (lineDataInfo.getX1() + thick < ex2) {
+                int spaceWidth = (int) (BorderPainter.DASHED_BORDER_SPACE_RATIO * dashWidth);
+                while (lineDataInfo.getX2() < ex2) {
                     dataStream.createLine(lineDataInfo);
-                    lineDataInfo.setX1 ( lineDataInfo.getX1() + 2 * thick );
-                    lineDataInfo.setX2 ( lineDataInfo.getX1() + thick );
+                    lineDataInfo.setX1 ( lineDataInfo.getX2() + spaceWidth );
+                    lineDataInfo.setX2 ( lineDataInfo.getX1() + dashWidth );
                 }
             } else {
+                int dashWidth = (int) BorderPainter.dashWidthCalculator(y2 - y1, thickness);
                 lineDataInfo.setX2 ( lineDataInfo.getX1() );
-                lineDataInfo.setY2 ( lineDataInfo.getY1() + thick );
+                lineDataInfo.setY2 ( lineDataInfo.getY1() + dashWidth );
                 int ey2 = Math.round(y2);
-                while (lineDataInfo.getY1() + thick < ey2) {
+                int spaceWidth = (int) (BorderPainter.DASHED_BORDER_SPACE_RATIO * dashWidth);
+                while (lineDataInfo.getY2() < ey2) {
                     dataStream.createLine(lineDataInfo);
-                    lineDataInfo.setY1 ( lineDataInfo.getY1() + 2 * thick );
-                    lineDataInfo.setY2 ( lineDataInfo.getY1() + thick );
+                    lineDataInfo.setY1 ( lineDataInfo.getY2() + spaceWidth );
+                    lineDataInfo.setY2 ( lineDataInfo.getY1() + dashWidth );
                 }
             }
             break;
index 19e30b50f61a5decfb909efb7eb936fb9c1a30a3..d63c4534bfc26af1b3de810c201f8013c52a8f6a 100644 (file)
@@ -56,6 +56,11 @@ public class BorderPainter {
     /** Convention index of bottom-left border corners */
     protected static final int BOTTOM_LEFT = 3;
 
+    /** The ratio between a solid dash and the white-space in a dashed-border */
+    public static final float DASHED_BORDER_SPACE_RATIO = 0.5f;
+    /** The length of the dash as a factor of the border width i.e. 4 -> dashWidth = 4*borderWidth */
+    protected static final float DASHED_BORDER_LENGTH_FACTOR = 4.0f;
+
     private final GraphicsPainter graphicsPainter;
 
     public BorderPainter(GraphicsPainter graphicsPainter) {
@@ -274,6 +279,21 @@ public class BorderPainter {
         return bp != null && bp.isCollapseOuter();
     }
 
+    /**
+     * This method calculates the length of the "dash" in a dashed border. The dash satisfies the
+     * condition that corners start on a dash and end with a dash (rather than ending with a white space).
+     * @param borderLength The length of the border.
+     * @param borderWidth The width/thickness of the border.
+     * @param dashSpaceRatio The ratio between dashes and white-space.
+     * @return returns the length of the dash such that it fits the criteria above.
+     */
+    public static float dashWidthCalculator(float borderLength, float borderWidth) {
+        float dashWidth = DASHED_BORDER_LENGTH_FACTOR * borderWidth;
+        int period = (int) ((borderLength - dashWidth) / dashWidth / (1.0f + DASHED_BORDER_SPACE_RATIO));
+        period = period < 0 ? 0 : period;
+        return borderLength / (period * (1.0f + DASHED_BORDER_SPACE_RATIO) + 1.0f);
+    }
+
     /** TODO merge with drawRectangularBorders?
      * @param borderRect the border rectangle
      * @param bpsBefore the border specification on the before side
index ff1ffb5ff3dc5db7c7ae46033e2e1ab2a775bf13..5e079269681ce1577423503db786fa32d0a5a1c8 100644 (file)
@@ -27,6 +27,7 @@ import java.io.IOException;
 import org.apache.fop.fo.Constants;
 import org.apache.fop.render.intermediate.ArcToBezierCurveTransformer;
 import org.apache.fop.render.intermediate.BezierCurvePainter;
+import org.apache.fop.render.intermediate.BorderPainter;
 import org.apache.fop.render.intermediate.GraphicsPainter;
 import org.apache.fop.traits.RuleStyle;
 import org.apache.fop.util.ColorUtil;
@@ -64,25 +65,15 @@ public class PDFGraphicsPainter implements GraphicsPainter, BezierCurvePainter {
         case Constants.EN_DASHED:
             generator.setColor(col);
             if (horz) {
-                float unit = Math.abs(2 * h);
-                int rep = (int) (w / unit);
-                if (rep % 2 == 0) {
-                    rep++;
-                }
-                unit = w / rep;
+                float dashedWidth = BorderPainter.dashWidthCalculator(w, h);
                 float ym = y1 + (h / 2);
-                generator.setDashLine(unit)
+                generator.setDashLine(dashedWidth, dashedWidth * BorderPainter.DASHED_BORDER_SPACE_RATIO)
                         .setLineWidth(h)
                         .strokeLine(x1, ym, x2, ym);
             } else {
-                float unit = Math.abs(2 * w);
-                int rep = (int) (h / unit);
-                if (rep % 2 == 0) {
-                    rep++;
-                }
-                unit = h / rep;
+                float dashedWidth = BorderPainter.dashWidthCalculator(h, w);
                 float xm = x1 + (w / 2);
-                generator.setDashLine(unit)
+                generator.setDashLine(dashedWidth, dashedWidth * BorderPainter.DASHED_BORDER_SPACE_RATIO)
                         .setLineWidth(w)
                         .strokeLine(xm, y1, xm, y2);
             }
index ab766f70143fdb558acd7b4adc90c1e5d7a300bc..9152d093702b0d54789f6c937271f69895c98759 100644 (file)
@@ -31,6 +31,7 @@ import org.apache.xmlgraphics.ps.PSGenerator;
 import org.apache.fop.fo.Constants;
 import org.apache.fop.render.intermediate.ArcToBezierCurveTransformer;
 import org.apache.fop.render.intermediate.BezierCurvePainter;
+import org.apache.fop.render.intermediate.BorderPainter;
 import org.apache.fop.render.intermediate.GraphicsPainter;
 import org.apache.fop.traits.RuleStyle;
 import org.apache.fop.util.ColorUtil;
@@ -87,25 +88,17 @@ public class PSGraphicsPainter implements GraphicsPainter, BezierCurvePainter {
         case Constants.EN_DASHED:
             gen.useColor(col);
             if (horz) {
-                float unit = Math.abs(2 * h);
-                int rep = (int) (w / unit);
-                if (rep % 2 == 0) {
-                    rep++;
-                }
-                unit = w / rep;
-                gen.useDash("[" + unit + "] 0");
+                float dashWidth = BorderPainter.dashWidthCalculator(w, h);
+                gen.useDash("[" + dashWidth + " " + BorderPainter.DASHED_BORDER_SPACE_RATIO
+                        * dashWidth + "] 0");
                 gen.useLineCap(0);
                 gen.useLineWidth(h);
                 float ym = y1 + (h / 2);
                 drawLine(gen, x1, ym, x2, ym);
             } else {
-                float unit = Math.abs(2 * w);
-                int rep = (int) (h / unit);
-                if (rep % 2 == 0) {
-                    rep++;
-                }
-                unit = h / rep;
-                gen.useDash("[" + unit + "] 0");
+                float dashWidth = BorderPainter.dashWidthCalculator(h, w);
+                gen.useDash("[" + dashWidth + " " + BorderPainter.DASHED_BORDER_SPACE_RATIO
+                        * dashWidth + "] 0");
                 gen.useLineCap(0);
                 gen.useLineWidth(w);
                 float xm = x1 + (w / 2);
index e08a24372ed428190a5c50182c55df3b58b206c7..f6cb335f63e0b2a8e9a6891acb143e1fc7d91aed 100644 (file)
@@ -59,6 +59,9 @@
       documents. Example: the fix of marks layering will be such a case when it's done.
     -->
     <release version="FOP Trunk" date="TBD">
+      <action context="Code" dev="MH" type="add" fixes-bug="45822">
+        Changed the dashed borders to appear more like dashes and less like dots
+      </action>
       <action context="Code" dev="PH" type="add" fixes-bug="54041">
         Added support for Rounded Corners on block areas.
       </action>
index ab5c18c9edf067f5b0c9397989044dfbd838b325..f0e9522d9b7a1918cdf2d1d6c1d870f0f4a3d045 100644 (file)
@@ -23,10 +23,6 @@ import java.io.IOException;
 
 import org.junit.Test;
 
-import org.apache.fop.fo.Constants;
-import org.apache.fop.traits.BorderProps;
-import org.apache.fop.traits.BorderProps.Mode;
-
 import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.doThrow;
@@ -36,6 +32,10 @@ import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyZeroInteractions;
 
+import org.apache.fop.fo.Constants;
+import org.apache.fop.traits.BorderProps;
+import org.apache.fop.traits.BorderProps.Mode;
+
 public class BorderPainterTestCase {
 
     private static final BorderProps BORDER_PROPS = new BorderProps(Constants.EN_SOLID, 10, 50, 50,
diff --git a/test/java/org/apache/fop/render/pdf/PDFBorderPainterTestCase.java b/test/java/org/apache/fop/render/pdf/PDFBorderPainterTestCase.java
new file mode 100644 (file)
index 0000000..5acad98
--- /dev/null
@@ -0,0 +1,47 @@
+package org.apache.fop.render.pdf;
+
+import java.awt.Color;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+import org.apache.fop.fo.Constants;
+import org.apache.fop.pdf.PDFDocument;
+
+public class PDFBorderPainterTestCase {
+
+    private PDFContentGenerator generator;
+    private ByteArrayOutputStream outStream;
+    private PDFGraphicsPainter borderPainter;
+
+    @Before
+    public void setUp() {
+        outStream = new ByteArrayOutputStream();
+        generator = new PDFContentGenerator(new PDFDocument("test"), outStream, null);
+        borderPainter = new PDFGraphicsPainter(generator);
+    }
+
+    /**
+     * This test will fail if either of the below statements isn't true:
+     * org.apache.fop.render.intermediate.BorderPainter.DASHED_BORDER_SPACE_RATIO = 0.5f:q
+     * org.apache.fop.render.intermediate.BorderPainter.DASHED_BORDER_LENGTH_FACTOR = 4.0f.
+     */
+    @Test
+    public void testDrawBorderLine() throws Exception {
+        borderPainter.drawBorderLine(0, 0, 40000, 1000, true, true,
+                Constants.EN_DASHED, Color.BLACK);
+        generator.flushPDFDoc();
+        OutputStream outStream = new ByteArrayOutputStream();
+        outStream = generator.getStream().getBufferOutputStream();
+        assertTrue(((ByteArrayOutputStream) outStream).toString().contains("[4 2] 0 d 1 w"));
+    }
+
+    public void tearDown() {
+        generator = null;
+        outStream= null;
+    }
+}
diff --git a/test/java/org/apache/fop/render/ps/PSBorderPainterTestCase.java b/test/java/org/apache/fop/render/ps/PSBorderPainterTestCase.java
new file mode 100644 (file)
index 0000000..1ffe879
--- /dev/null
@@ -0,0 +1,44 @@
+package org.apache.fop.render.ps;
+
+import java.awt.Color;
+import java.io.ByteArrayOutputStream;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+import org.apache.xmlgraphics.ps.PSGenerator;
+
+import org.apache.fop.fo.Constants;
+
+public class PSBorderPainterTestCase {
+
+    private PSGenerator generator;
+    private ByteArrayOutputStream outStream;
+    private PSGraphicsPainter borderPainter;
+
+    @Before
+    public void setUp() {
+        outStream = new ByteArrayOutputStream();
+        generator = new PSGenerator(outStream);
+        borderPainter = new PSGraphicsPainter(generator);
+    }
+
+    /**
+     * This test will fail if either of the below statements isn't true:
+     * org.apache.fop.render.intermediate.BorderPainter.DASHED_BORDER_SPACE_RATIO = 0.5f:q
+     * org.apache.fop.render.intermediate.BorderPainter.DASHED_BORDER_LENGTH_FACTOR = 4.0f.
+     */
+    @Test
+    public void testDrawBorderLine() throws Exception {
+        borderPainter.drawBorderLine(0, 0, 40000, 1000, true, true,
+                Constants.EN_DASHED, Color.BLACK);
+        assertTrue(outStream.toString().contains("[4.0 2.0] 0 setdash"));
+    }
+
+    public void tearDown() {
+        generator = null;
+        outStream= null;
+    }
+}