add picture height / width to PicturesManager arguments

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1180740 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-10-09 23:00:39 +00:00
parent 1df261a03c
commit 871070a8e1
6 changed files with 90 additions and 82 deletions

View File

@ -49,6 +49,7 @@ import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -672,9 +673,17 @@ public abstract class AbstractWordConverter
// usual shape? // usual shape?
return; 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 ); final PictureType type = PictureType.findMatchingType( pictureData );
String path = getPicturesManager().savePicture( pictureData, type, String path = getPicturesManager()
"s" + characterRun.getStartOffset() + "." + type ); .savePicture( pictureData, type,
"s" + characterRun.getStartOffset() + "." + type,
width, height );
processDrawnObject( doc, characterRun, officeDrawing, path, block ); processDrawnObject( doc, characterRun, officeDrawing, path, block );
} }
@ -778,8 +787,43 @@ public abstract class AbstractWordConverter
Element currentBlock, Range textRange, int currentTableLevel, Element currentBlock, Range textRange, int currentTableLevel,
String hyperlink ); 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, protected abstract void processImage( Element currentBlock,
boolean inlined, Picture picture ); boolean inlined, Picture picture, String url );
protected abstract void processLineBreak( Element block, protected abstract void processLineBreak( Element block,
CharacterRun characterRun ); CharacterRun characterRun );

View File

@ -38,9 +38,21 @@ public interface PicturesManager
* *
* @param content * @param content
* picture 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 * @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 * 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. * 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 );
} }

View File

@ -366,49 +366,6 @@ public class WordToFoConverter extends AbstractWordConverter
basicLink ); 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, protected void processImage( Element currentBlock, boolean inlined,
Picture picture, String url ) Picture picture, String url )
{ {
@ -418,6 +375,16 @@ public class WordToFoConverter extends AbstractWordConverter
currentBlock.appendChild( externalGraphic ); 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 @Override
protected void processLineBreak( Element block, CharacterRun characterRun ) protected void processLineBreak( Element block, CharacterRun characterRun )
{ {

View File

@ -313,39 +313,10 @@ public class WordToHtmlConverter extends AbstractWordConverter
basicLink ); basicLink );
} }
/** @Override
* This method shall store image bytes in external file and convert it if protected void processImageWithoutPicturesManager( Element currentBlock,
* necessary. Images shall be stored using PNG format. Other formats may be boolean inlined, Picture picture )
* 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 )
{ {
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 // no default implementation -- skip
currentBlock.appendChild( htmlDocumentFacade.document currentBlock.appendChild( htmlDocumentFacade.document
.createComment( "Image link to '" .createComment( "Image link to '"

View File

@ -244,8 +244,7 @@ public class WordToTextConverter extends AbstractWordConverter
} }
@Override @Override
public void processDocumentPart( HWPFDocumentCore wordDocument, public void processDocumentPart( HWPFDocumentCore wordDocument, Range range )
Range range )
{ {
super.processDocumentPart( wordDocument, range ); super.processDocumentPart( wordDocument, range );
afterProcess(); afterProcess();
@ -295,6 +294,20 @@ public class WordToTextConverter extends AbstractWordConverter
// ignore // 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 @Override
protected void processLineBreak( Element block, CharacterRun characterRun ) protected void processLineBreak( Element block, CharacterRun characterRun )
{ {

View File

@ -69,7 +69,8 @@ public class TestWordToHtmlConverter extends TestCase
wordToHtmlConverter.setPicturesManager( new PicturesManager() wordToHtmlConverter.setPicturesManager( new PicturesManager()
{ {
public String savePicture( byte[] content, public String savePicture( byte[] content,
PictureType pictureType, String suggestedName ) PictureType pictureType, String suggestedName,
float widthInches, float heightInches )
{ {
return suggestedName; return suggestedName;
} }