blob: 65da6ae47647b44766450d66538c05b074757a7d [file] [log] [blame]
bungeman@google.come8f05922012-08-16 16:13:40 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Mike Klein8f11d4d2018-01-24 12:42:55 -05009#if defined(SK_BUILD_FOR_WIN)
bungeman@google.come8f05922012-08-16 16:13:40 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPath.h"
12#include "src/utils/SkFloatUtils.h"
13#include "src/utils/win/SkDWriteGeometrySink.h"
Ben Wagner6cb6a072019-08-12 18:30:27 -040014#include "src/utils/win/SkObjBase.h"
bungeman@google.come8f05922012-08-16 16:13:40 +000015
16#include <dwrite.h>
17#include <d2d1.h>
18
19SkDWriteGeometrySink::SkDWriteGeometrySink(SkPath* path) : fRefCount(1), fPath(path) { }
20
21SkDWriteGeometrySink::~SkDWriteGeometrySink() { }
22
Ben Wagner6cb6a072019-08-12 18:30:27 -040023SK_STDMETHODIMP SkDWriteGeometrySink::QueryInterface(REFIID iid, void **object) {
halcanary96fcdcc2015-08-27 07:41:13 -070024 if (nullptr == object) {
bungeman@google.come8f05922012-08-16 16:13:40 +000025 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 {
halcanary96fcdcc2015-08-27 07:41:13 -070032 *object = nullptr;
rmistry@google.comd6176b02012-08-23 18:14:13 +000033 return E_NOINTERFACE;
bungeman@google.come8f05922012-08-16 16:13:40 +000034 }
35}
36
Ben Wagner6cb6a072019-08-12 18:30:27 -040037SK_STDMETHODIMP_(ULONG) SkDWriteGeometrySink::AddRef(void) {
bungeman@google.come8f05922012-08-16 16:13:40 +000038 return static_cast<ULONG>(InterlockedIncrement(&fRefCount));
39}
40
Ben Wagner6cb6a072019-08-12 18:30:27 -040041SK_STDMETHODIMP_(ULONG) SkDWriteGeometrySink::Release(void) {
bungeman@google.come8f05922012-08-16 16:13:40 +000042 ULONG res = static_cast<ULONG>(InterlockedDecrement(&fRefCount));
43 if (0 == res) {
44 delete this;
45 }
46 return res;
47}
48
Ben Wagner6cb6a072019-08-12 18:30:27 -040049SK_STDMETHODIMP_(void) SkDWriteGeometrySink::SetFillMode(D2D1_FILL_MODE fillMode) {
bungeman@google.come8f05922012-08-16 16:13:40 +000050 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:
mtklein@google.com330313a2013-08-22 15:37:26 +000058 SkDEBUGFAIL("Unknown D2D1_FILL_MODE.");
bungeman@google.come8f05922012-08-16 16:13:40 +000059 break;
60 }
61}
62
Ben Wagner6cb6a072019-08-12 18:30:27 -040063SK_STDMETHODIMP_(void) SkDWriteGeometrySink::SetSegmentFlags(D2D1_PATH_SEGMENT vertexFlags) {
bungeman@google.come8f05922012-08-16 16:13:40 +000064 if (vertexFlags == D2D1_PATH_SEGMENT_NONE || vertexFlags == D2D1_PATH_SEGMENT_FORCE_ROUND_LINE_JOIN) {
mtklein@google.com330313a2013-08-22 15:37:26 +000065 SkDEBUGFAIL("Invalid D2D1_PATH_SEGMENT value.");
bungeman@google.come8f05922012-08-16 16:13:40 +000066 }
67}
68
Ben Wagner6cb6a072019-08-12 18:30:27 -040069SK_STDMETHODIMP_(void) SkDWriteGeometrySink::BeginFigure(D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000070 fPath->moveTo(startPoint.x, startPoint.y);
bungeman@google.come8f05922012-08-16 16:13:40 +000071 if (figureBegin == D2D1_FIGURE_BEGIN_HOLLOW) {
mtklein@google.com330313a2013-08-22 15:37:26 +000072 SkDEBUGFAIL("Invalid D2D1_FIGURE_BEGIN value.");
bungeman@google.come8f05922012-08-16 16:13:40 +000073 }
74}
75
Ben Wagner6cb6a072019-08-12 18:30:27 -040076SK_STDMETHODIMP_(void) SkDWriteGeometrySink::AddLines(const D2D1_POINT_2F *points, UINT pointsCount) {
bungeman@google.come8f05922012-08-16 16:13:40 +000077 for (const D2D1_POINT_2F *end = &points[pointsCount]; points < end; ++points) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000078 fPath->lineTo(points->x, points->y);
bungeman@google.come8f05922012-08-16 16:13:40 +000079 }
80}
81
82static bool approximately_equal(float a, float b) {
83 const SkFloatingPoint<float, 10> lhs(a), rhs(b);
84 return lhs.AlmostEquals(rhs);
85}
86
87typedef struct {
88 float x;
89 float y;
90} Cubic[4], Quadratic[3];
91
92static 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
Ben Wagner6cb6a072019-08-12 18:30:27 -0400114SK_STDMETHODIMP_(void) SkDWriteGeometrySink::AddBeziers(const D2D1_BEZIER_SEGMENT *beziers, UINT beziersCount) {
bungeman@google.come8f05922012-08-16 16:13:40 +0000115 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)) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000126 fPath->quadTo(quadratic[1].x, quadratic[1].y,
127 quadratic[2].x, quadratic[2].y);
bungeman@google.come8f05922012-08-16 16:13:40 +0000128 } else {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000129 fPath->cubicTo(beziers->point1.x, beziers->point1.y,
130 beziers->point2.x, beziers->point2.y,
131 beziers->point3.x, beziers->point3.y);
bungeman@google.come8f05922012-08-16 16:13:40 +0000132 }
133 prevPt = beziers->point3;
134 }
135}
136
Ben Wagner6cb6a072019-08-12 18:30:27 -0400137SK_STDMETHODIMP_(void) SkDWriteGeometrySink::EndFigure(D2D1_FIGURE_END figureEnd) {
bungeman@google.come8f05922012-08-16 16:13:40 +0000138 fPath->close();
139}
140
Ben Wagner6cb6a072019-08-12 18:30:27 -0400141SK_STDMETHODIMP SkDWriteGeometrySink::Close() {
bungeman@google.come8f05922012-08-16 16:13:40 +0000142 return S_OK;
143}
144
145HRESULT SkDWriteGeometrySink::Create(SkPath* path, IDWriteGeometrySink** geometryToPath) {
146 *geometryToPath = new SkDWriteGeometrySink(path);
147 return S_OK;
148}
mtklein1ee76512015-11-02 10:20:27 -0800149
Mike Klein8f11d4d2018-01-24 12:42:55 -0500150#endif//defined(SK_BUILD_FOR_WIN)