Get the font height of a character in PDFBox

拟墨画扇 提交于 2019-12-19 16:58:04

问题


There is a method in PDFBox's font class, PDFont, named getFontHeight which sounds simple enough. However I don't quite understand the documentation and what the parameters stand for.

getFontHeight This will get the font width for a character.

Parameters:

  • c - The character code to get the width for.
  • offset - The offset into the array. length
  • The length of the data.

Returns: The width is in 1000 unit of text space, ie 333 or 777

Is this method the right one to use to get the height of a character in PDFBox and if so how? Is it some kind of relationship between font height and font size I can use instead?


回答1:


I believe the answer marked right requires some additional clarification. There are no "error" per font for getHeight() and hence I believe it is not a good practice manually guessing the coefficient for each new font. Guess it could be nice for your purposes simply use CapHeight instead of Height.

float height = ( font.getFontDescriptor().getCapHeight()) / 1000 * fontSize;

That will return the value similar to what you are trying to get by correcting the Height with 0.865 for Helvetica. But it will be universal for any font.

PDFBox docs do not explain too much what is it. But you can look at the image in the wikipedia Cap_height article to understand better how it is working and choose the parameter fit to your particular task.

https://en.wikipedia.org/wiki/Cap_height




回答2:


EDIT: Cap height was what I was looking for. See the accepted answer.

After digging through the source of PDFBox I found that this should do the trick of calculating the font height.

int fontSize = 14;
PDFont font = PDType1Font.HELVETICA;
font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize

The method isn't perfect though. If you draw a rectangle with the height 200 and a Y with the font size 200 you get the font height 231.2 calculated with the above method even though it actually is printed smaller then the rectangle.

Every font has a different error but with helvetica it is close to 13.5 precent too much independently of font size. Therefore, to get the right font height for helvetica this works...

font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize * 0.865



回答3:


Maybe use this?

http://pdfbox.apache.org/apidocs/org/apache/pdfbox/util/TextPosition.html

Seems like a wrap-around util for text. I haven't looked in the source if it accounts for font error though.




回答4:


this is a working method for splitting the text and finding the height

public float heightForWidth(float width) throws IOException {
    float height = 0;

    String[] split = getTxt().split("(?<=\\W)");
    int[] possibleWrapPoints = new int[split.length];
    possibleWrapPoints[0] = split[0].length();
    for (int i = 1; i < split.length; i++) {
        possibleWrapPoints[i] = possibleWrapPoints[i - 1] + split[i].length();
    }

    float leading = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
    int start = 0;
    int end = 0;
    for (int i : possibleWrapPoints) {
        float w = font.getStringWidth(getTxt().substring(start, i)) / 1000 * fontSize;
        if (start < end && w > width) {
            height += leading;
            start = end;
        }
        end = i;
    }

    height += leading;
    return height + 3;
}



回答5:


For imported True Type Fonts the total height of the font is

(org.apache.pdfbox.pdmodel.font.PDFont.getFontDescriptor().getDescent() + org.apache.pdfbox.pdmodel.font.PDFont.getFontDescriptor().getAscent() + org.apache.pdfbox.pdmodel.font.PDFont.getFontDescriptor().getLeading()) * point size * org.apache.pdfbox.pdmodel.font.PDFont.getFontMatrix().getValue(0, 0)

You will find that font.getFontDescriptor().getFontBoundingBox().getHeight() is 20% larger than the above value as it includes a 20% leading on the above value, but if you take the top value and remove 20%, the font will be right next too each other



来源:https://stackoverflow.com/questions/17171815/get-the-font-height-of-a-character-in-pdfbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!