RED-5381: Fixed calculation of bodyTextFrame

This commit is contained in:
deiflaender 2022-10-17 16:56:00 +02:00
parent c0e354d2d4
commit cccba91209
5 changed files with 100 additions and 72 deletions

View File

@ -20,15 +20,29 @@ public class Rectangle {
@JsonIgnore @JsonIgnore
@JsonAttribute(ignore = true) @JsonAttribute(ignore = true)
public float getX1(int dir, float pageWidth, float pageHeight) { public float getX1(int dir, float pageWidth, float pageHeight) {
if (dir == 90) { if (dir == 90) {
return topLeft.getX(); return topLeft.getY();
} }
else if (dir == 180) { else if (dir == 180) {
return pageWidth - topLeft.getX() + width; return topLeft.getX() + width;
} }
else if (dir == 270) { else if (dir == 270) {
@ -46,11 +60,11 @@ public class Rectangle {
public float getX2(int dir, float pageWidth, float pageHeight) { public float getX2(int dir, float pageWidth, float pageHeight) {
if (dir == 90) { if (dir == 90) {
return topLeft.getX() + width; return topLeft.getY() + height;
} }
else if (dir == 180) { else if (dir == 180) {
return pageWidth - topLeft.getX(); return topLeft.getX();
} }
else if (dir == 270) { else if (dir == 270) {
@ -72,11 +86,11 @@ public class Rectangle {
public float getY1(int dir, float pageWidth, float pageHeight) { public float getY1(int dir, float pageWidth, float pageHeight) {
if (dir == 90 ) { if (dir == 90 ) {
return topLeft.getY(); return topLeft.getX();
} }
else if (dir == 180) { else if (dir == 180) {
return pageHeight - topLeft.getY() + height; return pageHeight - topLeft.getY() - height;
} }
@ -95,7 +109,7 @@ public class Rectangle {
public float getY2(int dir, float pageWidth, float pageHeight) { public float getY2(int dir, float pageWidth, float pageHeight) {
if (dir == 90) { if (dir == 90) {
return topLeft.getY() + height; return topLeft.getX() + width;
} }
else if (dir == 180 ) { else if (dir == 180 ) {

View File

@ -61,7 +61,7 @@ public class BlockificationService {
|| isSplittedByRuling(minX, minY, word.getX1(), word.getY2(), verticalRulingLines, word.getDir().getDegrees(), cropbox.getWidth(), cropbox.getHeight()); || isSplittedByRuling(minX, minY, word.getX1(), word.getY2(), verticalRulingLines, word.getDir().getDegrees(), cropbox.getWidth(), cropbox.getHeight());
boolean splitByDir = prev != null && !prev.getDir().equals(word.getDir()); boolean splitByDir = prev != null && !prev.getDir().equals(word.getDir());
if (prev != null && (lineSeparation || startFromTop || xIsBeforeFirstX || splitByX || splitByDir || startFromTop || splittedByRuling)) { if (prev != null && (lineSeparation || startFromTop || xIsBeforeFirstX || splitByX || splitByDir || splittedByRuling)) {
Orientation prevOrientation = null; Orientation prevOrientation = null;
if (!chunkBlockList1.isEmpty()) { if (!chunkBlockList1.isEmpty()) {
@ -332,6 +332,20 @@ public class BlockificationService {
if (documentFontSizeCounter.getMostPopular() != null) { if (documentFontSizeCounter.getMostPopular() != null) {
if (textBlock.getMostPopularWordFontSize() >= documentFontSizeCounter.getMostPopular()) { if (textBlock.getMostPopularWordFontSize() >= documentFontSizeCounter.getMostPopular()) {
if(page.getRotation() == 90 || page.getRotation() == 270){
if (textBlock.getY1() < minX) {
minX = textBlock.getY1();
}
if (textBlock.getY2() > maxX) {
maxX = textBlock.getY2();
}
if (textBlock.getX1() < minY) {
minY = textBlock.getX1();
}
if (textBlock.getX2() > maxY) {
maxY = textBlock.getX2();
}
} else {
if (textBlock.getX1() < minX) { if (textBlock.getX1() < minX) {
minX = textBlock.getX1(); minX = textBlock.getX1();
} }
@ -344,7 +358,7 @@ public class BlockificationService {
if (textBlock.getY2() > maxY) { if (textBlock.getY2() > maxY) {
maxY = textBlock.getY2(); maxY = textBlock.getY2();
} }
}
} }
} }
} }
@ -358,6 +372,20 @@ public class BlockificationService {
continue; continue;
} }
for (TextBlock textBlock : cell.getTextBlocks()) { for (TextBlock textBlock : cell.getTextBlocks()) {
if(page.getRotation() == 90 || page.getRotation() == 270){
if (textBlock.getY1() < minX) {
minX = textBlock.getMinY();
}
if (textBlock.getY2() > maxX) {
maxX = textBlock.getY2();
}
if (textBlock.getX1() < minY) {
minY = textBlock.getX1();
}
if (textBlock.getX2() > maxY) {
maxY = textBlock.getX2();
}
} else {
if (textBlock.getX1() < minX) { if (textBlock.getX1() < minX) {
minX = textBlock.getX1(); minX = textBlock.getX1();
} }
@ -375,29 +403,15 @@ public class BlockificationService {
} }
} }
} }
}
System.out.println("Page: " + page.getPageNumber() + " Landscape: " + page.isLandscape()+ " MinX: " + minX + " MaxX: " + maxX + " MinY: " +minY + " MaxY: " + maxY);
} }
return new Rectangle(new Point(minX, minY), maxX - minX, maxY - minY, 0); return new Rectangle(new Point(minX, minY), maxX - minX, maxY - minY, 0);
} }
private void sortRotatedSequences(List<TextPositionSequence> sequences) {
List<TextPositionSequence> rotatedWords = new ArrayList<>();
Iterator<TextPositionSequence> itty = sequences.iterator();
while (itty.hasNext()) {
var pos = itty.next();
if (pos.getTextPositions().get(0).getDir() == 270) {
rotatedWords.add(pos);
itty.remove();
}
}
if (!rotatedWords.isEmpty() && !sequences.isEmpty()) {
rotatedWords.sort(Comparator.comparing(TextPositionSequence::getX1));
}
sequences.addAll(rotatedWords);
}
private double round(float value, int decimalPoints) { private double round(float value, int decimalPoints) {

View File

@ -34,7 +34,7 @@ public class ClassificationService {
for (Page page : document.getPages()) { for (Page page : document.getPages()) {
Rectangle btf = page.isLandscape() ? landscapeBodyTextFrame : bodyTextFrame; Rectangle btf = page.isLandscape() ? landscapeBodyTextFrame : bodyTextFrame;
if (!page.isLandscape() && (page.getRotation() == 90 || page.getRotation() == 270)){
var width = btf.getX2(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()) - btf.getX1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()); var width = btf.getX2(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()) - btf.getX1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight());
var height = btf.getY2(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()) - btf.getY1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()); var height = btf.getY2(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()) - btf.getY1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight());
@ -42,11 +42,7 @@ public class ClassificationService {
var y1 = btf.getY1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()); var y1 = btf.getY1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight());
page.setBodyTextFrame(new Rectangle(new Point(x1, y1), width, height,0)); page.setBodyTextFrame(new Rectangle(new Point(x1, y1), width, height,0));
} else if (page.getRotation() == 180) {
page.setBodyTextFrame(new Rectangle(new Point(btf.getTopLeft().getX(), page.getCropBox().getHeight() - btf.getTopLeft().getY() -btf.getHeight()), btf.getWidth(), btf.getHeight(),0));
} else {
page.setBodyTextFrame(btf);
}

View File

@ -79,10 +79,13 @@ public class PdfVisualisationService {
} }
contentStream.setStrokingColor(Color.YELLOW); contentStream.setStrokingColor(Color.YELLOW);
// var widhth = analyzedPage.getBodyTextFrame().getX2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()); // var width = analyzedPage.getBodyTextFrame().getX2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight());
// var height = analyzedPage.getBodyTextFrame().getY2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getY1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()); // var height = analyzedPage.getBodyTextFrame().getY2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getY1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight());
//
// contentStream.addRect(analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()), analyzedPage.getBodyTextFrame().getY1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()), width, height);
contentStream.addRect(analyzedPage.getBodyTextFrame().getTopLeft().getX(), analyzedPage.getBodyTextFrame().getTopLeft().getY(), analyzedPage.getBodyTextFrame().getWidth(), analyzedPage.getBodyTextFrame().getHeight()); contentStream.addRect(analyzedPage.getBodyTextFrame().getTopLeft().getX(), analyzedPage.getBodyTextFrame().getTopLeft().getY(), analyzedPage.getBodyTextFrame().getWidth(), analyzedPage.getBodyTextFrame().getHeight());
contentStream.stroke(); contentStream.stroke();
contentStream.close(); contentStream.close();
@ -97,17 +100,18 @@ public class PdfVisualisationService {
contentStream.addRect(textBlock.getX1(), textBlock.getY1(), textBlock.getX2() - textBlock.getX1(), textBlock.getY2() - textBlock.getY1()); contentStream.addRect(textBlock.getX1(), textBlock.getY1(), textBlock.getX2() - textBlock.getX1(), textBlock.getY2() - textBlock.getY1());
contentStream.stroke(); contentStream.stroke();
// if (textBlock.getClassification() != null) { if (textBlock.getClassification() != null) {
// contentStream.beginText(); contentStream.beginText();
//
// contentStream.setFont(PDType1Font.TIMES_ROMAN, 6f); contentStream.setNonStrokingColor(Color.BLUE);
// contentStream.setFont(PDType1Font.TIMES_ROMAN, 9f);
// contentStream.newLineAtOffset(textBlock.getX1(), textBlock.getY2() + 2);
// contentStream.showText(textBlock.getClassification() + textBlock.getOrientation() + "-->" + textBlock.getSequences().get(0).getDir()); contentStream.newLineAtOffset(textBlock.getX1(), textBlock.getY2() + 2);
// contentStream.showText(textBlock.getClassification() + textBlock.getOrientation() + "-->" + textBlock.getSequences().get(0).getDir());
// contentStream.endText();
// contentStream.endText();
//
// contentStream.setNonStrokingColor(Color.BLUE); // contentStream.setNonStrokingColor(Color.BLUE);
// contentStream.setFont(PDType1Font.TIMES_ROMAN, 2f); // contentStream.setFont(PDType1Font.TIMES_ROMAN, 2f);
// //
@ -131,7 +135,7 @@ public class PdfVisualisationService {
// //
// //
// contentStream.endText(); // contentStream.endText();
// } }
} }

View File

@ -1168,7 +1168,7 @@ public class RedactionIntegrationTest {
System.out.println("classificationTest"); System.out.println("classificationTest");
AnalyzeRequest request = prepareStorage("files/Metolachlor/S-Metolachlor_RAR_01_Volume_1_2018-09-06.pdf"); AnalyzeRequest request = prepareStorage("files/RSS/09 - CGA100251 - Acute Oral Toxicity (Up and Down Procedure) - Rat.pdf");
RedactionRequest redactionRequest = RedactionRequest.builder() RedactionRequest redactionRequest = RedactionRequest.builder()
.dossierId(request.getDossierId()) .dossierId(request.getDossierId())