secur32: Set output buffer size to zero during handshake when no data needs to be...
[wine.git] / dlls / d2d1 / geometry.c
blobd716fb128d70e4691f3957d5f0d2b3254f55a924
1 /*
2 * Copyright 2015 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include "d2d1_private.h"
23 #ifdef HAVE_FLOAT_H
24 #include <float.h>
25 #endif
27 WINE_DEFAULT_DEBUG_CHANNEL(d2d);
29 #define D2D_FIGURE_FLAG_CLOSED 0x00000001u
30 #define D2D_FIGURE_FLAG_HOLLOW 0x00000002u
32 #define D2D_CDT_EDGE_FLAG_FREED 0x80000000u
33 #define D2D_CDT_EDGE_FLAG_VISITED(r) (1u << (r))
35 #define D2D_FP_EPS (1.0f / (1 << FLT_MANT_DIG))
37 static const D2D1_MATRIX_3X2_F identity =
39 1.0f, 0.0f,
40 0.0f, 1.0f,
41 0.0f, 0.0f,
44 enum d2d_cdt_edge_next
46 D2D_EDGE_NEXT_ORIGIN = 0,
47 D2D_EDGE_NEXT_ROT = 1,
48 D2D_EDGE_NEXT_SYM = 2,
49 D2D_EDGE_NEXT_TOR = 3,
52 enum d2d_vertex_type
54 D2D_VERTEX_TYPE_NONE,
55 D2D_VERTEX_TYPE_LINE,
56 D2D_VERTEX_TYPE_BEZIER,
57 D2D_VERTEX_TYPE_SPLIT_BEZIER,
60 struct d2d_segment_idx
62 size_t figure_idx;
63 size_t vertex_idx;
64 size_t control_idx;
67 struct d2d_figure
69 D2D1_POINT_2F *vertices;
70 size_t vertices_size;
71 enum d2d_vertex_type *vertex_types;
72 size_t vertex_types_size;
73 size_t vertex_count;
75 D2D1_POINT_2F *bezier_controls;
76 size_t bezier_controls_size;
77 size_t bezier_control_count;
79 D2D1_POINT_2F *original_bezier_controls;
80 size_t original_bezier_control_count;
82 D2D1_RECT_F bounds;
83 unsigned int flags;
86 struct d2d_cdt_edge_ref
88 size_t idx;
89 enum d2d_cdt_edge_next r;
92 struct d2d_cdt_edge
94 struct d2d_cdt_edge_ref next[4];
95 size_t vertex[2];
96 unsigned int flags;
99 struct d2d_cdt
101 struct d2d_cdt_edge *edges;
102 size_t edges_size;
103 size_t edge_count;
104 size_t free_edge;
106 const D2D1_POINT_2F *vertices;
109 struct d2d_geometry_intersection
111 size_t figure_idx;
112 size_t vertex_idx;
113 size_t control_idx;
114 float t;
115 D2D1_POINT_2F p;
118 struct d2d_geometry_intersections
120 struct d2d_geometry_intersection *intersections;
121 size_t intersections_size;
122 size_t intersection_count;
125 struct d2d_fp_two_vec2
127 float x[2];
128 float y[2];
131 struct d2d_fp_fin
133 float *now, *other;
134 size_t length;
137 static void d2d_bezier_vertex_set(struct d2d_bezier_vertex *b,
138 const D2D1_POINT_2F *p, float u, float v, float sign)
140 b->position = *p;
141 b->texcoord.u = u;
142 b->texcoord.v = v;
143 b->texcoord.sign = sign;
146 static void d2d_face_set(struct d2d_face *f, UINT16 v0, UINT16 v1, UINT16 v2)
148 f->v[0] = v0;
149 f->v[1] = v1;
150 f->v[2] = v2;
153 static void d2d_outline_vertex_set(struct d2d_outline_vertex *v, float x, float y,
154 float prev_x, float prev_y, float next_x, float next_y)
156 d2d_point_set(&v->position, x, y);
157 d2d_point_set(&v->prev, prev_x, prev_y);
158 d2d_point_set(&v->next, next_x, next_y);
161 static void d2d_bezier_outline_vertex_set(struct d2d_bezier_outline_vertex *b, const D2D1_POINT_2F *position,
162 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2,
163 float prev_x, float prev_y, float next_x, float next_y)
165 b->position = *position;
166 b->p0 = *p0;
167 b->p1 = *p1;
168 b->p2 = *p2;
169 d2d_point_set(&b->prev, prev_x, prev_y);
170 d2d_point_set(&b->next, next_x, next_y);
173 static void d2d_fp_two_sum(float *out, float a, float b)
175 float a_virt, a_round, b_virt, b_round;
177 out[1] = a + b;
178 b_virt = out[1] - a;
179 a_virt = out[1] - b_virt;
180 b_round = b - b_virt;
181 a_round = a - a_virt;
182 out[0] = a_round + b_round;
185 static void d2d_fp_fast_two_sum(float *out, float a, float b)
187 float b_virt;
189 out[1] = a + b;
190 b_virt = out[1] - a;
191 out[0] = b - b_virt;
194 static void d2d_fp_two_two_sum(float *out, const float *a, const float *b)
196 float sum[2];
198 d2d_fp_two_sum(out, a[0], b[0]);
199 d2d_fp_two_sum(sum, a[1], out[1]);
200 d2d_fp_two_sum(&out[1], sum[0], b[1]);
201 d2d_fp_two_sum(&out[2], sum[1], out[2]);
204 static void d2d_fp_two_diff_tail(float *out, float a, float b, float x)
206 float a_virt, a_round, b_virt, b_round;
208 b_virt = a - x;
209 a_virt = x + b_virt;
210 b_round = b_virt - b;
211 a_round = a - a_virt;
212 *out = a_round + b_round;
215 static void d2d_fp_two_two_diff(float *out, const float *a, const float *b)
217 float sum[2], diff;
219 diff = a[0] - b[0];
220 d2d_fp_two_diff_tail(out, a[0], b[0], diff);
221 d2d_fp_two_sum(sum, a[1], diff);
222 diff = sum[0] - b[1];
223 d2d_fp_two_diff_tail(&out[1], sum[0], b[1], diff);
224 d2d_fp_two_sum(&out[2], sum[1], diff);
227 static void d2d_fp_split(float *out, float a)
229 float a_big, c;
231 c = a * ((1 << (FLT_MANT_DIG / 2)) + 1.0f);
232 a_big = c - a;
233 out[1] = c - a_big;
234 out[0] = a - out[1];
237 static void d2d_fp_two_product_presplit(float *out, float a, float b, const float *b_split)
239 float a_split[2], err1, err2, err3;
241 out[1] = a * b;
242 d2d_fp_split(a_split, a);
243 err1 = out[1] - (a_split[1] * b_split[1]);
244 err2 = err1 - (a_split[0] * b_split[1]);
245 err3 = err2 - (a_split[1] * b_split[0]);
246 out[0] = (a_split[0] * b_split[0]) - err3;
249 static void d2d_fp_two_product(float *out, float a, float b)
251 float b_split[2];
253 d2d_fp_split(b_split, b);
254 d2d_fp_two_product_presplit(out, a, b, b_split);
257 static void d2d_fp_square(float *out, float a)
259 float a_split[2], err1, err2;
261 out[1] = a * a;
262 d2d_fp_split(a_split, a);
263 err1 = out[1] - (a_split[1] * a_split[1]);
264 err2 = err1 - ((a_split[1] + a_split[1]) * a_split[0]);
265 out[0] = (a_split[0] * a_split[0]) - err2;
268 static float d2d_fp_estimate(float *a, size_t len)
270 float out = a[0];
271 size_t idx = 1;
273 while (idx < len)
274 out += a[idx++];
276 return out;
279 static void d2d_fp_fast_expansion_sum_zeroelim(float *out, size_t *out_len,
280 const float *a, size_t a_len, const float *b, size_t b_len)
282 float sum[2], q, a_curr, b_curr;
283 size_t a_idx, b_idx, out_idx;
285 a_curr = a[0];
286 b_curr = b[0];
287 a_idx = b_idx = 0;
288 if ((b_curr > a_curr) == (b_curr > -a_curr))
290 q = a_curr;
291 a_curr = a[++a_idx];
293 else
295 q = b_curr;
296 b_curr = b[++b_idx];
298 out_idx = 0;
299 if (a_idx < a_len && b_idx < b_len)
301 if ((b_curr > a_curr) == (b_curr > -a_curr))
303 d2d_fp_fast_two_sum(sum, a_curr, q);
304 a_curr = a[++a_idx];
306 else
308 d2d_fp_fast_two_sum(sum, b_curr, q);
309 b_curr = b[++b_idx];
311 if (sum[0] != 0.0f)
312 out[out_idx++] = sum[0];
313 q = sum[1];
314 while (a_idx < a_len && b_idx < b_len)
316 if ((b_curr > a_curr) == (b_curr > -a_curr))
318 d2d_fp_two_sum(sum, q, a_curr);
319 a_curr = a[++a_idx];
321 else
323 d2d_fp_two_sum(sum, q, b_curr);
324 b_curr = b[++b_idx];
326 if (sum[0] != 0.0f)
327 out[out_idx++] = sum[0];
328 q = sum[1];
331 while (a_idx < a_len)
333 d2d_fp_two_sum(sum, q, a_curr);
334 a_curr = a[++a_idx];
335 if (sum[0] != 0.0f)
336 out[out_idx++] = sum[0];
337 q = sum[1];
339 while (b_idx < b_len)
341 d2d_fp_two_sum(sum, q, b_curr);
342 b_curr = b[++b_idx];
343 if (sum[0] != 0.0f)
344 out[out_idx++] = sum[0];
345 q = sum[1];
347 if (q != 0.0f || !out_idx)
348 out[out_idx++] = q;
350 *out_len = out_idx;
353 static void d2d_fp_scale_expansion_zeroelim(float *out, size_t *out_len, const float *a, size_t a_len, float b)
355 float product[2], sum[2], b_split[2], q[2], a_curr;
356 size_t a_idx, out_idx;
358 d2d_fp_split(b_split, b);
359 d2d_fp_two_product_presplit(q, a[0], b, b_split);
360 out_idx = 0;
361 if (q[0] != 0.0f)
362 out[out_idx++] = q[0];
363 for (a_idx = 1; a_idx < a_len; ++a_idx)
365 a_curr = a[a_idx];
366 d2d_fp_two_product_presplit(product, a_curr, b, b_split);
367 d2d_fp_two_sum(sum, q[1], product[0]);
368 if (sum[0] != 0.0f)
369 out[out_idx++] = sum[0];
370 d2d_fp_fast_two_sum(q, product[1], sum[1]);
371 if (q[0] != 0.0f)
372 out[out_idx++] = q[0];
374 if (q[1] != 0.0f || !out_idx)
375 out[out_idx++] = q[1];
377 *out_len = out_idx;
380 static void d2d_point_subtract(D2D1_POINT_2F *out,
381 const D2D1_POINT_2F *a, const D2D1_POINT_2F *b)
383 out->x = a->x - b->x;
384 out->y = a->y - b->y;
387 static void d2d_point_scale(D2D1_POINT_2F *p, float scale)
389 p->x *= scale;
390 p->y *= scale;
393 static void d2d_point_lerp(D2D1_POINT_2F *out,
394 const D2D1_POINT_2F *a, const D2D1_POINT_2F *b, float t)
396 out->x = a->x * (1.0f - t) + b->x * t;
397 out->y = a->y * (1.0f - t) + b->y * t;
400 static void d2d_point_calculate_bezier(D2D1_POINT_2F *out, const D2D1_POINT_2F *p0,
401 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float t)
403 float t_c = 1.0f - t;
405 out->x = t_c * (t_c * p0->x + t * p1->x) + t * (t_c * p1->x + t * p2->x);
406 out->y = t_c * (t_c * p0->y + t * p1->y) + t * (t_c * p1->y + t * p2->y);
409 static void d2d_point_normalise(D2D1_POINT_2F *p)
411 float l;
413 if ((l = sqrtf(d2d_point_dot(p, p))) != 0.0f)
414 d2d_point_scale(p, 1.0f / l);
417 /* This implementation is based on the paper "Adaptive Precision
418 * Floating-Point Arithmetic and Fast Robust Geometric Predicates" and
419 * associated (Public Domain) code by Jonathan Richard Shewchuk. */
420 static float d2d_point_ccw(const D2D1_POINT_2F *a, const D2D1_POINT_2F *b, const D2D1_POINT_2F *c)
422 static const float err_bound_result = (3.0f + 8.0f * D2D_FP_EPS) * D2D_FP_EPS;
423 static const float err_bound_a = (3.0f + 16.0f * D2D_FP_EPS) * D2D_FP_EPS;
424 static const float err_bound_b = (2.0f + 12.0f * D2D_FP_EPS) * D2D_FP_EPS;
425 static const float err_bound_c = (9.0f + 64.0f * D2D_FP_EPS) * D2D_FP_EPS * D2D_FP_EPS;
426 float det_d[16], det_c2[12], det_c1[8], det_b[4], temp4[4], temp2a[2], temp2b[2], abxacy[2], abyacx[2];
427 size_t det_d_len, det_c2_len, det_c1_len;
428 float det, det_sum, err_bound;
429 struct d2d_fp_two_vec2 ab, ac;
431 ab.x[1] = b->x - a->x;
432 ab.y[1] = b->y - a->y;
433 ac.x[1] = c->x - a->x;
434 ac.y[1] = c->y - a->y;
436 abxacy[1] = ab.x[1] * ac.y[1];
437 abyacx[1] = ab.y[1] * ac.x[1];
438 det = abxacy[1] - abyacx[1];
440 if (abxacy[1] > 0.0f)
442 if (abyacx[1] <= 0.0f)
443 return det;
444 det_sum = abxacy[1] + abyacx[1];
446 else if (abxacy[1] < 0.0f)
448 if (abyacx[1] >= 0.0f)
449 return det;
450 det_sum = -abxacy[1] - abyacx[1];
452 else
454 return det;
457 err_bound = err_bound_a * det_sum;
458 if (det >= err_bound || -det >= err_bound)
459 return det;
461 d2d_fp_two_product(abxacy, ab.x[1], ac.y[1]);
462 d2d_fp_two_product(abyacx, ab.y[1], ac.x[1]);
463 d2d_fp_two_two_diff(det_b, abxacy, abyacx);
465 det = d2d_fp_estimate(det_b, 4);
466 err_bound = err_bound_b * det_sum;
467 if (det >= err_bound || -det >= err_bound)
468 return det;
470 d2d_fp_two_diff_tail(&ab.x[0], b->x, a->x, ab.x[1]);
471 d2d_fp_two_diff_tail(&ab.y[0], b->y, a->y, ab.y[1]);
472 d2d_fp_two_diff_tail(&ac.x[0], c->x, a->x, ac.x[1]);
473 d2d_fp_two_diff_tail(&ac.y[0], c->y, a->y, ac.y[1]);
475 if (ab.x[0] == 0.0f && ab.y[0] == 0.0f && ac.x[0] == 0.0f && ac.y[0] == 0.0f)
476 return det;
478 err_bound = err_bound_c * det_sum + err_bound_result * fabsf(det);
479 det += (ab.x[1] * ac.y[0] + ac.y[1] * ab.x[0]) - (ab.y[1] * ac.x[0] + ac.x[1] * ab.y[0]);
480 if (det >= err_bound || -det >= err_bound)
481 return det;
483 d2d_fp_two_product(temp2a, ab.x[0], ac.y[1]);
484 d2d_fp_two_product(temp2b, ab.y[0], ac.x[1]);
485 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
486 d2d_fp_fast_expansion_sum_zeroelim(det_c1, &det_c1_len, det_b, 4, temp4, 4);
488 d2d_fp_two_product(temp2a, ab.x[1], ac.y[0]);
489 d2d_fp_two_product(temp2b, ab.y[1], ac.x[0]);
490 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
491 d2d_fp_fast_expansion_sum_zeroelim(det_c2, &det_c2_len, det_c1, det_c1_len, temp4, 4);
493 d2d_fp_two_product(temp2a, ab.x[0], ac.y[0]);
494 d2d_fp_two_product(temp2b, ab.y[0], ac.x[0]);
495 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
496 d2d_fp_fast_expansion_sum_zeroelim(det_d, &det_d_len, det_c2, det_c2_len, temp4, 4);
498 return det_d[det_d_len - 1];
501 static void d2d_rect_union(D2D1_RECT_F *l, const D2D1_RECT_F *r)
503 l->left = min(l->left, r->left);
504 l->top = min(l->top, r->top);
505 l->right = max(l->right, r->right);
506 l->bottom = max(l->bottom, r->bottom);
509 static BOOL d2d_rect_check_overlap(const D2D_RECT_F *p, const D2D_RECT_F *q)
511 return p->left < q->right && p->top < q->bottom && p->right > q->left && p->bottom > q->top;
514 static void d2d_rect_get_bezier_bounds(D2D_RECT_F *bounds, const D2D1_POINT_2F *p0,
515 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
517 D2D1_POINT_2F p;
518 float root;
520 bounds->left = p0->x;
521 bounds->top = p0->y;
522 bounds->right = p0->x;
523 bounds->bottom = p0->y;
525 d2d_rect_expand(bounds, p2);
527 /* f(t) = (1 - t)²P₀ + 2(1 - t)tP₁ + t²P₂
528 * f'(t) = 2(1 - t)(P₁ - P₀) + 2t(P₂ - P₁)
529 * = 2(P₂ - 2P₁ + P₀)t + 2(P₁ - P₀)
531 * f'(t) = 0
532 * t = (P₀ - P₁) / (P₂ - 2P₁ + P₀) */
533 root = (p0->x - p1->x) / (p2->x - 2.0f * p1->x + p0->x);
534 if (root > 0.0f && root < 1.0f)
536 d2d_point_calculate_bezier(&p, p0, p1, p2, root);
537 d2d_rect_expand(bounds, &p);
540 root = (p0->y - p1->y) / (p2->y - 2.0f * p1->y + p0->y);
541 if (root > 0.0f && root < 1.0f)
543 d2d_point_calculate_bezier(&p, p0, p1, p2, root);
544 d2d_rect_expand(bounds, &p);
548 static void d2d_rect_get_bezier_segment_bounds(D2D_RECT_F *bounds, const D2D1_POINT_2F *p0,
549 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float start, float end)
551 D2D1_POINT_2F q[3], r[2];
553 d2d_point_lerp(&r[0], p0, p1, start);
554 d2d_point_lerp(&r[1], p1, p2, start);
555 d2d_point_lerp(&q[0], &r[0], &r[1], start);
557 end = (end - start) / (1.0f - start);
558 d2d_point_lerp(&q[1], &q[0], &r[1], end);
559 d2d_point_lerp(&r[0], &r[1], p2, end);
560 d2d_point_lerp(&q[2], &q[1], &r[0], end);
562 d2d_rect_get_bezier_bounds(bounds, &q[0], &q[1], &q[2]);
565 static BOOL d2d_figure_insert_vertex(struct d2d_figure *figure, size_t idx, D2D1_POINT_2F vertex)
567 if (!d2d_array_reserve((void **)&figure->vertices, &figure->vertices_size,
568 figure->vertex_count + 1, sizeof(*figure->vertices)))
570 ERR("Failed to grow vertices array.\n");
571 return FALSE;
574 if (!d2d_array_reserve((void **)&figure->vertex_types, &figure->vertex_types_size,
575 figure->vertex_count + 1, sizeof(*figure->vertex_types)))
577 ERR("Failed to grow vertex types array.\n");
578 return FALSE;
581 memmove(&figure->vertices[idx + 1], &figure->vertices[idx],
582 (figure->vertex_count - idx) * sizeof(*figure->vertices));
583 memmove(&figure->vertex_types[idx + 1], &figure->vertex_types[idx],
584 (figure->vertex_count - idx) * sizeof(*figure->vertex_types));
585 figure->vertices[idx] = vertex;
586 figure->vertex_types[idx] = D2D_VERTEX_TYPE_NONE;
587 d2d_rect_expand(&figure->bounds, &vertex);
588 ++figure->vertex_count;
589 return TRUE;
592 static BOOL d2d_figure_add_vertex(struct d2d_figure *figure, D2D1_POINT_2F vertex)
594 size_t last = figure->vertex_count - 1;
596 if (figure->vertex_count && figure->vertex_types[last] == D2D_VERTEX_TYPE_LINE
597 && !memcmp(&figure->vertices[last], &vertex, sizeof(vertex)))
598 return TRUE;
600 if (!d2d_array_reserve((void **)&figure->vertices, &figure->vertices_size,
601 figure->vertex_count + 1, sizeof(*figure->vertices)))
603 ERR("Failed to grow vertices array.\n");
604 return FALSE;
607 if (!d2d_array_reserve((void **)&figure->vertex_types, &figure->vertex_types_size,
608 figure->vertex_count + 1, sizeof(*figure->vertex_types)))
610 ERR("Failed to grow vertex types array.\n");
611 return FALSE;
614 figure->vertices[figure->vertex_count] = vertex;
615 figure->vertex_types[figure->vertex_count] = D2D_VERTEX_TYPE_NONE;
616 d2d_rect_expand(&figure->bounds, &vertex);
617 ++figure->vertex_count;
618 return TRUE;
621 static BOOL d2d_figure_insert_bezier_control(struct d2d_figure *figure, size_t idx, const D2D1_POINT_2F *p)
623 if (!d2d_array_reserve((void **)&figure->bezier_controls, &figure->bezier_controls_size,
624 figure->bezier_control_count + 1, sizeof(*figure->bezier_controls)))
626 ERR("Failed to grow bezier controls array.\n");
627 return FALSE;
630 memmove(&figure->bezier_controls[idx + 1], &figure->bezier_controls[idx],
631 (figure->bezier_control_count - idx) * sizeof(*figure->bezier_controls));
632 figure->bezier_controls[idx] = *p;
633 ++figure->bezier_control_count;
635 return TRUE;
638 static BOOL d2d_figure_add_bezier_control(struct d2d_figure *figure, const D2D1_POINT_2F *p)
640 if (!d2d_array_reserve((void **)&figure->bezier_controls, &figure->bezier_controls_size,
641 figure->bezier_control_count + 1, sizeof(*figure->bezier_controls)))
643 ERR("Failed to grow bezier controls array.\n");
644 return FALSE;
647 figure->bezier_controls[figure->bezier_control_count++] = *p;
649 return TRUE;
652 static void d2d_cdt_edge_rot(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
654 dst->idx = src->idx;
655 dst->r = (src->r + D2D_EDGE_NEXT_ROT) & 3;
658 static void d2d_cdt_edge_sym(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
660 dst->idx = src->idx;
661 dst->r = (src->r + D2D_EDGE_NEXT_SYM) & 3;
664 static void d2d_cdt_edge_tor(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
666 dst->idx = src->idx;
667 dst->r = (src->r + D2D_EDGE_NEXT_TOR) & 3;
670 static void d2d_cdt_edge_next_left(const struct d2d_cdt *cdt,
671 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
673 d2d_cdt_edge_rot(dst, &cdt->edges[src->idx].next[(src->r + D2D_EDGE_NEXT_TOR) & 3]);
676 static void d2d_cdt_edge_next_origin(const struct d2d_cdt *cdt,
677 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
679 *dst = cdt->edges[src->idx].next[src->r];
682 static void d2d_cdt_edge_prev_origin(const struct d2d_cdt *cdt,
683 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
685 d2d_cdt_edge_rot(dst, &cdt->edges[src->idx].next[(src->r + D2D_EDGE_NEXT_ROT) & 3]);
688 static size_t d2d_cdt_edge_origin(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
690 return cdt->edges[e->idx].vertex[e->r >> 1];
693 static size_t d2d_cdt_edge_destination(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
695 return cdt->edges[e->idx].vertex[!(e->r >> 1)];
698 static void d2d_cdt_edge_set_origin(const struct d2d_cdt *cdt,
699 const struct d2d_cdt_edge_ref *e, size_t vertex)
701 cdt->edges[e->idx].vertex[e->r >> 1] = vertex;
704 static void d2d_cdt_edge_set_destination(const struct d2d_cdt *cdt,
705 const struct d2d_cdt_edge_ref *e, size_t vertex)
707 cdt->edges[e->idx].vertex[!(e->r >> 1)] = vertex;
710 static float d2d_cdt_ccw(const struct d2d_cdt *cdt, size_t a, size_t b, size_t c)
712 return d2d_point_ccw(&cdt->vertices[a], &cdt->vertices[b], &cdt->vertices[c]);
715 static BOOL d2d_cdt_rightof(const struct d2d_cdt *cdt, size_t p, const struct d2d_cdt_edge_ref *e)
717 return d2d_cdt_ccw(cdt, p, d2d_cdt_edge_destination(cdt, e), d2d_cdt_edge_origin(cdt, e)) > 0.0f;
720 static BOOL d2d_cdt_leftof(const struct d2d_cdt *cdt, size_t p, const struct d2d_cdt_edge_ref *e)
722 return d2d_cdt_ccw(cdt, p, d2d_cdt_edge_origin(cdt, e), d2d_cdt_edge_destination(cdt, e)) > 0.0f;
725 /* |ax ay|
726 * |bx by| */
727 static void d2d_fp_four_det2x2(float *out, float ax, float ay, float bx, float by)
729 float axby[2], aybx[2];
731 d2d_fp_two_product(axby, ax, by);
732 d2d_fp_two_product(aybx, ay, bx);
733 d2d_fp_two_two_diff(out, axby, aybx);
736 /* (a->x² + a->y²) * det2x2 */
737 static void d2d_fp_sub_det3x3(float *out, size_t *out_len, const struct d2d_fp_two_vec2 *a, const float *det2x2)
739 size_t axd_len, ayd_len, axxd_len, ayyd_len;
740 float axd[8], ayd[8], axxd[16], ayyd[16];
742 d2d_fp_scale_expansion_zeroelim(axd, &axd_len, det2x2, 4, a->x[1]);
743 d2d_fp_scale_expansion_zeroelim(axxd, &axxd_len, axd, axd_len, a->x[1]);
744 d2d_fp_scale_expansion_zeroelim(ayd, &ayd_len, det2x2, 4, a->y[1]);
745 d2d_fp_scale_expansion_zeroelim(ayyd, &ayyd_len, ayd, ayd_len, a->y[1]);
746 d2d_fp_fast_expansion_sum_zeroelim(out, out_len, axxd, axxd_len, ayyd, ayyd_len);
749 /* det_abt = det_ab * c[0]
750 * fin += c[0] * (az * b - bz * a + c[1] * det_ab * 2.0f) */
751 static void d2d_cdt_incircle_refine1(struct d2d_fp_fin *fin, float *det_abt, size_t *det_abt_len,
752 const float *det_ab, float a, const float *az, float b, const float *bz, const float *c)
754 size_t temp48_len, temp32_len, temp16a_len, temp16b_len, temp16c_len, temp8_len;
755 float temp48[48], temp32[32], temp16a[16], temp16b[16], temp16c[16], temp8[8];
756 float *swap;
758 d2d_fp_scale_expansion_zeroelim(det_abt, det_abt_len, det_ab, 4, c[0]);
759 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, det_abt, *det_abt_len, 2.0f * c[1]);
760 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, az, 4, c[0]);
761 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, temp8, temp8_len, b);
762 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, bz, 4, c[0]);
763 d2d_fp_scale_expansion_zeroelim(temp16c, &temp16c_len, temp8, temp8_len, -a);
764 d2d_fp_fast_expansion_sum_zeroelim(temp32, &temp32_len, temp16a, temp16a_len, temp16b, temp16b_len);
765 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16c, temp16c_len, temp32, temp32_len);
766 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
767 swap = fin->now; fin->now = fin->other; fin->other = swap;
770 static void d2d_cdt_incircle_refine2(struct d2d_fp_fin *fin, const struct d2d_fp_two_vec2 *a,
771 const struct d2d_fp_two_vec2 *b, const float *bz, const struct d2d_fp_two_vec2 *c, const float *cz,
772 const float *axt_det_bc, size_t axt_det_bc_len, const float *ayt_det_bc, size_t ayt_det_bc_len)
774 size_t temp64_len, temp48_len, temp32a_len, temp32b_len, temp16a_len, temp16b_len, temp8_len;
775 float temp64[64], temp48[48], temp32a[32], temp32b[32], temp16a[16], temp16b[16], temp8[8];
776 float bct[8], bctt[4], temp4a[4], temp4b[4], temp2a[2], temp2b[2];
777 size_t bct_len, bctt_len;
778 float *swap;
780 /* bct = (b->x[0] * c->y[1] + b->x[1] * c->y[0]) - (c->x[0] * b->y[1] + c->x[1] * b->y[0]) */
781 /* bctt = b->x[0] * c->y[0] + c->x[0] * b->y[0] */
782 if (b->x[0] != 0.0f || b->y[0] != 0.0f || c->x[0] != 0.0f || c->y[0] != 0.0f)
784 d2d_fp_two_product(temp2a, b->x[0], c->y[1]);
785 d2d_fp_two_product(temp2b, b->x[1], c->y[0]);
786 d2d_fp_two_two_sum(temp4a, temp2a, temp2b);
787 d2d_fp_two_product(temp2a, c->x[0], -b->y[1]);
788 d2d_fp_two_product(temp2b, c->x[1], -b->y[0]);
789 d2d_fp_two_two_sum(temp4b, temp2a, temp2b);
790 d2d_fp_fast_expansion_sum_zeroelim(bct, &bct_len, temp4a, 4, temp4b, 4);
792 d2d_fp_two_product(temp2a, b->x[0], c->y[0]);
793 d2d_fp_two_product(temp2b, c->x[0], b->y[0]);
794 d2d_fp_two_two_diff(bctt, temp2a, temp2b);
795 bctt_len = 4;
797 else
799 bct[0] = 0.0f;
800 bct_len = 1;
801 bctt[0] = 0.0f;
802 bctt_len = 1;
805 if (a->x[0] != 0.0f)
807 size_t axt_bct_len, axt_bctt_len;
808 float axt_bct[16], axt_bctt[8];
810 /* fin += a->x[0] * (axt_det_bc + bct * 2.0f * a->x[1]) */
811 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, axt_det_bc, axt_det_bc_len, a->x[0]);
812 d2d_fp_scale_expansion_zeroelim(axt_bct, &axt_bct_len, bct, bct_len, a->x[0]);
813 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, axt_bct, axt_bct_len, 2.0f * a->x[1]);
814 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16a, temp16a_len, temp32a, temp32a_len);
815 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
816 swap = fin->now; fin->now = fin->other; fin->other = swap;
818 if (b->y[0] != 0.0f)
820 /* fin += a->x[0] * cz * b->y[0] */
821 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, cz, 4, a->x[0]);
822 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, temp8, temp8_len, b->y[0]);
823 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp16a, temp16a_len);
824 swap = fin->now; fin->now = fin->other; fin->other = swap;
827 if (c->y[0] != 0.0f)
829 /* fin -= a->x[0] * bz * c->y[0] */
830 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, bz, 4, -a->x[0]);
831 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, temp8, temp8_len, c->y[0]);
832 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp16a, temp16a_len);
833 swap = fin->now; fin->now = fin->other; fin->other = swap;
836 /* fin += a->x[0] * (bct * a->x[0] + bctt * (2.0f * a->x[1] + a->x[0])) */
837 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, axt_bct, axt_bct_len, a->x[0]);
838 d2d_fp_scale_expansion_zeroelim(axt_bctt, &axt_bctt_len, bctt, bctt_len, a->x[0]);
839 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, axt_bctt, axt_bctt_len, 2.0f * a->x[1]);
840 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, axt_bctt, axt_bctt_len, a->x[0]);
841 d2d_fp_fast_expansion_sum_zeroelim(temp32b, &temp32b_len, temp16a, temp16a_len, temp16b, temp16b_len);
842 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, temp32a, temp32a_len, temp32b, temp32b_len);
843 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp64, temp64_len);
844 swap = fin->now; fin->now = fin->other; fin->other = swap;
847 if (a->y[0] != 0.0f)
849 size_t ayt_bct_len, ayt_bctt_len;
850 float ayt_bct[16], ayt_bctt[8];
852 /* fin += a->y[0] * (ayt_det_bc + bct * 2.0f * a->y[1]) */
853 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, ayt_det_bc, ayt_det_bc_len, a->y[0]);
854 d2d_fp_scale_expansion_zeroelim(ayt_bct, &ayt_bct_len, bct, bct_len, a->y[0]);
855 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, ayt_bct, ayt_bct_len, 2.0f * a->y[1]);
856 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16a, temp16a_len, temp32a, temp32a_len);
857 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
858 swap = fin->now; fin->now = fin->other; fin->other = swap;
860 /* fin += a->y[0] * (bct * a->y[0] + bctt * (2.0f * a->y[1] + a->y[0])) */
861 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, ayt_bct, ayt_bct_len, a->y[0]);
862 d2d_fp_scale_expansion_zeroelim(ayt_bctt, &ayt_bctt_len, bctt, bctt_len, a->y[0]);
863 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, ayt_bctt, ayt_bctt_len, 2.0f * a->y[1]);
864 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, ayt_bctt, ayt_bctt_len, a->y[0]);
865 d2d_fp_fast_expansion_sum_zeroelim(temp32b, &temp32b_len, temp16a, temp16a_len, temp16b, temp16b_len);
866 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, temp32a, temp32a_len, temp32b, temp32b_len);
867 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp64, temp64_len);
868 swap = fin->now; fin->now = fin->other; fin->other = swap;
872 /* Determine if point D is inside or outside the circle defined by points A,
873 * B, C. As explained in the paper by Guibas and Stolfi, this is equivalent to
874 * calculating the signed volume of the tetrahedron defined by projecting the
875 * points onto the paraboloid of revolution x = x² + y²,
876 * λ:(x, y) → (x, y, x² + y²). I.e., D is inside the cirlce if
878 * |λ(A) 1|
879 * |λ(B) 1| > 0
880 * |λ(C) 1|
881 * |λ(D) 1|
883 * After translating D to the origin, that becomes:
885 * |λ(A-D)|
886 * |λ(B-D)| > 0
887 * |λ(C-D)|
889 * This implementation is based on the paper "Adaptive Precision
890 * Floating-Point Arithmetic and Fast Robust Geometric Predicates" and
891 * associated (Public Domain) code by Jonathan Richard Shewchuk. */
892 static BOOL d2d_cdt_incircle(const struct d2d_cdt *cdt, size_t a, size_t b, size_t c, size_t d)
894 static const float err_bound_result = (3.0f + 8.0f * D2D_FP_EPS) * D2D_FP_EPS;
895 static const float err_bound_a = (10.0f + 96.0f * D2D_FP_EPS) * D2D_FP_EPS;
896 static const float err_bound_b = (4.0f + 48.0f * D2D_FP_EPS) * D2D_FP_EPS;
897 static const float err_bound_c = (44.0f + 576.0f * D2D_FP_EPS) * D2D_FP_EPS * D2D_FP_EPS;
899 size_t axt_det_bc_len, ayt_det_bc_len, bxt_det_ca_len, byt_det_ca_len, cxt_det_ab_len, cyt_det_ab_len;
900 float axt_det_bc[8], ayt_det_bc[8], bxt_det_ca[8], byt_det_ca[8], cxt_det_ab[8], cyt_det_ab[8];
901 float fin1[1152], fin2[1152], temp64[64], sub_det_a[32], sub_det_b[32], sub_det_c[32];
902 float det_bc[4], det_ca[4], det_ab[4], daz[4], dbz[4], dcz[4], temp2a[2], temp2b[2];
903 size_t temp64_len, sub_det_a_len, sub_det_b_len, sub_det_c_len;
904 float dbxdcy, dbydcx, dcxday, dcydax, daxdby, daydbx;
905 const D2D1_POINT_2F *p = cdt->vertices;
906 struct d2d_fp_two_vec2 da, db, dc;
907 float permanent, err_bound, det;
908 struct d2d_fp_fin fin;
910 da.x[1] = p[a].x - p[d].x;
911 da.y[1] = p[a].y - p[d].y;
912 db.x[1] = p[b].x - p[d].x;
913 db.y[1] = p[b].y - p[d].y;
914 dc.x[1] = p[c].x - p[d].x;
915 dc.y[1] = p[c].y - p[d].y;
917 daz[3] = da.x[1] * da.x[1] + da.y[1] * da.y[1];
918 dbxdcy = db.x[1] * dc.y[1];
919 dbydcx = db.y[1] * dc.x[1];
921 dbz[3] = db.x[1] * db.x[1] + db.y[1] * db.y[1];
922 dcxday = dc.x[1] * da.y[1];
923 dcydax = dc.y[1] * da.x[1];
925 dcz[3] = dc.x[1] * dc.x[1] + dc.y[1] * dc.y[1];
926 daxdby = da.x[1] * db.y[1];
927 daydbx = da.y[1] * db.x[1];
929 det = daz[3] * (dbxdcy - dbydcx) + dbz[3] * (dcxday - dcydax) + dcz[3] * (daxdby - daydbx);
930 permanent = daz[3] * (fabsf(dbxdcy) + fabsf(dbydcx))
931 + dbz[3] * (fabsf(dcxday) + fabsf(dcydax))
932 + dcz[3] * (fabsf(daxdby) + fabsf(daydbx));
933 err_bound = err_bound_a * permanent;
934 if (det > err_bound || -det > err_bound)
935 return det > 0.0f;
937 fin.now = fin1;
938 fin.other = fin2;
940 d2d_fp_four_det2x2(det_bc, db.x[1], db.y[1], dc.x[1], dc.y[1]);
941 d2d_fp_sub_det3x3(sub_det_a, &sub_det_a_len, &da, det_bc);
943 d2d_fp_four_det2x2(det_ca, dc.x[1], dc.y[1], da.x[1], da.y[1]);
944 d2d_fp_sub_det3x3(sub_det_b, &sub_det_b_len, &db, det_ca);
946 d2d_fp_four_det2x2(det_ab, da.x[1], da.y[1], db.x[1], db.y[1]);
947 d2d_fp_sub_det3x3(sub_det_c, &sub_det_c_len, &dc, det_ab);
949 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, sub_det_a, sub_det_a_len, sub_det_b, sub_det_b_len);
950 d2d_fp_fast_expansion_sum_zeroelim(fin.now, &fin.length, temp64, temp64_len, sub_det_c, sub_det_c_len);
951 det = d2d_fp_estimate(fin.now, fin.length);
952 err_bound = err_bound_b * permanent;
953 if (det >= err_bound || -det >= err_bound)
954 return det > 0.0f;
956 d2d_fp_two_diff_tail(&da.x[0], p[a].x, p[d].x, da.x[1]);
957 d2d_fp_two_diff_tail(&da.y[0], p[a].y, p[d].y, da.y[1]);
958 d2d_fp_two_diff_tail(&db.x[0], p[b].x, p[d].x, db.x[1]);
959 d2d_fp_two_diff_tail(&db.y[0], p[b].y, p[d].y, db.y[1]);
960 d2d_fp_two_diff_tail(&dc.x[0], p[c].x, p[d].x, dc.x[1]);
961 d2d_fp_two_diff_tail(&dc.y[0], p[c].y, p[d].y, dc.y[1]);
962 if (da.x[0] == 0.0f && db.x[0] == 0.0f && dc.x[0] == 0.0f
963 && da.y[0] == 0.0f && db.y[0] == 0.0f && dc.y[0] == 0.0f)
964 return det > 0.0f;
966 err_bound = err_bound_c * permanent + err_bound_result * fabsf(det);
967 det += (daz[3] * ((db.x[1] * dc.y[0] + dc.y[1] * db.x[0]) - (db.y[1] * dc.x[0] + dc.x[1] * db.y[0]))
968 + 2.0f * (da.x[1] * da.x[0] + da.y[1] * da.y[0]) * (db.x[1] * dc.y[1] - db.y[1] * dc.x[1]))
969 + (dbz[3] * ((dc.x[1] * da.y[0] + da.y[1] * dc.x[0]) - (dc.y[1] * da.x[0] + da.x[1] * dc.y[0]))
970 + 2.0f * (db.x[1] * db.x[0] + db.y[1] * db.y[0]) * (dc.x[1] * da.y[1] - dc.y[1] * da.x[1]))
971 + (dcz[3] * ((da.x[1] * db.y[0] + db.y[1] * da.x[0]) - (da.y[1] * db.x[0] + db.x[1] * da.y[0]))
972 + 2.0f * (dc.x[1] * dc.x[0] + dc.y[1] * dc.y[0]) * (da.x[1] * db.y[1] - da.y[1] * db.x[1]));
973 if (det >= err_bound || -det >= err_bound)
974 return det > 0.0f;
976 if (db.x[0] != 0.0f || db.y[0] != 0.0f || dc.x[0] != 0.0f || dc.y[0] != 0.0f)
978 d2d_fp_square(temp2a, da.x[1]);
979 d2d_fp_square(temp2b, da.y[1]);
980 d2d_fp_two_two_sum(daz, temp2a, temp2b);
982 if (dc.x[0] != 0.0f || dc.y[0] != 0.0f || da.x[0] != 0.0f || da.y[0] != 0.0f)
984 d2d_fp_square(temp2a, db.x[1]);
985 d2d_fp_square(temp2b, db.y[1]);
986 d2d_fp_two_two_sum(dbz, temp2a, temp2b);
988 if (da.x[0] != 0.0f || da.y[0] != 0.0f || db.x[0] != 0.0f || db.y[0] != 0.0f)
990 d2d_fp_square(temp2a, dc.x[1]);
991 d2d_fp_square(temp2b, dc.y[1]);
992 d2d_fp_two_two_sum(dcz, temp2a, temp2b);
995 if (da.x[0] != 0.0f)
996 d2d_cdt_incircle_refine1(&fin, axt_det_bc, &axt_det_bc_len, det_bc, dc.y[1], dcz, db.y[1], dbz, da.x);
997 if (da.y[0] != 0.0f)
998 d2d_cdt_incircle_refine1(&fin, ayt_det_bc, &ayt_det_bc_len, det_bc, db.x[1], dbz, dc.x[1], dcz, da.y);
999 if (db.x[0] != 0.0f)
1000 d2d_cdt_incircle_refine1(&fin, bxt_det_ca, &bxt_det_ca_len, det_ca, da.y[1], daz, dc.y[1], dcz, db.x);
1001 if (db.y[0] != 0.0f)
1002 d2d_cdt_incircle_refine1(&fin, byt_det_ca, &byt_det_ca_len, det_ca, dc.x[1], dcz, da.x[1], daz, db.y);
1003 if (dc.x[0] != 0.0f)
1004 d2d_cdt_incircle_refine1(&fin, cxt_det_ab, &cxt_det_ab_len, det_ab, db.y[1], dbz, da.y[1], daz, dc.x);
1005 if (dc.y[0] != 0.0f)
1006 d2d_cdt_incircle_refine1(&fin, cyt_det_ab, &cyt_det_ab_len, det_ab, da.x[1], daz, db.x[1], dbz, dc.y);
1008 if (da.x[0] != 0.0f || da.y[0] != 0.0f)
1009 d2d_cdt_incircle_refine2(&fin, &da, &db, dbz, &dc, dcz,
1010 axt_det_bc, axt_det_bc_len, ayt_det_bc, ayt_det_bc_len);
1011 if (db.x[0] != 0.0f || db.y[0] != 0.0f)
1012 d2d_cdt_incircle_refine2(&fin, &db, &dc, dcz, &da, daz,
1013 bxt_det_ca, bxt_det_ca_len, byt_det_ca, byt_det_ca_len);
1014 if (dc.x[0] != 0.0f || dc.y[0] != 0.0f)
1015 d2d_cdt_incircle_refine2(&fin, &dc, &da, daz, &db, dbz,
1016 cxt_det_ab, cxt_det_ab_len, cyt_det_ab, cyt_det_ab_len);
1018 return fin.now[fin.length - 1] > 0.0f;
1021 static void d2d_cdt_splice(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *a,
1022 const struct d2d_cdt_edge_ref *b)
1024 struct d2d_cdt_edge_ref ta, tb, alpha, beta;
1026 ta = cdt->edges[a->idx].next[a->r];
1027 tb = cdt->edges[b->idx].next[b->r];
1028 cdt->edges[a->idx].next[a->r] = tb;
1029 cdt->edges[b->idx].next[b->r] = ta;
1031 d2d_cdt_edge_rot(&alpha, &ta);
1032 d2d_cdt_edge_rot(&beta, &tb);
1034 ta = cdt->edges[alpha.idx].next[alpha.r];
1035 tb = cdt->edges[beta.idx].next[beta.r];
1036 cdt->edges[alpha.idx].next[alpha.r] = tb;
1037 cdt->edges[beta.idx].next[beta.r] = ta;
1040 static BOOL d2d_cdt_create_edge(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *e)
1042 struct d2d_cdt_edge *edge;
1044 if (cdt->free_edge != ~0u)
1046 e->idx = cdt->free_edge;
1047 cdt->free_edge = cdt->edges[e->idx].next[D2D_EDGE_NEXT_ORIGIN].idx;
1049 else
1051 if (!d2d_array_reserve((void **)&cdt->edges, &cdt->edges_size, cdt->edge_count + 1, sizeof(*cdt->edges)))
1053 ERR("Failed to grow edges array.\n");
1054 return FALSE;
1056 e->idx = cdt->edge_count++;
1058 e->r = 0;
1060 edge = &cdt->edges[e->idx];
1061 edge->next[D2D_EDGE_NEXT_ORIGIN] = *e;
1062 d2d_cdt_edge_tor(&edge->next[D2D_EDGE_NEXT_ROT], e);
1063 d2d_cdt_edge_sym(&edge->next[D2D_EDGE_NEXT_SYM], e);
1064 d2d_cdt_edge_rot(&edge->next[D2D_EDGE_NEXT_TOR], e);
1065 edge->flags = 0;
1067 return TRUE;
1070 static void d2d_cdt_destroy_edge(struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
1072 struct d2d_cdt_edge_ref next, sym, prev;
1074 d2d_cdt_edge_next_origin(cdt, &next, e);
1075 if (next.idx != e->idx || next.r != e->r)
1077 d2d_cdt_edge_prev_origin(cdt, &prev, e);
1078 d2d_cdt_splice(cdt, e, &prev);
1081 d2d_cdt_edge_sym(&sym, e);
1083 d2d_cdt_edge_next_origin(cdt, &next, &sym);
1084 if (next.idx != sym.idx || next.r != sym.r)
1086 d2d_cdt_edge_prev_origin(cdt, &prev, &sym);
1087 d2d_cdt_splice(cdt, &sym, &prev);
1090 cdt->edges[e->idx].flags |= D2D_CDT_EDGE_FLAG_FREED;
1091 cdt->edges[e->idx].next[D2D_EDGE_NEXT_ORIGIN].idx = cdt->free_edge;
1092 cdt->free_edge = e->idx;
1095 static BOOL d2d_cdt_connect(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *e,
1096 const struct d2d_cdt_edge_ref *a, const struct d2d_cdt_edge_ref *b)
1098 struct d2d_cdt_edge_ref tmp;
1100 if (!d2d_cdt_create_edge(cdt, e))
1101 return FALSE;
1102 d2d_cdt_edge_set_origin(cdt, e, d2d_cdt_edge_destination(cdt, a));
1103 d2d_cdt_edge_set_destination(cdt, e, d2d_cdt_edge_origin(cdt, b));
1104 d2d_cdt_edge_next_left(cdt, &tmp, a);
1105 d2d_cdt_splice(cdt, e, &tmp);
1106 d2d_cdt_edge_sym(&tmp, e);
1107 d2d_cdt_splice(cdt, &tmp, b);
1109 return TRUE;
1112 static BOOL d2d_cdt_merge(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *left_outer,
1113 struct d2d_cdt_edge_ref *left_inner, struct d2d_cdt_edge_ref *right_inner,
1114 struct d2d_cdt_edge_ref *right_outer)
1116 struct d2d_cdt_edge_ref base_edge, tmp;
1118 /* Create the base edge between both parts. */
1119 for (;;)
1121 if (d2d_cdt_leftof(cdt, d2d_cdt_edge_origin(cdt, right_inner), left_inner))
1123 d2d_cdt_edge_next_left(cdt, left_inner, left_inner);
1125 else if (d2d_cdt_rightof(cdt, d2d_cdt_edge_origin(cdt, left_inner), right_inner))
1127 d2d_cdt_edge_sym(&tmp, right_inner);
1128 d2d_cdt_edge_next_origin(cdt, right_inner, &tmp);
1130 else
1132 break;
1136 d2d_cdt_edge_sym(&tmp, right_inner);
1137 if (!d2d_cdt_connect(cdt, &base_edge, &tmp, left_inner))
1138 return FALSE;
1139 if (d2d_cdt_edge_origin(cdt, left_inner) == d2d_cdt_edge_origin(cdt, left_outer))
1140 d2d_cdt_edge_sym(left_outer, &base_edge);
1141 if (d2d_cdt_edge_origin(cdt, right_inner) == d2d_cdt_edge_origin(cdt, right_outer))
1142 *right_outer = base_edge;
1144 for (;;)
1146 struct d2d_cdt_edge_ref left_candidate, right_candidate, sym_base_edge;
1147 BOOL left_valid, right_valid;
1149 /* Find the left candidate. */
1150 d2d_cdt_edge_sym(&sym_base_edge, &base_edge);
1151 d2d_cdt_edge_next_origin(cdt, &left_candidate, &sym_base_edge);
1152 if ((left_valid = d2d_cdt_leftof(cdt, d2d_cdt_edge_destination(cdt, &left_candidate), &sym_base_edge)))
1154 d2d_cdt_edge_next_origin(cdt, &tmp, &left_candidate);
1155 while (d2d_cdt_edge_destination(cdt, &tmp) != d2d_cdt_edge_destination(cdt, &sym_base_edge)
1156 && d2d_cdt_incircle(cdt,
1157 d2d_cdt_edge_origin(cdt, &sym_base_edge), d2d_cdt_edge_destination(cdt, &sym_base_edge),
1158 d2d_cdt_edge_destination(cdt, &left_candidate), d2d_cdt_edge_destination(cdt, &tmp)))
1160 d2d_cdt_destroy_edge(cdt, &left_candidate);
1161 left_candidate = tmp;
1162 d2d_cdt_edge_next_origin(cdt, &tmp, &left_candidate);
1165 d2d_cdt_edge_sym(&left_candidate, &left_candidate);
1167 /* Find the right candidate. */
1168 d2d_cdt_edge_prev_origin(cdt, &right_candidate, &base_edge);
1169 if ((right_valid = d2d_cdt_rightof(cdt, d2d_cdt_edge_destination(cdt, &right_candidate), &base_edge)))
1171 d2d_cdt_edge_prev_origin(cdt, &tmp, &right_candidate);
1172 while (d2d_cdt_edge_destination(cdt, &tmp) != d2d_cdt_edge_destination(cdt, &base_edge)
1173 && d2d_cdt_incircle(cdt,
1174 d2d_cdt_edge_origin(cdt, &sym_base_edge), d2d_cdt_edge_destination(cdt, &sym_base_edge),
1175 d2d_cdt_edge_destination(cdt, &right_candidate), d2d_cdt_edge_destination(cdt, &tmp)))
1177 d2d_cdt_destroy_edge(cdt, &right_candidate);
1178 right_candidate = tmp;
1179 d2d_cdt_edge_prev_origin(cdt, &tmp, &right_candidate);
1183 if (!left_valid && !right_valid)
1184 break;
1186 /* Connect the appropriate candidate with the base edge. */
1187 if (!left_valid || (right_valid && d2d_cdt_incircle(cdt,
1188 d2d_cdt_edge_origin(cdt, &left_candidate), d2d_cdt_edge_destination(cdt, &left_candidate),
1189 d2d_cdt_edge_origin(cdt, &right_candidate), d2d_cdt_edge_destination(cdt, &right_candidate))))
1191 if (!d2d_cdt_connect(cdt, &base_edge, &right_candidate, &sym_base_edge))
1192 return FALSE;
1194 else
1196 if (!d2d_cdt_connect(cdt, &base_edge, &sym_base_edge, &left_candidate))
1197 return FALSE;
1201 return TRUE;
1204 /* Create a Delaunay triangulation from a set of vertices. This is an
1205 * implementation of the divide-and-conquer algorithm described by Guibas and
1206 * Stolfi. Should be called with at least two vertices. */
1207 static BOOL d2d_cdt_triangulate(struct d2d_cdt *cdt, size_t start_vertex, size_t vertex_count,
1208 struct d2d_cdt_edge_ref *left_edge, struct d2d_cdt_edge_ref *right_edge)
1210 struct d2d_cdt_edge_ref left_inner, left_outer, right_inner, right_outer, tmp;
1211 size_t cut;
1213 /* Only two vertices, create a single edge. */
1214 if (vertex_count == 2)
1216 struct d2d_cdt_edge_ref a;
1218 if (!d2d_cdt_create_edge(cdt, &a))
1219 return FALSE;
1220 d2d_cdt_edge_set_origin(cdt, &a, start_vertex);
1221 d2d_cdt_edge_set_destination(cdt, &a, start_vertex + 1);
1223 *left_edge = a;
1224 d2d_cdt_edge_sym(right_edge, &a);
1226 return TRUE;
1229 /* Three vertices, create a triangle. */
1230 if (vertex_count == 3)
1232 struct d2d_cdt_edge_ref a, b, c;
1233 float det;
1235 if (!d2d_cdt_create_edge(cdt, &a))
1236 return FALSE;
1237 if (!d2d_cdt_create_edge(cdt, &b))
1238 return FALSE;
1239 d2d_cdt_edge_sym(&tmp, &a);
1240 d2d_cdt_splice(cdt, &tmp, &b);
1242 d2d_cdt_edge_set_origin(cdt, &a, start_vertex);
1243 d2d_cdt_edge_set_destination(cdt, &a, start_vertex + 1);
1244 d2d_cdt_edge_set_origin(cdt, &b, start_vertex + 1);
1245 d2d_cdt_edge_set_destination(cdt, &b, start_vertex + 2);
1247 det = d2d_cdt_ccw(cdt, start_vertex, start_vertex + 1, start_vertex + 2);
1248 if (det != 0.0f && !d2d_cdt_connect(cdt, &c, &b, &a))
1249 return FALSE;
1251 if (det < 0.0f)
1253 d2d_cdt_edge_sym(left_edge, &c);
1254 *right_edge = c;
1256 else
1258 *left_edge = a;
1259 d2d_cdt_edge_sym(right_edge, &b);
1262 return TRUE;
1265 /* More than tree vertices, divide. */
1266 cut = vertex_count / 2;
1267 if (!d2d_cdt_triangulate(cdt, start_vertex, cut, &left_outer, &left_inner))
1268 return FALSE;
1269 if (!d2d_cdt_triangulate(cdt, start_vertex + cut, vertex_count - cut, &right_inner, &right_outer))
1270 return FALSE;
1271 /* Merge the left and right parts. */
1272 if (!d2d_cdt_merge(cdt, &left_outer, &left_inner, &right_inner, &right_outer))
1273 return FALSE;
1275 *left_edge = left_outer;
1276 *right_edge = right_outer;
1277 return TRUE;
1280 static int d2d_cdt_compare_vertices(const void *a, const void *b)
1282 const D2D1_POINT_2F *p0 = a;
1283 const D2D1_POINT_2F *p1 = b;
1284 float diff = p0->x - p1->x;
1286 if (diff == 0.0f)
1287 diff = p0->y - p1->y;
1289 return diff == 0.0f ? 0 : (diff > 0.0f ? 1 : -1);
1292 /* Determine whether a given point is inside the geometry, using the current
1293 * fill mode rule. */
1294 static BOOL d2d_path_geometry_point_inside(const struct d2d_geometry *geometry,
1295 const D2D1_POINT_2F *probe, BOOL triangles_only)
1297 const D2D1_POINT_2F *p0, *p1;
1298 D2D1_POINT_2F v_p, v_probe;
1299 unsigned int score;
1300 size_t i, j, last;
1302 for (i = 0, score = 0; i < geometry->u.path.figure_count; ++i)
1304 const struct d2d_figure *figure = &geometry->u.path.figures[i];
1306 if (probe->x < figure->bounds.left || probe->x > figure->bounds.right
1307 || probe->y < figure->bounds.top || probe->y > figure->bounds.bottom)
1308 continue;
1310 last = figure->vertex_count - 1;
1311 if (!triangles_only)
1313 while (last && figure->vertex_types[last] == D2D_VERTEX_TYPE_NONE)
1314 --last;
1316 p0 = &figure->vertices[last];
1317 for (j = 0; j <= last; ++j)
1319 if (!triangles_only && figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
1320 continue;
1322 p1 = &figure->vertices[j];
1323 d2d_point_subtract(&v_p, p1, p0);
1324 d2d_point_subtract(&v_probe, probe, p0);
1326 if ((probe->y < p0->y) != (probe->y < p1->y) && v_probe.x < v_p.x * (v_probe.y / v_p.y))
1328 if (geometry->u.path.fill_mode == D2D1_FILL_MODE_ALTERNATE || (probe->y < p0->y))
1329 ++score;
1330 else
1331 --score;
1334 p0 = p1;
1338 return geometry->u.path.fill_mode == D2D1_FILL_MODE_ALTERNATE ? score & 1 : score;
1341 static BOOL d2d_path_geometry_add_fill_face(struct d2d_geometry *geometry, const struct d2d_cdt *cdt,
1342 const struct d2d_cdt_edge_ref *base_edge)
1344 struct d2d_cdt_edge_ref tmp;
1345 struct d2d_face *face;
1346 D2D1_POINT_2F probe;
1348 if (cdt->edges[base_edge->idx].flags & D2D_CDT_EDGE_FLAG_VISITED(base_edge->r))
1349 return TRUE;
1351 if (!d2d_array_reserve((void **)&geometry->fill.faces, &geometry->fill.faces_size,
1352 geometry->fill.face_count + 1, sizeof(*geometry->fill.faces)))
1354 ERR("Failed to grow faces array.\n");
1355 return FALSE;
1358 face = &geometry->fill.faces[geometry->fill.face_count];
1360 /* It may seem tempting to use the center of the face as probe origin, but
1361 * multiplying by powers of two works much better for preserving accuracy. */
1363 tmp = *base_edge;
1364 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1365 face->v[0] = d2d_cdt_edge_origin(cdt, &tmp);
1366 probe.x = cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.25f;
1367 probe.y = cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.25f;
1369 d2d_cdt_edge_next_left(cdt, &tmp, &tmp);
1370 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1371 face->v[1] = d2d_cdt_edge_origin(cdt, &tmp);
1372 probe.x += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.25f;
1373 probe.y += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.25f;
1375 d2d_cdt_edge_next_left(cdt, &tmp, &tmp);
1376 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1377 face->v[2] = d2d_cdt_edge_origin(cdt, &tmp);
1378 probe.x += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.50f;
1379 probe.y += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.50f;
1381 if (d2d_cdt_leftof(cdt, face->v[2], base_edge) && d2d_path_geometry_point_inside(geometry, &probe, TRUE))
1382 ++geometry->fill.face_count;
1384 return TRUE;
1387 static BOOL d2d_cdt_generate_faces(const struct d2d_cdt *cdt, struct d2d_geometry *geometry)
1389 struct d2d_cdt_edge_ref base_edge;
1390 size_t i;
1392 for (i = 0; i < cdt->edge_count; ++i)
1394 if (cdt->edges[i].flags & D2D_CDT_EDGE_FLAG_FREED)
1395 continue;
1397 base_edge.idx = i;
1398 base_edge.r = 0;
1399 if (!d2d_path_geometry_add_fill_face(geometry, cdt, &base_edge))
1400 goto fail;
1401 d2d_cdt_edge_sym(&base_edge, &base_edge);
1402 if (!d2d_path_geometry_add_fill_face(geometry, cdt, &base_edge))
1403 goto fail;
1406 return TRUE;
1408 fail:
1409 heap_free(geometry->fill.faces);
1410 geometry->fill.faces = NULL;
1411 geometry->fill.faces_size = 0;
1412 geometry->fill.face_count = 0;
1413 return FALSE;
1416 static BOOL d2d_cdt_fixup(struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *base_edge)
1418 struct d2d_cdt_edge_ref candidate, next, new_base;
1419 unsigned int count = 0;
1421 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1422 if (next.idx == base_edge->idx)
1424 ERR("Degenerate face.\n");
1425 return FALSE;
1428 candidate = next;
1429 while (d2d_cdt_edge_destination(cdt, &next) != d2d_cdt_edge_origin(cdt, base_edge))
1431 if (d2d_cdt_incircle(cdt, d2d_cdt_edge_origin(cdt, base_edge), d2d_cdt_edge_destination(cdt, base_edge),
1432 d2d_cdt_edge_destination(cdt, &candidate), d2d_cdt_edge_destination(cdt, &next)))
1433 candidate = next;
1434 d2d_cdt_edge_next_left(cdt, &next, &next);
1435 ++count;
1438 if (count > 1)
1440 d2d_cdt_edge_next_left(cdt, &next, &candidate);
1441 if (d2d_cdt_edge_destination(cdt, &next) == d2d_cdt_edge_origin(cdt, base_edge))
1442 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1443 else
1444 next = *base_edge;
1445 if (!d2d_cdt_connect(cdt, &new_base, &candidate, &next))
1446 return FALSE;
1447 if (!d2d_cdt_fixup(cdt, &new_base))
1448 return FALSE;
1449 d2d_cdt_edge_sym(&new_base, &new_base);
1450 if (!d2d_cdt_fixup(cdt, &new_base))
1451 return FALSE;
1454 return TRUE;
1457 static void d2d_cdt_cut_edges(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *end_edge,
1458 const struct d2d_cdt_edge_ref *base_edge, size_t start_vertex, size_t end_vertex)
1460 struct d2d_cdt_edge_ref next;
1461 float ccw;
1463 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1464 if (d2d_cdt_edge_destination(cdt, &next) == end_vertex)
1466 *end_edge = next;
1467 return;
1470 ccw = d2d_cdt_ccw(cdt, d2d_cdt_edge_destination(cdt, &next), end_vertex, start_vertex);
1471 if (ccw == 0.0f)
1473 *end_edge = next;
1474 return;
1477 if (ccw > 0.0f)
1478 d2d_cdt_edge_next_left(cdt, &next, &next);
1480 d2d_cdt_edge_sym(&next, &next);
1481 d2d_cdt_cut_edges(cdt, end_edge, &next, start_vertex, end_vertex);
1482 d2d_cdt_destroy_edge(cdt, &next);
1485 static BOOL d2d_cdt_insert_segment(struct d2d_cdt *cdt, struct d2d_geometry *geometry,
1486 const struct d2d_cdt_edge_ref *origin, struct d2d_cdt_edge_ref *edge, size_t end_vertex)
1488 struct d2d_cdt_edge_ref base_edge, current, new_origin, next, target;
1489 size_t current_destination, current_origin;
1491 for (current = *origin;; current = next)
1493 d2d_cdt_edge_next_origin(cdt, &next, &current);
1495 current_destination = d2d_cdt_edge_destination(cdt, &current);
1496 if (current_destination == end_vertex)
1498 d2d_cdt_edge_sym(edge, &current);
1499 return TRUE;
1502 current_origin = d2d_cdt_edge_origin(cdt, &current);
1503 if (d2d_cdt_ccw(cdt, end_vertex, current_origin, current_destination) == 0.0f
1504 && (cdt->vertices[current_destination].x > cdt->vertices[current_origin].x)
1505 == (cdt->vertices[end_vertex].x > cdt->vertices[current_origin].x)
1506 && (cdt->vertices[current_destination].y > cdt->vertices[current_origin].y)
1507 == (cdt->vertices[end_vertex].y > cdt->vertices[current_origin].y))
1509 d2d_cdt_edge_sym(&new_origin, &current);
1510 return d2d_cdt_insert_segment(cdt, geometry, &new_origin, edge, end_vertex);
1513 if (d2d_cdt_rightof(cdt, end_vertex, &next) && d2d_cdt_leftof(cdt, end_vertex, &current))
1515 d2d_cdt_edge_next_left(cdt, &base_edge, &current);
1517 d2d_cdt_edge_sym(&base_edge, &base_edge);
1518 d2d_cdt_cut_edges(cdt, &target, &base_edge, d2d_cdt_edge_origin(cdt, origin), end_vertex);
1519 d2d_cdt_destroy_edge(cdt, &base_edge);
1521 if (!d2d_cdt_connect(cdt, &base_edge, &target, &current))
1522 return FALSE;
1523 *edge = base_edge;
1524 if (!d2d_cdt_fixup(cdt, &base_edge))
1525 return FALSE;
1526 d2d_cdt_edge_sym(&base_edge, &base_edge);
1527 if (!d2d_cdt_fixup(cdt, &base_edge))
1528 return FALSE;
1530 if (d2d_cdt_edge_origin(cdt, edge) == end_vertex)
1531 return TRUE;
1532 new_origin = *edge;
1533 return d2d_cdt_insert_segment(cdt, geometry, &new_origin, edge, end_vertex);
1536 if (next.idx == origin->idx)
1538 ERR("Triangle not found.\n");
1539 return FALSE;
1544 static BOOL d2d_cdt_insert_segments(struct d2d_cdt *cdt, struct d2d_geometry *geometry)
1546 size_t start_vertex, end_vertex, i, j, k;
1547 struct d2d_cdt_edge_ref edge, new_edge;
1548 const struct d2d_figure *figure;
1549 const D2D1_POINT_2F *p;
1550 BOOL found;
1552 for (i = 0; i < geometry->u.path.figure_count; ++i)
1554 figure = &geometry->u.path.figures[i];
1556 /* Degenerate figure. */
1557 if (figure->vertex_count < 2)
1558 continue;
1560 p = bsearch(&figure->vertices[figure->vertex_count - 1], cdt->vertices,
1561 geometry->fill.vertex_count, sizeof(*p), d2d_cdt_compare_vertices);
1562 start_vertex = p - cdt->vertices;
1564 for (k = 0, found = FALSE; k < cdt->edge_count; ++k)
1566 if (cdt->edges[k].flags & D2D_CDT_EDGE_FLAG_FREED)
1567 continue;
1569 edge.idx = k;
1570 edge.r = 0;
1572 if (d2d_cdt_edge_origin(cdt, &edge) == start_vertex)
1574 found = TRUE;
1575 break;
1577 d2d_cdt_edge_sym(&edge, &edge);
1578 if (d2d_cdt_edge_origin(cdt, &edge) == start_vertex)
1580 found = TRUE;
1581 break;
1585 if (!found)
1587 ERR("Edge not found.\n");
1588 return FALSE;
1591 for (j = 0; j < figure->vertex_count; start_vertex = end_vertex, ++j)
1593 p = bsearch(&figure->vertices[j], cdt->vertices,
1594 geometry->fill.vertex_count, sizeof(*p), d2d_cdt_compare_vertices);
1595 end_vertex = p - cdt->vertices;
1597 if (start_vertex == end_vertex)
1598 continue;
1600 if (!d2d_cdt_insert_segment(cdt, geometry, &edge, &new_edge, end_vertex))
1601 return FALSE;
1602 edge = new_edge;
1606 return TRUE;
1609 static BOOL d2d_geometry_intersections_add(struct d2d_geometry_intersections *i,
1610 const struct d2d_segment_idx *segment_idx, float t, D2D1_POINT_2F p)
1612 struct d2d_geometry_intersection *intersection;
1614 if (!d2d_array_reserve((void **)&i->intersections, &i->intersections_size,
1615 i->intersection_count + 1, sizeof(*i->intersections)))
1617 ERR("Failed to grow intersections array.\n");
1618 return FALSE;
1621 intersection = &i->intersections[i->intersection_count++];
1622 intersection->figure_idx = segment_idx->figure_idx;
1623 intersection->vertex_idx = segment_idx->vertex_idx;
1624 intersection->control_idx = segment_idx->control_idx;
1625 intersection->t = t;
1626 intersection->p = p;
1628 return TRUE;
1631 static int d2d_geometry_intersections_compare(const void *a, const void *b)
1633 const struct d2d_geometry_intersection *i0 = a;
1634 const struct d2d_geometry_intersection *i1 = b;
1636 if (i0->figure_idx != i1->figure_idx)
1637 return i0->figure_idx - i1->figure_idx;
1638 if (i0->vertex_idx != i1->vertex_idx)
1639 return i0->vertex_idx - i1->vertex_idx;
1640 if (i0->t != i1->t)
1641 return i0->t > i1->t ? 1 : -1;
1642 return 0;
1645 static BOOL d2d_geometry_intersect_line_line(struct d2d_geometry *geometry,
1646 struct d2d_geometry_intersections *intersections, const struct d2d_segment_idx *idx_p,
1647 const struct d2d_segment_idx *idx_q)
1649 D2D1_POINT_2F v_p, v_q, v_qp, intersection;
1650 const D2D1_POINT_2F *p[2], *q[2];
1651 const struct d2d_figure *figure;
1652 float s, t, det;
1653 size_t next;
1655 figure = &geometry->u.path.figures[idx_p->figure_idx];
1656 p[0] = &figure->vertices[idx_p->vertex_idx];
1657 next = idx_p->vertex_idx + 1;
1658 if (next == figure->vertex_count)
1659 next = 0;
1660 p[1] = &figure->vertices[next];
1662 figure = &geometry->u.path.figures[idx_q->figure_idx];
1663 q[0] = &figure->vertices[idx_q->vertex_idx];
1664 next = idx_q->vertex_idx + 1;
1665 if (next == figure->vertex_count)
1666 next = 0;
1667 q[1] = &figure->vertices[next];
1669 d2d_point_subtract(&v_p, p[1], p[0]);
1670 d2d_point_subtract(&v_q, q[1], q[0]);
1671 d2d_point_subtract(&v_qp, p[0], q[0]);
1673 det = v_p.x * v_q.y - v_p.y * v_q.x;
1674 if (det == 0.0f)
1675 return TRUE;
1677 s = (v_q.x * v_qp.y - v_q.y * v_qp.x) / det;
1678 t = (v_p.x * v_qp.y - v_p.y * v_qp.x) / det;
1680 if (s < 0.0f || s > 1.0f || t < 0.0f || t > 1.0f)
1681 return TRUE;
1683 intersection.x = p[0]->x + v_p.x * s;
1684 intersection.y = p[0]->y + v_p.y * s;
1686 if (s > 0.0f && s < 1.0f && !d2d_geometry_intersections_add(intersections, idx_p, s, intersection))
1687 return FALSE;
1689 if (t > 0.0f && t < 1.0f && !d2d_geometry_intersections_add(intersections, idx_q, t, intersection))
1690 return FALSE;
1692 return TRUE;
1695 static BOOL d2d_geometry_add_bezier_line_intersections(struct d2d_geometry *geometry,
1696 struct d2d_geometry_intersections *intersections, const struct d2d_segment_idx *idx_p,
1697 const D2D1_POINT_2F **p, const struct d2d_segment_idx *idx_q, const D2D1_POINT_2F **q, float s)
1699 D2D1_POINT_2F intersection;
1700 float t;
1702 d2d_point_calculate_bezier(&intersection, p[0], p[1], p[2], s);
1703 if (fabsf(q[1]->x - q[0]->x) > fabsf(q[1]->y - q[0]->y))
1704 t = (intersection.x - q[0]->x) / (q[1]->x - q[0]->x);
1705 else
1706 t = (intersection.y - q[0]->y) / (q[1]->y - q[0]->y);
1707 if (t < 0.0f || t > 1.0f)
1708 return TRUE;
1710 d2d_point_lerp(&intersection, q[0], q[1], t);
1712 if (s > 0.0f && s < 1.0f && !d2d_geometry_intersections_add(intersections, idx_p, s, intersection))
1713 return FALSE;
1715 if (t > 0.0f && t < 1.0f && !d2d_geometry_intersections_add(intersections, idx_q, t, intersection))
1716 return FALSE;
1718 return TRUE;
1721 static BOOL d2d_geometry_intersect_bezier_line(struct d2d_geometry *geometry,
1722 struct d2d_geometry_intersections *intersections,
1723 const struct d2d_segment_idx *idx_p, const struct d2d_segment_idx *idx_q)
1725 const D2D1_POINT_2F *p[3], *q[2];
1726 const struct d2d_figure *figure;
1727 float y[3], root, theta, d, e;
1728 size_t next;
1730 figure = &geometry->u.path.figures[idx_p->figure_idx];
1731 p[0] = &figure->vertices[idx_p->vertex_idx];
1732 p[1] = &figure->bezier_controls[idx_p->control_idx];
1733 next = idx_p->vertex_idx + 1;
1734 if (next == figure->vertex_count)
1735 next = 0;
1736 p[2] = &figure->vertices[next];
1738 figure = &geometry->u.path.figures[idx_q->figure_idx];
1739 q[0] = &figure->vertices[idx_q->vertex_idx];
1740 next = idx_q->vertex_idx + 1;
1741 if (next == figure->vertex_count)
1742 next = 0;
1743 q[1] = &figure->vertices[next];
1745 /* Align the line with x-axis. */
1746 theta = -atan2f(q[1]->y - q[0]->y, q[1]->x - q[0]->x);
1747 y[0] = (p[0]->x - q[0]->x) * sinf(theta) + (p[0]->y - q[0]->y) * cosf(theta);
1748 y[1] = (p[1]->x - q[0]->x) * sinf(theta) + (p[1]->y - q[0]->y) * cosf(theta);
1749 y[2] = (p[2]->x - q[0]->x) * sinf(theta) + (p[2]->y - q[0]->y) * cosf(theta);
1751 /* Intersect the transformed curve with the x-axis.
1753 * f(t) = (1 - t)²P₀ + 2(1 - t)tP₁ + t²P₂
1754 * = (P₀ - 2P₁ + P₂)t² + 2(P₁ - P₀)t + P₀
1756 * a = P₀ - 2P₁ + P₂
1757 * b = 2(P₁ - P₀)
1758 * c = P₀
1760 * f(t) = 0
1761 * t = (-b ± √(b² - 4ac)) / 2a
1762 * = (-2(P₁ - P₀) ± √((2(P₁ - P₀))² - 4((P₀ - 2P₁ + P₂)P₀))) / 2(P₀ - 2P₁ + P₂)
1763 * = (2P₀ - 2P₁ ± √(4P₀² + 4P₁² - 8P₀P₁ - 4P₀² + 8P₀P₁ - 4P₀P₂)) / (2P₀ - 4P₁ + 2P₂)
1764 * = (P₀ - P₁ ± √(P₁² - P₀P₂)) / (P₀ - 2P₁ + P₂) */
1766 d = y[0] - 2 * y[1] + y[2];
1767 if (d == 0.0f)
1769 /* P₀ - 2P₁ + P₂ = 0
1770 * f(t) = (P₀ - 2P₁ + P₂)t² + 2(P₁ - P₀)t + P₀ = 0
1771 * t = -P₀ / 2(P₁ - P₀) */
1772 root = -y[0] / (2.0f * (y[1] - y[0]));
1773 if (root < 0.0f || root > 1.0f)
1774 return TRUE;
1776 return d2d_geometry_add_bezier_line_intersections(geometry, intersections, idx_p, p, idx_q, q, root);
1779 e = y[1] * y[1] - y[0] * y[2];
1780 if (e < 0.0f)
1781 return TRUE;
1783 root = (y[0] - y[1] + sqrtf(e)) / d;
1784 if (root >= 0.0f && root <= 1.0f && !d2d_geometry_add_bezier_line_intersections(geometry,
1785 intersections, idx_p, p, idx_q, q, root))
1786 return FALSE;
1788 root = (y[0] - y[1] - sqrtf(e)) / d;
1789 if (root >= 0.0f && root <= 1.0f && !d2d_geometry_add_bezier_line_intersections(geometry,
1790 intersections, idx_p, p, idx_q, q, root))
1791 return FALSE;
1793 return TRUE;
1796 static BOOL d2d_geometry_intersect_bezier_bezier(struct d2d_geometry *geometry,
1797 struct d2d_geometry_intersections *intersections,
1798 const struct d2d_segment_idx *idx_p, float start_p, float end_p,
1799 const struct d2d_segment_idx *idx_q, float start_q, float end_q)
1801 const D2D1_POINT_2F *p[3], *q[3];
1802 const struct d2d_figure *figure;
1803 D2D_RECT_F p_bounds, q_bounds;
1804 D2D1_POINT_2F intersection;
1805 float centre_p, centre_q;
1806 size_t next;
1808 figure = &geometry->u.path.figures[idx_p->figure_idx];
1809 p[0] = &figure->vertices[idx_p->vertex_idx];
1810 p[1] = &figure->bezier_controls[idx_p->control_idx];
1811 next = idx_p->vertex_idx + 1;
1812 if (next == figure->vertex_count)
1813 next = 0;
1814 p[2] = &figure->vertices[next];
1816 figure = &geometry->u.path.figures[idx_q->figure_idx];
1817 q[0] = &figure->vertices[idx_q->vertex_idx];
1818 q[1] = &figure->bezier_controls[idx_q->control_idx];
1819 next = idx_q->vertex_idx + 1;
1820 if (next == figure->vertex_count)
1821 next = 0;
1822 q[2] = &figure->vertices[next];
1824 d2d_rect_get_bezier_segment_bounds(&p_bounds, p[0], p[1], p[2], start_p, end_p);
1825 d2d_rect_get_bezier_segment_bounds(&q_bounds, q[0], q[1], q[2], start_q, end_q);
1827 if (!d2d_rect_check_overlap(&p_bounds, &q_bounds))
1828 return TRUE;
1830 centre_p = (start_p + end_p) / 2.0f;
1831 centre_q = (start_q + end_q) / 2.0f;
1833 if (end_p - start_p < 1e-3f)
1835 d2d_point_calculate_bezier(&intersection, p[0], p[1], p[2], centre_p);
1836 if (start_p > 0.0f && end_p < 1.0f && !d2d_geometry_intersections_add(intersections,
1837 idx_p, centre_p, intersection))
1838 return FALSE;
1839 if (start_q > 0.0f && end_q < 1.0f && !d2d_geometry_intersections_add(intersections,
1840 idx_q, centre_q, intersection))
1841 return FALSE;
1842 return TRUE;
1845 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1846 idx_p, start_p, centre_p, idx_q, start_q, centre_q))
1847 return FALSE;
1848 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1849 idx_p, start_p, centre_p, idx_q, centre_q, end_q))
1850 return FALSE;
1851 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1852 idx_p, centre_p, end_p, idx_q, start_q, centre_q))
1853 return FALSE;
1854 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1855 idx_p, centre_p, end_p, idx_q, centre_q, end_q))
1856 return FALSE;
1858 return TRUE;
1861 static BOOL d2d_geometry_apply_intersections(struct d2d_geometry *geometry,
1862 struct d2d_geometry_intersections *intersections)
1864 size_t vertex_offset, control_offset, next, i;
1865 struct d2d_geometry_intersection *inter;
1866 enum d2d_vertex_type vertex_type;
1867 const D2D1_POINT_2F *p[3];
1868 struct d2d_figure *figure;
1869 D2D1_POINT_2F q[2];
1870 float t, t_prev;
1872 for (i = 0; i < intersections->intersection_count; ++i)
1874 inter = &intersections->intersections[i];
1875 if (!i || inter->figure_idx != intersections->intersections[i - 1].figure_idx)
1876 vertex_offset = control_offset = 0;
1878 figure = &geometry->u.path.figures[inter->figure_idx];
1879 vertex_type = figure->vertex_types[inter->vertex_idx + vertex_offset];
1880 if (vertex_type != D2D_VERTEX_TYPE_BEZIER && vertex_type != D2D_VERTEX_TYPE_SPLIT_BEZIER)
1882 if (!d2d_figure_insert_vertex(&geometry->u.path.figures[inter->figure_idx],
1883 inter->vertex_idx + vertex_offset + 1, inter->p))
1884 return FALSE;
1885 ++vertex_offset;
1886 continue;
1889 t = inter->t;
1890 if (i && inter->figure_idx == intersections->intersections[i - 1].figure_idx
1891 && inter->vertex_idx == intersections->intersections[i - 1].vertex_idx)
1893 t_prev = intersections->intersections[i - 1].t;
1894 if (t - t_prev < 1e-3f)
1896 inter->t = intersections->intersections[i - 1].t;
1897 continue;
1899 t = (t - t_prev) / (1.0f - t_prev);
1902 p[0] = &figure->vertices[inter->vertex_idx + vertex_offset];
1903 p[1] = &figure->bezier_controls[inter->control_idx + control_offset];
1904 next = inter->vertex_idx + vertex_offset + 1;
1905 if (next == figure->vertex_count)
1906 next = 0;
1907 p[2] = &figure->vertices[next];
1909 d2d_point_lerp(&q[0], p[0], p[1], t);
1910 d2d_point_lerp(&q[1], p[1], p[2], t);
1912 figure->bezier_controls[inter->control_idx + control_offset] = q[0];
1913 if (!(d2d_figure_insert_bezier_control(figure, inter->control_idx + control_offset + 1, &q[1])))
1914 return FALSE;
1915 ++control_offset;
1917 if (!(d2d_figure_insert_vertex(figure, inter->vertex_idx + vertex_offset + 1, inter->p)))
1918 return FALSE;
1919 figure->vertex_types[inter->vertex_idx + vertex_offset + 1] = D2D_VERTEX_TYPE_SPLIT_BEZIER;
1920 ++vertex_offset;
1923 return TRUE;
1926 /* Intersect the geometry's segments with themselves. This uses the
1927 * straightforward approach of testing everything against everything, but
1928 * there certainly exist more scalable algorithms for this. */
1929 static BOOL d2d_geometry_intersect_self(struct d2d_geometry *geometry)
1931 struct d2d_geometry_intersections intersections = {0};
1932 const struct d2d_figure *figure_p, *figure_q;
1933 struct d2d_segment_idx idx_p, idx_q;
1934 enum d2d_vertex_type type_p, type_q;
1935 BOOL ret = FALSE;
1936 size_t max_q;
1938 if (!geometry->u.path.figure_count)
1939 return TRUE;
1941 for (idx_p.figure_idx = 0; idx_p.figure_idx < geometry->u.path.figure_count; ++idx_p.figure_idx)
1943 figure_p = &geometry->u.path.figures[idx_p.figure_idx];
1944 idx_p.control_idx = 0;
1945 for (idx_p.vertex_idx = 0; idx_p.vertex_idx < figure_p->vertex_count; ++idx_p.vertex_idx)
1947 type_p = figure_p->vertex_types[idx_p.vertex_idx];
1948 for (idx_q.figure_idx = 0; idx_q.figure_idx <= idx_p.figure_idx; ++idx_q.figure_idx)
1950 figure_q = &geometry->u.path.figures[idx_q.figure_idx];
1951 if (idx_q.figure_idx != idx_p.figure_idx)
1953 if (!d2d_rect_check_overlap(&figure_p->bounds, &figure_q->bounds))
1954 continue;
1955 max_q = figure_q->vertex_count;
1957 else
1959 max_q = idx_p.vertex_idx;
1962 idx_q.control_idx = 0;
1963 for (idx_q.vertex_idx = 0; idx_q.vertex_idx < max_q; ++idx_q.vertex_idx)
1965 type_q = figure_q->vertex_types[idx_q.vertex_idx];
1966 if (type_q == D2D_VERTEX_TYPE_BEZIER)
1968 if (type_p == D2D_VERTEX_TYPE_BEZIER)
1970 if (!d2d_geometry_intersect_bezier_bezier(geometry, &intersections,
1971 &idx_p, 0.0f, 1.0f, &idx_q, 0.0f, 1.0f))
1972 goto done;
1974 else
1976 if (!d2d_geometry_intersect_bezier_line(geometry, &intersections, &idx_q, &idx_p))
1977 goto done;
1979 ++idx_q.control_idx;
1981 else
1983 if (type_p == D2D_VERTEX_TYPE_BEZIER)
1985 if (!d2d_geometry_intersect_bezier_line(geometry, &intersections, &idx_p, &idx_q))
1986 goto done;
1988 else
1990 if (!d2d_geometry_intersect_line_line(geometry, &intersections, &idx_p, &idx_q))
1991 goto done;
1996 if (type_p == D2D_VERTEX_TYPE_BEZIER)
1997 ++idx_p.control_idx;
2001 qsort(intersections.intersections, intersections.intersection_count,
2002 sizeof(*intersections.intersections), d2d_geometry_intersections_compare);
2003 ret = d2d_geometry_apply_intersections(geometry, &intersections);
2005 done:
2006 heap_free(intersections.intersections);
2007 return ret;
2010 static HRESULT d2d_path_geometry_triangulate(struct d2d_geometry *geometry)
2012 struct d2d_cdt_edge_ref left_edge, right_edge;
2013 size_t vertex_count, i, j;
2014 struct d2d_cdt cdt = {0};
2015 D2D1_POINT_2F *vertices;
2017 for (i = 0, vertex_count = 0; i < geometry->u.path.figure_count; ++i)
2019 vertex_count += geometry->u.path.figures[i].vertex_count;
2022 if (vertex_count < 3)
2024 WARN("Geometry has %lu vertices.\n", (long)vertex_count);
2025 return S_OK;
2028 if (!(vertices = heap_calloc(vertex_count, sizeof(*vertices))))
2029 return E_OUTOFMEMORY;
2031 for (i = 0, j = 0; i < geometry->u.path.figure_count; ++i)
2033 memcpy(&vertices[j], geometry->u.path.figures[i].vertices,
2034 geometry->u.path.figures[i].vertex_count * sizeof(*vertices));
2035 j += geometry->u.path.figures[i].vertex_count;
2038 /* Sort vertices, eliminate duplicates. */
2039 qsort(vertices, vertex_count, sizeof(*vertices), d2d_cdt_compare_vertices);
2040 for (i = 1; i < vertex_count; ++i)
2042 if (!memcmp(&vertices[i - 1], &vertices[i], sizeof(*vertices)))
2044 --vertex_count;
2045 memmove(&vertices[i], &vertices[i + 1], (vertex_count - i) * sizeof(*vertices));
2046 --i;
2050 geometry->fill.vertices = vertices;
2051 geometry->fill.vertex_count = vertex_count;
2053 cdt.free_edge = ~0u;
2054 cdt.vertices = vertices;
2055 if (!d2d_cdt_triangulate(&cdt, 0, vertex_count, &left_edge, &right_edge))
2056 goto fail;
2057 if (!d2d_cdt_insert_segments(&cdt, geometry))
2058 goto fail;
2059 if (!d2d_cdt_generate_faces(&cdt, geometry))
2060 goto fail;
2062 heap_free(cdt.edges);
2063 return S_OK;
2065 fail:
2066 geometry->fill.vertices = NULL;
2067 geometry->fill.vertex_count = 0;
2068 heap_free(vertices);
2069 heap_free(cdt.edges);
2070 return E_FAIL;
2073 static BOOL d2d_path_geometry_add_figure(struct d2d_geometry *geometry)
2075 struct d2d_figure *figure;
2077 if (!d2d_array_reserve((void **)&geometry->u.path.figures, &geometry->u.path.figures_size,
2078 geometry->u.path.figure_count + 1, sizeof(*geometry->u.path.figures)))
2080 ERR("Failed to grow figures array.\n");
2081 return FALSE;
2084 figure = &geometry->u.path.figures[geometry->u.path.figure_count];
2085 memset(figure, 0, sizeof(*figure));
2086 figure->bounds.left = FLT_MAX;
2087 figure->bounds.top = FLT_MAX;
2088 figure->bounds.right = -FLT_MAX;
2089 figure->bounds.bottom = -FLT_MAX;
2091 ++geometry->u.path.figure_count;
2092 return TRUE;
2095 static BOOL d2d_geometry_outline_add_join(struct d2d_geometry *geometry,
2096 const D2D1_POINT_2F *prev, const D2D1_POINT_2F *p0, const D2D1_POINT_2F *next)
2098 D2D1_POINT_2F q_prev, q_next;
2099 struct d2d_outline_vertex *v;
2100 struct d2d_face *f;
2101 size_t base_idx;
2102 float ccw;
2104 if (!d2d_array_reserve((void **)&geometry->outline.vertices, &geometry->outline.vertices_size,
2105 geometry->outline.vertex_count + 4, sizeof(*geometry->outline.vertices)))
2107 ERR("Failed to grow outline vertices array.\n");
2108 return FALSE;
2110 base_idx = geometry->outline.vertex_count;
2111 v = &geometry->outline.vertices[base_idx];
2113 if (!d2d_array_reserve((void **)&geometry->outline.faces, &geometry->outline.faces_size,
2114 geometry->outline.face_count + 2, sizeof(*geometry->outline.faces)))
2116 ERR("Failed to grow outline faces array.\n");
2117 return FALSE;
2119 f = &geometry->outline.faces[geometry->outline.face_count];
2121 d2d_point_subtract(&q_prev, p0, prev);
2122 d2d_point_subtract(&q_next, next, p0);
2124 d2d_point_normalise(&q_prev);
2125 d2d_point_normalise(&q_next);
2127 ccw = d2d_point_ccw(p0, prev, next);
2128 if (ccw == 0.0f)
2130 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2131 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2132 d2d_outline_vertex_set(&v[2], p0->x + 25.0f * q_prev.x, p0->y + 25.0f * q_prev.y,
2133 -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2134 d2d_outline_vertex_set(&v[3], p0->x + 25.0f * q_prev.x, p0->y + 25.0f * q_prev.y,
2135 q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2137 else if (ccw < 0.0f)
2139 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_next.x, q_next.y, q_prev.x, q_prev.y);
2140 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2141 d2d_outline_vertex_set(&v[2], p0->x, p0->y, -q_next.x, -q_next.y, -q_prev.x, -q_prev.y);
2142 d2d_outline_vertex_set(&v[3], p0->x, p0->y, -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2144 else
2146 d2d_outline_vertex_set(&v[0], p0->x, p0->y, -q_prev.x, -q_prev.y, -q_next.x, -q_next.y);
2147 d2d_outline_vertex_set(&v[1], p0->x, p0->y, q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2148 d2d_outline_vertex_set(&v[2], p0->x, p0->y, q_prev.x, q_prev.y, q_next.x, q_next.y);
2149 d2d_outline_vertex_set(&v[3], p0->x, p0->y, q_next.x, q_next.y, q_next.x, q_next.y);
2151 geometry->outline.vertex_count += 4;
2153 d2d_face_set(&f[0], base_idx + 1, base_idx + 0, base_idx + 2);
2154 d2d_face_set(&f[1], base_idx + 2, base_idx + 0, base_idx + 3);
2155 geometry->outline.face_count += 2;
2157 return TRUE;
2160 static BOOL d2d_geometry_outline_add_line_segment(struct d2d_geometry *geometry,
2161 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *next)
2163 struct d2d_outline_vertex *v;
2164 D2D1_POINT_2F q_next;
2165 struct d2d_face *f;
2166 size_t base_idx;
2168 if (!d2d_array_reserve((void **)&geometry->outline.vertices, &geometry->outline.vertices_size,
2169 geometry->outline.vertex_count + 4, sizeof(*geometry->outline.vertices)))
2171 ERR("Failed to grow outline vertices array.\n");
2172 return FALSE;
2174 base_idx = geometry->outline.vertex_count;
2175 v = &geometry->outline.vertices[base_idx];
2177 if (!d2d_array_reserve((void **)&geometry->outline.faces, &geometry->outline.faces_size,
2178 geometry->outline.face_count + 2, sizeof(*geometry->outline.faces)))
2180 ERR("Failed to grow outline faces array.\n");
2181 return FALSE;
2183 f = &geometry->outline.faces[geometry->outline.face_count];
2185 d2d_point_subtract(&q_next, next, p0);
2186 d2d_point_normalise(&q_next);
2188 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_next.x, q_next.y, q_next.x, q_next.y);
2189 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2190 d2d_outline_vertex_set(&v[2], next->x, next->y, q_next.x, q_next.y, q_next.x, q_next.y);
2191 d2d_outline_vertex_set(&v[3], next->x, next->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2192 geometry->outline.vertex_count += 4;
2194 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2195 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2196 geometry->outline.face_count += 2;
2198 return TRUE;
2201 static BOOL d2d_geometry_outline_add_bezier_segment(struct d2d_geometry *geometry,
2202 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
2204 struct d2d_bezier_outline_vertex *b;
2205 D2D1_POINT_2F r0, r1, r2;
2206 D2D1_POINT_2F q0, q1, q2;
2207 struct d2d_face *f;
2208 size_t base_idx;
2210 if (!d2d_array_reserve((void **)&geometry->outline.beziers, &geometry->outline.beziers_size,
2211 geometry->outline.bezier_count + 7, sizeof(*geometry->outline.beziers)))
2213 ERR("Failed to grow outline beziers array.\n");
2214 return FALSE;
2216 base_idx = geometry->outline.bezier_count;
2217 b = &geometry->outline.beziers[base_idx];
2219 if (!d2d_array_reserve((void **)&geometry->outline.bezier_faces, &geometry->outline.bezier_faces_size,
2220 geometry->outline.bezier_face_count + 5, sizeof(*geometry->outline.bezier_faces)))
2222 ERR("Failed to grow outline faces array.\n");
2223 return FALSE;
2225 f = &geometry->outline.bezier_faces[geometry->outline.bezier_face_count];
2227 d2d_point_lerp(&q0, p0, p1, 0.5f);
2228 d2d_point_lerp(&q1, p1, p2, 0.5f);
2229 d2d_point_lerp(&q2, &q0, &q1, 0.5f);
2231 d2d_point_subtract(&r0, &q0, p0);
2232 d2d_point_subtract(&r1, &q1, &q0);
2233 d2d_point_subtract(&r2, p2, &q1);
2235 d2d_point_normalise(&r0);
2236 d2d_point_normalise(&r1);
2237 d2d_point_normalise(&r2);
2239 if (d2d_point_ccw(p0, p1, p2) > 0.0f)
2241 d2d_point_scale(&r0, -1.0f);
2242 d2d_point_scale(&r1, -1.0f);
2243 d2d_point_scale(&r2, -1.0f);
2246 d2d_bezier_outline_vertex_set(&b[0], p0, p0, p1, p2, r0.x, r0.y, r0.x, r0.y);
2247 d2d_bezier_outline_vertex_set(&b[1], p0, p0, p1, p2, -r0.x, -r0.y, -r0.x, -r0.y);
2248 d2d_bezier_outline_vertex_set(&b[2], &q0, p0, p1, p2, r0.x, r0.y, r1.x, r1.y);
2249 d2d_bezier_outline_vertex_set(&b[3], &q2, p0, p1, p2, -r1.x, -r1.y, -r1.x, -r1.y);
2250 d2d_bezier_outline_vertex_set(&b[4], &q1, p0, p1, p2, r1.x, r1.y, r2.x, r2.y);
2251 d2d_bezier_outline_vertex_set(&b[5], p2, p0, p1, p2, -r2.x, -r2.y, -r2.x, -r2.y);
2252 d2d_bezier_outline_vertex_set(&b[6], p2, p0, p1, p2, r2.x, r2.y, r2.x, r2.y);
2253 geometry->outline.bezier_count += 7;
2255 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2256 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2257 d2d_face_set(&f[2], base_idx + 3, base_idx + 4, base_idx + 2);
2258 d2d_face_set(&f[3], base_idx + 5, base_idx + 4, base_idx + 3);
2259 d2d_face_set(&f[4], base_idx + 5, base_idx + 6, base_idx + 4);
2260 geometry->outline.bezier_face_count += 5;
2262 return TRUE;
2265 static BOOL d2d_geometry_add_figure_outline(struct d2d_geometry *geometry,
2266 struct d2d_figure *figure, D2D1_FIGURE_END figure_end)
2268 const D2D1_POINT_2F *prev, *p0, *next;
2269 enum d2d_vertex_type prev_type, type;
2270 size_t bezier_idx, i;
2272 for (i = 0, bezier_idx = 0; i < figure->vertex_count; ++i)
2274 type = figure->vertex_types[i];
2275 if (type == D2D_VERTEX_TYPE_NONE)
2276 continue;
2278 p0 = &figure->vertices[i];
2280 if (!i)
2282 prev_type = figure->vertex_types[figure->vertex_count - 1];
2283 if (prev_type == D2D_VERTEX_TYPE_BEZIER)
2284 prev = &figure->bezier_controls[figure->bezier_control_count - 1];
2285 else
2286 prev = &figure->vertices[figure->vertex_count - 1];
2288 else
2290 prev_type = figure->vertex_types[i - 1];
2291 if (prev_type == D2D_VERTEX_TYPE_BEZIER)
2292 prev = &figure->bezier_controls[bezier_idx - 1];
2293 else
2294 prev = &figure->vertices[i - 1];
2297 if (type == D2D_VERTEX_TYPE_BEZIER)
2298 next = &figure->bezier_controls[bezier_idx++];
2299 else if (i == figure->vertex_count - 1)
2300 next = &figure->vertices[0];
2301 else
2302 next = &figure->vertices[i + 1];
2304 if ((figure_end == D2D1_FIGURE_END_CLOSED || (i && i < figure->vertex_count - 1))
2305 && !d2d_geometry_outline_add_join(geometry, prev, p0, next))
2307 ERR("Failed to add join.\n");
2308 return FALSE;
2311 if (type == D2D_VERTEX_TYPE_LINE && (figure_end == D2D1_FIGURE_END_CLOSED || i < figure->vertex_count - 1)
2312 && !d2d_geometry_outline_add_line_segment(geometry, p0, next))
2314 ERR("Failed to add line segment.\n");
2315 return FALSE;
2317 else if (type == D2D_VERTEX_TYPE_BEZIER)
2319 const D2D1_POINT_2F *p2;
2321 if (i == figure->vertex_count - 1)
2322 p2 = &figure->vertices[0];
2323 else
2324 p2 = &figure->vertices[i + 1];
2326 if (!d2d_geometry_outline_add_bezier_segment(geometry, p0, next, p2))
2328 ERR("Failed to add bezier segment.\n");
2329 return FALSE;
2334 return TRUE;
2337 static void d2d_geometry_cleanup(struct d2d_geometry *geometry)
2339 heap_free(geometry->outline.bezier_faces);
2340 heap_free(geometry->outline.beziers);
2341 heap_free(geometry->outline.faces);
2342 heap_free(geometry->outline.vertices);
2343 heap_free(geometry->fill.bezier_vertices);
2344 heap_free(geometry->fill.faces);
2345 heap_free(geometry->fill.vertices);
2346 ID2D1Factory_Release(geometry->factory);
2349 static void d2d_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
2350 const D2D1_MATRIX_3X2_F *transform, const struct ID2D1GeometryVtbl *vtbl)
2352 geometry->ID2D1Geometry_iface.lpVtbl = vtbl;
2353 geometry->refcount = 1;
2354 ID2D1Factory_AddRef(geometry->factory = factory);
2355 geometry->transform = *transform;
2358 static inline struct d2d_geometry *impl_from_ID2D1GeometrySink(ID2D1GeometrySink *iface)
2360 return CONTAINING_RECORD(iface, struct d2d_geometry, u.path.ID2D1GeometrySink_iface);
2363 static HRESULT STDMETHODCALLTYPE d2d_geometry_sink_QueryInterface(ID2D1GeometrySink *iface, REFIID iid, void **out)
2365 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
2367 if (IsEqualGUID(iid, &IID_ID2D1GeometrySink)
2368 || IsEqualGUID(iid, &IID_ID2D1SimplifiedGeometrySink)
2369 || IsEqualGUID(iid, &IID_IUnknown))
2371 ID2D1GeometrySink_AddRef(iface);
2372 *out = iface;
2373 return S_OK;
2376 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
2378 *out = NULL;
2379 return E_NOINTERFACE;
2382 static ULONG STDMETHODCALLTYPE d2d_geometry_sink_AddRef(ID2D1GeometrySink *iface)
2384 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2386 TRACE("iface %p.\n", iface);
2388 return ID2D1Geometry_AddRef(&geometry->ID2D1Geometry_iface);
2391 static ULONG STDMETHODCALLTYPE d2d_geometry_sink_Release(ID2D1GeometrySink *iface)
2393 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2395 TRACE("iface %p.\n", iface);
2397 return ID2D1Geometry_Release(&geometry->ID2D1Geometry_iface);
2400 static void STDMETHODCALLTYPE d2d_geometry_sink_SetFillMode(ID2D1GeometrySink *iface, D2D1_FILL_MODE mode)
2402 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2404 TRACE("iface %p, mode %#x.\n", iface, mode);
2406 if (geometry->u.path.state == D2D_GEOMETRY_STATE_CLOSED)
2407 return;
2408 geometry->u.path.fill_mode = mode;
2411 static void STDMETHODCALLTYPE d2d_geometry_sink_SetSegmentFlags(ID2D1GeometrySink *iface, D2D1_PATH_SEGMENT flags)
2413 FIXME("iface %p, flags %#x stub!\n", iface, flags);
2416 static void STDMETHODCALLTYPE d2d_geometry_sink_BeginFigure(ID2D1GeometrySink *iface,
2417 D2D1_POINT_2F start_point, D2D1_FIGURE_BEGIN figure_begin)
2419 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2420 struct d2d_figure *figure;
2422 TRACE("iface %p, start_point {%.8e, %.8e}, figure_begin %#x.\n",
2423 iface, start_point.x, start_point.y, figure_begin);
2425 if (geometry->u.path.state != D2D_GEOMETRY_STATE_OPEN)
2427 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2428 return;
2431 if (figure_begin != D2D1_FIGURE_BEGIN_FILLED)
2432 FIXME("Ignoring figure_begin %#x.\n", figure_begin);
2434 if (!d2d_path_geometry_add_figure(geometry))
2436 ERR("Failed to add figure.\n");
2437 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2438 return;
2441 figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2442 if (figure_begin == D2D1_FIGURE_BEGIN_HOLLOW)
2443 figure->flags |= D2D_FIGURE_FLAG_HOLLOW;
2445 if (!d2d_figure_add_vertex(figure, start_point))
2447 ERR("Failed to add vertex.\n");
2448 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2449 return;
2452 geometry->u.path.state = D2D_GEOMETRY_STATE_FIGURE;
2455 static void STDMETHODCALLTYPE d2d_geometry_sink_AddLines(ID2D1GeometrySink *iface,
2456 const D2D1_POINT_2F *points, UINT32 count)
2458 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2459 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2460 unsigned int i;
2462 TRACE("iface %p, points %p, count %u.\n", iface, points, count);
2464 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2466 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2467 return;
2470 for (i = 0; i < count; ++i)
2472 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_LINE;
2473 if (!d2d_figure_add_vertex(figure, points[i]))
2475 ERR("Failed to add vertex.\n");
2476 return;
2480 geometry->u.path.segment_count += count;
2483 static void STDMETHODCALLTYPE d2d_geometry_sink_AddBeziers(ID2D1GeometrySink *iface,
2484 const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
2486 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2487 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2488 D2D1_POINT_2F p;
2489 unsigned int i;
2491 TRACE("iface %p, beziers %p, count %u.\n", iface, beziers, count);
2493 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2495 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2496 return;
2499 for (i = 0; i < count; ++i)
2501 D2D1_RECT_F bezier_bounds;
2503 /* FIXME: This tries to approximate a cubic bezier with a quadratic one. */
2504 p.x = (beziers[i].point1.x + beziers[i].point2.x) * 0.75f;
2505 p.y = (beziers[i].point1.y + beziers[i].point2.y) * 0.75f;
2506 p.x -= (figure->vertices[figure->vertex_count - 1].x + beziers[i].point3.x) * 0.25f;
2507 p.y -= (figure->vertices[figure->vertex_count - 1].y + beziers[i].point3.y) * 0.25f;
2508 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_BEZIER;
2510 d2d_rect_get_bezier_bounds(&bezier_bounds, &figure->vertices[figure->vertex_count - 1],
2511 &p, &beziers[i].point3);
2513 if (!d2d_figure_add_bezier_control(figure, &p))
2515 ERR("Failed to add bezier control.\n");
2516 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2517 return;
2520 if (!d2d_figure_add_vertex(figure, beziers[i].point3))
2522 ERR("Failed to add bezier vertex.\n");
2523 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2524 return;
2527 d2d_rect_union(&figure->bounds, &bezier_bounds);
2530 geometry->u.path.segment_count += count;
2533 static void STDMETHODCALLTYPE d2d_geometry_sink_EndFigure(ID2D1GeometrySink *iface, D2D1_FIGURE_END figure_end)
2535 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2536 struct d2d_figure *figure;
2538 TRACE("iface %p, figure_end %#x.\n", iface, figure_end);
2540 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2542 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2543 return;
2546 figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2547 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_LINE;
2548 if (figure_end == D2D1_FIGURE_END_CLOSED)
2550 ++geometry->u.path.segment_count;
2551 figure->flags |= D2D_FIGURE_FLAG_CLOSED;
2552 if (!memcmp(&figure->vertices[0], &figure->vertices[figure->vertex_count - 1], sizeof(*figure->vertices)))
2553 --figure->vertex_count;
2556 if (!d2d_geometry_add_figure_outline(geometry, figure, figure_end))
2558 ERR("Failed to add figure outline.\n");
2559 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2560 return;
2563 geometry->u.path.state = D2D_GEOMETRY_STATE_OPEN;
2566 static void d2d_path_geometry_free_figures(struct d2d_geometry *geometry)
2568 size_t i;
2570 if (!geometry->u.path.figures)
2571 return;
2573 for (i = 0; i < geometry->u.path.figure_count; ++i)
2575 heap_free(geometry->u.path.figures[i].bezier_controls);
2576 heap_free(geometry->u.path.figures[i].original_bezier_controls);
2577 heap_free(geometry->u.path.figures[i].vertices);
2579 heap_free(geometry->u.path.figures);
2580 geometry->u.path.figures = NULL;
2581 geometry->u.path.figures_size = 0;
2584 static BOOL d2d_geometry_get_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx, BOOL next)
2586 if (next)
2588 ++idx->vertex_idx;
2589 ++idx->control_idx;
2592 for (; idx->figure_idx < geometry->u.path.figure_count; ++idx->figure_idx, idx->vertex_idx = idx->control_idx = 0)
2594 struct d2d_figure *figure = &geometry->u.path.figures[idx->figure_idx];
2596 if (!figure->bezier_control_count)
2597 continue;
2599 for (; idx->vertex_idx < figure->vertex_count; ++idx->vertex_idx)
2601 if (figure->vertex_types[idx->vertex_idx] == D2D_VERTEX_TYPE_BEZIER
2602 || figure->vertex_types[idx->vertex_idx] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
2603 return TRUE;
2607 return FALSE;
2610 static BOOL d2d_geometry_get_first_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx)
2612 memset(idx, 0, sizeof(*idx));
2614 return d2d_geometry_get_bezier_segment_idx(geometry, idx, FALSE);
2617 static BOOL d2d_geometry_get_next_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx)
2619 return d2d_geometry_get_bezier_segment_idx(geometry, idx, TRUE);
2622 static BOOL d2d_geometry_check_bezier_overlap(struct d2d_geometry *geometry,
2623 const struct d2d_segment_idx *idx_p, const struct d2d_segment_idx *idx_q)
2625 const D2D1_POINT_2F *a[3], *b[3], *p[2], *q;
2626 const struct d2d_figure *figure;
2627 D2D1_POINT_2F v_q[3], v_p, v_qp;
2628 unsigned int i, j, score;
2629 float det, t;
2631 figure = &geometry->u.path.figures[idx_p->figure_idx];
2632 a[0] = &figure->vertices[idx_p->vertex_idx];
2633 a[1] = &figure->bezier_controls[idx_p->control_idx];
2634 if (idx_p->vertex_idx == figure->vertex_count - 1)
2635 a[2] = &figure->vertices[0];
2636 else
2637 a[2] = &figure->vertices[idx_p->vertex_idx + 1];
2639 figure = &geometry->u.path.figures[idx_q->figure_idx];
2640 b[0] = &figure->vertices[idx_q->vertex_idx];
2641 b[1] = &figure->bezier_controls[idx_q->control_idx];
2642 if (idx_q->vertex_idx == figure->vertex_count - 1)
2643 b[2] = &figure->vertices[0];
2644 else
2645 b[2] = &figure->vertices[idx_q->vertex_idx + 1];
2647 if (d2d_point_ccw(a[0], a[1], a[2]) == 0.0f || d2d_point_ccw(b[0], b[1], b[2]) == 0.0f)
2648 return FALSE;
2650 d2d_point_subtract(&v_q[0], b[1], b[0]);
2651 d2d_point_subtract(&v_q[1], b[2], b[0]);
2652 d2d_point_subtract(&v_q[2], b[1], b[2]);
2654 /* Check for intersections between the edges. Strictly speaking we'd only
2655 * need to check 8 of the 9 possible intersections, since if there's any
2656 * intersection there has to be a second intersection as well. */
2657 for (i = 0; i < 3; ++i)
2659 d2d_point_subtract(&v_p, a[(i & 1) + 1], a[i & 2]);
2660 for (j = 0; j < 3; ++j)
2662 det = v_p.x * v_q[j].y - v_p.y * v_q[j].x;
2663 if (det == 0.0f)
2664 continue;
2666 d2d_point_subtract(&v_qp, a[i & 2], b[j & 2]);
2667 t = (v_q[j].x * v_qp.y - v_q[j].y * v_qp.x) / det;
2668 if (t <= 0.0f || t >= 1.0f)
2669 continue;
2671 t = (v_p.x * v_qp.y - v_p.y * v_qp.x) / det;
2672 if (t <= 0.0f || t >= 1.0f)
2673 continue;
2675 return TRUE;
2679 /* Check if one triangle is contained within the other. */
2680 for (j = 0, score = 0, q = a[1], p[0] = b[2]; j < 3; ++j)
2682 p[1] = b[j];
2683 d2d_point_subtract(&v_p, p[1], p[0]);
2684 d2d_point_subtract(&v_qp, q, p[0]);
2686 if ((q->y < p[0]->y) != (q->y < p[1]->y) && v_qp.x < v_p.x * (v_qp.y / v_p.y))
2687 ++score;
2689 p[0] = p[1];
2692 if (score & 1)
2693 return TRUE;
2695 for (j = 0, score = 0, q = b[1], p[0] = a[2]; j < 3; ++j)
2697 p[1] = a[j];
2698 d2d_point_subtract(&v_p, p[1], p[0]);
2699 d2d_point_subtract(&v_qp, q, p[0]);
2701 if ((q->y < p[0]->y) != (q->y < p[1]->y) && v_qp.x < v_p.x * (v_qp.y / v_p.y))
2702 ++score;
2704 p[0] = p[1];
2707 return score & 1;
2710 static float d2d_geometry_bezier_ccw(struct d2d_geometry *geometry, const struct d2d_segment_idx *idx)
2712 const struct d2d_figure *figure = &geometry->u.path.figures[idx->figure_idx];
2713 size_t next = idx->vertex_idx + 1;
2715 if (next == figure->vertex_count)
2716 next = 0;
2718 return d2d_point_ccw(&figure->vertices[idx->vertex_idx],
2719 &figure->bezier_controls[idx->control_idx], &figure->vertices[next]);
2722 static BOOL d2d_geometry_split_bezier(struct d2d_geometry *geometry, const struct d2d_segment_idx *idx)
2724 const D2D1_POINT_2F *p[3];
2725 struct d2d_figure *figure;
2726 D2D1_POINT_2F q[3];
2727 size_t next;
2729 figure = &geometry->u.path.figures[idx->figure_idx];
2730 p[0] = &figure->vertices[idx->vertex_idx];
2731 p[1] = &figure->bezier_controls[idx->control_idx];
2732 next = idx->vertex_idx + 1;
2733 if (next == figure->vertex_count)
2734 next = 0;
2735 p[2] = &figure->vertices[next];
2737 d2d_point_lerp(&q[0], p[0], p[1], 0.5f);
2738 d2d_point_lerp(&q[1], p[1], p[2], 0.5f);
2739 d2d_point_lerp(&q[2], &q[0], &q[1], 0.5f);
2741 figure->bezier_controls[idx->control_idx] = q[0];
2742 if (!(d2d_figure_insert_bezier_control(figure, idx->control_idx + 1, &q[1])))
2743 return FALSE;
2744 if (!(d2d_figure_insert_vertex(figure, idx->vertex_idx + 1, q[2])))
2745 return FALSE;
2746 figure->vertex_types[idx->vertex_idx + 1] = D2D_VERTEX_TYPE_SPLIT_BEZIER;
2748 return TRUE;
2751 static HRESULT d2d_geometry_resolve_beziers(struct d2d_geometry *geometry)
2753 struct d2d_segment_idx idx_p, idx_q;
2754 struct d2d_bezier_vertex *b;
2755 const D2D1_POINT_2F *p[3];
2756 struct d2d_figure *figure;
2757 size_t bezier_idx, i;
2759 if (!d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_p))
2760 return S_OK;
2762 /* Split overlapping bezier control triangles. */
2763 while (d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_p))
2765 d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_q);
2766 while (idx_q.figure_idx < idx_p.figure_idx || idx_q.vertex_idx < idx_p.vertex_idx)
2768 while (d2d_geometry_check_bezier_overlap(geometry, &idx_p, &idx_q))
2770 if (fabsf(d2d_geometry_bezier_ccw(geometry, &idx_q)) > fabsf(d2d_geometry_bezier_ccw(geometry, &idx_p)))
2772 if (!d2d_geometry_split_bezier(geometry, &idx_q))
2773 return E_OUTOFMEMORY;
2774 if (idx_p.figure_idx == idx_q.figure_idx)
2776 ++idx_p.vertex_idx;
2777 ++idx_p.control_idx;
2780 else
2782 if (!d2d_geometry_split_bezier(geometry, &idx_p))
2783 return E_OUTOFMEMORY;
2786 d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_q);
2790 for (i = 0; i < geometry->u.path.figure_count; ++i)
2792 geometry->fill.bezier_vertex_count += 3 * geometry->u.path.figures[i].bezier_control_count;
2795 if (!(geometry->fill.bezier_vertices = heap_calloc(geometry->fill.bezier_vertex_count,
2796 sizeof(*geometry->fill.bezier_vertices))))
2798 ERR("Failed to allocate bezier vertices array.\n");
2799 geometry->fill.bezier_vertex_count = 0;
2800 return E_OUTOFMEMORY;
2803 bezier_idx = 0;
2804 d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_p);
2805 for (;;)
2807 float sign = -1.0f;
2809 figure = &geometry->u.path.figures[idx_p.figure_idx];
2810 p[0] = &figure->vertices[idx_p.vertex_idx];
2811 p[1] = &figure->bezier_controls[idx_p.control_idx];
2813 i = idx_p.vertex_idx + 1;
2814 if (d2d_path_geometry_point_inside(geometry, p[1], FALSE))
2816 sign = 1.0f;
2817 d2d_figure_insert_vertex(figure, i, *p[1]);
2818 /* Inserting a vertex potentially invalidates p[0]. */
2819 p[0] = &figure->vertices[idx_p.vertex_idx];
2820 ++i;
2823 if (i == figure->vertex_count)
2824 i = 0;
2825 p[2] = &figure->vertices[i];
2827 b = &geometry->fill.bezier_vertices[bezier_idx * 3];
2828 d2d_bezier_vertex_set(&b[0], p[0], 0.0f, 0.0f, sign);
2829 d2d_bezier_vertex_set(&b[1], p[1], 0.5f, 0.0f, sign);
2830 d2d_bezier_vertex_set(&b[2], p[2], 1.0f, 1.0f, sign);
2832 if (!d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_p))
2833 break;
2834 ++bezier_idx;
2837 return S_OK;
2840 static HRESULT STDMETHODCALLTYPE d2d_geometry_sink_Close(ID2D1GeometrySink *iface)
2842 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2843 HRESULT hr = E_FAIL;
2844 size_t i;
2846 TRACE("iface %p.\n", iface);
2848 if (geometry->u.path.state != D2D_GEOMETRY_STATE_OPEN)
2850 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
2851 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2852 return D2DERR_WRONG_STATE;
2854 geometry->u.path.state = D2D_GEOMETRY_STATE_CLOSED;
2856 for (i = 0; i < geometry->u.path.figure_count; ++i)
2858 struct d2d_figure *figure = &geometry->u.path.figures[i];
2859 size_t size = figure->bezier_control_count * sizeof(*figure->original_bezier_controls);
2860 if (!(figure->original_bezier_controls = heap_alloc(size)))
2861 goto done;
2862 memcpy(figure->original_bezier_controls, figure->bezier_controls, size);
2865 if (!d2d_geometry_intersect_self(geometry))
2866 goto done;
2867 if (FAILED(hr = d2d_geometry_resolve_beziers(geometry)))
2868 goto done;
2869 if (FAILED(hr = d2d_path_geometry_triangulate(geometry)))
2870 goto done;
2872 done:
2873 if (FAILED(hr))
2875 heap_free(geometry->fill.bezier_vertices);
2876 geometry->fill.bezier_vertex_count = 0;
2877 d2d_path_geometry_free_figures(geometry);
2878 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2880 return hr;
2883 static void STDMETHODCALLTYPE d2d_geometry_sink_AddLine(ID2D1GeometrySink *iface, D2D1_POINT_2F point)
2885 TRACE("iface %p, point {%.8e, %.8e}.\n", iface, point.x, point.y);
2887 d2d_geometry_sink_AddLines(iface, &point, 1);
2890 static void STDMETHODCALLTYPE d2d_geometry_sink_AddBezier(ID2D1GeometrySink *iface, const D2D1_BEZIER_SEGMENT *bezier)
2892 TRACE("iface %p, bezier %p.\n", iface, bezier);
2894 d2d_geometry_sink_AddBeziers(iface, bezier, 1);
2897 static void STDMETHODCALLTYPE d2d_geometry_sink_AddQuadraticBezier(ID2D1GeometrySink *iface,
2898 const D2D1_QUADRATIC_BEZIER_SEGMENT *bezier)
2900 TRACE("iface %p, bezier %p.\n", iface, bezier);
2902 ID2D1GeometrySink_AddQuadraticBeziers(iface, bezier, 1);
2905 static void STDMETHODCALLTYPE d2d_geometry_sink_AddQuadraticBeziers(ID2D1GeometrySink *iface,
2906 const D2D1_QUADRATIC_BEZIER_SEGMENT *beziers, UINT32 bezier_count)
2908 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2909 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2910 unsigned int i;
2912 TRACE("iface %p, beziers %p, bezier_count %u.\n", iface, beziers, bezier_count);
2914 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2916 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2917 return;
2920 for (i = 0; i < bezier_count; ++i)
2922 D2D1_RECT_F bezier_bounds;
2924 d2d_rect_get_bezier_bounds(&bezier_bounds, &figure->vertices[figure->vertex_count - 1],
2925 &beziers[i].point1, &beziers[i].point2);
2927 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_BEZIER;
2928 if (!d2d_figure_add_bezier_control(figure, &beziers[i].point1))
2930 ERR("Failed to add bezier.\n");
2931 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2932 return;
2935 if (!d2d_figure_add_vertex(figure, beziers[i].point2))
2937 ERR("Failed to add bezier vertex.\n");
2938 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2939 return;
2942 d2d_rect_union(&figure->bounds, &bezier_bounds);
2945 geometry->u.path.segment_count += bezier_count;
2948 static void STDMETHODCALLTYPE d2d_geometry_sink_AddArc(ID2D1GeometrySink *iface, const D2D1_ARC_SEGMENT *arc)
2950 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2952 FIXME("iface %p, arc %p stub!\n", iface, arc);
2954 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2956 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2957 return;
2960 if (!d2d_figure_add_vertex(&geometry->u.path.figures[geometry->u.path.figure_count - 1], arc->point))
2962 ERR("Failed to add vertex.\n");
2963 return;
2966 ++geometry->u.path.segment_count;
2969 static const struct ID2D1GeometrySinkVtbl d2d_geometry_sink_vtbl =
2971 d2d_geometry_sink_QueryInterface,
2972 d2d_geometry_sink_AddRef,
2973 d2d_geometry_sink_Release,
2974 d2d_geometry_sink_SetFillMode,
2975 d2d_geometry_sink_SetSegmentFlags,
2976 d2d_geometry_sink_BeginFigure,
2977 d2d_geometry_sink_AddLines,
2978 d2d_geometry_sink_AddBeziers,
2979 d2d_geometry_sink_EndFigure,
2980 d2d_geometry_sink_Close,
2981 d2d_geometry_sink_AddLine,
2982 d2d_geometry_sink_AddBezier,
2983 d2d_geometry_sink_AddQuadraticBezier,
2984 d2d_geometry_sink_AddQuadraticBeziers,
2985 d2d_geometry_sink_AddArc,
2988 static inline struct d2d_geometry *impl_from_ID2D1PathGeometry(ID2D1PathGeometry *iface)
2990 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
2993 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_QueryInterface(ID2D1PathGeometry *iface, REFIID iid, void **out)
2995 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
2997 if (IsEqualGUID(iid, &IID_ID2D1PathGeometry)
2998 || IsEqualGUID(iid, &IID_ID2D1Geometry)
2999 || IsEqualGUID(iid, &IID_ID2D1Resource)
3000 || IsEqualGUID(iid, &IID_IUnknown))
3002 ID2D1PathGeometry_AddRef(iface);
3003 *out = iface;
3004 return S_OK;
3007 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3009 *out = NULL;
3010 return E_NOINTERFACE;
3013 static ULONG STDMETHODCALLTYPE d2d_path_geometry_AddRef(ID2D1PathGeometry *iface)
3015 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3016 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3018 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3020 return refcount;
3023 static ULONG STDMETHODCALLTYPE d2d_path_geometry_Release(ID2D1PathGeometry *iface)
3025 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3026 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3028 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3030 if (!refcount)
3032 d2d_path_geometry_free_figures(geometry);
3033 d2d_geometry_cleanup(geometry);
3034 heap_free(geometry);
3037 return refcount;
3040 static void STDMETHODCALLTYPE d2d_path_geometry_GetFactory(ID2D1PathGeometry *iface, ID2D1Factory **factory)
3042 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3044 TRACE("iface %p, factory %p.\n", iface, factory);
3046 ID2D1Factory_AddRef(*factory = geometry->factory);
3049 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetBounds(ID2D1PathGeometry *iface,
3050 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3052 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3053 size_t i;
3055 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3057 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3058 return D2DERR_WRONG_STATE;
3060 bounds->left = FLT_MAX;
3061 bounds->top = FLT_MAX;
3062 bounds->right = -FLT_MAX;
3063 bounds->bottom = -FLT_MAX;
3065 if (!transform)
3067 if (geometry->u.path.bounds.left > geometry->u.path.bounds.right)
3069 for (i = 0; i < geometry->u.path.figure_count; ++i)
3070 d2d_rect_union(&geometry->u.path.bounds, &geometry->u.path.figures[i].bounds);
3073 *bounds = geometry->u.path.bounds;
3074 return S_OK;
3077 for (i = 0; i < geometry->u.path.figure_count; ++i)
3079 const struct d2d_figure *figure = &geometry->u.path.figures[i];
3080 enum d2d_vertex_type type = D2D_VERTEX_TYPE_NONE;
3081 D2D1_RECT_F bezier_bounds;
3082 D2D1_POINT_2F p, p1, p2;
3083 size_t j, bezier_idx;
3085 /* Single vertex figures are reduced by CloseFigure(). */
3086 if (figure->vertex_count == 0)
3088 d2d_point_transform(&p, transform, figure->bounds.left, figure->bounds.top);
3089 d2d_rect_expand(bounds, &p);
3090 continue;
3093 for (j = 0; j < figure->vertex_count; ++j)
3095 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
3096 continue;
3098 p = figure->vertices[j];
3099 type = figure->vertex_types[j];
3100 d2d_point_transform(&p, transform, p.x, p.y);
3101 d2d_rect_expand(bounds, &p);
3102 break;
3105 for (bezier_idx = 0, ++j; j < figure->vertex_count; ++j)
3107 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE
3108 || figure->vertex_types[j] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
3109 continue;
3111 switch (type)
3113 case D2D_VERTEX_TYPE_LINE:
3114 p = figure->vertices[j];
3115 d2d_point_transform(&p, transform, p.x, p.y);
3116 d2d_rect_expand(bounds, &p);
3117 break;
3119 case D2D_VERTEX_TYPE_BEZIER:
3120 p1 = figure->original_bezier_controls[bezier_idx++];
3121 d2d_point_transform(&p1, transform, p1.x, p1.y);
3122 p2 = figure->vertices[j];
3123 d2d_point_transform(&p2, transform, p2.x, p2.y);
3124 d2d_rect_get_bezier_bounds(&bezier_bounds, &p, &p1, &p2);
3125 d2d_rect_union(bounds, &bezier_bounds);
3126 p = p2;
3127 break;
3129 default:
3130 FIXME("Unhandled vertex type %#x.\n", type);
3131 p = figure->vertices[j];
3132 d2d_point_transform(&p, transform, p.x, p.y);
3133 d2d_rect_expand(bounds, &p);
3134 break;
3137 type = figure->vertex_types[j];
3140 if (type == D2D_VERTEX_TYPE_BEZIER)
3142 p1 = figure->original_bezier_controls[bezier_idx++];
3143 d2d_point_transform(&p1, transform, p1.x, p1.y);
3144 p2 = figure->vertices[0];
3145 d2d_point_transform(&p2, transform, p2.x, p2.y);
3146 d2d_rect_get_bezier_bounds(&bezier_bounds, &p, &p1, &p2);
3147 d2d_rect_union(bounds, &bezier_bounds);
3151 return S_OK;
3154 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetWidenedBounds(ID2D1PathGeometry *iface, float stroke_width,
3155 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_RECT_F *bounds)
3157 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3158 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3160 return E_NOTIMPL;
3163 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_StrokeContainsPoint(ID2D1PathGeometry *iface,
3164 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3165 float tolerance, BOOL *contains)
3167 FIXME("iface %p, point {%.8e, %.8e}, stroke_width %.8e, stroke_style %p, "
3168 "transform %p, tolerance %.8e, contains %p stub!\n",
3169 iface, point.x, point.y, stroke_width, stroke_style, transform, tolerance, contains);
3171 return E_NOTIMPL;
3174 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_FillContainsPoint(ID2D1PathGeometry *iface,
3175 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3177 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3178 D2D1_MATRIX_3X2_F g_i;
3180 TRACE("iface %p, point {%.8e, %.8e}, transform %p, tolerance %.8e, contains %p.\n",
3181 iface, point.x, point.y, transform, tolerance, contains);
3183 if (transform)
3185 if (!d2d_matrix_invert(&g_i, transform))
3186 return D2DERR_UNSUPPORTED_OPERATION;
3187 d2d_point_transform(&point, &g_i, point.x, point.y);
3190 *contains = !!d2d_path_geometry_point_inside(geometry, &point, FALSE);
3192 TRACE("-> %#x.\n", *contains);
3194 return S_OK;
3197 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_CompareWithGeometry(ID2D1PathGeometry *iface,
3198 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3200 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3201 iface, geometry, transform, tolerance, relation);
3203 return E_NOTIMPL;
3206 static void d2d_geometry_flatten_cubic(ID2D1SimplifiedGeometrySink *sink, const D2D1_POINT_2F *p0,
3207 const D2D1_BEZIER_SEGMENT *b, float tolerance)
3209 D2D1_BEZIER_SEGMENT b0, b1;
3210 D2D1_POINT_2F q;
3211 float d;
3213 /* It's certainly possible to calculate the maximum deviation of the
3214 * approximation from the curve, but it's a little involved. Instead, note
3215 * that if the control points were evenly spaced and collinear, p1 would
3216 * be exactly between p0 and p2, and p2 would be exactly between p1 and
3217 * p3. The deviation is a decent enough approximation, and much easier to
3218 * calculate.
3220 * p1' = (p0 + p2) / 2
3221 * p2' = (p1 + p3) / 2
3222 * d = ‖p1 - p1'‖₁ + ‖p2 - p2'‖₁ */
3223 d2d_point_lerp(&q, p0, &b->point2, 0.5f);
3224 d2d_point_subtract(&q, &b->point1, &q);
3225 d = fabsf(q.x) + fabsf(q.y);
3226 d2d_point_lerp(&q, &b->point1, &b->point3, 0.5f);
3227 d2d_point_subtract(&q, &b->point2, &q);
3228 d += fabsf(q.x) + fabsf(q.y);
3229 if (d < tolerance)
3231 ID2D1SimplifiedGeometrySink_AddLines(sink, &b->point3, 1);
3232 return;
3235 d2d_point_lerp(&q, &b->point1, &b->point2, 0.5f);
3237 b1.point3 = b->point3;
3238 d2d_point_lerp(&b1.point2, &b1.point3, &b->point2, 0.5f);
3239 d2d_point_lerp(&b1.point1, &b1.point2, &q, 0.5f);
3241 d2d_point_lerp(&b0.point1, p0, &b->point1, 0.5f);
3242 d2d_point_lerp(&b0.point2, &b0.point1, &q, 0.5f);
3243 d2d_point_lerp(&b0.point3, &b0.point2, &b1.point1, 0.5f);
3245 d2d_geometry_flatten_cubic(sink, p0, &b0, tolerance);
3246 ID2D1SimplifiedGeometrySink_SetSegmentFlags(sink, D2D1_PATH_SEGMENT_FORCE_ROUND_LINE_JOIN);
3247 d2d_geometry_flatten_cubic(sink, &b0.point3, &b1, tolerance);
3248 ID2D1SimplifiedGeometrySink_SetSegmentFlags(sink, D2D1_PATH_SEGMENT_NONE);
3251 static void d2d_geometry_simplify_quadratic(ID2D1SimplifiedGeometrySink *sink,
3252 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_POINT_2F *p0,
3253 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float tolerance)
3255 D2D1_BEZIER_SEGMENT b;
3257 d2d_point_lerp(&b.point1, p0, p1, 2.0f / 3.0f);
3258 d2d_point_lerp(&b.point2, p2, p1, 2.0f / 3.0f);
3259 b.point3 = *p2;
3261 if (option == D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES)
3262 d2d_geometry_flatten_cubic(sink, p0, &b, tolerance);
3263 else
3264 ID2D1SimplifiedGeometrySink_AddBeziers(sink, &b, 1);
3267 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Simplify(ID2D1PathGeometry *iface,
3268 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3269 ID2D1SimplifiedGeometrySink *sink)
3271 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3272 enum d2d_vertex_type type = D2D_VERTEX_TYPE_NONE;
3273 unsigned int i, j, bezier_idx;
3274 D2D1_FIGURE_BEGIN begin;
3275 D2D1_POINT_2F p, p1, p2;
3276 D2D1_FIGURE_END end;
3278 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
3279 iface, option, transform, tolerance, sink);
3281 ID2D1SimplifiedGeometrySink_SetFillMode(sink, geometry->u.path.fill_mode);
3282 for (i = 0; i < geometry->u.path.figure_count; ++i)
3284 const struct d2d_figure *figure = &geometry->u.path.figures[i];
3286 for (j = 0; j < figure->vertex_count; ++j)
3288 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
3289 continue;
3291 p = figure->vertices[j];
3292 if (transform)
3293 d2d_point_transform(&p, transform, p.x, p.y);
3294 begin = figure->flags & D2D_FIGURE_FLAG_HOLLOW ? D2D1_FIGURE_BEGIN_HOLLOW : D2D1_FIGURE_BEGIN_FILLED;
3295 ID2D1SimplifiedGeometrySink_BeginFigure(sink, p, begin);
3296 type = figure->vertex_types[j];
3297 break;
3300 for (bezier_idx = 0, ++j; j < figure->vertex_count; ++j)
3302 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE
3303 || figure->vertex_types[j] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
3304 continue;
3306 switch (type)
3308 case D2D_VERTEX_TYPE_LINE:
3309 p = figure->vertices[j];
3310 if (transform)
3311 d2d_point_transform(&p, transform, p.x, p.y);
3312 ID2D1SimplifiedGeometrySink_AddLines(sink, &p, 1);
3313 break;
3315 case D2D_VERTEX_TYPE_BEZIER:
3316 p1 = figure->original_bezier_controls[bezier_idx++];
3317 if (transform)
3318 d2d_point_transform(&p1, transform, p1.x, p1.y);
3319 p2 = figure->vertices[j];
3320 if (transform)
3321 d2d_point_transform(&p2, transform, p2.x, p2.y);
3322 d2d_geometry_simplify_quadratic(sink, option, &p, &p1, &p2, tolerance);
3323 p = p2;
3324 break;
3326 default:
3327 FIXME("Unhandled vertex type %#x.\n", type);
3328 p = figure->vertices[j];
3329 if (transform)
3330 d2d_point_transform(&p, transform, p.x, p.y);
3331 ID2D1SimplifiedGeometrySink_AddLines(sink, &p, 1);
3332 break;
3335 type = figure->vertex_types[j];
3338 if (type == D2D_VERTEX_TYPE_BEZIER)
3340 p1 = figure->original_bezier_controls[bezier_idx++];
3341 if (transform)
3342 d2d_point_transform(&p1, transform, p1.x, p1.y);
3343 p2 = figure->vertices[0];
3344 if (transform)
3345 d2d_point_transform(&p2, transform, p2.x, p2.y);
3346 d2d_geometry_simplify_quadratic(sink, option, &p, &p1, &p2, tolerance);
3349 end = figure->flags & D2D_FIGURE_FLAG_CLOSED ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN;
3350 ID2D1SimplifiedGeometrySink_EndFigure(sink, end);
3353 return S_OK;
3356 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Tessellate(ID2D1PathGeometry *iface,
3357 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3359 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3361 return E_NOTIMPL;
3364 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_CombineWithGeometry(ID2D1PathGeometry *iface,
3365 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3366 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3368 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3369 iface, geometry, combine_mode, transform, tolerance, sink);
3371 return E_NOTIMPL;
3374 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Outline(ID2D1PathGeometry *iface,
3375 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
3377 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3379 return E_NOTIMPL;
3382 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputeArea(ID2D1PathGeometry *iface,
3383 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
3385 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
3387 return E_NOTIMPL;
3390 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputeLength(ID2D1PathGeometry *iface,
3391 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
3393 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
3395 return E_NOTIMPL;
3398 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputePointAtLength(ID2D1PathGeometry *iface, float length,
3399 const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point, D2D1_POINT_2F *tangent)
3401 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
3402 iface, length, transform, tolerance, point, tangent);
3404 return E_NOTIMPL;
3407 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Widen(ID2D1PathGeometry *iface, float stroke_width,
3408 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3409 ID2D1SimplifiedGeometrySink *sink)
3411 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
3412 iface, stroke_width, stroke_style, transform, tolerance, sink);
3414 return E_NOTIMPL;
3417 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Open(ID2D1PathGeometry *iface, ID2D1GeometrySink **sink)
3419 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3421 TRACE("iface %p, sink %p.\n", iface, sink);
3423 if (geometry->u.path.state != D2D_GEOMETRY_STATE_INITIAL)
3424 return D2DERR_WRONG_STATE;
3426 *sink = &geometry->u.path.ID2D1GeometrySink_iface;
3427 ID2D1GeometrySink_AddRef(*sink);
3429 geometry->u.path.state = D2D_GEOMETRY_STATE_OPEN;
3431 return S_OK;
3434 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Stream(ID2D1PathGeometry *iface, ID2D1GeometrySink *sink)
3436 FIXME("iface %p, sink %p stub!\n", iface, sink);
3438 return E_NOTIMPL;
3441 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetSegmentCount(ID2D1PathGeometry *iface, UINT32 *count)
3443 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3445 TRACE("iface %p, count %p.\n", iface, count);
3447 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3448 return D2DERR_WRONG_STATE;
3450 *count = geometry->u.path.segment_count;
3452 return S_OK;
3455 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetFigureCount(ID2D1PathGeometry *iface, UINT32 *count)
3457 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3459 TRACE("iface %p, count %p.\n", iface, count);
3461 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3462 return D2DERR_WRONG_STATE;
3464 *count = geometry->u.path.figure_count;
3466 return S_OK;
3469 static const struct ID2D1PathGeometryVtbl d2d_path_geometry_vtbl =
3471 d2d_path_geometry_QueryInterface,
3472 d2d_path_geometry_AddRef,
3473 d2d_path_geometry_Release,
3474 d2d_path_geometry_GetFactory,
3475 d2d_path_geometry_GetBounds,
3476 d2d_path_geometry_GetWidenedBounds,
3477 d2d_path_geometry_StrokeContainsPoint,
3478 d2d_path_geometry_FillContainsPoint,
3479 d2d_path_geometry_CompareWithGeometry,
3480 d2d_path_geometry_Simplify,
3481 d2d_path_geometry_Tessellate,
3482 d2d_path_geometry_CombineWithGeometry,
3483 d2d_path_geometry_Outline,
3484 d2d_path_geometry_ComputeArea,
3485 d2d_path_geometry_ComputeLength,
3486 d2d_path_geometry_ComputePointAtLength,
3487 d2d_path_geometry_Widen,
3488 d2d_path_geometry_Open,
3489 d2d_path_geometry_Stream,
3490 d2d_path_geometry_GetSegmentCount,
3491 d2d_path_geometry_GetFigureCount,
3494 void d2d_path_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory)
3496 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl);
3497 geometry->u.path.ID2D1GeometrySink_iface.lpVtbl = &d2d_geometry_sink_vtbl;
3498 geometry->u.path.bounds.left = FLT_MAX;
3499 geometry->u.path.bounds.right = -FLT_MAX;
3500 geometry->u.path.bounds.top = FLT_MAX;
3501 geometry->u.path.bounds.bottom = -FLT_MAX;
3504 static inline struct d2d_geometry *impl_from_ID2D1RectangleGeometry(ID2D1RectangleGeometry *iface)
3506 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3509 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_QueryInterface(ID2D1RectangleGeometry *iface,
3510 REFIID iid, void **out)
3512 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3514 if (IsEqualGUID(iid, &IID_ID2D1RectangleGeometry)
3515 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3516 || IsEqualGUID(iid, &IID_ID2D1Resource)
3517 || IsEqualGUID(iid, &IID_IUnknown))
3519 ID2D1RectangleGeometry_AddRef(iface);
3520 *out = iface;
3521 return S_OK;
3524 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3526 *out = NULL;
3527 return E_NOINTERFACE;
3530 static ULONG STDMETHODCALLTYPE d2d_rectangle_geometry_AddRef(ID2D1RectangleGeometry *iface)
3532 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3533 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3535 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3537 return refcount;
3540 static ULONG STDMETHODCALLTYPE d2d_rectangle_geometry_Release(ID2D1RectangleGeometry *iface)
3542 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3543 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3545 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3547 if (!refcount)
3549 d2d_geometry_cleanup(geometry);
3550 heap_free(geometry);
3553 return refcount;
3556 static void STDMETHODCALLTYPE d2d_rectangle_geometry_GetFactory(ID2D1RectangleGeometry *iface, ID2D1Factory **factory)
3558 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3560 TRACE("iface %p, factory %p.\n", iface, factory);
3562 ID2D1Factory_AddRef(*factory = geometry->factory);
3565 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_GetBounds(ID2D1RectangleGeometry *iface,
3566 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3568 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3569 D2D1_RECT_F *rect;
3570 D2D1_POINT_2F p;
3572 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3574 rect = &geometry->u.rectangle.rect;
3575 if (!transform)
3577 *bounds = *rect;
3578 return S_OK;
3581 bounds->left = FLT_MAX;
3582 bounds->top = FLT_MAX;
3583 bounds->right = -FLT_MAX;
3584 bounds->bottom = -FLT_MAX;
3586 d2d_point_transform(&p, transform, rect->left, rect->top);
3587 d2d_rect_expand(bounds, &p);
3588 d2d_point_transform(&p, transform, rect->left, rect->bottom);
3589 d2d_rect_expand(bounds, &p);
3590 d2d_point_transform(&p, transform, rect->right, rect->bottom);
3591 d2d_rect_expand(bounds, &p);
3592 d2d_point_transform(&p, transform, rect->right, rect->top);
3593 d2d_rect_expand(bounds, &p);
3595 return S_OK;
3598 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_GetWidenedBounds(ID2D1RectangleGeometry *iface,
3599 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3600 float tolerance, D2D1_RECT_F *bounds)
3602 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3603 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3605 return E_NOTIMPL;
3608 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_StrokeContainsPoint(ID2D1RectangleGeometry *iface,
3609 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3610 float tolerance, BOOL *contains)
3612 FIXME("iface %p, point {%.8e, %.8e}, stroke_width %.8e, stroke_style %p, "
3613 "transform %p, tolerance %.8e, contains %p stub!\n",
3614 iface, point.x, point.y, stroke_width, stroke_style, transform, tolerance, contains);
3616 return E_NOTIMPL;
3619 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_FillContainsPoint(ID2D1RectangleGeometry *iface,
3620 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3622 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3623 D2D1_RECT_F *rect = &geometry->u.rectangle.rect;
3624 float dx, dy;
3626 TRACE("iface %p, point {%.8e, %.8e}, transform %p, tolerance %.8e, contains %p.\n",
3627 iface, point.x, point.y, transform, tolerance, contains);
3629 if (transform)
3631 D2D1_MATRIX_3X2_F g_i;
3633 if (!d2d_matrix_invert(&g_i, transform))
3634 return D2DERR_UNSUPPORTED_OPERATION;
3635 d2d_point_transform(&point, &g_i, point.x, point.y);
3638 if (tolerance == 0.0f)
3639 tolerance = D2D1_DEFAULT_FLATTENING_TOLERANCE;
3641 dx = max(fabsf((rect->right + rect->left) / 2.0f - point.x) - (rect->right - rect->left) / 2.0f, 0.0f);
3642 dy = max(fabsf((rect->bottom + rect->top) / 2.0f - point.y) - (rect->bottom - rect->top) / 2.0f, 0.0f);
3644 *contains = tolerance * tolerance > (dx * dx + dy * dy);
3645 return S_OK;
3648 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_CompareWithGeometry(ID2D1RectangleGeometry *iface,
3649 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3651 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3652 iface, geometry, transform, tolerance, relation);
3654 return E_NOTIMPL;
3657 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Simplify(ID2D1RectangleGeometry *iface,
3658 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3659 ID2D1SimplifiedGeometrySink *sink)
3661 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3662 D2D1_RECT_F *rect = &geometry->u.rectangle.rect;
3663 D2D1_POINT_2F p[4];
3664 unsigned int i;
3666 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
3667 iface, option, transform, tolerance, sink);
3669 d2d_point_set(&p[0], rect->left, rect->top);
3670 d2d_point_set(&p[1], rect->right, rect->top);
3671 d2d_point_set(&p[2], rect->right, rect->bottom);
3672 d2d_point_set(&p[3], rect->left, rect->bottom);
3674 if (transform)
3676 for (i = 0; i < ARRAY_SIZE(p); ++i)
3678 d2d_point_transform(&p[i], transform, p[i].x, p[i].y);
3682 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_ALTERNATE);
3683 ID2D1SimplifiedGeometrySink_BeginFigure(sink, p[0], D2D1_FIGURE_BEGIN_FILLED);
3684 ID2D1SimplifiedGeometrySink_AddLines(sink, &p[1], 3);
3685 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
3687 return S_OK;
3690 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Tessellate(ID2D1RectangleGeometry *iface,
3691 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3693 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3695 return E_NOTIMPL;
3698 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_CombineWithGeometry(ID2D1RectangleGeometry *iface,
3699 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3700 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3702 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3703 iface, geometry, combine_mode, transform, tolerance, sink);
3705 return E_NOTIMPL;
3708 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Outline(ID2D1RectangleGeometry *iface,
3709 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
3711 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3713 return E_NOTIMPL;
3716 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputeArea(ID2D1RectangleGeometry *iface,
3717 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
3719 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
3721 return E_NOTIMPL;
3724 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputeLength(ID2D1RectangleGeometry *iface,
3725 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
3727 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
3729 return E_NOTIMPL;
3732 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputePointAtLength(ID2D1RectangleGeometry *iface,
3733 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
3734 D2D1_POINT_2F *tangent)
3736 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
3737 iface, length, transform, tolerance, point, tangent);
3739 return E_NOTIMPL;
3742 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Widen(ID2D1RectangleGeometry *iface, float stroke_width,
3743 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3744 ID2D1SimplifiedGeometrySink *sink)
3746 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
3747 iface, stroke_width, stroke_style, transform, tolerance, sink);
3749 return E_NOTIMPL;
3752 static void STDMETHODCALLTYPE d2d_rectangle_geometry_GetRect(ID2D1RectangleGeometry *iface, D2D1_RECT_F *rect)
3754 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3756 TRACE("iface %p, rect %p.\n", iface, rect);
3758 *rect = geometry->u.rectangle.rect;
3761 static const struct ID2D1RectangleGeometryVtbl d2d_rectangle_geometry_vtbl =
3763 d2d_rectangle_geometry_QueryInterface,
3764 d2d_rectangle_geometry_AddRef,
3765 d2d_rectangle_geometry_Release,
3766 d2d_rectangle_geometry_GetFactory,
3767 d2d_rectangle_geometry_GetBounds,
3768 d2d_rectangle_geometry_GetWidenedBounds,
3769 d2d_rectangle_geometry_StrokeContainsPoint,
3770 d2d_rectangle_geometry_FillContainsPoint,
3771 d2d_rectangle_geometry_CompareWithGeometry,
3772 d2d_rectangle_geometry_Simplify,
3773 d2d_rectangle_geometry_Tessellate,
3774 d2d_rectangle_geometry_CombineWithGeometry,
3775 d2d_rectangle_geometry_Outline,
3776 d2d_rectangle_geometry_ComputeArea,
3777 d2d_rectangle_geometry_ComputeLength,
3778 d2d_rectangle_geometry_ComputePointAtLength,
3779 d2d_rectangle_geometry_Widen,
3780 d2d_rectangle_geometry_GetRect,
3783 HRESULT d2d_rectangle_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory, const D2D1_RECT_F *rect)
3785 struct d2d_face *f;
3786 D2D1_POINT_2F *v;
3787 float l, r, t, b;
3789 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl);
3790 geometry->u.rectangle.rect = *rect;
3792 if (!(geometry->fill.vertices = heap_alloc(4 * sizeof(*geometry->fill.vertices))))
3793 goto fail;
3794 if (!d2d_array_reserve((void **)&geometry->fill.faces,
3795 &geometry->fill.faces_size, 2, sizeof(*geometry->fill.faces)))
3796 goto fail;
3798 l = min(rect->left, rect->right);
3799 r = max(rect->left, rect->right);
3800 t = min(rect->top, rect->bottom);
3801 b = max(rect->top, rect->bottom);
3803 v = geometry->fill.vertices;
3804 d2d_point_set(&v[0], l, t);
3805 d2d_point_set(&v[1], l, b);
3806 d2d_point_set(&v[2], r, b);
3807 d2d_point_set(&v[3], r, t);
3808 geometry->fill.vertex_count = 4;
3810 f = geometry->fill.faces;
3811 d2d_face_set(&f[0], 1, 2, 0);
3812 d2d_face_set(&f[1], 0, 2, 3);
3813 geometry->fill.face_count = 2;
3815 if (!d2d_geometry_outline_add_line_segment(geometry, &v[0], &v[1]))
3816 goto fail;
3817 if (!d2d_geometry_outline_add_line_segment(geometry, &v[1], &v[2]))
3818 goto fail;
3819 if (!d2d_geometry_outline_add_line_segment(geometry, &v[2], &v[3]))
3820 goto fail;
3821 if (!d2d_geometry_outline_add_line_segment(geometry, &v[3], &v[0]))
3822 goto fail;
3824 if (!d2d_geometry_outline_add_join(geometry, &v[3], &v[0], &v[1]))
3825 goto fail;
3826 if (!d2d_geometry_outline_add_join(geometry, &v[0], &v[1], &v[2]))
3827 goto fail;
3828 if (!d2d_geometry_outline_add_join(geometry, &v[1], &v[2], &v[3]))
3829 goto fail;
3830 if (!d2d_geometry_outline_add_join(geometry, &v[2], &v[3], &v[0]))
3831 goto fail;
3833 return S_OK;
3835 fail:
3836 d2d_geometry_cleanup(geometry);
3837 return E_OUTOFMEMORY;
3840 static inline struct d2d_geometry *impl_from_ID2D1TransformedGeometry(ID2D1TransformedGeometry *iface)
3842 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3845 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_QueryInterface(ID2D1TransformedGeometry *iface,
3846 REFIID iid, void **out)
3848 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3850 if (IsEqualGUID(iid, &IID_ID2D1TransformedGeometry)
3851 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3852 || IsEqualGUID(iid, &IID_ID2D1Resource)
3853 || IsEqualGUID(iid, &IID_IUnknown))
3855 ID2D1TransformedGeometry_AddRef(iface);
3856 *out = iface;
3857 return S_OK;
3860 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3862 *out = NULL;
3863 return E_NOINTERFACE;
3866 static ULONG STDMETHODCALLTYPE d2d_transformed_geometry_AddRef(ID2D1TransformedGeometry *iface)
3868 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3869 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3871 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3873 return refcount;
3876 static ULONG STDMETHODCALLTYPE d2d_transformed_geometry_Release(ID2D1TransformedGeometry *iface)
3878 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3879 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3881 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3883 if (!refcount)
3885 geometry->outline.bezier_faces = NULL;
3886 geometry->outline.beziers = NULL;
3887 geometry->outline.faces = NULL;
3888 geometry->outline.vertices = NULL;
3889 geometry->fill.bezier_vertices = NULL;
3890 geometry->fill.faces = NULL;
3891 geometry->fill.vertices = NULL;
3892 ID2D1Geometry_Release(geometry->u.transformed.src_geometry);
3893 d2d_geometry_cleanup(geometry);
3894 heap_free(geometry);
3897 return refcount;
3900 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetFactory(ID2D1TransformedGeometry *iface,
3901 ID2D1Factory **factory)
3903 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3905 TRACE("iface %p, factory %p.\n", iface, factory);
3907 ID2D1Factory_AddRef(*factory = geometry->factory);
3910 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_GetBounds(ID2D1TransformedGeometry *iface,
3911 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3913 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3914 D2D1_MATRIX_3X2_F g;
3916 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3918 g = geometry->transform;
3919 if (transform)
3920 d2d_matrix_multiply(&g, transform);
3922 return ID2D1Geometry_GetBounds(geometry->u.transformed.src_geometry, &g, bounds);
3925 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_GetWidenedBounds(ID2D1TransformedGeometry *iface,
3926 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3927 float tolerance, D2D1_RECT_F *bounds)
3929 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3930 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3932 return E_NOTIMPL;
3935 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_StrokeContainsPoint(ID2D1TransformedGeometry *iface,
3936 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3937 float tolerance, BOOL *contains)
3939 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3940 D2D1_MATRIX_3X2_F g;
3942 TRACE("iface %p, point {%.8e, %.8e}, stroke_width %.8e, stroke_style %p, "
3943 "transform %p, tolerance %.8e, contains %p.\n",
3944 iface, point.x, point.y, stroke_width, stroke_style, transform, tolerance, contains);
3946 g = geometry->transform;
3947 if (transform)
3948 d2d_matrix_multiply(&g, transform);
3950 return ID2D1Geometry_StrokeContainsPoint(geometry->u.transformed.src_geometry, point, stroke_width, stroke_style,
3951 &g, tolerance, contains);
3954 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_FillContainsPoint(ID2D1TransformedGeometry *iface,
3955 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3957 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3958 D2D1_MATRIX_3X2_F g;
3960 TRACE("iface %p, point {%.8e, %.8e}, transform %p, tolerance %.8e, contains %p.\n",
3961 iface, point.x, point.y, transform, tolerance, contains);
3963 g = geometry->transform;
3964 if (transform)
3965 d2d_matrix_multiply(&g, transform);
3967 return ID2D1Geometry_FillContainsPoint(geometry->u.transformed.src_geometry, point, &g, tolerance, contains);
3970 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_CompareWithGeometry(ID2D1TransformedGeometry *iface,
3971 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3973 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3974 iface, geometry, transform, tolerance, relation);
3976 return E_NOTIMPL;
3979 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Simplify(ID2D1TransformedGeometry *iface,
3980 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3981 ID2D1SimplifiedGeometrySink *sink)
3983 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3984 D2D1_MATRIX_3X2_F g;
3986 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
3987 iface, option, transform, tolerance, sink);
3989 g = geometry->transform;
3990 if (transform)
3991 d2d_matrix_multiply(&g, transform);
3993 return ID2D1Geometry_Simplify(geometry->u.transformed.src_geometry, option, &g, tolerance, sink);
3996 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Tessellate(ID2D1TransformedGeometry *iface,
3997 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3999 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4001 return E_NOTIMPL;
4004 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_CombineWithGeometry(ID2D1TransformedGeometry *iface,
4005 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
4006 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4008 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4009 iface, geometry, combine_mode, transform, tolerance, sink);
4011 return E_NOTIMPL;
4014 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Outline(ID2D1TransformedGeometry *iface,
4015 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4017 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4019 return E_NOTIMPL;
4022 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputeArea(ID2D1TransformedGeometry *iface,
4023 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4025 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4027 return E_NOTIMPL;
4030 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputeLength(ID2D1TransformedGeometry *iface,
4031 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4033 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4035 return E_NOTIMPL;
4038 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputePointAtLength(ID2D1TransformedGeometry *iface,
4039 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
4040 D2D1_POINT_2F *tangent)
4042 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4043 iface, length, transform, tolerance, point, tangent);
4045 return E_NOTIMPL;
4048 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Widen(ID2D1TransformedGeometry *iface, float stroke_width,
4049 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4050 ID2D1SimplifiedGeometrySink *sink)
4052 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4053 iface, stroke_width, stroke_style, transform, tolerance, sink);
4055 return E_NOTIMPL;
4058 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetSourceGeometry(ID2D1TransformedGeometry *iface,
4059 ID2D1Geometry **src_geometry)
4061 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4063 TRACE("iface %p, src_geometry %p.\n", iface, src_geometry);
4065 ID2D1Geometry_AddRef(*src_geometry = geometry->u.transformed.src_geometry);
4068 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetTransform(ID2D1TransformedGeometry *iface,
4069 D2D1_MATRIX_3X2_F *transform)
4071 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4073 TRACE("iface %p, transform %p.\n", iface, transform);
4075 *transform = geometry->u.transformed.transform;
4078 static const struct ID2D1TransformedGeometryVtbl d2d_transformed_geometry_vtbl =
4080 d2d_transformed_geometry_QueryInterface,
4081 d2d_transformed_geometry_AddRef,
4082 d2d_transformed_geometry_Release,
4083 d2d_transformed_geometry_GetFactory,
4084 d2d_transformed_geometry_GetBounds,
4085 d2d_transformed_geometry_GetWidenedBounds,
4086 d2d_transformed_geometry_StrokeContainsPoint,
4087 d2d_transformed_geometry_FillContainsPoint,
4088 d2d_transformed_geometry_CompareWithGeometry,
4089 d2d_transformed_geometry_Simplify,
4090 d2d_transformed_geometry_Tessellate,
4091 d2d_transformed_geometry_CombineWithGeometry,
4092 d2d_transformed_geometry_Outline,
4093 d2d_transformed_geometry_ComputeArea,
4094 d2d_transformed_geometry_ComputeLength,
4095 d2d_transformed_geometry_ComputePointAtLength,
4096 d2d_transformed_geometry_Widen,
4097 d2d_transformed_geometry_GetSourceGeometry,
4098 d2d_transformed_geometry_GetTransform,
4101 void d2d_transformed_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
4102 ID2D1Geometry *src_geometry, const D2D_MATRIX_3X2_F *transform)
4104 struct d2d_geometry *src_impl;
4105 D2D_MATRIX_3X2_F g;
4107 src_impl = unsafe_impl_from_ID2D1Geometry(src_geometry);
4109 g = src_impl->transform;
4110 d2d_matrix_multiply(&g, transform);
4111 d2d_geometry_init(geometry, factory, &g, (ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl);
4112 ID2D1Geometry_AddRef(geometry->u.transformed.src_geometry = src_geometry);
4113 geometry->u.transformed.transform = *transform;
4114 geometry->fill = src_impl->fill;
4115 geometry->outline = src_impl->outline;
4118 struct d2d_geometry *unsafe_impl_from_ID2D1Geometry(ID2D1Geometry *iface)
4120 if (!iface)
4121 return NULL;
4122 assert(iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl
4123 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl
4124 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl);
4125 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);