1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6#ifndef _INC_D2D1HELPER
7#define _INC_D2D1HELPER
8
9#ifndef D2D_USE_C_DEFINITIONS
10
11namespace D2D1 {
12
13D2D1FORCEINLINE D2D1_MATRIX_3X2_F IdentityMatrix();
14
15template<typename T> struct TypeTraits {
16    typedef D2D1_POINT_2F  Point;
17    typedef D2D1_SIZE_F    Size;
18    typedef D2D1_RECT_F    Rect;
19};
20
21template<> struct TypeTraits<UINT32> {
22    typedef D2D1_POINT_2U  Point;
23    typedef D2D1_SIZE_U    Size;
24    typedef D2D1_RECT_U    Rect;
25};
26
27static inline FLOAT FloatMax() {
28    return 3.402823466e+38f;
29}
30
31template<typename T> D2D1FORCEINLINE typename TypeTraits<T>::Point Point2(T x, T y) {
32    typename TypeTraits<T>::Point r = {x,y};
33    return r;
34}
35
36D2D1FORCEINLINE D2D1_POINT_2F Point2F(FLOAT x = 0.f, FLOAT y = 0.f) {
37    return Point2<FLOAT>(x, y);
38}
39
40D2D1FORCEINLINE D2D1_POINT_2U Point2U(UINT32 x = 0, UINT32 y = 0) {
41    return Point2<UINT32>(x, y);
42}
43
44template<typename T> D2D1FORCEINLINE typename TypeTraits<T>::Size Size(T width, T height) {
45    typename TypeTraits<T>::Size r = {width, height};
46    return r;
47}
48
49D2D1FORCEINLINE D2D1_SIZE_F SizeF(FLOAT width = 0.0f, FLOAT height = 0.0f) {
50    return Size<FLOAT>(width, height);
51}
52
53D2D1FORCEINLINE D2D1_SIZE_U SizeU(UINT32 width = 0, UINT32 height = 0) {
54    return Size<UINT32>(width, height);
55}
56
57template<typename T> D2D1FORCEINLINE typename TypeTraits<T>::Rect Rect(T left, T top, T right, T bottom) {
58    typename TypeTraits<T>::Rect r = {left, top, right, bottom};
59    return r;
60}
61
62D2D1FORCEINLINE D2D1_RECT_F RectF(FLOAT left = 0.0f, FLOAT top = 0.0f, FLOAT right = 0.0f, FLOAT bottom = 0.0f) {
63    return Rect<FLOAT>(left, top, right, bottom);
64}
65
66D2D1FORCEINLINE D2D1_RECT_U RectU(UINT32 left = 0, UINT32 top = 0, UINT32 right = 0, UINT32 bottom = 0) {
67    return Rect<UINT32>(left, top, right, bottom);
68}
69
70D2D1FORCEINLINE D2D1_RECT_F InfiniteRect() {
71    D2D1_RECT_F r = {-FloatMax(), -FloatMax(), FloatMax(),  FloatMax()};
72    return r;
73}
74
75D2D1FORCEINLINE D2D1_ARC_SEGMENT ArcSegment(const D2D1_POINT_2F &point, const D2D1_SIZE_F &size, const FLOAT rotationAngle, D2D1_SWEEP_DIRECTION sweepDirection, D2D1_ARC_SIZE arcSize) {
76    D2D1_ARC_SEGMENT r = {point, size, rotationAngle, sweepDirection, arcSize};
77    return r;
78}
79
80D2D1FORCEINLINE D2D1_BEZIER_SEGMENT BezierSegment(const D2D1_POINT_2F &point1, const D2D1_POINT_2F &point2, const D2D1_POINT_2F &point3) {
81    D2D1_BEZIER_SEGMENT r = {point1, point2, point3};
82    return r;
83}
84
85D2D1FORCEINLINE D2D1_ELLIPSE Ellipse(const D2D1_POINT_2F &center, FLOAT radiusX, FLOAT radiusY) {
86    D2D1_ELLIPSE r = {center, radiusX, radiusY};
87    return r;
88}
89
90D2D1FORCEINLINE D2D1_ROUNDED_RECT RoundedRect(const D2D1_RECT_F &rect, FLOAT radiusX, FLOAT radiusY) {
91    D2D1_ROUNDED_RECT r = {rect, radiusX, radiusY};
92    return r;
93}
94
95D2D1FORCEINLINE D2D1_BRUSH_PROPERTIES BrushProperties(
96        FLOAT opacity = 1.0f,
97        const D2D1_MATRIX_3X2_F &transform = D2D1::IdentityMatrix()) {
98    D2D1_BRUSH_PROPERTIES r = {opacity, transform};
99    return r;
100}
101
102D2D1FORCEINLINE D2D1_GRADIENT_STOP GradientStop(FLOAT position, const D2D1_COLOR_F &color) {
103    D2D1_GRADIENT_STOP r = {position, color};
104    return r;
105}
106
107D2D1FORCEINLINE D2D1_QUADRATIC_BEZIER_SEGMENT QuadraticBezierSegment(const D2D1_POINT_2F &point1, const D2D1_POINT_2F &point2) {
108    D2D1_QUADRATIC_BEZIER_SEGMENT r = {point1, point2};
109    return r;
110}
111
112D2D1FORCEINLINE D2D1_STROKE_STYLE_PROPERTIES StrokeStyleProperties(
113        D2D1_CAP_STYLE startCap = D2D1_CAP_STYLE_FLAT,
114        D2D1_CAP_STYLE endCap = D2D1_CAP_STYLE_FLAT,
115        D2D1_CAP_STYLE dashCap = D2D1_CAP_STYLE_FLAT,
116        D2D1_LINE_JOIN lineJoin = D2D1_LINE_JOIN_MITER,
117        FLOAT miterLimit = 10.0f,
118        D2D1_DASH_STYLE dashStyle = D2D1_DASH_STYLE_SOLID,
119        FLOAT dashOffset = 0.0f) {
120    D2D1_STROKE_STYLE_PROPERTIES r = {startCap, endCap, dashCap, lineJoin, miterLimit, dashStyle, dashOffset};
121    return r;
122}
123
124D2D1FORCEINLINE D2D1_BITMAP_BRUSH_PROPERTIES BitmapBrushProperties(
125        D2D1_EXTEND_MODE extendModeX = D2D1_EXTEND_MODE_CLAMP,
126        D2D1_EXTEND_MODE extendModeY = D2D1_EXTEND_MODE_CLAMP,
127        D2D1_BITMAP_INTERPOLATION_MODE interpolationMode = D2D1_BITMAP_INTERPOLATION_MODE_LINEAR) {
128    D2D1_BITMAP_BRUSH_PROPERTIES r = {extendModeX, extendModeY, interpolationMode};
129    return r;
130}
131
132D2D1FORCEINLINE D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES LinearGradientBrushProperties(const D2D1_POINT_2F &startPoint, const D2D1_POINT_2F &endPoint) {
133    D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES r = {startPoint, endPoint};
134    return r;
135}
136
137D2D1FORCEINLINE D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES RadialGradientBrushProperties(const D2D1_POINT_2F &center, const D2D1_POINT_2F &gradientOriginOffset, FLOAT radiusX, FLOAT radiusY) {
138    D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES r = {center, gradientOriginOffset, radiusX, radiusY};
139    return r;
140}
141
142D2D1FORCEINLINE D2D1_PIXEL_FORMAT PixelFormat(
143        DXGI_FORMAT dxgiFormat = DXGI_FORMAT_UNKNOWN,
144        D2D1_ALPHA_MODE alphaMode = D2D1_ALPHA_MODE_UNKNOWN)
145{
146    D2D1_PIXEL_FORMAT r = {dxgiFormat, alphaMode};
147    return r;
148}
149
150D2D1FORCEINLINE D2D1_BITMAP_PROPERTIES BitmapProperties(CONST D2D1_PIXEL_FORMAT &pixelFormat = D2D1::PixelFormat(),
151        FLOAT dpiX = 96.0f, FLOAT dpiY = 96.0f) {
152    D2D1_BITMAP_PROPERTIES r = {pixelFormat, dpiX, dpiY};
153    return r;
154}
155
156D2D1FORCEINLINE D2D1_RENDER_TARGET_PROPERTIES RenderTargetProperties(
157        D2D1_RENDER_TARGET_TYPE type =  D2D1_RENDER_TARGET_TYPE_DEFAULT,
158        CONST D2D1_PIXEL_FORMAT &pixelFormat = D2D1::PixelFormat(),
159        FLOAT dpiX = 0.0,
160        FLOAT dpiY = 0.0,
161        D2D1_RENDER_TARGET_USAGE usage = D2D1_RENDER_TARGET_USAGE_NONE,
162        D2D1_FEATURE_LEVEL  minLevel = D2D1_FEATURE_LEVEL_DEFAULT)
163{
164    D2D1_RENDER_TARGET_PROPERTIES r = {type, pixelFormat, dpiX, dpiY, usage, minLevel};
165    return r;
166}
167
168D2D1FORCEINLINE D2D1_HWND_RENDER_TARGET_PROPERTIES HwndRenderTargetProperties(
169        HWND hwnd,
170        D2D1_SIZE_U pixelSize = D2D1::Size(static_cast<UINT>(0), static_cast<UINT>(0)),
171        D2D1_PRESENT_OPTIONS presentOptions = D2D1_PRESENT_OPTIONS_NONE) {
172    D2D1_HWND_RENDER_TARGET_PROPERTIES r = {hwnd, pixelSize, presentOptions};
173    return r;
174}
175
176D2D1FORCEINLINE D2D1_LAYER_PARAMETERS LayerParameters(
177        CONST D2D1_RECT_F &contentBounds = D2D1::InfiniteRect(),
178        ID2D1Geometry *geometricMask = NULL,
179        D2D1_ANTIALIAS_MODE maskAntialiasMode = D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
180        D2D1_MATRIX_3X2_F maskTransform = D2D1::IdentityMatrix(),
181        FLOAT opacity = 1.0,
182        ID2D1Brush *opacityBrush = NULL,
183        D2D1_LAYER_OPTIONS layerOptions = D2D1_LAYER_OPTIONS_NONE) {
184    D2D1_LAYER_PARAMETERS r =
185        {contentBounds, geometricMask, maskAntialiasMode, maskTransform, opacity, opacityBrush, layerOptions };
186    return r;
187}
188
189D2D1FORCEINLINE D2D1_DRAWING_STATE_DESCRIPTION DrawingStateDescription(
190        D2D1_ANTIALIAS_MODE antialiasMode = D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
191        D2D1_TEXT_ANTIALIAS_MODE textAntialiasMode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT,
192        D2D1_TAG tag1 = 0,
193        D2D1_TAG tag2 = 0,
194        const D2D1_MATRIX_3X2_F &transform = D2D1::IdentityMatrix()) {
195    D2D1_DRAWING_STATE_DESCRIPTION r = {antialiasMode, textAntialiasMode, tag1, tag2, transform};
196    return r;
197}
198
199class ColorF : public D2D1_COLOR_F {
200public:
201    enum Enum {
202        AliceBlue             = 0xf0f8ff,
203        AntiqueWhite          = 0xfaebd7,
204        Aqua                  = 0x00ffff,
205        Aquamarine            = 0x7fffd4,
206        Azure                 = 0xf0ffff,
207        Beige                 = 0xf5f5dc,
208        Bisque                = 0xffe4c4,
209        Black                 = 0x000000,
210        BlanchedAlmond        = 0xffebcd,
211        Blue                  = 0x0000ff,
212        BlueViolet            = 0x8a2be2,
213        Brown                 = 0xa52a2a,
214        BurlyWood             = 0xdeb887,
215        CadetBlue             = 0x5f9ea0,
216        Chartreuse            = 0x7fff00,
217        Chocolate             = 0xd2691e,
218        Coral                 = 0xff7f50,
219        CornflowerBlue        = 0x6495ed,
220        Cornsilk              = 0xfff8dc,
221        Crimson               = 0xdc143c,
222        Cyan                  = 0x00ffff,
223        DarkBlue              = 0x00008b,
224        DarkCyan              = 0x008b8b,
225        DarkGoldenrod         = 0xb8860b,
226        DarkGray              = 0xa9a9a9,
227        DarkGreen             = 0x006400,
228        DarkKhaki             = 0xbdb76b,
229        DarkMagenta           = 0x8b008b,
230        DarkOliveGreen        = 0x556B2f,
231        DarkOrange            = 0xff8c00,
232        DarkOrchid            = 0x9932cc,
233        DarkRed               = 0x8b0000,
234        DarkSalmon            = 0xe9967a,
235        DarkSeaGreen          = 0x8fbc8f,
236        DarkSlateBlue         = 0x483d8b,
237        DarkSlateGray         = 0x2f4f4f,
238        DarkTurquoise         = 0x00ced1,
239        DarkViolet            = 0x9400d3,
240        DeepPink              = 0xff1493,
241        DeepSkyBlue           = 0x00bfff,
242        DimGray               = 0x696969,
243        DodgerBlue            = 0x1e90ff,
244        Firebrick             = 0xb22222,
245        FloralWhite           = 0xfffaf0,
246        ForestGreen           = 0x228b22,
247        Fuchsia               = 0xff00ff,
248        Gainsboro             = 0xdcdcdc,
249        GhostWhite            = 0xf8f8ff,
250        Gold                  = 0xffd700,
251        Goldenrod             = 0xdaa520,
252        Gray                  = 0x808080,
253        Green                 = 0x008000,
254        GreenYellow           = 0xadff2f,
255        Honeydew              = 0xf0fff0,
256        HotPink               = 0xff69b4,
257        IndianRed             = 0xcd5c5c,
258        Indigo                = 0x4b0082,
259        Ivory                 = 0xfffff0,
260        Khaki                 = 0xf0e68c,
261        Lavender              = 0xe6e6fa,
262        LavenderBlush         = 0xfff0f5,
263        LawnGreen             = 0x7cfc00,
264        LemonChiffon          = 0xfffacd,
265        LightBlue             = 0xadd8e6,
266        LightCoral            = 0xf08080,
267        LightCyan             = 0xe0ffff,
268        LightGoldenrodYellow  = 0xfafad2,
269        LightGreen            = 0x90ee90,
270        LightGray             = 0xd3d3d3,
271        LightPink             = 0xffb6c1,
272        LightSalmon           = 0xffa07a,
273        LightSeaGreen         = 0x20b2aa,
274        LightSkyBlue          = 0x87cefa,
275        LightSlateGray        = 0x778899,
276        LightSteelBlue        = 0xb0c4de,
277        LightYellow           = 0xffffe0,
278        Lime                  = 0x00ff00,
279        LimeGreen             = 0x32cd32,
280        Linen                 = 0xfaf0e6,
281        Magenta               = 0xff00ff,
282        Maroon                = 0x800000,
283        MediumAquamarine      = 0x66cdaa,
284        MediumBlue            = 0x0000cd,
285        MediumOrchid          = 0xba55d3,
286        MediumPurple          = 0x9370db,
287        MediumSeaGreen        = 0x3cb371,
288        MediumSlateBlue       = 0x7b68ee,
289        MediumSpringGreen     = 0x00fa9a,
290        MediumTurquoise       = 0x48d1cc,
291        MediumVioletRed       = 0xc71585,
292        MidnightBlue          = 0x191970,
293        MintCream             = 0xf5fffa,
294        MistyRose             = 0xffe4e1,
295        Moccasin              = 0xffe4b5,
296        NavajoWhite           = 0xffdead,
297        Navy                  = 0x000080,
298        OldLace               = 0xfdf5e6,
299        Olive                 = 0x808000,
300        OliveDrab             = 0x6b8e23,
301        Orange                = 0xffa500,
302        OrangeRed             = 0xff4500,
303        Orchid                = 0xda70d6,
304        PaleGoldenrod         = 0xeee8aa,
305        PaleGreen             = 0x98fb98,
306        PaleTurquoise         = 0xafeeee,
307        PaleVioletRed         = 0xdb7093,
308        PapayaWhip            = 0xffefd5,
309        PeachPuff             = 0xffdab9,
310        Peru                  = 0xcd853f,
311        Pink                  = 0xffc0cb,
312        Plum                  = 0xdda0dd,
313        PowderBlue            = 0xb0e0e6,
314        Purple                = 0x800080,
315        Red                   = 0xff0000,
316        RosyBrown             = 0xbc8f8f,
317        RoyalBlue             = 0x4169e1,
318        SaddleBrown           = 0x8b4513,
319        Salmon                = 0xfa8072,
320        SandyBrown            = 0xf4a460,
321        SeaGreen              = 0x2e8B57,
322        SeaShell              = 0xfff5ee,
323        Sienna                = 0xa0522d,
324        Silver                = 0xc0c0c0,
325        SkyBlue               = 0x87ceeb,
326        SlateBlue             = 0x6a5acd,
327        SlateGray             = 0x708090,
328        Snow                  = 0xfffafa,
329        SpringGreen           = 0x00ff7f,
330        SteelBlue             = 0x4682B4,
331        Tan                   = 0xd2b48c,
332        Teal                  = 0x008080,
333        Thistle               = 0xd8bfd8,
334        Tomato                = 0xff6347,
335        Turquoise             = 0x40e0d0,
336        Violet                = 0xee82ee,
337        Wheat                 = 0xf5deb3,
338        White                 = 0xffffff,
339        WhiteSmoke            = 0xf5f5f5,
340        Yellow                = 0xffff00,
341        YellowGreen           = 0x9acd32
342    };
343
344    FORCEINLINE ColorF(UINT32 rgb, FLOAT _a = 1.0) {
345        init(rgb, _a);
346    }
347
348    D2D1FORCEINLINE ColorF(Enum knownColor, FLOAT _a = 1.0) {
349        init(knownColor, _a);
350    }
351
352    D2D1FORCEINLINE ColorF(FLOAT _r, FLOAT _g, FLOAT _b, FLOAT _a = 1.0) {
353        r = _r;
354        g = _g;
355        b = _b;
356        a = _a;
357    }
358private:
359    D2D1FORCEINLINE void init(UINT32 rgb, FLOAT _a) {
360        r = static_cast<float>((rgb>>16)&0xff)/255.0f;
361        g = static_cast<float>((rgb>>8)&0xff)/255.0f;
362        b = static_cast<float>(rgb&0xff)/255.0f;
363        a = _a;
364    }
365};
366
367class Matrix3x2F : public D2D1_MATRIX_3X2_F {
368public:
369    D2D1FORCEINLINE Matrix3x2F(FLOAT __11, FLOAT __12, FLOAT __21, FLOAT __22, FLOAT __31, FLOAT __32) {
370        _11 = __11;
371        _12 = __12;
372        _21 = __21;
373        _22 = __22;
374        _31 = __31;
375        _32 = __32;
376    }
377
378    D2D1FORCEINLINE Matrix3x2F() {}
379
380    static D2D1FORCEINLINE Matrix3x2F Identity() {
381        return Matrix3x2F(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
382    }
383
384    static D2D1FORCEINLINE Matrix3x2F Translation(D2D1_SIZE_F size) {
385        return Translation(size.width, size.height);
386    }
387
388    static D2D1FORCEINLINE Matrix3x2F Translation(FLOAT x, FLOAT y) {
389        return Matrix3x2F(1.0f, 0.0f, 0.0f, 1.0f, x, y);
390    }
391
392    static D2D1FORCEINLINE Matrix3x2F Scale(D2D1_SIZE_F size, D2D1_POINT_2F center = D2D1::Point2F()) {
393        return Scale(size.width, size.height, center);
394    }
395
396    static D2D1FORCEINLINE Matrix3x2F Scale(FLOAT x, FLOAT y, D2D1_POINT_2F center = D2D1::Point2F()) {
397        return Matrix3x2F(x, 0.0f, 0.0f, y, center.x - x*center.x, center.y - y*center.y);
398    }
399
400    static D2D1FORCEINLINE Matrix3x2F Rotation(FLOAT angle, D2D1_POINT_2F center = D2D1::Point2F()) {
401        Matrix3x2F r;
402        D2D1MakeRotateMatrix(angle, center, &r);
403        return r;
404    }
405
406    static D2D1FORCEINLINE Matrix3x2F Skew(FLOAT angleX, FLOAT angleY, D2D1_POINT_2F center = D2D1::Point2F()) {
407        Matrix3x2F r;
408        D2D1MakeSkewMatrix(angleX, angleY, center, &r);
409        return r;
410    }
411
412    static inline const Matrix3x2F *ReinterpretBaseType(const D2D1_MATRIX_3X2_F *pMatrix) {
413        return static_cast<const Matrix3x2F *>(pMatrix);
414    }
415
416    static inline Matrix3x2F *ReinterpretBaseType(D2D1_MATRIX_3X2_F *pMatrix) {
417        return static_cast<Matrix3x2F *>(pMatrix);
418    }
419
420    inline FLOAT Determinant() const {
421        return _11*_22 - _12*_21;
422    }
423
424    inline bool IsInvertible() const {
425        return !!D2D1IsMatrixInvertible(this);
426    }
427
428    inline bool Invert() {
429        return !!D2D1InvertMatrix(this);
430    }
431
432    inline bool IsIdentity() const {
433        return _11 == 1.0f && _12 == 0.0f && _21 == 0.0f && _22 == 1.0f && _31 == 0.0f && _32 == 0.0f;
434    }
435
436    inline void SetProduct(const Matrix3x2F &a, const Matrix3x2F &b) {
437        _11 = a._11*b._11 + a._12*b._21;
438        _12 = a._11*b._12 + a._12*b._22;
439        _21 = a._21*b._11 + a._22*b._21;
440        _22 = a._21*b._12 + a._22*b._22;
441        _31 = a._31*b._11 + a._32*b._21 + b._31;
442        _32 = a._31*b._12 + a._32*b._22 + b._32;
443    }
444
445    D2D1FORCEINLINE Matrix3x2F operator*(const Matrix3x2F &matrix) const {
446        Matrix3x2F r;
447        r.SetProduct(*this, matrix);
448        return r;
449    }
450
451    D2D1FORCEINLINE D2D1_POINT_2F TransformPoint(D2D1_POINT_2F point) const {
452        return Point2F(point.x*_11 + point.y*_21 + _31, point.x*_12 + point.y*_22 + _32);
453    }
454};
455
456D2D1FORCEINLINE D2D1_POINT_2F operator*(const D2D1_POINT_2F &point, const D2D1_MATRIX_3X2_F &matrix) {
457    return Matrix3x2F::ReinterpretBaseType(&matrix)->TransformPoint(point);
458}
459
460D2D1FORCEINLINE D2D1_MATRIX_3X2_F IdentityMatrix() {
461    return Matrix3x2F::Identity();
462}
463
464}
465
466D2D1FORCEINLINE D2D1_MATRIX_3X2_F operator*(const D2D1_MATRIX_3X2_F &matrix1, const D2D1_MATRIX_3X2_F &matrix2) {
467    D2D1::Matrix3x2F r;
468    r.SetProduct(*D2D1::Matrix3x2F::ReinterpretBaseType(&matrix1), *D2D1::Matrix3x2F::ReinterpretBaseType(&matrix2));
469    return r;
470}
471
472#endif /* D2D_USE_C_DEFINITIONS */
473
474#endif /*_INC_D2D1HELPER*/
475