History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: ZF-313
Type: New Feature New Feature
Status: Open Open
Priority: Major Major
Assignee: Alexander Veremyev
Reporter: Tobias Plaputta
Votes: 13
Watchers: 8
Operations

If you were logged in you would be able to see more operations.
Google issue summary
Zend Framework

TTFBox or TextWidth/Height function (word wrapping functionality)

Created: 09/Aug/06 11:08 AM   Updated: 19/Dec/08 01:33 PM
Component/s: Zend_Pdf
Affects Version/s: 0.1.4
Fix Version/s: None

Time Tracking:
Not Specified

Issue Links:
Related
 

Tags:
Participants: Alexander Veremyev, Alexandre Lemaire, Bill Karwin, Remi, Tobias Plaputta, Wil Sinclair and Willie Alberty


 Description  « Hide
Problem:
When using the drawText function to write a string that is longer than the page, there is no WordWrap.
Is there a way to print text wrapped or to find out the width and height of the string that is about to be printed?

 All   Comments   Work Log   Change History   FishEye   Crucible      Sort Order: Ascending order - Click to sort in descending order
Willie Alberty - 09/Aug/06 06:32 PM
I am working on some advanced layout classes that will take care of wrapping long lines for you. This code won't be ready for several more weeks. In the meantime, you may be able to use this function:
/**
* 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);

Tobias Plaputta - 10/Aug/06 05:17 AM
@Wille Alberty: Amazing, this is 100% what I have needed! Thank you very much!

Bill Karwin - 13/Nov/06 03:09 PM
Changing fix version to unknown.

Bill Karwin - 13/Nov/06 03:26 PM
Changing fix version to 0.6.0.

Alexander Veremyev - 25/May/07 02:22 AM
Summary is changed to describe problem more clear.

Alexander Veremyev - 25/May/07 02:23 AM
Depends on other text formatting tasks (canvases, paragraphs and so on)

Alexander Veremyev - 25/May/07 06:34 AM
Assignee is changed to postpone issue to post-v1.0 period

Alexander Veremyev - 25/May/07 06:35 AM
Postponed to post-v1.0

Alexandre Lemaire - 12/Sep/08 02:32 PM
This only partially alleviates the metrics required for word wrapping though. This may be neat, but the instance where it doesn't help, is where you have a long string, need to word-wrap it, but need to know at which width a line should be cut without cutting words.

PHP inherently offers us the wordwrap function, which allows us to cut a string into an array where lines don't exceed a certain width, taking word composition into consideration. It's useless in this case though, since a 'max width' is very variable when font glyphs are of variable sizes.

Is there a function in Zend_PDF somewhat akin to the above, which allows us to determine a cut-strategy given:

  • a font
  • a font size
  • a page width (or width-coordinate restriction)
  • a (possibly multi-line) string of text?

Thanks.
Alex


Remi - 05/Nov/08 10:58 PM
There is a new Zend_Font Component Proposal available for this:
http://framework.zend.com/wiki/display/ZFPROP/Zend_Font+-+Karol+Babioch

I would love to see a little bit progress with Zend_Pdf.


Willie Alberty - 04/Dec/08 10:34 PM
Unassigning Zend_Pdf issues currently assigned to me pending decision on ZF 2.0 release timeframe and potential contribution of comprehensive changeset.

Wil Sinclair - 19/Dec/08 01:33 PM
Alex, could you please look at this issue and mark it 'postponed' if we won't be addressing it before 1.8. Note that it has several votes.