d2d1: Implement ellipse drawing.
[wine.git] / dlls / d2d1 / geometry.c
blobe2a44c30b67119376352bebe6523ceb7e5e7a0ad
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 "d2d1_private.h"
20 #include <float.h>
22 WINE_DEFAULT_DEBUG_CHANNEL(d2d);
24 #define D2D_FIGURE_FLAG_CLOSED 0x00000001u
25 #define D2D_FIGURE_FLAG_HOLLOW 0x00000002u
27 #define D2D_CDT_EDGE_FLAG_FREED 0x80000000u
28 #define D2D_CDT_EDGE_FLAG_VISITED(r) (1u << (r))
30 #define D2D_FP_EPS (1.0f / (1 << FLT_MANT_DIG))
32 static const D2D1_MATRIX_3X2_F identity =
34 1.0f, 0.0f,
35 0.0f, 1.0f,
36 0.0f, 0.0f,
39 enum d2d_cdt_edge_next
41 D2D_EDGE_NEXT_ORIGIN = 0,
42 D2D_EDGE_NEXT_ROT = 1,
43 D2D_EDGE_NEXT_SYM = 2,
44 D2D_EDGE_NEXT_TOR = 3,
47 enum d2d_vertex_type
49 D2D_VERTEX_TYPE_NONE,
50 D2D_VERTEX_TYPE_LINE,
51 D2D_VERTEX_TYPE_BEZIER,
52 D2D_VERTEX_TYPE_SPLIT_BEZIER,
55 struct d2d_segment_idx
57 size_t figure_idx;
58 size_t vertex_idx;
59 size_t control_idx;
62 struct d2d_figure
64 D2D1_POINT_2F *vertices;
65 size_t vertices_size;
66 enum d2d_vertex_type *vertex_types;
67 size_t vertex_types_size;
68 size_t vertex_count;
70 D2D1_POINT_2F *bezier_controls;
71 size_t bezier_controls_size;
72 size_t bezier_control_count;
74 D2D1_POINT_2F *original_bezier_controls;
75 size_t original_bezier_control_count;
77 D2D1_RECT_F bounds;
78 unsigned int flags;
81 struct d2d_cdt_edge_ref
83 size_t idx;
84 enum d2d_cdt_edge_next r;
87 struct d2d_cdt_edge
89 struct d2d_cdt_edge_ref next[4];
90 size_t vertex[2];
91 unsigned int flags;
94 struct d2d_cdt
96 struct d2d_cdt_edge *edges;
97 size_t edges_size;
98 size_t edge_count;
99 size_t free_edge;
101 const D2D1_POINT_2F *vertices;
104 struct d2d_geometry_intersection
106 size_t figure_idx;
107 size_t vertex_idx;
108 size_t control_idx;
109 float t;
110 D2D1_POINT_2F p;
113 struct d2d_geometry_intersections
115 struct d2d_geometry_intersection *intersections;
116 size_t intersections_size;
117 size_t intersection_count;
120 struct d2d_fp_two_vec2
122 float x[2];
123 float y[2];
126 struct d2d_fp_fin
128 float *now, *other;
129 size_t length;
132 static void d2d_bezier_vertex_set(struct d2d_bezier_vertex *b,
133 const D2D1_POINT_2F *p, float u, float v, float sign)
135 b->position = *p;
136 b->texcoord.u = u;
137 b->texcoord.v = v;
138 b->texcoord.sign = sign;
141 static void d2d_face_set(struct d2d_face *f, UINT16 v0, UINT16 v1, UINT16 v2)
143 f->v[0] = v0;
144 f->v[1] = v1;
145 f->v[2] = v2;
148 static void d2d_outline_vertex_set(struct d2d_outline_vertex *v, float x, float y,
149 float prev_x, float prev_y, float next_x, float next_y)
151 d2d_point_set(&v->position, x, y);
152 d2d_point_set(&v->prev, prev_x, prev_y);
153 d2d_point_set(&v->next, next_x, next_y);
156 static void d2d_bezier_outline_vertex_set(struct d2d_bezier_outline_vertex *b, const D2D1_POINT_2F *position,
157 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2,
158 float prev_x, float prev_y, float next_x, float next_y)
160 b->position = *position;
161 b->p0 = *p0;
162 b->p1 = *p1;
163 b->p2 = *p2;
164 d2d_point_set(&b->prev, prev_x, prev_y);
165 d2d_point_set(&b->next, next_x, next_y);
168 static void d2d_fp_two_sum(float *out, float a, float b)
170 float a_virt, a_round, b_virt, b_round;
172 out[1] = a + b;
173 b_virt = out[1] - a;
174 a_virt = out[1] - b_virt;
175 b_round = b - b_virt;
176 a_round = a - a_virt;
177 out[0] = a_round + b_round;
180 static void d2d_fp_fast_two_sum(float *out, float a, float b)
182 float b_virt;
184 out[1] = a + b;
185 b_virt = out[1] - a;
186 out[0] = b - b_virt;
189 static void d2d_fp_two_two_sum(float *out, const float *a, const float *b)
191 float sum[2];
193 d2d_fp_two_sum(out, a[0], b[0]);
194 d2d_fp_two_sum(sum, a[1], out[1]);
195 d2d_fp_two_sum(&out[1], sum[0], b[1]);
196 d2d_fp_two_sum(&out[2], sum[1], out[2]);
199 static void d2d_fp_two_diff_tail(float *out, float a, float b, float x)
201 float a_virt, a_round, b_virt, b_round;
203 b_virt = a - x;
204 a_virt = x + b_virt;
205 b_round = b_virt - b;
206 a_round = a - a_virt;
207 *out = a_round + b_round;
210 static void d2d_fp_two_two_diff(float *out, const float *a, const float *b)
212 float sum[2], diff;
214 diff = a[0] - b[0];
215 d2d_fp_two_diff_tail(out, a[0], b[0], diff);
216 d2d_fp_two_sum(sum, a[1], diff);
217 diff = sum[0] - b[1];
218 d2d_fp_two_diff_tail(&out[1], sum[0], b[1], diff);
219 d2d_fp_two_sum(&out[2], sum[1], diff);
222 static void d2d_fp_split(float *out, float a)
224 float a_big, c;
226 c = a * ((1 << (FLT_MANT_DIG / 2)) + 1.0f);
227 a_big = c - a;
228 out[1] = c - a_big;
229 out[0] = a - out[1];
232 static void d2d_fp_two_product_presplit(float *out, float a, float b, const float *b_split)
234 float a_split[2], err1, err2, err3;
236 out[1] = a * b;
237 d2d_fp_split(a_split, a);
238 err1 = out[1] - (a_split[1] * b_split[1]);
239 err2 = err1 - (a_split[0] * b_split[1]);
240 err3 = err2 - (a_split[1] * b_split[0]);
241 out[0] = (a_split[0] * b_split[0]) - err3;
244 static void d2d_fp_two_product(float *out, float a, float b)
246 float b_split[2];
248 d2d_fp_split(b_split, b);
249 d2d_fp_two_product_presplit(out, a, b, b_split);
252 static void d2d_fp_square(float *out, float a)
254 float a_split[2], err1, err2;
256 out[1] = a * a;
257 d2d_fp_split(a_split, a);
258 err1 = out[1] - (a_split[1] * a_split[1]);
259 err2 = err1 - ((a_split[1] + a_split[1]) * a_split[0]);
260 out[0] = (a_split[0] * a_split[0]) - err2;
263 static float d2d_fp_estimate(float *a, size_t len)
265 float out = a[0];
266 size_t idx = 1;
268 while (idx < len)
269 out += a[idx++];
271 return out;
274 static void d2d_fp_fast_expansion_sum_zeroelim(float *out, size_t *out_len,
275 const float *a, size_t a_len, const float *b, size_t b_len)
277 float sum[2], q, a_curr, b_curr;
278 size_t a_idx, b_idx, out_idx;
280 a_curr = a[0];
281 b_curr = b[0];
282 a_idx = b_idx = 0;
283 if ((b_curr > a_curr) == (b_curr > -a_curr))
285 q = a_curr;
286 a_curr = a[++a_idx];
288 else
290 q = b_curr;
291 b_curr = b[++b_idx];
293 out_idx = 0;
294 if (a_idx < a_len && b_idx < b_len)
296 if ((b_curr > a_curr) == (b_curr > -a_curr))
298 d2d_fp_fast_two_sum(sum, a_curr, q);
299 a_curr = a[++a_idx];
301 else
303 d2d_fp_fast_two_sum(sum, b_curr, q);
304 b_curr = b[++b_idx];
306 if (sum[0] != 0.0f)
307 out[out_idx++] = sum[0];
308 q = sum[1];
309 while (a_idx < a_len && b_idx < b_len)
311 if ((b_curr > a_curr) == (b_curr > -a_curr))
313 d2d_fp_two_sum(sum, q, a_curr);
314 a_curr = a[++a_idx];
316 else
318 d2d_fp_two_sum(sum, q, b_curr);
319 b_curr = b[++b_idx];
321 if (sum[0] != 0.0f)
322 out[out_idx++] = sum[0];
323 q = sum[1];
326 while (a_idx < a_len)
328 d2d_fp_two_sum(sum, q, a_curr);
329 a_curr = a[++a_idx];
330 if (sum[0] != 0.0f)
331 out[out_idx++] = sum[0];
332 q = sum[1];
334 while (b_idx < b_len)
336 d2d_fp_two_sum(sum, q, b_curr);
337 b_curr = b[++b_idx];
338 if (sum[0] != 0.0f)
339 out[out_idx++] = sum[0];
340 q = sum[1];
342 if (q != 0.0f || !out_idx)
343 out[out_idx++] = q;
345 *out_len = out_idx;
348 static void d2d_fp_scale_expansion_zeroelim(float *out, size_t *out_len, const float *a, size_t a_len, float b)
350 float product[2], sum[2], b_split[2], q[2], a_curr;
351 size_t a_idx, out_idx;
353 d2d_fp_split(b_split, b);
354 d2d_fp_two_product_presplit(q, a[0], b, b_split);
355 out_idx = 0;
356 if (q[0] != 0.0f)
357 out[out_idx++] = q[0];
358 for (a_idx = 1; a_idx < a_len; ++a_idx)
360 a_curr = a[a_idx];
361 d2d_fp_two_product_presplit(product, a_curr, b, b_split);
362 d2d_fp_two_sum(sum, q[1], product[0]);
363 if (sum[0] != 0.0f)
364 out[out_idx++] = sum[0];
365 d2d_fp_fast_two_sum(q, product[1], sum[1]);
366 if (q[0] != 0.0f)
367 out[out_idx++] = q[0];
369 if (q[1] != 0.0f || !out_idx)
370 out[out_idx++] = q[1];
372 *out_len = out_idx;
375 static void d2d_point_subtract(D2D1_POINT_2F *out,
376 const D2D1_POINT_2F *a, const D2D1_POINT_2F *b)
378 out->x = a->x - b->x;
379 out->y = a->y - b->y;
382 static void d2d_point_scale(D2D1_POINT_2F *p, float scale)
384 p->x *= scale;
385 p->y *= scale;
388 static void d2d_point_lerp(D2D1_POINT_2F *out,
389 const D2D1_POINT_2F *a, const D2D1_POINT_2F *b, float t)
391 out->x = a->x * (1.0f - t) + b->x * t;
392 out->y = a->y * (1.0f - t) + b->y * t;
395 static void d2d_point_calculate_bezier(D2D1_POINT_2F *out, const D2D1_POINT_2F *p0,
396 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float t)
398 float t_c = 1.0f - t;
400 out->x = t_c * (t_c * p0->x + t * p1->x) + t * (t_c * p1->x + t * p2->x);
401 out->y = t_c * (t_c * p0->y + t * p1->y) + t * (t_c * p1->y + t * p2->y);
404 static void d2d_point_normalise(D2D1_POINT_2F *p)
406 float l;
408 if ((l = sqrtf(d2d_point_dot(p, p))) != 0.0f)
409 d2d_point_scale(p, 1.0f / l);
412 /* This implementation is based on the paper "Adaptive Precision
413 * Floating-Point Arithmetic and Fast Robust Geometric Predicates" and
414 * associated (Public Domain) code by Jonathan Richard Shewchuk. */
415 static float d2d_point_ccw(const D2D1_POINT_2F *a, const D2D1_POINT_2F *b, const D2D1_POINT_2F *c)
417 static const float err_bound_result = (3.0f + 8.0f * D2D_FP_EPS) * D2D_FP_EPS;
418 static const float err_bound_a = (3.0f + 16.0f * D2D_FP_EPS) * D2D_FP_EPS;
419 static const float err_bound_b = (2.0f + 12.0f * D2D_FP_EPS) * D2D_FP_EPS;
420 static const float err_bound_c = (9.0f + 64.0f * D2D_FP_EPS) * D2D_FP_EPS * D2D_FP_EPS;
421 float det_d[16], det_c2[12], det_c1[8], det_b[4], temp4[4], temp2a[2], temp2b[2], abxacy[2], abyacx[2];
422 size_t det_d_len, det_c2_len, det_c1_len;
423 float det, det_sum, err_bound;
424 struct d2d_fp_two_vec2 ab, ac;
426 ab.x[1] = b->x - a->x;
427 ab.y[1] = b->y - a->y;
428 ac.x[1] = c->x - a->x;
429 ac.y[1] = c->y - a->y;
431 abxacy[1] = ab.x[1] * ac.y[1];
432 abyacx[1] = ab.y[1] * ac.x[1];
433 det = abxacy[1] - abyacx[1];
435 if (abxacy[1] > 0.0f)
437 if (abyacx[1] <= 0.0f)
438 return det;
439 det_sum = abxacy[1] + abyacx[1];
441 else if (abxacy[1] < 0.0f)
443 if (abyacx[1] >= 0.0f)
444 return det;
445 det_sum = -abxacy[1] - abyacx[1];
447 else
449 return det;
452 err_bound = err_bound_a * det_sum;
453 if (det >= err_bound || -det >= err_bound)
454 return det;
456 d2d_fp_two_product(abxacy, ab.x[1], ac.y[1]);
457 d2d_fp_two_product(abyacx, ab.y[1], ac.x[1]);
458 d2d_fp_two_two_diff(det_b, abxacy, abyacx);
460 det = d2d_fp_estimate(det_b, 4);
461 err_bound = err_bound_b * det_sum;
462 if (det >= err_bound || -det >= err_bound)
463 return det;
465 d2d_fp_two_diff_tail(&ab.x[0], b->x, a->x, ab.x[1]);
466 d2d_fp_two_diff_tail(&ab.y[0], b->y, a->y, ab.y[1]);
467 d2d_fp_two_diff_tail(&ac.x[0], c->x, a->x, ac.x[1]);
468 d2d_fp_two_diff_tail(&ac.y[0], c->y, a->y, ac.y[1]);
470 if (ab.x[0] == 0.0f && ab.y[0] == 0.0f && ac.x[0] == 0.0f && ac.y[0] == 0.0f)
471 return det;
473 err_bound = err_bound_c * det_sum + err_bound_result * fabsf(det);
474 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]);
475 if (det >= err_bound || -det >= err_bound)
476 return det;
478 d2d_fp_two_product(temp2a, ab.x[0], ac.y[1]);
479 d2d_fp_two_product(temp2b, ab.y[0], ac.x[1]);
480 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
481 d2d_fp_fast_expansion_sum_zeroelim(det_c1, &det_c1_len, det_b, 4, temp4, 4);
483 d2d_fp_two_product(temp2a, ab.x[1], ac.y[0]);
484 d2d_fp_two_product(temp2b, ab.y[1], ac.x[0]);
485 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
486 d2d_fp_fast_expansion_sum_zeroelim(det_c2, &det_c2_len, det_c1, det_c1_len, temp4, 4);
488 d2d_fp_two_product(temp2a, ab.x[0], ac.y[0]);
489 d2d_fp_two_product(temp2b, ab.y[0], ac.x[0]);
490 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
491 d2d_fp_fast_expansion_sum_zeroelim(det_d, &det_d_len, det_c2, det_c2_len, temp4, 4);
493 return det_d[det_d_len - 1];
496 static void d2d_rect_union(D2D1_RECT_F *l, const D2D1_RECT_F *r)
498 l->left = min(l->left, r->left);
499 l->top = min(l->top, r->top);
500 l->right = max(l->right, r->right);
501 l->bottom = max(l->bottom, r->bottom);
504 static BOOL d2d_rect_check_overlap(const D2D_RECT_F *p, const D2D_RECT_F *q)
506 return p->left < q->right && p->top < q->bottom && p->right > q->left && p->bottom > q->top;
509 static void d2d_rect_get_bezier_bounds(D2D_RECT_F *bounds, const D2D1_POINT_2F *p0,
510 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
512 D2D1_POINT_2F p;
513 float root;
515 bounds->left = p0->x;
516 bounds->top = p0->y;
517 bounds->right = p0->x;
518 bounds->bottom = p0->y;
520 d2d_rect_expand(bounds, p2);
522 /* f(t) = (1 - t)²P₀ + 2(1 - t)tP₁ + t²P₂
523 * f'(t) = 2(1 - t)(P₁ - P₀) + 2t(P₂ - P₁)
524 * = 2(P₂ - 2P₁ + P₀)t + 2(P₁ - P₀)
526 * f'(t) = 0
527 * t = (P₀ - P₁) / (P₂ - 2P₁ + P₀) */
528 root = (p0->x - p1->x) / (p2->x - 2.0f * p1->x + p0->x);
529 if (root > 0.0f && root < 1.0f)
531 d2d_point_calculate_bezier(&p, p0, p1, p2, root);
532 d2d_rect_expand(bounds, &p);
535 root = (p0->y - p1->y) / (p2->y - 2.0f * p1->y + p0->y);
536 if (root > 0.0f && root < 1.0f)
538 d2d_point_calculate_bezier(&p, p0, p1, p2, root);
539 d2d_rect_expand(bounds, &p);
543 static void d2d_rect_get_bezier_segment_bounds(D2D_RECT_F *bounds, const D2D1_POINT_2F *p0,
544 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float start, float end)
546 D2D1_POINT_2F q[3], r[2];
548 d2d_point_lerp(&r[0], p0, p1, start);
549 d2d_point_lerp(&r[1], p1, p2, start);
550 d2d_point_lerp(&q[0], &r[0], &r[1], start);
552 end = (end - start) / (1.0f - start);
553 d2d_point_lerp(&q[1], &q[0], &r[1], end);
554 d2d_point_lerp(&r[0], &r[1], p2, end);
555 d2d_point_lerp(&q[2], &q[1], &r[0], end);
557 d2d_rect_get_bezier_bounds(bounds, &q[0], &q[1], &q[2]);
560 static BOOL d2d_figure_insert_vertex(struct d2d_figure *figure, size_t idx, D2D1_POINT_2F vertex)
562 if (!d2d_array_reserve((void **)&figure->vertices, &figure->vertices_size,
563 figure->vertex_count + 1, sizeof(*figure->vertices)))
565 ERR("Failed to grow vertices array.\n");
566 return FALSE;
569 if (!d2d_array_reserve((void **)&figure->vertex_types, &figure->vertex_types_size,
570 figure->vertex_count + 1, sizeof(*figure->vertex_types)))
572 ERR("Failed to grow vertex types array.\n");
573 return FALSE;
576 memmove(&figure->vertices[idx + 1], &figure->vertices[idx],
577 (figure->vertex_count - idx) * sizeof(*figure->vertices));
578 memmove(&figure->vertex_types[idx + 1], &figure->vertex_types[idx],
579 (figure->vertex_count - idx) * sizeof(*figure->vertex_types));
580 figure->vertices[idx] = vertex;
581 figure->vertex_types[idx] = D2D_VERTEX_TYPE_NONE;
582 d2d_rect_expand(&figure->bounds, &vertex);
583 ++figure->vertex_count;
584 return TRUE;
587 static BOOL d2d_figure_add_vertex(struct d2d_figure *figure, D2D1_POINT_2F vertex)
589 size_t last = figure->vertex_count - 1;
591 if (figure->vertex_count && figure->vertex_types[last] == D2D_VERTEX_TYPE_LINE
592 && !memcmp(&figure->vertices[last], &vertex, sizeof(vertex)))
593 return TRUE;
595 if (!d2d_array_reserve((void **)&figure->vertices, &figure->vertices_size,
596 figure->vertex_count + 1, sizeof(*figure->vertices)))
598 ERR("Failed to grow vertices array.\n");
599 return FALSE;
602 if (!d2d_array_reserve((void **)&figure->vertex_types, &figure->vertex_types_size,
603 figure->vertex_count + 1, sizeof(*figure->vertex_types)))
605 ERR("Failed to grow vertex types array.\n");
606 return FALSE;
609 figure->vertices[figure->vertex_count] = vertex;
610 figure->vertex_types[figure->vertex_count] = D2D_VERTEX_TYPE_NONE;
611 d2d_rect_expand(&figure->bounds, &vertex);
612 ++figure->vertex_count;
613 return TRUE;
616 static BOOL d2d_figure_insert_bezier_control(struct d2d_figure *figure, size_t idx, const D2D1_POINT_2F *p)
618 if (!d2d_array_reserve((void **)&figure->bezier_controls, &figure->bezier_controls_size,
619 figure->bezier_control_count + 1, sizeof(*figure->bezier_controls)))
621 ERR("Failed to grow bezier controls array.\n");
622 return FALSE;
625 memmove(&figure->bezier_controls[idx + 1], &figure->bezier_controls[idx],
626 (figure->bezier_control_count - idx) * sizeof(*figure->bezier_controls));
627 figure->bezier_controls[idx] = *p;
628 ++figure->bezier_control_count;
630 return TRUE;
633 static BOOL d2d_figure_add_bezier_control(struct d2d_figure *figure, const D2D1_POINT_2F *p)
635 if (!d2d_array_reserve((void **)&figure->bezier_controls, &figure->bezier_controls_size,
636 figure->bezier_control_count + 1, sizeof(*figure->bezier_controls)))
638 ERR("Failed to grow bezier controls array.\n");
639 return FALSE;
642 figure->bezier_controls[figure->bezier_control_count++] = *p;
644 return TRUE;
647 static void d2d_cdt_edge_rot(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
649 dst->idx = src->idx;
650 dst->r = (src->r + D2D_EDGE_NEXT_ROT) & 3;
653 static void d2d_cdt_edge_sym(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
655 dst->idx = src->idx;
656 dst->r = (src->r + D2D_EDGE_NEXT_SYM) & 3;
659 static void d2d_cdt_edge_tor(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
661 dst->idx = src->idx;
662 dst->r = (src->r + D2D_EDGE_NEXT_TOR) & 3;
665 static void d2d_cdt_edge_next_left(const struct d2d_cdt *cdt,
666 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
668 d2d_cdt_edge_rot(dst, &cdt->edges[src->idx].next[(src->r + D2D_EDGE_NEXT_TOR) & 3]);
671 static void d2d_cdt_edge_next_origin(const struct d2d_cdt *cdt,
672 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
674 *dst = cdt->edges[src->idx].next[src->r];
677 static void d2d_cdt_edge_prev_origin(const struct d2d_cdt *cdt,
678 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
680 d2d_cdt_edge_rot(dst, &cdt->edges[src->idx].next[(src->r + D2D_EDGE_NEXT_ROT) & 3]);
683 static size_t d2d_cdt_edge_origin(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
685 return cdt->edges[e->idx].vertex[e->r >> 1];
688 static size_t d2d_cdt_edge_destination(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
690 return cdt->edges[e->idx].vertex[!(e->r >> 1)];
693 static void d2d_cdt_edge_set_origin(const struct d2d_cdt *cdt,
694 const struct d2d_cdt_edge_ref *e, size_t vertex)
696 cdt->edges[e->idx].vertex[e->r >> 1] = vertex;
699 static void d2d_cdt_edge_set_destination(const struct d2d_cdt *cdt,
700 const struct d2d_cdt_edge_ref *e, size_t vertex)
702 cdt->edges[e->idx].vertex[!(e->r >> 1)] = vertex;
705 static float d2d_cdt_ccw(const struct d2d_cdt *cdt, size_t a, size_t b, size_t c)
707 return d2d_point_ccw(&cdt->vertices[a], &cdt->vertices[b], &cdt->vertices[c]);
710 static BOOL d2d_cdt_rightof(const struct d2d_cdt *cdt, size_t p, const struct d2d_cdt_edge_ref *e)
712 return d2d_cdt_ccw(cdt, p, d2d_cdt_edge_destination(cdt, e), d2d_cdt_edge_origin(cdt, e)) > 0.0f;
715 static BOOL d2d_cdt_leftof(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_origin(cdt, e), d2d_cdt_edge_destination(cdt, e)) > 0.0f;
720 /* |ax ay|
721 * |bx by| */
722 static void d2d_fp_four_det2x2(float *out, float ax, float ay, float bx, float by)
724 float axby[2], aybx[2];
726 d2d_fp_two_product(axby, ax, by);
727 d2d_fp_two_product(aybx, ay, bx);
728 d2d_fp_two_two_diff(out, axby, aybx);
731 /* (a->x² + a->y²) * det2x2 */
732 static void d2d_fp_sub_det3x3(float *out, size_t *out_len, const struct d2d_fp_two_vec2 *a, const float *det2x2)
734 size_t axd_len, ayd_len, axxd_len, ayyd_len;
735 float axd[8], ayd[8], axxd[16], ayyd[16];
737 d2d_fp_scale_expansion_zeroelim(axd, &axd_len, det2x2, 4, a->x[1]);
738 d2d_fp_scale_expansion_zeroelim(axxd, &axxd_len, axd, axd_len, a->x[1]);
739 d2d_fp_scale_expansion_zeroelim(ayd, &ayd_len, det2x2, 4, a->y[1]);
740 d2d_fp_scale_expansion_zeroelim(ayyd, &ayyd_len, ayd, ayd_len, a->y[1]);
741 d2d_fp_fast_expansion_sum_zeroelim(out, out_len, axxd, axxd_len, ayyd, ayyd_len);
744 /* det_abt = det_ab * c[0]
745 * fin += c[0] * (az * b - bz * a + c[1] * det_ab * 2.0f) */
746 static void d2d_cdt_incircle_refine1(struct d2d_fp_fin *fin, float *det_abt, size_t *det_abt_len,
747 const float *det_ab, float a, const float *az, float b, const float *bz, const float *c)
749 size_t temp48_len, temp32_len, temp16a_len, temp16b_len, temp16c_len, temp8_len;
750 float temp48[48], temp32[32], temp16a[16], temp16b[16], temp16c[16], temp8[8];
751 float *swap;
753 d2d_fp_scale_expansion_zeroelim(det_abt, det_abt_len, det_ab, 4, c[0]);
754 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, det_abt, *det_abt_len, 2.0f * c[1]);
755 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, az, 4, c[0]);
756 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, temp8, temp8_len, b);
757 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, bz, 4, c[0]);
758 d2d_fp_scale_expansion_zeroelim(temp16c, &temp16c_len, temp8, temp8_len, -a);
759 d2d_fp_fast_expansion_sum_zeroelim(temp32, &temp32_len, temp16a, temp16a_len, temp16b, temp16b_len);
760 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16c, temp16c_len, temp32, temp32_len);
761 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
762 swap = fin->now; fin->now = fin->other; fin->other = swap;
765 static void d2d_cdt_incircle_refine2(struct d2d_fp_fin *fin, const struct d2d_fp_two_vec2 *a,
766 const struct d2d_fp_two_vec2 *b, const float *bz, const struct d2d_fp_two_vec2 *c, const float *cz,
767 const float *axt_det_bc, size_t axt_det_bc_len, const float *ayt_det_bc, size_t ayt_det_bc_len)
769 size_t temp64_len, temp48_len, temp32a_len, temp32b_len, temp16a_len, temp16b_len, temp8_len;
770 float temp64[64], temp48[48], temp32a[32], temp32b[32], temp16a[16], temp16b[16], temp8[8];
771 float bct[8], bctt[4], temp4a[4], temp4b[4], temp2a[2], temp2b[2];
772 size_t bct_len, bctt_len;
773 float *swap;
775 /* 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]) */
776 /* bctt = b->x[0] * c->y[0] + c->x[0] * b->y[0] */
777 if (b->x[0] != 0.0f || b->y[0] != 0.0f || c->x[0] != 0.0f || c->y[0] != 0.0f)
779 d2d_fp_two_product(temp2a, b->x[0], c->y[1]);
780 d2d_fp_two_product(temp2b, b->x[1], c->y[0]);
781 d2d_fp_two_two_sum(temp4a, temp2a, temp2b);
782 d2d_fp_two_product(temp2a, c->x[0], -b->y[1]);
783 d2d_fp_two_product(temp2b, c->x[1], -b->y[0]);
784 d2d_fp_two_two_sum(temp4b, temp2a, temp2b);
785 d2d_fp_fast_expansion_sum_zeroelim(bct, &bct_len, temp4a, 4, temp4b, 4);
787 d2d_fp_two_product(temp2a, b->x[0], c->y[0]);
788 d2d_fp_two_product(temp2b, c->x[0], b->y[0]);
789 d2d_fp_two_two_diff(bctt, temp2a, temp2b);
790 bctt_len = 4;
792 else
794 bct[0] = 0.0f;
795 bct_len = 1;
796 bctt[0] = 0.0f;
797 bctt_len = 1;
800 if (a->x[0] != 0.0f)
802 size_t axt_bct_len, axt_bctt_len;
803 float axt_bct[16], axt_bctt[8];
805 /* fin += a->x[0] * (axt_det_bc + bct * 2.0f * a->x[1]) */
806 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, axt_det_bc, axt_det_bc_len, a->x[0]);
807 d2d_fp_scale_expansion_zeroelim(axt_bct, &axt_bct_len, bct, bct_len, a->x[0]);
808 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, axt_bct, axt_bct_len, 2.0f * a->x[1]);
809 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16a, temp16a_len, temp32a, temp32a_len);
810 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
811 swap = fin->now; fin->now = fin->other; fin->other = swap;
813 if (b->y[0] != 0.0f)
815 /* fin += a->x[0] * cz * b->y[0] */
816 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, cz, 4, a->x[0]);
817 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, temp8, temp8_len, b->y[0]);
818 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp16a, temp16a_len);
819 swap = fin->now; fin->now = fin->other; fin->other = swap;
822 if (c->y[0] != 0.0f)
824 /* fin -= a->x[0] * bz * c->y[0] */
825 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, bz, 4, -a->x[0]);
826 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, temp8, temp8_len, c->y[0]);
827 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp16a, temp16a_len);
828 swap = fin->now; fin->now = fin->other; fin->other = swap;
831 /* fin += a->x[0] * (bct * a->x[0] + bctt * (2.0f * a->x[1] + a->x[0])) */
832 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, axt_bct, axt_bct_len, a->x[0]);
833 d2d_fp_scale_expansion_zeroelim(axt_bctt, &axt_bctt_len, bctt, bctt_len, a->x[0]);
834 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, axt_bctt, axt_bctt_len, 2.0f * a->x[1]);
835 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, axt_bctt, axt_bctt_len, a->x[0]);
836 d2d_fp_fast_expansion_sum_zeroelim(temp32b, &temp32b_len, temp16a, temp16a_len, temp16b, temp16b_len);
837 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, temp32a, temp32a_len, temp32b, temp32b_len);
838 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp64, temp64_len);
839 swap = fin->now; fin->now = fin->other; fin->other = swap;
842 if (a->y[0] != 0.0f)
844 size_t ayt_bct_len, ayt_bctt_len;
845 float ayt_bct[16], ayt_bctt[8];
847 /* fin += a->y[0] * (ayt_det_bc + bct * 2.0f * a->y[1]) */
848 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, ayt_det_bc, ayt_det_bc_len, a->y[0]);
849 d2d_fp_scale_expansion_zeroelim(ayt_bct, &ayt_bct_len, bct, bct_len, a->y[0]);
850 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, ayt_bct, ayt_bct_len, 2.0f * a->y[1]);
851 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16a, temp16a_len, temp32a, temp32a_len);
852 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
853 swap = fin->now; fin->now = fin->other; fin->other = swap;
855 /* fin += a->y[0] * (bct * a->y[0] + bctt * (2.0f * a->y[1] + a->y[0])) */
856 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, ayt_bct, ayt_bct_len, a->y[0]);
857 d2d_fp_scale_expansion_zeroelim(ayt_bctt, &ayt_bctt_len, bctt, bctt_len, a->y[0]);
858 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, ayt_bctt, ayt_bctt_len, 2.0f * a->y[1]);
859 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, ayt_bctt, ayt_bctt_len, a->y[0]);
860 d2d_fp_fast_expansion_sum_zeroelim(temp32b, &temp32b_len, temp16a, temp16a_len, temp16b, temp16b_len);
861 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, temp32a, temp32a_len, temp32b, temp32b_len);
862 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp64, temp64_len);
863 swap = fin->now; fin->now = fin->other; fin->other = swap;
867 /* Determine if point D is inside or outside the circle defined by points A,
868 * B, C. As explained in the paper by Guibas and Stolfi, this is equivalent to
869 * calculating the signed volume of the tetrahedron defined by projecting the
870 * points onto the paraboloid of revolution x = x² + y²,
871 * λ:(x, y) → (x, y, x² + y²). I.e., D is inside the cirlce if
873 * |λ(A) 1|
874 * |λ(B) 1| > 0
875 * |λ(C) 1|
876 * |λ(D) 1|
878 * After translating D to the origin, that becomes:
880 * |λ(A-D)|
881 * |λ(B-D)| > 0
882 * |λ(C-D)|
884 * This implementation is based on the paper "Adaptive Precision
885 * Floating-Point Arithmetic and Fast Robust Geometric Predicates" and
886 * associated (Public Domain) code by Jonathan Richard Shewchuk. */
887 static BOOL d2d_cdt_incircle(const struct d2d_cdt *cdt, size_t a, size_t b, size_t c, size_t d)
889 static const float err_bound_result = (3.0f + 8.0f * D2D_FP_EPS) * D2D_FP_EPS;
890 static const float err_bound_a = (10.0f + 96.0f * D2D_FP_EPS) * D2D_FP_EPS;
891 static const float err_bound_b = (4.0f + 48.0f * D2D_FP_EPS) * D2D_FP_EPS;
892 static const float err_bound_c = (44.0f + 576.0f * D2D_FP_EPS) * D2D_FP_EPS * D2D_FP_EPS;
894 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;
895 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];
896 float fin1[1152], fin2[1152], temp64[64], sub_det_a[32], sub_det_b[32], sub_det_c[32];
897 float det_bc[4], det_ca[4], det_ab[4], daz[4], dbz[4], dcz[4], temp2a[2], temp2b[2];
898 size_t temp64_len, sub_det_a_len, sub_det_b_len, sub_det_c_len;
899 float dbxdcy, dbydcx, dcxday, dcydax, daxdby, daydbx;
900 const D2D1_POINT_2F *p = cdt->vertices;
901 struct d2d_fp_two_vec2 da, db, dc;
902 float permanent, err_bound, det;
903 struct d2d_fp_fin fin;
905 da.x[1] = p[a].x - p[d].x;
906 da.y[1] = p[a].y - p[d].y;
907 db.x[1] = p[b].x - p[d].x;
908 db.y[1] = p[b].y - p[d].y;
909 dc.x[1] = p[c].x - p[d].x;
910 dc.y[1] = p[c].y - p[d].y;
912 daz[3] = da.x[1] * da.x[1] + da.y[1] * da.y[1];
913 dbxdcy = db.x[1] * dc.y[1];
914 dbydcx = db.y[1] * dc.x[1];
916 dbz[3] = db.x[1] * db.x[1] + db.y[1] * db.y[1];
917 dcxday = dc.x[1] * da.y[1];
918 dcydax = dc.y[1] * da.x[1];
920 dcz[3] = dc.x[1] * dc.x[1] + dc.y[1] * dc.y[1];
921 daxdby = da.x[1] * db.y[1];
922 daydbx = da.y[1] * db.x[1];
924 det = daz[3] * (dbxdcy - dbydcx) + dbz[3] * (dcxday - dcydax) + dcz[3] * (daxdby - daydbx);
925 permanent = daz[3] * (fabsf(dbxdcy) + fabsf(dbydcx))
926 + dbz[3] * (fabsf(dcxday) + fabsf(dcydax))
927 + dcz[3] * (fabsf(daxdby) + fabsf(daydbx));
928 err_bound = err_bound_a * permanent;
929 if (det > err_bound || -det > err_bound)
930 return det > 0.0f;
932 fin.now = fin1;
933 fin.other = fin2;
935 d2d_fp_four_det2x2(det_bc, db.x[1], db.y[1], dc.x[1], dc.y[1]);
936 d2d_fp_sub_det3x3(sub_det_a, &sub_det_a_len, &da, det_bc);
938 d2d_fp_four_det2x2(det_ca, dc.x[1], dc.y[1], da.x[1], da.y[1]);
939 d2d_fp_sub_det3x3(sub_det_b, &sub_det_b_len, &db, det_ca);
941 d2d_fp_four_det2x2(det_ab, da.x[1], da.y[1], db.x[1], db.y[1]);
942 d2d_fp_sub_det3x3(sub_det_c, &sub_det_c_len, &dc, det_ab);
944 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, sub_det_a, sub_det_a_len, sub_det_b, sub_det_b_len);
945 d2d_fp_fast_expansion_sum_zeroelim(fin.now, &fin.length, temp64, temp64_len, sub_det_c, sub_det_c_len);
946 det = d2d_fp_estimate(fin.now, fin.length);
947 err_bound = err_bound_b * permanent;
948 if (det >= err_bound || -det >= err_bound)
949 return det > 0.0f;
951 d2d_fp_two_diff_tail(&da.x[0], p[a].x, p[d].x, da.x[1]);
952 d2d_fp_two_diff_tail(&da.y[0], p[a].y, p[d].y, da.y[1]);
953 d2d_fp_two_diff_tail(&db.x[0], p[b].x, p[d].x, db.x[1]);
954 d2d_fp_two_diff_tail(&db.y[0], p[b].y, p[d].y, db.y[1]);
955 d2d_fp_two_diff_tail(&dc.x[0], p[c].x, p[d].x, dc.x[1]);
956 d2d_fp_two_diff_tail(&dc.y[0], p[c].y, p[d].y, dc.y[1]);
957 if (da.x[0] == 0.0f && db.x[0] == 0.0f && dc.x[0] == 0.0f
958 && da.y[0] == 0.0f && db.y[0] == 0.0f && dc.y[0] == 0.0f)
959 return det > 0.0f;
961 err_bound = err_bound_c * permanent + err_bound_result * fabsf(det);
962 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]))
963 + 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]))
964 + (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]))
965 + 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]))
966 + (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]))
967 + 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]));
968 if (det >= err_bound || -det >= err_bound)
969 return det > 0.0f;
971 if (db.x[0] != 0.0f || db.y[0] != 0.0f || dc.x[0] != 0.0f || dc.y[0] != 0.0f)
973 d2d_fp_square(temp2a, da.x[1]);
974 d2d_fp_square(temp2b, da.y[1]);
975 d2d_fp_two_two_sum(daz, temp2a, temp2b);
977 if (dc.x[0] != 0.0f || dc.y[0] != 0.0f || da.x[0] != 0.0f || da.y[0] != 0.0f)
979 d2d_fp_square(temp2a, db.x[1]);
980 d2d_fp_square(temp2b, db.y[1]);
981 d2d_fp_two_two_sum(dbz, temp2a, temp2b);
983 if (da.x[0] != 0.0f || da.y[0] != 0.0f || db.x[0] != 0.0f || db.y[0] != 0.0f)
985 d2d_fp_square(temp2a, dc.x[1]);
986 d2d_fp_square(temp2b, dc.y[1]);
987 d2d_fp_two_two_sum(dcz, temp2a, temp2b);
990 if (da.x[0] != 0.0f)
991 d2d_cdt_incircle_refine1(&fin, axt_det_bc, &axt_det_bc_len, det_bc, dc.y[1], dcz, db.y[1], dbz, da.x);
992 if (da.y[0] != 0.0f)
993 d2d_cdt_incircle_refine1(&fin, ayt_det_bc, &ayt_det_bc_len, det_bc, db.x[1], dbz, dc.x[1], dcz, da.y);
994 if (db.x[0] != 0.0f)
995 d2d_cdt_incircle_refine1(&fin, bxt_det_ca, &bxt_det_ca_len, det_ca, da.y[1], daz, dc.y[1], dcz, db.x);
996 if (db.y[0] != 0.0f)
997 d2d_cdt_incircle_refine1(&fin, byt_det_ca, &byt_det_ca_len, det_ca, dc.x[1], dcz, da.x[1], daz, db.y);
998 if (dc.x[0] != 0.0f)
999 d2d_cdt_incircle_refine1(&fin, cxt_det_ab, &cxt_det_ab_len, det_ab, db.y[1], dbz, da.y[1], daz, dc.x);
1000 if (dc.y[0] != 0.0f)
1001 d2d_cdt_incircle_refine1(&fin, cyt_det_ab, &cyt_det_ab_len, det_ab, da.x[1], daz, db.x[1], dbz, dc.y);
1003 if (da.x[0] != 0.0f || da.y[0] != 0.0f)
1004 d2d_cdt_incircle_refine2(&fin, &da, &db, dbz, &dc, dcz,
1005 axt_det_bc, axt_det_bc_len, ayt_det_bc, ayt_det_bc_len);
1006 if (db.x[0] != 0.0f || db.y[0] != 0.0f)
1007 d2d_cdt_incircle_refine2(&fin, &db, &dc, dcz, &da, daz,
1008 bxt_det_ca, bxt_det_ca_len, byt_det_ca, byt_det_ca_len);
1009 if (dc.x[0] != 0.0f || dc.y[0] != 0.0f)
1010 d2d_cdt_incircle_refine2(&fin, &dc, &da, daz, &db, dbz,
1011 cxt_det_ab, cxt_det_ab_len, cyt_det_ab, cyt_det_ab_len);
1013 return fin.now[fin.length - 1] > 0.0f;
1016 static void d2d_cdt_splice(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *a,
1017 const struct d2d_cdt_edge_ref *b)
1019 struct d2d_cdt_edge_ref ta, tb, alpha, beta;
1021 ta = cdt->edges[a->idx].next[a->r];
1022 tb = cdt->edges[b->idx].next[b->r];
1023 cdt->edges[a->idx].next[a->r] = tb;
1024 cdt->edges[b->idx].next[b->r] = ta;
1026 d2d_cdt_edge_rot(&alpha, &ta);
1027 d2d_cdt_edge_rot(&beta, &tb);
1029 ta = cdt->edges[alpha.idx].next[alpha.r];
1030 tb = cdt->edges[beta.idx].next[beta.r];
1031 cdt->edges[alpha.idx].next[alpha.r] = tb;
1032 cdt->edges[beta.idx].next[beta.r] = ta;
1035 static BOOL d2d_cdt_create_edge(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *e)
1037 struct d2d_cdt_edge *edge;
1039 if (cdt->free_edge != ~0u)
1041 e->idx = cdt->free_edge;
1042 cdt->free_edge = cdt->edges[e->idx].next[D2D_EDGE_NEXT_ORIGIN].idx;
1044 else
1046 if (!d2d_array_reserve((void **)&cdt->edges, &cdt->edges_size, cdt->edge_count + 1, sizeof(*cdt->edges)))
1048 ERR("Failed to grow edges array.\n");
1049 return FALSE;
1051 e->idx = cdt->edge_count++;
1053 e->r = 0;
1055 edge = &cdt->edges[e->idx];
1056 edge->next[D2D_EDGE_NEXT_ORIGIN] = *e;
1057 d2d_cdt_edge_tor(&edge->next[D2D_EDGE_NEXT_ROT], e);
1058 d2d_cdt_edge_sym(&edge->next[D2D_EDGE_NEXT_SYM], e);
1059 d2d_cdt_edge_rot(&edge->next[D2D_EDGE_NEXT_TOR], e);
1060 edge->flags = 0;
1062 return TRUE;
1065 static void d2d_cdt_destroy_edge(struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
1067 struct d2d_cdt_edge_ref next, sym, prev;
1069 d2d_cdt_edge_next_origin(cdt, &next, e);
1070 if (next.idx != e->idx || next.r != e->r)
1072 d2d_cdt_edge_prev_origin(cdt, &prev, e);
1073 d2d_cdt_splice(cdt, e, &prev);
1076 d2d_cdt_edge_sym(&sym, e);
1078 d2d_cdt_edge_next_origin(cdt, &next, &sym);
1079 if (next.idx != sym.idx || next.r != sym.r)
1081 d2d_cdt_edge_prev_origin(cdt, &prev, &sym);
1082 d2d_cdt_splice(cdt, &sym, &prev);
1085 cdt->edges[e->idx].flags |= D2D_CDT_EDGE_FLAG_FREED;
1086 cdt->edges[e->idx].next[D2D_EDGE_NEXT_ORIGIN].idx = cdt->free_edge;
1087 cdt->free_edge = e->idx;
1090 static BOOL d2d_cdt_connect(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *e,
1091 const struct d2d_cdt_edge_ref *a, const struct d2d_cdt_edge_ref *b)
1093 struct d2d_cdt_edge_ref tmp;
1095 if (!d2d_cdt_create_edge(cdt, e))
1096 return FALSE;
1097 d2d_cdt_edge_set_origin(cdt, e, d2d_cdt_edge_destination(cdt, a));
1098 d2d_cdt_edge_set_destination(cdt, e, d2d_cdt_edge_origin(cdt, b));
1099 d2d_cdt_edge_next_left(cdt, &tmp, a);
1100 d2d_cdt_splice(cdt, e, &tmp);
1101 d2d_cdt_edge_sym(&tmp, e);
1102 d2d_cdt_splice(cdt, &tmp, b);
1104 return TRUE;
1107 static BOOL d2d_cdt_merge(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *left_outer,
1108 struct d2d_cdt_edge_ref *left_inner, struct d2d_cdt_edge_ref *right_inner,
1109 struct d2d_cdt_edge_ref *right_outer)
1111 struct d2d_cdt_edge_ref base_edge, tmp;
1113 /* Create the base edge between both parts. */
1114 for (;;)
1116 if (d2d_cdt_leftof(cdt, d2d_cdt_edge_origin(cdt, right_inner), left_inner))
1118 d2d_cdt_edge_next_left(cdt, left_inner, left_inner);
1120 else if (d2d_cdt_rightof(cdt, d2d_cdt_edge_origin(cdt, left_inner), right_inner))
1122 d2d_cdt_edge_sym(&tmp, right_inner);
1123 d2d_cdt_edge_next_origin(cdt, right_inner, &tmp);
1125 else
1127 break;
1131 d2d_cdt_edge_sym(&tmp, right_inner);
1132 if (!d2d_cdt_connect(cdt, &base_edge, &tmp, left_inner))
1133 return FALSE;
1134 if (d2d_cdt_edge_origin(cdt, left_inner) == d2d_cdt_edge_origin(cdt, left_outer))
1135 d2d_cdt_edge_sym(left_outer, &base_edge);
1136 if (d2d_cdt_edge_origin(cdt, right_inner) == d2d_cdt_edge_origin(cdt, right_outer))
1137 *right_outer = base_edge;
1139 for (;;)
1141 struct d2d_cdt_edge_ref left_candidate, right_candidate, sym_base_edge;
1142 BOOL left_valid, right_valid;
1144 /* Find the left candidate. */
1145 d2d_cdt_edge_sym(&sym_base_edge, &base_edge);
1146 d2d_cdt_edge_next_origin(cdt, &left_candidate, &sym_base_edge);
1147 if ((left_valid = d2d_cdt_leftof(cdt, d2d_cdt_edge_destination(cdt, &left_candidate), &sym_base_edge)))
1149 d2d_cdt_edge_next_origin(cdt, &tmp, &left_candidate);
1150 while (d2d_cdt_edge_destination(cdt, &tmp) != d2d_cdt_edge_destination(cdt, &sym_base_edge)
1151 && d2d_cdt_incircle(cdt,
1152 d2d_cdt_edge_origin(cdt, &sym_base_edge), d2d_cdt_edge_destination(cdt, &sym_base_edge),
1153 d2d_cdt_edge_destination(cdt, &left_candidate), d2d_cdt_edge_destination(cdt, &tmp)))
1155 d2d_cdt_destroy_edge(cdt, &left_candidate);
1156 left_candidate = tmp;
1157 d2d_cdt_edge_next_origin(cdt, &tmp, &left_candidate);
1160 d2d_cdt_edge_sym(&left_candidate, &left_candidate);
1162 /* Find the right candidate. */
1163 d2d_cdt_edge_prev_origin(cdt, &right_candidate, &base_edge);
1164 if ((right_valid = d2d_cdt_rightof(cdt, d2d_cdt_edge_destination(cdt, &right_candidate), &base_edge)))
1166 d2d_cdt_edge_prev_origin(cdt, &tmp, &right_candidate);
1167 while (d2d_cdt_edge_destination(cdt, &tmp) != d2d_cdt_edge_destination(cdt, &base_edge)
1168 && d2d_cdt_incircle(cdt,
1169 d2d_cdt_edge_origin(cdt, &sym_base_edge), d2d_cdt_edge_destination(cdt, &sym_base_edge),
1170 d2d_cdt_edge_destination(cdt, &right_candidate), d2d_cdt_edge_destination(cdt, &tmp)))
1172 d2d_cdt_destroy_edge(cdt, &right_candidate);
1173 right_candidate = tmp;
1174 d2d_cdt_edge_prev_origin(cdt, &tmp, &right_candidate);
1178 if (!left_valid && !right_valid)
1179 break;
1181 /* Connect the appropriate candidate with the base edge. */
1182 if (!left_valid || (right_valid && d2d_cdt_incircle(cdt,
1183 d2d_cdt_edge_origin(cdt, &left_candidate), d2d_cdt_edge_destination(cdt, &left_candidate),
1184 d2d_cdt_edge_origin(cdt, &right_candidate), d2d_cdt_edge_destination(cdt, &right_candidate))))
1186 if (!d2d_cdt_connect(cdt, &base_edge, &right_candidate, &sym_base_edge))
1187 return FALSE;
1189 else
1191 if (!d2d_cdt_connect(cdt, &base_edge, &sym_base_edge, &left_candidate))
1192 return FALSE;
1196 return TRUE;
1199 /* Create a Delaunay triangulation from a set of vertices. This is an
1200 * implementation of the divide-and-conquer algorithm described by Guibas and
1201 * Stolfi. Should be called with at least two vertices. */
1202 static BOOL d2d_cdt_triangulate(struct d2d_cdt *cdt, size_t start_vertex, size_t vertex_count,
1203 struct d2d_cdt_edge_ref *left_edge, struct d2d_cdt_edge_ref *right_edge)
1205 struct d2d_cdt_edge_ref left_inner, left_outer, right_inner, right_outer, tmp;
1206 size_t cut;
1208 /* Only two vertices, create a single edge. */
1209 if (vertex_count == 2)
1211 struct d2d_cdt_edge_ref a;
1213 if (!d2d_cdt_create_edge(cdt, &a))
1214 return FALSE;
1215 d2d_cdt_edge_set_origin(cdt, &a, start_vertex);
1216 d2d_cdt_edge_set_destination(cdt, &a, start_vertex + 1);
1218 *left_edge = a;
1219 d2d_cdt_edge_sym(right_edge, &a);
1221 return TRUE;
1224 /* Three vertices, create a triangle. */
1225 if (vertex_count == 3)
1227 struct d2d_cdt_edge_ref a, b, c;
1228 float det;
1230 if (!d2d_cdt_create_edge(cdt, &a))
1231 return FALSE;
1232 if (!d2d_cdt_create_edge(cdt, &b))
1233 return FALSE;
1234 d2d_cdt_edge_sym(&tmp, &a);
1235 d2d_cdt_splice(cdt, &tmp, &b);
1237 d2d_cdt_edge_set_origin(cdt, &a, start_vertex);
1238 d2d_cdt_edge_set_destination(cdt, &a, start_vertex + 1);
1239 d2d_cdt_edge_set_origin(cdt, &b, start_vertex + 1);
1240 d2d_cdt_edge_set_destination(cdt, &b, start_vertex + 2);
1242 det = d2d_cdt_ccw(cdt, start_vertex, start_vertex + 1, start_vertex + 2);
1243 if (det != 0.0f && !d2d_cdt_connect(cdt, &c, &b, &a))
1244 return FALSE;
1246 if (det < 0.0f)
1248 d2d_cdt_edge_sym(left_edge, &c);
1249 *right_edge = c;
1251 else
1253 *left_edge = a;
1254 d2d_cdt_edge_sym(right_edge, &b);
1257 return TRUE;
1260 /* More than tree vertices, divide. */
1261 cut = vertex_count / 2;
1262 if (!d2d_cdt_triangulate(cdt, start_vertex, cut, &left_outer, &left_inner))
1263 return FALSE;
1264 if (!d2d_cdt_triangulate(cdt, start_vertex + cut, vertex_count - cut, &right_inner, &right_outer))
1265 return FALSE;
1266 /* Merge the left and right parts. */
1267 if (!d2d_cdt_merge(cdt, &left_outer, &left_inner, &right_inner, &right_outer))
1268 return FALSE;
1270 *left_edge = left_outer;
1271 *right_edge = right_outer;
1272 return TRUE;
1275 static int __cdecl d2d_cdt_compare_vertices(const void *a, const void *b)
1277 const D2D1_POINT_2F *p0 = a;
1278 const D2D1_POINT_2F *p1 = b;
1279 float diff = p0->x - p1->x;
1281 if (diff == 0.0f)
1282 diff = p0->y - p1->y;
1284 return diff == 0.0f ? 0 : (diff > 0.0f ? 1 : -1);
1287 /* Determine whether a given point is inside the geometry, using the current
1288 * fill mode rule. */
1289 static BOOL d2d_path_geometry_point_inside(const struct d2d_geometry *geometry,
1290 const D2D1_POINT_2F *probe, BOOL triangles_only)
1292 const D2D1_POINT_2F *p0, *p1;
1293 D2D1_POINT_2F v_p, v_probe;
1294 unsigned int score;
1295 size_t i, j, last;
1297 for (i = 0, score = 0; i < geometry->u.path.figure_count; ++i)
1299 const struct d2d_figure *figure = &geometry->u.path.figures[i];
1301 if (probe->x < figure->bounds.left || probe->x > figure->bounds.right
1302 || probe->y < figure->bounds.top || probe->y > figure->bounds.bottom)
1303 continue;
1305 last = figure->vertex_count - 1;
1306 if (!triangles_only)
1308 while (last && figure->vertex_types[last] == D2D_VERTEX_TYPE_NONE)
1309 --last;
1311 p0 = &figure->vertices[last];
1312 for (j = 0; j <= last; ++j)
1314 if (!triangles_only && figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
1315 continue;
1317 p1 = &figure->vertices[j];
1318 d2d_point_subtract(&v_p, p1, p0);
1319 d2d_point_subtract(&v_probe, probe, p0);
1321 if ((probe->y < p0->y) != (probe->y < p1->y) && v_probe.x < v_p.x * (v_probe.y / v_p.y))
1323 if (geometry->u.path.fill_mode == D2D1_FILL_MODE_ALTERNATE || (probe->y < p0->y))
1324 ++score;
1325 else
1326 --score;
1329 p0 = p1;
1333 return geometry->u.path.fill_mode == D2D1_FILL_MODE_ALTERNATE ? score & 1 : score;
1336 static BOOL d2d_path_geometry_add_fill_face(struct d2d_geometry *geometry, const struct d2d_cdt *cdt,
1337 const struct d2d_cdt_edge_ref *base_edge)
1339 struct d2d_cdt_edge_ref tmp;
1340 struct d2d_face *face;
1341 D2D1_POINT_2F probe;
1343 if (cdt->edges[base_edge->idx].flags & D2D_CDT_EDGE_FLAG_VISITED(base_edge->r))
1344 return TRUE;
1346 if (!d2d_array_reserve((void **)&geometry->fill.faces, &geometry->fill.faces_size,
1347 geometry->fill.face_count + 1, sizeof(*geometry->fill.faces)))
1349 ERR("Failed to grow faces array.\n");
1350 return FALSE;
1353 face = &geometry->fill.faces[geometry->fill.face_count];
1355 /* It may seem tempting to use the center of the face as probe origin, but
1356 * multiplying by powers of two works much better for preserving accuracy. */
1358 tmp = *base_edge;
1359 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1360 face->v[0] = d2d_cdt_edge_origin(cdt, &tmp);
1361 probe.x = cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.25f;
1362 probe.y = cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.25f;
1364 d2d_cdt_edge_next_left(cdt, &tmp, &tmp);
1365 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1366 face->v[1] = d2d_cdt_edge_origin(cdt, &tmp);
1367 probe.x += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.25f;
1368 probe.y += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.25f;
1370 d2d_cdt_edge_next_left(cdt, &tmp, &tmp);
1371 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1372 face->v[2] = d2d_cdt_edge_origin(cdt, &tmp);
1373 probe.x += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.50f;
1374 probe.y += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.50f;
1376 if (d2d_cdt_leftof(cdt, face->v[2], base_edge) && d2d_path_geometry_point_inside(geometry, &probe, TRUE))
1377 ++geometry->fill.face_count;
1379 return TRUE;
1382 static BOOL d2d_cdt_generate_faces(const struct d2d_cdt *cdt, struct d2d_geometry *geometry)
1384 struct d2d_cdt_edge_ref base_edge;
1385 size_t i;
1387 for (i = 0; i < cdt->edge_count; ++i)
1389 if (cdt->edges[i].flags & D2D_CDT_EDGE_FLAG_FREED)
1390 continue;
1392 base_edge.idx = i;
1393 base_edge.r = 0;
1394 if (!d2d_path_geometry_add_fill_face(geometry, cdt, &base_edge))
1395 goto fail;
1396 d2d_cdt_edge_sym(&base_edge, &base_edge);
1397 if (!d2d_path_geometry_add_fill_face(geometry, cdt, &base_edge))
1398 goto fail;
1401 return TRUE;
1403 fail:
1404 heap_free(geometry->fill.faces);
1405 geometry->fill.faces = NULL;
1406 geometry->fill.faces_size = 0;
1407 geometry->fill.face_count = 0;
1408 return FALSE;
1411 static BOOL d2d_cdt_fixup(struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *base_edge)
1413 struct d2d_cdt_edge_ref candidate, next, new_base;
1414 unsigned int count = 0;
1416 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1417 if (next.idx == base_edge->idx)
1419 ERR("Degenerate face.\n");
1420 return FALSE;
1423 candidate = next;
1424 while (d2d_cdt_edge_destination(cdt, &next) != d2d_cdt_edge_origin(cdt, base_edge))
1426 if (d2d_cdt_incircle(cdt, d2d_cdt_edge_origin(cdt, base_edge), d2d_cdt_edge_destination(cdt, base_edge),
1427 d2d_cdt_edge_destination(cdt, &candidate), d2d_cdt_edge_destination(cdt, &next)))
1428 candidate = next;
1429 d2d_cdt_edge_next_left(cdt, &next, &next);
1430 ++count;
1433 if (count > 1)
1435 d2d_cdt_edge_next_left(cdt, &next, &candidate);
1436 if (d2d_cdt_edge_destination(cdt, &next) == d2d_cdt_edge_origin(cdt, base_edge))
1437 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1438 else
1439 next = *base_edge;
1440 if (!d2d_cdt_connect(cdt, &new_base, &candidate, &next))
1441 return FALSE;
1442 if (!d2d_cdt_fixup(cdt, &new_base))
1443 return FALSE;
1444 d2d_cdt_edge_sym(&new_base, &new_base);
1445 if (!d2d_cdt_fixup(cdt, &new_base))
1446 return FALSE;
1449 return TRUE;
1452 static void d2d_cdt_cut_edges(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *end_edge,
1453 const struct d2d_cdt_edge_ref *base_edge, size_t start_vertex, size_t end_vertex)
1455 struct d2d_cdt_edge_ref next;
1456 float ccw;
1458 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1459 if (d2d_cdt_edge_destination(cdt, &next) == end_vertex)
1461 *end_edge = next;
1462 return;
1465 ccw = d2d_cdt_ccw(cdt, d2d_cdt_edge_destination(cdt, &next), end_vertex, start_vertex);
1466 if (ccw == 0.0f)
1468 *end_edge = next;
1469 return;
1472 if (ccw > 0.0f)
1473 d2d_cdt_edge_next_left(cdt, &next, &next);
1475 d2d_cdt_edge_sym(&next, &next);
1476 d2d_cdt_cut_edges(cdt, end_edge, &next, start_vertex, end_vertex);
1477 d2d_cdt_destroy_edge(cdt, &next);
1480 static BOOL d2d_cdt_insert_segment(struct d2d_cdt *cdt, struct d2d_geometry *geometry,
1481 const struct d2d_cdt_edge_ref *origin, struct d2d_cdt_edge_ref *edge, size_t end_vertex)
1483 struct d2d_cdt_edge_ref base_edge, current, new_origin, next, target;
1484 size_t current_destination, current_origin;
1486 for (current = *origin;; current = next)
1488 d2d_cdt_edge_next_origin(cdt, &next, &current);
1490 current_destination = d2d_cdt_edge_destination(cdt, &current);
1491 if (current_destination == end_vertex)
1493 d2d_cdt_edge_sym(edge, &current);
1494 return TRUE;
1497 current_origin = d2d_cdt_edge_origin(cdt, &current);
1498 if (d2d_cdt_ccw(cdt, end_vertex, current_origin, current_destination) == 0.0f
1499 && (cdt->vertices[current_destination].x > cdt->vertices[current_origin].x)
1500 == (cdt->vertices[end_vertex].x > cdt->vertices[current_origin].x)
1501 && (cdt->vertices[current_destination].y > cdt->vertices[current_origin].y)
1502 == (cdt->vertices[end_vertex].y > cdt->vertices[current_origin].y))
1504 d2d_cdt_edge_sym(&new_origin, &current);
1505 return d2d_cdt_insert_segment(cdt, geometry, &new_origin, edge, end_vertex);
1508 if (d2d_cdt_rightof(cdt, end_vertex, &next) && d2d_cdt_leftof(cdt, end_vertex, &current))
1510 d2d_cdt_edge_next_left(cdt, &base_edge, &current);
1512 d2d_cdt_edge_sym(&base_edge, &base_edge);
1513 d2d_cdt_cut_edges(cdt, &target, &base_edge, d2d_cdt_edge_origin(cdt, origin), end_vertex);
1514 d2d_cdt_destroy_edge(cdt, &base_edge);
1516 if (!d2d_cdt_connect(cdt, &base_edge, &target, &current))
1517 return FALSE;
1518 *edge = base_edge;
1519 if (!d2d_cdt_fixup(cdt, &base_edge))
1520 return FALSE;
1521 d2d_cdt_edge_sym(&base_edge, &base_edge);
1522 if (!d2d_cdt_fixup(cdt, &base_edge))
1523 return FALSE;
1525 if (d2d_cdt_edge_origin(cdt, edge) == end_vertex)
1526 return TRUE;
1527 new_origin = *edge;
1528 return d2d_cdt_insert_segment(cdt, geometry, &new_origin, edge, end_vertex);
1531 if (next.idx == origin->idx)
1533 ERR("Triangle not found.\n");
1534 return FALSE;
1539 static BOOL d2d_cdt_insert_segments(struct d2d_cdt *cdt, struct d2d_geometry *geometry)
1541 size_t start_vertex, end_vertex, i, j, k;
1542 struct d2d_cdt_edge_ref edge, new_edge;
1543 const struct d2d_figure *figure;
1544 const D2D1_POINT_2F *p;
1545 BOOL found;
1547 for (i = 0; i < geometry->u.path.figure_count; ++i)
1549 figure = &geometry->u.path.figures[i];
1551 /* Degenerate figure. */
1552 if (figure->vertex_count < 2)
1553 continue;
1555 p = bsearch(&figure->vertices[figure->vertex_count - 1], cdt->vertices,
1556 geometry->fill.vertex_count, sizeof(*p), d2d_cdt_compare_vertices);
1557 start_vertex = p - cdt->vertices;
1559 for (k = 0, found = FALSE; k < cdt->edge_count; ++k)
1561 if (cdt->edges[k].flags & D2D_CDT_EDGE_FLAG_FREED)
1562 continue;
1564 edge.idx = k;
1565 edge.r = 0;
1567 if (d2d_cdt_edge_origin(cdt, &edge) == start_vertex)
1569 found = TRUE;
1570 break;
1572 d2d_cdt_edge_sym(&edge, &edge);
1573 if (d2d_cdt_edge_origin(cdt, &edge) == start_vertex)
1575 found = TRUE;
1576 break;
1580 if (!found)
1582 ERR("Edge not found.\n");
1583 return FALSE;
1586 for (j = 0; j < figure->vertex_count; start_vertex = end_vertex, ++j)
1588 p = bsearch(&figure->vertices[j], cdt->vertices,
1589 geometry->fill.vertex_count, sizeof(*p), d2d_cdt_compare_vertices);
1590 end_vertex = p - cdt->vertices;
1592 if (start_vertex == end_vertex)
1593 continue;
1595 if (!d2d_cdt_insert_segment(cdt, geometry, &edge, &new_edge, end_vertex))
1596 return FALSE;
1597 edge = new_edge;
1601 return TRUE;
1604 static BOOL d2d_geometry_intersections_add(struct d2d_geometry_intersections *i,
1605 const struct d2d_segment_idx *segment_idx, float t, D2D1_POINT_2F p)
1607 struct d2d_geometry_intersection *intersection;
1609 if (!d2d_array_reserve((void **)&i->intersections, &i->intersections_size,
1610 i->intersection_count + 1, sizeof(*i->intersections)))
1612 ERR("Failed to grow intersections array.\n");
1613 return FALSE;
1616 intersection = &i->intersections[i->intersection_count++];
1617 intersection->figure_idx = segment_idx->figure_idx;
1618 intersection->vertex_idx = segment_idx->vertex_idx;
1619 intersection->control_idx = segment_idx->control_idx;
1620 intersection->t = t;
1621 intersection->p = p;
1623 return TRUE;
1626 static int __cdecl d2d_geometry_intersections_compare(const void *a, const void *b)
1628 const struct d2d_geometry_intersection *i0 = a;
1629 const struct d2d_geometry_intersection *i1 = b;
1631 if (i0->figure_idx != i1->figure_idx)
1632 return i0->figure_idx - i1->figure_idx;
1633 if (i0->vertex_idx != i1->vertex_idx)
1634 return i0->vertex_idx - i1->vertex_idx;
1635 if (i0->t != i1->t)
1636 return i0->t > i1->t ? 1 : -1;
1637 return 0;
1640 static BOOL d2d_geometry_intersect_line_line(struct d2d_geometry *geometry,
1641 struct d2d_geometry_intersections *intersections, const struct d2d_segment_idx *idx_p,
1642 const struct d2d_segment_idx *idx_q)
1644 D2D1_POINT_2F v_p, v_q, v_qp, intersection;
1645 const D2D1_POINT_2F *p[2], *q[2];
1646 const struct d2d_figure *figure;
1647 float s, t, det;
1648 size_t next;
1650 figure = &geometry->u.path.figures[idx_p->figure_idx];
1651 p[0] = &figure->vertices[idx_p->vertex_idx];
1652 next = idx_p->vertex_idx + 1;
1653 if (next == figure->vertex_count)
1654 next = 0;
1655 p[1] = &figure->vertices[next];
1657 figure = &geometry->u.path.figures[idx_q->figure_idx];
1658 q[0] = &figure->vertices[idx_q->vertex_idx];
1659 next = idx_q->vertex_idx + 1;
1660 if (next == figure->vertex_count)
1661 next = 0;
1662 q[1] = &figure->vertices[next];
1664 d2d_point_subtract(&v_p, p[1], p[0]);
1665 d2d_point_subtract(&v_q, q[1], q[0]);
1666 d2d_point_subtract(&v_qp, p[0], q[0]);
1668 det = v_p.x * v_q.y - v_p.y * v_q.x;
1669 if (det == 0.0f)
1670 return TRUE;
1672 s = (v_q.x * v_qp.y - v_q.y * v_qp.x) / det;
1673 t = (v_p.x * v_qp.y - v_p.y * v_qp.x) / det;
1675 if (s < 0.0f || s > 1.0f || t < 0.0f || t > 1.0f)
1676 return TRUE;
1678 intersection.x = p[0]->x + v_p.x * s;
1679 intersection.y = p[0]->y + v_p.y * s;
1681 if (s > 0.0f && s < 1.0f && !d2d_geometry_intersections_add(intersections, idx_p, s, intersection))
1682 return FALSE;
1684 if (t > 0.0f && t < 1.0f && !d2d_geometry_intersections_add(intersections, idx_q, t, intersection))
1685 return FALSE;
1687 return TRUE;
1690 static BOOL d2d_geometry_add_bezier_line_intersections(struct d2d_geometry *geometry,
1691 struct d2d_geometry_intersections *intersections, const struct d2d_segment_idx *idx_p,
1692 const D2D1_POINT_2F **p, const struct d2d_segment_idx *idx_q, const D2D1_POINT_2F **q, float s)
1694 D2D1_POINT_2F intersection;
1695 float t;
1697 d2d_point_calculate_bezier(&intersection, p[0], p[1], p[2], s);
1698 if (fabsf(q[1]->x - q[0]->x) > fabsf(q[1]->y - q[0]->y))
1699 t = (intersection.x - q[0]->x) / (q[1]->x - q[0]->x);
1700 else
1701 t = (intersection.y - q[0]->y) / (q[1]->y - q[0]->y);
1702 if (t < 0.0f || t > 1.0f)
1703 return TRUE;
1705 d2d_point_lerp(&intersection, q[0], q[1], t);
1707 if (s > 0.0f && s < 1.0f && !d2d_geometry_intersections_add(intersections, idx_p, s, intersection))
1708 return FALSE;
1710 if (t > 0.0f && t < 1.0f && !d2d_geometry_intersections_add(intersections, idx_q, t, intersection))
1711 return FALSE;
1713 return TRUE;
1716 static BOOL d2d_geometry_intersect_bezier_line(struct d2d_geometry *geometry,
1717 struct d2d_geometry_intersections *intersections,
1718 const struct d2d_segment_idx *idx_p, const struct d2d_segment_idx *idx_q)
1720 const D2D1_POINT_2F *p[3], *q[2];
1721 const struct d2d_figure *figure;
1722 float y[3], root, theta, d, e;
1723 size_t next;
1725 figure = &geometry->u.path.figures[idx_p->figure_idx];
1726 p[0] = &figure->vertices[idx_p->vertex_idx];
1727 p[1] = &figure->bezier_controls[idx_p->control_idx];
1728 next = idx_p->vertex_idx + 1;
1729 if (next == figure->vertex_count)
1730 next = 0;
1731 p[2] = &figure->vertices[next];
1733 figure = &geometry->u.path.figures[idx_q->figure_idx];
1734 q[0] = &figure->vertices[idx_q->vertex_idx];
1735 next = idx_q->vertex_idx + 1;
1736 if (next == figure->vertex_count)
1737 next = 0;
1738 q[1] = &figure->vertices[next];
1740 /* Align the line with x-axis. */
1741 theta = -atan2f(q[1]->y - q[0]->y, q[1]->x - q[0]->x);
1742 y[0] = (p[0]->x - q[0]->x) * sinf(theta) + (p[0]->y - q[0]->y) * cosf(theta);
1743 y[1] = (p[1]->x - q[0]->x) * sinf(theta) + (p[1]->y - q[0]->y) * cosf(theta);
1744 y[2] = (p[2]->x - q[0]->x) * sinf(theta) + (p[2]->y - q[0]->y) * cosf(theta);
1746 /* Intersect the transformed curve with the x-axis.
1748 * f(t) = (1 - t)²P₀ + 2(1 - t)tP₁ + t²P₂
1749 * = (P₀ - 2P₁ + P₂)t² + 2(P₁ - P₀)t + P₀
1751 * a = P₀ - 2P₁ + P₂
1752 * b = 2(P₁ - P₀)
1753 * c = P₀
1755 * f(t) = 0
1756 * t = (-b ± √(b² - 4ac)) / 2a
1757 * = (-2(P₁ - P₀) ± √((2(P₁ - P₀))² - 4((P₀ - 2P₁ + P₂)P₀))) / 2(P₀ - 2P₁ + P₂)
1758 * = (2P₀ - 2P₁ ± √(4P₀² + 4P₁² - 8P₀P₁ - 4P₀² + 8P₀P₁ - 4P₀P₂)) / (2P₀ - 4P₁ + 2P₂)
1759 * = (P₀ - P₁ ± √(P₁² - P₀P₂)) / (P₀ - 2P₁ + P₂) */
1761 d = y[0] - 2 * y[1] + y[2];
1762 if (d == 0.0f)
1764 /* P₀ - 2P₁ + P₂ = 0
1765 * f(t) = (P₀ - 2P₁ + P₂)t² + 2(P₁ - P₀)t + P₀ = 0
1766 * t = -P₀ / 2(P₁ - P₀) */
1767 root = -y[0] / (2.0f * (y[1] - y[0]));
1768 if (root < 0.0f || root > 1.0f)
1769 return TRUE;
1771 return d2d_geometry_add_bezier_line_intersections(geometry, intersections, idx_p, p, idx_q, q, root);
1774 e = y[1] * y[1] - y[0] * y[2];
1775 if (e < 0.0f)
1776 return TRUE;
1778 root = (y[0] - y[1] + sqrtf(e)) / d;
1779 if (root >= 0.0f && root <= 1.0f && !d2d_geometry_add_bezier_line_intersections(geometry,
1780 intersections, idx_p, p, idx_q, q, root))
1781 return FALSE;
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 return TRUE;
1791 static BOOL d2d_geometry_intersect_bezier_bezier(struct d2d_geometry *geometry,
1792 struct d2d_geometry_intersections *intersections,
1793 const struct d2d_segment_idx *idx_p, float start_p, float end_p,
1794 const struct d2d_segment_idx *idx_q, float start_q, float end_q)
1796 const D2D1_POINT_2F *p[3], *q[3];
1797 const struct d2d_figure *figure;
1798 D2D_RECT_F p_bounds, q_bounds;
1799 D2D1_POINT_2F intersection;
1800 float centre_p, centre_q;
1801 size_t next;
1803 figure = &geometry->u.path.figures[idx_p->figure_idx];
1804 p[0] = &figure->vertices[idx_p->vertex_idx];
1805 p[1] = &figure->bezier_controls[idx_p->control_idx];
1806 next = idx_p->vertex_idx + 1;
1807 if (next == figure->vertex_count)
1808 next = 0;
1809 p[2] = &figure->vertices[next];
1811 figure = &geometry->u.path.figures[idx_q->figure_idx];
1812 q[0] = &figure->vertices[idx_q->vertex_idx];
1813 q[1] = &figure->bezier_controls[idx_q->control_idx];
1814 next = idx_q->vertex_idx + 1;
1815 if (next == figure->vertex_count)
1816 next = 0;
1817 q[2] = &figure->vertices[next];
1819 d2d_rect_get_bezier_segment_bounds(&p_bounds, p[0], p[1], p[2], start_p, end_p);
1820 d2d_rect_get_bezier_segment_bounds(&q_bounds, q[0], q[1], q[2], start_q, end_q);
1822 if (!d2d_rect_check_overlap(&p_bounds, &q_bounds))
1823 return TRUE;
1825 centre_p = (start_p + end_p) / 2.0f;
1826 centre_q = (start_q + end_q) / 2.0f;
1828 if (end_p - start_p < 1e-3f)
1830 d2d_point_calculate_bezier(&intersection, p[0], p[1], p[2], centre_p);
1831 if (start_p > 0.0f && end_p < 1.0f && !d2d_geometry_intersections_add(intersections,
1832 idx_p, centre_p, intersection))
1833 return FALSE;
1834 if (start_q > 0.0f && end_q < 1.0f && !d2d_geometry_intersections_add(intersections,
1835 idx_q, centre_q, intersection))
1836 return FALSE;
1837 return TRUE;
1840 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1841 idx_p, start_p, centre_p, idx_q, start_q, centre_q))
1842 return FALSE;
1843 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1844 idx_p, start_p, centre_p, idx_q, centre_q, end_q))
1845 return FALSE;
1846 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1847 idx_p, centre_p, end_p, idx_q, start_q, centre_q))
1848 return FALSE;
1849 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1850 idx_p, centre_p, end_p, idx_q, centre_q, end_q))
1851 return FALSE;
1853 return TRUE;
1856 static BOOL d2d_geometry_apply_intersections(struct d2d_geometry *geometry,
1857 struct d2d_geometry_intersections *intersections)
1859 size_t vertex_offset, control_offset, next, i;
1860 struct d2d_geometry_intersection *inter;
1861 enum d2d_vertex_type vertex_type;
1862 const D2D1_POINT_2F *p[3];
1863 struct d2d_figure *figure;
1864 D2D1_POINT_2F q[2];
1865 float t, t_prev;
1867 for (i = 0; i < intersections->intersection_count; ++i)
1869 inter = &intersections->intersections[i];
1870 if (!i || inter->figure_idx != intersections->intersections[i - 1].figure_idx)
1871 vertex_offset = control_offset = 0;
1873 figure = &geometry->u.path.figures[inter->figure_idx];
1874 vertex_type = figure->vertex_types[inter->vertex_idx + vertex_offset];
1875 if (vertex_type != D2D_VERTEX_TYPE_BEZIER && vertex_type != D2D_VERTEX_TYPE_SPLIT_BEZIER)
1877 if (!d2d_figure_insert_vertex(&geometry->u.path.figures[inter->figure_idx],
1878 inter->vertex_idx + vertex_offset + 1, inter->p))
1879 return FALSE;
1880 ++vertex_offset;
1881 continue;
1884 t = inter->t;
1885 if (i && inter->figure_idx == intersections->intersections[i - 1].figure_idx
1886 && inter->vertex_idx == intersections->intersections[i - 1].vertex_idx)
1888 t_prev = intersections->intersections[i - 1].t;
1889 if (t - t_prev < 1e-3f)
1891 inter->t = intersections->intersections[i - 1].t;
1892 continue;
1894 t = (t - t_prev) / (1.0f - t_prev);
1897 p[0] = &figure->vertices[inter->vertex_idx + vertex_offset];
1898 p[1] = &figure->bezier_controls[inter->control_idx + control_offset];
1899 next = inter->vertex_idx + vertex_offset + 1;
1900 if (next == figure->vertex_count)
1901 next = 0;
1902 p[2] = &figure->vertices[next];
1904 d2d_point_lerp(&q[0], p[0], p[1], t);
1905 d2d_point_lerp(&q[1], p[1], p[2], t);
1907 figure->bezier_controls[inter->control_idx + control_offset] = q[0];
1908 if (!(d2d_figure_insert_bezier_control(figure, inter->control_idx + control_offset + 1, &q[1])))
1909 return FALSE;
1910 ++control_offset;
1912 if (!(d2d_figure_insert_vertex(figure, inter->vertex_idx + vertex_offset + 1, inter->p)))
1913 return FALSE;
1914 figure->vertex_types[inter->vertex_idx + vertex_offset + 1] = D2D_VERTEX_TYPE_SPLIT_BEZIER;
1915 ++vertex_offset;
1918 return TRUE;
1921 /* Intersect the geometry's segments with themselves. This uses the
1922 * straightforward approach of testing everything against everything, but
1923 * there certainly exist more scalable algorithms for this. */
1924 static BOOL d2d_geometry_intersect_self(struct d2d_geometry *geometry)
1926 struct d2d_geometry_intersections intersections = {0};
1927 const struct d2d_figure *figure_p, *figure_q;
1928 struct d2d_segment_idx idx_p, idx_q;
1929 enum d2d_vertex_type type_p, type_q;
1930 BOOL ret = FALSE;
1931 size_t max_q;
1933 if (!geometry->u.path.figure_count)
1934 return TRUE;
1936 for (idx_p.figure_idx = 0; idx_p.figure_idx < geometry->u.path.figure_count; ++idx_p.figure_idx)
1938 figure_p = &geometry->u.path.figures[idx_p.figure_idx];
1939 idx_p.control_idx = 0;
1940 for (idx_p.vertex_idx = 0; idx_p.vertex_idx < figure_p->vertex_count; ++idx_p.vertex_idx)
1942 type_p = figure_p->vertex_types[idx_p.vertex_idx];
1943 for (idx_q.figure_idx = 0; idx_q.figure_idx <= idx_p.figure_idx; ++idx_q.figure_idx)
1945 figure_q = &geometry->u.path.figures[idx_q.figure_idx];
1946 if (idx_q.figure_idx != idx_p.figure_idx)
1948 if (!d2d_rect_check_overlap(&figure_p->bounds, &figure_q->bounds))
1949 continue;
1950 max_q = figure_q->vertex_count;
1952 else
1954 max_q = idx_p.vertex_idx;
1957 idx_q.control_idx = 0;
1958 for (idx_q.vertex_idx = 0; idx_q.vertex_idx < max_q; ++idx_q.vertex_idx)
1960 type_q = figure_q->vertex_types[idx_q.vertex_idx];
1961 if (type_q == D2D_VERTEX_TYPE_BEZIER)
1963 if (type_p == D2D_VERTEX_TYPE_BEZIER)
1965 if (!d2d_geometry_intersect_bezier_bezier(geometry, &intersections,
1966 &idx_p, 0.0f, 1.0f, &idx_q, 0.0f, 1.0f))
1967 goto done;
1969 else
1971 if (!d2d_geometry_intersect_bezier_line(geometry, &intersections, &idx_q, &idx_p))
1972 goto done;
1974 ++idx_q.control_idx;
1976 else
1978 if (type_p == D2D_VERTEX_TYPE_BEZIER)
1980 if (!d2d_geometry_intersect_bezier_line(geometry, &intersections, &idx_p, &idx_q))
1981 goto done;
1983 else
1985 if (!d2d_geometry_intersect_line_line(geometry, &intersections, &idx_p, &idx_q))
1986 goto done;
1991 if (type_p == D2D_VERTEX_TYPE_BEZIER)
1992 ++idx_p.control_idx;
1996 qsort(intersections.intersections, intersections.intersection_count,
1997 sizeof(*intersections.intersections), d2d_geometry_intersections_compare);
1998 ret = d2d_geometry_apply_intersections(geometry, &intersections);
2000 done:
2001 heap_free(intersections.intersections);
2002 return ret;
2005 static HRESULT d2d_path_geometry_triangulate(struct d2d_geometry *geometry)
2007 struct d2d_cdt_edge_ref left_edge, right_edge;
2008 size_t vertex_count, i, j;
2009 struct d2d_cdt cdt = {0};
2010 D2D1_POINT_2F *vertices;
2012 for (i = 0, vertex_count = 0; i < geometry->u.path.figure_count; ++i)
2014 vertex_count += geometry->u.path.figures[i].vertex_count;
2017 if (vertex_count < 3)
2019 WARN("Geometry has %lu vertices.\n", (long)vertex_count);
2020 return S_OK;
2023 if (!(vertices = heap_calloc(vertex_count, sizeof(*vertices))))
2024 return E_OUTOFMEMORY;
2026 for (i = 0, j = 0; i < geometry->u.path.figure_count; ++i)
2028 memcpy(&vertices[j], geometry->u.path.figures[i].vertices,
2029 geometry->u.path.figures[i].vertex_count * sizeof(*vertices));
2030 j += geometry->u.path.figures[i].vertex_count;
2033 /* Sort vertices, eliminate duplicates. */
2034 qsort(vertices, vertex_count, sizeof(*vertices), d2d_cdt_compare_vertices);
2035 for (i = 1; i < vertex_count; ++i)
2037 if (!memcmp(&vertices[i - 1], &vertices[i], sizeof(*vertices)))
2039 --vertex_count;
2040 memmove(&vertices[i], &vertices[i + 1], (vertex_count - i) * sizeof(*vertices));
2041 --i;
2045 geometry->fill.vertices = vertices;
2046 geometry->fill.vertex_count = vertex_count;
2048 cdt.free_edge = ~0u;
2049 cdt.vertices = vertices;
2050 if (!d2d_cdt_triangulate(&cdt, 0, vertex_count, &left_edge, &right_edge))
2051 goto fail;
2052 if (!d2d_cdt_insert_segments(&cdt, geometry))
2053 goto fail;
2054 if (!d2d_cdt_generate_faces(&cdt, geometry))
2055 goto fail;
2057 heap_free(cdt.edges);
2058 return S_OK;
2060 fail:
2061 geometry->fill.vertices = NULL;
2062 geometry->fill.vertex_count = 0;
2063 heap_free(vertices);
2064 heap_free(cdt.edges);
2065 return E_FAIL;
2068 static BOOL d2d_path_geometry_add_figure(struct d2d_geometry *geometry)
2070 struct d2d_figure *figure;
2072 if (!d2d_array_reserve((void **)&geometry->u.path.figures, &geometry->u.path.figures_size,
2073 geometry->u.path.figure_count + 1, sizeof(*geometry->u.path.figures)))
2075 ERR("Failed to grow figures array.\n");
2076 return FALSE;
2079 figure = &geometry->u.path.figures[geometry->u.path.figure_count];
2080 memset(figure, 0, sizeof(*figure));
2081 figure->bounds.left = FLT_MAX;
2082 figure->bounds.top = FLT_MAX;
2083 figure->bounds.right = -FLT_MAX;
2084 figure->bounds.bottom = -FLT_MAX;
2086 ++geometry->u.path.figure_count;
2087 return TRUE;
2090 static BOOL d2d_geometry_outline_add_join(struct d2d_geometry *geometry,
2091 const D2D1_POINT_2F *prev, const D2D1_POINT_2F *p0, const D2D1_POINT_2F *next)
2093 D2D1_POINT_2F q_prev, q_next;
2094 struct d2d_outline_vertex *v;
2095 struct d2d_face *f;
2096 size_t base_idx;
2097 float ccw;
2099 if (!d2d_array_reserve((void **)&geometry->outline.vertices, &geometry->outline.vertices_size,
2100 geometry->outline.vertex_count + 4, sizeof(*geometry->outline.vertices)))
2102 ERR("Failed to grow outline vertices array.\n");
2103 return FALSE;
2105 base_idx = geometry->outline.vertex_count;
2106 v = &geometry->outline.vertices[base_idx];
2108 if (!d2d_array_reserve((void **)&geometry->outline.faces, &geometry->outline.faces_size,
2109 geometry->outline.face_count + 2, sizeof(*geometry->outline.faces)))
2111 ERR("Failed to grow outline faces array.\n");
2112 return FALSE;
2114 f = &geometry->outline.faces[geometry->outline.face_count];
2116 d2d_point_subtract(&q_prev, p0, prev);
2117 d2d_point_subtract(&q_next, next, p0);
2119 d2d_point_normalise(&q_prev);
2120 d2d_point_normalise(&q_next);
2122 ccw = d2d_point_ccw(p0, prev, next);
2123 if (ccw == 0.0f)
2125 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2126 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2127 d2d_outline_vertex_set(&v[2], p0->x + 25.0f * q_prev.x, p0->y + 25.0f * q_prev.y,
2128 -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2129 d2d_outline_vertex_set(&v[3], p0->x + 25.0f * q_prev.x, p0->y + 25.0f * q_prev.y,
2130 q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2132 else if (ccw < 0.0f)
2134 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_next.x, q_next.y, q_prev.x, q_prev.y);
2135 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2136 d2d_outline_vertex_set(&v[2], p0->x, p0->y, -q_next.x, -q_next.y, -q_prev.x, -q_prev.y);
2137 d2d_outline_vertex_set(&v[3], p0->x, p0->y, -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2139 else
2141 d2d_outline_vertex_set(&v[0], p0->x, p0->y, -q_prev.x, -q_prev.y, -q_next.x, -q_next.y);
2142 d2d_outline_vertex_set(&v[1], p0->x, p0->y, q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2143 d2d_outline_vertex_set(&v[2], p0->x, p0->y, q_prev.x, q_prev.y, q_next.x, q_next.y);
2144 d2d_outline_vertex_set(&v[3], p0->x, p0->y, q_next.x, q_next.y, q_next.x, q_next.y);
2146 geometry->outline.vertex_count += 4;
2148 d2d_face_set(&f[0], base_idx + 1, base_idx + 0, base_idx + 2);
2149 d2d_face_set(&f[1], base_idx + 2, base_idx + 0, base_idx + 3);
2150 geometry->outline.face_count += 2;
2152 return TRUE;
2155 static BOOL d2d_geometry_outline_add_line_segment(struct d2d_geometry *geometry,
2156 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *next)
2158 struct d2d_outline_vertex *v;
2159 D2D1_POINT_2F q_next;
2160 struct d2d_face *f;
2161 size_t base_idx;
2163 if (!d2d_array_reserve((void **)&geometry->outline.vertices, &geometry->outline.vertices_size,
2164 geometry->outline.vertex_count + 4, sizeof(*geometry->outline.vertices)))
2166 ERR("Failed to grow outline vertices array.\n");
2167 return FALSE;
2169 base_idx = geometry->outline.vertex_count;
2170 v = &geometry->outline.vertices[base_idx];
2172 if (!d2d_array_reserve((void **)&geometry->outline.faces, &geometry->outline.faces_size,
2173 geometry->outline.face_count + 2, sizeof(*geometry->outline.faces)))
2175 ERR("Failed to grow outline faces array.\n");
2176 return FALSE;
2178 f = &geometry->outline.faces[geometry->outline.face_count];
2180 d2d_point_subtract(&q_next, next, p0);
2181 d2d_point_normalise(&q_next);
2183 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_next.x, q_next.y, q_next.x, q_next.y);
2184 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2185 d2d_outline_vertex_set(&v[2], next->x, next->y, q_next.x, q_next.y, q_next.x, q_next.y);
2186 d2d_outline_vertex_set(&v[3], next->x, next->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2187 geometry->outline.vertex_count += 4;
2189 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2190 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2191 geometry->outline.face_count += 2;
2193 return TRUE;
2196 static BOOL d2d_geometry_outline_add_bezier_segment(struct d2d_geometry *geometry,
2197 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
2199 struct d2d_bezier_outline_vertex *b;
2200 D2D1_POINT_2F r0, r1, r2;
2201 D2D1_POINT_2F q0, q1, q2;
2202 struct d2d_face *f;
2203 size_t base_idx;
2205 if (!d2d_array_reserve((void **)&geometry->outline.beziers, &geometry->outline.beziers_size,
2206 geometry->outline.bezier_count + 7, sizeof(*geometry->outline.beziers)))
2208 ERR("Failed to grow outline beziers array.\n");
2209 return FALSE;
2211 base_idx = geometry->outline.bezier_count;
2212 b = &geometry->outline.beziers[base_idx];
2214 if (!d2d_array_reserve((void **)&geometry->outline.bezier_faces, &geometry->outline.bezier_faces_size,
2215 geometry->outline.bezier_face_count + 5, sizeof(*geometry->outline.bezier_faces)))
2217 ERR("Failed to grow outline faces array.\n");
2218 return FALSE;
2220 f = &geometry->outline.bezier_faces[geometry->outline.bezier_face_count];
2222 d2d_point_lerp(&q0, p0, p1, 0.5f);
2223 d2d_point_lerp(&q1, p1, p2, 0.5f);
2224 d2d_point_lerp(&q2, &q0, &q1, 0.5f);
2226 d2d_point_subtract(&r0, &q0, p0);
2227 d2d_point_subtract(&r1, &q1, &q0);
2228 d2d_point_subtract(&r2, p2, &q1);
2230 d2d_point_normalise(&r0);
2231 d2d_point_normalise(&r1);
2232 d2d_point_normalise(&r2);
2234 if (d2d_point_ccw(p0, p1, p2) > 0.0f)
2236 d2d_point_scale(&r0, -1.0f);
2237 d2d_point_scale(&r1, -1.0f);
2238 d2d_point_scale(&r2, -1.0f);
2241 d2d_bezier_outline_vertex_set(&b[0], p0, p0, p1, p2, r0.x, r0.y, r0.x, r0.y);
2242 d2d_bezier_outline_vertex_set(&b[1], p0, p0, p1, p2, -r0.x, -r0.y, -r0.x, -r0.y);
2243 d2d_bezier_outline_vertex_set(&b[2], &q0, p0, p1, p2, r0.x, r0.y, r1.x, r1.y);
2244 d2d_bezier_outline_vertex_set(&b[3], &q2, p0, p1, p2, -r1.x, -r1.y, -r1.x, -r1.y);
2245 d2d_bezier_outline_vertex_set(&b[4], &q1, p0, p1, p2, r1.x, r1.y, r2.x, r2.y);
2246 d2d_bezier_outline_vertex_set(&b[5], p2, p0, p1, p2, -r2.x, -r2.y, -r2.x, -r2.y);
2247 d2d_bezier_outline_vertex_set(&b[6], p2, p0, p1, p2, r2.x, r2.y, r2.x, r2.y);
2248 geometry->outline.bezier_count += 7;
2250 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2251 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2252 d2d_face_set(&f[2], base_idx + 3, base_idx + 4, base_idx + 2);
2253 d2d_face_set(&f[3], base_idx + 5, base_idx + 4, base_idx + 3);
2254 d2d_face_set(&f[4], base_idx + 5, base_idx + 6, base_idx + 4);
2255 geometry->outline.bezier_face_count += 5;
2257 return TRUE;
2260 static BOOL d2d_geometry_outline_add_arc_quadrant(struct d2d_geometry *geometry,
2261 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
2263 FIXME("Approximating arc quadrant with Bezier curve.\n");
2265 return d2d_geometry_outline_add_bezier_segment(geometry, p0, p1, p2);
2268 static BOOL d2d_geometry_add_figure_outline(struct d2d_geometry *geometry,
2269 struct d2d_figure *figure, D2D1_FIGURE_END figure_end)
2271 const D2D1_POINT_2F *prev, *p0, *next;
2272 enum d2d_vertex_type prev_type, type;
2273 size_t bezier_idx, i;
2275 for (i = 0, bezier_idx = 0; i < figure->vertex_count; ++i)
2277 type = figure->vertex_types[i];
2278 if (type == D2D_VERTEX_TYPE_NONE)
2279 continue;
2281 p0 = &figure->vertices[i];
2283 if (!i)
2285 prev_type = figure->vertex_types[figure->vertex_count - 1];
2286 if (prev_type == D2D_VERTEX_TYPE_BEZIER)
2287 prev = &figure->bezier_controls[figure->bezier_control_count - 1];
2288 else
2289 prev = &figure->vertices[figure->vertex_count - 1];
2291 else
2293 prev_type = figure->vertex_types[i - 1];
2294 if (prev_type == D2D_VERTEX_TYPE_BEZIER)
2295 prev = &figure->bezier_controls[bezier_idx - 1];
2296 else
2297 prev = &figure->vertices[i - 1];
2300 if (type == D2D_VERTEX_TYPE_BEZIER)
2301 next = &figure->bezier_controls[bezier_idx++];
2302 else if (i == figure->vertex_count - 1)
2303 next = &figure->vertices[0];
2304 else
2305 next = &figure->vertices[i + 1];
2307 if ((figure_end == D2D1_FIGURE_END_CLOSED || (i && i < figure->vertex_count - 1))
2308 && !d2d_geometry_outline_add_join(geometry, prev, p0, next))
2310 ERR("Failed to add join.\n");
2311 return FALSE;
2314 if (type == D2D_VERTEX_TYPE_LINE && (figure_end == D2D1_FIGURE_END_CLOSED || i < figure->vertex_count - 1)
2315 && !d2d_geometry_outline_add_line_segment(geometry, p0, next))
2317 ERR("Failed to add line segment.\n");
2318 return FALSE;
2320 else if (type == D2D_VERTEX_TYPE_BEZIER)
2322 const D2D1_POINT_2F *p2;
2324 if (i == figure->vertex_count - 1)
2325 p2 = &figure->vertices[0];
2326 else
2327 p2 = &figure->vertices[i + 1];
2329 if (!d2d_geometry_outline_add_bezier_segment(geometry, p0, next, p2))
2331 ERR("Failed to add bezier segment.\n");
2332 return FALSE;
2337 return TRUE;
2340 static BOOL d2d_geometry_fill_add_arc_triangle(struct d2d_geometry *geometry,
2341 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
2343 struct d2d_bezier_vertex *b;
2345 FIXME("Approximating arc triangle with Bezier triangle.\n");
2347 if (!d2d_array_reserve((void **)&geometry->fill.bezier_vertices, &geometry->fill.bezier_vertices_size,
2348 geometry->fill.bezier_vertex_count + 3, sizeof(*geometry->fill.bezier_vertices)))
2349 return FALSE;
2351 b = &geometry->fill.bezier_vertices[geometry->fill.bezier_vertex_count];
2352 d2d_bezier_vertex_set(&b[0], p0, 0.0f, 0.0f, -1.0f);
2353 d2d_bezier_vertex_set(&b[1], p1, 0.5f, 0.0f, -1.0f);
2354 d2d_bezier_vertex_set(&b[2], p2, 1.0f, 1.0f, -1.0f);
2355 geometry->fill.bezier_vertex_count += 3;
2357 return TRUE;
2360 static void d2d_geometry_cleanup(struct d2d_geometry *geometry)
2362 heap_free(geometry->outline.bezier_faces);
2363 heap_free(geometry->outline.beziers);
2364 heap_free(geometry->outline.faces);
2365 heap_free(geometry->outline.vertices);
2366 heap_free(geometry->fill.bezier_vertices);
2367 heap_free(geometry->fill.faces);
2368 heap_free(geometry->fill.vertices);
2369 ID2D1Factory_Release(geometry->factory);
2372 static void d2d_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
2373 const D2D1_MATRIX_3X2_F *transform, const struct ID2D1GeometryVtbl *vtbl)
2375 geometry->ID2D1Geometry_iface.lpVtbl = vtbl;
2376 geometry->refcount = 1;
2377 ID2D1Factory_AddRef(geometry->factory = factory);
2378 geometry->transform = *transform;
2381 static inline struct d2d_geometry *impl_from_ID2D1GeometrySink(ID2D1GeometrySink *iface)
2383 return CONTAINING_RECORD(iface, struct d2d_geometry, u.path.ID2D1GeometrySink_iface);
2386 static HRESULT STDMETHODCALLTYPE d2d_geometry_sink_QueryInterface(ID2D1GeometrySink *iface, REFIID iid, void **out)
2388 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
2390 if (IsEqualGUID(iid, &IID_ID2D1GeometrySink)
2391 || IsEqualGUID(iid, &IID_ID2D1SimplifiedGeometrySink)
2392 || IsEqualGUID(iid, &IID_IUnknown))
2394 ID2D1GeometrySink_AddRef(iface);
2395 *out = iface;
2396 return S_OK;
2399 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
2401 *out = NULL;
2402 return E_NOINTERFACE;
2405 static ULONG STDMETHODCALLTYPE d2d_geometry_sink_AddRef(ID2D1GeometrySink *iface)
2407 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2409 TRACE("iface %p.\n", iface);
2411 return ID2D1Geometry_AddRef(&geometry->ID2D1Geometry_iface);
2414 static ULONG STDMETHODCALLTYPE d2d_geometry_sink_Release(ID2D1GeometrySink *iface)
2416 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2418 TRACE("iface %p.\n", iface);
2420 return ID2D1Geometry_Release(&geometry->ID2D1Geometry_iface);
2423 static void STDMETHODCALLTYPE d2d_geometry_sink_SetFillMode(ID2D1GeometrySink *iface, D2D1_FILL_MODE mode)
2425 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2427 TRACE("iface %p, mode %#x.\n", iface, mode);
2429 if (geometry->u.path.state == D2D_GEOMETRY_STATE_CLOSED)
2430 return;
2431 geometry->u.path.fill_mode = mode;
2434 static void STDMETHODCALLTYPE d2d_geometry_sink_SetSegmentFlags(ID2D1GeometrySink *iface, D2D1_PATH_SEGMENT flags)
2436 FIXME("iface %p, flags %#x stub!\n", iface, flags);
2439 static void STDMETHODCALLTYPE d2d_geometry_sink_BeginFigure(ID2D1GeometrySink *iface,
2440 D2D1_POINT_2F start_point, D2D1_FIGURE_BEGIN figure_begin)
2442 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2443 struct d2d_figure *figure;
2445 TRACE("iface %p, start_point %s, figure_begin %#x.\n",
2446 iface, debug_d2d_point_2f(&start_point), figure_begin);
2448 if (geometry->u.path.state != D2D_GEOMETRY_STATE_OPEN)
2450 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2451 return;
2454 if (figure_begin != D2D1_FIGURE_BEGIN_FILLED)
2455 FIXME("Ignoring figure_begin %#x.\n", figure_begin);
2457 if (!d2d_path_geometry_add_figure(geometry))
2459 ERR("Failed to add figure.\n");
2460 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2461 return;
2464 figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2465 if (figure_begin == D2D1_FIGURE_BEGIN_HOLLOW)
2466 figure->flags |= D2D_FIGURE_FLAG_HOLLOW;
2468 if (!d2d_figure_add_vertex(figure, start_point))
2470 ERR("Failed to add vertex.\n");
2471 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2472 return;
2475 geometry->u.path.state = D2D_GEOMETRY_STATE_FIGURE;
2478 static void STDMETHODCALLTYPE d2d_geometry_sink_AddLines(ID2D1GeometrySink *iface,
2479 const D2D1_POINT_2F *points, UINT32 count)
2481 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2482 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2483 unsigned int i;
2485 TRACE("iface %p, points %p, count %u.\n", iface, points, count);
2487 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2489 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2490 return;
2493 for (i = 0; i < count; ++i)
2495 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_LINE;
2496 if (!d2d_figure_add_vertex(figure, points[i]))
2498 ERR("Failed to add vertex.\n");
2499 return;
2503 geometry->u.path.segment_count += count;
2506 static void STDMETHODCALLTYPE d2d_geometry_sink_AddBeziers(ID2D1GeometrySink *iface,
2507 const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
2509 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2510 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2511 D2D1_POINT_2F p;
2512 unsigned int i;
2514 TRACE("iface %p, beziers %p, count %u.\n", iface, beziers, count);
2516 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2518 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2519 return;
2522 for (i = 0; i < count; ++i)
2524 D2D1_RECT_F bezier_bounds;
2526 /* FIXME: This tries to approximate a cubic bezier with a quadratic one. */
2527 p.x = (beziers[i].point1.x + beziers[i].point2.x) * 0.75f;
2528 p.y = (beziers[i].point1.y + beziers[i].point2.y) * 0.75f;
2529 p.x -= (figure->vertices[figure->vertex_count - 1].x + beziers[i].point3.x) * 0.25f;
2530 p.y -= (figure->vertices[figure->vertex_count - 1].y + beziers[i].point3.y) * 0.25f;
2531 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_BEZIER;
2533 d2d_rect_get_bezier_bounds(&bezier_bounds, &figure->vertices[figure->vertex_count - 1],
2534 &p, &beziers[i].point3);
2536 if (!d2d_figure_add_bezier_control(figure, &p))
2538 ERR("Failed to add bezier control.\n");
2539 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2540 return;
2543 if (!d2d_figure_add_vertex(figure, beziers[i].point3))
2545 ERR("Failed to add bezier vertex.\n");
2546 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2547 return;
2550 d2d_rect_union(&figure->bounds, &bezier_bounds);
2553 geometry->u.path.segment_count += count;
2556 static void STDMETHODCALLTYPE d2d_geometry_sink_EndFigure(ID2D1GeometrySink *iface, D2D1_FIGURE_END figure_end)
2558 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2559 struct d2d_figure *figure;
2561 TRACE("iface %p, figure_end %#x.\n", iface, figure_end);
2563 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2565 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2566 return;
2569 figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2570 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_LINE;
2571 if (figure_end == D2D1_FIGURE_END_CLOSED)
2573 ++geometry->u.path.segment_count;
2574 figure->flags |= D2D_FIGURE_FLAG_CLOSED;
2575 if (!memcmp(&figure->vertices[0], &figure->vertices[figure->vertex_count - 1], sizeof(*figure->vertices)))
2576 --figure->vertex_count;
2579 if (!d2d_geometry_add_figure_outline(geometry, figure, figure_end))
2581 ERR("Failed to add figure outline.\n");
2582 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2583 return;
2586 geometry->u.path.state = D2D_GEOMETRY_STATE_OPEN;
2589 static void d2d_path_geometry_free_figures(struct d2d_geometry *geometry)
2591 size_t i;
2593 if (!geometry->u.path.figures)
2594 return;
2596 for (i = 0; i < geometry->u.path.figure_count; ++i)
2598 heap_free(geometry->u.path.figures[i].bezier_controls);
2599 heap_free(geometry->u.path.figures[i].original_bezier_controls);
2600 heap_free(geometry->u.path.figures[i].vertices);
2602 heap_free(geometry->u.path.figures);
2603 geometry->u.path.figures = NULL;
2604 geometry->u.path.figures_size = 0;
2607 static BOOL d2d_geometry_get_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx, BOOL next)
2609 if (next)
2611 ++idx->vertex_idx;
2612 ++idx->control_idx;
2615 for (; idx->figure_idx < geometry->u.path.figure_count; ++idx->figure_idx, idx->vertex_idx = idx->control_idx = 0)
2617 struct d2d_figure *figure = &geometry->u.path.figures[idx->figure_idx];
2619 if (!figure->bezier_control_count)
2620 continue;
2622 for (; idx->vertex_idx < figure->vertex_count; ++idx->vertex_idx)
2624 if (figure->vertex_types[idx->vertex_idx] == D2D_VERTEX_TYPE_BEZIER
2625 || figure->vertex_types[idx->vertex_idx] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
2626 return TRUE;
2630 return FALSE;
2633 static BOOL d2d_geometry_get_first_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx)
2635 memset(idx, 0, sizeof(*idx));
2637 return d2d_geometry_get_bezier_segment_idx(geometry, idx, FALSE);
2640 static BOOL d2d_geometry_get_next_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx)
2642 return d2d_geometry_get_bezier_segment_idx(geometry, idx, TRUE);
2645 static BOOL d2d_geometry_check_bezier_overlap(struct d2d_geometry *geometry,
2646 const struct d2d_segment_idx *idx_p, const struct d2d_segment_idx *idx_q)
2648 const D2D1_POINT_2F *a[3], *b[3], *p[2], *q;
2649 const struct d2d_figure *figure;
2650 D2D1_POINT_2F v_q[3], v_p, v_qp;
2651 unsigned int i, j, score;
2652 float det, t;
2654 figure = &geometry->u.path.figures[idx_p->figure_idx];
2655 a[0] = &figure->vertices[idx_p->vertex_idx];
2656 a[1] = &figure->bezier_controls[idx_p->control_idx];
2657 if (idx_p->vertex_idx == figure->vertex_count - 1)
2658 a[2] = &figure->vertices[0];
2659 else
2660 a[2] = &figure->vertices[idx_p->vertex_idx + 1];
2662 figure = &geometry->u.path.figures[idx_q->figure_idx];
2663 b[0] = &figure->vertices[idx_q->vertex_idx];
2664 b[1] = &figure->bezier_controls[idx_q->control_idx];
2665 if (idx_q->vertex_idx == figure->vertex_count - 1)
2666 b[2] = &figure->vertices[0];
2667 else
2668 b[2] = &figure->vertices[idx_q->vertex_idx + 1];
2670 if (d2d_point_ccw(a[0], a[1], a[2]) == 0.0f || d2d_point_ccw(b[0], b[1], b[2]) == 0.0f)
2671 return FALSE;
2673 d2d_point_subtract(&v_q[0], b[1], b[0]);
2674 d2d_point_subtract(&v_q[1], b[2], b[0]);
2675 d2d_point_subtract(&v_q[2], b[1], b[2]);
2677 /* Check for intersections between the edges. Strictly speaking we'd only
2678 * need to check 8 of the 9 possible intersections, since if there's any
2679 * intersection there has to be a second intersection as well. */
2680 for (i = 0; i < 3; ++i)
2682 d2d_point_subtract(&v_p, a[(i & 1) + 1], a[i & 2]);
2683 for (j = 0; j < 3; ++j)
2685 det = v_p.x * v_q[j].y - v_p.y * v_q[j].x;
2686 if (det == 0.0f)
2687 continue;
2689 d2d_point_subtract(&v_qp, a[i & 2], b[j & 2]);
2690 t = (v_q[j].x * v_qp.y - v_q[j].y * v_qp.x) / det;
2691 if (t <= 0.0f || t >= 1.0f)
2692 continue;
2694 t = (v_p.x * v_qp.y - v_p.y * v_qp.x) / det;
2695 if (t <= 0.0f || t >= 1.0f)
2696 continue;
2698 return TRUE;
2702 /* Check if one triangle is contained within the other. */
2703 for (j = 0, score = 0, q = a[1], p[0] = b[2]; j < 3; ++j)
2705 p[1] = b[j];
2706 d2d_point_subtract(&v_p, p[1], p[0]);
2707 d2d_point_subtract(&v_qp, q, p[0]);
2709 if ((q->y < p[0]->y) != (q->y < p[1]->y) && v_qp.x < v_p.x * (v_qp.y / v_p.y))
2710 ++score;
2712 p[0] = p[1];
2715 if (score & 1)
2716 return TRUE;
2718 for (j = 0, score = 0, q = b[1], p[0] = a[2]; j < 3; ++j)
2720 p[1] = a[j];
2721 d2d_point_subtract(&v_p, p[1], p[0]);
2722 d2d_point_subtract(&v_qp, q, p[0]);
2724 if ((q->y < p[0]->y) != (q->y < p[1]->y) && v_qp.x < v_p.x * (v_qp.y / v_p.y))
2725 ++score;
2727 p[0] = p[1];
2730 return score & 1;
2733 static float d2d_geometry_bezier_ccw(struct d2d_geometry *geometry, const struct d2d_segment_idx *idx)
2735 const struct d2d_figure *figure = &geometry->u.path.figures[idx->figure_idx];
2736 size_t next = idx->vertex_idx + 1;
2738 if (next == figure->vertex_count)
2739 next = 0;
2741 return d2d_point_ccw(&figure->vertices[idx->vertex_idx],
2742 &figure->bezier_controls[idx->control_idx], &figure->vertices[next]);
2745 static BOOL d2d_geometry_split_bezier(struct d2d_geometry *geometry, const struct d2d_segment_idx *idx)
2747 const D2D1_POINT_2F *p[3];
2748 struct d2d_figure *figure;
2749 D2D1_POINT_2F q[3];
2750 size_t next;
2752 figure = &geometry->u.path.figures[idx->figure_idx];
2753 p[0] = &figure->vertices[idx->vertex_idx];
2754 p[1] = &figure->bezier_controls[idx->control_idx];
2755 next = idx->vertex_idx + 1;
2756 if (next == figure->vertex_count)
2757 next = 0;
2758 p[2] = &figure->vertices[next];
2760 d2d_point_lerp(&q[0], p[0], p[1], 0.5f);
2761 d2d_point_lerp(&q[1], p[1], p[2], 0.5f);
2762 d2d_point_lerp(&q[2], &q[0], &q[1], 0.5f);
2764 figure->bezier_controls[idx->control_idx] = q[0];
2765 if (!(d2d_figure_insert_bezier_control(figure, idx->control_idx + 1, &q[1])))
2766 return FALSE;
2767 if (!(d2d_figure_insert_vertex(figure, idx->vertex_idx + 1, q[2])))
2768 return FALSE;
2769 figure->vertex_types[idx->vertex_idx + 1] = D2D_VERTEX_TYPE_SPLIT_BEZIER;
2771 return TRUE;
2774 static HRESULT d2d_geometry_resolve_beziers(struct d2d_geometry *geometry)
2776 struct d2d_segment_idx idx_p, idx_q;
2777 struct d2d_bezier_vertex *b;
2778 const D2D1_POINT_2F *p[3];
2779 struct d2d_figure *figure;
2780 size_t bezier_idx, i;
2782 if (!d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_p))
2783 return S_OK;
2785 /* Split overlapping bezier control triangles. */
2786 while (d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_p))
2788 d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_q);
2789 while (idx_q.figure_idx < idx_p.figure_idx || idx_q.vertex_idx < idx_p.vertex_idx)
2791 while (d2d_geometry_check_bezier_overlap(geometry, &idx_p, &idx_q))
2793 if (fabsf(d2d_geometry_bezier_ccw(geometry, &idx_q)) > fabsf(d2d_geometry_bezier_ccw(geometry, &idx_p)))
2795 if (!d2d_geometry_split_bezier(geometry, &idx_q))
2796 return E_OUTOFMEMORY;
2797 if (idx_p.figure_idx == idx_q.figure_idx)
2799 ++idx_p.vertex_idx;
2800 ++idx_p.control_idx;
2803 else
2805 if (!d2d_geometry_split_bezier(geometry, &idx_p))
2806 return E_OUTOFMEMORY;
2809 d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_q);
2813 for (i = 0; i < geometry->u.path.figure_count; ++i)
2815 geometry->fill.bezier_vertex_count += 3 * geometry->u.path.figures[i].bezier_control_count;
2818 if (!(geometry->fill.bezier_vertices = heap_calloc(geometry->fill.bezier_vertex_count,
2819 sizeof(*geometry->fill.bezier_vertices))))
2821 ERR("Failed to allocate bezier vertices array.\n");
2822 geometry->fill.bezier_vertex_count = 0;
2823 return E_OUTOFMEMORY;
2826 bezier_idx = 0;
2827 d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_p);
2828 for (;;)
2830 float sign = -1.0f;
2832 figure = &geometry->u.path.figures[idx_p.figure_idx];
2833 p[0] = &figure->vertices[idx_p.vertex_idx];
2834 p[1] = &figure->bezier_controls[idx_p.control_idx];
2836 i = idx_p.vertex_idx + 1;
2837 if (d2d_path_geometry_point_inside(geometry, p[1], FALSE))
2839 sign = 1.0f;
2840 d2d_figure_insert_vertex(figure, i, *p[1]);
2841 /* Inserting a vertex potentially invalidates p[0]. */
2842 p[0] = &figure->vertices[idx_p.vertex_idx];
2843 ++i;
2846 if (i == figure->vertex_count)
2847 i = 0;
2848 p[2] = &figure->vertices[i];
2850 b = &geometry->fill.bezier_vertices[bezier_idx * 3];
2851 d2d_bezier_vertex_set(&b[0], p[0], 0.0f, 0.0f, sign);
2852 d2d_bezier_vertex_set(&b[1], p[1], 0.5f, 0.0f, sign);
2853 d2d_bezier_vertex_set(&b[2], p[2], 1.0f, 1.0f, sign);
2855 if (!d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_p))
2856 break;
2857 ++bezier_idx;
2860 return S_OK;
2863 static HRESULT STDMETHODCALLTYPE d2d_geometry_sink_Close(ID2D1GeometrySink *iface)
2865 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2866 HRESULT hr = E_FAIL;
2867 size_t i;
2869 TRACE("iface %p.\n", iface);
2871 if (geometry->u.path.state != D2D_GEOMETRY_STATE_OPEN)
2873 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
2874 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2875 return D2DERR_WRONG_STATE;
2877 geometry->u.path.state = D2D_GEOMETRY_STATE_CLOSED;
2879 for (i = 0; i < geometry->u.path.figure_count; ++i)
2881 struct d2d_figure *figure = &geometry->u.path.figures[i];
2882 size_t size = figure->bezier_control_count * sizeof(*figure->original_bezier_controls);
2883 if (!(figure->original_bezier_controls = heap_alloc(size)))
2884 goto done;
2885 memcpy(figure->original_bezier_controls, figure->bezier_controls, size);
2888 if (!d2d_geometry_intersect_self(geometry))
2889 goto done;
2890 if (FAILED(hr = d2d_geometry_resolve_beziers(geometry)))
2891 goto done;
2892 if (FAILED(hr = d2d_path_geometry_triangulate(geometry)))
2893 goto done;
2895 done:
2896 if (FAILED(hr))
2898 heap_free(geometry->fill.bezier_vertices);
2899 geometry->fill.bezier_vertex_count = 0;
2900 d2d_path_geometry_free_figures(geometry);
2901 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2903 return hr;
2906 static void STDMETHODCALLTYPE d2d_geometry_sink_AddLine(ID2D1GeometrySink *iface, D2D1_POINT_2F point)
2908 TRACE("iface %p, point %s.\n", iface, debug_d2d_point_2f(&point));
2910 d2d_geometry_sink_AddLines(iface, &point, 1);
2913 static void STDMETHODCALLTYPE d2d_geometry_sink_AddBezier(ID2D1GeometrySink *iface, const D2D1_BEZIER_SEGMENT *bezier)
2915 TRACE("iface %p, bezier %p.\n", iface, bezier);
2917 d2d_geometry_sink_AddBeziers(iface, bezier, 1);
2920 static void STDMETHODCALLTYPE d2d_geometry_sink_AddQuadraticBezier(ID2D1GeometrySink *iface,
2921 const D2D1_QUADRATIC_BEZIER_SEGMENT *bezier)
2923 TRACE("iface %p, bezier %p.\n", iface, bezier);
2925 ID2D1GeometrySink_AddQuadraticBeziers(iface, bezier, 1);
2928 static void STDMETHODCALLTYPE d2d_geometry_sink_AddQuadraticBeziers(ID2D1GeometrySink *iface,
2929 const D2D1_QUADRATIC_BEZIER_SEGMENT *beziers, UINT32 bezier_count)
2931 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2932 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2933 unsigned int i;
2935 TRACE("iface %p, beziers %p, bezier_count %u.\n", iface, beziers, bezier_count);
2937 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2939 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2940 return;
2943 for (i = 0; i < bezier_count; ++i)
2945 D2D1_RECT_F bezier_bounds;
2947 d2d_rect_get_bezier_bounds(&bezier_bounds, &figure->vertices[figure->vertex_count - 1],
2948 &beziers[i].point1, &beziers[i].point2);
2950 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_BEZIER;
2951 if (!d2d_figure_add_bezier_control(figure, &beziers[i].point1))
2953 ERR("Failed to add bezier.\n");
2954 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2955 return;
2958 if (!d2d_figure_add_vertex(figure, beziers[i].point2))
2960 ERR("Failed to add bezier vertex.\n");
2961 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2962 return;
2965 d2d_rect_union(&figure->bounds, &bezier_bounds);
2968 geometry->u.path.segment_count += bezier_count;
2971 static void STDMETHODCALLTYPE d2d_geometry_sink_AddArc(ID2D1GeometrySink *iface, const D2D1_ARC_SEGMENT *arc)
2973 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2975 FIXME("iface %p, arc %p stub!\n", iface, arc);
2977 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2979 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2980 return;
2983 if (!d2d_figure_add_vertex(&geometry->u.path.figures[geometry->u.path.figure_count - 1], arc->point))
2985 ERR("Failed to add vertex.\n");
2986 return;
2989 ++geometry->u.path.segment_count;
2992 static const struct ID2D1GeometrySinkVtbl d2d_geometry_sink_vtbl =
2994 d2d_geometry_sink_QueryInterface,
2995 d2d_geometry_sink_AddRef,
2996 d2d_geometry_sink_Release,
2997 d2d_geometry_sink_SetFillMode,
2998 d2d_geometry_sink_SetSegmentFlags,
2999 d2d_geometry_sink_BeginFigure,
3000 d2d_geometry_sink_AddLines,
3001 d2d_geometry_sink_AddBeziers,
3002 d2d_geometry_sink_EndFigure,
3003 d2d_geometry_sink_Close,
3004 d2d_geometry_sink_AddLine,
3005 d2d_geometry_sink_AddBezier,
3006 d2d_geometry_sink_AddQuadraticBezier,
3007 d2d_geometry_sink_AddQuadraticBeziers,
3008 d2d_geometry_sink_AddArc,
3011 static inline struct d2d_geometry *impl_from_ID2D1PathGeometry(ID2D1PathGeometry *iface)
3013 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3016 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_QueryInterface(ID2D1PathGeometry *iface, REFIID iid, void **out)
3018 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3020 if (IsEqualGUID(iid, &IID_ID2D1PathGeometry)
3021 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3022 || IsEqualGUID(iid, &IID_ID2D1Resource)
3023 || IsEqualGUID(iid, &IID_IUnknown))
3025 ID2D1PathGeometry_AddRef(iface);
3026 *out = iface;
3027 return S_OK;
3030 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3032 *out = NULL;
3033 return E_NOINTERFACE;
3036 static ULONG STDMETHODCALLTYPE d2d_path_geometry_AddRef(ID2D1PathGeometry *iface)
3038 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3039 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3041 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3043 return refcount;
3046 static ULONG STDMETHODCALLTYPE d2d_path_geometry_Release(ID2D1PathGeometry *iface)
3048 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3049 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3051 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3053 if (!refcount)
3055 d2d_path_geometry_free_figures(geometry);
3056 d2d_geometry_cleanup(geometry);
3057 heap_free(geometry);
3060 return refcount;
3063 static void STDMETHODCALLTYPE d2d_path_geometry_GetFactory(ID2D1PathGeometry *iface, ID2D1Factory **factory)
3065 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3067 TRACE("iface %p, factory %p.\n", iface, factory);
3069 ID2D1Factory_AddRef(*factory = geometry->factory);
3072 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetBounds(ID2D1PathGeometry *iface,
3073 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3075 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3076 size_t i;
3078 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3080 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3081 return D2DERR_WRONG_STATE;
3083 bounds->left = FLT_MAX;
3084 bounds->top = FLT_MAX;
3085 bounds->right = -FLT_MAX;
3086 bounds->bottom = -FLT_MAX;
3088 if (!transform)
3090 if (geometry->u.path.bounds.left > geometry->u.path.bounds.right)
3092 for (i = 0; i < geometry->u.path.figure_count; ++i)
3093 d2d_rect_union(&geometry->u.path.bounds, &geometry->u.path.figures[i].bounds);
3096 *bounds = geometry->u.path.bounds;
3097 return S_OK;
3100 for (i = 0; i < geometry->u.path.figure_count; ++i)
3102 const struct d2d_figure *figure = &geometry->u.path.figures[i];
3103 enum d2d_vertex_type type = D2D_VERTEX_TYPE_NONE;
3104 D2D1_RECT_F bezier_bounds;
3105 D2D1_POINT_2F p, p1, p2;
3106 size_t j, bezier_idx;
3108 /* Single vertex figures are reduced by CloseFigure(). */
3109 if (figure->vertex_count == 0)
3111 d2d_point_transform(&p, transform, figure->bounds.left, figure->bounds.top);
3112 d2d_rect_expand(bounds, &p);
3113 continue;
3116 for (j = 0; j < figure->vertex_count; ++j)
3118 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
3119 continue;
3121 p = figure->vertices[j];
3122 type = figure->vertex_types[j];
3123 d2d_point_transform(&p, transform, p.x, p.y);
3124 d2d_rect_expand(bounds, &p);
3125 break;
3128 for (bezier_idx = 0, ++j; j < figure->vertex_count; ++j)
3130 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE
3131 || figure->vertex_types[j] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
3132 continue;
3134 switch (type)
3136 case D2D_VERTEX_TYPE_LINE:
3137 p = figure->vertices[j];
3138 d2d_point_transform(&p, transform, p.x, p.y);
3139 d2d_rect_expand(bounds, &p);
3140 break;
3142 case D2D_VERTEX_TYPE_BEZIER:
3143 p1 = figure->original_bezier_controls[bezier_idx++];
3144 d2d_point_transform(&p1, transform, p1.x, p1.y);
3145 p2 = figure->vertices[j];
3146 d2d_point_transform(&p2, transform, p2.x, p2.y);
3147 d2d_rect_get_bezier_bounds(&bezier_bounds, &p, &p1, &p2);
3148 d2d_rect_union(bounds, &bezier_bounds);
3149 p = p2;
3150 break;
3152 default:
3153 FIXME("Unhandled vertex type %#x.\n", type);
3154 p = figure->vertices[j];
3155 d2d_point_transform(&p, transform, p.x, p.y);
3156 d2d_rect_expand(bounds, &p);
3157 break;
3160 type = figure->vertex_types[j];
3163 if (type == D2D_VERTEX_TYPE_BEZIER)
3165 p1 = figure->original_bezier_controls[bezier_idx++];
3166 d2d_point_transform(&p1, transform, p1.x, p1.y);
3167 p2 = figure->vertices[0];
3168 d2d_point_transform(&p2, transform, p2.x, p2.y);
3169 d2d_rect_get_bezier_bounds(&bezier_bounds, &p, &p1, &p2);
3170 d2d_rect_union(bounds, &bezier_bounds);
3174 return S_OK;
3177 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetWidenedBounds(ID2D1PathGeometry *iface, float stroke_width,
3178 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_RECT_F *bounds)
3180 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3181 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3183 return E_NOTIMPL;
3186 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_StrokeContainsPoint(ID2D1PathGeometry *iface,
3187 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3188 float tolerance, BOOL *contains)
3190 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, "
3191 "transform %p, tolerance %.8e, contains %p stub!\n",
3192 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
3194 return E_NOTIMPL;
3197 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_FillContainsPoint(ID2D1PathGeometry *iface,
3198 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3200 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3201 D2D1_MATRIX_3X2_F g_i;
3203 TRACE("iface %p, point %s, transform %p, tolerance %.8e, contains %p.\n",
3204 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
3206 if (transform)
3208 if (!d2d_matrix_invert(&g_i, transform))
3209 return D2DERR_UNSUPPORTED_OPERATION;
3210 d2d_point_transform(&point, &g_i, point.x, point.y);
3213 *contains = !!d2d_path_geometry_point_inside(geometry, &point, FALSE);
3215 TRACE("-> %#x.\n", *contains);
3217 return S_OK;
3220 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_CompareWithGeometry(ID2D1PathGeometry *iface,
3221 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3223 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3224 iface, geometry, transform, tolerance, relation);
3226 return E_NOTIMPL;
3229 static void d2d_geometry_flatten_cubic(ID2D1SimplifiedGeometrySink *sink, const D2D1_POINT_2F *p0,
3230 const D2D1_BEZIER_SEGMENT *b, float tolerance)
3232 D2D1_BEZIER_SEGMENT b0, b1;
3233 D2D1_POINT_2F q;
3234 float d;
3236 /* It's certainly possible to calculate the maximum deviation of the
3237 * approximation from the curve, but it's a little involved. Instead, note
3238 * that if the control points were evenly spaced and collinear, p1 would
3239 * be exactly between p0 and p2, and p2 would be exactly between p1 and
3240 * p3. The deviation is a decent enough approximation, and much easier to
3241 * calculate.
3243 * p1' = (p0 + p2) / 2
3244 * p2' = (p1 + p3) / 2
3245 * d = ‖p1 - p1'‖₁ + ‖p2 - p2'‖₁ */
3246 d2d_point_lerp(&q, p0, &b->point2, 0.5f);
3247 d2d_point_subtract(&q, &b->point1, &q);
3248 d = fabsf(q.x) + fabsf(q.y);
3249 d2d_point_lerp(&q, &b->point1, &b->point3, 0.5f);
3250 d2d_point_subtract(&q, &b->point2, &q);
3251 d += fabsf(q.x) + fabsf(q.y);
3252 if (d < tolerance)
3254 ID2D1SimplifiedGeometrySink_AddLines(sink, &b->point3, 1);
3255 return;
3258 d2d_point_lerp(&q, &b->point1, &b->point2, 0.5f);
3260 b1.point3 = b->point3;
3261 d2d_point_lerp(&b1.point2, &b1.point3, &b->point2, 0.5f);
3262 d2d_point_lerp(&b1.point1, &b1.point2, &q, 0.5f);
3264 d2d_point_lerp(&b0.point1, p0, &b->point1, 0.5f);
3265 d2d_point_lerp(&b0.point2, &b0.point1, &q, 0.5f);
3266 d2d_point_lerp(&b0.point3, &b0.point2, &b1.point1, 0.5f);
3268 d2d_geometry_flatten_cubic(sink, p0, &b0, tolerance);
3269 ID2D1SimplifiedGeometrySink_SetSegmentFlags(sink, D2D1_PATH_SEGMENT_FORCE_ROUND_LINE_JOIN);
3270 d2d_geometry_flatten_cubic(sink, &b0.point3, &b1, tolerance);
3271 ID2D1SimplifiedGeometrySink_SetSegmentFlags(sink, D2D1_PATH_SEGMENT_NONE);
3274 static void d2d_geometry_simplify_quadratic(ID2D1SimplifiedGeometrySink *sink,
3275 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_POINT_2F *p0,
3276 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float tolerance)
3278 D2D1_BEZIER_SEGMENT b;
3280 d2d_point_lerp(&b.point1, p0, p1, 2.0f / 3.0f);
3281 d2d_point_lerp(&b.point2, p2, p1, 2.0f / 3.0f);
3282 b.point3 = *p2;
3284 if (option == D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES)
3285 d2d_geometry_flatten_cubic(sink, p0, &b, tolerance);
3286 else
3287 ID2D1SimplifiedGeometrySink_AddBeziers(sink, &b, 1);
3290 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Simplify(ID2D1PathGeometry *iface,
3291 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3292 ID2D1SimplifiedGeometrySink *sink)
3294 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3295 enum d2d_vertex_type type = D2D_VERTEX_TYPE_NONE;
3296 unsigned int i, j, bezier_idx;
3297 D2D1_FIGURE_BEGIN begin;
3298 D2D1_POINT_2F p, p1, p2;
3299 D2D1_FIGURE_END end;
3301 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
3302 iface, option, transform, tolerance, sink);
3304 ID2D1SimplifiedGeometrySink_SetFillMode(sink, geometry->u.path.fill_mode);
3305 for (i = 0; i < geometry->u.path.figure_count; ++i)
3307 const struct d2d_figure *figure = &geometry->u.path.figures[i];
3309 for (j = 0; j < figure->vertex_count; ++j)
3311 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
3312 continue;
3314 p = figure->vertices[j];
3315 if (transform)
3316 d2d_point_transform(&p, transform, p.x, p.y);
3317 begin = figure->flags & D2D_FIGURE_FLAG_HOLLOW ? D2D1_FIGURE_BEGIN_HOLLOW : D2D1_FIGURE_BEGIN_FILLED;
3318 ID2D1SimplifiedGeometrySink_BeginFigure(sink, p, begin);
3319 type = figure->vertex_types[j];
3320 break;
3323 for (bezier_idx = 0, ++j; j < figure->vertex_count; ++j)
3325 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE
3326 || figure->vertex_types[j] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
3327 continue;
3329 switch (type)
3331 case D2D_VERTEX_TYPE_LINE:
3332 p = figure->vertices[j];
3333 if (transform)
3334 d2d_point_transform(&p, transform, p.x, p.y);
3335 ID2D1SimplifiedGeometrySink_AddLines(sink, &p, 1);
3336 break;
3338 case D2D_VERTEX_TYPE_BEZIER:
3339 p1 = figure->original_bezier_controls[bezier_idx++];
3340 if (transform)
3341 d2d_point_transform(&p1, transform, p1.x, p1.y);
3342 p2 = figure->vertices[j];
3343 if (transform)
3344 d2d_point_transform(&p2, transform, p2.x, p2.y);
3345 d2d_geometry_simplify_quadratic(sink, option, &p, &p1, &p2, tolerance);
3346 p = p2;
3347 break;
3349 default:
3350 FIXME("Unhandled vertex type %#x.\n", type);
3351 p = figure->vertices[j];
3352 if (transform)
3353 d2d_point_transform(&p, transform, p.x, p.y);
3354 ID2D1SimplifiedGeometrySink_AddLines(sink, &p, 1);
3355 break;
3358 type = figure->vertex_types[j];
3361 if (type == D2D_VERTEX_TYPE_BEZIER)
3363 p1 = figure->original_bezier_controls[bezier_idx++];
3364 if (transform)
3365 d2d_point_transform(&p1, transform, p1.x, p1.y);
3366 p2 = figure->vertices[0];
3367 if (transform)
3368 d2d_point_transform(&p2, transform, p2.x, p2.y);
3369 d2d_geometry_simplify_quadratic(sink, option, &p, &p1, &p2, tolerance);
3372 end = figure->flags & D2D_FIGURE_FLAG_CLOSED ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN;
3373 ID2D1SimplifiedGeometrySink_EndFigure(sink, end);
3376 return S_OK;
3379 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Tessellate(ID2D1PathGeometry *iface,
3380 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3382 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3384 return E_NOTIMPL;
3387 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_CombineWithGeometry(ID2D1PathGeometry *iface,
3388 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3389 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3391 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3392 iface, geometry, combine_mode, transform, tolerance, sink);
3394 return E_NOTIMPL;
3397 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Outline(ID2D1PathGeometry *iface,
3398 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
3400 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3402 return E_NOTIMPL;
3405 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputeArea(ID2D1PathGeometry *iface,
3406 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
3408 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
3410 return E_NOTIMPL;
3413 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputeLength(ID2D1PathGeometry *iface,
3414 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
3416 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
3418 return E_NOTIMPL;
3421 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputePointAtLength(ID2D1PathGeometry *iface, float length,
3422 const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point, D2D1_POINT_2F *tangent)
3424 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
3425 iface, length, transform, tolerance, point, tangent);
3427 return E_NOTIMPL;
3430 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Widen(ID2D1PathGeometry *iface, float stroke_width,
3431 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3432 ID2D1SimplifiedGeometrySink *sink)
3434 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
3435 iface, stroke_width, stroke_style, transform, tolerance, sink);
3437 return E_NOTIMPL;
3440 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Open(ID2D1PathGeometry *iface, ID2D1GeometrySink **sink)
3442 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3444 TRACE("iface %p, sink %p.\n", iface, sink);
3446 if (geometry->u.path.state != D2D_GEOMETRY_STATE_INITIAL)
3447 return D2DERR_WRONG_STATE;
3449 *sink = &geometry->u.path.ID2D1GeometrySink_iface;
3450 ID2D1GeometrySink_AddRef(*sink);
3452 geometry->u.path.state = D2D_GEOMETRY_STATE_OPEN;
3454 return S_OK;
3457 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Stream(ID2D1PathGeometry *iface, ID2D1GeometrySink *sink)
3459 FIXME("iface %p, sink %p stub!\n", iface, sink);
3461 return E_NOTIMPL;
3464 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetSegmentCount(ID2D1PathGeometry *iface, UINT32 *count)
3466 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3468 TRACE("iface %p, count %p.\n", iface, count);
3470 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3471 return D2DERR_WRONG_STATE;
3473 *count = geometry->u.path.segment_count;
3475 return S_OK;
3478 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetFigureCount(ID2D1PathGeometry *iface, UINT32 *count)
3480 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3482 TRACE("iface %p, count %p.\n", iface, count);
3484 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3485 return D2DERR_WRONG_STATE;
3487 *count = geometry->u.path.figure_count;
3489 return S_OK;
3492 static const struct ID2D1PathGeometryVtbl d2d_path_geometry_vtbl =
3494 d2d_path_geometry_QueryInterface,
3495 d2d_path_geometry_AddRef,
3496 d2d_path_geometry_Release,
3497 d2d_path_geometry_GetFactory,
3498 d2d_path_geometry_GetBounds,
3499 d2d_path_geometry_GetWidenedBounds,
3500 d2d_path_geometry_StrokeContainsPoint,
3501 d2d_path_geometry_FillContainsPoint,
3502 d2d_path_geometry_CompareWithGeometry,
3503 d2d_path_geometry_Simplify,
3504 d2d_path_geometry_Tessellate,
3505 d2d_path_geometry_CombineWithGeometry,
3506 d2d_path_geometry_Outline,
3507 d2d_path_geometry_ComputeArea,
3508 d2d_path_geometry_ComputeLength,
3509 d2d_path_geometry_ComputePointAtLength,
3510 d2d_path_geometry_Widen,
3511 d2d_path_geometry_Open,
3512 d2d_path_geometry_Stream,
3513 d2d_path_geometry_GetSegmentCount,
3514 d2d_path_geometry_GetFigureCount,
3517 void d2d_path_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory)
3519 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl);
3520 geometry->u.path.ID2D1GeometrySink_iface.lpVtbl = &d2d_geometry_sink_vtbl;
3521 geometry->u.path.bounds.left = FLT_MAX;
3522 geometry->u.path.bounds.right = -FLT_MAX;
3523 geometry->u.path.bounds.top = FLT_MAX;
3524 geometry->u.path.bounds.bottom = -FLT_MAX;
3527 static inline struct d2d_geometry *impl_from_ID2D1EllipseGeometry(ID2D1EllipseGeometry *iface)
3529 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3532 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_QueryInterface(ID2D1EllipseGeometry *iface,
3533 REFIID iid, void **out)
3535 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3537 if (IsEqualGUID(iid, &IID_ID2D1EllipseGeometry)
3538 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3539 || IsEqualGUID(iid, &IID_ID2D1Resource)
3540 || IsEqualGUID(iid, &IID_IUnknown))
3542 ID2D1EllipseGeometry_AddRef(iface);
3543 *out = iface;
3544 return S_OK;
3547 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3549 *out = NULL;
3550 return E_NOINTERFACE;
3553 static ULONG STDMETHODCALLTYPE d2d_ellipse_geometry_AddRef(ID2D1EllipseGeometry *iface)
3555 struct d2d_geometry *geometry = impl_from_ID2D1EllipseGeometry(iface);
3556 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3558 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3560 return refcount;
3563 static ULONG STDMETHODCALLTYPE d2d_ellipse_geometry_Release(ID2D1EllipseGeometry *iface)
3565 struct d2d_geometry *geometry = impl_from_ID2D1EllipseGeometry(iface);
3566 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3568 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3570 if (!refcount)
3572 d2d_geometry_cleanup(geometry);
3573 heap_free(geometry);
3576 return refcount;
3579 static void STDMETHODCALLTYPE d2d_ellipse_geometry_GetFactory(ID2D1EllipseGeometry *iface, ID2D1Factory **factory)
3581 struct d2d_geometry *geometry = impl_from_ID2D1EllipseGeometry(iface);
3583 TRACE("iface %p, factory %p.\n", iface, factory);
3585 ID2D1Factory_AddRef(*factory = geometry->factory);
3588 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_GetBounds(ID2D1EllipseGeometry *iface,
3589 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3591 FIXME("iface %p, transform %p, bounds %p stub!\n", iface, transform, bounds);
3593 return E_NOTIMPL;
3596 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_GetWidenedBounds(ID2D1EllipseGeometry *iface,
3597 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3598 float tolerance, D2D1_RECT_F *bounds)
3600 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3601 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3603 return E_NOTIMPL;
3606 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_StrokeContainsPoint(ID2D1EllipseGeometry *iface,
3607 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3608 float tolerance, BOOL *contains)
3610 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p stub!\n",
3611 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
3613 return E_NOTIMPL;
3616 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_FillContainsPoint(ID2D1EllipseGeometry *iface,
3617 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3619 FIXME("iface %p, point %s, transform %p, tolerance %.8e, contains %p stub!\n",
3620 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
3622 return E_NOTIMPL;
3625 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_CompareWithGeometry(ID2D1EllipseGeometry *iface,
3626 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3628 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3629 iface, geometry, transform, tolerance, relation);
3631 return E_NOTIMPL;
3634 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_Simplify(ID2D1EllipseGeometry *iface,
3635 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3636 ID2D1SimplifiedGeometrySink *sink)
3638 FIXME("iface %p, option %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3639 iface, option, transform, tolerance, sink);
3641 return E_NOTIMPL;
3644 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_Tessellate(ID2D1EllipseGeometry *iface,
3645 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3647 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3649 return E_NOTIMPL;
3652 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_CombineWithGeometry(ID2D1EllipseGeometry *iface,
3653 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3654 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3656 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3657 iface, geometry, combine_mode, transform, tolerance, sink);
3659 return E_NOTIMPL;
3662 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_Outline(ID2D1EllipseGeometry *iface,
3663 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
3665 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3667 return E_NOTIMPL;
3670 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_ComputeArea(ID2D1EllipseGeometry *iface,
3671 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
3673 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
3675 return E_NOTIMPL;
3678 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_ComputeLength(ID2D1EllipseGeometry *iface,
3679 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
3681 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
3683 return E_NOTIMPL;
3686 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_ComputePointAtLength(ID2D1EllipseGeometry *iface,
3687 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
3688 D2D1_POINT_2F *tangent)
3690 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
3691 iface, length, transform, tolerance, point, tangent);
3693 return E_NOTIMPL;
3696 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_Widen(ID2D1EllipseGeometry *iface, float stroke_width,
3697 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3698 ID2D1SimplifiedGeometrySink *sink)
3700 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
3701 iface, stroke_width, stroke_style, transform, tolerance, sink);
3703 return E_NOTIMPL;
3706 static void STDMETHODCALLTYPE d2d_ellipse_geometry_GetRoundedRect(ID2D1EllipseGeometry *iface, D2D1_ELLIPSE *ellipse)
3708 struct d2d_geometry *geometry = impl_from_ID2D1EllipseGeometry(iface);
3710 TRACE("iface %p, ellipse %p.\n", iface, ellipse);
3712 *ellipse = geometry->u.ellipse.ellipse;
3715 static const struct ID2D1EllipseGeometryVtbl d2d_ellipse_geometry_vtbl =
3717 d2d_ellipse_geometry_QueryInterface,
3718 d2d_ellipse_geometry_AddRef,
3719 d2d_ellipse_geometry_Release,
3720 d2d_ellipse_geometry_GetFactory,
3721 d2d_ellipse_geometry_GetBounds,
3722 d2d_ellipse_geometry_GetWidenedBounds,
3723 d2d_ellipse_geometry_StrokeContainsPoint,
3724 d2d_ellipse_geometry_FillContainsPoint,
3725 d2d_ellipse_geometry_CompareWithGeometry,
3726 d2d_ellipse_geometry_Simplify,
3727 d2d_ellipse_geometry_Tessellate,
3728 d2d_ellipse_geometry_CombineWithGeometry,
3729 d2d_ellipse_geometry_Outline,
3730 d2d_ellipse_geometry_ComputeArea,
3731 d2d_ellipse_geometry_ComputeLength,
3732 d2d_ellipse_geometry_ComputePointAtLength,
3733 d2d_ellipse_geometry_Widen,
3734 d2d_ellipse_geometry_GetRoundedRect,
3737 HRESULT d2d_ellipse_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory, const D2D1_ELLIPSE *ellipse)
3739 D2D1_POINT_2F *v, v1, v2, v3, v4;
3740 struct d2d_face *f;
3741 float l, r, t, b;
3743 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_ellipse_geometry_vtbl);
3744 geometry->u.ellipse.ellipse = *ellipse;
3746 if (!(geometry->fill.vertices = heap_alloc(4 * sizeof(*geometry->fill.vertices))))
3747 goto fail;
3748 if (!d2d_array_reserve((void **)&geometry->fill.faces,
3749 &geometry->fill.faces_size, 2, sizeof(*geometry->fill.faces)))
3750 goto fail;
3752 l = ellipse->point.x - ellipse->radiusX;
3753 r = ellipse->point.x + ellipse->radiusX;
3754 t = ellipse->point.y - ellipse->radiusY;
3755 b = ellipse->point.y + ellipse->radiusY;
3757 d2d_point_set(&v1, r, t);
3758 d2d_point_set(&v2, r, b);
3759 d2d_point_set(&v3, l, b);
3760 d2d_point_set(&v4, l, t);
3762 v = geometry->fill.vertices;
3763 d2d_point_set(&v[0], ellipse->point.x, t);
3764 d2d_point_set(&v[1], r, ellipse->point.y);
3765 d2d_point_set(&v[2], ellipse->point.x, b);
3766 d2d_point_set(&v[3], l, ellipse->point.y);
3767 geometry->fill.vertex_count = 4;
3769 f = geometry->fill.faces;
3770 d2d_face_set(&f[0], 0, 3, 2);
3771 d2d_face_set(&f[1], 0, 2, 1);
3772 geometry->fill.face_count = 2;
3774 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[0], &v1, &v[1]))
3775 goto fail;
3776 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[1], &v2, &v[2]))
3777 goto fail;
3778 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[2], &v3, &v[3]))
3779 goto fail;
3780 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[3], &v4, &v[0]))
3781 goto fail;
3783 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[0], &v1, &v[1]))
3784 goto fail;
3785 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[1], &v2, &v[2]))
3786 goto fail;
3787 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[2], &v3, &v[3]))
3788 goto fail;
3789 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[3], &v4, &v[0]))
3790 goto fail;
3792 return S_OK;
3794 fail:
3795 d2d_geometry_cleanup(geometry);
3796 return E_OUTOFMEMORY;
3799 static inline struct d2d_geometry *impl_from_ID2D1RectangleGeometry(ID2D1RectangleGeometry *iface)
3801 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3804 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_QueryInterface(ID2D1RectangleGeometry *iface,
3805 REFIID iid, void **out)
3807 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3809 if (IsEqualGUID(iid, &IID_ID2D1RectangleGeometry)
3810 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3811 || IsEqualGUID(iid, &IID_ID2D1Resource)
3812 || IsEqualGUID(iid, &IID_IUnknown))
3814 ID2D1RectangleGeometry_AddRef(iface);
3815 *out = iface;
3816 return S_OK;
3819 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3821 *out = NULL;
3822 return E_NOINTERFACE;
3825 static ULONG STDMETHODCALLTYPE d2d_rectangle_geometry_AddRef(ID2D1RectangleGeometry *iface)
3827 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3828 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3830 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3832 return refcount;
3835 static ULONG STDMETHODCALLTYPE d2d_rectangle_geometry_Release(ID2D1RectangleGeometry *iface)
3837 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3838 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3840 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3842 if (!refcount)
3844 d2d_geometry_cleanup(geometry);
3845 heap_free(geometry);
3848 return refcount;
3851 static void STDMETHODCALLTYPE d2d_rectangle_geometry_GetFactory(ID2D1RectangleGeometry *iface, ID2D1Factory **factory)
3853 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3855 TRACE("iface %p, factory %p.\n", iface, factory);
3857 ID2D1Factory_AddRef(*factory = geometry->factory);
3860 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_GetBounds(ID2D1RectangleGeometry *iface,
3861 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3863 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3864 D2D1_RECT_F *rect;
3865 D2D1_POINT_2F p;
3867 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3869 rect = &geometry->u.rectangle.rect;
3870 if (!transform)
3872 *bounds = *rect;
3873 return S_OK;
3876 bounds->left = FLT_MAX;
3877 bounds->top = FLT_MAX;
3878 bounds->right = -FLT_MAX;
3879 bounds->bottom = -FLT_MAX;
3881 d2d_point_transform(&p, transform, rect->left, rect->top);
3882 d2d_rect_expand(bounds, &p);
3883 d2d_point_transform(&p, transform, rect->left, rect->bottom);
3884 d2d_rect_expand(bounds, &p);
3885 d2d_point_transform(&p, transform, rect->right, rect->bottom);
3886 d2d_rect_expand(bounds, &p);
3887 d2d_point_transform(&p, transform, rect->right, rect->top);
3888 d2d_rect_expand(bounds, &p);
3890 return S_OK;
3893 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_GetWidenedBounds(ID2D1RectangleGeometry *iface,
3894 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3895 float tolerance, D2D1_RECT_F *bounds)
3897 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3898 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3900 return E_NOTIMPL;
3903 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_StrokeContainsPoint(ID2D1RectangleGeometry *iface,
3904 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3905 float tolerance, BOOL *contains)
3907 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p stub!\n",
3908 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
3910 return E_NOTIMPL;
3913 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_FillContainsPoint(ID2D1RectangleGeometry *iface,
3914 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3916 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3917 D2D1_RECT_F *rect = &geometry->u.rectangle.rect;
3918 float dx, dy;
3920 TRACE("iface %p, point %s, transform %p, tolerance %.8e, contains %p.\n",
3921 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
3923 if (transform)
3925 D2D1_MATRIX_3X2_F g_i;
3927 if (!d2d_matrix_invert(&g_i, transform))
3928 return D2DERR_UNSUPPORTED_OPERATION;
3929 d2d_point_transform(&point, &g_i, point.x, point.y);
3932 if (tolerance == 0.0f)
3933 tolerance = D2D1_DEFAULT_FLATTENING_TOLERANCE;
3935 dx = max(fabsf((rect->right + rect->left) / 2.0f - point.x) - (rect->right - rect->left) / 2.0f, 0.0f);
3936 dy = max(fabsf((rect->bottom + rect->top) / 2.0f - point.y) - (rect->bottom - rect->top) / 2.0f, 0.0f);
3938 *contains = tolerance * tolerance > (dx * dx + dy * dy);
3939 return S_OK;
3942 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_CompareWithGeometry(ID2D1RectangleGeometry *iface,
3943 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3945 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3946 iface, geometry, transform, tolerance, relation);
3948 return E_NOTIMPL;
3951 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Simplify(ID2D1RectangleGeometry *iface,
3952 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3953 ID2D1SimplifiedGeometrySink *sink)
3955 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3956 D2D1_RECT_F *rect = &geometry->u.rectangle.rect;
3957 D2D1_POINT_2F p[4];
3958 unsigned int i;
3960 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
3961 iface, option, transform, tolerance, sink);
3963 d2d_point_set(&p[0], rect->left, rect->top);
3964 d2d_point_set(&p[1], rect->right, rect->top);
3965 d2d_point_set(&p[2], rect->right, rect->bottom);
3966 d2d_point_set(&p[3], rect->left, rect->bottom);
3968 if (transform)
3970 for (i = 0; i < ARRAY_SIZE(p); ++i)
3972 d2d_point_transform(&p[i], transform, p[i].x, p[i].y);
3976 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_ALTERNATE);
3977 ID2D1SimplifiedGeometrySink_BeginFigure(sink, p[0], D2D1_FIGURE_BEGIN_FILLED);
3978 ID2D1SimplifiedGeometrySink_AddLines(sink, &p[1], 3);
3979 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
3981 return S_OK;
3984 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Tessellate(ID2D1RectangleGeometry *iface,
3985 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3987 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3989 return E_NOTIMPL;
3992 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_CombineWithGeometry(ID2D1RectangleGeometry *iface,
3993 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3994 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3996 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3997 iface, geometry, combine_mode, transform, tolerance, sink);
3999 return E_NOTIMPL;
4002 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Outline(ID2D1RectangleGeometry *iface,
4003 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4005 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4007 return E_NOTIMPL;
4010 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputeArea(ID2D1RectangleGeometry *iface,
4011 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4013 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4015 return E_NOTIMPL;
4018 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputeLength(ID2D1RectangleGeometry *iface,
4019 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4021 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4023 return E_NOTIMPL;
4026 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputePointAtLength(ID2D1RectangleGeometry *iface,
4027 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
4028 D2D1_POINT_2F *tangent)
4030 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4031 iface, length, transform, tolerance, point, tangent);
4033 return E_NOTIMPL;
4036 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Widen(ID2D1RectangleGeometry *iface, float stroke_width,
4037 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4038 ID2D1SimplifiedGeometrySink *sink)
4040 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4041 iface, stroke_width, stroke_style, transform, tolerance, sink);
4043 return E_NOTIMPL;
4046 static void STDMETHODCALLTYPE d2d_rectangle_geometry_GetRect(ID2D1RectangleGeometry *iface, D2D1_RECT_F *rect)
4048 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
4050 TRACE("iface %p, rect %p.\n", iface, rect);
4052 *rect = geometry->u.rectangle.rect;
4055 static const struct ID2D1RectangleGeometryVtbl d2d_rectangle_geometry_vtbl =
4057 d2d_rectangle_geometry_QueryInterface,
4058 d2d_rectangle_geometry_AddRef,
4059 d2d_rectangle_geometry_Release,
4060 d2d_rectangle_geometry_GetFactory,
4061 d2d_rectangle_geometry_GetBounds,
4062 d2d_rectangle_geometry_GetWidenedBounds,
4063 d2d_rectangle_geometry_StrokeContainsPoint,
4064 d2d_rectangle_geometry_FillContainsPoint,
4065 d2d_rectangle_geometry_CompareWithGeometry,
4066 d2d_rectangle_geometry_Simplify,
4067 d2d_rectangle_geometry_Tessellate,
4068 d2d_rectangle_geometry_CombineWithGeometry,
4069 d2d_rectangle_geometry_Outline,
4070 d2d_rectangle_geometry_ComputeArea,
4071 d2d_rectangle_geometry_ComputeLength,
4072 d2d_rectangle_geometry_ComputePointAtLength,
4073 d2d_rectangle_geometry_Widen,
4074 d2d_rectangle_geometry_GetRect,
4077 HRESULT d2d_rectangle_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory, const D2D1_RECT_F *rect)
4079 struct d2d_face *f;
4080 D2D1_POINT_2F *v, v0p, v0n, v1p, v1n, v2p, v2n, v3p, v3n;
4081 float l, r, t, b;
4083 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl);
4084 geometry->u.rectangle.rect = *rect;
4086 if (!(geometry->fill.vertices = heap_alloc(4 * sizeof(*geometry->fill.vertices))))
4087 goto fail;
4088 if (!d2d_array_reserve((void **)&geometry->fill.faces,
4089 &geometry->fill.faces_size, 2, sizeof(*geometry->fill.faces)))
4090 goto fail;
4092 l = min(rect->left, rect->right);
4093 r = max(rect->left, rect->right);
4094 t = min(rect->top, rect->bottom);
4095 b = max(rect->top, rect->bottom);
4097 v = geometry->fill.vertices;
4098 d2d_point_set(&v[0], l, t);
4099 d2d_point_set(&v[1], l, b);
4100 d2d_point_set(&v[2], r, b);
4101 d2d_point_set(&v[3], r, t);
4102 geometry->fill.vertex_count = 4;
4104 f = geometry->fill.faces;
4105 d2d_face_set(&f[0], 1, 2, 0);
4106 d2d_face_set(&f[1], 0, 2, 3);
4107 geometry->fill.face_count = 2;
4109 d2d_point_set(&v0p, l+1.0f, t);
4110 d2d_point_set(&v0n, l, t+1.0f);
4111 d2d_point_set(&v1p, l, b-1.0f);
4112 d2d_point_set(&v1n, l+1.0f, b);
4113 d2d_point_set(&v2p, r-1.0f, b);
4114 d2d_point_set(&v2n, r, b-1.0f);
4115 d2d_point_set(&v3p, r, t+1.0f);
4116 d2d_point_set(&v3n, r-1.0f, t);
4118 if (!d2d_geometry_outline_add_line_segment(geometry, &v[0], &v[1]))
4119 goto fail;
4120 if (!d2d_geometry_outline_add_line_segment(geometry, &v[1], &v[2]))
4121 goto fail;
4122 if (!d2d_geometry_outline_add_line_segment(geometry, &v[2], &v[3]))
4123 goto fail;
4124 if (!d2d_geometry_outline_add_line_segment(geometry, &v[3], &v[0]))
4125 goto fail;
4127 if (!d2d_geometry_outline_add_join(geometry, &v0p, &v[0], &v0n))
4128 goto fail;
4129 if (!d2d_geometry_outline_add_join(geometry, &v1p, &v[1], &v1n))
4130 goto fail;
4131 if (!d2d_geometry_outline_add_join(geometry, &v2p, &v[2], &v2n))
4132 goto fail;
4133 if (!d2d_geometry_outline_add_join(geometry, &v3p, &v[3], &v3n))
4134 goto fail;
4136 return S_OK;
4138 fail:
4139 d2d_geometry_cleanup(geometry);
4140 return E_OUTOFMEMORY;
4143 static inline struct d2d_geometry *impl_from_ID2D1RoundedRectangleGeometry(ID2D1RoundedRectangleGeometry *iface)
4145 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
4148 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_QueryInterface(ID2D1RoundedRectangleGeometry *iface,
4149 REFIID iid, void **out)
4151 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
4153 if (IsEqualGUID(iid, &IID_ID2D1RoundedRectangleGeometry)
4154 || IsEqualGUID(iid, &IID_ID2D1Geometry)
4155 || IsEqualGUID(iid, &IID_ID2D1Resource)
4156 || IsEqualGUID(iid, &IID_IUnknown))
4158 ID2D1RoundedRectangleGeometry_AddRef(iface);
4159 *out = iface;
4160 return S_OK;
4163 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
4165 *out = NULL;
4166 return E_NOINTERFACE;
4169 static ULONG STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_AddRef(ID2D1RoundedRectangleGeometry *iface)
4171 struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
4172 ULONG refcount = InterlockedIncrement(&geometry->refcount);
4174 TRACE("%p increasing refcount to %u.\n", iface, refcount);
4176 return refcount;
4179 static ULONG STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Release(ID2D1RoundedRectangleGeometry *iface)
4181 struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
4182 ULONG refcount = InterlockedDecrement(&geometry->refcount);
4184 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
4186 if (!refcount)
4188 d2d_geometry_cleanup(geometry);
4189 heap_free(geometry);
4192 return refcount;
4195 static void STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetFactory(ID2D1RoundedRectangleGeometry *iface,
4196 ID2D1Factory **factory)
4198 struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
4200 TRACE("iface %p, factory %p.\n", iface, factory);
4202 ID2D1Factory_AddRef(*factory = geometry->factory);
4205 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetBounds(ID2D1RoundedRectangleGeometry *iface,
4206 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
4208 FIXME("iface %p, transform %p, bounds %p stub!\n", iface, transform, bounds);
4210 return E_NOTIMPL;
4213 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetWidenedBounds(ID2D1RoundedRectangleGeometry *iface,
4214 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4215 float tolerance, D2D1_RECT_F *bounds)
4217 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
4218 iface, stroke_width, stroke_style, transform, tolerance, bounds);
4220 return E_NOTIMPL;
4223 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_StrokeContainsPoint(
4224 ID2D1RoundedRectangleGeometry *iface, D2D1_POINT_2F point, float stroke_width,
4225 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4227 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p stub!\n",
4228 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
4230 return E_NOTIMPL;
4233 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_FillContainsPoint(ID2D1RoundedRectangleGeometry *iface,
4234 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4236 FIXME("iface %p, point %s, transform %p, tolerance %.8e, contains %p stub!\n",
4237 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
4239 return E_NOTIMPL;
4242 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_CompareWithGeometry(
4243 ID2D1RoundedRectangleGeometry *iface, ID2D1Geometry *geometry,
4244 const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
4246 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
4247 iface, geometry, transform, tolerance, relation);
4249 return E_NOTIMPL;
4252 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Simplify(ID2D1RoundedRectangleGeometry *iface,
4253 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4254 ID2D1SimplifiedGeometrySink *sink)
4256 FIXME("iface %p, option %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4257 iface, option, transform, tolerance, sink);
4259 return E_NOTIMPL;
4262 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Tessellate(ID2D1RoundedRectangleGeometry *iface,
4263 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
4265 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4267 return E_NOTIMPL;
4270 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_CombineWithGeometry(
4271 ID2D1RoundedRectangleGeometry *iface, ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode,
4272 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4274 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4275 iface, geometry, combine_mode, transform, tolerance, sink);
4277 return E_NOTIMPL;
4280 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Outline(ID2D1RoundedRectangleGeometry *iface,
4281 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4283 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4285 return E_NOTIMPL;
4288 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputeArea(ID2D1RoundedRectangleGeometry *iface,
4289 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4291 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4293 return E_NOTIMPL;
4296 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputeLength(ID2D1RoundedRectangleGeometry *iface,
4297 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4299 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4301 return E_NOTIMPL;
4304 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputePointAtLength(
4305 ID2D1RoundedRectangleGeometry *iface, float length, const D2D1_MATRIX_3X2_F *transform,
4306 float tolerance, D2D1_POINT_2F *point, D2D1_POINT_2F *tangent)
4308 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4309 iface, length, transform, tolerance, point, tangent);
4311 return E_NOTIMPL;
4314 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Widen(ID2D1RoundedRectangleGeometry *iface,
4315 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4316 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4318 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4319 iface, stroke_width, stroke_style, transform, tolerance, sink);
4321 return E_NOTIMPL;
4324 static void STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetRoundedRect(ID2D1RoundedRectangleGeometry *iface,
4325 D2D1_ROUNDED_RECT *rounded_rect)
4327 struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
4329 TRACE("iface %p, rounded_rect %p.\n", iface, rounded_rect);
4331 *rounded_rect = geometry->u.rounded_rectangle.rounded_rect;
4334 static const struct ID2D1RoundedRectangleGeometryVtbl d2d_rounded_rectangle_geometry_vtbl =
4336 d2d_rounded_rectangle_geometry_QueryInterface,
4337 d2d_rounded_rectangle_geometry_AddRef,
4338 d2d_rounded_rectangle_geometry_Release,
4339 d2d_rounded_rectangle_geometry_GetFactory,
4340 d2d_rounded_rectangle_geometry_GetBounds,
4341 d2d_rounded_rectangle_geometry_GetWidenedBounds,
4342 d2d_rounded_rectangle_geometry_StrokeContainsPoint,
4343 d2d_rounded_rectangle_geometry_FillContainsPoint,
4344 d2d_rounded_rectangle_geometry_CompareWithGeometry,
4345 d2d_rounded_rectangle_geometry_Simplify,
4346 d2d_rounded_rectangle_geometry_Tessellate,
4347 d2d_rounded_rectangle_geometry_CombineWithGeometry,
4348 d2d_rounded_rectangle_geometry_Outline,
4349 d2d_rounded_rectangle_geometry_ComputeArea,
4350 d2d_rounded_rectangle_geometry_ComputeLength,
4351 d2d_rounded_rectangle_geometry_ComputePointAtLength,
4352 d2d_rounded_rectangle_geometry_Widen,
4353 d2d_rounded_rectangle_geometry_GetRoundedRect,
4356 HRESULT d2d_rounded_rectangle_geometry_init(struct d2d_geometry *geometry,
4357 ID2D1Factory *factory, const D2D1_ROUNDED_RECT *rounded_rect)
4359 D2D1_POINT_2F *v, v1, v2, v3, v4;
4360 struct d2d_face *f;
4361 float l, r, t, b;
4362 float rx, ry;
4364 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_rounded_rectangle_geometry_vtbl);
4365 geometry->u.rounded_rectangle.rounded_rect = *rounded_rect;
4367 if (!(geometry->fill.vertices = heap_alloc(8 * sizeof(*geometry->fill.vertices))))
4368 goto fail;
4369 if (!d2d_array_reserve((void **)&geometry->fill.faces,
4370 &geometry->fill.faces_size, 6, sizeof(*geometry->fill.faces)))
4371 goto fail;
4373 l = min(rounded_rect->rect.left, rounded_rect->rect.right);
4374 r = max(rounded_rect->rect.left, rounded_rect->rect.right);
4375 t = min(rounded_rect->rect.top, rounded_rect->rect.bottom);
4376 b = max(rounded_rect->rect.top, rounded_rect->rect.bottom);
4378 rx = min(rounded_rect->radiusX, 0.5f * (r - l));
4379 ry = min(rounded_rect->radiusY, 0.5f * (b - t));
4381 d2d_point_set(&v1, r, t);
4382 d2d_point_set(&v2, r, b);
4383 d2d_point_set(&v3, l, b);
4384 d2d_point_set(&v4, l, t);
4386 v = geometry->fill.vertices;
4387 d2d_point_set(&v[0], l + rx, t);
4388 d2d_point_set(&v[1], r - rx, t);
4389 d2d_point_set(&v[2], r, t + ry);
4390 d2d_point_set(&v[3], r, b - ry);
4391 d2d_point_set(&v[4], r - rx, b);
4392 d2d_point_set(&v[5], l + rx, b);
4393 d2d_point_set(&v[6], l, b - ry);
4394 d2d_point_set(&v[7], l, t + ry);
4395 geometry->fill.vertex_count = 8;
4397 f = geometry->fill.faces;
4398 d2d_face_set(&f[0], 0, 7, 6);
4399 d2d_face_set(&f[1], 0, 6, 5);
4400 d2d_face_set(&f[2], 0, 5, 4);
4401 d2d_face_set(&f[3], 0, 4, 1);
4402 d2d_face_set(&f[4], 1, 4, 3);
4403 d2d_face_set(&f[5], 1, 3, 2);
4404 geometry->fill.face_count = 6;
4406 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[1], &v1, &v[2]))
4407 goto fail;
4408 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[3], &v2, &v[4]))
4409 goto fail;
4410 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[5], &v3, &v[6]))
4411 goto fail;
4412 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[7], &v4, &v[0]))
4413 goto fail;
4415 if (!d2d_geometry_outline_add_line_segment(geometry, &v[0], &v[1]))
4416 goto fail;
4417 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[1], &v1, &v[2]))
4418 goto fail;
4419 if (!d2d_geometry_outline_add_line_segment(geometry, &v[2], &v[3]))
4420 goto fail;
4421 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[3], &v2, &v[4]))
4422 goto fail;
4423 if (!d2d_geometry_outline_add_line_segment(geometry, &v[4], &v[5]))
4424 goto fail;
4425 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[5], &v3, &v[6]))
4426 goto fail;
4427 if (!d2d_geometry_outline_add_line_segment(geometry, &v[6], &v[7]))
4428 goto fail;
4429 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[7], &v4, &v[0]))
4430 goto fail;
4432 return S_OK;
4434 fail:
4435 d2d_geometry_cleanup(geometry);
4436 return E_OUTOFMEMORY;
4439 static inline struct d2d_geometry *impl_from_ID2D1TransformedGeometry(ID2D1TransformedGeometry *iface)
4441 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
4444 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_QueryInterface(ID2D1TransformedGeometry *iface,
4445 REFIID iid, void **out)
4447 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
4449 if (IsEqualGUID(iid, &IID_ID2D1TransformedGeometry)
4450 || IsEqualGUID(iid, &IID_ID2D1Geometry)
4451 || IsEqualGUID(iid, &IID_ID2D1Resource)
4452 || IsEqualGUID(iid, &IID_IUnknown))
4454 ID2D1TransformedGeometry_AddRef(iface);
4455 *out = iface;
4456 return S_OK;
4459 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
4461 *out = NULL;
4462 return E_NOINTERFACE;
4465 static ULONG STDMETHODCALLTYPE d2d_transformed_geometry_AddRef(ID2D1TransformedGeometry *iface)
4467 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4468 ULONG refcount = InterlockedIncrement(&geometry->refcount);
4470 TRACE("%p increasing refcount to %u.\n", iface, refcount);
4472 return refcount;
4475 static ULONG STDMETHODCALLTYPE d2d_transformed_geometry_Release(ID2D1TransformedGeometry *iface)
4477 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4478 ULONG refcount = InterlockedDecrement(&geometry->refcount);
4480 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
4482 if (!refcount)
4484 geometry->outline.bezier_faces = NULL;
4485 geometry->outline.beziers = NULL;
4486 geometry->outline.faces = NULL;
4487 geometry->outline.vertices = NULL;
4488 geometry->fill.bezier_vertices = NULL;
4489 geometry->fill.faces = NULL;
4490 geometry->fill.vertices = NULL;
4491 ID2D1Geometry_Release(geometry->u.transformed.src_geometry);
4492 d2d_geometry_cleanup(geometry);
4493 heap_free(geometry);
4496 return refcount;
4499 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetFactory(ID2D1TransformedGeometry *iface,
4500 ID2D1Factory **factory)
4502 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4504 TRACE("iface %p, factory %p.\n", iface, factory);
4506 ID2D1Factory_AddRef(*factory = geometry->factory);
4509 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_GetBounds(ID2D1TransformedGeometry *iface,
4510 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
4512 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4513 D2D1_MATRIX_3X2_F g;
4515 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
4517 g = geometry->transform;
4518 if (transform)
4519 d2d_matrix_multiply(&g, transform);
4521 return ID2D1Geometry_GetBounds(geometry->u.transformed.src_geometry, &g, bounds);
4524 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_GetWidenedBounds(ID2D1TransformedGeometry *iface,
4525 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4526 float tolerance, D2D1_RECT_F *bounds)
4528 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
4529 iface, stroke_width, stroke_style, transform, tolerance, bounds);
4531 return E_NOTIMPL;
4534 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_StrokeContainsPoint(ID2D1TransformedGeometry *iface,
4535 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4536 float tolerance, BOOL *contains)
4538 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4539 D2D1_MATRIX_3X2_F g;
4541 TRACE("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p.\n",
4542 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
4544 g = geometry->transform;
4545 if (transform)
4546 d2d_matrix_multiply(&g, transform);
4548 return ID2D1Geometry_StrokeContainsPoint(geometry->u.transformed.src_geometry, point, stroke_width, stroke_style,
4549 &g, tolerance, contains);
4552 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_FillContainsPoint(ID2D1TransformedGeometry *iface,
4553 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4555 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4556 D2D1_MATRIX_3X2_F g;
4558 TRACE("iface %p, point %s, transform %p, tolerance %.8e, contains %p.\n",
4559 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
4561 g = geometry->transform;
4562 if (transform)
4563 d2d_matrix_multiply(&g, transform);
4565 return ID2D1Geometry_FillContainsPoint(geometry->u.transformed.src_geometry, point, &g, tolerance, contains);
4568 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_CompareWithGeometry(ID2D1TransformedGeometry *iface,
4569 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
4571 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
4572 iface, geometry, transform, tolerance, relation);
4574 return E_NOTIMPL;
4577 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Simplify(ID2D1TransformedGeometry *iface,
4578 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4579 ID2D1SimplifiedGeometrySink *sink)
4581 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4582 D2D1_MATRIX_3X2_F g;
4584 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
4585 iface, option, transform, tolerance, sink);
4587 g = geometry->transform;
4588 if (transform)
4589 d2d_matrix_multiply(&g, transform);
4591 return ID2D1Geometry_Simplify(geometry->u.transformed.src_geometry, option, &g, tolerance, sink);
4594 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Tessellate(ID2D1TransformedGeometry *iface,
4595 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
4597 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4599 return E_NOTIMPL;
4602 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_CombineWithGeometry(ID2D1TransformedGeometry *iface,
4603 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
4604 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4606 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4607 iface, geometry, combine_mode, transform, tolerance, sink);
4609 return E_NOTIMPL;
4612 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Outline(ID2D1TransformedGeometry *iface,
4613 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4615 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4617 return E_NOTIMPL;
4620 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputeArea(ID2D1TransformedGeometry *iface,
4621 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4623 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4625 return E_NOTIMPL;
4628 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputeLength(ID2D1TransformedGeometry *iface,
4629 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4631 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4633 return E_NOTIMPL;
4636 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputePointAtLength(ID2D1TransformedGeometry *iface,
4637 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
4638 D2D1_POINT_2F *tangent)
4640 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4641 iface, length, transform, tolerance, point, tangent);
4643 return E_NOTIMPL;
4646 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Widen(ID2D1TransformedGeometry *iface, float stroke_width,
4647 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4648 ID2D1SimplifiedGeometrySink *sink)
4650 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4651 iface, stroke_width, stroke_style, transform, tolerance, sink);
4653 return E_NOTIMPL;
4656 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetSourceGeometry(ID2D1TransformedGeometry *iface,
4657 ID2D1Geometry **src_geometry)
4659 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4661 TRACE("iface %p, src_geometry %p.\n", iface, src_geometry);
4663 ID2D1Geometry_AddRef(*src_geometry = geometry->u.transformed.src_geometry);
4666 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetTransform(ID2D1TransformedGeometry *iface,
4667 D2D1_MATRIX_3X2_F *transform)
4669 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4671 TRACE("iface %p, transform %p.\n", iface, transform);
4673 *transform = geometry->u.transformed.transform;
4676 static const struct ID2D1TransformedGeometryVtbl d2d_transformed_geometry_vtbl =
4678 d2d_transformed_geometry_QueryInterface,
4679 d2d_transformed_geometry_AddRef,
4680 d2d_transformed_geometry_Release,
4681 d2d_transformed_geometry_GetFactory,
4682 d2d_transformed_geometry_GetBounds,
4683 d2d_transformed_geometry_GetWidenedBounds,
4684 d2d_transformed_geometry_StrokeContainsPoint,
4685 d2d_transformed_geometry_FillContainsPoint,
4686 d2d_transformed_geometry_CompareWithGeometry,
4687 d2d_transformed_geometry_Simplify,
4688 d2d_transformed_geometry_Tessellate,
4689 d2d_transformed_geometry_CombineWithGeometry,
4690 d2d_transformed_geometry_Outline,
4691 d2d_transformed_geometry_ComputeArea,
4692 d2d_transformed_geometry_ComputeLength,
4693 d2d_transformed_geometry_ComputePointAtLength,
4694 d2d_transformed_geometry_Widen,
4695 d2d_transformed_geometry_GetSourceGeometry,
4696 d2d_transformed_geometry_GetTransform,
4699 void d2d_transformed_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
4700 ID2D1Geometry *src_geometry, const D2D_MATRIX_3X2_F *transform)
4702 struct d2d_geometry *src_impl;
4703 D2D_MATRIX_3X2_F g;
4705 src_impl = unsafe_impl_from_ID2D1Geometry(src_geometry);
4707 g = src_impl->transform;
4708 d2d_matrix_multiply(&g, transform);
4709 d2d_geometry_init(geometry, factory, &g, (ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl);
4710 ID2D1Geometry_AddRef(geometry->u.transformed.src_geometry = src_geometry);
4711 geometry->u.transformed.transform = *transform;
4712 geometry->fill = src_impl->fill;
4713 geometry->outline = src_impl->outline;
4716 static inline struct d2d_geometry *impl_from_ID2D1GeometryGroup(ID2D1GeometryGroup *iface)
4718 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
4721 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_QueryInterface(ID2D1GeometryGroup *iface,
4722 REFIID iid, void **out)
4724 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
4726 if (IsEqualGUID(iid, &IID_ID2D1GeometryGroup)
4727 || IsEqualGUID(iid, &IID_ID2D1Geometry)
4728 || IsEqualGUID(iid, &IID_ID2D1Resource)
4729 || IsEqualGUID(iid, &IID_IUnknown))
4731 ID2D1GeometryGroup_AddRef(iface);
4732 *out = iface;
4733 return S_OK;
4736 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
4738 *out = NULL;
4739 return E_NOINTERFACE;
4742 static ULONG STDMETHODCALLTYPE d2d_geometry_group_AddRef(ID2D1GeometryGroup *iface)
4744 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4745 ULONG refcount = InterlockedIncrement(&geometry->refcount);
4747 TRACE("%p increasing refcount to %u.\n", iface, refcount);
4749 return refcount;
4752 static ULONG STDMETHODCALLTYPE d2d_geometry_group_Release(ID2D1GeometryGroup *iface)
4754 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4755 ULONG refcount = InterlockedDecrement(&geometry->refcount);
4756 unsigned int i;
4758 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
4760 if (!refcount)
4762 for (i = 0; i < geometry->u.group.geometry_count; ++i)
4763 ID2D1Geometry_Release(geometry->u.group.src_geometries[i]);
4764 heap_free(geometry->u.group.src_geometries);
4765 d2d_geometry_cleanup(geometry);
4766 heap_free(geometry);
4769 return refcount;
4772 static void STDMETHODCALLTYPE d2d_geometry_group_GetFactory(ID2D1GeometryGroup *iface,
4773 ID2D1Factory **factory)
4775 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4777 TRACE("iface %p, factory %p.\n", iface, factory);
4779 ID2D1Factory_AddRef(*factory = geometry->factory);
4782 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_GetBounds(ID2D1GeometryGroup *iface,
4783 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
4785 FIXME("iface %p, transform %p, bounds %p stub!.\n", iface, transform, bounds);
4787 return E_NOTIMPL;
4790 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_GetWidenedBounds(ID2D1GeometryGroup *iface,
4791 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4792 float tolerance, D2D1_RECT_F *bounds)
4794 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
4795 iface, stroke_width, stroke_style, transform, tolerance, bounds);
4797 return E_NOTIMPL;
4800 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_StrokeContainsPoint(ID2D1GeometryGroup *iface,
4801 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4802 float tolerance, BOOL *contains)
4804 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p.\n",
4805 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
4807 return E_NOTIMPL;
4810 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_FillContainsPoint(ID2D1GeometryGroup *iface,
4811 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4813 FIXME("iface %p, point %s, transform %p, tolerance %.8e, contains %p stub!.\n",
4814 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
4816 return E_NOTIMPL;
4819 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_CompareWithGeometry(ID2D1GeometryGroup *iface,
4820 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
4822 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
4823 iface, geometry, transform, tolerance, relation);
4825 return E_NOTIMPL;
4828 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_Simplify(ID2D1GeometryGroup *iface,
4829 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4830 ID2D1SimplifiedGeometrySink *sink)
4832 FIXME("iface %p, option %#x, transform %p, tolerance %.8e, sink %p stub!.\n",
4833 iface, option, transform, tolerance, sink);
4835 return E_NOTIMPL;
4838 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_Tessellate(ID2D1GeometryGroup *iface,
4839 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
4841 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4843 return E_NOTIMPL;
4846 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_CombineWithGeometry(ID2D1GeometryGroup *iface,
4847 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
4848 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4850 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4851 iface, geometry, combine_mode, transform, tolerance, sink);
4853 return E_NOTIMPL;
4856 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_Outline(ID2D1GeometryGroup *iface,
4857 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4859 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4861 return E_NOTIMPL;
4864 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_ComputeArea(ID2D1GeometryGroup *iface,
4865 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4867 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4869 return E_NOTIMPL;
4872 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_ComputeLength(ID2D1GeometryGroup *iface,
4873 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4875 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4877 return E_NOTIMPL;
4880 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_ComputePointAtLength(ID2D1GeometryGroup *iface,
4881 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
4882 D2D1_POINT_2F *tangent)
4884 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4885 iface, length, transform, tolerance, point, tangent);
4887 return E_NOTIMPL;
4890 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_Widen(ID2D1GeometryGroup *iface, float stroke_width,
4891 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4892 ID2D1SimplifiedGeometrySink *sink)
4894 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4895 iface, stroke_width, stroke_style, transform, tolerance, sink);
4897 return E_NOTIMPL;
4900 static D2D1_FILL_MODE STDMETHODCALLTYPE d2d_geometry_group_GetFillMode(ID2D1GeometryGroup *iface)
4902 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4904 TRACE("iface %p.\n", iface);
4906 return geometry->u.group.fill_mode;
4909 static UINT32 STDMETHODCALLTYPE d2d_geometry_group_GetSourceGeometryCount(ID2D1GeometryGroup *iface)
4911 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4913 TRACE("iface %p.\n", iface);
4915 return geometry->u.group.geometry_count;
4918 static void STDMETHODCALLTYPE d2d_geometry_group_GetSourceGeometries(ID2D1GeometryGroup *iface,
4919 ID2D1Geometry **geometries, UINT32 geometry_count)
4921 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4922 unsigned int i;
4924 TRACE("iface %p, geometries %p, geometry_count %u.\n", iface, geometries, geometry_count);
4926 geometry_count = min(geometry_count, geometry->u.group.geometry_count);
4927 for (i = 0; i < geometry_count; ++i)
4928 ID2D1Geometry_AddRef(geometries[i] = geometry->u.group.src_geometries[i]);
4931 static const struct ID2D1GeometryGroupVtbl d2d_geometry_group_vtbl =
4933 d2d_geometry_group_QueryInterface,
4934 d2d_geometry_group_AddRef,
4935 d2d_geometry_group_Release,
4936 d2d_geometry_group_GetFactory,
4937 d2d_geometry_group_GetBounds,
4938 d2d_geometry_group_GetWidenedBounds,
4939 d2d_geometry_group_StrokeContainsPoint,
4940 d2d_geometry_group_FillContainsPoint,
4941 d2d_geometry_group_CompareWithGeometry,
4942 d2d_geometry_group_Simplify,
4943 d2d_geometry_group_Tessellate,
4944 d2d_geometry_group_CombineWithGeometry,
4945 d2d_geometry_group_Outline,
4946 d2d_geometry_group_ComputeArea,
4947 d2d_geometry_group_ComputeLength,
4948 d2d_geometry_group_ComputePointAtLength,
4949 d2d_geometry_group_Widen,
4950 d2d_geometry_group_GetFillMode,
4951 d2d_geometry_group_GetSourceGeometryCount,
4952 d2d_geometry_group_GetSourceGeometries,
4955 HRESULT d2d_geometry_group_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
4956 D2D1_FILL_MODE fill_mode, ID2D1Geometry **geometries, unsigned int geometry_count)
4958 unsigned int i;
4960 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_geometry_group_vtbl);
4962 if (!(geometry->u.group.src_geometries = heap_calloc(geometry_count, sizeof(*geometries))))
4964 d2d_geometry_cleanup(geometry);
4965 return E_OUTOFMEMORY;
4968 for (i = 0; i < geometry_count; ++i)
4970 ID2D1Geometry_AddRef(geometry->u.group.src_geometries[i] = geometries[i]);
4972 geometry->u.group.geometry_count = geometry_count;
4973 geometry->u.group.fill_mode = fill_mode;
4975 return S_OK;
4978 struct d2d_geometry *unsafe_impl_from_ID2D1Geometry(ID2D1Geometry *iface)
4980 if (!iface)
4981 return NULL;
4982 assert(iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_ellipse_geometry_vtbl
4983 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl
4984 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl
4985 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_rounded_rectangle_geometry_vtbl
4986 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl
4987 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_geometry_group_vtbl);
4988 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);