Example #1
0
void Glyph::CreateSplineCoeffs(const CRect& spdrc)
{
    spline.RemoveAll();

    if (style.placement.path.IsEmpty()) {
        return;
    }

    size_t i = 0, j = style.placement.path.GetCount();

    CAtlArray<Point> pts;
    pts.SetCount(j + 2);

    Point p;

    while (i < j) {
        p.x = style.placement.path[i].x * scale.cx + spdrc.left * 64;
        p.y = style.placement.path[i].y * scale.cy + spdrc.top * 64;
        pts[++i] = p;
    }

    if (pts.GetCount() >= 4) {
        if (pts[1].x == pts[j].x && pts[1].y == pts[j].y) {
            pts.SetAt(0, pts[j - 1]);
            pts.SetAt(j + 1, pts[2]);
        } else {
            p.x = pts[1].x * 2 - pts[2].x;
            p.y = pts[1].y * 2 - pts[2].y;
            pts.SetAt(0, p);

            p.x = pts[j].x * 2 - pts[j - 1].x;
            p.y = pts[j].y * 2 - pts[j - 1].y;
            pts.SetAt(j + 1, p);
        }

        spline.SetCount(pts.GetCount() - 3);

        for (size_t i = 0, j = pts.GetCount() - 4; i <= j; i++) {
            static const float _1div6 = 1.0f / 6;

            SplineCoeffs sc;

            sc.cx[3] = _1div6 * (-  pts[i + 0].x + 3 * pts[i + 1].x - 3 * pts[i + 2].x + pts[i + 3].x);
            sc.cx[2] = _1div6 * (3 * pts[i + 0].x - 6 * pts[i + 1].x + 3 * pts[i + 2].x);
            sc.cx[1] = _1div6 * (-3 * pts[i + 0].x                + 3 * pts[i + 2].x);
            sc.cx[0] = _1div6 * (pts[i + 0].x + 4 * pts[i + 1].x + 1 * pts[i + 2].x);

            sc.cy[3] = _1div6 * (-  pts[i + 0].y + 3 * pts[i + 1].y - 3 * pts[i + 2].y + pts[i + 3].y);
            sc.cy[2] = _1div6 * (3 * pts[i + 0].y - 6 * pts[i + 1].y + 3 * pts[i + 2].y);
            sc.cy[1] = _1div6 * (-3 * pts[i + 0].y                + 3 * pts[i + 2].y);
            sc.cy[0] = _1div6 * (pts[i + 0].y + 4 * pts[i + 1].y + 1 * pts[i + 2].y);

            spline.SetAt(i, sc);
        }
    }
}