changeset 17454:fd3e999305ea

Moved functionality from latex and ft renderer to base_text_renderer. * txt-render.cc/.h: Created three new methods: rotate_data, alignment and rotate_bounding_box. * txt-latex.cc/.h: Moved part of functionality to three new methods of base_text_render. * txt-eng-ft.cc/.h: Moved part of functionality to three new methods of base_text_render. To avoid multiple copy/paste code.
author Andrej Lojdl <andrej.lojdl@gmail.com>
date Tue, 17 Sep 2013 20:40:02 +0200
parents a46a648a2de2
children 9bc940e63aa2
files libinterp/corefcn/txt-eng-ft.cc libinterp/corefcn/txt-eng-ft.h libinterp/corefcn/txt-latex.cc libinterp/corefcn/txt-latex.h libinterp/corefcn/txt-render.cc libinterp/corefcn/txt-render.h
diffstat 6 files changed, 143 insertions(+), 206 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/txt-eng-ft.cc
+++ b/libinterp/corefcn/txt-eng-ft.cc
@@ -856,51 +856,8 @@
   if (pixels.numel () > 0)
     {
       elt->accept (*this);
-
-      switch (rotation)
-        {
-        case ROTATION_0:
-          break;
-        case ROTATION_90:
-            {
-              Array<octave_idx_type> perm (dim_vector (3, 1));
-              perm(0) = 0;
-              perm(1) = 2;
-              perm(2) = 1;
-              pixels = pixels.permute (perm);
-
-              Array<idx_vector> idx (dim_vector (3, 1));
-              idx(0) = idx_vector (':');
-              idx(1) = idx_vector (pixels.dim2 ()-1, -1, -1);
-              idx(2) = idx_vector (':');
-              pixels = uint8NDArray (pixels.index (idx));
-            }
-          break;
-        case ROTATION_180:
-            {
-              Array<idx_vector> idx (dim_vector (3, 1));
-              idx(0) = idx_vector (':');
-              idx(1) = idx_vector (pixels.dim2 ()-1, -1, -1);
-              idx(2)=  idx_vector (pixels.dim3 ()-1, -1, -1);
-              pixels = uint8NDArray (pixels.index (idx));
-            }
-          break;
-        case ROTATION_270:
-            {
-              Array<octave_idx_type> perm (dim_vector (3, 1));
-              perm(0) = 0;
-              perm(1) = 2;
-              perm(2) = 1;
-              pixels = pixels.permute (perm);
-
-              Array<idx_vector> idx (dim_vector (3, 1));
-              idx(0) = idx_vector (':');
-              idx(1) = idx_vector (':');
-              idx(2) = idx_vector (pixels.dim3 ()-1, -1, -1);
-              pixels = uint8NDArray (pixels.index (idx));
-            }
-          break;
-        }
+      uint8NDArray data = pixels;
+      pixels = rotate_data (rotation, data);     
     }
 
   return pixels;
@@ -946,21 +903,6 @@
   return extent;
 }
 
-int
-ft_render::rotation_to_mode (double rotation) const
-{
-  if (rotation == 0.0)
-    return ROTATION_0;
-  else if (rotation == 90.0)
-    return ROTATION_90;
-  else if (rotation == 180.0)
-    return ROTATION_180;
-  else if (rotation == 270.0)
-    return ROTATION_270;
-  else
-    return ROTATION_0;
-}
-
 void
 ft_render::text_to_pixels (const std::string& txt,
                            uint8NDArray& pixels_, Matrix& box,
@@ -981,38 +923,13 @@
       return;
     }
 
-  switch (halign)
-    {
-    default: box(0) = 0; break;
-    case 1: box(0) = -box(2)/2; break;
-    case 2: box(0) = -box(2); break;
-    }
-  switch (valign)
-    {
-    default: box(1) = 0; break;
-    case 1: box(1) = -box(3)/2; break;
-    case 2: box(1) = -box(3); break;
-    case 3: break;
-    case 4: box(1) = -box(3)-box(1); break;
-    }
+  Matrix bounding_box = box;
+  
+  box = alignment (halign, valign, bounding_box);
 
-  switch (rot_mode)
-    {
-    case ROTATION_90:
-      std::swap (box(0), box(1));
-      std::swap (box(2), box(3));
-      box(0) = -box(0)-box(2);
-      break;
-    case ROTATION_180:
-      box(0) = -box(0)-box(2);
-      box(1) = -box(1)-box(3);
-      break;
-    case ROTATION_270:
-      std::swap (box(0), box(1));
-      std::swap (box(2), box(3));
-      box(1) = -box(1)-box(3);
-      break;
-    }
+  bounding_box = box;
+  
+  box = rotate_bounding_box (rot_mode, bounding_box);
 }
 
 ft_render::ft_font::ft_font (const ft_font& ft)
--- a/libinterp/corefcn/txt-eng-ft.h
+++ b/libinterp/corefcn/txt-eng-ft.h
@@ -46,13 +46,6 @@
       MODE_RENDER = 1
   };
 
-  enum {
-      ROTATION_0   = 0,
-      ROTATION_90  = 1,
-      ROTATION_180 = 2,
-      ROTATION_270 = 3
-  };
-
 public:
   ft_render (const caseless_str& argument);
 
@@ -102,8 +95,6 @@
                        int halign, int valign, double rotation);
 
 private:
-  int rotation_to_mode (double rotation) const;
-
   // No copying!
 
   ft_render (const ft_render&);
--- a/libinterp/corefcn/txt-latex.cc
+++ b/libinterp/corefcn/txt-latex.cc
@@ -347,22 +347,6 @@
     ::warning ("latex_render::set_color: invalid color");
 }
 
-int
-latex_render::rotation_to_mode (double rotation) const
-{
-  if (rotation == 0.0)
-    return ROTATION_0;
-  else if (rotation == 90.0)
-    return ROTATION_90;
-  else if (rotation == 180.0)
-    return ROTATION_180;
-  else if (rotation == 270.0)
-    return ROTATION_270;
-  else
-    return ROTATION_0;
-}
-
-
 void
 latex_render::text_to_pixels (const std::string& txt,
                               uint8NDArray& pixels, Matrix& bbox,
@@ -377,6 +361,8 @@
   {
      pixels = uint8NDArray (dim_vector (4, 10, 10), static_cast<uint8_t> (0));
   }
+  
+  int rot_mode = rotation_to_mode (rotation);
 
   if(pixels.ndims () < 3)
     ::warning ("Pixels variable not properly set.");
@@ -386,92 +372,21 @@
       bbox (2) = pixels.dim2 ();
       bbox (3) = pixels.dim3 ();
       
-      switch (halign)
-      {
-        default: bbox(0) = 0; break;
-        case 1: bbox(0) = -bbox(2)/2; break;
-        case 2: bbox(0) = -bbox(2); break;
-      }
-      switch (valign)
-      {
-        default: bbox(1) = 0; break;
-        case 1: bbox(1) = -bbox(3)/2; break;
-        case 2: bbox(1) = -bbox(3); break;
-        case 3: break;
-        case 4: bbox(1) = -bbox(3)-bbox(1); break;
-      }
+      Matrix bounding_box = bbox;
       
-      int rot_mode = rotation_to_mode (rotation);
+      bbox = alignment (halign, valign, bounding_box);
       
-      switch (rot_mode)
-      {
-      case ROTATION_90:
-        std::swap (bbox(0), bbox(1));
-        std::swap (bbox(2), bbox(3));
-        bbox(0) = -bbox(0)-bbox(2);
-        break;
-      case ROTATION_180:
-        bbox(0) = -bbox(0)-bbox(2);
-        bbox(1) = -bbox(1)-bbox(3);
-        break;
-      case ROTATION_270:
-        std::swap (bbox(0), bbox(1));
-        std::swap (bbox(2), bbox(3));
-        bbox(1) = -bbox(1)-bbox(3);
-        break;
-      }
+      bounding_box = bbox;
+      
+      bbox = rotate_bounding_box (rot_mode, bounding_box);     
     }
     
-    /* Rotating pixels */
-    int rot_mode = rotation_to_mode (rotation);
-    
+    /* Rotating pixels */    
     if (pixels.numel () > 0)
     {
-      switch (rot_mode)
-        {
-        case ROTATION_0:
-          break;
-        case ROTATION_90:
-            {
-              Array<octave_idx_type> perm (dim_vector (3, 1));
-              perm(0) = 0;
-              perm(1) = 2;
-              perm(2) = 1;
-              pixels = pixels.permute (perm);
-
-              Array<idx_vector> idx (dim_vector (3, 1));
-              idx(0) = idx_vector (':');
-              idx(1) = idx_vector (pixels.dim2 ()-1, -1, -1);
-              idx(2) = idx_vector (':');
-              pixels = uint8NDArray (pixels.index (idx));
-            }
-          break;
-        case ROTATION_180:
-            {
-              Array<idx_vector> idx (dim_vector (3, 1));
-              idx(0) = idx_vector (':');
-              idx(1) = idx_vector (pixels.dim2 ()-1, -1, -1);
-              idx(2)=  idx_vector (pixels.dim3 ()-1, -1, -1);
-              pixels = uint8NDArray (pixels.index (idx));
-            }
-          break;
-        case ROTATION_270:
-            {
-              Array<octave_idx_type> perm (dim_vector (3, 1));
-              perm(0) = 0;
-              perm(1) = 2;
-              perm(2) = 1;
-              pixels = pixels.permute (perm);
-
-              Array<idx_vector> idx (dim_vector (3, 1));
-              idx(0) = idx_vector (':');
-              idx(1) = idx_vector (':');
-              idx(2) = idx_vector (pixels.dim3 ()-1, -1, -1);
-              pixels = uint8NDArray (pixels.index (idx));
-            }
-          break;
-        }
-      }
+      uint8NDArray data = pixels;
+      pixels = rotate_data (rot_mode, data);
+    }
 }
 
 Matrix
--- a/libinterp/corefcn/txt-latex.h
+++ b/libinterp/corefcn/txt-latex.h
@@ -33,15 +33,7 @@
 class 
 OCTINTERP_API
 latex_render : public base_text_render
-{
-public:
-    enum {
-      ROTATION_0   = 0,
-      ROTATION_90  = 1,
-      ROTATION_180 = 2,
-      ROTATION_270 = 3
-  };
-  
+{  
 public:
   latex_render (void);
 
@@ -69,7 +61,6 @@
   uint8NDArray render (void);
 
 private:
-  int rotation_to_mode (double rotation) const;
   int font_size;
   std::string font_name,directory_path;
   Matrix bbox;
--- a/libinterp/corefcn/txt-render.cc
+++ b/libinterp/corefcn/txt-render.cc
@@ -83,3 +83,113 @@
     }
   return rep;
 }
+
+int
+base_text_render::rotation_to_mode (double rotation) const
+{
+  if (rotation == 0.0)
+    return ROTATION_0;
+  else if (rotation == 90.0)
+    return ROTATION_90;
+  else if (rotation == 180.0)
+    return ROTATION_180;
+  else if (rotation == 270.0)
+    return ROTATION_270;
+  else
+    return ROTATION_0;
+}
+
+uint8NDArray
+base_text_render::rotate_data (int mode, uint8NDArray data)
+{
+  switch (mode)
+        {
+        case ROTATION_0:
+          break;
+        case ROTATION_90:
+            {
+              Array<octave_idx_type> perm (dim_vector (3, 1));
+              perm(0) = 0;
+              perm(1) = 2;
+              perm(2) = 1;
+              data = data.permute (perm);
+
+              Array<idx_vector> idx (dim_vector (3, 1));
+              idx(0) = idx_vector (':');
+              idx(1) = idx_vector (data.dim2 ()-1, -1, -1);
+              idx(2) = idx_vector (':');
+              data = uint8NDArray (data.index (idx));
+            }
+          break;
+        case ROTATION_180:
+            {
+              Array<idx_vector> idx (dim_vector (3, 1));
+              idx(0) = idx_vector (':');
+              idx(1) = idx_vector (data.dim2 ()-1, -1, -1);
+              idx(2)=  idx_vector (data.dim3 ()-1, -1, -1);
+              data = uint8NDArray (data.index (idx));
+            }
+          break;
+        case ROTATION_270:
+            {
+              Array<octave_idx_type> perm (dim_vector (3, 1));
+              perm(0) = 0;
+              perm(1) = 2;
+              perm(2) = 1;
+              data = data.permute (perm);
+
+              Array<idx_vector> idx (dim_vector (3, 1));
+              idx(0) = idx_vector (':');
+              idx(1) = idx_vector (':');
+              idx(2) = idx_vector (data.dim3 ()-1, -1, -1);
+              data = uint8NDArray (data.index (idx));
+            }
+          break;
+        }
+        return data;
+}
+
+Matrix
+base_text_render::alignment (int halign, int valign, Matrix bounding_box)
+{
+  switch (halign)
+      {
+        default: bounding_box (0) = 0; break;
+        case 1: bounding_box (0) = -bounding_box (2) / 2; break;
+        case 2: bounding_box (0) = -bounding_box (2); break;
+      }
+      switch (valign)
+      {
+        default: bounding_box (1) = 0; break;
+        case 1: bounding_box (1) = -bounding_box (3) / 2; break;
+        case 2: bounding_box (1) = -bounding_box (3); break;
+        case 3: break;
+        case 4: bounding_box (1) = -bounding_box (3) - bounding_box (1); break;
+      }
+      
+      return bounding_box;
+}
+
+Matrix 
+base_text_render::rotate_bounding_box (int rotation, Matrix bounding_box)
+{
+   switch (rotation)
+      {
+      case ROTATION_90:
+        std::swap (bounding_box (0), bounding_box (1));
+        std::swap (bounding_box (2), bounding_box (3));
+        bounding_box (0) = -bounding_box (0) - bounding_box (2);
+        break;
+      case ROTATION_180:
+        bounding_box (0) = -bounding_box (0) - bounding_box (2);
+        bounding_box (1) = -bounding_box (1) - bounding_box (3);
+        break;
+      case ROTATION_270:
+        std::swap (bounding_box (0), bounding_box (1));
+        std::swap (bounding_box (2), bounding_box (3));
+        bounding_box (1) = -bounding_box (1) - bounding_box(3);
+        break;
+      }
+      
+      return bounding_box;
+}
--- a/libinterp/corefcn/txt-render.h
+++ b/libinterp/corefcn/txt-render.h
@@ -37,6 +37,14 @@
 base_text_render
 {
 public:
+   enum {
+      ROTATION_0   = 0,
+      ROTATION_90  = 1,
+      ROTATION_180 = 2,
+      ROTATION_270 = 3
+  };
+  
+public:
   friend class text_render;
 
   base_text_render (void) : count(1) { }
@@ -54,6 +62,11 @@
 
   virtual Matrix get_extent (text_element *elt, double rotation = 0.0) = 0;
   virtual Matrix get_extent (const std::string& txt, double rotation = 0.0) = 0;
+  
+  int rotation_to_mode (double rotation) const;
+  uint8NDArray rotate_data (int mode, uint8NDArray data);
+  Matrix alignment (int halign, int valign, Matrix bounding_box);
+  Matrix rotate_bounding_box (int rotation, Matrix bounding_box);
 
 private: 
   octave_refcount<int> count;