Ste*_*eve 12 php fonts zend-framework zend-pdf
有没有人有一个简单的方法来计算一页文本将以特定字体和大小消耗多少点?(容易=最小的代码行+计算上便宜).Zend_Pdf似乎没有这样做的函数,除了对getGlyphForCharacter(),getUnitsPerEm()和getWidthsForGlyph()的每个字符进行一些非常昂贵的调用.
我正在生成一个多页PDF,每页都有几个表,需要在列中包装文本.它已经花了几秒钟来创建它,我不希望它花费太多时间,或者我不得不开始搞乱后台任务或进度条等.
我想出的唯一解决方案是预先计算每个字体使用的每个字符的宽度(以磅为单位),然后在每个字符串上添加这些字符.仍然相当昂贵.
我错过了什么吗?或者你有什么更简单的?
谢谢!
Dav*_*unt 29
有一种方法可以精确计算宽度,而不是使用Gorilla3D的最坏情况算法.
请尝试http://devzone.zend.com/article/2525-Zend_Pdf-tutorial#comments-2535上的此代码
我在我的应用程序中使用它来计算右对齐文本的偏移量并且它有效
/**
* Returns the total width in points of the string using the specified font and
* size.
*
* This is not the most efficient way to perform this calculation. I'm
* concentrating optimization efforts on the upcoming layout manager class.
* Similar calculations exist inside the layout manager class, but widths are
* generally calculated only after determining line fragments.
*
* @link http://devzone.zend.com/article/2525-Zend_Pdf-tutorial#comments-2535
* @param string $string
* @param Zend_Pdf_Resource_Font $font
* @param float $fontSize Font size in points
* @return float
*/
function widthForStringUsingFontSize($string, $font, $fontSize)
{
$drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $string);
$characters = array();
for ($i = 0; $i < strlen($drawingString); $i++) {
$characters[] = (ord($drawingString[$i++]) << 8 ) | ord($drawingString[$i]);
}
$glyphs = $font->glyphNumbersForCharacters($characters);
$widths = $font->widthsForGlyphs($glyphs);
$stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
return $stringWidth;
}
Run Code Online (Sandbox Code Playgroud)
关于性能,我没有在脚本中强烈使用它,但我可以想象它很慢.如果可能的话,我建议将PDF写入磁盘,因此重复视图非常快,并尽可能缓存/硬编码数据.