Skip to content

Commit

Permalink
Refactor shouldScale into dedicated, testable method
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiante committed Mar 21, 2024
1 parent 09085c1 commit 7ff590e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
14 changes: 10 additions & 4 deletions src/main/java/one/squeeze/pdftools/cli/cmds/FixPDFCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,26 @@ private static void fix(File input, File output) throws IOException {
}
}

public static IScaler buildScaler(PDPage page) {
public static boolean shouldScale(PDPage page) {
PDRectangle mediaBox = page.getMediaBox();
boolean isPortrait = mediaBox.getWidth() < mediaBox.getHeight();
boolean shouldScale = false;

if (isPortrait) {
shouldScale = mediaBox.getWidth() > MAX_WIDTH || mediaBox.getHeight() > MAX_HEIGHT;
return mediaBox.getWidth() > MAX_WIDTH || mediaBox.getHeight() > MAX_HEIGHT;
} else {
shouldScale = mediaBox.getWidth() > MAX_HEIGHT || mediaBox.getHeight() > MAX_WIDTH;
return mediaBox.getWidth() > MAX_HEIGHT || mediaBox.getHeight() > MAX_WIDTH;
}
}

public static IScaler buildScaler(PDPage page) {
boolean shouldScale = shouldScale(page);
if (!shouldScale) {
return new NoopScaler();
}

PDRectangle mediaBox = page.getMediaBox();
boolean isPortrait = mediaBox.getWidth() < mediaBox.getHeight();

// Calculate scale factors. Depending on the orientation this requires division of height or width.
float fWidth = 1;
float fHeight = 1;
Expand Down
18 changes: 5 additions & 13 deletions src/test/java/one/squeeze/pdftools/cli/cmds/FixPDFCommandTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package one.squeeze.pdftools.cli.cmds;

import one.squeeze.pdftools.DIN;
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

class FixPDFCommandTest {

Expand All @@ -24,21 +22,17 @@ class FixPDFCommandTest {
@Test
void testBuildScaler_NoopOnA4Portrait() {
PDPage page = new PDPage();
PDRectangle box = new PDRectangle();
page.setMediaBox(DIN.A4);

IScaler scaler = FixPDFCommand.buildScaler(page);
assertTrue(scaler instanceof NoopScaler);
assertFalse(FixPDFCommand.shouldScale(page));
}

@Test
void testBuildScaler_NoopOnA4Landscape() {
PDPage page = new PDPage();
PDRectangle box = new PDRectangle();
page.setMediaBox(DIN.A4_Landscape);

IScaler scaler = FixPDFCommand.buildScaler(page);
assertTrue(scaler instanceof NoopScaler);
assertFalse(FixPDFCommand.shouldScale(page));
}

@Test
Expand All @@ -49,8 +43,7 @@ void testBuildScaler_NoopOnSmallerThanA4Portrait() {
box.setUpperRightY(DIN.A4.getHeight() - 1);
page.setMediaBox(box);

IScaler scaler = FixPDFCommand.buildScaler(page);
assertTrue(scaler instanceof NoopScaler);
assertFalse(FixPDFCommand.shouldScale(page));
}

@Test
Expand All @@ -61,8 +54,7 @@ void testBuildScaler_NoopOnSmallerThanA4Landscape() {
box.setUpperRightY(DIN.A4_Landscape.getHeight() - 1);
page.setMediaBox(box);

IScaler scaler = FixPDFCommand.buildScaler(page);
assertTrue(scaler instanceof NoopScaler);
assertFalse(FixPDFCommand.shouldScale(page));
}

@Test
Expand Down

0 comments on commit 7ff590e

Please sign in to comment.