Changeset 1078


Ignore:
Timestamp:
02/23/10 17:32:26 (2 years ago)
Author:
jharrop
Message:

Support for WMF (but not EMF, yet) as SVG in HTML output.

Location:
trunk/docx4j
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/build.xml

    r1073 r1078  
    5151        <pathelement location="${m2Repository}/docx4j/fop-patched/0.95.756437/fop-patched-0.95.756437.jar"/> 
    5252        <pathelement location="${m2Repository}/org/apache/commons/commons-vfs-patched/1.9.1/commons-vfs-patched-1.9.1.jar"/> 
     53        <pathelement location="${m2Repository}/net/arnx/wmf2svg/0.8.3/wmf2svg-0.8.3.jar"/> 
    5354    </path> 
    5455    <path id="docx4j.classpath"> 
  • trunk/docx4j/pom.xml

    r1073 r1078  
    231231                        mvn deploy:deploy-file -f wagon-svn-pom.xml -Dfile=dist/docx4j.jar -DpomFile=pom.xml  -Dpackaging=jar -DrepositoryId=docx4j -Durl=svn:http://dev.plutext.org/svn/docx4j/trunk/docx4j/m2                   
    232232                --> 
    233  
     233                 
    234234                <!--  We need SVN build, not v1.3.1, 
    235235                          in order to run headless. 
     
    240240                        <artifactId>xmlgraphics-commons</artifactId> 
    241241                        <version>1.3.757686</version> 
     242                </dependency> 
     243 
     244                <dependency> 
     245                        <groupId>net.arnx</groupId> 
     246                        <artifactId>wmf2svg</artifactId> 
     247                        <version>0.8.3</version> 
    242248                </dependency> 
    243249 
     
    320326                              --> 
    321327 
    322  
     328                 
    323329<!--  
    324330                <dependency> 
  • trunk/docx4j/src/main/java/org/docx4j/model/images/WordXmlPicture.java

    r979 r1078  
    2828import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 
    2929import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; 
     30import org.docx4j.openpackaging.parts.WordprocessingML.MetafileEmfPart; 
     31import org.docx4j.openpackaging.parts.WordprocessingML.MetafilePart; 
     32import org.docx4j.openpackaging.parts.WordprocessingML.MetafileWmfPart; 
    3033import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.CxCy; 
     34import org.docx4j.openpackaging.parts.WordprocessingML.MetafileWmfPart.SvgDocument; 
    3135import org.docx4j.relationships.Relationship; 
    3236import org.docx4j.wml.SectPr; 
    3337import org.w3c.dom.Document; 
    3438import org.w3c.dom.DocumentFragment; 
     39import org.w3c.dom.Element; 
    3540import org.w3c.dom.Node; 
     41import org.w3c.dom.Text; 
    3642import org.w3c.dom.css.CSSPrimitiveValue; 
    3743import org.w3c.dom.css.CSSStyleDeclaration; 
     
    5864    Node imageElement = null; 
    5965    Node linkElement = null; 
     66     
     67    private MetafilePart metaFile; 
    6068 
    6169    /** Extension function to create an HTML <img> element 
     
    8088                         picLink,  linkData, true); 
    8189         
    82         Document d = picture.createHtmlImageElement(); 
    83  
    84                 DocumentFragment docfrag = d.createDocumentFragment(); 
     90        return getHtmlDocumentFragment(picture); 
     91 
     92    } 
     93     
     94    public static DocumentFragment getHtmlDocumentFragment(WordXmlPicture picture) { 
     95         
     96        DocumentFragment docfrag=null; 
     97        Document d=null; 
     98        try { 
     99                if (picture==null) { 
     100                        log.warn("picture was null!"); 
     101                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();         
     102                         try { 
     103                                d = factory.newDocumentBuilder().newDocument(); 
     104                        } catch (ParserConfigurationException e1) { 
     105                                // TODO Auto-generated catch block 
     106                                e1.printStackTrace(); 
     107                        } 
     108                        Element span = d.createElement("span"); 
     109                        span.setAttribute("style", "color:red;"); 
     110                        d.appendChild(span); 
     111                         
     112                        Text err = d.createTextNode( "[null img]" ); 
     113                        span.appendChild(err); 
     114                 
     115                } else if (picture.metaFile==null) { 
     116                                // Usual case            
     117                            d = picture.createHtmlImageElement(); 
     118                        } else if (picture.metaFile instanceof MetafileWmfPart) { 
     119                                 
     120                                SvgDocument svgdoc = ((MetafileWmfPart)picture.metaFile).toSVG(); 
     121                                d = svgdoc.getDomDocument(); 
     122                                 
     123                        } else if (picture.metaFile instanceof MetafileEmfPart) { 
     124                                 
     125                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();         
     126                                 d = factory.newDocumentBuilder().newDocument(); 
     127                                 
     128                                //log.info("Document: " + document.getClass().getName() ); 
     129 
     130                                Node span = d.createElement("span");                     
     131                                d.appendChild(span); 
     132                                 
     133                                Text err = d.createTextNode( "[TODO emf image]" ); 
     134                                span.appendChild(err); 
     135                                 
     136                        } 
     137                } catch (Exception e) { 
     138                        log.error(e); 
     139                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();         
     140                         try { 
     141                                d = factory.newDocumentBuilder().newDocument(); 
     142                        } catch (ParserConfigurationException e1) { 
     143                                // TODO Auto-generated catch block 
     144                                e1.printStackTrace(); 
     145                        } 
     146                        Element span = d.createElement("span"); 
     147                        span.setAttribute("style", "color:red;"); 
     148                        d.appendChild(span); 
     149                         
     150                        Text err = d.createTextNode( e.getMessage() ); 
     151                        span.appendChild(err); 
     152                } 
     153                docfrag = d.createDocumentFragment(); 
    85154                docfrag.appendChild(d.getDocumentElement()); 
    86  
    87155                return docfrag; 
    88156    } 
     
    179247                if (rel.getTargetMode() == null 
    180248                                                || rel.getTargetMode().equals("Internal")) { 
    181  
    182                         BinaryPartAbstractImage part = (BinaryPartAbstractImage)wmlPackage.getMainDocumentPart() 
    183                                                         .getRelationshipsPart().getPart(rel); 
     249                         
     250                         
     251                        BinaryPart part = (BinaryPart)wmlPackage.getMainDocumentPart() 
     252                                                .getRelationshipsPart().getPart(rel); 
     253                         
     254                        if (part instanceof MetafilePart) { 
     255                                 
     256                                picture.metaFile = (MetafilePart)part; 
     257                                 
     258                        } else { 
     259 
     260                                BinaryPartAbstractImage imagepart = (BinaryPartAbstractImage)part; 
     261                                                 
     262                                                String uri = handlePart(imageDirPath, picture, imagepart); 
     263                                                // Scale it?  Shouldn't be necessary, since Word should 
     264                                                // be providing the height/width 
     265        //                                      try { 
     266        //                                              ImageInfo imageInfo = BinaryPartAbstractImage.getImageInfo(uri); 
     267        //                                               
     268        //                                              List<SectionWrapper> sections = wmlPackage.getDocumentModel().getSections(); 
     269        //                                              PageDimensions page = sections.get(sections.size()-1).getPageDimensions(); 
     270        //                                               
     271        //                                              picture.ensureFitsPage(imageInfo, page ); 
     272        //                                      } catch (Exception e) { 
     273        //                                              e.printStackTrace(); 
     274        //                                      } 
     275 
     276                        } 
    184277                                         
    185                                         String uri = handlePart(imageDirPath, picture, part); 
    186                                         // Scale it?  Shouldn't be necessary, since Word should 
    187                                         // be providing the height/width 
    188 //                                      try { 
    189 //                                              ImageInfo imageInfo = BinaryPartAbstractImage.getImageInfo(uri); 
    190 //                                               
    191 //                                              List<SectionWrapper> sections = wmlPackage.getDocumentModel().getSections(); 
    192 //                                              PageDimensions page = sections.get(sections.size()-1).getPageDimensions(); 
    193 //                                               
    194 //                                              picture.ensureFitsPage(imageInfo, page ); 
    195 //                                      } catch (Exception e) { 
    196 //                                              e.printStackTrace(); 
    197 //                                      } 
    198  
    199278                                } else { // External 
    200279                                        picture.setSrc(rel.getTarget()); 
     280                                         
     281                                        // TODO: handle external metafiles 
    201282                                } 
    202283 
     
    420501                         shape,  imageData, true); 
    421502         
    422         if (picture==null) { 
    423                  
    424             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    425             Document d; 
    426                         try { 
    427                                 d = factory.newDocumentBuilder().newDocument(); 
    428                         return d.createDocumentFragment(); 
    429                         } catch (ParserConfigurationException e) { 
    430                                 log.error(e); 
    431                                 return null; 
    432                         }   
    433                          
    434         } else { 
    435          
    436                 Document d = picture.createHtmlImageElement(); 
    437          
    438                         DocumentFragment docfrag = d.createDocumentFragment(); 
    439                         docfrag.appendChild(d.getDocumentElement()); 
    440          
    441                         return docfrag; 
    442         }         
     503        return getHtmlDocumentFragment(picture); 
    443504    } 
    444505 
     
    526587                                || rel.getTargetMode().equals("Internal") ) { 
    527588                         
    528                         BinaryPartAbstractImage part = (BinaryPartAbstractImage)wmlPackage.getMainDocumentPart() 
     589                        BinaryPart part = (BinaryPartAbstractImage)wmlPackage.getMainDocumentPart() 
    529590                                        .getRelationshipsPart().getPart(rel); 
    530                                 String uri = handlePart(imageDirPath, picture, part); 
     591                         
     592                        if (part instanceof MetafilePart) { 
     593                                 
     594                                picture.metaFile = (MetafilePart)part; 
     595                                 
     596                        } else { 
     597                         
     598                        BinaryPartAbstractImage imagepart = (BinaryPartAbstractImage)part;                               
     599                        String uri = handlePart(imageDirPath, picture, imagepart); 
    531600                                 
    532601                                // Scale it?  Shouldn't be necessary, since Word should 
     
    542611//                                      e.printStackTrace(); 
    543612//                              } 
    544                                  
     613                        }                                
    545614                         
    546615                } else { 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/contenttype/ContentTypeManager.java

    r1050 r1078  
    320320                         
    321321                        return new org.docx4j.openpackaging.parts.WordprocessingML.ImageTiffPart(new PartName(partName)); 
     322                } else if (contentType.equals(ContentTypes.IMAGE_EMF)) { 
     323                        return new MetafileEmfPart(new PartName(partName)); 
     324                } else if (contentType.equals(ContentTypes.IMAGE_WMF)) { 
     325                        return new MetafileWmfPart(new PartName(partName)); 
    322326                } else if (contentType.equals(ContentTypes.APPLICATION_XML)) { 
    323327                        log.warn("DefaultPart used for part '" + partName  
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/LoadFromZipNG.java

    r1063 r1078  
    329329                        log.info("\n For Relationship Id=" + r.getId()  
    330330                                        + " Source is " + rp.getSourceP().getPartName()  
    331                                         + ", Target is " + r.getTarget() ); 
     331                                        + ", Target is " + r.getTarget() 
     332                                        + ", type: " + r.getType() ); 
     333                                         
    332334                                // This is usually the first logged comment for 
    333335                                // a part, so start with a line break. 
  • trunk/docx4j/src/main/java/org/docx4j/samples/CreateHtml.java

    r1044 r1078  
    2121package org.docx4j.samples; 
    2222 
    23 import java.io.FileInputStream; 
    2423import java.io.OutputStream; 
    25  
    26 import javax.xml.bind.JAXBContext; 
    27 import javax.xml.bind.JAXBElement; 
    28 import javax.xml.bind.Unmarshaller; 
    2924 
    3025import org.docx4j.convert.out.html.AbstractHtmlExporter; 
    3126import org.docx4j.convert.out.html.HtmlExporter; 
    32 import org.docx4j.convert.out.html.HtmlExporterNG; 
    3327import org.docx4j.convert.out.html.HtmlExporterNG2; 
    34 import org.docx4j.jaxb.Context; 
    3528import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
    36 import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; 
    3729 
     30/** 
     31 * If the source docx contained a WMF, that 
     32 * will get converted to inline SVG.  In order 
     33 * to see the SVG in your browser, you'll need  
     34 * to rename the file to .xml or serve 
     35 * it with MIME type application/xhtml+xml 
     36 * 
     37 */ 
    3838public class CreateHtml { 
    3939             
     
    4444                boolean useHtmlExporterNG = true; 
    4545 
     46                        String inputfilepath = System.getProperty("user.dir") + "/tmp/wmf.docx"; 
     47                 
    4648//              String inputfilepath = System.getProperty("user.dir") + "/sample-docs/sample-docx.xml";          
    47                 String inputfilepath = System.getProperty("user.dir") + "/docs/Docx4j_GettingStarted.xml";               
     49//              String inputfilepath = System.getProperty("user.dir") + "/docs/Docx4j_GettingStarted.xml";               
    4850//              String inputfilepath = System.getProperty("user.dir")  
    4951//              + "/sample-docs/test-docs/endnotes.xml";                 
Note: See TracChangeset for help on using the changeset viewer.