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