OpenType substitution rules: order of application?

Hi,

I’m looking for some insight into how and in which order substitution rules are applied. I read the tutorials on contextual alternates and the opentype cookbook, but can’t wrap my head around this.

When I type “AOOOA” no matter in which order I put these lines, always does only the first “O” get replaced:

# attempt 1
sub A O @default' O A by @alt; # replace second O
sub @default' O O A by @alt; # replace first O
sub A O O @default' A by @alt; # replace third O

# attempt 2
sub @default' O O A by @alt; # replace first O
sub A O @default' O A by @alt; # replace second O
sub A O O @default' A by @alt; # replace third O

# attempt 3
sub A O O @default' A by @alt; # replace third O
sub @default' O O A by @alt; # replace first O
sub A O @default' O A by @alt; # replace second O

Why does the substitution that replaces the first “O” overrule the others?

Also in another case the order confuses me:

# COE
sub @C @O @default' by @alt; 
sub @C @default' @E by @alt; 
sub @C @O @default' by @alt; 

In the string “COE” the code above replaces the “O”. So the second rule seems to “win”.
I would expect the first rule to win, because it would turn the string to "C O E.ss01 " and the rules below wouldn’t even apply. Apparently I misunderstand how this is working :sweat_smile:

Thank you very much in advance!

Cheers
Jens


PS:
The classes used are

@default = [O E];
@alt = [O.ss01 E.ss01];
@C = [C Cacute Ccaron Ccedilla];
@E = [E Eacute Ecaron Ecircumflex Edieresis Edotaccent Egrave Emacron Eogonek];
@O = [O Oacute Ocircumflex Odieresis Ograve Ohungarumlaut Omacron Oslash Otilde ];

The OpenType layout engine iterates over each glyph, checking each lookup and if any lookup matches it applies the first marching rule in the lookup and goes to the next lookup.

You should put each of these rules in a separate lookup, this way the order of the lookups will be respected, though it might still not do what you want since the order of the glyphs in the string make difference. In the string AOOOA, rules matching first A will be applied first, then first O and so on.

2 Likes

You have to take into account that applying one substitution, the context for the others changes.

1 Like