123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- /*-- $Id$ --
-
- ============================================================================
- The Apache Software License, Version 1.1
- ============================================================================
-
- Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
-
- Redistribution and use in source and binary forms, with or without modifica-
- tion, are permitted provided that the following conditions are met:
-
- 1. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
-
- 3. The end-user documentation included with the redistribution, if any, must
- include the following acknowledgment: "This product includes software
- developed by the Apache Software Foundation (http://www.apache.org/)."
- Alternately, this acknowledgment may appear in the software itself, if
- and wherever such third-party acknowledgments normally appear.
-
- 4. The names "FOP" and "Apache Software Foundation" must not be used to
- endorse or promote products derived from this software without prior
- written permission. For written permission, please contact
- apache@apache.org.
-
- 5. Products derived from this software may not be called "Apache", nor may
- "Apache" appear in their name, without prior written permission of the
- Apache Software Foundation.
-
- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- This software consists of voluntary contributions made by many individuals
- on behalf of the Apache Software Foundation and was originally created by
- James Tauber <jtauber@jtauber.com>. For more information on the Apache
- Software Foundation, please see <http://www.apache.org/>.
-
- */
-
- // Author : Seshadri G
-
-
-
-
- package org.apache.fop.render.mif;
-
- // FOP
- import org.apache.fop.render.Renderer;
- import org.apache.fop.messaging.MessageHandler;
- import org.apache.fop.image.ImageArea;
- import org.apache.fop.image.FopImage;
- import org.apache.fop.apps.FOPException;
- import org.apache.fop.fo.properties.*;
- import org.apache.fop.fo.*;
- import org.apache.fop.layout.*;
- import org.apache.fop.layout.inline.*;
- import org.apache.fop.datatypes.*;
- import org.apache.fop.svg.*;
- import org.apache.fop.mif.*;
- import org.apache.fop.layout.*;
- import org.apache.fop.image.*;
-
- import org.w3c.dom.*;
- import org.w3c.dom.svg.*;
- import org.w3c.dom.css.*;
- import org.w3c.dom.svg.SVGLength;
-
- // Java
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import java.awt.Rectangle;
- import java.util.Vector;
- import java.util.Hashtable;
- /**
- * Renderer that renders areas to MIF
- */
- public class MIFRenderer implements Renderer {
-
- private String currentFontName;
- private String currentFontSize;
- private int pageHeight;
- private int pageWidth;
-
- /** the current vertical position in millipoints from bottom */
- protected int currentYPosition = 0;
-
- /** the current horizontal position in millipoints from left */
- protected int currentXPosition = 0;
-
- /** the horizontal position of the current area container */
- private int currentAreaContainerXPosition = 0;
-
-
- /** the MIF Document being created */
- protected MIFDocument mifDoc;
-
-
- /* is a table currently open? */
- private boolean inTable=false;
-
- /** options */
- protected Hashtable options;
-
- /**
- * create the MIF renderer
- */
- public MIFRenderer() {
- this.mifDoc = new MIFDocument();
- }
-
- /** set up renderer options */
- public void setOptions(Hashtable options) {
- this.options = options;
- }
-
- /**
- * render the areas into MIF
- *
- * @param areaTree the laid-out area tree
- * @param writer the PrintWriter to write the MIF with
- */
-
- public void render(AreaTree areaTree,
- OutputStream stream) throws IOException, FOPException {
-
- MessageHandler.logln("rendering areas to MIF");
- // idReferences=areaTree.getIDReferences();
- //this.pdfResources = this.pdfDoc.getResources();
- //this.pdfDoc.setIDReferences(idReferences);
- Enumeration e = areaTree.getPages().elements();
- while (e.hasMoreElements()) {
- this.renderPage((Page) e.nextElement());
- }
-
-
- // MessageHandler.logln("writing out MIF");
-
- this.mifDoc.output(stream);
- stream.close();
- }
-
-
-
-
- /** set up the given FontInfo */
- public void setupFontInfo(FontInfo fontInfo) {
-
- FontSetup.setup(fontInfo);
- //FontSetup.addToFontFormat(this.mifDoc, fontInfo);
-
-
- }
-
- /** set the producer of the rendering */
- public void setProducer(String producer) {}
-
-
- public void renderAreaContainer(AreaContainer area) {
-
- if (area.foCreator != null && area.foCreator.getName() == "fo:table") {
-
- this.mifDoc.createTable();
- this.inTable=true;
- }
- else
- if (area.foCreator != null && area.foCreator.getName() == "fo:table-body") {
-
- this.mifDoc.setCurrent("fo:table-body");
- }
- else
- if (area.foCreator != null && area.foCreator.getName() == "fo:table-column") {
-
- int colWidth=((org.apache.fop.fo.flow.TableColumn) area.foCreator).getColumnWidth();
- this.mifDoc.setColumnProp(colWidth);
- }
- else
- if (area.foCreator != null && area.foCreator.getName() == "fo:table-row") {
-
- this.mifDoc.startRow();
- }
- else
- if (area.foCreator != null && area.foCreator.getName() == "fo:table-cell") {
-
- int rowSpan=((org.apache.fop.fo.flow.TableCell) area.foCreator).getNumRowsSpanned();
- int colSpan=((org.apache.fop.fo.flow.TableCell) area.foCreator).getNumColumnsSpanned();
- this.mifDoc.startCell(rowSpan,colSpan);
- }
- else
- if (inTable) {
-
- inTable=false;
- this.mifDoc.endTable();
-
- }
- int saveY = this.currentYPosition;
- int saveX = this.currentAreaContainerXPosition;
-
- if (area.getPosition() == Position.ABSOLUTE) {
- // Y position is computed assuming positive Y axis, adjust for negative postscript one
- this.currentYPosition =area.getYPosition() - 2 * area.getPaddingTop() - 2 * area.getBorderTopWidth();
-
- this.currentAreaContainerXPosition = area.getXPosition();
- } else if (area.getPosition() == Position.RELATIVE) {
-
- this.currentYPosition -= area.getYPosition();
- this.currentAreaContainerXPosition += area.getXPosition();
-
- } else if (area.getPosition() == Position.STATIC) {
-
- this.currentYPosition -=
- area.getPaddingTop() + area.getBorderTopWidth();
- this.currentAreaContainerXPosition +=area.getPaddingLeft() + area.getBorderLeftWidth();
- }
-
- this.currentXPosition = this.currentAreaContainerXPosition;
- doFrame(area);
-
- Enumeration e = area.getChildren().elements();
- while (e.hasMoreElements()) {
- Box b = (Box) e.nextElement();
- b.render(this);
- }
- if (area.getPosition() != Position.STATIC) {
- this.currentYPosition = saveY;
- this.currentAreaContainerXPosition = saveX;
- } else
- this.currentYPosition -= area.getHeight();
- }
-
- public void renderBodyAreaContainer(BodyAreaContainer area) {
-
-
- int saveY = this.currentYPosition;
- int saveX = this.currentAreaContainerXPosition;
-
- if (area.getPosition() == Position.ABSOLUTE) {
- // Y position is computed assuming positive Y axis, adjust for negative postscript one
- this.currentYPosition = area.getYPosition();
- this.currentAreaContainerXPosition = area.getXPosition();
- } else if (area.getPosition() == Position.RELATIVE) {
- this.currentYPosition -= area.getYPosition();
- this.currentAreaContainerXPosition += area.getXPosition();
- }
-
- this.currentXPosition = this.currentAreaContainerXPosition;
- int w, h;
- int rx = this.currentAreaContainerXPosition;
- w = area.getContentWidth();
- h = area.getContentHeight();
- int ry = this.currentYPosition;
- ColorType bg = area.getBackgroundColor();
-
- /*
-
- // I'm not sure I should have to check for bg being null
- // but I do
- if ((bg != null) && (bg.alpha() == 0)) {
- this.addRect(rx, ry, w, -h, new PDFColor(bg), new PDFColor(bg));
- }
-
- */
- /*
-
- // floats & footnotes stuff
- renderAreaContainer(area.getBeforeFloatReferenceArea());
- renderAreaContainer(area.getFootnoteReferenceArea());
-
- */
- // main reference area
- Enumeration e = area.getMainReferenceArea().getChildren().elements();
- while (e.hasMoreElements()) {
- Box b = (Box) e.nextElement();
- b.render(this); // span areas
- }
-
- if (area.getPosition() != Position.STATIC) {
- this.currentYPosition = saveY;
- this.currentAreaContainerXPosition = saveX;
- } else
- this.currentYPosition -= area.getHeight();
-
- }
-
-
- private void doFrame(Area area) {
- int w, h;
- int rx = this.currentAreaContainerXPosition;
- w = area.getContentWidth();
-
- if (area instanceof BlockArea)
- rx += ((BlockArea)area).getStartIndent();
-
- h = area.getContentHeight();
- int ry = this.currentYPosition;
- ColorType bg = area.getBackgroundColor();
-
- rx = rx - area.getPaddingLeft();
- ry = ry + area.getPaddingTop();
- w = w + area.getPaddingLeft() + area.getPaddingRight();
- h = h + area.getPaddingTop() + area.getPaddingBottom();
-
- /*
- // I'm not sure I should have to check for bg being null
- // but I do
- if ((bg != null) && (bg.alpha() == 0)) {
- this.addRect(rx, ry, w, -h,
- new PDFColor(bg),
- new PDFColor(bg));
- }
-
- */
-
- rx = rx - area.getBorderLeftWidth();
- ry = ry + area.getBorderTopWidth();
- w = w + area.getBorderLeftWidth() + area.getBorderRightWidth();
- h = h + area.getBorderTopWidth() + area.getBorderBottomWidth();
-
- //Create a textrect with these dimensions.
- //The y co-ordinate is measured +ve downwards so subtract page-height
-
- this.mifDoc.setTextRectProp(rx,pageHeight-ry,w,h);
-
- /*
- BorderAndPadding bp = area.getBorderAndPadding();
- if (area.getBorderTopWidth() != 0)
- addLine(rx, ry, rx + w, ry, area.getBorderTopWidth(),
- new PDFColor(bp.getBorderColor(BorderAndPadding.TOP)));
- if (area.getBorderLeftWidth() != 0)
- addLine(rx, ry, rx, ry - h, area.getBorderLeftWidth(),
- new PDFColor(bp.getBorderColor(BorderAndPadding.LEFT)));
- if (area.getBorderRightWidth() != 0)
- addLine(rx + w, ry, rx + w, ry - h, area.getBorderRightWidth(),
- new PDFColor(bp.getBorderColor(BorderAndPadding.RIGHT)));
- if (area.getBorderBottomWidth() != 0)
- addLine(rx, ry - h, rx + w, ry - h, area.getBorderBottomWidth(),
- new PDFColor(bp.getBorderColor(BorderAndPadding.BOTTOM)));
- */
- }
-
- public void renderSpanArea(SpanArea area) {
-
- //A span maps to a textframe
-
-
- this.mifDoc.createTextRect(area.getColumnCount());
-
- Enumeration e = area.getChildren().elements();
- while (e.hasMoreElements()) {
- Box b = (Box) e.nextElement();
- b.render(this); // column areas
- }
- }
-
-
-
-
-
-
- /** render the given block area */
- public void renderBlockArea(BlockArea area) {
-
- this.mifDoc.setBlockProp(area.getStartIndent(),area.getEndIndent());
- Enumeration e = area.getChildren().elements();
- while (e.hasMoreElements()) {
- Box b = (Box) e.nextElement();
- b.render(this);
- }
-
- }
-
- /** render the given display space */
- public void renderDisplaySpace(DisplaySpace space) {
-
- int d = space.getSize();
- this.currentYPosition -= d;
-
- }
-
- /** render the given SVG area */
- public void renderSVGArea(SVGArea area) {}
-
- /** render a foreign object area */
- public void renderForeignObjectArea(ForeignObjectArea area) {
- }
-
- public void renderWordArea(WordArea area) {
- String s;
- s = area.getText();
- this.mifDoc.addToStream(s);
-
- this.currentXPosition += area.getContentWidth();
- }
-
- /** render the given image area */
- public void renderImageArea(ImageArea area) {
-
- int x = this.currentAreaContainerXPosition + area.getXOffset();
- int y = this.currentYPosition;
- int w = area.getContentWidth();
- int h = area.getHeight();
-
- this.currentYPosition -= h;
-
- FopImage img = area.getImage();
- if (img instanceof SVGImage) {
- /* try {
- SVGSVGElement svg =
- ((SVGImage) img).getSVGDocument().getRootElement();
- currentStream.add("ET\nq\n" + (((float) w) / 1000f) +
- " 0 0 " + (((float) h) / 1000f) + " " +
- (((float) x) / 1000f) + " " +
- (((float)(y - h)) / 1000f) + " cm\n");
- // renderSVG(svg, (int) x, (int) y);
- currentStream.add("Q\nBT\n");
- } catch (FopImageException e) {
- } */
-
- MessageHandler.logln("Warning: SVG images not supported in this version");
- } else {
- String url = img.getURL();
- this.mifDoc.addImage(url,x,pageHeight-y,w,h);
-
- }
- }
-
-
-
- /** render the given inline area */
- public void renderInlineArea(InlineArea area) {}
-
- /** render the given inline space */
- public void renderInlineSpace(InlineSpace space) {
-
- // I dont need the size of space! I just need to
- // leave a blank space each time
- String s=" ";
- this.mifDoc.addToStream(s); // cool!
- this.currentXPosition += space.getSize();
- }
-
- /** render the given line area */
- public void renderLineArea(LineArea area) {
-
-
-
- int rx = this.currentAreaContainerXPosition
- + area.getStartIndent();
- int ry = this.currentYPosition;
- int w = area.getContentWidth();
- int h = area.getHeight();
-
- this.currentYPosition -= area.getPlacementOffset();
- this.currentXPosition = rx;
-
- int bl = this.currentYPosition;
-
- //The start of a new linearea corresponds to a new para in FM
-
- this.mifDoc.startLine();
-
- Enumeration e = area.getChildren().elements();
- while (e.hasMoreElements()) {
-
- Box b = (Box) e.nextElement();
- this.currentYPosition = ry - area.getPlacementOffset();
- b.render(this);
-
- }
- this.currentYPosition = ry-h;
- this.currentXPosition = rx;
-
-
- }
-
-
-
- /** render the given page */
- public void renderPage(Page page) {
-
- AreaContainer before, after;
- BodyAreaContainer body;
- body = page.getBody();
- before = page.getBefore();
- after = page.getAfter();
-
- this.currentFontName = "";
- this.currentFontSize = "0";
-
- pageHeight=page.getHeight();
- pageWidth=page.getWidth();
- this.mifDoc.setDocumentHeightWidth(pageHeight,pageWidth);
-
-
- this.mifDoc.createPage();
-
- renderBodyAreaContainer(body);
-
-
- // If the area is an instance of anything other than body, it goes into the
- // corresponding master page.
-
-
- if (before != null) {
-
- this.mifDoc.createTextRect(1); // Create a rect with one col
- renderAreaContainer(before);
- }
-
- if (after != null) {
-
- this.mifDoc.createTextRect(1); // Create a rect with one col
- renderAreaContainer(after);
- }
-
- }
-
- /** render the given leader area */
- public void renderLeaderArea(LeaderArea area) {}
-
-
- }
|