nowar-fonts / otfcc-quad2cubic

A port of AFDKO’s quadratic to cubic conversion algorithm.
3 stars 0 forks source link

C# version #1

Open db300 opened 4 years ago

db300 commented 4 years ago

Hi, how are you. I need some help. Firstly, I'm very happy to find this library. But, I need rewrite this library using C#. So, would you please explain the convertion arithmetic. Thanks a lot.

CyanoHao commented 4 years ago

I’m trying to explain it in top-down approach.

Quad2Cubic (defined in otfcc-q2c.cpp) handles metadata of the font. It writes CFF font dict (CFF_ in JSON-serialized OpenType font file, aka “otd file”), and calls Tt2Ps to convert glyphs.

Tt2Ps (defined in tt2ps.cpp) converts TT-outlined glyphs to PS-outlined glyphs. For each in glyf in a otd file, it calls ConvertApprox to do the conversion.

In ConvertApprox, for each contour, the algorithm handles 2 quadratic segments at a time. It calls CombinePair to determine whether the 2 quadratic segments can be combined to a single cubic curve. If yes, append the combined cubic curve to cubic contour (by ConstructCffPath::Curve), or convert the first segment to cubic one (by SimpleCurve) and append it to cubic contour, the second segment to be proceeded next time.

The 2 quadratic segments that are handled at a time is stored in Point p[6]. image

TT points are read, proceeded, and accumulated in p. (Note that an on-curve point can be emitted if it is mid-point of 2 off-curve points.) This is implemented using a state machine as follows:

   state    sequence        points
            0=off,1=on      accumulated
   0        1               0
   1        1 0             0-1
   2        1 0 0           0-3 (p[2] is mid-point of p[1] and p[3])
   3        1 0 1           0-2
   4        1 0 1 0         0-3

(see also https://github.com/adobe-type-tools/afdko/blob/464008517cbd92ce211ac51182c9941b95303ce8/c/public/lib/source/ttread/ttread.c#L3107)