书写时计算多行文本的高度
使用 PDPageContentStream 我正在编写这样的文本:
stream.beginText();
stream.newLineAtOffset(400, 400);
stream.setFont(myFont, 10);
stream.setLeading(11.5f);
stream.showText("First line.");
stream.newLine();
stream.showText("Second line.");
stream.setFont(myFontBold, 10);
stream.setLeading(15f);
stream.newLine();
stream.showText("Final line. (bold)");
stream.endText();
所以它就像
- 第 1 行:前导 11.5,myFont,大小 10
- 第 2 行:前导 11.5,myFont,大小 10
- 第 3 行:前导 15,myFontBold,大小 10
我将这些行放在列表。但是当我尝试计算总高度以便在它们周围画一个框时,我无法得到正确的结果。
我尝试总结所有错误的线索。我尝试将所有行距和字体边界框高度相加,但它有点太高了。我也尝试过字符上限高度,但它是错误的。
我为每行做了边界框之类的事情
total += (font.getBoundingBox().getHeight() / 1000.0f) * fontSize;
total += leading;
。
Using PDPageContentStream I am writing a text like this:
stream.beginText();
stream.newLineAtOffset(400, 400);
stream.setFont(myFont, 10);
stream.setLeading(11.5f);
stream.showText("First line.");
stream.newLine();
stream.showText("Second line.");
stream.setFont(myFontBold, 10);
stream.setLeading(15f);
stream.newLine();
stream.showText("Final line. (bold)");
stream.endText();
So it is like
- line 1: leading 11.5, myFont, size 10
- line 2: leading 11.5, myFont, size 10
- line 3: leading 15, myFontBold, size 10
I have these lines in a list. But when I try to calculate the total height so I can draw a box around them, I can't get it to be correct.
I've tried summing all the leadings which is wrong. I have tried summing all the leadings and then the font bounding box height, but it is a little too high. I have also tried character cap height but it is wrong.
I did the bounding box thing like
total += (font.getBoundingBox().getHeight() / 1000.0f) * fontSize;
total += leading;
for each line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
计算段落高度的算法如下:
如果第一行或最后一行有多种字体或字体大小,则必须找到最大的一个。
根据字母表,您还可以添加第一行上升,但在我的情况下不需要。
The algorithm for calculating the height of a paragraph is the following:
If the first or last line has multiple fonts or font sizes you will have to find the largest one.
Depending on the alphabet you can also add the first line ascent but that is not needed in my case.