From d51946e2c1b2823e0b2d100e9aca675eb5f0ec4b Mon Sep 17 00:00:00 2001
From: Eric Alvares <erica@344430.no-reply.drupal.org>
Date: Mon, 21 Mar 2011 00:22:47 +0100
Subject: [PATCH] Issue #1099546 by Eric_A: Moved image resource knowledge out of template_preprocess_chessboard().

---
 chessboard.module                          |   55 +++++++++++++++++++
 chessboard_filter/chessboard_filter.module |   19 +++++++
 chessboard_render.php                      |   82 +---------------------------
 3 files changed, 75 insertions(+), 81 deletions(-)

diff --git a/chessboard.module b/chessboard.module
index 63fce2b..c7d4484 100644
--- a/chessboard.module
+++ b/chessboard.module
@@ -1,4 +1,35 @@
 <?php
+function chessboard_border_filename($border) {
+  // $border: T, B, L, R, TL, TR, BL, BR
+  switch ($border) {
+    case 'T':
+    case 'B':
+      return 'h.png';
+    case 'L':
+    case 'R':
+      return 'v.png';
+    default:
+      return 'c.png';
+  }
+}
+
+function chessboard_piece_filename($piece, $square_color) {
+  // $piece        : KQBNRPkqbnrp-
+  // $square_color : 0 1
+  switch ($piece) {
+    case 'x':
+      $name = 'xx';
+      break;
+    case '-':
+      $name = '';
+      break;
+    default:
+      $name = strtolower($piece) . (ctype_lower($piece) ? 'd' : 'l');
+      break;
+  }
+
+  return $name . ($square_color ? 'd' : 'l') . '.png';
+}
 
 function chessboard_process_chessboard_notation($elements) {
   $item = $elements['#item'];
@@ -49,6 +80,30 @@ function chessboard_process_chessboard_notation($elements) {
       $file++;
     }
   }
+
+  $image_directory_path = $elements['#image_directory_path'];
+  $size_attributes = $elements['#dimensions'];
+  $groups = array('square', 'border-h', 'border-v', 'border-c');
+  $test_paths = array(
+    'square' => $image_directory_path . '/' . chessboard_piece_filename('-', 0),
+    'border-h' => $image_directory_path . '/' . chessboard_border_filename('T'),
+    'border-v' => $image_directory_path . '/' . chessboard_border_filename('R'),
+    'border-c' => $image_directory_path . '/' . chessboard_border_filename('TR'),
+  );
+  foreach ($test_paths as $group => $test_path) {
+    if (!isset($size_attributes[$group]) && is_file($test_path) && (list($width, $height) = @getimagesize($test_path))) {
+      $size_attributes[$group] = array(
+        'width' => $width,
+        'height' => $height,
+      );
+    }
+  }
+  foreach ($groups as $group) {
+    if (!isset($size_attributes[$group])) {
+      $size_attributes[$group] = array();
+    }
+  }
+  $elements['#dimensions'] = $size_attributes;
   return $elements;
 }
 
diff --git a/chessboard_filter/chessboard_filter.module b/chessboard_filter/chessboard_filter.module
index 821fc71..0f5ed3a 100755
--- a/chessboard_filter/chessboard_filter.module
+++ b/chessboard_filter/chessboard_filter.module
@@ -218,8 +218,27 @@ function _chessboard_filter_formatter_view($items) {
           drupal_get_path('module', 'chessboard') . '/chessboard.css' => array('media' => 'all', 'preprocess' => FALSE),
         ),
       ),
+      '#dimensions' => array(
+        'square' => array(
+          'width' => 40,
+          'height' => 40,
+        ),
+        'border-h' => array(
+          'width' => 40,
+          'height' => 4,
+        ),
+        'border-v' => array(
+          'width' => 4,
+          'height' => 40,
+        ),
+        'border-c' => array(
+          'width' => 4,
+          'height' => 4,
+        ),
+      ),
       '#pre_render' => array('chessboard_process_chessboard_notation'),
       '#theme' => 'chessboard',
+      '#image_directory_path' => drupal_get_path('module', 'chessboard') . '/default',
       '#item' => $item,
       '#prefix' => '<div class="chessboard">',
       '#suffix' => '</div>',
diff --git a/chessboard_render.php b/chessboard_render.php
index ebcc210..f634cca 100644
--- a/chessboard_render.php
+++ b/chessboard_render.php
@@ -1,57 +1,14 @@
 <?php
-
 /**
  * @file
  * Theme related functions for the Chessboard Renderer module.
  */
 
-function chessboard_border_filename($border) {
-  // $border: T, B, L, R, TL, TR, BL, BR
-  switch ($border) {
-    case 'T':
-    case 'B':
-      return 'h.png';
-    case 'L':
-    case 'R':
-      return 'v.png';
-    default:
-      return 'c.png';
-  }
-}
-
-function chessboard_piece_filename($piece, $square_color) {
-  // $piece        : KQBNRPkqbnrp-
-  // $square_color : 0 1
-  switch ($piece) {
-    case 'x':
-      $name = 'xx';
-      break;
-    case '-':
-      $name = '';
-      break;
-    default:
-      $name = strtolower($piece) . (ctype_lower($piece) ? 'd' : 'l');
-      break;
-  }
-
-  return $name . ($square_color ? 'd' : 'l') . '.png';
-}
-
 function template_preprocess_chessboard(&$variables) {
   static $tables_xhtml = array();
 
-  if (!isset($variables['element']['#image_directory_path'])) {
-    $variables['element']['#image_directory_path'] = drupal_get_path('module', 'chessboard') . '/default';
-  }
   $image_directory_path = $variables['element']['#image_directory_path'];
   if (!isset($tables_xhtml[$image_directory_path])) {
-    $groups = array('square', 'border-h', 'border-v', 'border-c');
-    $test_paths = array(
-      'square' => $image_directory_path . '/' . chessboard_piece_filename('-', 0),
-      'border-h' => $image_directory_path . '/' . chessboard_border_filename('T'),
-      'border-v' => $image_directory_path . '/' . chessboard_border_filename('R'),
-      'border-c' => $image_directory_path . '/' . chessboard_border_filename('TR'),
-    );
     $class_attributes = array(
       'square' => array(
         'class' => 'chessboard-square',
@@ -66,44 +23,7 @@ function template_preprocess_chessboard(&$variables) {
         'class' => 'chessboard-border-c',
       ),
     );
-    $default_image_directory_path = drupal_get_path('module', 'chessboard') . '/default';
-    if ($image_directory_path === $default_image_directory_path) {
-      $size_attributes = array(
-        'square' => array(
-          'width' => 40,
-          'height' => 40,
-        ),
-        'border-h' => array(
-          'width' => 40,
-          'height' => 4,
-        ),
-        'border-v' => array(
-          'width' => 4,
-          'height' => 40,
-        ),
-        'border-c' => array(
-          'width' => 4,
-          'height' => 4,
-        ),
-      );
-    }
-    foreach ($test_paths as $group => $test_path) {
-      if (!isset($size_attributes[$group]) && is_file($test_path) && (list($width, $height) = @getimagesize($test_path))) {
-        $size_attributes[$group] = array(
-          'width' => $width,
-          'height' => $height,
-        );
-      }
-    }
-
-    foreach ($groups as $group) {
-      if (!isset($size_attributes[$group])) {
-        $size_attributes[$group] = array();
-      }
-      if (!isset($class_attributes[$group])) {
-        $class_attributes[$group] = array();
-      }
-    }
+    $size_attributes = $variables['element']['#dimensions'];
 
     // Construct the translation table
 
-- 
1.7.4

