1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-11-11 02:10:11 +01:00

refactor data clumps

This commit is contained in:
tschoemaker 2024-06-06 23:50:37 +02:00
parent 7b08d98232
commit 82331a030d
2 changed files with 67 additions and 23 deletions

View File

@ -0,0 +1,46 @@
package stirling.software.SPDF.controller.api;
import java.io.File;
import java.util.List;
public class OverlayParameters {
private final int basePageCount;
private final File[] overlayFiles;
private final String mode;
private final int[] counts;
private final List<File> tempFiles;
public OverlayParameters(
int basePageCount,
File[] overlayFiles,
String mode,
int[] counts,
List<File> tempFiles) {
this.basePageCount = basePageCount;
this.overlayFiles = overlayFiles;
this.mode = mode;
this.counts = counts;
this.tempFiles = tempFiles;
}
public int getBasePageCount() {
return basePageCount;
}
public File[] getOverlayFiles() {
return overlayFiles;
}
public String getMode() {
return mode;
}
public int[] getCounts() {
return counts;
}
public List<File> getTempFiles() {
return tempFiles;
}
}

View File

@ -58,13 +58,9 @@ public class PdfOverlayController {
try (PDDocument basePdf = Loader.loadPDF(baseFile.getBytes()); try (PDDocument basePdf = Loader.loadPDF(baseFile.getBytes());
Overlay overlay = new Overlay()) { Overlay overlay = new Overlay()) {
Map<Integer, String> overlayGuide = OverlayParameters overlayParameters =
prepareOverlayGuide( new OverlayParameters(overlayPos, overlayPdfFiles, mode, counts, tempFiles);
basePdf.getNumberOfPages(), Map<Integer, String> overlayGuide = prepareOverlayGuide(overlayParameters);
overlayPdfFiles,
mode,
counts,
tempFiles);
overlay.setInputPDF(basePdf); overlay.setInputPDF(basePdf);
if (overlayPos == 0) { if (overlayPos == 0) {
@ -98,19 +94,17 @@ public class PdfOverlayController {
} }
} }
private Map<Integer, String> prepareOverlayGuide( private Map<Integer, String> prepareOverlayGuide(OverlayParameters params) throws IOException {
int basePageCount, File[] overlayFiles, String mode, int[] counts, List<File> tempFiles)
throws IOException {
Map<Integer, String> overlayGuide = new HashMap<>(); Map<Integer, String> overlayGuide = new HashMap<>();
switch (mode) { switch (params.getMode()) {
case "SequentialOverlay": case "SequentialOverlay":
sequentialOverlay(overlayGuide, overlayFiles, basePageCount, tempFiles); sequentialOverlay(overlayGuide, params);
break; break;
case "InterleavedOverlay": case "InterleavedOverlay":
interleavedOverlay(overlayGuide, overlayFiles, basePageCount); interleavedOverlay(overlayGuide, params);
break; break;
case "FixedRepeatOverlay": case "FixedRepeatOverlay":
fixedRepeatOverlay(overlayGuide, overlayFiles, counts, basePageCount); fixedRepeatOverlay(overlayGuide, params);
break; break;
default: default:
throw new IllegalArgumentException("Invalid overlay mode"); throw new IllegalArgumentException("Invalid overlay mode");
@ -118,14 +112,13 @@ public class PdfOverlayController {
return overlayGuide; return overlayGuide;
} }
private void sequentialOverlay( private void sequentialOverlay(Map<Integer, String> overlayGuide, OverlayParameters params)
Map<Integer, String> overlayGuide,
File[] overlayFiles,
int basePageCount,
List<File> tempFiles)
throws IOException { throws IOException {
int overlayFileIndex = 0; int overlayFileIndex = 0;
int pageCountInCurrentOverlay = 0; int pageCountInCurrentOverlay = 0;
File[] overlayFiles = params.getOverlayFiles();
List<File> tempFiles = params.getTempFiles();
int basePageCount = params.getBasePageCount();
for (int basePageIndex = 1; basePageIndex <= basePageCount; basePageIndex++) { for (int basePageIndex = 1; basePageIndex <= basePageCount; basePageIndex++) {
if (pageCountInCurrentOverlay == 0 if (pageCountInCurrentOverlay == 0
@ -156,9 +149,11 @@ public class PdfOverlayController {
} }
} }
private void interleavedOverlay( private void interleavedOverlay(Map<Integer, String> overlayGuide, OverlayParameters params)
Map<Integer, String> overlayGuide, File[] overlayFiles, int basePageCount)
throws IOException { throws IOException {
File[] overlayFiles = params.getOverlayFiles();
int basePageCount = params.getBasePageCount();
for (int basePageIndex = 1; basePageIndex <= basePageCount; basePageIndex++) { for (int basePageIndex = 1; basePageIndex <= basePageCount; basePageIndex++) {
File overlayFile = overlayFiles[(basePageIndex - 1) % overlayFiles.length]; File overlayFile = overlayFiles[(basePageIndex - 1) % overlayFiles.length];
@ -172,9 +167,12 @@ public class PdfOverlayController {
} }
} }
private void fixedRepeatOverlay( private void fixedRepeatOverlay(Map<Integer, String> overlayGuide, OverlayParameters params)
Map<Integer, String> overlayGuide, File[] overlayFiles, int[] counts, int basePageCount)
throws IOException { throws IOException {
File[] overlayFiles = params.getOverlayFiles();
int[] counts = params.getCounts();
int basePageCount = params.getBasePageCount();
if (overlayFiles.length != counts.length) { if (overlayFiles.length != counts.length) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Counts array length must match the number of overlay files"); "Counts array length must match the number of overlay files");