Skip to content

PDFBOX-5284: Refactor Refactor PDFTabulaTextStripper to improve test … #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 40 additions & 57 deletions pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;


/**
* Test suite for PDFTextStripper.
Expand Down Expand Up @@ -108,6 +112,40 @@
class TestTextStripper
{

PDFTextStripper mockPDFTextStripper1() throws IOException {
PDFTextStripper mockInstance = spy(PDFTextStripper.class);
doAnswer((stubInvo) -> {
PDFont font = stubInvo.getArgument(0);
BoundingBox bbox = font.getBoundingBox();
if (bbox.getLowerLeftY() < Short.MIN_VALUE) {
bbox.setLowerLeftY(-(bbox.getLowerLeftY() + 65536));
}
float glyphHeight = bbox.getHeight() / 2;
PDFontDescriptor fontDescriptor = font.getFontDescriptor();
if (fontDescriptor != null) {
float capHeight = fontDescriptor.getCapHeight();
if (Float.compare(capHeight, 0) != 0
&& (capHeight < glyphHeight || Float.compare(glyphHeight, 0) == 0)) {
glyphHeight = capHeight;
}
float ascent = fontDescriptor.getAscent();
float descent = fontDescriptor.getDescent();
if (ascent > 0 && descent < 0
&& ((ascent - descent) / 2 < glyphHeight || Float.compare(glyphHeight, 0) == 0)) {
glyphHeight = (ascent - descent) / 2;
}
}
float height;
if (font instanceof PDType3Font) {
height = font.getFontMatrix().transformPoint(0, glyphHeight).y;
} else {
height = glyphHeight / 1000;
}
return height;
}).when(mockInstance).computeFontHeight(any(PDFont.class));
return mockInstance;
}

/**
* Logger instance.
*/
Expand Down Expand Up @@ -597,7 +635,8 @@ void testTabula() throws IOException
File expectedOutFile = new File("src/test/resources/input","eu-001.pdf-tabula.txt");
File diffFile = new File("target/test-output","eu-001.pdf-tabula-diff.txt");
PDDocument tabulaDocument = Loader.loadPDF(pdfFile);
PDFTextStripper tabulaStripper = new PDFTabulaTextStripper();
// Construct mock object
PDFTextStripper tabulaStripper = mockPDFTextStripper1();

try (OutputStream os = new FileOutputStream(outFile))
{
Expand All @@ -616,60 +655,4 @@ void testTabula() throws IOException
assertFalse(bFail);
}

private class PDFTabulaTextStripper extends PDFTextStripper
{
PDFTabulaTextStripper() throws IOException
{
// empty
}

@Override
protected float computeFontHeight(PDFont font) throws IOException
{
BoundingBox bbox = font.getBoundingBox();
if (bbox.getLowerLeftY() < Short.MIN_VALUE)
{
// PDFBOX-2158 and PDFBOX-3130
// files by Salmat eSolutions / ClibPDF Library
bbox.setLowerLeftY(-(bbox.getLowerLeftY() + 65536));
}
// 1/2 the bbox is used as the height todo: why?
float glyphHeight = bbox.getHeight() / 2;

// sometimes the bbox has very high values, but CapHeight is OK
PDFontDescriptor fontDescriptor = font.getFontDescriptor();
if (fontDescriptor != null)
{
float capHeight = fontDescriptor.getCapHeight();
if (Float.compare(capHeight, 0) != 0
&& (capHeight < glyphHeight || Float.compare(glyphHeight, 0) == 0))
{
glyphHeight = capHeight;
}
// PDFBOX-3464, PDFBOX-448:
// sometimes even CapHeight has very high value, but Ascent and Descent are ok
float ascent = fontDescriptor.getAscent();
float descent = fontDescriptor.getDescent();
if (ascent > 0 && descent < 0
&& ((ascent - descent) / 2 < glyphHeight || Float.compare(glyphHeight, 0) == 0))
{
glyphHeight = (ascent - descent) / 2;
}
}

// transformPoint from glyph space -> text space
float height;
if (font instanceof PDType3Font)
{
height = font.getFontMatrix().transformPoint(0, glyphHeight).y;
}
else
{
height = glyphHeight / 1000;
}

return height;
}
}

}