Cache Pango layouts
[gregoa/zavai.git] / src / fisheye.vala
index 62251208442cbc9962a7864fb84c41b4d6bed737..87ae4b069d30f28be89a5fc7df85f7fb0341d8a5 100644 (file)
@@ -28,7 +28,7 @@ public class FisheyeList : Gtk.DrawingArea
        protected int cur_el;
        protected double distortion_factor;
 
-       protected Pango.FontDescription[] fontdesc_cache;
+       protected Pango.Layout[] pango_cache;
 
        // Number of items shown before and after the focus element
        protected int focus_first;
@@ -46,9 +46,9 @@ public class FisheyeList : Gtk.DrawingArea
                for (int i = 0; i < 300; ++i)
                        list[i] = "Lorem Ipsum %d".printf(i);
 
-               fontdesc_cache = new Pango.FontDescription[30];
+               pango_cache = new Pango.Layout[30];
                for (int i = 0; i < 30; ++i)
-                       fontdesc_cache[i] = null;
+                       pango_cache[i] = null;
 
                cur_el = 0;
                focus_centre = 0;
@@ -203,18 +203,21 @@ public class FisheyeList : Gtk.DrawingArea
                                        0, y0, allocation.width, y1-y0);
                        }
 
-                       var layout = create_pango_layout(list[idx]);
                
-                       
+                       // TODO: cache pango contexts instead of fontdescs
                        int size = (y1-y0)*80/100;
                        if (size <= 0) size = 1;
-                       if (size >= fontdesc_cache.length) size = fontdesc_cache.length - 1;
-                       if (fontdesc_cache[size] == null)
+                       if (size >= pango_cache.length) size = pango_cache.length - 1;
+                       if (pango_cache[size] == null)
                        {
-                               fontdesc_cache[size] = style.font_desc.copy();
-                               fontdesc_cache[size].set_absolute_size(size*Pango.SCALE);
+                               var fd = style.font_desc.copy();
+                               fd.set_absolute_size(size*Pango.SCALE);
+                               var pc = create_pango_context();
+                               pc.set_font_description(fd);
+                               pango_cache[size] = new Pango.Layout(pc);
                        }
-                       layout.set_font_description(fontdesc_cache[size]);
+                       pango_cache[size].set_text(list[idx], -1);
+                       var layout = pango_cache[size];
                        //stderr.printf("AZAZA %p\n", layout.get_attributes());
                        //var attrlist = layout.get_attributes().copy();
                        //stderr.printf("AL %p\n", attrlist);