Discussion:
Finding width of a drawText Text in Zend_Pdf
allan vernon
2007-03-13 07:15:09 UTC
Permalink
Dear Kevin/Alexander/Willie

I cannot find a way of calculating how many points a text string would take
up using
$pdfPage->drawText('What is the width in points this text would require
using the current font and fontsize?', 72, 720);.
I need this so that I can right margin justify text output to a Pdf file.
The functions seem to be set out in library/Zend/Pdf/Resource/Font.php.
Under abstract class Zend_Pdf_Resource_Font extends Zend_Pdf_Resource there
are

public function getUnitsPerEm()
{
return self::$this->_unitsPerEm;
return $this->_unitsPerEm;
}

and

public function widthsForGlyphs(&$glyphNumbers)
{
$widths = array();
foreach ($glyphNumbers as $key => $glyphNumber) {
if (($glyphNumber < 0) || ($glyphNumber >
$this->_glyphMaxIndex)) {
throw new Zend_Pdf_Exception("Glyph number is out of range:
$glyphNumber",

Zend_Pdf_Exception::GLYPH_OUT_OF_RANGE);
}
$widths[$key] = $this->_glyphWidths[$glyphNumber];
}
print_r($widths);
return $widths;
}

/**
* Returns the width of the glyph.
*
* Like {@link widthsForGlyphs()} but used for one glyph at a time.
*
* @param integer $glyphNumber
* @return integer
* @throws Zend_Pdf_Exception
*/
public function widthForGlyph($glyphNumber)
{
if (($glyphNumber < 0) || ($glyphNumber > $this->_glyphMaxIndex)) {
throw new Zend_Pdf_Exception("Glyph number is out of range:
$glyphNumber",

Zend_Pdf_Exception::GLYPH_OUT_OF_RANGE);
}
return $this->_glyphWidths[$glyphNumber];
}

However, I have no idea how to implement these functions in the user space.

Help would be much appreciated.

Allan Vernon
--
View this message in context: http://www.nabble.com/Finding-width-of-a-drawText-Text-in-Zend_Pdf-tf3394291s16154.html#a9449424
Sent from the Zend MFS mailing list archive at Nabble.com.
Kevin McArthur
2007-03-13 16:13:00 UTC
Permalink
Allan,

IIRC there are some measurement items to get the gliph widths. Email willie
directly, he wrote that part.

Kevin
----- Original Message -----
From: "allan vernon" <avernon-***@public.gmane.org>
To: <fw-formats-***@public.gmane.org>
Sent: Tuesday, March 13, 2007 12:15 AM
Subject: [fw-formats] Finding width of a drawText Text in Zend_Pdf
Post by allan vernon
Dear Kevin/Alexander/Willie
I cannot find a way of calculating how many points a text string would take
up using
$pdfPage->drawText('What is the width in points this text would require
using the current font and fontsize?', 72, 720);.
I need this so that I can right margin justify text output to a Pdf file.
The functions seem to be set out in library/Zend/Pdf/Resource/Font.php.
Under abstract class Zend_Pdf_Resource_Font extends Zend_Pdf_Resource there
are
public function getUnitsPerEm()
{
return self::$this->_unitsPerEm;
return $this->_unitsPerEm;
}
and
public function widthsForGlyphs(&$glyphNumbers)
{
$widths = array();
foreach ($glyphNumbers as $key => $glyphNumber) {
if (($glyphNumber < 0) || ($glyphNumber >
$this->_glyphMaxIndex)) {
$glyphNumber",
Zend_Pdf_Exception::GLYPH_OUT_OF_RANGE);
}
$widths[$key] = $this->_glyphWidths[$glyphNumber];
}
print_r($widths);
return $widths;
}
/**
* Returns the width of the glyph.
*
*
*/
public function widthForGlyph($glyphNumber)
{
if (($glyphNumber < 0) || ($glyphNumber > $this->_glyphMaxIndex)) {
$glyphNumber",
Zend_Pdf_Exception::GLYPH_OUT_OF_RANGE);
}
return $this->_glyphWidths[$glyphNumber];
}
However, I have no idea how to implement these functions in the user space.
Help would be much appreciated.
Allan Vernon
--
http://www.nabble.com/Finding-width-of-a-drawText-Text-in-Zend_Pdf-tf3394291s16154.html#a9449424
Sent from the Zend MFS mailing list archive at Nabble.com.
Willie Alberty
2007-05-18 06:13:43 UTC
Permalink
Post by allan vernon
I cannot find a way of calculating how many points a text string would take
up using
$pdfPage->drawText('What is the width in points this text would require
using the current font and fontsize?', 72, 720);.
I need this so that I can right margin justify text output to a Pdf file.
I'm back from the dead. :-) This response is two months late, but
hopefully it will still help...

This function should do exactly what you need:

/**
* 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.
*
* @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-16BE', $string);
$characters = array();
for ($i = 0; $i < strlen($drawingString); $i++) {
$characters[] = (ord($drawingString[$i++]) << 8) | ord
($drawingString[$i]);
}
$glyphs = $font->cmap->glyphNumbersForCharacters($characters);
$widths = $font->widthsForGlyphs($glyphs);
$stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) *
$fontSize;
return $stringWidth;
}

$font = Zend_Pdf_FontFactory::fontWithName
(Zend_Pdf_Const::FONT_HELVETICA);
$stringWidth = widthForStringUsingFontSize('Hello world!', $font, 12);

--

Willie Alberty, Owner
Spenlen Media
willie-***@public.gmane.org

http://www.spenlen.com/

Loading...