]> source.dussan.org Git - poi.git/commitdiff
add picture height / width to PicturesManager arguments
authorSergey Vladimirov <sergey@apache.org>
Sun, 9 Oct 2011 23:00:39 +0000 (23:00 +0000)
committerSergey Vladimirov <sergey@apache.org>
Sun, 9 Oct 2011 23:00:39 +0000 (23:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1180740 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/PicturesManager.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java
src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java

index 69e8b8428c7218469d0a04ed344cdc9159595f53..30b7f5f1bc960df5440b790f63ca6d821282f41c 100644 (file)
@@ -49,6 +49,7 @@ import org.apache.poi.hwpf.usermodel.TableCell;
 import org.apache.poi.hwpf.usermodel.TableRow;
 import org.apache.poi.poifs.filesystem.Entry;
 import org.apache.poi.util.Beta;
+import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.w3c.dom.Document;
@@ -672,9 +673,17 @@ public abstract class AbstractWordConverter
             // usual shape?
             return;
 
+        float width = officeDrawing.getRectangleRight()
+                - officeDrawing.getRectangleLeft()
+                / AbstractWordUtils.TWIPS_PER_INCH;
+        float height = ( officeDrawing.getRectangleBottom() - officeDrawing
+                .getRectangleTop() ) / AbstractWordUtils.TWIPS_PER_INCH;
+
         final PictureType type = PictureType.findMatchingType( pictureData );
-        String path = getPicturesManager().savePicture( pictureData, type,
-                "s" + characterRun.getStartOffset() + "." + type );
+        String path = getPicturesManager()
+                .savePicture( pictureData, type,
+                        "s" + characterRun.getStartOffset() + "." + type,
+                        width, height );
 
         processDrawnObject( doc, characterRun, officeDrawing, path, block );
     }
@@ -778,8 +787,43 @@ public abstract class AbstractWordConverter
             Element currentBlock, Range textRange, int currentTableLevel,
             String hyperlink );
 
+    protected void processImage( Element currentBlock, boolean inlined,
+            Picture picture )
+    {
+        PicturesManager fileManager = getPicturesManager();
+        if ( fileManager != null )
+        {
+            final int aspectRatioX = picture.getHorizontalScalingFactor();
+            final int aspectRatioY = picture.getVerticalScalingFactor();
+
+            final float imageWidth = aspectRatioX > 0 ? picture.getDxaGoal()
+                    * aspectRatioX / 1000 / AbstractWordUtils.TWIPS_PER_INCH
+                    : picture.getDxaGoal() / AbstractWordUtils.TWIPS_PER_INCH;
+            final float imageHeight = aspectRatioY > 0 ? picture.getDyaGoal()
+                    * aspectRatioY / 1000 / AbstractWordUtils.TWIPS_PER_INCH
+                    : picture.getDyaGoal() / AbstractWordUtils.TWIPS_PER_INCH;
+
+            String url = fileManager.savePicture( picture.getContent(),
+                    picture.suggestPictureType(),
+                    picture.suggestFullFileName(), imageWidth, imageHeight );
+
+            if ( WordToFoUtils.isNotEmpty( url ) )
+            {
+                processImage( currentBlock, inlined, picture, url );
+                return;
+            }
+        }
+
+        processImageWithoutPicturesManager( currentBlock, inlined, picture );
+
+    }
+
+    @Internal
+    protected abstract void processImageWithoutPicturesManager(
+            Element currentBlock, boolean inlined, Picture picture );
+
     protected abstract void processImage( Element currentBlock,
-            boolean inlined, Picture picture );
+            boolean inlined, Picture picture, String url );
 
     protected abstract void processLineBreak( Element block,
             CharacterRun characterRun );
index d4befb75a3a9d4e58824e2ebb2e0e9f5982b2bce..e8bb2e2c5a9dc749d23cea5b34880628b89dade3 100644 (file)
@@ -38,9 +38,21 @@ public interface PicturesManager
      * 
      * @param content
      *            picture content
+     * @param pictureType
+     *            detected picture type (may be {@link PictureType#UNKNOWN}
+     * @param suggestedName
+     *            suggested picture name (based on picture offset in file),
+     *            supposed to be unique
+     * @param widthInches
+     *            display width in inches (scaled). May be useful for rendering
+     *            vector images (such as EMF or WMF)
+     * @param heightInches
+     *            display height in inches (scaled). May be useful for rendering
+     *            vector images (such as EMF or WMF)
      * @return path to file that can be used as reference in HTML (img's src) of
      *         XLS FO (fo:external-graphic's src) or <tt>null</tt> if image were
      *         not saved and should not be referenced from result HTML / FO.
      */
-    String savePicture( byte[] content, PictureType pictureType, String suggestedName );
+    String savePicture( byte[] content, PictureType pictureType,
+            String suggestedName, float widthInches, float heightInches );
 }
index cde7076aac0328c40be21fc77090ce6d66f4b590..c57d5d0a1c98a8aed2feb8c9275722c7b82575af 100644 (file)
@@ -366,49 +366,6 @@ public class WordToFoConverter extends AbstractWordConverter
                     basicLink );
     }
 
-    /**
-     * This method shall store image bytes in external file and convert it if
-     * necessary. Images shall be stored using PNG format (for bitmap) or SVG
-     * (for vector). Other formats may be not supported by your XSL FO
-     * processor.
-     * <p>
-     * Please note the
-     * {@link WordToFoUtils#setPictureProperties(Picture, Element)} method.
-     * 
-     * @param currentBlock
-     *            currently processed FO element, like <tt>fo:block</tt>. Shall
-     *            be used as parent of newly created
-     *            <tt>fo:external-graphic</tt> or
-     *            <tt>fo:instream-foreign-object</tt>
-     * @param inlined
-     *            if image is inlined
-     * @param picture
-     *            HWPF object, contained picture data and properties
-     */
-    protected void processImage( Element currentBlock, boolean inlined,
-            Picture picture )
-    {
-        PicturesManager fileManager = getPicturesManager();
-        if ( fileManager != null )
-        {
-            String url = fileManager
-                    .savePicture( picture.getContent(),
-                            picture.suggestPictureType(),
-                            picture.suggestFullFileName() );
-
-            if ( WordToFoUtils.isNotEmpty( url ) )
-            {
-                processImage( currentBlock, inlined, picture, url );
-                return;
-            }
-        }
-
-        // no default implementation -- skip
-        currentBlock.appendChild( foDocumentFacade.document
-                .createComment( "Image link to '"
-                        + picture.suggestFullFileName() + "' can be here" ) );
-    }
-
     protected void processImage( Element currentBlock, boolean inlined,
             Picture picture, String url )
     {
@@ -418,6 +375,16 @@ public class WordToFoConverter extends AbstractWordConverter
         currentBlock.appendChild( externalGraphic );
     }
 
+    @Override
+    protected void processImageWithoutPicturesManager( Element currentBlock,
+            boolean inlined, Picture picture )
+    {
+        // no default implementation -- skip
+        currentBlock.appendChild( foDocumentFacade.document
+                .createComment( "Image link to '"
+                        + picture.suggestFullFileName() + "' can be here" ) );
+    }
+
     @Override
     protected void processLineBreak( Element block, CharacterRun characterRun )
     {
index b9450f1685b16b6315366ede309503072d411207..5cea888f2dd7b40723e33ea20226e3e1fdc91bd0 100644 (file)
@@ -313,39 +313,10 @@ public class WordToHtmlConverter extends AbstractWordConverter
                     basicLink );
     }
 
-    /**
-     * This method shall store image bytes in external file and convert it if
-     * necessary. Images shall be stored using PNG format. Other formats may be
-     * not supported by user browser.
-     * <p>
-     * Please note the {@link #processImage(Element, boolean, Picture, String)}.
-     * 
-     * @param currentBlock
-     *            currently processed HTML element, like <tt>p</tt>. Shall be
-     *            used as parent of newly created <tt>img</tt>
-     * @param inlined
-     *            if image is inlined
-     * @param picture
-     *            HWPF object, contained picture data and properties
-     */
-    protected void processImage( Element currentBlock, boolean inlined,
-            Picture picture )
+    @Override
+    protected void processImageWithoutPicturesManager( Element currentBlock,
+            boolean inlined, Picture picture )
     {
-        PicturesManager fileManager = getPicturesManager();
-        if ( fileManager != null )
-        {
-            String url = fileManager
-                    .savePicture( picture.getContent(),
-                            picture.suggestPictureType(),
-                            picture.suggestFullFileName() );
-
-            if ( WordToHtmlUtils.isNotEmpty( url ) )
-            {
-                processImage( currentBlock, inlined, picture, url );
-                return;
-            }
-        }
-
         // no default implementation -- skip
         currentBlock.appendChild( htmlDocumentFacade.document
                 .createComment( "Image link to '"
index 57ec92f8eebe676027ac5870a686381bfe83cabd..0933f2854f83b9ca65487048dead32ad18e16a15 100644 (file)
@@ -244,8 +244,7 @@ public class WordToTextConverter extends AbstractWordConverter
     }
 
     @Override
-    public void processDocumentPart( HWPFDocumentCore wordDocument,
-            Range range )
+    public void processDocumentPart( HWPFDocumentCore wordDocument, Range range )
     {
         super.processDocumentPart( wordDocument, range );
         afterProcess();
@@ -295,6 +294,20 @@ public class WordToTextConverter extends AbstractWordConverter
         // ignore
     }
 
+    @Override
+    protected void processImage( Element currentBlock, boolean inlined,
+            Picture picture, String url )
+    {
+        // ignore
+    }
+
+    @Override
+    protected void processImageWithoutPicturesManager( Element currentBlock,
+            boolean inlined, Picture picture )
+    {
+        // ignore
+    }
+
     @Override
     protected void processLineBreak( Element block, CharacterRun characterRun )
     {
index a3efb666a1e75fa91945ecb50b573e8a6342729c..a7a8212398603bdecdba659d66c3dadfffc79ca9 100644 (file)
@@ -69,7 +69,8 @@ public class TestWordToHtmlConverter extends TestCase
             wordToHtmlConverter.setPicturesManager( new PicturesManager()
             {
                 public String savePicture( byte[] content,
-                        PictureType pictureType, String suggestedName )
+                        PictureType pictureType, String suggestedName,
+                        float widthInches, float heightInches )
                 {
                     return suggestedName;
                 }