Typophile

Grouping zeros: OpenType ligature substitutions right to left

Ian Tuomi0 votes

Is it possible to do ligature substitutions right to left in OpenType?

I'm working on a special monospaced typeface with ligatures, you can check it out here.

I'm trying to separate zeros in groups of three with a ligature. Examples: 1000000 -> 1 000 000, and 10000 -> 10 000. The substitution should be done right to left, or otherwise the 10000 becomes 1 000 0, as with the following code:

feature liga {
sub zero zero zero by zero_zero_zero;
}

I'd also appreciate information on how one would use kerning rules in a situation like this, though I'm not sure if they would apply to a monospaced typeface.

You can use contextual substitutions to replace a zero, or a digit, with a variant; for instance a digit followed by three digits but not a fourth can be replaced by a digit with a larger right side bearing. Knowing that lookups are applied on the input line one after the other, that gives the impression that the processing is right to left though it is not. Here is some code that I just checked in FontForge and that groups digits in groups of three.

@dgt = [\zero \one \two \three \four
\five \six \seven \eight \nine] ;
@dgtrsp = [\zero.rsp \one.rsp \two.rsp \three.rsp
\four.rsp \five.rsp \six.rsp
\seven.rsp \eight.rsp \nine.rsp ] ;

lookup addspace1 {
ignore sub @dgt' @dgt @dgt @dgt @dgt ;
sub @dgt' @dgt @dgt @dgt by @dgtrsp ;
} addspace1 ;

lookup addspace2 {
sub @dgt' @dgt @dgt @dgtrsp by @dgtrsp ;
} addspace2 ;

lookup addspace3 {
sub @dgt' @dgt @dgt @dgtrsp by @dgtrsp ;
} addspace3 ;

lookup addspace4 {
sub @dgt' @dgt @dgt @dgtrsp by @dgtrsp ;
} addspace4 ;

feature calt {
script DFLT;
language dflt ;
lookup addspace1 ;
lookup addspace2 ;
lookup addspace3 ;
lookup addspace4 ;
script latn ;
language dflt;
lookup addspace1 ;
lookup addspace2 ;
lookup addspace3 ;
lookup addspace4 ;
} calt ;

Of course, if you wanted to group digits after the decimal point, you would consider left contexts and use digits with a large left side bearing instead.

It was assumed above that the glyph zero.rsp is the glyph zero with a right side bearing increased by some constant value and similarly for other .rsp glyphs. It is also possible to keep all .rsp digits the same width as digits and then to add a kerning equal to that constant value (for you, the character widths) between the .rsp digits and the digits; that has the same visual effect.

Captured from web.archive.org on 1 Jul 2017. view this capture