note 59623 added to function.imagettfbbox

From: Date: Sun, 11 Dec 2005 17:31:21 +0000
Subject: note 59623 added to function.imagettfbbox
Groups: php.notes 
Request: Send a blank email to php-notes+get-99958@lists.php.net to get a copy of this message
I have revised my mb_wordwrap function to fix 2 major bugs: cannot handle line break and infinite loop while handling very very long long long single byte word. <?php function mb_wordwrap($txt,$font,$size,$width) { $pointer = 0; // Current character position pointer $this_line_start = 0; // Starting character position of current line $this_line_strlen = 1; // How long is the current line $single_byte_stack = ""; // Variable for storing single byte word $sbs_line_width = 0; // Pixel width of the Single byte word $this_is_cr = FALSE; // Check if the character is new line code (ASCII=10) $result_lines = array(); // Array for storing the return result while ($pointer < mb_strlen($txt)) { $this_char = mb_substr($txt,$pointer,1); if (ord($this_char[0])==10) $this_is_cr = TRUE; // Check if it is a new line // Check current line width $tmp_line = mb_substr($txt, $this_line_start, $this_line_strlen); $tmp_line_bbox = imagettfbbox($size,0,$font,$tmp_line); $this_line_width = $tmp_line_bbox[2]-$tmp_line_bbox[0]; // Prevent to cut off english word at the end of line // if this character is a alphanumeric character or open bracket, put it into stack if (is_alphanumeric($this_char, $single_byte_stack)) $single_byte_stack .= $this_char; // Check the width of single byte words if ($single_byte_stack != "") { $tmp_line_bbox = imagettfbbox($size,0,$font,$single_byte_stack); $sbs_line_width = $tmp_line_bbox[2]-$tmp_line_bbox[0]; } if ($this_is_cr || $this_line_width > $width || $sbs_line_width >= $width) { // If last word is alphanumeric, put it to next line rather then cut it off if ($single_byte_stack != "" && is_alphanumeric($this_char, $single_byte_stack) && $sbs_line_width < $width) { $stack_len = mb_strlen($single_byte_stack); $this_line_strlen = $this_line_strlen - $stack_len + 1; $pointer = $pointer - $stack_len + 1; } // Move the current line to result array and reset all counter $result_lines[] = mb_substr($txt, $this_line_start, $this_line_strlen-1); if ($this_is_cr) { $pointer++; $this_is_cr=FALSE; } if ($sbs_line_width >= $width) $sbs_line_width = 0; $this_line_start = $pointer; $this_line_strlen = 1; $single_byte_stack = ""; } else { if (!is_alphanumeric($this_char, $single_byte_stack)) { $single_byte_stack = ""; // Clear stack if met multibyte character and not line end } $this_line_strlen++; $pointer++; } } // Move remained word to result $result_lines[] = mb_substr($txt, $this_line_start); return $result_lines; } function is_alphanumeric($character, $stack) { if ( (ord($character)>=48 && ord($character)<=57) || (ord($character)>=65 && ord($character)<=91) || (ord($character)>=97 && ord($character)<=123) || ord($character)==40 || ord($character)==60 || ($stack=="" && (ord($character)==34 || ord($character)==39)) ) return TRUE; else return FALSE; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59623 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59623&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59623&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59623&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59623&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59623&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59623&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59623&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59623&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99958) next »