From bacc153b5daf6baacb9f1b3b65d8a5ffe7ecc5d1 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Fri, 15 Aug 2008 15:31:55 +0000 Subject: [PATCH] Had to introduce "clip-rect" to handle clipping on background images, leader etc. A full viewport is probably too heavy-weight here. This is to be considered temporary pending verification with SVG and PCL implementations. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@686253 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/render/intermediate/IFConstants.java | 1 + .../render/intermediate/IFGraphicContext.java | 6 ++++ .../fop/render/intermediate/IFPainter.java | 8 +++++ .../fop/render/intermediate/IFParser.java | 13 ++++++++ .../fop/render/intermediate/IFRenderer.java | 30 +++++++++---------- .../fop/render/intermediate/IFSerializer.java | 14 +++++++++ .../org/apache/fop/render/pdf/PDFPainter.java | 27 +++++++++++------ .../fop/render/svg/AbstractSVGPainter.java | 5 ++++ 8 files changed, 80 insertions(+), 24 deletions(-) diff --git a/src/java/org/apache/fop/render/intermediate/IFConstants.java b/src/java/org/apache/fop/render/intermediate/IFConstants.java index 8fe094604..f101ad363 100644 --- a/src/java/org/apache/fop/render/intermediate/IFConstants.java +++ b/src/java/org/apache/fop/render/intermediate/IFConstants.java @@ -44,6 +44,7 @@ public interface IFConstants extends XMLConstants { String EL_VIEWPORT = "viewport"; String EL_GROUP = "g"; String EL_IMAGE = "image"; + String EL_CLIP_RECT = "clip-rect"; String EL_RECT = "rect"; String EL_BORDER_RECT = "border-rect"; String EL_FONT = "font"; diff --git a/src/java/org/apache/fop/render/intermediate/IFGraphicContext.java b/src/java/org/apache/fop/render/intermediate/IFGraphicContext.java index 6183bbbad..7250dcc39 100644 --- a/src/java/org/apache/fop/render/intermediate/IFGraphicContext.java +++ b/src/java/org/apache/fop/render/intermediate/IFGraphicContext.java @@ -31,6 +31,8 @@ import org.apache.xmlgraphics.java2d.GraphicContext; */ public class IFGraphicContext extends GraphicContext { + private static final AffineTransform[] EMPTY_TRANSFORM_ARRAY = new AffineTransform[0]; + private List groupList = new java.util.ArrayList(); /** @@ -88,6 +90,10 @@ public class IFGraphicContext extends GraphicContext { this(new AffineTransform[] {transform}); } + public Group() { + this(EMPTY_TRANSFORM_ARRAY); + } + public AffineTransform[] getTransforms() { return this.transforms; } diff --git a/src/java/org/apache/fop/render/intermediate/IFPainter.java b/src/java/org/apache/fop/render/intermediate/IFPainter.java index 6042ed816..4722a02ac 100644 --- a/src/java/org/apache/fop/render/intermediate/IFPainter.java +++ b/src/java/org/apache/fop/render/intermediate/IFPainter.java @@ -268,6 +268,14 @@ public interface IFPainter { */ void drawText(int x, int y, int[] dx, int[] dy, String text) throws IFException; + /** + * Restricts the current clipping region with the given rectangle. + * @param rect the rectangle's coordinates and extent + * @throws IFException if an error occurs while handling this event + */ + void clipRect(Rectangle rect) throws IFException; + //TODO clipRect() shall be considered temporary until verified with SVG and PCL + /** * Draws a rectangle. Either fill or stroke has to be specified. * @param rect the rectangle's coordinates and extent diff --git a/src/java/org/apache/fop/render/intermediate/IFParser.java b/src/java/org/apache/fop/render/intermediate/IFParser.java index e365bb519..50c061ff4 100644 --- a/src/java/org/apache/fop/render/intermediate/IFParser.java +++ b/src/java/org/apache/fop/render/intermediate/IFParser.java @@ -136,6 +136,7 @@ public class IFParser implements IFConstants { elementHandlers.put(EL_GROUP, new GroupHandler()); elementHandlers.put(EL_FONT, new FontHandler()); elementHandlers.put(EL_TEXT, new TextHandler()); + elementHandlers.put(EL_CLIP_RECT, new ClipRectHandler()); elementHandlers.put(EL_RECT, new RectHandler()); elementHandlers.put(EL_BORDER_RECT, new BorderRectHandler()); elementHandlers.put(EL_IMAGE, new ImageHandler()); @@ -441,6 +442,18 @@ public class IFParser implements IFConstants { } + private class ClipRectHandler extends AbstractElementHandler { + + public void startElement(Attributes attributes) throws IFException { + int x = Integer.parseInt(attributes.getValue("x")); + int y = Integer.parseInt(attributes.getValue("y")); + int width = Integer.parseInt(attributes.getValue("width")); + int height = Integer.parseInt(attributes.getValue("height")); + painter.clipRect(new Rectangle(x, y, width, height)); + } + + } + private class RectHandler extends AbstractElementHandler { public void startElement(Attributes attributes) throws IFException { diff --git a/src/java/org/apache/fop/render/intermediate/IFRenderer.java b/src/java/org/apache/fop/render/intermediate/IFRenderer.java index 19c616df1..0b74987b5 100644 --- a/src/java/org/apache/fop/render/intermediate/IFRenderer.java +++ b/src/java/org/apache/fop/render/intermediate/IFRenderer.java @@ -584,12 +584,12 @@ public class IFRenderer extends AbstractPathOrientedRenderer { /** {@inheritDoc} */ protected void concatenateTransformationMatrix(AffineTransform at) { if (!at.isIdentity()) { - concatenateTransformationMatrixMpt(ptToMpt(at)); + concatenateTransformationMatrixMpt(ptToMpt(at), false); } } - private void concatenateTransformationMatrixMpt(AffineTransform at) { - if (!at.isIdentity()) { + private void concatenateTransformationMatrixMpt(AffineTransform at, boolean force) { + if (force || !at.isIdentity()) { if (log.isTraceEnabled()) { log.trace("-----concatenateTransformationMatrix: " + at); } @@ -659,7 +659,7 @@ public class IFRenderer extends AbstractPathOrientedRenderer { saveGraphicsState(); //Viewport position - concatenateTransformationMatrixMpt(positionTransform); + concatenateTransformationMatrixMpt(positionTransform, false); //Background and borders float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd()); @@ -670,7 +670,7 @@ public class IFRenderer extends AbstractPathOrientedRenderer { //Shift to content rectangle after border painting AffineTransform contentRectTransform = new AffineTransform(); contentRectTransform.translate(borderPaddingStart, borderPaddingBefore); - concatenateTransformationMatrixMpt(contentRectTransform); + concatenateTransformationMatrixMpt(contentRectTransform, false); //Clipping Rectangle clipRect = null; @@ -994,20 +994,22 @@ public class IFRenderer extends AbstractPathOrientedRenderer { /** {@inheritDoc} */ protected void clip() { - // TODO Auto-generated method stub - log.warn("clip() NYI"); + throw new IllegalStateException("Not used"); } /** {@inheritDoc} */ protected void clipRect(float x, float y, float width, float height) { - // TODO Auto-generated method stub - log.warn("clipRect() NYI"); + pushGroup(new IFGraphicContext.Group()); + try { + painter.clipRect(toMillipointRectangle(x, y, width, height)); + } catch (IFException ife) { + handleIFException(ife); + } } /** {@inheritDoc} */ protected void closePath() { - // TODO Auto-generated method stub - log.warn("closePath() NYI"); + throw new IllegalStateException("Not used"); } /** {@inheritDoc} */ @@ -1056,14 +1058,12 @@ public class IFRenderer extends AbstractPathOrientedRenderer { /** {@inheritDoc} */ protected void moveTo(float x, float y) { - // TODO Auto-generated method stub - log.warn("moveTo() NYI"); + throw new IllegalStateException("Not used"); } /** {@inheritDoc} */ protected void lineTo(float x, float y) { - // TODO Auto-generated method stub - log.warn("lineTo() NYI"); + throw new IllegalStateException("Not used"); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/render/intermediate/IFSerializer.java b/src/java/org/apache/fop/render/intermediate/IFSerializer.java index 40cd9a25b..4eb7f0713 100644 --- a/src/java/org/apache/fop/render/intermediate/IFSerializer.java +++ b/src/java/org/apache/fop/render/intermediate/IFSerializer.java @@ -349,6 +349,20 @@ public class IFSerializer extends AbstractXMLWritingIFPainter implements IFConst } } + /** {@inheritDoc} */ + public void clipRect(Rectangle rect) throws IFException { + try { + AttributesImpl atts = new AttributesImpl(); + addAttribute(atts, "x", Integer.toString(rect.x)); + addAttribute(atts, "y", Integer.toString(rect.y)); + addAttribute(atts, "width", Integer.toString(rect.width)); + addAttribute(atts, "height", Integer.toString(rect.height)); + element(EL_CLIP_RECT, atts); + } catch (SAXException e) { + throw new IFException("SAX error in clipRect()", e); + } + } + /** {@inheritDoc} */ public void drawRect(Rectangle rect, Paint fill, Color stroke) throws IFException { if (fill == null && stroke == null) { diff --git a/src/java/org/apache/fop/render/pdf/PDFPainter.java b/src/java/org/apache/fop/render/pdf/PDFPainter.java index 2d1399814..0ddfd8324 100644 --- a/src/java/org/apache/fop/render/pdf/PDFPainter.java +++ b/src/java/org/apache/fop/render/pdf/PDFPainter.java @@ -241,12 +241,7 @@ public class PDFPainter extends AbstractBinaryWritingIFPainter { generator.saveGraphicsState(); generator.concatenate(generator.toPoints(transform)); if (clipRect != null) { - StringBuffer sb = new StringBuffer(); - sb.append(format(clipRect.x)).append(' '); - sb.append(format(clipRect.y)).append(' '); - sb.append(format(clipRect.width)).append(' '); - sb.append(format(clipRect.height)).append(" re W n\n"); - generator.add(sb.toString()); + clipRect(clipRect); } } @@ -329,6 +324,17 @@ public class PDFPainter extends AbstractBinaryWritingIFPainter { return PDFNumber.doubleOut(value / 1000f); } + /** {@inheritDoc} */ + public void clipRect(Rectangle rect) throws IFException { + generator.endTextObject(); + StringBuffer sb = new StringBuffer(); + sb.append(format(rect.x)).append(' '); + sb.append(format(rect.y)).append(' '); + sb.append(format(rect.width)).append(' '); + sb.append(format(rect.height)).append(" re W n\n"); + generator.add(sb.toString()); + } + /** {@inheritDoc} */ public void drawRect(Rectangle rect, Paint fill, Color stroke) throws IFException { if (fill == null && stroke == null) { @@ -365,8 +371,10 @@ public class PDFPainter extends AbstractBinaryWritingIFPainter { /** {@inheritDoc} */ public void drawBorderRect(Rectangle rect, BorderProps before, BorderProps after, BorderProps start, BorderProps end) throws IFException { - generator.endTextObject(); - this.borderPainter.drawBorders(rect, before, after, start, end); + if (before != null || after != null || start != null || end != null) { + generator.endTextObject(); + this.borderPainter.drawBorders(rect, before, after, start, end); + } } private Typeface getTypeface(String fontName) { @@ -387,6 +395,7 @@ public class PDFPainter extends AbstractBinaryWritingIFPainter { FontTriplet triplet = new FontTriplet( state.getFontFamily(), state.getFontStyle(), state.getFontWeight()); //TODO Ignored: state.getFontVariant() + //TODO Opportunity for font caching if font state is more heavily used String fontKey = fontInfo.getInternalFontKey(triplet); int sizeMillipoints = state.getFontSize(); float fontSize = sizeMillipoints / 1000f; @@ -431,7 +440,7 @@ public class PDFPainter extends AbstractBinaryWritingIFPainter { if (CharUtilities.isFixedWidthSpace(orgChar)) { //Fixed width space are rendered as spaces so copy/paste works in a reader ch = font.mapChar(CharUtilities.SPACE); - glyphAdjust = font.getCharWidth(ch) - font.getCharWidth(orgChar); + glyphAdjust = -(font.getCharWidth(ch) - font.getCharWidth(orgChar)); } else { ch = font.mapChar(orgChar); } diff --git a/src/sandbox/org/apache/fop/render/svg/AbstractSVGPainter.java b/src/sandbox/org/apache/fop/render/svg/AbstractSVGPainter.java index e32b9f937..80fcde0df 100644 --- a/src/sandbox/org/apache/fop/render/svg/AbstractSVGPainter.java +++ b/src/sandbox/org/apache/fop/render/svg/AbstractSVGPainter.java @@ -278,6 +278,11 @@ public abstract class AbstractSVGPainter extends AbstractXMLWritingIFPainter } } + /** {@inheritDoc} */ + public void clipRect(Rectangle rect) throws IFException { + //TODO Implement me!!! + } + /** {@inheritDoc} */ public void drawRect(Rectangle rect, Paint fill, Color stroke) throws IFException { if (fill == null && stroke == null) { -- 2.39.5