You are here

function _textimage_get_text_wrapper in Textimage 7.3

Get the image containing the text.

This is separated from textimage_text_effect() so that it can also be used by the textimage_text_effect_dimensions() function.

2 calls to _textimage_get_text_wrapper()
textimage_text_effect in effects/textimage_text.inc
Implements 'textimage_text' image effect callback.
textimage_text_effect_dimensions in effects/textimage_text.inc
Implements 'textimage_text' image dimensions callback.

File

effects/textimage_text.inc, line 867
Implementation of the 'textimage_text' image effect.

Code

function _textimage_get_text_wrapper($image, array $data) {

  // Include toolkit specific image functions.
  if (!_textimage_load_toolkit_functions()) {
    return NULL;
  }

  // Check font path.
  $font_uri = $data['font']['uri'];
  $data['font']['uri'] = _textimage_get_font_path($image, $data['font']['uri']);
  if (!$data['font']['uri']) {
    _textimage_diag(t("Textimage could not find the font file @fontfile for font @fontname", array(
      '@fontfile' => $font_uri,
      '@fontname' => $data['font']['name'],
    )), WATCHDOG_ERROR, __FUNCTION__);
    return NULL;
  }

  // If the effect is executed outside of the context of the TextimageImager
  // class (e.g. by the core Image module), then the text_string has not been
  // pre-processed to translate tokens or apply text conversion.
  if (!(TextimageImager::getState('building_module') == 'textimage')) {
    $data['text_string'] = TextimageImager::processTextString($data['text_string'], $data['text']['case_format']);
  }

  // Determine if outline/shadow is required.
  $outline = $shadow = FALSE;
  if ($data['font']['stroke_mode'] == 'outline' && ($data['font']['outline_top'] || $data['font']['outline_right'] || $data['font']['outline_bottom'] || $data['font']['outline_left']) && $data['font']['stroke_color']) {
    $outline = TRUE;
  }
  elseif ($data['font']['stroke_mode'] == 'shadow' && ($data['font']['shadow_x_offset'] || $data['font']['shadow_y_offset'] || $data['font']['shadow_width'] || $data['font']['shadow_height']) && $data['font']['stroke_color']) {
    $shadow = TRUE;
  }

  // Add stroke to padding to ensure inner box includes entire font space.
  if ($outline) {
    $data['layout']['padding_top'] += $data['font']['outline_top'];
    $data['layout']['padding_right'] += $data['font']['outline_right'];
    $data['layout']['padding_bottom'] += $data['font']['outline_bottom'];
    $data['layout']['padding_left'] += $data['font']['outline_left'];
  }
  elseif ($shadow) {
    $data['layout']['padding_top'] += $data['font']['shadow_y_offset'] < 0 ? -$data['font']['shadow_y_offset'] : 0;
    $data['layout']['padding_right'] += $data['font']['shadow_x_offset'] > 0 ? $data['font']['shadow_x_offset'] : 0;
    $data['layout']['padding_bottom'] += $data['font']['shadow_y_offset'] > 0 ? $data['font']['shadow_y_offset'] : 0;
    $data['layout']['padding_left'] += $data['font']['shadow_x_offset'] < 0 ? -$data['font']['shadow_x_offset'] : 0;
    $shadow_width = $data['font']['shadow_x_offset'] != 0 ? $data['font']['shadow_width'] + 1 : $data['font']['shadow_width'];
    $shadow_height = $data['font']['shadow_y_offset'] != 0 ? $data['font']['shadow_height'] + 1 : $data['font']['shadow_height'];
    $net_right = $shadow_width + ($data['font']['shadow_x_offset'] >= 0 ? 0 : $data['font']['shadow_x_offset']);
    $data['layout']['padding_right'] += $net_right > 0 ? $net_right : 0;
    $net_bottom = $shadow_height + ($data['font']['shadow_y_offset'] >= 0 ? 0 : $data['font']['shadow_y_offset']);
    $data['layout']['padding_bottom'] += $net_bottom > 0 ? $net_bottom : 0;
  }

  // Perform text wrapping, if necessary.
  if ($data['text']['maximum_width'] > 0) {
    $data['text_string'] = _textimage_wrap_text($image, $data['text_string'], $data['font']['size'], $data['font']['uri'], $data['text']['maximum_width'] - $data['layout']['padding_left'] - $data['layout']['padding_right'] - 1, $data['text']['align']);
  }

  // Load text lines to array elements.
  $text_lines = explode("\n", $data['text_string']);
  $num_lines = count($text_lines);

  // Calculate bounding boxes.
  // --------------------------------------------------------------------
  // Inner box: the exact bounding box of the text.
  // Outer box: the box where the inner box is - can be different because
  // of padding.
  // Wrapper:the canvass where the outer box is laid.
  // --------------------------------------------------------------------
  //
  // Get inner box, for horizontal text, unpadded.
  $inner_box = _textimage_get_bounding_box($image, $data['text_string'], $num_lines, $data['font']['size'], $data['font']['uri']);

  // Adjust to fixed width, if requested.
  if ($data['text']['fixed_width'] && !empty($data['text']['maximum_width'])) {
    $inner_box
      ->set('width', $data['text']['maximum_width'] - $data['layout']['padding_left'] - $data['layout']['padding_right']);
  }

  // Determine average text line height.
  $line_height = round($inner_box
    ->get('height') / $num_lines);

  // Manage leading (line spacing), adding total line spacing to height.
  if ($data['text']['line_spacing']) {
    $inner_box
      ->set('height', $inner_box
      ->get('height') + $data['text']['line_spacing'] * ($num_lines - 1));
  }

  // Apply padding to get outer box.
  $outer_box = clone $inner_box;
  $outer_box
    ->set('width', $outer_box
    ->get('width') + $data['layout']['padding_right'] + $data['layout']['padding_left']);
  $outer_box
    ->set('height', $outer_box
    ->get('height') + $data['layout']['padding_top'] + $data['layout']['padding_bottom']);

  // Get details for the rotated/translated boxes.
  $outer_box_t = $outer_box
    ->getTranslatedBox($data['font']['angle']);
  $inner_box_t = $inner_box
    ->getTranslatedBox($data['font']['angle'], array(
    $data['layout']['padding_left'],
    $data['layout']['padding_top'],
  ), $outer_box_t
    ->get('topLeftCornerPosition'));

  // Create the wrapper image object as a canvass for the text.
  $wrapper = new stdClass();
  $wrapper->info['width'] = $outer_box_t
    ->get('width');
  $wrapper->info['height'] = $outer_box_t
    ->get('height');
  $wrapper->info['extension'] = $image->info['extension'];
  $wrapper->info['mime_type'] = $image->info['mime_type'];
  $wrapper->toolkit = $image->toolkit;

  // Calls image generation for the wrapper image.
  $data_textimage = array(
    'font' => $data['font'],
    'layout' => $data['layout'],
    'text' => $data['text'],
    'text_lines' => $text_lines,
    'inner_width' => $inner_box
      ->get('width'),
    'inner_height' => $inner_box
      ->get('height'),
    'inner_basepoint' => $inner_box
      ->get('basepoint'),
    'topLeftCornerPosition' => $outer_box_t
      ->get('topLeftCornerPosition'),
    'inner_box' => $inner_box_t
      ->get('points'),
    'outer_box' => $outer_box_t
      ->get('points'),
    'line_height' => $line_height,
    'debug_visuals' => isset($data['debug_visuals']) ? $data['debug_visuals'] : FALSE,
    'gif_transparency_color' => TextimageImager::getState('gif_transparency_color'),
  );
  if (!image_toolkit_invoke('textimage_text_to_image', $wrapper, array(
    $data_textimage,
  ))) {
    return NULL;
  }
  return $wrapper;
}