The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Fix contextual substitution

These changes was commited to the Birdfont repository Sun, 13 Aug 2017 21:00:03 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
author Johan Mattsson <johan.mattsson.m@gmail.com>
Sun, 13 Aug 2017 21:00:03 +0000 (23:00 +0200)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Sun, 13 Aug 2017 21:00:03 +0000 (23:00 +0200)
commit 8337fa3f16da8147a4dc39a91baeca31e4999d3b
tree f38fdc89f2366be63ab2673cf74ff546706bb3a3
parent 8dcd2b93ac6ef5895ae1b63385f528d1a5dbbee8
Fix contextual substitution

libbirdfont/GlyphSequence.vala
--- a/libbirdfont/GlyphSequence.vala +++ b/libbirdfont/GlyphSequence.vala @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 2015 Johan Mattsson + Copyright (C) 2013 2015 2017 Johan Mattsson This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -80,15 +80,24 @@ ligatures = font.get_ligatures (); - foreach (ContextualLigature c in ligatures.contextual_ligatures) { - if (c.is_valid ()) { - ligature_sequence.replace_contextual (c.get_backtrack (), - c.get_input (), - c.get_lookahead (), - c.get_ligature_sequence ()); + for (int index = 0; index < glyph.size; index++) { + bool found = false; + foreach (ContextualLigature c in ligatures.contextual_ligatures) { + if (c.is_valid ()) { + found = ligature_sequence.replace_contextual (c.get_backtrack (), + c.get_input (), + c.get_lookahead (), + c.get_ligature_sequence (), + ref index); + + if (found) { + index--; + break; + } + } } } - + ligatures.get_single_substitution_ligatures ((substitute, ligature) => { ligature_sequence.replace (substitute, ligature); }); @@ -144,36 +153,30 @@ } } - void replace_contextual (GlyphSequence backtrack, GlyphSequence input, GlyphSequence lookahead, - GlyphSequence replacement) { + bool replace_contextual (GlyphSequence backtrack, GlyphSequence input, GlyphSequence lookahead, + GlyphSequence replacement, ref int index) { bool start, middle, end; - int i = 0; int advance = 0; - while (i < glyph.size) { - start = starts_with (backtrack, i); - middle = starts_with (input, i + backtrack.length ()); - end = starts_with (lookahead, i + backtrack.length () + input.length ()); - - if (start && middle && end) { - glyph = substitute (i + backtrack.length (), input.length (), replacement); - - advance = backtrack.length () + replacement.length (); - i += advance; + start = starts_with (backtrack, index); + middle = starts_with (input, index + backtrack.length ()); + end = starts_with (lookahead, index + backtrack.length () + input.length ()); - if (replacement.length () == 0) { - i++; - } + if (start && middle && end) { + glyph = substitute (index + backtrack.length (), input.length (), replacement); + advance = backtrack.length () + replacement.length (); + index += advance; - if (advance <= 0) { - warning ("No advancement."); - return; - } - } else { - i++; + if (advance <= 0) { + warning ("No advancement."); + return false; } + + return true; } + + return false; } bool starts_with (GlyphSequence old, uint index) { @@ -215,8 +218,21 @@ } return new_list; + } + + public string to_string () { + StringBuilder s = new StringBuilder(); + foreach (Glyph g in glyph) { + if (g.unichar_code == '\0') { + s.append(@"[$(g.name)]"); + } else { + s.append(@"$((!) g.unichar_code)"); + } + } + + return s.str; } } }