more progress with WordToFoExtractor, see Bugzilla 51351

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1136001 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2011-06-15 11:41:22 +00:00
parent ffc5d68b12
commit cbb3176c72
2 changed files with 236 additions and 156 deletions

View File

@ -279,8 +279,27 @@ public class WordToFoExtractor {
} }
} }
@SuppressWarnings("unused") /**
protected void processImage(Element currentBlock, Picture picture) { * 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) {
// no default implementation -- skip // no default implementation -- skip
} }
@ -329,11 +348,24 @@ public class WordToFoExtractor {
for (int c = 0; c < charRuns; c++) { for (int c = 0; c < charRuns; c++) {
CharacterRun characterRun = paragraph.getCharacterRun(c); CharacterRun characterRun = paragraph.getCharacterRun(c);
if (hwpfDocument.getPicturesTable().hasPicture(characterRun)) {
Picture picture = hwpfDocument.getPicturesTable()
.extractPicture(characterRun, true);
processImage(block, characterRun.text().charAt(0) == 0x01,
picture);
continue;
}
String text = characterRun.text(); String text = characterRun.text();
if (text.getBytes().length == 0) if (text.getBytes().length == 0)
continue; continue;
if (text.getBytes()[0] == FIELD_BEGIN_MARK) { if (text.getBytes()[0] == FIELD_BEGIN_MARK) {
/*
* check if we have a field with calculated image as a result.
* MathType equation, for example.
*/
int skipTo = tryImageWithinField(hwpfDocument, paragraph, c, int skipTo = tryImageWithinField(hwpfDocument, paragraph, c,
block); block);
@ -553,6 +585,7 @@ public class WordToFoExtractor {
Paragraph paragraph, int beginMark, Element currentBlock) { Paragraph paragraph, int beginMark, Element currentBlock) {
int separatorMark = -1; int separatorMark = -1;
int pictureMark = -1; int pictureMark = -1;
int pictureChar = Integer.MIN_VALUE;
int endMark = -1; int endMark = -1;
for (int c = beginMark + 1; c < paragraph.numCharacterRuns(); c++) { for (int c = beginMark + 1; c < paragraph.numCharacterRuns(); c++) {
CharacterRun characterRun = paragraph.getCharacterRun(c); CharacterRun characterRun = paragraph.getCharacterRun(c);
@ -582,12 +615,13 @@ public class WordToFoExtractor {
} }
if (hwpfDocument.getPicturesTable().hasPicture(characterRun)) { if (hwpfDocument.getPicturesTable().hasPicture(characterRun)) {
if (pictureMark != -1) { if (c != -1) {
// double; // double;
return beginMark; return beginMark;
} }
pictureMark = c; pictureMark = c;
pictureChar = characterRun.text().charAt(0);
continue; continue;
} }
} }
@ -598,12 +632,12 @@ public class WordToFoExtractor {
final CharacterRun pictureRun = paragraph.getCharacterRun(pictureMark); final CharacterRun pictureRun = paragraph.getCharacterRun(pictureMark);
final Picture picture = hwpfDocument.getPicturesTable().extractPicture( final Picture picture = hwpfDocument.getPicturesTable().extractPicture(
pictureRun, true); pictureRun, true);
processImage(currentBlock, picture);
processImage(currentBlock, pictureChar == 0x01, picture);
return endMark; return endMark;
} }
/** /**
* Java main() interface to interact with WordToFoExtractor * Java main() interface to interact with WordToFoExtractor
* *

View File

@ -9,6 +9,7 @@ import org.apache.poi.hwpf.usermodel.BorderCode;
import org.apache.poi.hwpf.usermodel.CharacterProperties; import org.apache.poi.hwpf.usermodel.CharacterProperties;
import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section; import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.hwpf.usermodel.SectionProperties; import org.apache.poi.hwpf.usermodel.SectionProperties;
@ -179,6 +180,31 @@ public class WordToFoUtils {
} }
} }
public static String getJustification(int js) {
switch (js) {
case 0:
return "start";
case 1:
return "center";
case 2:
return "end";
case 3:
case 4:
return "justify";
case 5:
return "center";
case 6:
return "left";
case 7:
return "start";
case 8:
return "end";
case 9:
return "justify";
}
return "";
}
public static String getListItemNumberLabel(int number, int format) { public static String getListItemNumberLabel(int number, int format) {
if (format != 0) if (format != 0)
@ -283,6 +309,9 @@ public class WordToFoUtils {
textDecorations.append(" "); textDecorations.append(" ");
textDecorations.append("underline"); textDecorations.append("underline");
} }
if (characterRun.isVanished()) {
inline.setAttribute("visibility", "hidden");
}
if (textDecorations.length() > 0) { if (textDecorations.length() > 0) {
inline.setAttribute("text-decoration", textDecorations.toString()); inline.setAttribute("text-decoration", textDecorations.toString());
} }
@ -336,39 +365,9 @@ public class WordToFoUtils {
public static void setJustification(Paragraph paragraph, public static void setJustification(Paragraph paragraph,
final Element element) { final Element element) {
final int justification = paragraph.getJustification(); String justification = getJustification(paragraph.getJustification());
switch (justification) { if (isNotEmpty(justification))
case 0: element.setAttribute("text-align", justification);
element.setAttribute("text-align", "start");
break;
case 1:
element.setAttribute("text-align", "center");
break;
case 2:
element.setAttribute("text-align", "end");
break;
case 3:
element.setAttribute("text-align", "justify");
break;
case 4:
element.setAttribute("text-align", "justify");
break;
case 5:
element.setAttribute("text-align", "center");
break;
case 6:
element.setAttribute("text-align", "left");
break;
case 7:
element.setAttribute("text-align", "start");
break;
case 8:
element.setAttribute("text-align", "end");
break;
case 9:
element.setAttribute("text-align", "justify");
break;
}
} }
public static void setParagraphProperties(Paragraph paragraph, Element block) { public static void setParagraphProperties(Paragraph paragraph, Element block) {
@ -399,6 +398,53 @@ public class WordToFoUtils {
block.setAttribute("white-space-collapse", "false"); block.setAttribute("white-space-collapse", "false");
} }
public static void setPictureProperties(Picture picture,
Element graphicElement) {
final int aspectRatioX = picture.getAspectRatioX();
final int aspectRatioY = picture.getAspectRatioY();
if (aspectRatioX > 0) {
graphicElement.setAttribute("content-width", ((picture.getDxaGoal()
* aspectRatioX / 100) / WordToFoUtils.TWIPS_PER_PT)
+ "pt");
} else
graphicElement.setAttribute("content-width",
(picture.getDxaGoal() / WordToFoUtils.TWIPS_PER_PT) + "pt");
if (aspectRatioY > 0)
graphicElement
.setAttribute("content-height", ((picture.getDyaGoal()
* aspectRatioY / 100) / WordToFoUtils.TWIPS_PER_PT)
+ "pt");
else
graphicElement.setAttribute("content-height",
(picture.getDyaGoal() / WordToFoUtils.TWIPS_PER_PT) + "pt");
if (aspectRatioX <= 0 || aspectRatioY <= 0) {
graphicElement.setAttribute("scaling", "uniform");
} else {
graphicElement.setAttribute("scaling", "non-uniform");
}
graphicElement.setAttribute("vertical-align", "text-bottom");
if (picture.getDyaCropTop() != 0 || picture.getDxaCropRight() != 0
|| picture.getDyaCropBottom() != 0
|| picture.getDxaCropLeft() != 0) {
int rectTop = picture.getDyaCropTop() / WordToFoUtils.TWIPS_PER_PT;
int rectRight = picture.getDxaCropRight()
/ WordToFoUtils.TWIPS_PER_PT;
int rectBottom = picture.getDyaCropBottom()
/ WordToFoUtils.TWIPS_PER_PT;
int rectLeft = picture.getDxaCropLeft()
/ WordToFoUtils.TWIPS_PER_PT;
graphicElement.setAttribute("clip", "rect(" + rectTop + "pt, "
+ rectRight + "pt, " + rectBottom + "pt, " + rectLeft
+ "pt)");
graphicElement.setAttribute("oveerflow", "hidden");
}
}
public static void setTableCellProperties(TableRow tableRow, public static void setTableCellProperties(TableRow tableRow,
TableCell tableCell, Element element, boolean toppest, TableCell tableCell, Element element, boolean toppest,
boolean bottomest, boolean leftest, boolean rightest) { boolean bottomest, boolean leftest, boolean rightest) {