Changeset 1085


Ignore:
Timestamp:
02/26/10 11:55:07 (2 years ago)
Author:
jharrop
Message:

Convert line to SVG

Location:
trunk/docx4j/src/pptx4j/java/org/pptx4j
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/pptx4j/java/org/pptx4j/convert/out/svginhtml/SvgExporter.java

    r1061 r1085  
    44import java.io.IOException; 
    55 
     6import javax.xml.bind.JAXBContext; 
     7import javax.xml.bind.JAXBElement; 
    68import javax.xml.bind.JAXBException; 
    79import javax.xml.bind.Unmarshaller; 
    810import javax.xml.parsers.DocumentBuilderFactory; 
     11import javax.xml.parsers.ParserConfigurationException; 
    912import javax.xml.transform.Result; 
    1013import javax.xml.transform.Source; 
     
    2124import org.docx4j.dml.CTTextListStyle; 
    2225import org.docx4j.dml.CTTextParagraphProperties; 
     26import org.docx4j.dml.CTTransform2D; 
    2327import org.docx4j.jaxb.Context; 
    2428import org.docx4j.model.styles.StyleTree; 
     
    3034import org.docx4j.wml.PPr; 
    3135import org.docx4j.wml.Style; 
     36import org.plutext.jaxb.svg11.Line; 
     37import org.plutext.jaxb.svg11.ObjectFactory; 
     38import org.plutext.jaxb.svg11.Svg; 
     39import org.pptx4j.Box; 
     40import org.pptx4j.Point; 
    3241import org.pptx4j.model.ResolvedLayout; 
    3342import org.pptx4j.model.TextStyles; 
     43import org.pptx4j.pml.CxnSp; 
    3444import org.pptx4j.pml.GroupShape; 
    3545import org.w3c.dom.DOMException; 
     
    4454         
    4555        protected static Logger log = Logger.getLogger(SvgExporter.class);       
    46          
     56 
     57        static JAXBContext jcSVG;        
     58    static ObjectFactory oFactory; 
    4759        static Templates xslt;                   
    4860        static { 
     61                 
    4962                try { 
     63                        jcSVG = JAXBContext.newInstance("org.plutext.jaxb.svg11"); 
     64                        oFactory = new ObjectFactory(); 
     65 
    5066                        Source xsltSource = new StreamSource( 
    5167                                                org.docx4j.utils.ResourceUtils.getResource( 
    5268                                                                "org/pptx4j/convert/out/svginhtml/pptx2svginhtml.xslt")); 
    5369                        xslt = XmlUtils.getTransformerTemplate(xsltSource); 
    54                 } catch (IOException e) { 
     70                } catch (Exception e) { 
    5571                        e.printStackTrace(); 
    56                 } catch (TransformerConfigurationException e) { 
    57                         e.printStackTrace(); 
    58                 } 
     72                }  
    5973        } 
    6074         
     
    6882                         
    6983                String svg = intermediate.toString("UTF-8"); 
    70                 log.debug(svg); 
     84                log.info(svg); 
    7185        } 
    7286         
     
    337351    } 
    338352     
    339  
     353    public static DocumentFragment shapeToSVG(  
     354                PresentationMLPackage pmlPackage, 
     355                NodeIterator shapeIt ) { 
     356         
     357        try { 
     358                Object shape = null; 
     359                 
     360                if (shapeIt!=null) { 
     361                        Node n = shapeIt.nextNode(); 
     362                         
     363                        log.debug(n.getClass().getName()); 
     364//                      cxnSp 
     365//                      System.out.println(n.getLocalName()); 
     366//                      p:cxnSp 
     367//                      System.out.println(n.getNodeName()); 
     368                         
     369                        String str = XmlUtils.w3CDomNodeToString(n); 
     370                        System.out.println("STRING-->" + str); 
     371                        if (str.equals("")) { 
     372                                 
     373                        } else if (str.startsWith("<p:cxnSp") ){ 
     374                                shape = XmlUtils.unmarshalString(str, Context.jcPML, CxnSp.class); 
     375                                Document d = CxnSpToSVG( (CxnSp)shape); 
     376                                 
     377                                // Machinery 
     378                                DocumentFragment docfrag = d.createDocumentFragment(); 
     379                                docfrag.appendChild(d.getDocumentElement()); 
     380                                return docfrag; 
     381                        } 
     382                         
     383                        log.info("** GOT " + shape.getClass().getName() ); 
     384                        if (shape instanceof JAXBElement) { 
     385                                log.info( 
     386                                                XmlUtils.JAXBElementDebug( (JAXBElement)shape) 
     387                                        ); 
     388                        } 
     389                } 
     390        } catch (Exception e) { 
     391                // TODO Auto-generated catch block 
     392                e.printStackTrace(); 
     393        }  
     394        return null; 
     395         
     396    } 
     397     
     398     
     399    public static Document CxnSpToSVG(CxnSp cxnSp) { 
     400         
     401         
     402        // Geometrical transforms 
     403        CTTransform2D xfrm = cxnSp.getSpPr().getXfrm(); 
     404        Box b = new Box(xfrm.getOff().getX(), xfrm.getOff().getY(), 
     405                        xfrm.getExt().getCx(), xfrm.getExt().getCx() ); 
     406         
     407        if (xfrm.getRot()!=0) { 
     408                b.rotate(xfrm.getRot()); 
     409        } 
     410        if (xfrm.isFlipH() ) { 
     411                b.flipH(); 
     412        } 
     413        if (xfrm.isFlipV() ) { 
     414                b.flipV(); 
     415        } 
     416         
     417        // Convert from EMU to pixels 
     418        b.toPixels(); 
     419 
     420        // Wrap in a div positioning it on the page 
     421        Document document = createDocument(); 
     422                Element xhtmlDiv = document.createElement("div"); 
     423                // Firefox needs the following; Chrome doesn't 
     424                xhtmlDiv.setAttribute("style",  
     425                                "position: absolute; width:100%; height:100%; left:0px; top:0px;"); 
     426                 
     427                Node n = document.appendChild(xhtmlDiv); 
     428                // TODO - set style 
     429         
     430        // Convert the object itself to SVG 
     431                Svg svg = oFactory.createSvg(); 
     432        Line line = oFactory.createLine(); 
     433        svg.getSVGDescriptionClassOrSVGAnimationClassOrSVGStructureClass().add(line); 
     434         
     435        line.setX1(b.getOffset().getXAsString() ); 
     436        line.setY1(b.getOffset().getYAsString() ); 
     437         
     438        Point otherEnd = b.getOtherCorner(); 
     439         
     440        line.setX2( otherEnd.getXAsString() ); 
     441        line.setY2( otherEnd.getYAsString() ); 
     442 
     443        line.setStyle("stroke:rgb(99,99,99)"); 
     444        // You can't see the line in Midori, unless you specify the color. 
     445        // width eg stroke-width:2 is optional 
     446         
     447        Document d2 = XmlUtils.marshaltoW3CDomDocument(svg, jcSVG);    
     448        XmlUtils.treeCopy(d2, n); 
     449        return document; 
     450         
     451    } 
     452     
     453    public static Document createDocument() { 
     454         
     455        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();         
     456                Document document=null; 
     457                try { 
     458                        document = factory.newDocumentBuilder().newDocument(); 
     459                } catch (ParserConfigurationException e) { 
     460                        e.printStackTrace(); 
     461                } 
     462                return document; 
     463    } 
    340464} 
  • trunk/docx4j/src/pptx4j/java/org/pptx4j/convert/out/svginhtml/pptx2svginhtml.xslt

    r1061 r1085  
    11 
    22<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     3        xmlns="http://www.w3.org/1999/xhtml"  
    34    xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" 
    45    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" 
     
    3031       
    3132      but, for now, make indent yes--> 
    32 <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="yes" />  
     33<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="no" indent="yes"  
     34doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
     35     doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/> 
     36<!-- either strict or transitional work for inline SVG -->  
    3337 
    3438<!--  Input to this transform is a Shape Tree p:spTree.  
    3539 
    36         Ouput is SVG in HTML. 
     40        Output is SVG in HTML. 
    3741 
    3842        <text x="{$x}" y="{$y}"><xsl:value-of select="a:p/a:r/a:t"/></text> 
     
    5559        <xsl:param name="wmlPackage"/> <!--  really, its pml --> 
    5660        <xsl:param name="resolvedLayout"/> 
    57 <!--  
    58         <xsl:param name="modelStates"/>  
    59         <xsl:param name="imageDirPath"/> 
    60             
    61         <xsl:param name="fontMapper"/>   
    62         <xsl:param name="fontFamilyStack"/> 
    63          
    64         <xsl:param name="conditionalComments"/>  
    65          --> 
    66  
    67 <!--  
    68  
    69 <p:spTree xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"> 
    70     <p:nvGrpSpPr> 
    71         <p:cNvPr name="" id="1"/> 
    72         <p:cNvGrpSpPr/> 
    73         <p:nvPr/> 
    74     </p:nvGrpSpPr> 
    75     <p:grpSpPr> 
    76         <a:xfrm> 
    77             <a:off y="0" x="0"/> 
    78             <a:ext cy="0" cx="0"/> 
    79             <a:chOff y="0" x="0"/> 
    80             <a:chExt cy="0" cx="0"/> 
    81         </a:xfrm> 
    82     </p:grpSpPr> 
    83     <p:sp> 
    84         <p:nvSpPr> 
    85             <p:cNvPr name="Title 1" id="2"/> 
    86             <p:cNvSpPr> 
    87                 <a:spLocks noGrp="true"/> 
    88             </p:cNvSpPr> 
    89             <p:nvPr/> 
    90         </p:nvSpPr> 
    91         <p:spPr> 
    92             <a:xfrm> 
    93                 <a:off y="2130425" x="685800"/> 
    94                 <a:ext cy="1470025" cx="7772400"/> 
    95             </a:xfrm> 
    96         </p:spPr> 
    97         <p:txBody> 
    98             <a:bodyPr/> 
    99             <a:lstStyle/> 
    100             <a:p> 
    101                 <a:r> 
    102                     <a:rPr smtClean="false" lang="en-US"/> 
    103                     <a:t>My title</a:t> 
    104                 </a:r> 
    105                 <a:endParaRPr lang="en-US"/> 
    106             </a:p> 
    107         </p:txBody> 
    108     </p:sp> 
    109     <p:sp> 
    110         <p:nvSpPr> 
    111             <p:cNvPr name="Subtitle 2" id="3"/> 
    112             <p:cNvSpPr> 
    113                 <a:spLocks noGrp="true"/> 
    114             </p:cNvSpPr> 
    115             <p:nvPr/> 
    116         </p:nvSpPr> 
    117         <p:spPr> 
    118             <a:xfrm> 
    119                 <a:off y="3886200" x="1371600"/> 
    120                 <a:ext cy="1752600" cx="6400800"/> 
    121             </a:xfrm> 
    122         </p:spPr> 
    123         <p:txBody> 
    124             <a:bodyPr/> 
    125             <a:lstStyle/> 
    126             <a:p> 
    127                 <a:r> 
    128                     <a:rPr smtClean="false" lang="en-US"/> 
    129                     <a:t>My subtitle</a:t> 
    130                 </a:r> 
    131                 <a:endParaRPr lang="en-US"/> 
    132             </a:p> 
    133         </p:txBody> 
    134     </p:sp> 
    135 </p:spTree> 
    13661 
    13762 
    138  
    139  --> 
    140  
    14163<xsl:template match="/"> 
    142         <html> 
     64        <html xmlns="http://www.w3.org/1999/xhtml"  
     65      xmlns:svg="http://www.w3.org/2000/svg" 
     66      xmlns:xlink="http://www.w3.org/1999/xlink"> 
    14367                <head> 
    14468                        <title>Slide output proof of concept</title> 
     
    15579                </head> 
    15680                <body> 
    157                  
    15881                        <xsl:apply-templates/>           
    159                  
    16082                </body> 
    161         </html> 
    162          
    163 <!--  
    164         <svg width="600px" height="600px" version="1.1" 
    165                         baseProfile="full"> 
    166                 <xsl:apply-templates/> 
    167         </svg> 
    168          --> 
     83        </html>  
    16984</xsl:template> 
    17085 
     
    17691<xsl:template match="p:grpSpPr"/> 
    17792 
    178 <!--  
    179     <p:sp> 
    180         <p:nvSpPr> 
    181             <p:cNvPr name="Title 1" id="2"/> 
    182             <p:cNvSpPr> 
    183                 <a:spLocks noGrp="true"/> 
    184             </p:cNvSpPr> 
    185             <p:nvPr/> 
    186         </p:nvSpPr> 
    187         <p:spPr> 
    188             <a:xfrm> 
    189                 <a:off y="2130425" x="685800"/> 
    190                 <a:ext cy="1470025" cx="7772400"/> 
    191             </a:xfrm> 
    192         </p:spPr> 
    193         <p:txBody> 
    194             <a:bodyPr/> 
    195             <a:lstStyle/> 
    196             <a:p> 
    197                 <a:r> 
    198                     <a:rPr smtClean="false" lang="en-US"/> 
    199                     <a:t>My title</a:t> 
    200                 </a:r> 
    201                 <a:endParaRPr lang="en-US"/> 
    202             </a:p> 
    203         </p:txBody> 
    204     </p:sp> 
    205  --> 
    20693 <xsl:template match="p:sp"> 
    20794        <xsl:apply-templates select="p:txBody"/> 
     
    233120        <!--  At present, docx4j doesn't do text boxes in its docx html, 
    234121              so handle the box here.  --> 
    235         <div style="position: absolute; width:{$cx}; height:{$cy}; left:{$x}; top:{$y}; border: red dashed;"> 
     122        <div style="position: absolute; width:{$cx}px; height:{$cy}px; left:{$x}px; top:{$y}px; border: red dashed;"> 
    236123                <xsl:apply-templates select="a:p"/> 
    237124        </div> 
    238125 </xsl:template> 
     126 
     127 <xsl:template match="p:cxnSp"> 
     128 
     129                <xsl:variable name="shape" select="."/> 
     130 
     131                <xsl:copy-of select="java:org.pptx4j.convert.out.svginhtml.SvgExporter.shapeToSVG( 
     132                        $wmlPackage, $shape)" /> 
     133  
     134 </xsl:template> 
     135 
    239136 
    240137 <xsl:template match="a:p"> 
Note: See TracChangeset for help on using the changeset viewer.