diff options
author | Peter Hancock <phancock@apache.org> | 2012-06-29 11:22:42 +0000 |
---|---|---|
committer | Peter Hancock <phancock@apache.org> | 2012-06-29 11:22:42 +0000 |
commit | 3e31e071ea47f37ef7f8749b324dfdfe76745d18 (patch) | |
tree | 7d3b646c521349634592b62556a8832ec082cc9c /src/java/org/apache/fop/render/intermediate/IFSerializer.java | |
parent | ae8a5035235e1262e412e1eacd0eb3adfaef83ef (diff) | |
parent | 015538e0f11f031e3d7bd05db8c29e2a40365678 (diff) | |
download | xmlgraphics-fop-3e31e071ea47f37ef7f8749b324dfdfe76745d18.tar.gz xmlgraphics-fop-3e31e071ea47f37ef7f8749b324dfdfe76745d18.zip |
Merged trunk@1354651
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_RoundedCorners@1355321 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/intermediate/IFSerializer.java')
-rw-r--r-- | src/java/org/apache/fop/render/intermediate/IFSerializer.java | 176 |
1 files changed, 124 insertions, 52 deletions
diff --git a/src/java/org/apache/fop/render/intermediate/IFSerializer.java b/src/java/org/apache/fop/render/intermediate/IFSerializer.java index 4af90faed..4c6781706 100644 --- a/src/java/org/apache/fop/render/intermediate/IFSerializer.java +++ b/src/java/org/apache/fop/render/intermediate/IFSerializer.java @@ -31,19 +31,18 @@ import java.util.Locale; import java.util.Map; import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; import org.apache.xmlgraphics.util.QName; import org.apache.xmlgraphics.util.XMLizable; -import org.apache.fop.accessibility.StructureTree; +import org.apache.fop.accessibility.StructureTreeEventHandler; +import org.apache.fop.fo.extensions.InternalElementMapping; import org.apache.fop.fonts.FontInfo; import org.apache.fop.render.PrintRendererConfigurator; import org.apache.fop.render.RenderingContext; +import org.apache.fop.render.intermediate.IFStructureTreeBuilder.IFStructureTreeElement; import org.apache.fop.render.intermediate.extensions.AbstractAction; import org.apache.fop.render.intermediate.extensions.Bookmark; import org.apache.fop.render.intermediate.extensions.BookmarkTree; @@ -54,28 +53,38 @@ import org.apache.fop.traits.BorderProps; import org.apache.fop.traits.RuleStyle; import org.apache.fop.util.ColorUtil; import org.apache.fop.util.DOM2SAX; +import org.apache.fop.util.LanguageTags; import org.apache.fop.util.XMLConstants; import org.apache.fop.util.XMLUtil; + /** * IFPainter implementation that serializes the intermediate format to XML. */ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler implements IFConstants, IFPainter, IFDocumentNavigationHandler { + /** + * Intermediate Format (IF) version, used to express an @version attribute + * in the root element of the IF document, the initial value of which + * is set to '2.0' to signify that something preceded it (but didn't + * happen to be marked as such), and that this version is not necessarily + * backwards compatible with the unmarked (<2.0) version. + */ + public static final String VERSION = "2.0"; + private IFDocumentHandler mimicHandler; private int pageSequenceIndex; // used for accessibility /** Holds the intermediate format state */ private IFState state; - /** - * Default constructor. - */ - public IFSerializer() { - } + private String currentID = ""; + + private IFStructureTreeBuilder structureTreeBuilder; /** {@inheritDoc} */ + @Override protected String getMainNamespace() { return NAMESPACE; } @@ -102,6 +111,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ + @Override public IFDocumentNavigationHandler getDocumentNavigationHandler() { return this; } @@ -146,7 +156,16 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } } + @Override + public StructureTreeEventHandler getStructureTreeEventHandler() { + if (structureTreeBuilder == null) { + structureTreeBuilder = new IFStructureTreeBuilder(); + } + return structureTreeBuilder; + } + /** {@inheritDoc} */ + @Override public void startDocument() throws IFException { super.startDocument(); try { @@ -155,13 +174,31 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler handler.startPrefixMapping(XLINK_PREFIX, XLINK_NAMESPACE); handler.startPrefixMapping(DocumentNavigationExtensionConstants.PREFIX, DocumentNavigationExtensionConstants.NAMESPACE); - handler.startElement(EL_DOCUMENT); + handler.startPrefixMapping(InternalElementMapping.STANDARD_PREFIX, + InternalElementMapping.URI); + AttributesImpl atts = new AttributesImpl(); + addAttribute(atts, "version", VERSION); + handler.startElement(EL_DOCUMENT, atts); } catch (SAXException e) { throw new IFException("SAX error in startDocument()", e); } } + @Override + public void setDocumentLocale(Locale locale) { + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute(XML_NAMESPACE, "lang", "xml:lang", XMLUtil.CDATA, + LanguageTags.toLanguageTag(locale)); + try { + handler.startElement(EL_LOCALE, atts); + handler.endElement(EL_LOCALE); + } catch (SAXException e) { + throw new RuntimeException("Unable to create the " + EL_LOCALE + " element.", e); + } + } + /** {@inheritDoc} */ + @Override public void startDocumentHeader() throws IFException { try { handler.startElement(EL_HEADER); @@ -171,6 +208,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ + @Override public void endDocumentHeader() throws IFException { try { handler.endElement(EL_HEADER); @@ -180,6 +218,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ + @Override public void startDocumentTrailer() throws IFException { try { handler.startElement(EL_TRAILER); @@ -189,6 +228,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ + @Override public void endDocumentTrailer() throws IFException { try { handler.endElement(EL_TRAILER); @@ -218,20 +258,14 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler Locale lang = getContext().getLanguage(); if (lang != null) { atts.addAttribute(XML_NAMESPACE, "lang", "xml:lang", XMLUtil.CDATA, - XMLUtil.toRFC3066(lang)); + LanguageTags.toLanguageTag(lang)); } XMLUtil.addAttribute(atts, XMLConstants.XML_SPACE, "preserve"); addForeignAttributes(atts); handler.startElement(EL_PAGE_SEQUENCE, atts); if (this.getUserAgent().isAccessibilityEnabled()) { - StructureTree structureTree = getUserAgent().getStructureTree(); - handler.startElement(EL_STRUCTURE_TREE); // add structure tree - NodeList nodes = structureTree.getPageSequence(pageSequenceIndex++); - for (int i = 0, n = nodes.getLength(); i < n; i++) { - Node node = nodes.item(i); - new DOM2SAX(handler).writeFragment(node); - } - handler.endElement(EL_STRUCTURE_TREE); + assert (structureTreeBuilder != null); + structureTreeBuilder.replayEventsForPageSequence(handler, pageSequenceIndex++); } } catch (SAXException e) { throw new IFException("SAX error in startPageSequence()", e); @@ -241,6 +275,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler /** {@inheritDoc} */ public void endPageSequence() throws IFException { try { + handler.endElement(EL_PAGE_SEQUENCE); } catch (SAXException e) { throw new IFException("SAX error in endPageSequence()", e); @@ -265,6 +300,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ + @Override public void startPageHeader() throws IFException { try { handler.startElement(EL_PAGE_HEADER); @@ -274,6 +310,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ + @Override public void endPageHeader() throws IFException { try { handler.endElement(EL_PAGE_HEADER); @@ -297,6 +334,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler public void endPageContent() throws IFException { try { this.state = null; + currentID = ""; handler.endElement(EL_PAGE_CONTENT); } catch (SAXException e) { throw new IFException("SAX error in endPageContent()", e); @@ -304,6 +342,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ + @Override public void startPageTrailer() throws IFException { try { handler.startElement(EL_PAGE_TRAILER); @@ -313,6 +352,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ + @Override public void endPageTrailer() throws IFException { try { commitNavigation(); @@ -406,6 +446,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler /** {@inheritDoc} */ public void drawImage(String uri, Rectangle rect) throws IFException { try { + addID(); AttributesImpl atts = new AttributesImpl(); addAttribute(atts, XLINK_HREF, uri); addAttribute(atts, "x", Integer.toString(rect.x)); @@ -413,7 +454,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler addAttribute(atts, "width", Integer.toString(rect.width)); addAttribute(atts, "height", Integer.toString(rect.height)); addForeignAttributes(atts); - addStructurePointerAttribute(atts); + addStructureReference(atts); handler.element(EL_IMAGE, atts); } catch (SAXException e) { throw new IFException("SAX error in startGroup()", e); @@ -434,13 +475,14 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler /** {@inheritDoc} */ public void drawImage(Document doc, Rectangle rect) throws IFException { try { + addID(); 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)); addForeignAttributes(atts); - addStructurePointerAttribute(atts); + addStructureReference(atts); handler.startElement(EL_IMAGE, atts); new DOM2SAX(handler).writeDocument(doc, true); handler.endElement(EL_IMAGE); @@ -550,9 +592,9 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ - public void drawBorderRect(Rectangle rect, BorderProps before, BorderProps after, - BorderProps start, BorderProps end, Color innerBackgroundColor) throws IFException { - if (before == null && after == null && start == null && end == null) { + public void drawBorderRect(Rectangle rect, BorderProps top, BorderProps bottom, + BorderProps left, BorderProps right, Color innerBackgroundColor) throws IFException { + if (top == null && bottom == null && left == null && right == null) { return; } try { @@ -561,17 +603,17 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler addAttribute(atts, "y", Integer.toString(rect.y)); addAttribute(atts, "width", Integer.toString(rect.width)); addAttribute(atts, "height", Integer.toString(rect.height)); - if (before != null) { - addAttribute(atts, "before", before.toString()); + if (top != null) { + addAttribute(atts, "top", top.toString()); } - if (after != null) { - addAttribute(atts, "after", after.toString()); + if (bottom != null) { + addAttribute(atts, "bottom", bottom.toString()); } - if (start != null) { - addAttribute(atts, "start", start.toString()); + if (left != null) { + addAttribute(atts, "left", left.toString()); } - if (end != null) { - addAttribute(atts, "end", end.toString()); + if (right != null) { + addAttribute(atts, "right", right.toString()); } if (innerBackgroundColor != null) { @@ -589,6 +631,7 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler public void drawLine(Point start, Point end, int width, Color color, RuleStyle style) throws IFException { try { + addID(); AttributesImpl atts = new AttributesImpl(); addAttribute(atts, "x1", Integer.toString(start.x)); addAttribute(atts, "y1", Integer.toString(start.y)); @@ -605,8 +648,9 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler /** {@inheritDoc} */ public void drawText(int x, int y, int letterSpacing, int wordSpacing, - int[] dx, String text) throws IFException { + int[][] dp, String text) throws IFException { try { + addID(); AttributesImpl atts = new AttributesImpl(); addAttribute(atts, "x", Integer.toString(x)); addAttribute(atts, "y", Integer.toString(y)); @@ -616,10 +660,19 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler if (wordSpacing != 0) { addAttribute(atts, "word-spacing", Integer.toString(wordSpacing)); } - if (dx != null) { - addAttribute(atts, "dx", IFUtil.toString(dx)); + if (dp != null) { + if ( IFUtil.isDPIdentity(dp) ) { + // don't add dx or dp attribute + } else if ( IFUtil.isDPOnlyDX(dp) ) { + // add dx attribute only + int[] dx = IFUtil.convertDPToDX(dp); + addAttribute(atts, "dx", IFUtil.toString(dx)); + } else { + // add dp attribute only + addAttribute(atts, "dp", XMLUtil.encodePositionAdjustments(dp)); + } } - addStructurePointerAttribute(atts); + addStructureReference(atts); handler.startElement(EL_TEXT, atts); char[] chars = text.toCharArray(); handler.characters(chars, 0, chars.length); @@ -671,7 +724,8 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } } if (color != null) { - changed = !color.equals(state.getTextColor()); + changed = !org.apache.xmlgraphics.java2d.color.ColorUtil.isSameColor( + color, state.getTextColor()); if (changed) { state.setTextColor(color); addAttribute(atts, "color", toString(color)); @@ -718,14 +772,32 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler XMLUtil.addAttribute(atts, localName, value); } - private void addStructurePointerAttribute(AttributesImpl atts) { - String ptr = getContext().getStructurePointer(); - if (ptr != null) { - addAttribute(atts, "ptr", ptr); + private void addStructureReference(AttributesImpl atts) { + IFStructureTreeElement structureTreeElement + = (IFStructureTreeElement) getContext().getStructureTreeElement(); + if (structureTreeElement != null) { + addStructRefAttribute(atts, structureTreeElement.getId()); } } - // ---=== IFDocumentNavigationHandler ===--- + private void addStructRefAttribute(AttributesImpl atts, String id) { + atts.addAttribute(InternalElementMapping.URI, + InternalElementMapping.STRUCT_REF, + InternalElementMapping.STANDARD_PREFIX + ":" + InternalElementMapping.STRUCT_REF, + XMLConstants.CDATA, + id); + } + + private void addID() throws SAXException { + String id = getContext().getID(); + if (!currentID.equals(id)) { + AttributesImpl atts = new AttributesImpl(); + addAttribute(atts, "name", id); + handler.startElement(EL_ID, atts); + handler.endElement(EL_ID); + currentID = id; + } + } private Map incompleteActions = new java.util.HashMap(); private List completeActions = new java.util.LinkedList(); @@ -763,7 +835,9 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler Iterator iter = tree.getBookmarks().iterator(); while (iter.hasNext()) { Bookmark b = (Bookmark)iter.next(); - serializeBookmark(b); + if (b.getAction() != null) { + serializeBookmark(b); + } } handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE); } catch (SAXException e) { @@ -783,10 +857,11 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler Iterator iter = bookmark.getChildBookmarks().iterator(); while (iter.hasNext()) { Bookmark b = (Bookmark)iter.next(); - serializeBookmark(b); + if (b.getAction() != null) { + serializeBookmark(b); + } } handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK); - } /** {@inheritDoc} */ @@ -797,7 +872,8 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler atts.addAttribute(null, "rect", "rect", XMLConstants.CDATA, IFUtil.toString(link.getTargetRect())); if (getUserAgent().isAccessibilityEnabled()) { - addAttribute(atts, "ptr", link.getAction().getStructurePointer()); + addStructRefAttribute(atts, + ((IFStructureTreeElement) link.getAction().getStructureTreeElement()).getId()); } try { handler.startElement(DocumentNavigationExtensionConstants.LINK, atts); @@ -843,12 +919,8 @@ public class IFSerializer extends AbstractXMLWritingIFDocumentHandler } /** {@inheritDoc} */ - public boolean isBackgroundRequired(BorderProps bpsBefore, BorderProps bpsAfter, - BorderProps bpsStart, BorderProps bpsEnd) { + public boolean isBackgroundRequired(BorderProps bpsTop, BorderProps bpsBottom, + BorderProps bpsLeft, BorderProps bpsRight) { return true; } - - - - } |