Andrew Yager wrote: > > Hi, > > Is there a simple way to format a text string when writing? In > particular I am interested in right aligning a variable width string. > > In FPDF there is a flag you can pass to specify the justification. > I'd even be happy if I could computationally determine the width of a > string before I rendered it so I could correctly have it displayed. > > Thanks, > Andrew > > _________________________ > Andrew Yager, Managing Director > Real World Technology Solutions Pty Ltd > ph: 1300 798 718 or (02) 9563 4840 > fax: (02) 9563 4848 mob: 0405 152 568 > http://www.rwts.com.au/ > _________________________ > > > >
Have a look at Willie Alberty's post concerning this: http://www.nabble.com/forum/ViewPost.jtp?post=7343094&framed=y&skin=16154 of Nov 2006 Quote: " 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. " I have found this useful. Allan Vernon -- View this message in context: http://www.nabble.com/Text-formatting-in-PDFs-tf3668934s16154.html#a10265647 Sent from the Zend Framework mailing list archive at Nabble.com.