开发者

PDF calculate Glyph sizes

I (think) have every values for Text-Rendering in a PDF.

* Position (Text Matrix)
* FontDescriptor with Widths Array
* FontBBox
* StemV/StemH
* FontName
* Descent
* Ascent
* CapHeight
* XHeight
* It开发者_JAVA百科alicAngle

My problem is I don't know what to do with these values. I went through the PDF Spec 1.7 a couple of times and cannot find a formular to calculate the real pixel sizes of every glyph in PDF. Can you give me a hint?

Thank you.


What are you trying to do? Rendering PDF is a lot of work and you also need to factor in leading, Text raise, kerning, CTM and several other factors.


Position:     (optional, you can avoid it)
Text Matrix:  (optional, you can avoid it) 
Widths Array: (use empty array [], PDF can read it directly from CFF (FontFile3 stream))
FontBBox:     font file->'CFF ' table->Top DICT INDEX->DICT-> 4 operands for 'FontBBox'     operator
StemV:        (optional, you can avoid it)
StemH:        (optional, you can avoid it)
FontName:     font file->'name' table->records
      or: font file->'CFF ' table->Top DICT INDEX->string by index 0 for 'fonts names' operator

Descent:      font file->'hhea' table->'Descender' parameter
Ascent:       font file->'hhea' table->'Ascender' parameter 
CapHeight:    font file->'OS/2' table->'sCapHeight' parameter
XHeight:      font file->'OS/2' table->'sxHeight' parameter
ItalicAngle:  font file->'OS/2' table->'sxHeight' parameter

Actually, you can calculate Widths array. For each glyph:

Decoding array(PDF) -> Glyph name (PDF) -> Glyph index (CFF table of font file) -> table 'hmtx' -> Glyph 'hMetrics'[Glyph index] = array ('advanceWidth', 'leftSideBearing')   

I spent a WEEK, to understand it...


If you want just highlight a text, it's not necessary calculate the text. You can add as much as you want content objects to the page (rectangle, image, line, semi-transparent stuff) and re-calculate the PDF structure. It is really simple. Ask your mouse about the selection coordinates))


These values are designed to properly typeset type, not draw glyphs, so you can't get the exact pixel size of each glyph from these attributes. The only way to get the exact pixel dimensions of a glyph is to draw the glyph into an image and analyze that image.

The FontBBox (font bounding box) is the smallest box that will hold each glyph. The Widths Array holds information on how far apart each character should be drawn, not the actual glyph image size. Some fonts will draw some glyphs outsize that width.

When you highlight text in a typical text editor, the highlight will be the full height of the font, and the width of each individual character. This highlight is made by getting the FontBBox height, and each character's width from the Widths Array, and transforming those values to match the current font's attributes (size, etc.). This information is sufficient to make your app draw type like typical applications.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜