note 58972 deleted from function.imagettfbbox by cmb

From: Date: Sun, 07 Jul 2019 08:10:46 +0000
Subject: note 58972 deleted from function.imagettfbbox by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211515@lists.php.net to get a copy of this message
Note Submitter: Mickey9801 at ComicParty dot com ---- 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; }

« previous php.notes (#211515) next »