-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from DEXPRO-Solutions-GmbH/fix/landscape-portra…
…it-check Fix unwanted scaling on landscape A4
- Loading branch information
Showing
7 changed files
with
221 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package one.squeeze.pdftools.app.scale; | ||
|
||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.pdmodel.PDPage; | ||
|
||
import java.io.IOException; | ||
|
||
public interface IScaler { | ||
|
||
public void scale(PDDocument pdf, PDPage page) throws IOException; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/one/squeeze/pdftools/app/scale/NoopScaler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package one.squeeze.pdftools.app.scale; | ||
|
||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.pdmodel.PDPage; | ||
|
||
import java.io.IOException; | ||
|
||
public class NoopScaler implements IScaler { | ||
@Override | ||
public void scale(PDDocument pdf, PDPage page) throws IOException {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package one.squeeze.pdftools.app.scale; | ||
|
||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.pdmodel.PDPage; | ||
import org.apache.pdfbox.pdmodel.PDPageContentStream; | ||
import org.apache.pdfbox.pdmodel.common.PDRectangle; | ||
import org.apache.pdfbox.util.Matrix; | ||
|
||
import java.io.IOException; | ||
|
||
public class Scaler implements IScaler { | ||
|
||
private final float factor; | ||
|
||
private final PDRectangle targetMediaBox; | ||
|
||
public Scaler(float factor, PDRectangle targetMediaBox) { | ||
if (factor <= 0 || factor == 1) { | ||
throw new IllegalArgumentException("Invalid factor: " + factor); | ||
} | ||
if (targetMediaBox == null) { | ||
throw new IllegalArgumentException("Null targetMediaBox argument"); | ||
} | ||
|
||
this.factor = factor; | ||
this.targetMediaBox = targetMediaBox; | ||
} | ||
|
||
@Override | ||
public void scale(PDDocument pdf, PDPage page) throws IOException { | ||
PDPageContentStream contentStream = new PDPageContentStream(pdf, page, PDPageContentStream.AppendMode.PREPEND, false); | ||
contentStream.transform(Matrix.getScaleInstance(factor, factor)); | ||
contentStream.close(); | ||
|
||
page.setMediaBox(targetMediaBox); | ||
} | ||
|
||
public float getFactor() { | ||
return factor; | ||
} | ||
|
||
public PDRectangle getTargetMediaBox() { | ||
return targetMediaBox; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/test/java/one/squeeze/pdftools/cli/cmds/FixPDFCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package one.squeeze.pdftools.cli.cmds; | ||
|
||
import one.squeeze.pdftools.app.scale.IScaler; | ||
import one.squeeze.pdftools.app.scale.NoopScaler; | ||
import one.squeeze.pdftools.app.scale.Scaler; | ||
import org.apache.pdfbox.pdmodel.PDPage; | ||
import org.apache.pdfbox.pdmodel.common.PDRectangle; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class FixPDFCommandTest { | ||
|
||
|
||
/** | ||
* This constant is used to compare the expected output of factor calculation against 1. | ||
* This is a nasty workarround, it would be better to improve the assertions using this constant. | ||
* Either way, this currently works fine. | ||
*/ | ||
public static final double TEN_PERCENT = 0.10000000149011612; | ||
|
||
@Test | ||
void testBuildScaler_NoopOnA4Portrait() { | ||
PDPage page = new PDPage(); | ||
PDRectangle box = new PDRectangle(); | ||
box.setUpperRightX(595); | ||
box.setUpperRightY(841); | ||
page.setMediaBox(box); | ||
|
||
IScaler scaler = FixPDFCommand.buildScaler(page); | ||
assertTrue(scaler instanceof NoopScaler); | ||
} | ||
|
||
@Test | ||
void testBuildScaler_NoopOnA4Landscape() { | ||
PDPage page = new PDPage(); | ||
PDRectangle box = new PDRectangle(); | ||
box.setUpperRightX(841); | ||
box.setUpperRightY(595); | ||
page.setMediaBox(box); | ||
|
||
IScaler scaler = FixPDFCommand.buildScaler(page); | ||
assertTrue(scaler instanceof NoopScaler); | ||
} | ||
|
||
@Test | ||
void testBuildScaler_NoopOnSmallerThanA4Portrait() { | ||
PDPage page = new PDPage(); | ||
PDRectangle box = new PDRectangle(); | ||
box.setUpperRightX((float) 595 / 2); | ||
box.setUpperRightY((float) 841 / 2); | ||
page.setMediaBox(box); | ||
|
||
IScaler scaler = FixPDFCommand.buildScaler(page); | ||
assertTrue(scaler instanceof NoopScaler); | ||
} | ||
|
||
@Test | ||
void testBuildScaler_NoopOnSmallerThanA4Landscape() { | ||
PDPage page = new PDPage(); | ||
PDRectangle box = new PDRectangle(); | ||
box.setUpperRightX((float) 841 / 2); | ||
box.setUpperRightY((float) 595 / 2); | ||
page.setMediaBox(box); | ||
|
||
IScaler scaler = FixPDFCommand.buildScaler(page); | ||
assertTrue(scaler instanceof NoopScaler); | ||
} | ||
|
||
@Test | ||
void testBuildScaler_ScalesLargePortrait() { | ||
PDPage page = new PDPage(); | ||
PDRectangle box = new PDRectangle(); | ||
box.setUpperRightX(595 * 10); | ||
box.setUpperRightY(841 * 10); | ||
page.setMediaBox(box); | ||
|
||
Scaler scaler = (Scaler) FixPDFCommand.buildScaler(page); | ||
assertEquals(TEN_PERCENT, scaler.getFactor()); | ||
assertEquals(PDRectangle.A4, scaler.getTargetMediaBox()); | ||
} | ||
|
||
@Test | ||
void testBuildScaler_ScalesLargeLandscape() { | ||
PDPage page = new PDPage(); | ||
PDRectangle box = new PDRectangle(); | ||
box.setUpperRightX(841 * 10); | ||
box.setUpperRightY(595 * 10); | ||
page.setMediaBox(box); | ||
|
||
Scaler scaler = (Scaler) FixPDFCommand.buildScaler(page); | ||
assertEquals(TEN_PERCENT, scaler.getFactor()); | ||
assertEquals(PDRectangle.A4, scaler.getTargetMediaBox()); | ||
} | ||
} |