blob: 9f6a97caf14b5b6c863c4638df0ca73341191764 [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
8#include "SkTypes.h"
mtklein1ee76512015-11-02 10:20:27 -08009#if defined(SK_BUILD_FOR_WIN32)
bungeman@google.come8f05922012-08-16 16:13:40 +000010
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) {
halcanary96fcdcc2015-08-27 07:41:13 -070023 if (nullptr == object) {
bungeman@google.come8f05922012-08-16 16:13:40 +000024 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 {
halcanary96fcdcc2015-08-27 07:41:13 -070031 *object = nullptr;
rmistry@google.comd6176b02012-08-23 18:14:13 +000032 return E_NOINTERFACE;
bungeman@google.come8f05922012-08-16 16:13:40 +000033 }
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:
mtklein@google.com330313a2013-08-22 15:37:26 +000057 SkDEBUGFAIL("Unknown D2D1_FILL_MODE.");
bungeman@google.come8f05922012-08-16 16:13:40 +000058 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) {
mtklein@google.com330313a2013-08-22 15:37:26 +000064 SkDEBUGFAIL("Invalid D2D1_PATH_SEGMENT value.");
bungeman@google.come8f05922012-08-16 16:13:40 +000065 }
66}
67
68void STDMETHODCALLTYPE SkDWriteGeometrySink::BeginFigure(D2D1_POINT_2F startPoint, D2D1_FIGURE_BEGIN figureBegin) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000069 fPath->moveTo(startPoint.x, startPoint.y);
bungeman@google.come8f05922012-08-16 16:13:40 +000070 if (figureBegin == D2D1_FIGURE_BEGIN_HOLLOW) {
mtklein@google.com330313a2013-08-22 15:37:26 +000071 SkDEBUGFAIL("Invalid D2D1_FIGURE_BEGIN value.");
bungeman@google.come8f05922012-08-16 16:13:40 +000072 }
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) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000077 fPath->lineTo(points->x, points->y);
bungeman@google.come8f05922012-08-16 16:13:40 +000078 }
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)) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000125 fPath->quadTo(quadratic[1].x, quadratic[1].y,
126 quadratic[2].x, quadratic[2].y);
bungeman@google.come8f05922012-08-16 16:13:40 +0000127 } else {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000128 fPath->cubicTo(beziers->point1.x, beziers->point1.y,
129 beziers->point2.x, beziers->point2.y,
130 beziers->point3.x, beziers->point3.y);
bungeman@google.come8f05922012-08-16 16:13:40 +0000131 }
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}
mtklein1ee76512015-11-02 10:20:27 -0800148
149#endif//defined(SK_BUILD_FOR_WIN32)