The Birdfont Source Code


All Repositories / birdfont.git / commitdiff – RSS feed

Thread safety in text rendering

These changes was commited to the Birdfont repository Mon, 09 Mar 2015 11:56:36 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
[Mon, 09 Mar 2015 11:56:36 +0000]

Updated Files

dodo.py
libbirdfont/Font.vala
libbirdfont/GlyphRange.vala
libbirdfont/GlyphSequence.vala
libbirdfont/KerningClasses.vala
libbirdfont/KerningDisplay.vala
libbirdfont/Ligatures.vala
libbirdfont/Renderer/Text.vala
libbirdfont/SpacingData.vala
libbirdfont/SpacingTab.vala
diff --git a/dodo.py b/dodo.py
--- a/dodo.py +++ b/dodo.py @@ -88,6 +88,7 @@ valac_options = [ + '--thread', '--enable-experimental-non-null', '--enable-experimental', '--target-glib=2.34', # see bug 0000004
--- a/libbirdfont/Font.vala +++ b/libbirdfont/Font.vala @@ -132,7 +132,7 @@ bfp_file = new BirdFontPart (this); deleted_glyphs = new Gee.ArrayList<Glyph> (); - ligatures_substitution = new Ligatures (); + ligatures_substitution = new Ligatures (this); background_images = new Gee.ArrayList<BackgroundImage> (); }
--- a/libbirdfont/GlyphRange.vala +++ b/libbirdfont/GlyphRange.vala @@ -399,7 +399,7 @@ string chr; UniRange r; StringBuilder sb; - unichar c; + unichar c; if (index > len + unassigned.size) { return "\0".dup(); @@ -428,8 +428,14 @@ sb = new StringBuilder (); c = r.get_char ((unichar) (ti + r.length ())); + + if (unlikely (!c.validate ())) { + warning ("Not a valid unicode character."); + return ""; + } + sb.append_unichar (c); - + return sb.str; }
--- a/libbirdfont/GlyphSequence.vala +++ b/libbirdfont/GlyphSequence.vala @@ -13,7 +13,7 @@ */ namespace BirdFont { - public class GlyphSequence { + public class GlyphSequence : GLib.Object { /** A list of all glyphs */ public Gee.ArrayList<Glyph?> glyph; @@ -33,10 +33,9 @@ /** Do ligature substitution. * @return a new sequence with ligatures */ - public GlyphSequence process_ligatures () { + public GlyphSequence process_ligatures (Font font) { // FIXME add range to ligature GlyphSequence ligature_sequence = new GlyphSequence (); - Font font = BirdFont.get_current_font (); bool has_range = false; Ligatures ligatures;
--- a/libbirdfont/KerningClasses.vala +++ b/libbirdfont/KerningClasses.vala @@ -37,7 +37,7 @@ /** Ensure that map iterator is not invalidated because of inserts. */ bool protect_map = false; - Font font; + public Font font; public KerningClasses (Font font) { this.font = font;
--- a/libbirdfont/KerningDisplay.vala +++ b/libbirdfont/KerningDisplay.vala @@ -122,7 +122,7 @@ foreach (GlyphSequence word in row) { wi = 0; - word_with_ligatures = word.process_ligatures (); + word_with_ligatures = word.process_ligatures (font); gr_left = null; gr_right = null; foreach (Glyph? g in word_with_ligatures.glyph) { @@ -311,7 +311,7 @@ } foreach (GlyphSequence word in row) { - word_with_ligatures = word.process_ligatures (); + word_with_ligatures = word.process_ligatures (font); ranges_index = 0; foreach (Glyph? g in word_with_ligatures.glyph) { @@ -503,8 +503,9 @@ Glyph? g; selected_handle = handle; GlyphSequence sequence_with_ligatures; + Font font = BirdFont.get_current_font (); - sequence_with_ligatures = row.get (0).process_ligatures (); + sequence_with_ligatures = row.get (0).process_ligatures (font); if (selected_handle <= 0) { selected_handle = 1; @@ -724,6 +725,7 @@ int col_index = 0; double fs = KerningTools.font_size; Glyph glyph = new Glyph.no_lines (""); + Font font = BirdFont.get_current_font (); GlyphRange? gr_left, gr_right; @@ -734,7 +736,7 @@ foreach (GlyphSequence word in row) { col_index = 0; - word_with_ligatures = word.process_ligatures (); + word_with_ligatures = word.process_ligatures (font); foreach (Glyph? g in word_with_ligatures.glyph) { if (g == null) { w = 50;
--- a/libbirdfont/Ligatures.vala +++ b/libbirdfont/Ligatures.vala @@ -27,7 +27,10 @@ public delegate void ContextualLigatureIterator (ContextualLigature lig); - public Ligatures () { + Font font; + + public Ligatures (Font font) { + this.font = font; } public void get_ligatures (LigatureIterator iter) { @@ -44,7 +47,6 @@ public void get_single_substitution_ligatures (SingleLigatureIterator iter) { get_ligatures ((substitution, ligature) => { - Font font = BirdFont.get_current_font (); GlyphCollection? gc; GlyphCollection li; GlyphSequence gs;
--- a/libbirdfont/Renderer/Text.vala +++ b/libbirdfont/Renderer/Text.vala @@ -130,6 +130,12 @@ GlyphSequence word; Glyph? g; KerningClasses kc; + + // FIXME: Create a thread safe implementation of the bf text rendering + // The problem is (probably) in the spacing and kerning code. + if (MenuTab.suppress_event) { + return; + } glyph = new Glyph ("", '\0'); @@ -140,11 +146,12 @@ word = glyph_sequence; wi = 0; - word_with_ligatures = word.process_ligatures (); + return_if_fail (current_font != null); + word_with_ligatures = word.process_ligatures ((!) current_font); gr_left = null; gr_right = null; - kc = font.get_kerning_classes (); + kc = ((!) current_font).get_kerning_classes (); for (int i = 0; i < word_with_ligatures.glyph.size; i++) { g = word_with_ligatures.glyph.get (i); @@ -324,7 +331,9 @@ double x, y; double ratio; double cc_y; - int64 cache_id = (cacheid < 0) ? get_cache_id () : cacheid; + int64 cache_id; + + cache_id = (cacheid < 0) ? get_cache_id () : cacheid; ratio = get_scale (); cc_y = (font.top_limit - font.base_line) * ratio;
--- a/libbirdfont/SpacingData.vala +++ b/libbirdfont/SpacingData.vala @@ -21,7 +21,7 @@ public KerningClasses kerning_classes; public Gee.ArrayList<SpacingClass> classes = new Gee.ArrayList<SpacingClass> (); - Gee.ArrayList<string> connections = new Gee.ArrayList<string> (); + Gee.ArrayList<String> connections = new Gee.ArrayList<String> (); public SpacingData (KerningClasses kerning) { kerning_classes = kerning; @@ -32,32 +32,46 @@ } public Gee.ArrayList<string> get_all_connections (string glyph) { + String s; Gee.ArrayList<string> c = new Gee.ArrayList<string> (); connections.clear (); + add_connections (glyph); - foreach (string t in connections) { - c.add (t); + foreach (String t in connections) { + c.add (t.data.dup ()); } connections.clear (); return c; + } + + bool has_connection (string s) { + foreach (String t in connections) { + if (t.data == s) { + return true; + } + } + + return false; } public void add_connections (string glyph) { - connections.add (glyph); + String sb = new String (glyph); + + connections.add (sb); foreach (SpacingClass s in classes) { if (s.first == glyph) { - if (connections.index_of (s.next) == -1) { + if (has_connection (s.next)) { add_connections (s.next); } } if (s.next == glyph) { - if (connections.index_of (s.first) == -1) { + if (has_connection (s.first)) { add_connections (s.first); } } @@ -81,7 +95,7 @@ } public void update_kerning (SpacingClass s) { - Font font = BirdFont.get_current_font (); + Font font = kerning_classes.font; GlyphCollection? g; GlyphCollection gc; @@ -104,8 +118,17 @@ } KerningTools.update_spacing_classes (); + } + + class String : GLib.Object { + + public string data; + + public String (string s) { + data = s.dup (); + } } } }
--- a/libbirdfont/SpacingTab.vala +++ b/libbirdfont/SpacingTab.vala @@ -47,6 +47,7 @@ void draw_spacing_metrix (WidgetAllocation allocation, Context cr) { GlyphSequence row; int index; + Font font = BirdFont.get_current_font (); // background cr.save (); @@ -69,7 +70,7 @@ cr.restore (); // TODO: add button for processing ligatures - row = get_first_row ().process_ligatures (); + row = get_first_row ().process_ligatures (font); index = 0; foreach (Glyph? g in row.glyph) { draw_glyph_spacing (allocation, cr, g, index); @@ -163,10 +164,11 @@ public override void button_press (uint button, double ex, double ey) { GlyphSequence row; double p; + Font font = BirdFont.get_current_font (); if (ey >= allocation.height - height) { // TODO: add button for processing ligatures - row = get_first_row ().process_ligatures (); + row = get_first_row ().process_ligatures (font); p = 0; foreach (Glyph? g in row.glyph) { if (p < ex < p + box_size / 2.0) {