RED-8212: fix specific case

*refactor slightly
This commit is contained in:
Kilian Schuettler 2024-01-23 12:34:41 +01:00
parent 2a9583318b
commit 323c5a47b5

View File

@ -50,11 +50,11 @@ public class InvisibleElementRemovalService {
* -Elements covered by widely stroked path * -Elements covered by widely stroked path
* -Any Text set to clipping with its many interactions with other elements * -Any Text set to clipping with its many interactions with other elements
* *
* @param pdfFile The PDF file to process * @param pdfFile The PDF file to process
* @param removePaths If this flag is set, invisible path elements will be removed * @param removePaths If this flag is set, invisible path elements will be removed
* @param delta If this flag is set only the removed Elements will be written to the output file. * @param delta If this flag is set only the removed Elements will be written to the output file.
* The Elements are red if they are removed by clipping path, blue for transparency, and a green bounding box for overlap. * The Elements are red if they are removed by clipping path, blue for transparency, and a green bounding box for overlap.
* @param out OutputStream to write the resulting file to * @param out OutputStream to write the resulting file to
**/ **/
@SneakyThrows @SneakyThrows
public void removeInvisibleElements(InputStream pdfFile, OutputStream out, boolean delta, boolean removePaths) { public void removeInvisibleElements(InputStream pdfFile, OutputStream out, boolean delta, boolean removePaths) {
@ -85,6 +85,7 @@ public class InvisibleElementRemovalService {
} }
/** /**
* This method is similar to {@link #removeInvisibleElements(InputStream, OutputStream, boolean, boolean)}, just with a PDFDoc. * This method is similar to {@link #removeInvisibleElements(InputStream, OutputStream, boolean, boolean)}, just with a PDFDoc.
*/ */
@ -94,6 +95,7 @@ public class InvisibleElementRemovalService {
execute(pdfDoc, delta, removePaths); execute(pdfDoc, delta, removePaths);
} }
/** /**
* This method is equal to {@link #removeInvisibleElements(PDFDoc, boolean, boolean)}, with removePaths == true. * This method is equal to {@link #removeInvisibleElements(PDFDoc, boolean, boolean)}, with removePaths == true.
*/ */
@ -306,18 +308,12 @@ public class InvisibleElementRemovalService {
if (inClippingPath) { if (inClippingPath) {
if (isFilledAndNonTransparent(pathElement)) { if (isFilledAndNonTransparent(pathElement)) {
List<ElementFeatures> currentOverlappedElements = context.visibleElements() calculateOverlapsForLinePath(context, linePath);
.stream()
.filter(features -> almostContains(linePath, features.getBoundingBox()))
.toList();
context.overlappedElements().addAll(currentOverlappedElements);
context.visibleElements().removeAll(currentOverlappedElements);
} }
context.visibleElements().add(ElementFeatureFactory.extractFeatures(pathElement)); context.visibleElements().add(ElementFeatureFactory.extractFeatures(pathElement));
if (!context.delta()) { }
writer.writeElement(pathElement);
} if (!context.delta() && (inClippingPath || !context.removePaths())) {
} else if (!context.delta() && !context.removePaths()) {
writer.writeElement(pathElement); writer.writeElement(pathElement);
} }
@ -332,6 +328,17 @@ public class InvisibleElementRemovalService {
} }
private void calculateOverlapsForLinePath(InvisibleElementRemovalContext context, GeneralPath linePath) {
List<ElementFeatures> currentOverlappedElements = context.visibleElements()
.stream()
.filter(features -> almostContains(linePath, features.getBoundingBox()))
.toList();
context.overlappedElements().addAll(currentOverlappedElements);
context.visibleElements().removeAll(currentOverlappedElements);
}
private void removeOverlappedElements(Page page, ElementWriter writer, InvisibleElementRemovalContext context) throws PDFNetException { private void removeOverlappedElements(Page page, ElementWriter writer, InvisibleElementRemovalContext context) throws PDFNetException {
context.reader().begin(page); context.reader().begin(page);