Commit c667aba
Faster text and icon rendering This commit makes switching tools many times faster.
Changed files (8)
modifiedlibbirdfont/CharacterInfo.valamodifiedlibbirdfont/DrawingTools.valamodifiedlibbirdfont/Expander.valamodifiedlibbirdfont/Path.valamodifiedlibbirdfont/Renderer/Text.valamodifiedlibbirdfont/Renderer/TextArea.valamodifiedlibbirdfont/TabBar.valamodifiedlibbirdfont/Tool.vala
Diff
@@ -36,7 +36,6 @@ public class CharacterInfo : GLib.Object { ligature = ((!) gc).is_unassigned (); name = ((!) gc).get_name (); icon.load_font ("icons.bf");- icon.use_cache (true); } } @@ -108,7 +108,11 @@ public class DrawingTools : ToolCollection { static Tool line_cap_butt; static Tool line_cap_round; static Tool line_cap_square;- ++ private Text backgrounds_headline = new Text (t_("Background Tools"));+ private Text control_points_headline = new Text (t_("Control Points"));+ private Text object_tools_headline = new Text (t_("Object Tools"));+ public DrawingTools (GlyphCanvas main_glyph_canvas) { bool selected_line; @@ -1324,7 +1328,7 @@ public class DrawingTools : ToolCollection { } void show_background_tool_modifiers () {- draw_tool_modifiers.set_headline (t_("Background Tools"));+ draw_tool_modifiers.set_headline (backgrounds_headline); cut_background.set_tool_visibility (true); show_bg.set_tool_visibility (true);@@ -1339,7 +1343,7 @@ public class DrawingTools : ToolCollection { } void show_point_tool_modifiers () {- draw_tool_modifiers.set_headline (t_("Control Points"));+ draw_tool_modifiers.set_headline (control_points_headline); tie_handles.set_tool_visibility (true); reflect_handle.set_tool_visibility (true);@@ -1355,7 +1359,7 @@ public class DrawingTools : ToolCollection { } void show_object_tool_modifiers () {- draw_tool_modifiers.set_headline (t_("Object Tools"));+ draw_tool_modifiers.set_headline (object_tools_headline); x_coordinate.set_tool_visibility (true); y_coordinate.set_tool_visibility (true);@@ -53,7 +53,7 @@ public class Expander : GLib.Object { title = new Text (); if (headline != null) {- set_headline(headline);+ title.set_text ((!) headline); } tool = new Gee.ArrayList<Tool> ();@@ -63,13 +63,13 @@ public class Expander : GLib.Object { cached = null; } - public void set_headline (string? h) {- headline = h;- title.set_text ((!) headline);+ public void set_headline (Text h) {+ headline = h.get_text ();+ title = h; } public double get_content_height () {- return content_height;+ return content_height; } /** Returns true if tools can be used with the current canvas after@@ -217,7 +217,6 @@ public class Expander : GLib.Object { } public void redraw () {- cached = null; Toolbox.redraw_tool_box (); } @@ -290,11 +289,12 @@ public class Expander : GLib.Object { } draw_content (cc, offset_y);- cached = (!) cache;+ cached = (!) cache; } if (cached != null) { cache = (!) cached;+ cr.set_antialias (Cairo.Antialias.NONE); Screen.paint_background_surface (cr, cache, 0, (int) (y + scroll)); } }@@ -320,7 +320,7 @@ public class Path : GLib.Object { double max = Glyph.CANVAS_MIN; Text arrow; double x, y, angle;- double size = 50 * Glyph.ivz ();+ double size = 50 * Screen.get_scale (); foreach (EditPoint e in points) { if (e.y > max) {@@ -331,7 +331,6 @@ public class Path : GLib.Object { arrow = new Text ("orientation_arrow", size); arrow.load_font ("icons.bf");- arrow.use_cache (false); Theme.text_color_opacity (arrow, "Highlighted 1", opacity); @@ -342,6 +341,8 @@ public class Path : GLib.Object { if (points.size > 0) { cr.save (); cr.translate (x, y);+ double inverted_zoom = Glyph.ivz ();+ cr.scale (inverted_zoom, inverted_zoom); cr.rotate (-angle); cr.translate (-x, -y); @@ -20,7 +20,9 @@ namespace BirdFont { public class Text : Widget { FontCache font_cache; public CachedFont cached_font;-+ + Surface? cache = null;+ public string text; GlyphSequence glyph_sequence {@@ -44,8 +46,6 @@ public class Text : Widget { public double g = 0; public double b = 0; public double a = 1;- - bool use_cached_glyphs = true; double truncated_width = -1; public Text (string text = "", double size = 17, double margin_bottom = 0) {@@ -57,8 +57,8 @@ public class Text : Widget { set_font_size (size); } - public void use_cache (bool cache) {- use_cached_glyphs = cache;+ public string get_text () {+ return text; } /** Set font for this text area.@@ -332,49 +332,61 @@ public class Text : Widget { double ratio; double cc_y; + if (cache == null) {+ cache = draw_on_cache_surface (cacheid);+ }+ + double s = get_font_scale ();+ double cache_y = py - s * (cached_font.top_limit - cached_font.base_line);+ cr.set_source_surface ((!) cache, (int) px, (int) cache_y);+ cr.paint ();+ }+ + Surface draw_on_cache_surface (string cacheid) {+ double x, y;+ double ratio;+ double cc_y;+ Context cr;+ Surface cache_surface;+ double screen_scale = Screen.get_scale();+ double h = font_size * screen_scale + 1;+ double w = get_sidebearing_extent () * screen_scale + 1;+ + cache_surface = Screen.create_background_surface ((int) w, (int) h);+ cr = new Context (cache_surface);+ cr.scale(screen_scale, screen_scale);+ ratio = get_font_scale (); cc_y = (cached_font.top_limit - cached_font.base_line) * ratio; - y = py;- x = px;+ double px = 0;+ double py = cc_y;+ + y = cc_y;+ x = 0; if (unlikely (cached_font.base_line != 0)) { warning ("Base line not zero."); } - if (use_cached_glyphs) {- iterate ((glyph, kerning, last) => {- double end;- - x += kerning * ratio;- end = x + glyph.get_width () * ratio;- - // truncation- if (truncated_width > 0 && end - px > truncated_width) {- return;- }+ iterate ((glyph, kerning, last) => {+ double end;+ + x += kerning * ratio;+ end = x + glyph.get_width () * ratio;+ + // truncation+ if (truncated_width > 0 && end - px > truncated_width) {+ return;+ } - draw_chached (cr, glyph, kerning, last, x, y, cc_y, - ratio, cacheid);- - x = end;- });- } else {- iterate ((glyph, kerning, last) => {- double end;- - x += kerning * ratio;- end = x + glyph.get_width () * ratio;- - // truncation- if (truncated_width > 0 && end - px > truncated_width) {- return;- }- - draw_without_cache (cr, glyph, kerning, last, x, y, cc_y, ratio);- x = end;- });- }+ draw_chached (cr, glyph, kerning, last, x, y, cc_y, + ratio, cacheid);+ + x = end;+ });+ + return cache_surface; } void draw_without_cache (Context cr, Glyph glyph, double kerning, bool last, @@ -64,7 +64,6 @@ public class TextArea : Widget { bool store_undo_state_at_next_event = false; public bool editable;- public bool use_cache = true; public TextArea (double font_size = 20, Color? c = null) { this.font_size = font_size;@@ -1068,7 +1067,6 @@ public class TextArea : Widget { cr.save (); word = new Text ();- word.use_cache (use_cache); width = this.width - padding; x += padding;@@ -682,8 +682,6 @@ public class TabBar : GLib.Object { wheel.widget_x = middlex; wheel.widget_y = middley; - wheel.use_cache (false);- cr.save (); if (!has_stop_button ()) { cr.translate (middlex, middley);@@ -801,7 +799,6 @@ public class TabBar : GLib.Object { // tab label label = new Text ();- label.use_cache (false); label.set_text (t.get_label ()); text_height = (int) (16 / scale); label.set_font_size (text_height);@@ -240,7 +240,6 @@ public class Tool : Widget { icon_file = Theme.get_icon_file (); icon_font = new Text ((!) name); found = icon_font.load_font (icon_file);- icon_font.use_cache (true); icon_font.set_font_size (40 * Toolbox.get_scale ()); if (!found) {