Zend Framework

find width of text to be drawn

Details

  • Type: Improvement Improvement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.8.4
  • Fix Version/s: None
  • Component/s: Zend_Pdf
  • Labels:
    None

Description

Hi,

When generating pdfs through Zend_Pdf, I felt the need of finding the width of the text to be drawn.
Purpose is to be able to horizontally center text, align it to right and more.

It looks like other people felt that need too, here is a function found on internet that does it pretty well and it would be great to have it incorporated natively within Zend_Pdf_Page.

/**
 * Return length of generated string in points
 *
 * @param string $string
 * @param Zend_Pdf_Resource_Font $font
 * @param int $font_size
 * @return double
 */
public function getTextWidth($text, Zend_Pdf_Resource_Font $font, $font_size) 
{
	$drawing_text = iconv('', 'UTF-16BE', $text);
	$characters    = array();
	for ($i = 0; $i < strlen($drawing_text); $i++) {
		$characters[] = (ord($drawing_text[$i++]) << 8) | ord ($drawing_text[$i]);
	}
	$glyphs        = $font->glyphNumbersForCharacters($characters);
	$widths        = $font->widthsForGlyphs($glyphs);
	$text_width   = (array_sum($widths) / $font->getUnitsPerEm()) * $font_size;
	return $text_width;	
}

ps: maybe Zend_Pdf_Style::getLineWidth() is related to this topic, but I could not find any info on the doc nor in the code.

Activity

There are no comments yet on this issue.

People

Vote (2)
Watch (1)

Dates

  • Created:
    Updated: