Changeset 1098


Ignore:
Timestamp:
03/02/10 10:14:12 (2 years ago)
Author:
jharrop
Message:

Support image of type anchor, not just inline.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/model/images/WordXmlPictureE20.java

    r1097 r1098  
    2323import org.docx4j.XmlUtils; 
    2424import org.docx4j.dml.CTBlip; 
     25import org.docx4j.dml.CTNonVisualDrawingProps; 
    2526import org.docx4j.dml.CTPositiveSize2D; 
    2627import org.docx4j.dml.picture.Pic; 
     28import org.docx4j.dml.wordprocessingDrawing.Anchor; 
    2729import org.docx4j.dml.wordprocessingDrawing.Inline; 
    2830import org.docx4j.jaxb.Context; 
     31import org.docx4j.model.images.AbstractWordXmlPicture.Dimensions; 
    2932import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
    3033import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; 
     
    7477                                        </wp:inline> 
    7578                                </w:drawing> 
     79                                 
     80                        <w:drawing> 
     81                                        <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0"  
     82                                        relativeHeight="251662336" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1"> 
     83                                                <wp:simplePos x="0" y="0" /> 
     84                                                <wp:positionH relativeFrom="column"> 
     85                                                        <wp:posOffset>3400425</wp:posOffset> 
     86                                                </wp:positionH> 
     87                                                <wp:positionV relativeFrom="paragraph"> 
     88                                                        <wp:posOffset>1991360</wp:posOffset> 
     89                                                </wp:positionV> 
     90                                                <wp:extent cx="552450" cy="209550" /> 
     91                                                <wp:effectExtent l="38100" t="0" r="19050" b="38100" /> 
     92                                                <wp:wrapTopAndBottom /> 
     93                                                <wp:docPr id="4" name="Picture 1" descr="D:\\stuff\\untitled.bmp" /> 
     94                                                <wp:cNvGraphicFramePr> 
     95                                                        <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" /> 
     96                                                </wp:cNvGraphicFramePr> 
     97                                                <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> 
     98                                                        <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"> 
     99                                                                <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"> 
     100                                                                        <pic:nvPicPr> 
     101                                                                                <pic:cNvPr id="0" name="Picture 1" descr="D:\\stuff\\untitled.bmp" /> 
     102                                                                                <pic:cNvPicPr> 
     103                                                                                        <a:picLocks noChangeAspect="1" noChangeArrowheads="1" /> 
     104                                                                                </pic:cNvPicPr> 
     105                                                                        </pic:nvPicPr> 
     106                                                                        <pic:blipFill> 
     107                                                                                <a:blip r:embed="rId7" cstate="print"> 
     108                                                                                        <a:clrChange> 
     109                                                                                                <a:clrFrom> 
     110                                                                                                        <a:srgbClr val="FFFFFF" /> 
     111                                                                                                </a:clrFrom> 
     112                                                                                                <a:clrTo> 
     113                                                                                                        <a:srgbClr val="FFFFFF"> 
     114                                                                                                                <a:alpha val="0" /> 
     115                                                                                                        </a:srgbClr> 
     116                                                                                                </a:clrTo> 
     117                                                                                        </a:clrChange> 
     118                                                                                        <a:biLevel thresh="50000" /> 
     119                                                                                </a:blip> 
     120                                                                                <a:srcRect l="35365" t="28689" r="52846" b="65300" /> 
     121                                                                                <a:stretch> 
     122                                                                                        <a:fillRect /> 
     123                                                                                </a:stretch> 
     124                                                                        </pic:blipFill> 
     125                                                                        <pic:spPr bwMode="auto"> 
     126                                                                                <a:xfrm rot="21023481"> 
     127                                                                                        <a:off x="0" y="0" /> 
     128                                                                                        <a:ext cx="552450" cy="209550" /> 
     129                                                                                </a:xfrm> 
     130                                                                                <a:prstGeom prst="rect"> 
     131                                                                                        <a:avLst /> 
     132                                                                                </a:prstGeom> 
     133                                                                                <a:noFill /> 
     134                                                                                <a:ln w="9525"> 
     135                                                                                        <a:noFill /> 
     136                                                                                        <a:miter lim="800000" /> 
     137                                                                                        <a:headEnd /> 
     138                                                                                        <a:tailEnd /> 
     139                                                                                </a:ln> 
     140                                                                        </pic:spPr> 
     141                                                                </pic:pic> 
     142                                                        </a:graphicData> 
     143                                                </a:graphic> 
     144                                        </wp:anchor> 
     145                                </w:drawing>                             
    76146 *  
    77147 * 
     
    81151        protected static Logger log = Logger.getLogger(WordXmlPictureE20.class); 
    82152 
     153        // It'll be one or the other of these 
    83154        private Inline inline; 
    84      
    85     private WordXmlPictureE20(WordprocessingMLPackage wmlPackage, NodeIterator wpInline) { 
     155        private Anchor anchor; 
     156     
     157    private WordXmlPictureE20(WordprocessingMLPackage wmlPackage, NodeIterator anchorOrInline) { 
    86158         
    87159        this.wmlPackage = wmlPackage; 
    88160         
    89         // TODO: what if its wp:anchor instead? 
    90          
    91         if (wpInline!=null) { 
    92                 Node n = wpInline.nextNode(); 
     161        if (anchorOrInline!=null) { 
     162                Node n = anchorOrInline.nextNode(); 
    93163                if (n!=null) { 
    94164                        Object jaxb=null; 
     
    107177                                        if (jb.getDeclaredType().getName().equals("org.docx4j.dml.wordprocessingDrawing.Inline")) { 
    108178                                                this.inline =  (Inline)jb.getValue(); 
     179                                        } else if (jb.getDeclaredType().getName().equals("org.docx4j.dml.wordprocessingDrawing.Anchor")) { 
     180                                                this.anchor =  (Anchor)jb.getValue(); 
    109181                                        } else { 
    110182                                        log.error("UNEXPECTED " + 
     
    115187                                } else if (jaxb instanceof Inline) {                                     
    116188                                        this.inline =  (Inline)jaxb; 
     189                                } else if (jaxb instanceof Anchor) {                                     
     190                                        this.anchor =  (Anchor)jaxb; 
    117191                                } else { 
    118192                                        log.error( jaxb.getClass().getName() );  
     
    120194                                } 
    121195                        } catch (ClassCastException e) { 
    122                         log.error("Couldn't cast " + jaxb.getClass().getName() + " to Inline"); 
     196                        log.error("Couldn't cast " + jaxb.getClass().getName() + " to Anchor or Inline "); 
    123197                        }                                                
    124198                } 
     
    137211     * @return 
    138212     */ 
    139     public static WordXmlPictureE20 createWordXmlPictureFromE20( 
    140                 WordprocessingMLPackage wmlPackage, 
     213    public static WordXmlPictureE20 createWordXmlPictureFromE20(WordprocessingMLPackage wmlPackage, 
    141214                String imageDirPath, 
    142                 NodeIterator wpInline) { 
    143  
    144         WordXmlPictureE20 converter = new WordXmlPictureE20(wmlPackage, wpInline); 
     215                NodeIterator anchorOrInline) { 
     216 
     217        WordXmlPictureE20 converter = new WordXmlPictureE20(wmlPackage, anchorOrInline); 
    145218         
    146219        converter.readDimensions(); 
    147220        converter.readHyperlink(); 
    148221         
    149         if (converter.inline.getGraphic()==null 
    150                         || converter.inline.getGraphic().getGraphicData()==null 
    151                         || converter.inline.getGraphic().getGraphicData().getPic()==null) { 
     222         
     223        Pic pic = converter.getPic(); 
     224        if (pic==null) { 
    152225                log.error("pic missing!!"); 
    153226                return null;                     
    154227        } 
    155          
    156         Pic pic = converter.inline.getGraphic().getGraphicData().getPic(); 
    157228         
    158229        if (pic.getBlipFill()==null 
     
    175246                return converter; 
    176247        } 
     248     
     249    private Pic getPic() { 
     250         
     251        if (inline!=null) { 
     252                if (inline.getGraphic()==null 
     253                                || inline.getGraphic().getGraphicData()==null 
     254                                || inline.getGraphic().getGraphicData().getPic()==null) { 
     255                        log.error("pic missing!!"); 
     256                        return null;                     
     257                } 
     258                return inline.getGraphic().getGraphicData().getPic(); 
     259        } 
     260 
     261        if (anchor!=null) { 
     262                if (anchor.getGraphic()==null 
     263                                || anchor.getGraphic().getGraphicData()==null 
     264                                || anchor.getGraphic().getGraphicData().getPic()==null) { 
     265                        log.error("pic missing!!"); 
     266                        return null;                     
     267                } 
     268                return anchor.getGraphic().getGraphicData().getPic(); 
     269        } 
     270         
     271        log.error("Anchor and inline both null!"); 
     272        return null; 
     273    } 
    177274 
    178275 
     
    190287     * @return 
    191288     */ 
    192     public static DocumentFragment createHtmlImgE20( 
    193                 WordprocessingMLPackage wmlPackage, 
     289    public static DocumentFragment createHtmlImgE20(WordprocessingMLPackage wmlPackage, 
    194290                String imageDirPath, 
    195291                NodeIterator wpInline) { 
     
    213309     * @return 
    214310     */ 
    215     public static DocumentFragment createXslFoImgE20( 
    216                 WordprocessingMLPackage wmlPackage, 
     311    public static DocumentFragment createXslFoImgE20(WordprocessingMLPackage wmlPackage, 
    217312                String imageDirPath, 
    218313                NodeIterator wpInline) { 
     
    230325     
    231326    private void readDimensions() { 
    232         CTPositiveSize2D size2d = inline.getExtent(); 
     327        CTPositiveSize2D size2d = getExtent(); 
    233328        if (size2d==null) { 
    234329                log.warn("wp:inline/wp:extent missing!"); 
     
    245340        }        
    246341    } 
     342     
     343     
     344    private CTPositiveSize2D getExtent() { 
     345         
     346        if (inline!=null) { 
     347                return inline.getExtent(); 
     348        } 
     349 
     350        if (anchor!=null) { 
     351                return anchor.getExtent(); 
     352        } 
     353         
     354        log.error("Anchor and inline both null!"); 
     355        return null; 
     356    } 
     357     
     358     
    247359    private final int extentToPixelConversionFactor = 12700; 
    248360     
    249361    private void readHyperlink() { 
    250         if (inline.getDocPr()!=null 
    251                         && inline.getDocPr().getHlinkClick()!=null) { 
     362        if (getDocPr()!=null 
     363                        && getDocPr().getHlinkClick()!=null) { 
    252364                 
    253                 String linkRelId = inline.getDocPr().getHlinkClick().getId(); 
     365                String linkRelId = getDocPr().getHlinkClick().getId(); 
    254366                 
    255367            if ( linkRelId!=null && !linkRelId.equals("") )  
     
    266378            } 
    267379             
    268             targetFrame = inline.getDocPr().getHlinkClick().getTgtFrame(); 
    269             tooltip = inline.getDocPr().getHlinkClick().getTooltip(); 
    270         } 
    271     } 
     380            targetFrame = getDocPr().getHlinkClick().getTgtFrame(); 
     381            tooltip = getDocPr().getHlinkClick().getTooltip(); 
     382        } 
     383    } 
     384 
     385    private CTNonVisualDrawingProps getDocPr() { 
     386         
     387        if (inline!=null) { 
     388                if (inline.getDocPr()==null) { 
     389                        log.error("DocPr missing!!"); 
     390                        return null;                     
     391                } 
     392                return inline.getDocPr(); 
     393        } 
     394 
     395        if (anchor!=null) { 
     396                if (anchor.getDocPr()==null) { 
     397                        log.error("DocPr missing!!"); 
     398                        return null;                     
     399                } 
     400                return anchor.getDocPr(); 
     401        } 
     402         
     403        log.error("Anchor and inline both null!"); 
     404        return null; 
     405    } 
     406     
    272407     
    273408        private void handleImageRel(String imgRelId, String imageDirPath) { 
Note: See TracChangeset for help on using the changeset viewer.