Thanks Willie, I understand fully now!!!!


Willie Alberty wrote:
> 
> On Nov 14, 2006, at 7:11 AM, Brent Robinson wrote:
> 
>> I am having some issues with generating a script to align  
>> characters to the right on the framework pdf component.
>>
>> I am trying to extract the glyph width but am getting very large  
>> widths like 333 for L and 556 for E. This is very high or I am not  
>> sure what I am supposed to work that number against.
> 
> Glyph widths are typically normalized to a 1000 unit-per-em box,  
> which is independent of the font size. For the font you're using, the  
> L takes up a third of the total width of the widest character in the  
> font; the E about half. You were on the right track with your sample  
> code, but were just missing a couple of steps.
> 
>> Is there any way of aligning right or an easier way of gaining the  
>> width of a text for a specific font?
> 
> There should be an easier way, and I'm working on one now (trying to  
> find time amongst the other projects I've got going). A future layout  
> class will allow you to specify text alignment as a paragraph  
> attribute and it will handle all of the messy details. It will also  
> take care of things like line wrapping, multiple fonts and sizes,  
> etc. I've got a paying project that will be using these classes, but  
> it's not on the schedule to be complete until January.
> 
> In the meantime, here is a function you can use that will return the  
> width in points for a given string, font, and size:
> 
> /**
> * 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);
> 
> 
> You can then use the width of the string to calculate your $x  
> coordinate for center or right alignment.
> 
> --
> 
> Willie Alberty, Owner
> Spenlen Media
> [EMAIL PROTECTED]
> 
> http://www.spenlen.com/
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Zend-PDF---align-right-tf2630009s16154.html#a7374847
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to