ralfstuckert/pdfbox-layout

TextFlow.getHeight not including descenders

Opened this issue · 1 comments

I am trying to get the height of a box from the top of the top line of text to the bottom of the text including descenders.

The following code snippet draws text with a box around it, but the problem is that the descenders are outside the box. There is also a little space above the first line of text as well, but that isn't as big of a problem.

Is there any way to account for the descenders height on the bottom line?

TextFlow text=new TextFlow();
StyledText t=new StyledText("Now is the time for all good men to come to the aid of their country."
                            + " The quick brown fox jumps over the lazy dog. Lorem ipsum dolor sit amet,"
                            + "consetetur sadipscing elitr",s,font,Color.red);
text.add(t);
text.setMaxWidth(w);
text.setApplyLineSpacingToFirstLine(false);
text.drawText(contents,new Position(x,y),Alignment.Left,null);
Shape shape2 = new Rect();
shape2.draw(doc, contents, new Position(x, y), w,
                        text.getHeight(), Color.magenta, new Stroke(1), null);

I've come up with a workaround that has worked in a few limited tests. If anybody with more knowledge sees issues or a better method please let me know.

Basically I'm calculating space for the descenders like this where s is the font size:
float descent=(text.getLineSpacing()-1)*s;

So the snippet above would become:

TextFlow text=new TextFlow();
StyledText t=new StyledText("Now is the time for all good men to come to the aid of their country."
                            + " The quick brown fox jumps over the lazy dog. Lorem ipsum dolor sit amet,"
                            + "consetetur sadipscing elitr",s,font,Color.red);
text.add(t);
text.setMaxWidth(w);
text.setApplyLineSpacingToFirstLine(false);
text.drawText(contents,new Position(x,y),Alignment.Left,null);
float descent=(text.getLineSpacing()-1)*s;

Shape shape2 = new Rect();
shape2.draw(doc, contents, new Position(x, y), w,
                        text.getHeight()+descent, Color.magenta, new Stroke(1), null);