package org.apache.fop.area;
+import java.awt.Rectangle;
+
/**
* A BlockViewport.
* This is used for block level Viewport/reference pairs.
* The block-container creates this area.
*/
-public class BlockViewport extends Block {
+public class BlockViewport extends Block implements Viewport {
private static final long serialVersionUID = -7840580922580735157L;
clip = cl;
}
- /**
- * Get the clipping for this viewport.
- *
- * @return the clipping for the viewport
- * true if the contents should be clipped for this viewport
- */
- public boolean getClip() {
+ public boolean hasClip() {
return clip;
}
+
+ public Rectangle getClipRectangle() {
+ if (clip) {
+ return new Rectangle(getIPD(), getBPD());
+ } else {
+ return null;
+ }
+ }
}
package org.apache.fop.area;
+import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.util.HashMap;
* region-reference-area as its child. These areas are described
* in the fo:region-body description in the XSL Recommendation.
*/
-public class RegionViewport extends Area implements Cloneable {
+public class RegionViewport extends Area implements Cloneable, Viewport {
private static final long serialVersionUID = 505781815165102572L;
clip = c;
}
- /** @return true if the viewport should be clipped. */
- public boolean isClip() {
+ public boolean hasClip() {
return this.clip;
}
+ public Rectangle getClipRectangle() {
+ if (clip) {
+ return new Rectangle(getIPD(), getBPD());
+ } else {
+ return null;
+ }
+ }
+
/**
* Get the view area of this viewport.
*
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.area;
+
+import java.awt.Rectangle;
+
+/**
+ * A viewport-area that may clip its content.
+ */
+public interface Viewport {
+
+ /**
+ * Returns true if this area will clip overflowing content.
+ *
+ * @return {@code true} if the overflow trait has the value "hidden", "scroll" or
+ * "error-if-overflow"
+ */
+ boolean hasClip();
+
+ /**
+ * Returns the clipping rectangle of this viewport area.
+ *
+ * @return the clipping rectangle expressed in the viewport's coordinate system, or
+ * null if clipping is disabled
+ */
+ Rectangle getClipRectangle();
+}
package org.apache.fop.area.inline;
-import org.apache.fop.area.Area;
-
-import java.io.IOException;
+import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
+import java.io.IOException;
import java.util.HashMap;
+import org.apache.fop.area.Area;
+
/**
* Inline viewport area.
* This is an inline-level viewport area for inline container,
* external graphic and instream foreign object. This viewport
* holds the area and positions it.
*/
-public class Viewport extends InlineArea {
+public class Viewport extends InlineArea implements org.apache.fop.area.Viewport {
private static final long serialVersionUID = 813338534627918689L;
this.clip = c;
}
- /**
- * Get the clip of this viewport.
- *
- * @return true if this viewport should clip
- */
- public boolean getClip() {
+ public boolean hasClip() {
return this.clip;
}
+ public Rectangle getClipRectangle() {
+ if (clip) {
+ return new Rectangle(getIPD(), getBPD());
+ } else {
+ return null;
+ }
+ }
+
/**
* Set the position and size of the content of this viewport.
*
}
//Clipping
- if (bv.getClip()) {
+ if (bv.hasClip()) {
clipRect(0f, 0f, width, height);
}
//Now adjust for border/padding
currentBPPosition += borderPaddingBefore;
- Rectangle2D clippingRect = null;
- if (bv.getClip()) {
+ Rectangle clippingRect = null;
+ if (bv.hasClip()) {
clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
bv.getIPD(), bv.getBPD());
}
drawBackAndBorders(viewport, x, y, width + bpwidth, height + bpheight);
- if (viewport.getClip()) {
+ if (viewport.hasClip()) {
saveGraphicsState();
clipRect(x + borderPaddingStart, y + borderPaddingBefore, width, height);
}
super.renderViewport(viewport);
- if (viewport.getClip()) {
+ if (viewport.hasClip()) {
restoreGraphicsState();
}
}
* @param port The region viewport to be rendered
*/
protected void renderRegionViewport(RegionViewport port) {
- Rectangle2D view = port.getViewArea();
// The CTM will transform coordinates relative to
// this region-reference area into page coords, so
// set origin for the region to 0,0.
handleRegionTraits(port);
// shouldn't the viewport have the CTM
- startVParea(regionReference.getCTM(), port.isClip() ? view : null);
+ startVParea(regionReference.getCTM(), port.getClipRectangle());
// do after starting viewport area
if (regionReference.getRegionClass() == FO_REGION_BODY) {
renderBodyRegion((BodyRegion) regionReference);
* @param clippingRect the clipping rectangle if the viewport should be clipping,
* null if no clipping is performed.
*/
- protected abstract void startVParea(CTM ctm, Rectangle2D clippingRect);
+ protected abstract void startVParea(CTM ctm, Rectangle clippingRect);
/**
* Signals exit from a viewport area. Subclasses can restore transformation matrices
int saveIP = currentIPPosition;
int saveBP = currentBPPosition;
- Rectangle2D clippingRect = null;
- if (bv.getClip()) {
+ Rectangle clippingRect = null;
+ if (bv.hasClip()) {
clippingRect = new Rectangle(saveIP, saveBP, bv.getIPD(), bv.getBPD());
}
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
-
import org.xml.sax.SAXException;
import org.apache.batik.parser.AWTTransformProducer;
contentRectTransform.translate(borderPaddingStart, borderPaddingBefore);
concatenateTransformationMatrixMpt(contentRectTransform, false);
- //Clipping
- Rectangle clipRect = null;
- if (bv.getClip()) {
- clipRect = new Rectangle(0, 0, dim.width, dim.height);
- //clipRect(0f, 0f, width, height);
- }
-
//saveGraphicsState();
//Set up coordinate system for content rectangle
AffineTransform contentTransform = ctm.toAffineTransform();
//concatenateTransformationMatrixMpt(contentTransform);
- startViewport(contentTransform, clipRect);
+ startViewport(contentTransform, bv.getClipRectangle());
currentIPPosition = 0;
currentBPPosition = 0;
//Now adjust for border/padding
currentBPPosition += borderPaddingBefore;
- Rectangle2D clippingRect = null;
- if (bv.getClip()) {
- clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
- bv.getIPD(), bv.getBPD());
- }
-
- startVParea(ctm, clippingRect);
+ startVParea(ctm, bv.getClipRectangle());
currentIPPosition = 0;
currentBPPosition = 0;
renderBlocks(bv, children);
}
/** {@inheritDoc} */
- protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
+ protected void startVParea(CTM ctm, Rectangle clippingRect) {
if (log.isTraceEnabled()) {
log.trace("startVParea() ctm=" + ctm + ", clippingRect=" + clippingRect);
}
AffineTransform at = new AffineTransform(ctm.toArray());
- Rectangle clipRect = null;
- if (clippingRect != null) {
- clipRect = new Rectangle(
- (int)clippingRect.getMinX() - currentIPPosition,
- (int)clippingRect.getMinY() - currentBPPosition,
- (int)clippingRect.getWidth(), (int)clippingRect.getHeight());
- }
- startViewport(at, clipRect);
+ startViewport(at, clippingRect);
if (log.isTraceEnabled()) {
log.trace("startVPArea: " + at + " --> " + graphicContext.getTransform());
}
}
/** {@inheritDoc} */
- protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
+ protected void startVParea(CTM ctm, Rectangle clippingRect) {
saveGraphicsState();
import java.awt.Color;
import java.awt.Point;
+import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
/**
* {@inheritDoc}
*/
- protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
+ protected void startVParea(CTM ctm, Rectangle clippingRect) {
currentState.push(ctm);
}
// Java
import java.awt.Color;
+import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.OutputStream;
addAreaAttributes(port);
addTraitAttributes(port);
addAttribute("rect", port.getViewArea());
- if (port.isClip()) {
+ if (port.hasClip()) {
addAttribute("clipped", "true");
}
startElement("regionViewport", atts);
}
}
- /** {@inheritDoc} */
@Override
- protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
+ protected void startVParea(CTM ctm, Rectangle clippingRect) {
//only necessary for graphical output
}
addAttribute("top-position", bvp.getYOffset());
}
addAttribute("ctm", bvp.getCTM().toString());
- if (bvp.getClip()) {
+ if (bvp.hasClip()) {
addAttribute("clipped", "true");
}
} else {
addTraitAttributes(viewport);
addAttribute("offset", viewport.getOffset());
addAttribute("pos", viewport.getContentPosition());
- if (viewport.getClip()) {
+ if (viewport.hasClip()) {
addAttribute("clip", "true");
}
startElement("viewport", atts);
import junit.framework.Test;
import junit.framework.TestSuite;
+import org.apache.fop.area.ViewportTestSuite;
import org.apache.fop.fonts.DejaVuLGCSerifTest;
import org.apache.fop.image.loader.batik.ImageLoaderTestCase;
import org.apache.fop.image.loader.batik.ImagePreloaderTestCase;
suite.addTest(new TestSuite(PageBoundariesTest.class));
suite.addTest(new TestSuite(PageScaleTest.class));
suite.addTest(new TestSuite(MinOptMaxTest.class));
+ suite.addTest(ViewportTestSuite.suite());
//$JUnit-END$
return suite;
}
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.area;
+
+
+/**
+ * Tests the {@linkplain BlockViewport} class.
+ */
+public class BlockViewportTestCase extends ViewportTestCase {
+
+ public void testNonClip() throws Exception {
+ BlockViewport bv = new BlockViewport();
+ bv.setIPD(100);
+ bv.setBPD(50);
+ checkNonClip(bv);
+ }
+
+ public void testClip() throws Exception {
+ BlockViewport bv = new BlockViewport();
+ int ipd = 100;
+ int bpd = 50;
+ bv.setIPD(ipd);
+ bv.setBPD(bpd);
+ bv.setClip(true);
+ checkClip(bv, ipd, bpd);
+ }
+}
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.area;
+
+import java.awt.Rectangle;
+import java.awt.geom.Rectangle2D;
+
+/**
+ * Tests the {@linkplain RegionViewport} class.
+ */
+public class RegionViewportTestCase extends ViewportTestCase {
+
+ private RegionViewport createRegionViewport(int x, int y, int ipd, int bpd) {
+ Rectangle2D v = new Rectangle(x, y, ipd, bpd);
+ RegionViewport viewport = new RegionViewport(v);
+ viewport.setIPD(ipd);
+ viewport.setBPD(bpd);
+ return viewport;
+ }
+
+ public void testNonClip() throws Exception {
+ RegionViewport viewport = createRegionViewport(10, 10, 100, 20);
+ checkNonClip(viewport);
+ }
+
+ public void testClip() throws Exception {
+ int ipd = 150;
+ int bpd = 20;
+ RegionViewport viewport = createRegionViewport(10, 10, ipd, bpd);
+ viewport.setClip(true);
+ checkClip(viewport, ipd, bpd);
+ }
+}
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.area;
+
+import java.awt.Rectangle;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests implementations of the {@linkplain Viewport} interface.
+ */
+public abstract class ViewportTestCase extends TestCase {
+
+ protected void checkNonClip(Viewport v) throws Exception {
+ assertFalse(v.hasClip());
+ assertNull(v.getClipRectangle());
+ }
+
+ protected void checkClip(Viewport v, int expectedWidth, int expectedHeight) throws Exception {
+ assertTrue(v.hasClip());
+ assertEquals(new Rectangle(0, 0, expectedWidth, expectedHeight), v.getClipRectangle());
+ }
+}
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.area;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.fop.area.inline.InlineViewportTestCase;
+
+/**
+ * A suite of all the tests relating to the {@linkplain Viewport} interface.
+ */
+public final class ViewportTestSuite {
+
+ private ViewportTestSuite() { }
+
+ /**
+ * Returns the suite of {@linkplain Viewport} implementation tests.
+ *
+ * @return the tests
+ */
+ public static Test suite() {
+ TestSuite suite = new TestSuite("Tests for viewport-areas");
+ suite.addTestSuite(RegionViewportTestCase.class);
+ suite.addTestSuite(BlockViewportTestCase.class);
+ suite.addTestSuite(InlineViewportTestCase.class);
+ return suite;
+ }
+
+}
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.area.inline;
+
+import org.apache.fop.area.ViewportTestCase;
+
+/**
+ * Tests the {@linkplain Viewport} class.
+ */
+public class InlineViewportTestCase extends ViewportTestCase {
+
+ public void testNonClip() throws Exception {
+ Viewport v = new Viewport(null);
+ v.setIPD(50);
+ v.setBPD(25);
+ checkNonClip(v);
+ }
+
+ public void testClip() throws Exception {
+ Viewport v = new Viewport(null);
+ int ipd = 50;
+ int bpd = 25;
+ v.setIPD(ipd);
+ v.setBPD(bpd);
+ v.setClip(true);
+ checkClip(v, ipd, bpd);
+ }
+
+}