PDF计算字形大小

时间:2010-10-20 11:03:48

标签: iphone pdf ios quartz-graphics linear-algebra

我(想)在PDF中拥有Text-Rendering的每个值。

* Position (Text Matrix)
* FontDescriptor with Widths Array
* FontBBox
* StemV/StemH
* FontName
* Descent
* Ascent
* CapHeight
* XHeight
* ItalicAngle

我的问题是我不知道如何处理这些值。我经历了几次PDF Spec 1.7,无法找到一个公式来计算PDF中每个字形的实际像素大小。你能给我一个暗示吗?

谢谢。

4 个答案:

答案 0 :(得分:3)

你想做什么?渲染PDF是一项很多工作,你还需要考虑领先,文本提升,字距调整,CTM和其他几个因素。

答案 1 :(得分:2)

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

实际上,您可以计算Widths数组。对于每个字形:

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

我花了一周,了解它...

答案 2 :(得分:1)

如果您只想突出显示文字,则无需计算文字。您可以向页面添加内容对象(矩形,图像,线条,半透明内容),并重新计算PDF结构。这很简单。向你的鼠标询问选择坐标))

答案 3 :(得分:0)

这些值旨在正确排版类型,而不是绘制字形,因此您无法从这些属性中获取每个字形的确切像素大小。获得字形的精确像素尺寸的唯一方法是将字形绘制到图像中并分析该图像。

FontBBox(字体边界框)是保存每个字形的最小框。宽度数组保存有关应绘制每个字符的距离的信息,而不是实际的字形图像大小。某些字体会绘制一些超出该宽度的字形。

在典型文本编辑器中突出显示文本时,突出显示将是字体的完整高度以及每个字符的宽度。通过获取FontBBox高度和Widths数组中每个字符的宽度,并转换这些值以匹配当前字体的属性(大小等)来实现此突出显示。此信息足以使您的应用程序绘制类型与典型应用程序类似。