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