From: Glen Mazza Date: Thu, 2 Oct 2003 00:32:02 +0000 (+0000) Subject: 1. BodyRegion's clone() now including child areas (makes it a bona fide clone() X-Git-Tag: Root_Temp_KnuthStylePageBreaking~1071 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=37d588c2610f156838e7ae1c0a31f2b05ff80a1b;p=xmlgraphics-fop.git 1. BodyRegion's clone() now including child areas (makes it a bona fide clone() function, also AWTRenderer() needs it.) 2. Starting implementation of RenderWord() in AWTRenderer; also looking at an alternate implementation (storing a series of BufferedImages instead of the area tree elements); temporarily storing both strategies in this commit. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@196941 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/fop/area/BodyRegion.java b/src/java/org/apache/fop/area/BodyRegion.java index e6116bf79..c8b3087c3 100644 --- a/src/java/org/apache/fop/area/BodyRegion.java +++ b/src/java/org/apache/fop/area/BodyRegion.java @@ -159,8 +159,6 @@ public class BodyRegion extends RegionReference { /** * Clone this object. - * This is only used to clone the current object, the child areas - * are assumed to be null and are not cloned. * * @return a shallow copy of this object */ @@ -170,6 +168,9 @@ public class BodyRegion extends RegionReference { br.setIPD(getIPD()); br.columnGap = columnGap; br.columnCount = columnCount; + br.beforeFloat = beforeFloat; + br.mainReference = mainReference; + br.footnote = footnote; return br; } } diff --git a/src/java/org/apache/fop/render/awt/AWTRenderer.java b/src/java/org/apache/fop/render/awt/AWTRenderer.java index 68b52695f..01caff77e 100644 --- a/src/java/org/apache/fop/render/awt/AWTRenderer.java +++ b/src/java/org/apache/fop/render/awt/AWTRenderer.java @@ -85,12 +85,17 @@ import org.apache.fop.area.Page; import org.apache.fop.area.PageViewport; import org.apache.fop.area.RegionViewport; import org.apache.fop.area.Trait; +import org.apache.fop.area.inline.Word; +import org.apache.fop.datatypes.ColorType; import org.apache.fop.fo.FOTreeControl; import org.apache.fop.fo.properties.BackgroundRepeat; +import org.apache.fop.fonts.Typeface; +import org.apache.fop.fonts.FontMetrics; import org.apache.fop.image.FopImage; import org.apache.fop.image.ImageFactory; import org.apache.fop.render.AbstractRenderer; import org.apache.fop.traits.BorderProps; +import org.apache.fop.render.awt.FontMetricsMapper; import org.apache.fop.render.awt.viewer.PreviewDialog; import org.apache.fop.render.awt.viewer.Translator; @@ -101,10 +106,13 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable protected double scaleFactor = 100.0; protected int pageNumber = 0; - protected Vector pageViewportList = new java.util.Vector(); - protected Vector pageList = new java.util.Vector(); - protected BufferedImage currentPageImage = null; - + private int pageWidth = 0; + private int pageHeight = 0; + private Vector pageViewportList = new java.util.Vector(); + private Vector pageList = new java.util.Vector(); + private Vector bufferedImageList = new java.util.Vector(); + private BufferedImage currentPageImage = null; + /** Font configuration */ protected Document fontInfo; @@ -112,33 +120,16 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable The InputHandler associated with this Renderer. Sent to the PreviewDialog for document reloading. */ - protected InputHandler inputHandler; + private InputHandler inputHandler; /** * The resource bundle used for AWT messages. */ protected Translator translator = null; - protected Map fontNames = new java.util.Hashtable(); - protected Map fontStyles = new java.util.Hashtable(); - protected Color saveColor = null; - - /** - * The current (internal) font name - */ - protected String currentFontName; - - /** - * The current font size in millipoints - */ - protected int currentFontSize; - - /** - * The current colour's red, green and blue component - */ - protected float currentRed = 0; - protected float currentGreen = 0; - protected float currentBlue = 0; + private Map fontNames = new java.util.Hashtable(); + private Map fontStyles = new java.util.Hashtable(); + private Color saveColor = null; /** * The preview dialog frame used for display of the documents. @@ -158,6 +149,13 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable createPreviewDialog(null); } + /** + * @see org.apache.fop.render.Renderer + */ + public boolean supportsOutOfOrder() { + return false; + } + public Translator getTranslator() { return translator; } @@ -191,6 +189,7 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable // empty pageViewportList, in case of a reload from PreviewDialog pageViewportList.removeAllElements(); pageList.removeAllElements(); + bufferedImageList.removeAllElements(); System.out.println("\nRegion Types: 0-Before/Top, 1-Start/Left, 2-Body, 3-End/Right, 4-After/Bottom"); } @@ -249,6 +248,11 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable public void renderPage(PageViewport pageViewport) throws IOException, FOPException { pageViewportList.add(pageViewport); pageList.add(pageViewport.getPage().clone()); + bufferedImageList.add(getPageImage(pageViewport)); + } + + public BufferedImage getBufferedPageImage(int pageNum) throws FOPException { + return (BufferedImage) bufferedImageList.get(pageNum); } /** Generates a desired page from the renderer's page viewport vector. @@ -256,6 +260,51 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable * @return the java.awt.image.BufferedImage corresponding to the page * @throws FOPException in case of an out-of-range page number requested */ + public BufferedImage getPageImage(PageViewport pageViewport) throws FOPException { + Page page = pageViewport.getPage(); + + Rectangle2D bounds = pageViewport.getViewArea(); + pageWidth = (int) Math.round(bounds.getWidth() / 1000f ); + pageHeight = (int) Math.round(bounds.getHeight() / 1000f ); +/* + System.out.println("(Page) X, Y, Width, Height: " + bounds.getX() + + " " + bounds.getY() + + " " + bounds.getWidth() + + " " + bounds.getHeight()); +*/ + currentPageImage = + new BufferedImage((int)((pageWidth * (int)scaleFactor) / 100), + (int)((pageHeight * (int)scaleFactor) / 100), + BufferedImage.TYPE_INT_RGB); + + Graphics2D graphics = currentPageImage.createGraphics(); + graphics.setRenderingHint (RenderingHints.KEY_FRACTIONALMETRICS, + RenderingHints.VALUE_FRACTIONALMETRICS_ON); + + // transform page based on scale factor supplied + AffineTransform at = graphics.getTransform(); + at.scale(scaleFactor / 100.0, scaleFactor / 100.0); + graphics.setTransform(at); + + // draw page frame + graphics.setColor(Color.white); + graphics.fillRect(0, 0, pageWidth, pageHeight); + graphics.setColor(Color.black); + graphics.drawRect(-1, -1, pageWidth + 2, pageHeight + 2); + graphics.drawLine(pageWidth + 2, 0, pageWidth + 2, pageHeight + 2); + graphics.drawLine(pageWidth + 3, 1, pageWidth + 3, pageHeight + 3); + graphics.drawLine(0, pageHeight + 2, pageWidth + 2, pageHeight + 2); + graphics.drawLine(1, pageHeight + 3, pageWidth + 3, pageHeight + 3); + + renderPageAreas(page); + return currentPageImage; + } + + /** Generates a desired page from the renderer's page viewport vector. + * @param pageNum the 0-based page number to generate + * @return the java.awt.image.BufferedImage corresponding to the page + * @throws FOPException in case of an out-of-range page number requested + */ public BufferedImage getPageImage(int pageNum) throws FOPException { if (pageNum < 0 || pageNum >= pageViewportList.size()) { throw new FOPException("out-of-range page number (" + pageNum @@ -266,8 +315,8 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable Page page = (Page) pageList.get(pageNum); Rectangle2D bounds = pageViewport.getViewArea(); - int pageWidth = (int) Math.round(bounds.getWidth() / 1000f ); - int pageHeight = (int) Math.round(bounds.getHeight() / 1000f ); + pageWidth = (int) Math.round(bounds.getWidth() / 1000f ); + pageHeight = (int) Math.round(bounds.getHeight() / 1000f ); /* System.out.println("(Page) X, Y, Width, Height: " + bounds.getX() + " " + bounds.getY() @@ -298,8 +347,6 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable graphics.drawLine(0, pageHeight + 2, pageWidth + 2, pageHeight + 2); graphics.drawLine(1, pageHeight + 3, pageWidth + 3, pageHeight + 3); - currentFontName = ""; - currentFontSize = 0; renderPageAreas(page); return currentPageImage; } @@ -311,8 +358,6 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable * @param region the RegionViewport whose region is to be drawn */ protected void handleRegionTraits(RegionViewport region) { - currentFontName = ""; - currentFontSize = 0; Rectangle2D viewArea = region.getViewArea(); int startX = (int) Math.round((viewArea.getX() / 1000f) @@ -329,11 +374,7 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable if (region.getRegion() != null) { System.out.print("\nRegion type = " + region.getRegion().getRegionClass()); } -/* System.out.println("\nView X, Width, Y, Height: " + viewArea.getX() - + " " + viewArea.getWidth() - + " " + viewArea.getY() - + " " + viewArea.getHeight() - ); */ + System.out.println(" X, Width, Y, Height: " + startX + " " + width + " " + startY @@ -391,8 +432,6 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable if (bps != null) { int borderWidth = (int) Math.round((bps.width / 1000f) * (scaleFactor / 100f)); graphics.setColor(bps.color.getAWTColor()); -// System.out.println("Border at (" + startx + "," + (starty + height - borderWidth) + -// ") width, height: (" + (width) + "," + (borderWidth) + ")"); graphics.fillRect(startx, starty, width, borderWidth); } bps = (BorderProps) block.getTrait(Trait.BORDER_AFTER); @@ -417,5 +456,45 @@ public class AWTRenderer extends AbstractRenderer implements Printable, Pageable graphics.fillRect(startx + width - borderWidth, starty, borderWidth, height); } + + } + + /** + * @see org.apache.fop.render.Renderer#renderWord(Word) + */ + public void renderWord(Word word) { + System.out.println("In render word: " + word.getWord()); + + Graphics2D graphics = currentPageImage.createGraphics(); + String fontName = (String) word.getTrait(Trait.FONT_NAME); + int size = ((Integer) word.getTrait(Trait.FONT_SIZE)).intValue(); +// Typeface f = (Typeface) fontInfo.getFonts().get(fontName); + ColorType ct = (ColorType) word.getTrait(Trait.COLOR); + + FontMetricsMapper mapper = (FontMetricsMapper) + fontInfo.getMetricsFor(fontName); + if (mapper == null) { + mapper = new FontMetricsMapper("MonoSpaced", java.awt.Font.PLAIN, + graphics); + } + +// graphics.setColor(ct.getAWTColor()); +// graphics.setFont(mapper.getFont(size)); + graphics.setColor(java.awt.Color.black); + graphics.setFont(new java.awt.Font("monospaced", java.awt.Font.PLAIN, + 10)); + + int rx = currentBlockIPPosition; + int bl = currentBPPosition + word.getOffset(); + + int newx = (int) (rx + 500) / 1000; + int newy = (int) (pageHeight - (bl + 500) / 1000); + + String s = word.getWord(); +// graphics.drawString(s, newx, newy); + graphics.drawString(s, 220, 200); + + // TODO: render text decorations + currentBlockIPPosition += word.getWidth(); } }