diff options
Diffstat (limited to 'src/java/org/apache')
4 files changed, 45 insertions, 37 deletions
diff --git a/src/java/org/apache/fop/afp/AFPBorderPainter.java b/src/java/org/apache/fop/afp/AFPBorderPainter.java index 42c58aa7d..06f1ecd11 100644 --- a/src/java/org/apache/fop/afp/AFPBorderPainter.java +++ b/src/java/org/apache/fop/afp/AFPBorderPainter.java @@ -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; diff --git a/src/java/org/apache/fop/render/intermediate/BorderPainter.java b/src/java/org/apache/fop/render/intermediate/BorderPainter.java index 19e30b50f..d63c4534b 100644 --- a/src/java/org/apache/fop/render/intermediate/BorderPainter.java +++ b/src/java/org/apache/fop/render/intermediate/BorderPainter.java @@ -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 diff --git a/src/java/org/apache/fop/render/pdf/PDFGraphicsPainter.java b/src/java/org/apache/fop/render/pdf/PDFGraphicsPainter.java index ff1ffb5ff..5e0792696 100644 --- a/src/java/org/apache/fop/render/pdf/PDFGraphicsPainter.java +++ b/src/java/org/apache/fop/render/pdf/PDFGraphicsPainter.java @@ -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); } diff --git a/src/java/org/apache/fop/render/ps/PSGraphicsPainter.java b/src/java/org/apache/fop/render/ps/PSGraphicsPainter.java index ab766f701..9152d0937 100644 --- a/src/java/org/apache/fop/render/ps/PSGraphicsPainter.java +++ b/src/java/org/apache/fop/render/ps/PSGraphicsPainter.java @@ -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); |