googlefonts / ufo2ft

A bridge from UFOs to FontTools objects (and therefore, OTFs and TTFs).
MIT License
151 stars 43 forks source link

KernWriter adding DFLT/dflt without languagesystem #578

Open simoncozens opened 2 years ago

simoncozens commented 2 years ago

Suppose we have a complex script where the kerning rules go in a dist feature (let's say Sinhala, since that's what I'm working on). ufo2ft adds the lookup to the dist feature of the script and also to the kern feature of the DFLT script:

feature kern {
    script DFLT;
    language dflt;
    lookup kern_ltr;
} kern;

feature dist {
    script sinh;
    language dflt;
    lookup kern_ltr;
} dist;

So far so good. However, if I am using manual lookups with # Automatic Code Start, and I have set a languagesystem sinh dflt languagesystem (because it's a Sinhala font) and I haven't added languagesystem DFLT dflt; (because I don't have any DFLT rules), then very strange things happen. We get a feature file like this:

languagesystem sinh dflt;
feature dist {
    pos aisinh uni0D81' <0 340 0 0>;
} dist;

lookup kern_ltr {
    lookupflag IgnoreMarks;
   ...
} kern_ltr;

feature kern {
    script DFLT;
    language dflt;
    lookup kern_ltr;
} kern;

feature dist {
    script sinh;
    language dflt;
    lookup kern_ltr;
} dist;

The feature compiler then adds the kern_ltr lookup to both kern and dist in the DFLT script (but not the contextual positioning), and only adds the contextual positioning (no kerning lookups) to sinh script. This is very much related to https://github.com/fonttools/fonttools/issues/2183, and reflects undefined behavior in the AFDKO spec.

The fix is, if ufo2ft is going to add random DFLT script rules to my feature file, it needs to add a languagesystem DFLT dflt statement if there isn't one already. (Or better, just don't add the random DFLT rules if there isn't a DFLT languagesystem.)