views:

545

answers:

3

Hi,

I am doing some TTF work for MOSA (the correlating body between all the C# operating systems). Me and Colin Burn are currently working on getting some TTF code working (less me these days :) - he made a lot of progress).

In any case, the TTF spec allows for an arbitrary amount of control points between the 'handles' and gasp NO handles at all (the TTF has an example of a circle demonstrating it - well done idiots - you saved 10 bytes).

Can anyone give me a pointer on how this could be done? I looked at the Bezier article on Wikipedia but it wasn't really that much help - they show it happening, but don't give any math. Something 'program' ready would help (my Calculus isn't what it should be) - some pseudocode or something.

Thanks guys.

+3  A: 

From the Bezier article in wikipedia, with some practical calculus knowledge, you can translate the formulas to a computer program like the following pseudo C# code listing. I'm doing it with quadratic spline, but it is easy to translate to another.

// Quadratic spline, with three given points
// B(t) = (1-t)^2P(0) + 2*tP(1) + t^2P(2)
// where T is a real number in the interval [0, 1]

public void DrawQuadSpline(Point p0, Point p1, Point p2, int steps) 
{
    Point next = p0;
    Point previous = p0;
    double tStep = 1 / ((float) steps);
    double t = 0;
    for (int i = 0; i < steps; i++) 
    {
        float x = CalculateQuadSpline(P0.x, P1.x, P2.x, t);
        float y = CalculateQuadSpline(P0.y, P1.y, P2.y, t);
        Point next = new Point(x, y);
        drawLine(previous, next);
        previous = next;
        t = t + tStep;
    }
} 

private void CalculateQuadSpline(float z0, float z1, float z2, float t) 
{
    return (1.0-t)*(1.0-t)*z0 + 2.0*t*z1 + t*t*z2;
}

It might need some tweaking as I've only did this in Java before, but that's basically it.

Spoike
Thanks for the answer, but that isn't really what I was looking for. The problem is that we have what you gave me - but some fonts have more than two control points (in your case, one point). We need to turn this higher orders into lower orders (like yours)...
Jonathan C Dickinson
I should vote you down, but I won't because that is a genuinely useful answer.
Jonathan C Dickinson
If its a genuinely useful answer, you should vote him UP.
paxdiablo
@PAX But it isn't the right one :).
Jonathan C Dickinson
"This was helpful"; let me fix that for you, burnig a vote for +1.
paxdiablo
@Jonathan C Dickinson: I haven't read the True Type Font spec (I guess TTF stands for that), but I guess it should have some formulas listed in it to turn points to higher order. Just translate it into code in similar fashion.
Spoike
+1  A: 

I did some digging and found some algorithms for the TTF spec over at this site over here.

Spoike
A: 

Okay, it looks like TTF outlines are defined as quadratic b-splines.

There are two algorithms that you'll want to be familiar with.

The first is Bezier extraction via knot insertion. This will get you quadratic Bezier segments. Then you'll want to degree-elevate each Bezier segment to get cubics.

The main reference I use is my CAGD class textbook, which is online. Bezier extraction is covered in section 6.3. Degree elevation of Bezier curves is covered in section 2.4. Let me know if you have any problems..

tfinniga
Basically, a glyph is defined as a series of points. These points can lie on (handle) or off (control) the curve. There is no limit to how many off points can go between the on points. You don't even need on points. I need the glyph in a Quadratic, or if must, a Cubic bezier curves.
Jonathan C Dickinson
I can't find the doc again, it has been a long time since I actually worked on the project.
Jonathan C Dickinson
Sorry, and "I need the glyph in a Quadratic, or if must, a Cubic bezier curves." should have been "I need the glyph in many Quadratic, or if must, many Cubic bezier curves."
Jonathan C Dickinson