From f92821eb1c287ea08df00d9983010f17fc43b0b0 Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Sun, 9 Oct 2011 23:00:39 +0000 Subject: [PATCH] add picture height / width to PicturesManager arguments git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1180740 13f79535-47bb-0310-9956-ffa450edef68 --- .../hwpf/converter/AbstractWordConverter.java | 50 +++++++++++++++-- .../poi/hwpf/converter/PicturesManager.java | 14 ++++- .../poi/hwpf/converter/WordToFoConverter.java | 53 ++++--------------- .../hwpf/converter/WordToHtmlConverter.java | 35 ++---------- .../hwpf/converter/WordToTextConverter.java | 17 +++++- .../converter/TestWordToHtmlConverter.java | 3 +- 6 files changed, 90 insertions(+), 82 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java index 69e8b8428c..30b7f5f1bc 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/AbstractWordConverter.java @@ -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 ); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/PicturesManager.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/PicturesManager.java index d4befb75a3..e8bb2e2c5a 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/PicturesManager.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/PicturesManager.java @@ -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 null 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 ); } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java index cde7076aac..c57d5d0a1c 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToFoConverter.java @@ -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. - *

- * Please note the - * {@link WordToFoUtils#setPictureProperties(Picture, Element)} method. - * - * @param currentBlock - * currently processed FO element, like fo:block. Shall - * be used as parent of newly created - * fo:external-graphic or - * fo:instream-foreign-object - * @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 ) { diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java index b9450f1685..5cea888f2d 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java @@ -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. - *

- * Please note the {@link #processImage(Element, boolean, Picture, String)}. - * - * @param currentBlock - * currently processed HTML element, like p. Shall be - * used as parent of newly created img - * @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 '" diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java index 57ec92f8ee..0933f2854f 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToTextConverter.java @@ -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 ) { diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java index a3efb666a1..a7a8212398 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java @@ -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; }