note 58972 added to function.imagettfbbox
| From: | Mickey9801atComicPartydotcom at osu1 dot php dot net | Date: | Tue, 22 Nov 2005 20:49:16 +0000 |
| Subject: | note 58972 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98940@lists.php.net to get a copy of this message | ||
Most of functions shared here seems only work with western language and is not suitable for
multibyte characters (like Chinese). I have written a function using mb_string functions to match
the need of multibyte character word wrapping.
I also added some machanism so that English word won't be cut off at the end of line. Of couse
you must use unicode string on GD.
function mb_wordwrap($txt,$font,$size,$width) {
$pointer = 0;
$this_line_start = 0;
$this_line_strlen = 1;
$single_byte_stack = "";
$result_lines = array();
while ($pointer <= mb_strlen($txt)) {
$this_char = mb_substr($txt,$pointer,1);
$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];
if ($this_line_width > $width) {
// If last word is alphanumeric, put it to next line rather then cut it off
if ($single_byte_stack != "") {
$stack_len = mb_strlen($single_byte_stack);
$this_line_strlen -= $stack_len;
$pointer -= $stack_len;
}
$result_lines[] = mb_substr($txt, $this_line_start, $this_line_strlen-1);
$this_line_start = $pointer;
$this_line_strlen = 1;
$single_byte_stack = "";
} else {
// 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 (
(ord($this_char)>=48 && ord($this_char)<=57) ||
(ord($this_char)>=65 && ord($this_char)<=91) ||
(ord($this_char)>=97 && ord($this_char)<=123) ||
ord($this_char)==40 ||
ord($this_char)==60 ||
($single_byte_stack=="" && (ord($this_char)==34 || ord($this_char)==39))
) $single_byte_stack .= $this_char;
else $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;
}
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58972
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58972&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58972&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58972&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58972&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58972&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58972&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58972&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58972&report=yes
Search -- http://master.php.net/manage/user-notes.php