gfx/skia/trunk/src/utils/win/SkDWriteGeometrySink.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial