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

Convert line to SVG

File:
1 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} 
Note: See TracChangeset for help on using the changeset viewer.