1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkTypes.h"
9#if defined(SK_BUILD_FOR_WIN32)
10
11#include "SkDWriteGeometrySink.h"
12#include "SkFloatUtils.h"
13#include "SkPath.h"
14
15#include <dwrite.h>
16#include <d2d1.h>
17
18SkDWriteGeometrySink::SkDWriteGeometrySink(SkPath* path) : fRefCount(1), fPath(path) { }
19
20SkDWriteGeometrySink::~SkDWriteGeometrySink() { }
21
22HRESULT STDMETHODCALLTYPE SkDWriteGeometrySink::QueryInterface(REFIID iid, void **object) {
23    if (nullptr == object) {
24        return E_INVALIDARG;
25    }
26    if (iid == __uuidof(IUnknown) || iid == __uuidof(IDWriteGeometrySink)) {
27        *object = static_cast<IDWriteGeometrySink*>(this);
28        this->AddRef();
29        return S_OK;
30    } else {
31        *object = nullptr;
32        return E_NOINTERFACE;
33    }
34}
35
36ULONG STDMETHODCALLTYPE SkDWriteGeometrySink::AddRef(void) {
37    return static_cast<ULONG>(InterlockedIncrement(&fRefCount));
38}
39
40ULONG STDMETHODCALLTYPE SkDWriteGeometrySink::Release(void) {
41    ULONG res = static_cast<ULONG>(InterlockedDecrement(&fRefCount));
42    if (0 == res) {
43        delete this;
44    }
45    return res;
46}
47
48void STDMETHODCALLTYPE SkDWriteGeometrySink::SetFillMode(D2D1_FILL_MODE fillMode) {
49    switch (fillMode) {
50    case D2D1_FILL_MODE_ALTERNATE:
51        fPath->setFillType(SkPath::kEvenOdd_FillType);
52        break;
53    case D2D1_FILL_MODE_WINDING:
54        fPath->setFillType(SkPath::kWinding_FillType);
55        break;
56    default:
57        SkDEBUGFAIL("Unknown D2D1_FILL_MODE.");
58        break;
59    }
60}
61
62void STDMETHODCALLTYPE SkDWriteGeometrySink::SetSegmentFlags(D2D1_PATH_SEGMENT vertexFlags) {
63    if (vertexFlags == D2D1_PATH_SEGMENT_NONE || vertexFlags == D2D1_PATH_SEGMENT_FORCE_ROUND_LINE_JOIN) {
64        SkDEBUGFAIL("Invalid D2D1_PATH_SEGMENT value.");
65    }
66}
67
68void STDMETHODCALLTYPE SkDWriteGeometrySink::BeginFigure(D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin) {
69    fPath->moveTo(startPoint.x, startPoint.y);
70    if (figureBegin == D2D1_FIGURE_BEGIN_HOLLOW) {
71        SkDEBUGFAIL("Invalid D2D1_FIGURE_BEGIN value.");
72    }
73}
74
75void STDMETHODCALLTYPE SkDWriteGeometrySink::AddLines(const D2D1_POINT_2F *points, UINT pointsCount) {
76    for (const D2D1_POINT_2F *end = &points[pointsCount]; points < end; ++points) {
77        fPath->lineTo(points->x, points->y);
78    }
79}
80
81static bool approximately_equal(float a, float b) {
82    const SkFloatingPoint<float, 10> lhs(a), rhs(b);
83    return lhs.AlmostEquals(rhs);
84}
85
86typedef struct {
87    float x;
88    float y;
89} Cubic[4], Quadratic[3];
90
91static bool check_quadratic(const Cubic& cubic, Quadratic& reduction) {
92    float dx10 = cubic[1].x - cubic[0].x;
93    float dx23 = cubic[2].x - cubic[3].x;
94    float midX = cubic[0].x + dx10 * 3 / 2;
95    //NOTE: !approximately_equal(midX - cubic[3].x, dx23 * 3 / 2)
96    //does not work as subnormals get in between the left side and 0.
97    if (!approximately_equal(midX, (dx23 * 3 / 2) + cubic[3].x)) {
98        return false;
99    }
100    float dy10 = cubic[1].y - cubic[0].y;
101    float dy23 = cubic[2].y - cubic[3].y;
102    float midY = cubic[0].y + dy10 * 3 / 2;
103    if (!approximately_equal(midY, (dy23 * 3 / 2) + cubic[3].y)) {
104        return false;
105    }
106    reduction[0] = cubic[0];
107    reduction[1].x = midX;
108    reduction[1].y = midY;
109    reduction[2] = cubic[3];
110    return true;
111}
112
113void STDMETHODCALLTYPE SkDWriteGeometrySink::AddBeziers(const D2D1_BEZIER_SEGMENT *beziers, UINT beziersCount) {
114    SkPoint lastPt;
115    fPath->getLastPt(&lastPt);
116    D2D1_POINT_2F prevPt = { SkScalarToFloat(lastPt.fX), SkScalarToFloat(lastPt.fY) };
117
118    for (const D2D1_BEZIER_SEGMENT *end = &beziers[beziersCount]; beziers < end; ++beziers) {
119        Cubic cubic = { { prevPt.x, prevPt.y },
120                        { beziers->point1.x, beziers->point1.y },
121                        { beziers->point2.x, beziers->point2.y },
122                        { beziers->point3.x, beziers->point3.y }, };
123        Quadratic quadratic;
124        if (check_quadratic(cubic, quadratic)) {
125            fPath->quadTo(quadratic[1].x, quadratic[1].y,
126                          quadratic[2].x, quadratic[2].y);
127        } else {
128            fPath->cubicTo(beziers->point1.x, beziers->point1.y,
129                           beziers->point2.x, beziers->point2.y,
130                           beziers->point3.x, beziers->point3.y);
131        }
132        prevPt = beziers->point3;
133    }
134}
135
136void STDMETHODCALLTYPE SkDWriteGeometrySink::EndFigure(D2D1_FIGURE_END figureEnd) {
137    fPath->close();
138}
139
140HRESULT SkDWriteGeometrySink::Close() {
141    return S_OK;
142}
143
144HRESULT SkDWriteGeometrySink::Create(SkPath* path, IDWriteGeometrySink** geometryToPath) {
145    *geometryToPath = new SkDWriteGeometrySink(path);
146    return S_OK;
147}
148
149#endif//defined(SK_BUILD_FOR_WIN32)
150