Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docx4j yf dpifix #281

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.2.3-SNAPSHOT</version>
<version>3.2.3-yf</version>
<packaging>jar</packaging>
<name>docx4j</name>
<description>
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/org/docx4j/UnitsOfMeasurement.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,25 @@ public static int pxToTwip(float px) {
return inchToTwip(inch);

}


/**
* Convert an inches value to twips and return as a double.
*/
public static double inchToTwipDouble(double inch) {
// 1440 twip = 1 inch;
return inch * 1440;
}

/**
* Convert a pixel value to twips and return as a double.
* This may be useful where the returned value will then be converted to another
* unit, and we don't want to lose precision due to rounding.
*/
public static double pxToTwipDouble(double px) {
double inch = px / DPI;
return inchToTwipDouble(inch);
}


/**
* 1440 twip = 1 inch;Try to guess whether inches or mm looks nicer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1163,10 +1163,16 @@ public static CxCy scale(ImageInfo imageInfo, PageDimensions page) {
log.debug("writableWidthTwips: " + writableWidthTwips);

ImageSize size = imageInfo.getSize();

// get image size in pixels
Dimension2D dpx = size.getDimensionPx();

Dimension2D dPt = size.getDimensionPt();
double imageWidthTwips = dPt.getWidth() * 20;
log.debug("imageWidthTwips: " + imageWidthTwips);
// convert pixels to twips (uses configured DPI setting - NOT the image DPI)
double imageWidthTwips = UnitsOfMeasurement.pxToTwipDouble(dpx.getWidth());
double imageHeightTwips = UnitsOfMeasurement.pxToTwipDouble(dpx.getHeight());

log.debug("imageWidthTwips: " + imageWidthTwips);
log.debug("imageHeightTwips: " + imageHeightTwips);

long cx;
long cy;
Expand All @@ -1177,14 +1183,14 @@ public static CxCy scale(ImageInfo imageInfo, PageDimensions page) {
scaled = true;

cx = UnitsOfMeasurement.twipToEMU(writableWidthTwips);
cy = UnitsOfMeasurement.twipToEMU(dPt.getHeight() * 20 * writableWidthTwips / imageWidthTwips);
cy = UnitsOfMeasurement.twipToEMU(imageHeightTwips * writableWidthTwips / imageWidthTwips);

} else {

log.debug("Scaling image - not necessary");

cx = UnitsOfMeasurement.twipToEMU(imageWidthTwips);
cy = UnitsOfMeasurement.twipToEMU(dPt.getHeight() * 20);
cy = UnitsOfMeasurement.twipToEMU(imageHeightTwips);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testTblStyle_AllSilent() throws Exception {

ParagraphStylesInTableFix.process(wordMLPackage);

Style s = getStyle(wordMLPackage, STYLE_NAME);
Style s = getStyle(wordMLPackage, getStyleName());
Assert.assertTrue(s.getRPr().getSz().getVal().intValue()==EXPECTED_RESULT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testTblStyle_AllSilent() throws Exception {

ParagraphStylesInTableFix.process(wordMLPackage);

Style s = getStyle(wordMLPackage, STYLE_NAME);
Style s = getStyle(wordMLPackage, getStyleName());
Assert.assertTrue(s.getRPr().getSz().getVal().intValue()==40);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void testTblStyle_AllSilent() throws Exception {

ParagraphStylesInTableFix.process(wordMLPackage);

Style s = getStyle(wordMLPackage, STYLE_NAME);
Style s = getStyle(wordMLPackage, getStyleName());
Assert.assertTrue(s.getRPr().getSz().getVal().intValue()==EXPECTED_RESULT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testTblStyle_AllSilent() throws Exception {

ParagraphStylesInTableFix.process(wordMLPackage);

Style s = getStyle(wordMLPackage, STYLE_NAME);
Style s = getStyle(wordMLPackage, getStyleName());
Assert.assertTrue(s.getRPr().getSz().getVal().intValue()==40);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ public void init() {

initTbls(false); // use TableNormal
initOtherXml();

STYLE_NAME = "Normal-TableNormal-BR";

}

@Override
protected String getStyleName() {
return "Normal-TableNormal-BR";
}


Expand Down Expand Up @@ -135,7 +139,7 @@ public void testTblStyle_AllSilent() throws Exception {

ParagraphStylesInTableFix.process(wordMLPackage);

Style s = getStyle(wordMLPackage, STYLE_NAME);
Style s = getStyle(wordMLPackage, getStyleName());
Assert.assertTrue(s.getRPr().getSz()==null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ public void init() {

initTbls(false); // use TableNormal
initOtherXml();

STYLE_NAME = "Normal-TableNormal-BR";

}

@Override
protected String getStyleName() {
return "Normal-TableNormal-BR";
}


@Test
Expand Down Expand Up @@ -114,7 +117,7 @@ public void testTblStyle_AllSilent() throws Exception {

ParagraphStylesInTableFix.process(wordMLPackage);

Style s = getStyle(wordMLPackage, STYLE_NAME);
Style s = getStyle(wordMLPackage, getStyleName());
Assert.assertTrue(s.getRPr().getSz()==null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public abstract class PStyleTableAbstract {

protected static boolean OVERRIDE;
protected static int EXPECTED_RESULT;

protected static String STYLE_NAME = "Normal-TableGrid-BR";

protected String getStyleName() {
return "Normal-TableGrid-BR";
}

static String styles_inRPrDefault;
static String styles_inNormal;
Expand Down Expand Up @@ -74,7 +76,8 @@ protected WordprocessingMLPackage test(String documentXml, String styleXml, int
//
// this.saveDocx(wordMLPackage, null);

Style s = getStyle(wordMLPackage, STYLE_NAME);
Style s = getStyle(wordMLPackage, getStyleName());

Assert.assertTrue(s.getRPr().getSz().getVal().intValue()==expectedResult);

return wordMLPackage;
Expand Down