function textimage_wrap_text in Textimage 7.2
Same name and namespace in other branches
- 5.2 textimage.module \textimage_wrap_text()
- 5 textimage.module \textimage_wrap_text()
- 6.2 textimage.module \textimage_wrap_text()
Wrap text for rendering at a given width.
1 call to textimage_wrap_text()
- textimage_text_to_image in ./
textimage.utils.inc - Generate an image containing text with the given parameters.
File
- ./
textimage.utils.inc, line 126 - Utility routines of Textimage.
Code
function textimage_wrap_text($text, $fontsize, $font, $maximum_width, $align) {
// State variables for the search interval.
$end = 0;
$begin = 0;
$fit = $begin;
if ($align == ALIGN_CENTER) {
// Get width of the space character.
$space_width = textimage_measure_text_width(' ', $fontsize, $font);
}
// Note: we count in bytes for speed reasons, but maintain character
// boundaries.
while (TRUE) {
// Find the next wrap point (always after trailing whitespace).
if (drupal_preg_match('/[' . PREG_CLASS_PUNCTUATION . '][' . PREG_CLASS_SEPARATOR . ']*|[' . PREG_CLASS_SEPARATOR . ']+/u', $text, $match, PREG_OFFSET_CAPTURE, $end)) {
$end = $match[0][1] + drupal_strlen($match[0][0]);
}
else {
$end = drupal_strlen($text);
}
// Fetch text, removing trailing white-space and measure it.
$line = preg_replace('/[' . PREG_CLASS_SEPARATOR . ']+$/u', '', drupal_substr($text, $begin, $end - $begin));
$width = textimage_measure_text_width($line, $fontsize, $font);
// See if $line extends past the available space.
if ($width > $maximum_width) {
// If this is the first word, we need to truncate it.
if ($fit == $begin) {
// Cut off letters until it fits.
while (drupal_strlen($line) > 0 && $width > $maximum_width) {
$line = drupal_substr($line, 0, -1);
$width = textimage_measure_text_width($line, $fontsize, $font);
}
// If no fit was found, the image is too narrow..
$fit = drupal_strlen($line) ? $begin + drupal_strlen($line) : $end;
}
// We have a valid fit for the next line. Insert a line-break and reset
// the search interval.
$first_part = drupal_substr($text, 0, $fit);
$last_part = drupal_substr($text, $fit);
if ($align == ALIGN_CENTER) {
$text = $first_part . "\n" . $last_part;
// Get last line of the text's first part.
$first_part_array = explode("\n", $first_part);
$last_index = count($first_part_array) - 1;
$last_line = $first_part_array[$last_index];
$last_line_width = textimage_measure_text_width($last_line, $fontsize, $font);
// Get needed space for padding.
$num_space = floor(($maximum_width - $last_line_width) / $space_width);
// Add space padding (to center the last line of the text's first
// part).
for ($i = 0; $i < $num_space; ++$i) {
$last_line = ' ' . $last_line;
}
$first_part_array[$last_index] = $last_line;
$first_part = implode("\n", $first_part_array);
$last_part_width = textimage_measure_text_width($last_part, $fontsize, $font);
if ($last_part_width < $maximum_width) {
// Get needed space for last part's padding.
$num_space = floor(($maximum_width - $last_part_width) / $space_width) - 2;
$last_part = trim($last_part);
// Add space padding (to center the last line of the
// text's first part).
for ($i = 0; $i < $num_space; ++$i) {
$last_part = ' ' . $last_part;
}
}
}
$text = $first_part . "\n" . $last_part;
$begin = ++$fit;
$end = $begin;
}
else {
// We can fit this text. Wait for now.
$fit = $end;
}
if ($end == drupal_strlen($text)) {
// All text fits. No more changes are needed.
break;
}
}
return $text;
}