diff src/graphics.cc @ 11455:2be9e22796d2

improvements in text-extent calculation
author Konstantinos Poulios <logari81@googlemail.com>
date Thu, 06 Jan 2011 20:46:03 +0100
parents 5eb10763069f
children fbdb95640852
line wrap: on
line diff
--- a/src/graphics.cc	Thu Jan 06 19:56:50 2011 +0100
+++ b/src/graphics.cc	Thu Jan 06 20:46:03 2011 +0100
@@ -5057,6 +5057,12 @@
   return pos;
 }
 
+Matrix
+text::properties::get_extent_matrix (void) const
+{
+  return extent.get ().matrix_value ();
+}
+
 octave_value
 text::properties::get_extent (void) const
 {
@@ -5070,30 +5076,37 @@
 {
 #ifdef HAVE_FREETYPE
 
-  text_element *elt;
-  ft_render text_renderer;
-  Matrix box;
-
-  // FIXME: parsed content should be cached for efficiency
-  
-  elt = text_parser_none ().parse (get_string ());
+  // FIXME: font and color should be set only when modified, for efficiency
 #ifdef HAVE_FONTCONFIG
-  text_renderer.set_font (*this);
+  renderer.set_font (get ("fontname").string_value (),
+                     get ("fontweight").string_value (),
+                     get ("fontangle").string_value (),
+                     get ("fontsize").double_value ());
 #endif
-  box = text_renderer.get_extent (elt, get_rotation ());
-
-  delete elt;
-
-  Matrix ext (1, 4, 0.0);
-
-  // FIXME: also handle left and bottom components
-
-  ext(0) = ext(1) = 1;
-  ext(2) = box(0);
-  ext(3) = box(1);
-
-  set_extent (ext);
-
+  renderer.set_color (get_color_rgb ());
+
+  int halign = 0, valign = 0;
+
+  if (horizontalalignment_is ("center"))
+    halign = 1;
+  else if (horizontalalignment_is ("right"))
+    halign = 2;
+  
+  if (verticalalignment_is ("top"))
+    valign = 2;
+  else if (verticalalignment_is ("baseline"))
+    valign = 3;
+  else if (verticalalignment_is ("middle"))
+    valign = 1;
+
+  Matrix bbox;
+  // FIXME: string should be parsed only when modified, for efficiency
+  renderer.text_to_pixels (get_string (), pixels, bbox,
+                           halign, valign, get_rotation ());
+
+  set_extent (bbox);
+#else
+  warning ("update_text_extent: cannot render text, Freetype library not available");
 #endif
 }