书写时计算多行文本的高度

发布于 2025-01-12 12:34:10 字数 861 浏览 3 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-01-19 12:34:10

计算段落高度的算法如下:

float firstLineCapHeight = (firstLineFont.getFontDescriptor().getCapHeight() / 1000f) * firstLineFontSize;

// this value is negative
float lastLineDescent = (lastLineFont.getFontDescriptor().getDescent() / 1000f) * lastLineFontSize;

float height = firstLineCapHeight + leadingOfAllLinesExceptFirst - lastLineDescent;

如果第一行或最后一行有多种字体或字体大小,则必须找到最大的一个。

根据字母表,您还可以添加第一行上升,但在我的情况下不需要。

The algorithm for calculating the height of a paragraph is the following:

float firstLineCapHeight = (firstLineFont.getFontDescriptor().getCapHeight() / 1000f) * firstLineFontSize;

// this value is negative
float lastLineDescent = (lastLineFont.getFontDescriptor().getDescent() / 1000f) * lastLineFontSize;

float height = firstLineCapHeight + leadingOfAllLinesExceptFirst - lastLineDescent;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文