mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-08 17:51:20 +02:00
refactor: clean up code formatting and improve readability in RedactionService
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
parent
338b77de99
commit
f00952b856
@ -42,6 +42,7 @@ import org.apache.pdfbox.pdmodel.PDResources;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
import org.apache.pdfbox.pdmodel.common.PDStream;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType0Font;
|
||||
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
|
||||
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
||||
import org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern;
|
||||
@ -2672,16 +2673,14 @@ public class RedactionService {
|
||||
merger.setDestinationFileName(finalOutputFile.toString());
|
||||
for (int pageNum = 0; pageNum < pageCount; pageNum++) {
|
||||
BufferedImage image = pdfRenderer.renderImageWithDPI(pageNum, 600);
|
||||
File imagePath =
|
||||
new File(tempImagesDir, "page_" + pageNum + ".png");
|
||||
File imagePath = new File(tempImagesDir, "page_" + pageNum + ".png");
|
||||
ImageIO.write(image, "png", imagePath);
|
||||
List<String> command =
|
||||
new ArrayList<>(
|
||||
Arrays.asList(
|
||||
"tesseract",
|
||||
imagePath.toString(),
|
||||
new File(tempOutputDir, "page_" + pageNum)
|
||||
.toString(),
|
||||
new File(tempOutputDir, "page_" + pageNum).toString(),
|
||||
"-l",
|
||||
buildLanguageOption(request),
|
||||
"--dpi",
|
||||
@ -2696,8 +2695,7 @@ public class RedactionService {
|
||||
throw new IOException(
|
||||
"Tesseract restoration failed with return code: " + result.getRc());
|
||||
}
|
||||
java.io.File pageOutputPath =
|
||||
new java.io.File(tempOutputDir, "page_" + pageNum + ".pdf");
|
||||
File pageOutputPath = new File(tempOutputDir, "page_" + pageNum + ".pdf");
|
||||
merger.addSource(pageOutputPath);
|
||||
}
|
||||
merger.mergeDocuments(null);
|
||||
@ -2736,13 +2734,8 @@ public class RedactionService {
|
||||
pageText.append(pdfText.getText()).append(" ");
|
||||
}
|
||||
}
|
||||
|
||||
String extractedText = pageText.toString().trim();
|
||||
for (String word : targetWords) {
|
||||
if (extractedText.toLowerCase().contains(word.toLowerCase())) {}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.debug("Failed to get direct text from page", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2776,8 +2769,6 @@ public class RedactionService {
|
||||
}
|
||||
|
||||
String completeText = allText.toString().trim();
|
||||
if (!completeText.isEmpty() && hasProblematicChars) {}
|
||||
|
||||
}
|
||||
|
||||
List<MatchRange> matches;
|
||||
@ -2939,8 +2930,7 @@ public class RedactionService {
|
||||
NORMALIZE_WHITESPACE
|
||||
}
|
||||
|
||||
public interface SemanticScrubber {
|
||||
}
|
||||
public interface SemanticScrubber {}
|
||||
|
||||
private static class GlyphCoverageProbe {
|
||||
private final PDFont font;
|
||||
@ -2956,7 +2946,7 @@ public class RedactionService {
|
||||
if (font == null) return coverage;
|
||||
|
||||
try {
|
||||
if (font instanceof org.apache.pdfbox.pdmodel.font.PDType0Font) {
|
||||
if (font instanceof PDType0Font) {
|
||||
for (int cid = 0; cid < 65536; cid++) {
|
||||
try {
|
||||
String unicode = font.toUnicode(cid);
|
||||
@ -2964,12 +2954,12 @@ public class RedactionService {
|
||||
coverage.add(cid);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
log.debug("Failed to get unicode for cid {}", cid, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
log.debug("Failed to get glyph coverage for font {}", font, e);
|
||||
}
|
||||
return coverage;
|
||||
}
|
||||
@ -2995,17 +2985,17 @@ public class RedactionService {
|
||||
String charStr = new String(Character.toChars(codePoint));
|
||||
return font.getStringWidth(charStr) / FONT_SCALE_FACTOR * fontSize;
|
||||
} catch (Exception e) {
|
||||
|
||||
log.debug("Failed to get width for codepoint {}", codePoint, e);
|
||||
}
|
||||
}
|
||||
return switch (strategy) {
|
||||
case EMBED_WIDTH -> getEmbeddedProgramWidth(codePoint, fontSize);
|
||||
case EMBED_WIDTH -> getEmbeddedProgramWidth(fontSize);
|
||||
case AVERAGE_WIDTH -> getAverageFontWidth(fontSize);
|
||||
case LEGACY_SUM -> getLegacySumFallback(codePoint, fontSize);
|
||||
};
|
||||
}
|
||||
|
||||
private float getEmbeddedProgramWidth(int codePoint, float fontSize) {
|
||||
private float getEmbeddedProgramWidth(float fontSize) {
|
||||
try {
|
||||
if (font.getFontDescriptor() != null) {
|
||||
float avgWidth = font.getFontDescriptor().getAverageWidth();
|
||||
@ -3033,7 +3023,7 @@ public class RedactionService {
|
||||
validChars++;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
log.debug("Failed to get width for char {}", ch, e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3091,7 +3081,7 @@ public class RedactionService {
|
||||
}
|
||||
}
|
||||
|
||||
private void scrubStructureElement(COSDictionary element, Set<ScrubOption> options) {
|
||||
private static void scrubStructureElement(COSDictionary element, Set<ScrubOption> options) {
|
||||
if (element == null) return;
|
||||
|
||||
if (options.contains(ScrubOption.REMOVE_ACTUALTEXT)) {
|
||||
@ -3120,12 +3110,11 @@ public class RedactionService {
|
||||
}
|
||||
}
|
||||
|
||||
private void normalizeWhitespaceInElement(COSDictionary element) {
|
||||
private static void normalizeWhitespaceInElement(COSDictionary element) {
|
||||
for (COSName key : List.of(COSName.ACTUAL_TEXT, COSName.ALT, COSName.TU)) {
|
||||
COSBase value = element.getDictionaryObject(key);
|
||||
if (value instanceof COSString cosString) {
|
||||
String text = cosString.getString();
|
||||
if (text != null) {
|
||||
String normalized = text.replaceAll("\\s+", " ").trim();
|
||||
if (normalized.length() > 256) {
|
||||
normalized = normalized.substring(0, 256);
|
||||
@ -3134,7 +3123,6 @@ public class RedactionService {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void scrubAnnotations(PDDocument document, Set<ScrubOption> options) {
|
||||
try {
|
||||
@ -3157,7 +3145,7 @@ public class RedactionService {
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
log.debug("Failed to scrub annotations", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user