wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / d2d1 / geometry.c
blob6da93de3eb4b90e06ca20e4e1578442da830f092
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 =
33 {{{
34 1.0f, 0.0f,
35 0.0f, 1.0f,
36 0.0f, 0.0f,
37 }}};
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_curve_vertex_set(struct d2d_curve_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_curve_outline_vertex_set(struct d2d_curve_outline_vertex *a, 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 a->position = *position;
161 a->p0 = *p0;
162 a->p1 = *p1;
163 a->p2 = *p2;
164 d2d_point_set(&a->prev, prev_x, prev_y);
165 d2d_point_set(&a->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 static BOOL d2d_vertex_type_is_bezier(enum d2d_vertex_type t)
414 return (t == D2D_VERTEX_TYPE_BEZIER || t == D2D_VERTEX_TYPE_SPLIT_BEZIER);
417 static BOOL d2d_vertex_type_is_split_bezier(enum d2d_vertex_type t)
419 return t == D2D_VERTEX_TYPE_SPLIT_BEZIER;
422 /* This implementation is based on the paper "Adaptive Precision
423 * Floating-Point Arithmetic and Fast Robust Geometric Predicates" and
424 * associated (Public Domain) code by Jonathan Richard Shewchuk. */
425 static float d2d_point_ccw(const D2D1_POINT_2F *a, const D2D1_POINT_2F *b, const D2D1_POINT_2F *c)
427 static const float err_bound_result = (3.0f + 8.0f * D2D_FP_EPS) * D2D_FP_EPS;
428 static const float err_bound_a = (3.0f + 16.0f * D2D_FP_EPS) * D2D_FP_EPS;
429 static const float err_bound_b = (2.0f + 12.0f * D2D_FP_EPS) * D2D_FP_EPS;
430 static const float err_bound_c = (9.0f + 64.0f * D2D_FP_EPS) * D2D_FP_EPS * D2D_FP_EPS;
431 float det_d[16], det_c2[12], det_c1[8], det_b[4], temp4[4], temp2a[2], temp2b[2], abxacy[2], abyacx[2];
432 size_t det_d_len, det_c2_len, det_c1_len;
433 float det, det_sum, err_bound;
434 struct d2d_fp_two_vec2 ab, ac;
436 ab.x[1] = b->x - a->x;
437 ab.y[1] = b->y - a->y;
438 ac.x[1] = c->x - a->x;
439 ac.y[1] = c->y - a->y;
441 abxacy[1] = ab.x[1] * ac.y[1];
442 abyacx[1] = ab.y[1] * ac.x[1];
443 det = abxacy[1] - abyacx[1];
445 if (abxacy[1] > 0.0f)
447 if (abyacx[1] <= 0.0f)
448 return det;
449 det_sum = abxacy[1] + abyacx[1];
451 else if (abxacy[1] < 0.0f)
453 if (abyacx[1] >= 0.0f)
454 return det;
455 det_sum = -abxacy[1] - abyacx[1];
457 else
459 return det;
462 err_bound = err_bound_a * det_sum;
463 if (det >= err_bound || -det >= err_bound)
464 return det;
466 d2d_fp_two_product(abxacy, ab.x[1], ac.y[1]);
467 d2d_fp_two_product(abyacx, ab.y[1], ac.x[1]);
468 d2d_fp_two_two_diff(det_b, abxacy, abyacx);
470 det = d2d_fp_estimate(det_b, 4);
471 err_bound = err_bound_b * det_sum;
472 if (det >= err_bound || -det >= err_bound)
473 return det;
475 d2d_fp_two_diff_tail(&ab.x[0], b->x, a->x, ab.x[1]);
476 d2d_fp_two_diff_tail(&ab.y[0], b->y, a->y, ab.y[1]);
477 d2d_fp_two_diff_tail(&ac.x[0], c->x, a->x, ac.x[1]);
478 d2d_fp_two_diff_tail(&ac.y[0], c->y, a->y, ac.y[1]);
480 if (ab.x[0] == 0.0f && ab.y[0] == 0.0f && ac.x[0] == 0.0f && ac.y[0] == 0.0f)
481 return det;
483 err_bound = err_bound_c * det_sum + err_bound_result * fabsf(det);
484 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]);
485 if (det >= err_bound || -det >= err_bound)
486 return det;
488 d2d_fp_two_product(temp2a, ab.x[0], ac.y[1]);
489 d2d_fp_two_product(temp2b, ab.y[0], ac.x[1]);
490 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
491 d2d_fp_fast_expansion_sum_zeroelim(det_c1, &det_c1_len, det_b, 4, temp4, 4);
493 d2d_fp_two_product(temp2a, ab.x[1], ac.y[0]);
494 d2d_fp_two_product(temp2b, ab.y[1], ac.x[0]);
495 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
496 d2d_fp_fast_expansion_sum_zeroelim(det_c2, &det_c2_len, det_c1, det_c1_len, temp4, 4);
498 d2d_fp_two_product(temp2a, ab.x[0], ac.y[0]);
499 d2d_fp_two_product(temp2b, ab.y[0], ac.x[0]);
500 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
501 d2d_fp_fast_expansion_sum_zeroelim(det_d, &det_d_len, det_c2, det_c2_len, temp4, 4);
503 return det_d[det_d_len - 1];
506 static void d2d_rect_union(D2D1_RECT_F *l, const D2D1_RECT_F *r)
508 l->left = min(l->left, r->left);
509 l->top = min(l->top, r->top);
510 l->right = max(l->right, r->right);
511 l->bottom = max(l->bottom, r->bottom);
514 static BOOL d2d_rect_check_overlap(const D2D_RECT_F *p, const D2D_RECT_F *q)
516 return p->left < q->right && p->top < q->bottom && p->right > q->left && p->bottom > q->top;
519 static void d2d_rect_get_bezier_bounds(D2D_RECT_F *bounds, const D2D1_POINT_2F *p0,
520 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
522 D2D1_POINT_2F p;
523 float root;
525 bounds->left = p0->x;
526 bounds->top = p0->y;
527 bounds->right = p0->x;
528 bounds->bottom = p0->y;
530 d2d_rect_expand(bounds, p2);
532 /* f(t) = (1 - t)²P₀ + 2(1 - t)tP₁ + t²P₂
533 * f'(t) = 2(1 - t)(P₁ - P₀) + 2t(P₂ - P₁)
534 * = 2(P₂ - 2P₁ + P₀)t + 2(P₁ - P₀)
536 * f'(t) = 0
537 * t = (P₀ - P₁) / (P₂ - 2P₁ + P₀) */
538 root = (p0->x - p1->x) / (p2->x - 2.0f * p1->x + p0->x);
539 if (root > 0.0f && root < 1.0f)
541 d2d_point_calculate_bezier(&p, p0, p1, p2, root);
542 d2d_rect_expand(bounds, &p);
545 root = (p0->y - p1->y) / (p2->y - 2.0f * p1->y + p0->y);
546 if (root > 0.0f && root < 1.0f)
548 d2d_point_calculate_bezier(&p, p0, p1, p2, root);
549 d2d_rect_expand(bounds, &p);
553 static void d2d_rect_get_bezier_segment_bounds(D2D_RECT_F *bounds, const D2D1_POINT_2F *p0,
554 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float start, float end)
556 D2D1_POINT_2F q[3], r[2];
558 d2d_point_lerp(&r[0], p0, p1, start);
559 d2d_point_lerp(&r[1], p1, p2, start);
560 d2d_point_lerp(&q[0], &r[0], &r[1], start);
562 end = (end - start) / (1.0f - start);
563 d2d_point_lerp(&q[1], &q[0], &r[1], end);
564 d2d_point_lerp(&r[0], &r[1], p2, end);
565 d2d_point_lerp(&q[2], &q[1], &r[0], end);
567 d2d_rect_get_bezier_bounds(bounds, &q[0], &q[1], &q[2]);
570 static BOOL d2d_figure_insert_vertex(struct d2d_figure *figure, size_t idx, D2D1_POINT_2F vertex)
572 if (!d2d_array_reserve((void **)&figure->vertices, &figure->vertices_size,
573 figure->vertex_count + 1, sizeof(*figure->vertices)))
575 ERR("Failed to grow vertices array.\n");
576 return FALSE;
579 if (!d2d_array_reserve((void **)&figure->vertex_types, &figure->vertex_types_size,
580 figure->vertex_count + 1, sizeof(*figure->vertex_types)))
582 ERR("Failed to grow vertex types array.\n");
583 return FALSE;
586 memmove(&figure->vertices[idx + 1], &figure->vertices[idx],
587 (figure->vertex_count - idx) * sizeof(*figure->vertices));
588 memmove(&figure->vertex_types[idx + 1], &figure->vertex_types[idx],
589 (figure->vertex_count - idx) * sizeof(*figure->vertex_types));
590 figure->vertices[idx] = vertex;
591 figure->vertex_types[idx] = D2D_VERTEX_TYPE_NONE;
592 d2d_rect_expand(&figure->bounds, &vertex);
593 ++figure->vertex_count;
594 return TRUE;
597 static BOOL d2d_figure_add_vertex(struct d2d_figure *figure, D2D1_POINT_2F vertex)
599 size_t last = figure->vertex_count - 1;
601 if (figure->vertex_count && figure->vertex_types[last] == D2D_VERTEX_TYPE_LINE
602 && !memcmp(&figure->vertices[last], &vertex, sizeof(vertex)))
603 return TRUE;
605 if (!d2d_array_reserve((void **)&figure->vertices, &figure->vertices_size,
606 figure->vertex_count + 1, sizeof(*figure->vertices)))
608 ERR("Failed to grow vertices array.\n");
609 return FALSE;
612 if (!d2d_array_reserve((void **)&figure->vertex_types, &figure->vertex_types_size,
613 figure->vertex_count + 1, sizeof(*figure->vertex_types)))
615 ERR("Failed to grow vertex types array.\n");
616 return FALSE;
619 figure->vertices[figure->vertex_count] = vertex;
620 figure->vertex_types[figure->vertex_count] = D2D_VERTEX_TYPE_NONE;
621 d2d_rect_expand(&figure->bounds, &vertex);
622 ++figure->vertex_count;
623 return TRUE;
626 static BOOL d2d_figure_insert_bezier_controls(struct d2d_figure *figure,
627 size_t idx, size_t count, const D2D1_POINT_2F *p)
629 if (!d2d_array_reserve((void **)&figure->bezier_controls, &figure->bezier_controls_size,
630 figure->bezier_control_count + count, sizeof(*figure->bezier_controls)))
632 ERR("Failed to grow bezier controls array.\n");
633 return FALSE;
636 memmove(&figure->bezier_controls[idx + count], &figure->bezier_controls[idx],
637 (figure->bezier_control_count - idx) * sizeof(*figure->bezier_controls));
638 memcpy(&figure->bezier_controls[idx], p, count * sizeof(*figure->bezier_controls));
639 figure->bezier_control_count += count;
641 return TRUE;
644 static BOOL d2d_figure_add_bezier_controls(struct d2d_figure *figure, size_t count, const D2D1_POINT_2F *p)
646 if (!d2d_array_reserve((void **)&figure->bezier_controls, &figure->bezier_controls_size,
647 figure->bezier_control_count + count, sizeof(*figure->bezier_controls)))
649 ERR("Failed to grow bezier controls array.\n");
650 return FALSE;
653 memcpy(&figure->bezier_controls[figure->bezier_control_count], p, count * sizeof(*figure->bezier_controls));
654 figure->bezier_control_count += count;
656 return TRUE;
659 static void d2d_cdt_edge_rot(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_ROT) & 3;
665 static void d2d_cdt_edge_sym(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
667 dst->idx = src->idx;
668 dst->r = (src->r + D2D_EDGE_NEXT_SYM) & 3;
671 static void d2d_cdt_edge_tor(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
673 dst->idx = src->idx;
674 dst->r = (src->r + D2D_EDGE_NEXT_TOR) & 3;
677 static void d2d_cdt_edge_next_left(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_TOR) & 3]);
683 static void d2d_cdt_edge_next_origin(const struct d2d_cdt *cdt,
684 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
686 *dst = cdt->edges[src->idx].next[src->r];
689 static void d2d_cdt_edge_prev_origin(const struct d2d_cdt *cdt,
690 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
692 d2d_cdt_edge_rot(dst, &cdt->edges[src->idx].next[(src->r + D2D_EDGE_NEXT_ROT) & 3]);
695 static size_t d2d_cdt_edge_origin(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
697 return cdt->edges[e->idx].vertex[e->r >> 1];
700 static size_t d2d_cdt_edge_destination(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
702 return cdt->edges[e->idx].vertex[!(e->r >> 1)];
705 static void d2d_cdt_edge_set_origin(const struct d2d_cdt *cdt,
706 const struct d2d_cdt_edge_ref *e, size_t vertex)
708 cdt->edges[e->idx].vertex[e->r >> 1] = vertex;
711 static void d2d_cdt_edge_set_destination(const struct d2d_cdt *cdt,
712 const struct d2d_cdt_edge_ref *e, size_t vertex)
714 cdt->edges[e->idx].vertex[!(e->r >> 1)] = vertex;
717 static float d2d_cdt_ccw(const struct d2d_cdt *cdt, size_t a, size_t b, size_t c)
719 return d2d_point_ccw(&cdt->vertices[a], &cdt->vertices[b], &cdt->vertices[c]);
722 static BOOL d2d_cdt_rightof(const struct d2d_cdt *cdt, size_t p, const struct d2d_cdt_edge_ref *e)
724 return d2d_cdt_ccw(cdt, p, d2d_cdt_edge_destination(cdt, e), d2d_cdt_edge_origin(cdt, e)) > 0.0f;
727 static BOOL d2d_cdt_leftof(const struct d2d_cdt *cdt, size_t p, const struct d2d_cdt_edge_ref *e)
729 return d2d_cdt_ccw(cdt, p, d2d_cdt_edge_origin(cdt, e), d2d_cdt_edge_destination(cdt, e)) > 0.0f;
732 /* |ax ay|
733 * |bx by| */
734 static void d2d_fp_four_det2x2(float *out, float ax, float ay, float bx, float by)
736 float axby[2], aybx[2];
738 d2d_fp_two_product(axby, ax, by);
739 d2d_fp_two_product(aybx, ay, bx);
740 d2d_fp_two_two_diff(out, axby, aybx);
743 /* (a->x² + a->y²) * det2x2 */
744 static void d2d_fp_sub_det3x3(float *out, size_t *out_len, const struct d2d_fp_two_vec2 *a, const float *det2x2)
746 size_t axd_len, ayd_len, axxd_len, ayyd_len;
747 float axd[8], ayd[8], axxd[16], ayyd[16];
749 d2d_fp_scale_expansion_zeroelim(axd, &axd_len, det2x2, 4, a->x[1]);
750 d2d_fp_scale_expansion_zeroelim(axxd, &axxd_len, axd, axd_len, a->x[1]);
751 d2d_fp_scale_expansion_zeroelim(ayd, &ayd_len, det2x2, 4, a->y[1]);
752 d2d_fp_scale_expansion_zeroelim(ayyd, &ayyd_len, ayd, ayd_len, a->y[1]);
753 d2d_fp_fast_expansion_sum_zeroelim(out, out_len, axxd, axxd_len, ayyd, ayyd_len);
756 /* det_abt = det_ab * c[0]
757 * fin += c[0] * (az * b - bz * a + c[1] * det_ab * 2.0f) */
758 static void d2d_cdt_incircle_refine1(struct d2d_fp_fin *fin, float *det_abt, size_t *det_abt_len,
759 const float *det_ab, float a, const float *az, float b, const float *bz, const float *c)
761 size_t temp48_len, temp32_len, temp16a_len, temp16b_len, temp16c_len, temp8_len;
762 float temp48[48], temp32[32], temp16a[16], temp16b[16], temp16c[16], temp8[8];
763 float *swap;
765 d2d_fp_scale_expansion_zeroelim(det_abt, det_abt_len, det_ab, 4, c[0]);
766 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, det_abt, *det_abt_len, 2.0f * c[1]);
767 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, az, 4, c[0]);
768 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, temp8, temp8_len, b);
769 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, bz, 4, c[0]);
770 d2d_fp_scale_expansion_zeroelim(temp16c, &temp16c_len, temp8, temp8_len, -a);
771 d2d_fp_fast_expansion_sum_zeroelim(temp32, &temp32_len, temp16a, temp16a_len, temp16b, temp16b_len);
772 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16c, temp16c_len, temp32, temp32_len);
773 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
774 swap = fin->now; fin->now = fin->other; fin->other = swap;
777 static void d2d_cdt_incircle_refine2(struct d2d_fp_fin *fin, const struct d2d_fp_two_vec2 *a,
778 const struct d2d_fp_two_vec2 *b, const float *bz, const struct d2d_fp_two_vec2 *c, const float *cz,
779 const float *axt_det_bc, size_t axt_det_bc_len, const float *ayt_det_bc, size_t ayt_det_bc_len)
781 size_t temp64_len, temp48_len, temp32a_len, temp32b_len, temp16a_len, temp16b_len, temp8_len;
782 float temp64[64], temp48[48], temp32a[32], temp32b[32], temp16a[16], temp16b[16], temp8[8];
783 float bct[8], bctt[4], temp4a[4], temp4b[4], temp2a[2], temp2b[2];
784 size_t bct_len, bctt_len;
785 float *swap;
787 /* 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]) */
788 /* bctt = b->x[0] * c->y[0] + c->x[0] * b->y[0] */
789 if (b->x[0] != 0.0f || b->y[0] != 0.0f || c->x[0] != 0.0f || c->y[0] != 0.0f)
791 d2d_fp_two_product(temp2a, b->x[0], c->y[1]);
792 d2d_fp_two_product(temp2b, b->x[1], c->y[0]);
793 d2d_fp_two_two_sum(temp4a, temp2a, temp2b);
794 d2d_fp_two_product(temp2a, c->x[0], -b->y[1]);
795 d2d_fp_two_product(temp2b, c->x[1], -b->y[0]);
796 d2d_fp_two_two_sum(temp4b, temp2a, temp2b);
797 d2d_fp_fast_expansion_sum_zeroelim(bct, &bct_len, temp4a, 4, temp4b, 4);
799 d2d_fp_two_product(temp2a, b->x[0], c->y[0]);
800 d2d_fp_two_product(temp2b, c->x[0], b->y[0]);
801 d2d_fp_two_two_diff(bctt, temp2a, temp2b);
802 bctt_len = 4;
804 else
806 bct[0] = 0.0f;
807 bct_len = 1;
808 bctt[0] = 0.0f;
809 bctt_len = 1;
812 if (a->x[0] != 0.0f)
814 size_t axt_bct_len, axt_bctt_len;
815 float axt_bct[16], axt_bctt[8];
817 /* fin += a->x[0] * (axt_det_bc + bct * 2.0f * a->x[1]) */
818 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, axt_det_bc, axt_det_bc_len, a->x[0]);
819 d2d_fp_scale_expansion_zeroelim(axt_bct, &axt_bct_len, bct, bct_len, a->x[0]);
820 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, axt_bct, axt_bct_len, 2.0f * a->x[1]);
821 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16a, temp16a_len, temp32a, temp32a_len);
822 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
823 swap = fin->now; fin->now = fin->other; fin->other = swap;
825 if (b->y[0] != 0.0f)
827 /* fin += a->x[0] * cz * b->y[0] */
828 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, cz, 4, a->x[0]);
829 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, temp8, temp8_len, b->y[0]);
830 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp16a, temp16a_len);
831 swap = fin->now; fin->now = fin->other; fin->other = swap;
834 if (c->y[0] != 0.0f)
836 /* fin -= a->x[0] * bz * c->y[0] */
837 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, bz, 4, -a->x[0]);
838 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, temp8, temp8_len, c->y[0]);
839 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp16a, temp16a_len);
840 swap = fin->now; fin->now = fin->other; fin->other = swap;
843 /* fin += a->x[0] * (bct * a->x[0] + bctt * (2.0f * a->x[1] + a->x[0])) */
844 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, axt_bct, axt_bct_len, a->x[0]);
845 d2d_fp_scale_expansion_zeroelim(axt_bctt, &axt_bctt_len, bctt, bctt_len, a->x[0]);
846 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, axt_bctt, axt_bctt_len, 2.0f * a->x[1]);
847 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, axt_bctt, axt_bctt_len, a->x[0]);
848 d2d_fp_fast_expansion_sum_zeroelim(temp32b, &temp32b_len, temp16a, temp16a_len, temp16b, temp16b_len);
849 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, temp32a, temp32a_len, temp32b, temp32b_len);
850 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp64, temp64_len);
851 swap = fin->now; fin->now = fin->other; fin->other = swap;
854 if (a->y[0] != 0.0f)
856 size_t ayt_bct_len, ayt_bctt_len;
857 float ayt_bct[16], ayt_bctt[8];
859 /* fin += a->y[0] * (ayt_det_bc + bct * 2.0f * a->y[1]) */
860 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, ayt_det_bc, ayt_det_bc_len, a->y[0]);
861 d2d_fp_scale_expansion_zeroelim(ayt_bct, &ayt_bct_len, bct, bct_len, a->y[0]);
862 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, ayt_bct, ayt_bct_len, 2.0f * a->y[1]);
863 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16a, temp16a_len, temp32a, temp32a_len);
864 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
865 swap = fin->now; fin->now = fin->other; fin->other = swap;
867 /* fin += a->y[0] * (bct * a->y[0] + bctt * (2.0f * a->y[1] + a->y[0])) */
868 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, ayt_bct, ayt_bct_len, a->y[0]);
869 d2d_fp_scale_expansion_zeroelim(ayt_bctt, &ayt_bctt_len, bctt, bctt_len, a->y[0]);
870 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, ayt_bctt, ayt_bctt_len, 2.0f * a->y[1]);
871 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, ayt_bctt, ayt_bctt_len, a->y[0]);
872 d2d_fp_fast_expansion_sum_zeroelim(temp32b, &temp32b_len, temp16a, temp16a_len, temp16b, temp16b_len);
873 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, temp32a, temp32a_len, temp32b, temp32b_len);
874 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp64, temp64_len);
875 swap = fin->now; fin->now = fin->other; fin->other = swap;
879 /* Determine if point D is inside or outside the circle defined by points A,
880 * B, C. As explained in the paper by Guibas and Stolfi, this is equivalent to
881 * calculating the signed volume of the tetrahedron defined by projecting the
882 * points onto the paraboloid of revolution x = x² + y²,
883 * λ:(x, y) → (x, y, x² + y²). I.e., D is inside the cirlce if
885 * |λ(A) 1|
886 * |λ(B) 1| > 0
887 * |λ(C) 1|
888 * |λ(D) 1|
890 * After translating D to the origin, that becomes:
892 * |λ(A-D)|
893 * |λ(B-D)| > 0
894 * |λ(C-D)|
896 * This implementation is based on the paper "Adaptive Precision
897 * Floating-Point Arithmetic and Fast Robust Geometric Predicates" and
898 * associated (Public Domain) code by Jonathan Richard Shewchuk. */
899 static BOOL d2d_cdt_incircle(const struct d2d_cdt *cdt, size_t a, size_t b, size_t c, size_t d)
901 static const float err_bound_result = (3.0f + 8.0f * D2D_FP_EPS) * D2D_FP_EPS;
902 static const float err_bound_a = (10.0f + 96.0f * D2D_FP_EPS) * D2D_FP_EPS;
903 static const float err_bound_b = (4.0f + 48.0f * D2D_FP_EPS) * D2D_FP_EPS;
904 static const float err_bound_c = (44.0f + 576.0f * D2D_FP_EPS) * D2D_FP_EPS * D2D_FP_EPS;
906 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;
907 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];
908 float fin1[1152], fin2[1152], temp64[64], sub_det_a[32], sub_det_b[32], sub_det_c[32];
909 float det_bc[4], det_ca[4], det_ab[4], daz[4], dbz[4], dcz[4], temp2a[2], temp2b[2];
910 size_t temp64_len, sub_det_a_len, sub_det_b_len, sub_det_c_len;
911 float dbxdcy, dbydcx, dcxday, dcydax, daxdby, daydbx;
912 const D2D1_POINT_2F *p = cdt->vertices;
913 struct d2d_fp_two_vec2 da, db, dc;
914 float permanent, err_bound, det;
915 struct d2d_fp_fin fin;
917 da.x[1] = p[a].x - p[d].x;
918 da.y[1] = p[a].y - p[d].y;
919 db.x[1] = p[b].x - p[d].x;
920 db.y[1] = p[b].y - p[d].y;
921 dc.x[1] = p[c].x - p[d].x;
922 dc.y[1] = p[c].y - p[d].y;
924 daz[3] = da.x[1] * da.x[1] + da.y[1] * da.y[1];
925 dbxdcy = db.x[1] * dc.y[1];
926 dbydcx = db.y[1] * dc.x[1];
928 dbz[3] = db.x[1] * db.x[1] + db.y[1] * db.y[1];
929 dcxday = dc.x[1] * da.y[1];
930 dcydax = dc.y[1] * da.x[1];
932 dcz[3] = dc.x[1] * dc.x[1] + dc.y[1] * dc.y[1];
933 daxdby = da.x[1] * db.y[1];
934 daydbx = da.y[1] * db.x[1];
936 det = daz[3] * (dbxdcy - dbydcx) + dbz[3] * (dcxday - dcydax) + dcz[3] * (daxdby - daydbx);
937 permanent = daz[3] * (fabsf(dbxdcy) + fabsf(dbydcx))
938 + dbz[3] * (fabsf(dcxday) + fabsf(dcydax))
939 + dcz[3] * (fabsf(daxdby) + fabsf(daydbx));
940 err_bound = err_bound_a * permanent;
941 if (det > err_bound || -det > err_bound)
942 return det > 0.0f;
944 fin.now = fin1;
945 fin.other = fin2;
947 d2d_fp_four_det2x2(det_bc, db.x[1], db.y[1], dc.x[1], dc.y[1]);
948 d2d_fp_sub_det3x3(sub_det_a, &sub_det_a_len, &da, det_bc);
950 d2d_fp_four_det2x2(det_ca, dc.x[1], dc.y[1], da.x[1], da.y[1]);
951 d2d_fp_sub_det3x3(sub_det_b, &sub_det_b_len, &db, det_ca);
953 d2d_fp_four_det2x2(det_ab, da.x[1], da.y[1], db.x[1], db.y[1]);
954 d2d_fp_sub_det3x3(sub_det_c, &sub_det_c_len, &dc, det_ab);
956 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, sub_det_a, sub_det_a_len, sub_det_b, sub_det_b_len);
957 d2d_fp_fast_expansion_sum_zeroelim(fin.now, &fin.length, temp64, temp64_len, sub_det_c, sub_det_c_len);
958 det = d2d_fp_estimate(fin.now, fin.length);
959 err_bound = err_bound_b * permanent;
960 if (det >= err_bound || -det >= err_bound)
961 return det > 0.0f;
963 d2d_fp_two_diff_tail(&da.x[0], p[a].x, p[d].x, da.x[1]);
964 d2d_fp_two_diff_tail(&da.y[0], p[a].y, p[d].y, da.y[1]);
965 d2d_fp_two_diff_tail(&db.x[0], p[b].x, p[d].x, db.x[1]);
966 d2d_fp_two_diff_tail(&db.y[0], p[b].y, p[d].y, db.y[1]);
967 d2d_fp_two_diff_tail(&dc.x[0], p[c].x, p[d].x, dc.x[1]);
968 d2d_fp_two_diff_tail(&dc.y[0], p[c].y, p[d].y, dc.y[1]);
969 if (da.x[0] == 0.0f && db.x[0] == 0.0f && dc.x[0] == 0.0f
970 && da.y[0] == 0.0f && db.y[0] == 0.0f && dc.y[0] == 0.0f)
971 return det > 0.0f;
973 err_bound = err_bound_c * permanent + err_bound_result * fabsf(det);
974 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]))
975 + 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]))
976 + (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]))
977 + 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]))
978 + (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]))
979 + 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]));
980 if (det >= err_bound || -det >= err_bound)
981 return det > 0.0f;
983 if (db.x[0] != 0.0f || db.y[0] != 0.0f || dc.x[0] != 0.0f || dc.y[0] != 0.0f)
985 d2d_fp_square(temp2a, da.x[1]);
986 d2d_fp_square(temp2b, da.y[1]);
987 d2d_fp_two_two_sum(daz, temp2a, temp2b);
989 if (dc.x[0] != 0.0f || dc.y[0] != 0.0f || da.x[0] != 0.0f || da.y[0] != 0.0f)
991 d2d_fp_square(temp2a, db.x[1]);
992 d2d_fp_square(temp2b, db.y[1]);
993 d2d_fp_two_two_sum(dbz, temp2a, temp2b);
995 if (da.x[0] != 0.0f || da.y[0] != 0.0f || db.x[0] != 0.0f || db.y[0] != 0.0f)
997 d2d_fp_square(temp2a, dc.x[1]);
998 d2d_fp_square(temp2b, dc.y[1]);
999 d2d_fp_two_two_sum(dcz, temp2a, temp2b);
1002 if (da.x[0] != 0.0f)
1003 d2d_cdt_incircle_refine1(&fin, axt_det_bc, &axt_det_bc_len, det_bc, dc.y[1], dcz, db.y[1], dbz, da.x);
1004 if (da.y[0] != 0.0f)
1005 d2d_cdt_incircle_refine1(&fin, ayt_det_bc, &ayt_det_bc_len, det_bc, db.x[1], dbz, dc.x[1], dcz, da.y);
1006 if (db.x[0] != 0.0f)
1007 d2d_cdt_incircle_refine1(&fin, bxt_det_ca, &bxt_det_ca_len, det_ca, da.y[1], daz, dc.y[1], dcz, db.x);
1008 if (db.y[0] != 0.0f)
1009 d2d_cdt_incircle_refine1(&fin, byt_det_ca, &byt_det_ca_len, det_ca, dc.x[1], dcz, da.x[1], daz, db.y);
1010 if (dc.x[0] != 0.0f)
1011 d2d_cdt_incircle_refine1(&fin, cxt_det_ab, &cxt_det_ab_len, det_ab, db.y[1], dbz, da.y[1], daz, dc.x);
1012 if (dc.y[0] != 0.0f)
1013 d2d_cdt_incircle_refine1(&fin, cyt_det_ab, &cyt_det_ab_len, det_ab, da.x[1], daz, db.x[1], dbz, dc.y);
1015 if (da.x[0] != 0.0f || da.y[0] != 0.0f)
1016 d2d_cdt_incircle_refine2(&fin, &da, &db, dbz, &dc, dcz,
1017 axt_det_bc, axt_det_bc_len, ayt_det_bc, ayt_det_bc_len);
1018 if (db.x[0] != 0.0f || db.y[0] != 0.0f)
1019 d2d_cdt_incircle_refine2(&fin, &db, &dc, dcz, &da, daz,
1020 bxt_det_ca, bxt_det_ca_len, byt_det_ca, byt_det_ca_len);
1021 if (dc.x[0] != 0.0f || dc.y[0] != 0.0f)
1022 d2d_cdt_incircle_refine2(&fin, &dc, &da, daz, &db, dbz,
1023 cxt_det_ab, cxt_det_ab_len, cyt_det_ab, cyt_det_ab_len);
1025 return fin.now[fin.length - 1] > 0.0f;
1028 static void d2d_cdt_splice(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *a,
1029 const struct d2d_cdt_edge_ref *b)
1031 struct d2d_cdt_edge_ref ta, tb, alpha, beta;
1033 ta = cdt->edges[a->idx].next[a->r];
1034 tb = cdt->edges[b->idx].next[b->r];
1035 cdt->edges[a->idx].next[a->r] = tb;
1036 cdt->edges[b->idx].next[b->r] = ta;
1038 d2d_cdt_edge_rot(&alpha, &ta);
1039 d2d_cdt_edge_rot(&beta, &tb);
1041 ta = cdt->edges[alpha.idx].next[alpha.r];
1042 tb = cdt->edges[beta.idx].next[beta.r];
1043 cdt->edges[alpha.idx].next[alpha.r] = tb;
1044 cdt->edges[beta.idx].next[beta.r] = ta;
1047 static BOOL d2d_cdt_create_edge(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *e)
1049 struct d2d_cdt_edge *edge;
1051 if (cdt->free_edge != ~0u)
1053 e->idx = cdt->free_edge;
1054 cdt->free_edge = cdt->edges[e->idx].next[D2D_EDGE_NEXT_ORIGIN].idx;
1056 else
1058 if (!d2d_array_reserve((void **)&cdt->edges, &cdt->edges_size, cdt->edge_count + 1, sizeof(*cdt->edges)))
1060 ERR("Failed to grow edges array.\n");
1061 return FALSE;
1063 e->idx = cdt->edge_count++;
1065 e->r = 0;
1067 edge = &cdt->edges[e->idx];
1068 edge->next[D2D_EDGE_NEXT_ORIGIN] = *e;
1069 d2d_cdt_edge_tor(&edge->next[D2D_EDGE_NEXT_ROT], e);
1070 d2d_cdt_edge_sym(&edge->next[D2D_EDGE_NEXT_SYM], e);
1071 d2d_cdt_edge_rot(&edge->next[D2D_EDGE_NEXT_TOR], e);
1072 edge->flags = 0;
1074 return TRUE;
1077 static void d2d_cdt_destroy_edge(struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
1079 struct d2d_cdt_edge_ref next, sym, prev;
1081 d2d_cdt_edge_next_origin(cdt, &next, e);
1082 if (next.idx != e->idx || next.r != e->r)
1084 d2d_cdt_edge_prev_origin(cdt, &prev, e);
1085 d2d_cdt_splice(cdt, e, &prev);
1088 d2d_cdt_edge_sym(&sym, e);
1090 d2d_cdt_edge_next_origin(cdt, &next, &sym);
1091 if (next.idx != sym.idx || next.r != sym.r)
1093 d2d_cdt_edge_prev_origin(cdt, &prev, &sym);
1094 d2d_cdt_splice(cdt, &sym, &prev);
1097 cdt->edges[e->idx].flags |= D2D_CDT_EDGE_FLAG_FREED;
1098 cdt->edges[e->idx].next[D2D_EDGE_NEXT_ORIGIN].idx = cdt->free_edge;
1099 cdt->free_edge = e->idx;
1102 static BOOL d2d_cdt_connect(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *e,
1103 const struct d2d_cdt_edge_ref *a, const struct d2d_cdt_edge_ref *b)
1105 struct d2d_cdt_edge_ref tmp;
1107 if (!d2d_cdt_create_edge(cdt, e))
1108 return FALSE;
1109 d2d_cdt_edge_set_origin(cdt, e, d2d_cdt_edge_destination(cdt, a));
1110 d2d_cdt_edge_set_destination(cdt, e, d2d_cdt_edge_origin(cdt, b));
1111 d2d_cdt_edge_next_left(cdt, &tmp, a);
1112 d2d_cdt_splice(cdt, e, &tmp);
1113 d2d_cdt_edge_sym(&tmp, e);
1114 d2d_cdt_splice(cdt, &tmp, b);
1116 return TRUE;
1119 static BOOL d2d_cdt_merge(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *left_outer,
1120 struct d2d_cdt_edge_ref *left_inner, struct d2d_cdt_edge_ref *right_inner,
1121 struct d2d_cdt_edge_ref *right_outer)
1123 struct d2d_cdt_edge_ref base_edge, tmp;
1125 /* Create the base edge between both parts. */
1126 for (;;)
1128 if (d2d_cdt_leftof(cdt, d2d_cdt_edge_origin(cdt, right_inner), left_inner))
1130 d2d_cdt_edge_next_left(cdt, left_inner, left_inner);
1132 else if (d2d_cdt_rightof(cdt, d2d_cdt_edge_origin(cdt, left_inner), right_inner))
1134 d2d_cdt_edge_sym(&tmp, right_inner);
1135 d2d_cdt_edge_next_origin(cdt, right_inner, &tmp);
1137 else
1139 break;
1143 d2d_cdt_edge_sym(&tmp, right_inner);
1144 if (!d2d_cdt_connect(cdt, &base_edge, &tmp, left_inner))
1145 return FALSE;
1146 if (d2d_cdt_edge_origin(cdt, left_inner) == d2d_cdt_edge_origin(cdt, left_outer))
1147 d2d_cdt_edge_sym(left_outer, &base_edge);
1148 if (d2d_cdt_edge_origin(cdt, right_inner) == d2d_cdt_edge_origin(cdt, right_outer))
1149 *right_outer = base_edge;
1151 for (;;)
1153 struct d2d_cdt_edge_ref left_candidate, right_candidate, sym_base_edge;
1154 BOOL left_valid, right_valid;
1156 /* Find the left candidate. */
1157 d2d_cdt_edge_sym(&sym_base_edge, &base_edge);
1158 d2d_cdt_edge_next_origin(cdt, &left_candidate, &sym_base_edge);
1159 if ((left_valid = d2d_cdt_leftof(cdt, d2d_cdt_edge_destination(cdt, &left_candidate), &sym_base_edge)))
1161 d2d_cdt_edge_next_origin(cdt, &tmp, &left_candidate);
1162 while (d2d_cdt_edge_destination(cdt, &tmp) != d2d_cdt_edge_destination(cdt, &sym_base_edge)
1163 && d2d_cdt_incircle(cdt,
1164 d2d_cdt_edge_origin(cdt, &sym_base_edge), d2d_cdt_edge_destination(cdt, &sym_base_edge),
1165 d2d_cdt_edge_destination(cdt, &left_candidate), d2d_cdt_edge_destination(cdt, &tmp)))
1167 d2d_cdt_destroy_edge(cdt, &left_candidate);
1168 left_candidate = tmp;
1169 d2d_cdt_edge_next_origin(cdt, &tmp, &left_candidate);
1172 d2d_cdt_edge_sym(&left_candidate, &left_candidate);
1174 /* Find the right candidate. */
1175 d2d_cdt_edge_prev_origin(cdt, &right_candidate, &base_edge);
1176 if ((right_valid = d2d_cdt_rightof(cdt, d2d_cdt_edge_destination(cdt, &right_candidate), &base_edge)))
1178 d2d_cdt_edge_prev_origin(cdt, &tmp, &right_candidate);
1179 while (d2d_cdt_edge_destination(cdt, &tmp) != d2d_cdt_edge_destination(cdt, &base_edge)
1180 && d2d_cdt_incircle(cdt,
1181 d2d_cdt_edge_origin(cdt, &sym_base_edge), d2d_cdt_edge_destination(cdt, &sym_base_edge),
1182 d2d_cdt_edge_destination(cdt, &right_candidate), d2d_cdt_edge_destination(cdt, &tmp)))
1184 d2d_cdt_destroy_edge(cdt, &right_candidate);
1185 right_candidate = tmp;
1186 d2d_cdt_edge_prev_origin(cdt, &tmp, &right_candidate);
1190 if (!left_valid && !right_valid)
1191 break;
1193 /* Connect the appropriate candidate with the base edge. */
1194 if (!left_valid || (right_valid && d2d_cdt_incircle(cdt,
1195 d2d_cdt_edge_origin(cdt, &left_candidate), d2d_cdt_edge_destination(cdt, &left_candidate),
1196 d2d_cdt_edge_origin(cdt, &right_candidate), d2d_cdt_edge_destination(cdt, &right_candidate))))
1198 if (!d2d_cdt_connect(cdt, &base_edge, &right_candidate, &sym_base_edge))
1199 return FALSE;
1201 else
1203 if (!d2d_cdt_connect(cdt, &base_edge, &sym_base_edge, &left_candidate))
1204 return FALSE;
1208 return TRUE;
1211 /* Create a Delaunay triangulation from a set of vertices. This is an
1212 * implementation of the divide-and-conquer algorithm described by Guibas and
1213 * Stolfi. Should be called with at least two vertices. */
1214 static BOOL d2d_cdt_triangulate(struct d2d_cdt *cdt, size_t start_vertex, size_t vertex_count,
1215 struct d2d_cdt_edge_ref *left_edge, struct d2d_cdt_edge_ref *right_edge)
1217 struct d2d_cdt_edge_ref left_inner, left_outer, right_inner, right_outer, tmp;
1218 size_t cut;
1220 /* Only two vertices, create a single edge. */
1221 if (vertex_count == 2)
1223 struct d2d_cdt_edge_ref a;
1225 if (!d2d_cdt_create_edge(cdt, &a))
1226 return FALSE;
1227 d2d_cdt_edge_set_origin(cdt, &a, start_vertex);
1228 d2d_cdt_edge_set_destination(cdt, &a, start_vertex + 1);
1230 *left_edge = a;
1231 d2d_cdt_edge_sym(right_edge, &a);
1233 return TRUE;
1236 /* Three vertices, create a triangle. */
1237 if (vertex_count == 3)
1239 struct d2d_cdt_edge_ref a, b, c;
1240 float det;
1242 if (!d2d_cdt_create_edge(cdt, &a))
1243 return FALSE;
1244 if (!d2d_cdt_create_edge(cdt, &b))
1245 return FALSE;
1246 d2d_cdt_edge_sym(&tmp, &a);
1247 d2d_cdt_splice(cdt, &tmp, &b);
1249 d2d_cdt_edge_set_origin(cdt, &a, start_vertex);
1250 d2d_cdt_edge_set_destination(cdt, &a, start_vertex + 1);
1251 d2d_cdt_edge_set_origin(cdt, &b, start_vertex + 1);
1252 d2d_cdt_edge_set_destination(cdt, &b, start_vertex + 2);
1254 det = d2d_cdt_ccw(cdt, start_vertex, start_vertex + 1, start_vertex + 2);
1255 if (det != 0.0f && !d2d_cdt_connect(cdt, &c, &b, &a))
1256 return FALSE;
1258 if (det < 0.0f)
1260 d2d_cdt_edge_sym(left_edge, &c);
1261 *right_edge = c;
1263 else
1265 *left_edge = a;
1266 d2d_cdt_edge_sym(right_edge, &b);
1269 return TRUE;
1272 /* More than tree vertices, divide. */
1273 cut = vertex_count / 2;
1274 if (!d2d_cdt_triangulate(cdt, start_vertex, cut, &left_outer, &left_inner))
1275 return FALSE;
1276 if (!d2d_cdt_triangulate(cdt, start_vertex + cut, vertex_count - cut, &right_inner, &right_outer))
1277 return FALSE;
1278 /* Merge the left and right parts. */
1279 if (!d2d_cdt_merge(cdt, &left_outer, &left_inner, &right_inner, &right_outer))
1280 return FALSE;
1282 *left_edge = left_outer;
1283 *right_edge = right_outer;
1284 return TRUE;
1287 static int __cdecl d2d_cdt_compare_vertices(const void *a, const void *b)
1289 const D2D1_POINT_2F *p0 = a;
1290 const D2D1_POINT_2F *p1 = b;
1291 float diff = p0->x - p1->x;
1293 if (diff == 0.0f)
1294 diff = p0->y - p1->y;
1296 return diff == 0.0f ? 0 : (diff > 0.0f ? 1 : -1);
1299 /* Determine whether a given point is inside the geometry, using the current
1300 * fill mode rule. */
1301 static BOOL d2d_path_geometry_point_inside(const struct d2d_geometry *geometry,
1302 const D2D1_POINT_2F *probe, BOOL triangles_only)
1304 const D2D1_POINT_2F *p0, *p1;
1305 D2D1_POINT_2F v_p, v_probe;
1306 unsigned int score;
1307 size_t i, j, last;
1309 for (i = 0, score = 0; i < geometry->u.path.figure_count; ++i)
1311 const struct d2d_figure *figure = &geometry->u.path.figures[i];
1313 if (probe->x < figure->bounds.left || probe->x > figure->bounds.right
1314 || probe->y < figure->bounds.top || probe->y > figure->bounds.bottom)
1315 continue;
1317 last = figure->vertex_count - 1;
1318 if (!triangles_only)
1320 while (last && figure->vertex_types[last] == D2D_VERTEX_TYPE_NONE)
1321 --last;
1323 p0 = &figure->vertices[last];
1324 for (j = 0; j <= last; ++j)
1326 if (!triangles_only && figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
1327 continue;
1329 p1 = &figure->vertices[j];
1330 d2d_point_subtract(&v_p, p1, p0);
1331 d2d_point_subtract(&v_probe, probe, p0);
1333 if ((probe->y < p0->y) != (probe->y < p1->y) && v_probe.x < v_p.x * (v_probe.y / v_p.y))
1335 if (geometry->u.path.fill_mode == D2D1_FILL_MODE_ALTERNATE || (probe->y < p0->y))
1336 ++score;
1337 else
1338 --score;
1341 p0 = p1;
1345 return geometry->u.path.fill_mode == D2D1_FILL_MODE_ALTERNATE ? score & 1 : score;
1348 static BOOL d2d_path_geometry_add_fill_face(struct d2d_geometry *geometry, const struct d2d_cdt *cdt,
1349 const struct d2d_cdt_edge_ref *base_edge)
1351 struct d2d_cdt_edge_ref tmp;
1352 struct d2d_face *face;
1353 D2D1_POINT_2F probe;
1355 if (cdt->edges[base_edge->idx].flags & D2D_CDT_EDGE_FLAG_VISITED(base_edge->r))
1356 return TRUE;
1358 if (!d2d_array_reserve((void **)&geometry->fill.faces, &geometry->fill.faces_size,
1359 geometry->fill.face_count + 1, sizeof(*geometry->fill.faces)))
1361 ERR("Failed to grow faces array.\n");
1362 return FALSE;
1365 face = &geometry->fill.faces[geometry->fill.face_count];
1367 /* It may seem tempting to use the center of the face as probe origin, but
1368 * multiplying by powers of two works much better for preserving accuracy. */
1370 tmp = *base_edge;
1371 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1372 face->v[0] = d2d_cdt_edge_origin(cdt, &tmp);
1373 probe.x = cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.25f;
1374 probe.y = cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.25f;
1376 d2d_cdt_edge_next_left(cdt, &tmp, &tmp);
1377 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1378 face->v[1] = d2d_cdt_edge_origin(cdt, &tmp);
1379 probe.x += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.25f;
1380 probe.y += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.25f;
1382 d2d_cdt_edge_next_left(cdt, &tmp, &tmp);
1383 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1384 face->v[2] = d2d_cdt_edge_origin(cdt, &tmp);
1385 probe.x += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.50f;
1386 probe.y += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.50f;
1388 if (d2d_cdt_leftof(cdt, face->v[2], base_edge) && d2d_path_geometry_point_inside(geometry, &probe, TRUE))
1389 ++geometry->fill.face_count;
1391 return TRUE;
1394 static BOOL d2d_cdt_generate_faces(const struct d2d_cdt *cdt, struct d2d_geometry *geometry)
1396 struct d2d_cdt_edge_ref base_edge;
1397 size_t i;
1399 for (i = 0; i < cdt->edge_count; ++i)
1401 if (cdt->edges[i].flags & D2D_CDT_EDGE_FLAG_FREED)
1402 continue;
1404 base_edge.idx = i;
1405 base_edge.r = 0;
1406 if (!d2d_path_geometry_add_fill_face(geometry, cdt, &base_edge))
1407 goto fail;
1408 d2d_cdt_edge_sym(&base_edge, &base_edge);
1409 if (!d2d_path_geometry_add_fill_face(geometry, cdt, &base_edge))
1410 goto fail;
1413 return TRUE;
1415 fail:
1416 heap_free(geometry->fill.faces);
1417 geometry->fill.faces = NULL;
1418 geometry->fill.faces_size = 0;
1419 geometry->fill.face_count = 0;
1420 return FALSE;
1423 static BOOL d2d_cdt_fixup(struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *base_edge)
1425 struct d2d_cdt_edge_ref candidate, next, new_base;
1426 unsigned int count = 0;
1428 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1429 if (next.idx == base_edge->idx)
1431 ERR("Degenerate face.\n");
1432 return FALSE;
1435 candidate = next;
1436 while (d2d_cdt_edge_destination(cdt, &next) != d2d_cdt_edge_origin(cdt, base_edge))
1438 if (d2d_cdt_incircle(cdt, d2d_cdt_edge_origin(cdt, base_edge), d2d_cdt_edge_destination(cdt, base_edge),
1439 d2d_cdt_edge_destination(cdt, &candidate), d2d_cdt_edge_destination(cdt, &next)))
1440 candidate = next;
1441 d2d_cdt_edge_next_left(cdt, &next, &next);
1442 ++count;
1445 if (count > 1)
1447 d2d_cdt_edge_next_left(cdt, &next, &candidate);
1448 if (d2d_cdt_edge_destination(cdt, &next) == d2d_cdt_edge_origin(cdt, base_edge))
1449 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1450 else
1451 next = *base_edge;
1452 if (!d2d_cdt_connect(cdt, &new_base, &candidate, &next))
1453 return FALSE;
1454 if (!d2d_cdt_fixup(cdt, &new_base))
1455 return FALSE;
1456 d2d_cdt_edge_sym(&new_base, &new_base);
1457 if (!d2d_cdt_fixup(cdt, &new_base))
1458 return FALSE;
1461 return TRUE;
1464 static void d2d_cdt_cut_edges(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *end_edge,
1465 const struct d2d_cdt_edge_ref *base_edge, size_t start_vertex, size_t end_vertex)
1467 struct d2d_cdt_edge_ref next;
1468 float ccw;
1470 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1471 if (d2d_cdt_edge_destination(cdt, &next) == end_vertex)
1473 *end_edge = next;
1474 return;
1477 ccw = d2d_cdt_ccw(cdt, d2d_cdt_edge_destination(cdt, &next), end_vertex, start_vertex);
1478 if (ccw == 0.0f)
1480 *end_edge = next;
1481 return;
1484 if (ccw > 0.0f)
1485 d2d_cdt_edge_next_left(cdt, &next, &next);
1487 d2d_cdt_edge_sym(&next, &next);
1488 d2d_cdt_cut_edges(cdt, end_edge, &next, start_vertex, end_vertex);
1489 d2d_cdt_destroy_edge(cdt, &next);
1492 static BOOL d2d_cdt_insert_segment(struct d2d_cdt *cdt, struct d2d_geometry *geometry,
1493 const struct d2d_cdt_edge_ref *origin, struct d2d_cdt_edge_ref *edge, size_t end_vertex)
1495 struct d2d_cdt_edge_ref base_edge, current, new_origin, next, target;
1496 size_t current_destination, current_origin;
1498 for (current = *origin;; current = next)
1500 d2d_cdt_edge_next_origin(cdt, &next, &current);
1502 current_destination = d2d_cdt_edge_destination(cdt, &current);
1503 if (current_destination == end_vertex)
1505 d2d_cdt_edge_sym(edge, &current);
1506 return TRUE;
1509 current_origin = d2d_cdt_edge_origin(cdt, &current);
1510 if (d2d_cdt_ccw(cdt, end_vertex, current_origin, current_destination) == 0.0f
1511 && (cdt->vertices[current_destination].x > cdt->vertices[current_origin].x)
1512 == (cdt->vertices[end_vertex].x > cdt->vertices[current_origin].x)
1513 && (cdt->vertices[current_destination].y > cdt->vertices[current_origin].y)
1514 == (cdt->vertices[end_vertex].y > cdt->vertices[current_origin].y))
1516 d2d_cdt_edge_sym(&new_origin, &current);
1517 return d2d_cdt_insert_segment(cdt, geometry, &new_origin, edge, end_vertex);
1520 if (d2d_cdt_rightof(cdt, end_vertex, &next) && d2d_cdt_leftof(cdt, end_vertex, &current))
1522 d2d_cdt_edge_next_left(cdt, &base_edge, &current);
1524 d2d_cdt_edge_sym(&base_edge, &base_edge);
1525 d2d_cdt_cut_edges(cdt, &target, &base_edge, d2d_cdt_edge_origin(cdt, origin), end_vertex);
1526 d2d_cdt_destroy_edge(cdt, &base_edge);
1528 if (!d2d_cdt_connect(cdt, &base_edge, &target, &current))
1529 return FALSE;
1530 *edge = base_edge;
1531 if (!d2d_cdt_fixup(cdt, &base_edge))
1532 return FALSE;
1533 d2d_cdt_edge_sym(&base_edge, &base_edge);
1534 if (!d2d_cdt_fixup(cdt, &base_edge))
1535 return FALSE;
1537 if (d2d_cdt_edge_origin(cdt, edge) == end_vertex)
1538 return TRUE;
1539 new_origin = *edge;
1540 return d2d_cdt_insert_segment(cdt, geometry, &new_origin, edge, end_vertex);
1543 if (next.idx == origin->idx)
1545 ERR("Triangle not found.\n");
1546 return FALSE;
1551 static BOOL d2d_cdt_insert_segments(struct d2d_cdt *cdt, struct d2d_geometry *geometry)
1553 size_t start_vertex, end_vertex, i, j, k;
1554 struct d2d_cdt_edge_ref edge, new_edge;
1555 const struct d2d_figure *figure;
1556 const D2D1_POINT_2F *p;
1557 BOOL found;
1559 for (i = 0; i < geometry->u.path.figure_count; ++i)
1561 figure = &geometry->u.path.figures[i];
1563 if (figure->flags & D2D_FIGURE_FLAG_HOLLOW)
1564 continue;
1566 /* Degenerate figure. */
1567 if (figure->vertex_count < 2)
1568 continue;
1570 p = bsearch(&figure->vertices[figure->vertex_count - 1], cdt->vertices,
1571 geometry->fill.vertex_count, sizeof(*p), d2d_cdt_compare_vertices);
1572 start_vertex = p - cdt->vertices;
1574 for (k = 0, found = FALSE; k < cdt->edge_count; ++k)
1576 if (cdt->edges[k].flags & D2D_CDT_EDGE_FLAG_FREED)
1577 continue;
1579 edge.idx = k;
1580 edge.r = 0;
1582 if (d2d_cdt_edge_origin(cdt, &edge) == start_vertex)
1584 found = TRUE;
1585 break;
1587 d2d_cdt_edge_sym(&edge, &edge);
1588 if (d2d_cdt_edge_origin(cdt, &edge) == start_vertex)
1590 found = TRUE;
1591 break;
1595 if (!found)
1597 ERR("Edge not found.\n");
1598 return FALSE;
1601 for (j = 0; j < figure->vertex_count; start_vertex = end_vertex, ++j)
1603 p = bsearch(&figure->vertices[j], cdt->vertices,
1604 geometry->fill.vertex_count, sizeof(*p), d2d_cdt_compare_vertices);
1605 end_vertex = p - cdt->vertices;
1607 if (start_vertex == end_vertex)
1608 continue;
1610 if (!d2d_cdt_insert_segment(cdt, geometry, &edge, &new_edge, end_vertex))
1611 return FALSE;
1612 edge = new_edge;
1616 return TRUE;
1619 static BOOL d2d_geometry_intersections_add(struct d2d_geometry_intersections *i,
1620 const struct d2d_segment_idx *segment_idx, float t, D2D1_POINT_2F p)
1622 struct d2d_geometry_intersection *intersection;
1624 if (!d2d_array_reserve((void **)&i->intersections, &i->intersections_size,
1625 i->intersection_count + 1, sizeof(*i->intersections)))
1627 ERR("Failed to grow intersections array.\n");
1628 return FALSE;
1631 intersection = &i->intersections[i->intersection_count++];
1632 intersection->figure_idx = segment_idx->figure_idx;
1633 intersection->vertex_idx = segment_idx->vertex_idx;
1634 intersection->control_idx = segment_idx->control_idx;
1635 intersection->t = t;
1636 intersection->p = p;
1638 return TRUE;
1641 static int __cdecl d2d_geometry_intersections_compare(const void *a, const void *b)
1643 const struct d2d_geometry_intersection *i0 = a;
1644 const struct d2d_geometry_intersection *i1 = b;
1646 if (i0->figure_idx != i1->figure_idx)
1647 return i0->figure_idx - i1->figure_idx;
1648 if (i0->vertex_idx != i1->vertex_idx)
1649 return i0->vertex_idx - i1->vertex_idx;
1650 if (i0->t != i1->t)
1651 return i0->t > i1->t ? 1 : -1;
1652 return 0;
1655 static BOOL d2d_geometry_intersect_line_line(struct d2d_geometry *geometry,
1656 struct d2d_geometry_intersections *intersections, const struct d2d_segment_idx *idx_p,
1657 const struct d2d_segment_idx *idx_q)
1659 D2D1_POINT_2F v_p, v_q, v_qp, intersection;
1660 const D2D1_POINT_2F *p[2], *q[2];
1661 const struct d2d_figure *figure;
1662 float s, t, det;
1663 size_t next;
1665 figure = &geometry->u.path.figures[idx_p->figure_idx];
1666 p[0] = &figure->vertices[idx_p->vertex_idx];
1667 next = idx_p->vertex_idx + 1;
1668 if (next == figure->vertex_count)
1669 next = 0;
1670 p[1] = &figure->vertices[next];
1672 figure = &geometry->u.path.figures[idx_q->figure_idx];
1673 q[0] = &figure->vertices[idx_q->vertex_idx];
1674 next = idx_q->vertex_idx + 1;
1675 if (next == figure->vertex_count)
1676 next = 0;
1677 q[1] = &figure->vertices[next];
1679 d2d_point_subtract(&v_p, p[1], p[0]);
1680 d2d_point_subtract(&v_q, q[1], q[0]);
1681 d2d_point_subtract(&v_qp, p[0], q[0]);
1683 det = v_p.x * v_q.y - v_p.y * v_q.x;
1684 if (det == 0.0f)
1685 return TRUE;
1687 s = (v_q.x * v_qp.y - v_q.y * v_qp.x) / det;
1688 t = (v_p.x * v_qp.y - v_p.y * v_qp.x) / det;
1690 if (s < 0.0f || s > 1.0f || t < 0.0f || t > 1.0f)
1691 return TRUE;
1693 intersection.x = p[0]->x + v_p.x * s;
1694 intersection.y = p[0]->y + v_p.y * s;
1696 if (s > 0.0f && s < 1.0f && !d2d_geometry_intersections_add(intersections, idx_p, s, intersection))
1697 return FALSE;
1699 if (t > 0.0f && t < 1.0f && !d2d_geometry_intersections_add(intersections, idx_q, t, intersection))
1700 return FALSE;
1702 return TRUE;
1705 static BOOL d2d_geometry_add_bezier_line_intersections(struct d2d_geometry *geometry,
1706 struct d2d_geometry_intersections *intersections, const struct d2d_segment_idx *idx_p,
1707 const D2D1_POINT_2F **p, const struct d2d_segment_idx *idx_q, const D2D1_POINT_2F **q, float s)
1709 D2D1_POINT_2F intersection;
1710 float t;
1712 d2d_point_calculate_bezier(&intersection, p[0], p[1], p[2], s);
1713 if (fabsf(q[1]->x - q[0]->x) > fabsf(q[1]->y - q[0]->y))
1714 t = (intersection.x - q[0]->x) / (q[1]->x - q[0]->x);
1715 else
1716 t = (intersection.y - q[0]->y) / (q[1]->y - q[0]->y);
1717 if (t < 0.0f || t > 1.0f)
1718 return TRUE;
1720 d2d_point_lerp(&intersection, q[0], q[1], t);
1722 if (s > 0.0f && s < 1.0f && !d2d_geometry_intersections_add(intersections, idx_p, s, intersection))
1723 return FALSE;
1725 if (t > 0.0f && t < 1.0f && !d2d_geometry_intersections_add(intersections, idx_q, t, intersection))
1726 return FALSE;
1728 return TRUE;
1731 static BOOL d2d_geometry_intersect_bezier_line(struct d2d_geometry *geometry,
1732 struct d2d_geometry_intersections *intersections,
1733 const struct d2d_segment_idx *idx_p, const struct d2d_segment_idx *idx_q)
1735 const D2D1_POINT_2F *p[3], *q[2];
1736 const struct d2d_figure *figure;
1737 float y[3], root, theta, d, e;
1738 size_t next;
1740 figure = &geometry->u.path.figures[idx_p->figure_idx];
1741 p[0] = &figure->vertices[idx_p->vertex_idx];
1742 p[1] = &figure->bezier_controls[idx_p->control_idx];
1743 next = idx_p->vertex_idx + 1;
1744 if (next == figure->vertex_count)
1745 next = 0;
1746 p[2] = &figure->vertices[next];
1748 figure = &geometry->u.path.figures[idx_q->figure_idx];
1749 q[0] = &figure->vertices[idx_q->vertex_idx];
1750 next = idx_q->vertex_idx + 1;
1751 if (next == figure->vertex_count)
1752 next = 0;
1753 q[1] = &figure->vertices[next];
1755 /* Align the line with x-axis. */
1756 theta = -atan2f(q[1]->y - q[0]->y, q[1]->x - q[0]->x);
1757 y[0] = (p[0]->x - q[0]->x) * sinf(theta) + (p[0]->y - q[0]->y) * cosf(theta);
1758 y[1] = (p[1]->x - q[0]->x) * sinf(theta) + (p[1]->y - q[0]->y) * cosf(theta);
1759 y[2] = (p[2]->x - q[0]->x) * sinf(theta) + (p[2]->y - q[0]->y) * cosf(theta);
1761 /* Intersect the transformed curve with the x-axis.
1763 * f(t) = (1 - t)²P₀ + 2(1 - t)tP₁ + t²P₂
1764 * = (P₀ - 2P₁ + P₂)t² + 2(P₁ - P₀)t + P₀
1766 * a = P₀ - 2P₁ + P₂
1767 * b = 2(P₁ - P₀)
1768 * c = P₀
1770 * f(t) = 0
1771 * t = (-b ± √(b² - 4ac)) / 2a
1772 * = (-2(P₁ - P₀) ± √((2(P₁ - P₀))² - 4((P₀ - 2P₁ + P₂)P₀))) / 2(P₀ - 2P₁ + P₂)
1773 * = (2P₀ - 2P₁ ± √(4P₀² + 4P₁² - 8P₀P₁ - 4P₀² + 8P₀P₁ - 4P₀P₂)) / (2P₀ - 4P₁ + 2P₂)
1774 * = (P₀ - P₁ ± √(P₁² - P₀P₂)) / (P₀ - 2P₁ + P₂) */
1776 d = y[0] - 2 * y[1] + y[2];
1777 if (d == 0.0f)
1779 /* P₀ - 2P₁ + P₂ = 0
1780 * f(t) = (P₀ - 2P₁ + P₂)t² + 2(P₁ - P₀)t + P₀ = 0
1781 * t = -P₀ / 2(P₁ - P₀) */
1782 root = -y[0] / (2.0f * (y[1] - y[0]));
1783 if (root < 0.0f || root > 1.0f)
1784 return TRUE;
1786 return d2d_geometry_add_bezier_line_intersections(geometry, intersections, idx_p, p, idx_q, q, root);
1789 e = y[1] * y[1] - y[0] * y[2];
1790 if (e < 0.0f)
1791 return TRUE;
1793 root = (y[0] - y[1] + sqrtf(e)) / d;
1794 if (root >= 0.0f && root <= 1.0f && !d2d_geometry_add_bezier_line_intersections(geometry,
1795 intersections, idx_p, p, idx_q, q, root))
1796 return FALSE;
1798 root = (y[0] - y[1] - sqrtf(e)) / d;
1799 if (root >= 0.0f && root <= 1.0f && !d2d_geometry_add_bezier_line_intersections(geometry,
1800 intersections, idx_p, p, idx_q, q, root))
1801 return FALSE;
1803 return TRUE;
1806 static BOOL d2d_geometry_intersect_bezier_bezier(struct d2d_geometry *geometry,
1807 struct d2d_geometry_intersections *intersections,
1808 const struct d2d_segment_idx *idx_p, float start_p, float end_p,
1809 const struct d2d_segment_idx *idx_q, float start_q, float end_q)
1811 const D2D1_POINT_2F *p[3], *q[3];
1812 const struct d2d_figure *figure;
1813 D2D_RECT_F p_bounds, q_bounds;
1814 D2D1_POINT_2F intersection;
1815 float centre_p, centre_q;
1816 size_t next;
1818 figure = &geometry->u.path.figures[idx_p->figure_idx];
1819 p[0] = &figure->vertices[idx_p->vertex_idx];
1820 p[1] = &figure->bezier_controls[idx_p->control_idx];
1821 next = idx_p->vertex_idx + 1;
1822 if (next == figure->vertex_count)
1823 next = 0;
1824 p[2] = &figure->vertices[next];
1826 figure = &geometry->u.path.figures[idx_q->figure_idx];
1827 q[0] = &figure->vertices[idx_q->vertex_idx];
1828 q[1] = &figure->bezier_controls[idx_q->control_idx];
1829 next = idx_q->vertex_idx + 1;
1830 if (next == figure->vertex_count)
1831 next = 0;
1832 q[2] = &figure->vertices[next];
1834 d2d_rect_get_bezier_segment_bounds(&p_bounds, p[0], p[1], p[2], start_p, end_p);
1835 d2d_rect_get_bezier_segment_bounds(&q_bounds, q[0], q[1], q[2], start_q, end_q);
1837 if (!d2d_rect_check_overlap(&p_bounds, &q_bounds))
1838 return TRUE;
1840 centre_p = (start_p + end_p) / 2.0f;
1841 centre_q = (start_q + end_q) / 2.0f;
1843 if (end_p - start_p < 1e-3f)
1845 d2d_point_calculate_bezier(&intersection, p[0], p[1], p[2], centre_p);
1846 if (start_p > 0.0f && end_p < 1.0f && !d2d_geometry_intersections_add(intersections,
1847 idx_p, centre_p, intersection))
1848 return FALSE;
1849 if (start_q > 0.0f && end_q < 1.0f && !d2d_geometry_intersections_add(intersections,
1850 idx_q, centre_q, intersection))
1851 return FALSE;
1852 return TRUE;
1855 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1856 idx_p, start_p, centre_p, idx_q, start_q, centre_q))
1857 return FALSE;
1858 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1859 idx_p, start_p, centre_p, idx_q, centre_q, end_q))
1860 return FALSE;
1861 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1862 idx_p, centre_p, end_p, idx_q, start_q, centre_q))
1863 return FALSE;
1864 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1865 idx_p, centre_p, end_p, idx_q, centre_q, end_q))
1866 return FALSE;
1868 return TRUE;
1871 static BOOL d2d_geometry_apply_intersections(struct d2d_geometry *geometry,
1872 struct d2d_geometry_intersections *intersections)
1874 size_t vertex_offset, control_offset, next, i;
1875 struct d2d_geometry_intersection *inter;
1876 enum d2d_vertex_type vertex_type;
1877 const D2D1_POINT_2F *p[3];
1878 struct d2d_figure *figure;
1879 D2D1_POINT_2F q[2];
1880 float t, t_prev;
1882 for (i = 0; i < intersections->intersection_count; ++i)
1884 inter = &intersections->intersections[i];
1885 if (!i || inter->figure_idx != intersections->intersections[i - 1].figure_idx)
1886 vertex_offset = control_offset = 0;
1888 figure = &geometry->u.path.figures[inter->figure_idx];
1889 vertex_type = figure->vertex_types[inter->vertex_idx + vertex_offset];
1890 if (!d2d_vertex_type_is_bezier(vertex_type))
1892 if (!d2d_figure_insert_vertex(&geometry->u.path.figures[inter->figure_idx],
1893 inter->vertex_idx + vertex_offset + 1, inter->p))
1894 return FALSE;
1895 ++vertex_offset;
1896 continue;
1899 t = inter->t;
1900 if (i && inter->figure_idx == intersections->intersections[i - 1].figure_idx
1901 && inter->vertex_idx == intersections->intersections[i - 1].vertex_idx)
1903 t_prev = intersections->intersections[i - 1].t;
1904 if (t - t_prev < 1e-3f)
1906 inter->t = intersections->intersections[i - 1].t;
1907 continue;
1909 t = (t - t_prev) / (1.0f - t_prev);
1912 p[0] = &figure->vertices[inter->vertex_idx + vertex_offset];
1913 p[1] = &figure->bezier_controls[inter->control_idx + control_offset];
1914 next = inter->vertex_idx + vertex_offset + 1;
1915 if (next == figure->vertex_count)
1916 next = 0;
1917 p[2] = &figure->vertices[next];
1919 d2d_point_lerp(&q[0], p[0], p[1], t);
1920 d2d_point_lerp(&q[1], p[1], p[2], t);
1922 figure->bezier_controls[inter->control_idx + control_offset] = q[0];
1923 if (!(d2d_figure_insert_bezier_controls(figure, inter->control_idx + control_offset + 1, 1, &q[1])))
1924 return FALSE;
1925 ++control_offset;
1927 if (!(d2d_figure_insert_vertex(figure, inter->vertex_idx + vertex_offset + 1, inter->p)))
1928 return FALSE;
1929 figure->vertex_types[inter->vertex_idx + vertex_offset + 1] = D2D_VERTEX_TYPE_SPLIT_BEZIER;
1930 ++vertex_offset;
1933 return TRUE;
1936 /* Intersect the geometry's segments with themselves. This uses the
1937 * straightforward approach of testing everything against everything, but
1938 * there certainly exist more scalable algorithms for this. */
1939 static BOOL d2d_geometry_intersect_self(struct d2d_geometry *geometry)
1941 struct d2d_geometry_intersections intersections = {0};
1942 const struct d2d_figure *figure_p, *figure_q;
1943 struct d2d_segment_idx idx_p, idx_q;
1944 enum d2d_vertex_type type_p, type_q;
1945 BOOL ret = FALSE;
1946 size_t max_q;
1948 if (!geometry->u.path.figure_count)
1949 return TRUE;
1951 for (idx_p.figure_idx = 0; idx_p.figure_idx < geometry->u.path.figure_count; ++idx_p.figure_idx)
1953 figure_p = &geometry->u.path.figures[idx_p.figure_idx];
1954 idx_p.control_idx = 0;
1955 for (idx_p.vertex_idx = 0; idx_p.vertex_idx < figure_p->vertex_count; ++idx_p.vertex_idx)
1957 type_p = figure_p->vertex_types[idx_p.vertex_idx];
1958 for (idx_q.figure_idx = 0; idx_q.figure_idx <= idx_p.figure_idx; ++idx_q.figure_idx)
1960 figure_q = &geometry->u.path.figures[idx_q.figure_idx];
1961 if (idx_q.figure_idx != idx_p.figure_idx)
1963 if (!d2d_rect_check_overlap(&figure_p->bounds, &figure_q->bounds))
1964 continue;
1965 max_q = figure_q->vertex_count;
1967 else
1969 max_q = idx_p.vertex_idx;
1972 idx_q.control_idx = 0;
1973 for (idx_q.vertex_idx = 0; idx_q.vertex_idx < max_q; ++idx_q.vertex_idx)
1975 type_q = figure_q->vertex_types[idx_q.vertex_idx];
1976 if (d2d_vertex_type_is_bezier(type_q))
1978 if (d2d_vertex_type_is_bezier(type_p))
1980 if (!d2d_geometry_intersect_bezier_bezier(geometry, &intersections,
1981 &idx_p, 0.0f, 1.0f, &idx_q, 0.0f, 1.0f))
1982 goto done;
1984 else
1986 if (!d2d_geometry_intersect_bezier_line(geometry, &intersections, &idx_q, &idx_p))
1987 goto done;
1989 ++idx_q.control_idx;
1991 else
1993 if (d2d_vertex_type_is_bezier(type_p))
1995 if (!d2d_geometry_intersect_bezier_line(geometry, &intersections, &idx_p, &idx_q))
1996 goto done;
1998 else
2000 if (!d2d_geometry_intersect_line_line(geometry, &intersections, &idx_p, &idx_q))
2001 goto done;
2006 if (d2d_vertex_type_is_bezier(type_p))
2007 ++idx_p.control_idx;
2011 qsort(intersections.intersections, intersections.intersection_count,
2012 sizeof(*intersections.intersections), d2d_geometry_intersections_compare);
2013 ret = d2d_geometry_apply_intersections(geometry, &intersections);
2015 done:
2016 heap_free(intersections.intersections);
2017 return ret;
2020 static HRESULT d2d_path_geometry_triangulate(struct d2d_geometry *geometry)
2022 struct d2d_cdt_edge_ref left_edge, right_edge;
2023 size_t vertex_count, i, j;
2024 struct d2d_cdt cdt = {0};
2025 D2D1_POINT_2F *vertices;
2027 for (i = 0, vertex_count = 0; i < geometry->u.path.figure_count; ++i)
2029 if (geometry->u.path.figures[i].flags & D2D_FIGURE_FLAG_HOLLOW)
2030 continue;
2031 vertex_count += geometry->u.path.figures[i].vertex_count;
2034 if (vertex_count < 3)
2036 WARN("Geometry has %lu vertices.\n", (long)vertex_count);
2037 return S_OK;
2040 if (!(vertices = heap_calloc(vertex_count, sizeof(*vertices))))
2041 return E_OUTOFMEMORY;
2043 for (i = 0, j = 0; i < geometry->u.path.figure_count; ++i)
2045 if (geometry->u.path.figures[i].flags & D2D_FIGURE_FLAG_HOLLOW)
2046 continue;
2047 memcpy(&vertices[j], geometry->u.path.figures[i].vertices,
2048 geometry->u.path.figures[i].vertex_count * sizeof(*vertices));
2049 j += geometry->u.path.figures[i].vertex_count;
2052 /* Sort vertices, eliminate duplicates. */
2053 qsort(vertices, vertex_count, sizeof(*vertices), d2d_cdt_compare_vertices);
2054 for (i = 1; i < vertex_count; ++i)
2056 if (!memcmp(&vertices[i - 1], &vertices[i], sizeof(*vertices)))
2058 --vertex_count;
2059 memmove(&vertices[i], &vertices[i + 1], (vertex_count - i) * sizeof(*vertices));
2060 --i;
2064 geometry->fill.vertices = vertices;
2065 geometry->fill.vertex_count = vertex_count;
2067 cdt.free_edge = ~0u;
2068 cdt.vertices = vertices;
2069 if (!d2d_cdt_triangulate(&cdt, 0, vertex_count, &left_edge, &right_edge))
2070 goto fail;
2071 if (!d2d_cdt_insert_segments(&cdt, geometry))
2072 goto fail;
2073 if (!d2d_cdt_generate_faces(&cdt, geometry))
2074 goto fail;
2076 heap_free(cdt.edges);
2077 return S_OK;
2079 fail:
2080 geometry->fill.vertices = NULL;
2081 geometry->fill.vertex_count = 0;
2082 heap_free(vertices);
2083 heap_free(cdt.edges);
2084 return E_FAIL;
2087 static BOOL d2d_path_geometry_add_figure(struct d2d_geometry *geometry)
2089 struct d2d_figure *figure;
2091 if (!d2d_array_reserve((void **)&geometry->u.path.figures, &geometry->u.path.figures_size,
2092 geometry->u.path.figure_count + 1, sizeof(*geometry->u.path.figures)))
2094 ERR("Failed to grow figures array.\n");
2095 return FALSE;
2098 figure = &geometry->u.path.figures[geometry->u.path.figure_count];
2099 memset(figure, 0, sizeof(*figure));
2100 figure->bounds.left = FLT_MAX;
2101 figure->bounds.top = FLT_MAX;
2102 figure->bounds.right = -FLT_MAX;
2103 figure->bounds.bottom = -FLT_MAX;
2105 ++geometry->u.path.figure_count;
2106 return TRUE;
2109 static BOOL d2d_geometry_outline_add_join(struct d2d_geometry *geometry,
2110 const D2D1_POINT_2F *prev, const D2D1_POINT_2F *p0, const D2D1_POINT_2F *next)
2112 static const D2D1_POINT_2F origin = {0.0f, 0.0f};
2113 struct d2d_outline_vertex *v;
2114 struct d2d_face *f;
2115 size_t base_idx;
2116 float ccw;
2118 if (!d2d_array_reserve((void **)&geometry->outline.vertices, &geometry->outline.vertices_size,
2119 geometry->outline.vertex_count + 4, sizeof(*geometry->outline.vertices)))
2121 ERR("Failed to grow outline vertices array.\n");
2122 return FALSE;
2124 base_idx = geometry->outline.vertex_count;
2125 v = &geometry->outline.vertices[base_idx];
2127 if (!d2d_array_reserve((void **)&geometry->outline.faces, &geometry->outline.faces_size,
2128 geometry->outline.face_count + 2, sizeof(*geometry->outline.faces)))
2130 ERR("Failed to grow outline faces array.\n");
2131 return FALSE;
2133 f = &geometry->outline.faces[geometry->outline.face_count];
2135 ccw = d2d_point_ccw(&origin, prev, next);
2136 if (ccw == 0.0f)
2138 d2d_outline_vertex_set(&v[0], p0->x, p0->y, -prev->x, -prev->y, -prev->x, -prev->y);
2139 d2d_outline_vertex_set(&v[1], p0->x, p0->y, prev->x, prev->y, prev->x, prev->y);
2140 d2d_outline_vertex_set(&v[2], p0->x + 25.0f * -prev->x, p0->y + 25.0f * -prev->y,
2141 prev->x, prev->y, prev->x, prev->y);
2142 d2d_outline_vertex_set(&v[3], p0->x + 25.0f * -prev->x, p0->y + 25.0f * -prev->y,
2143 -prev->x, -prev->y, -prev->x, -prev->y);
2145 else if (ccw < 0.0f)
2147 d2d_outline_vertex_set(&v[0], p0->x, p0->y, next->x, next->y, -prev->x, -prev->y);
2148 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -next->x, -next->y, -next->x, -next->y);
2149 d2d_outline_vertex_set(&v[2], p0->x, p0->y, -next->x, -next->y, prev->x, prev->y);
2150 d2d_outline_vertex_set(&v[3], p0->x, p0->y, prev->x, prev->y, prev->x, prev->y);
2152 else
2154 d2d_outline_vertex_set(&v[0], p0->x, p0->y, prev->x, prev->y, -next->x, -next->y);
2155 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -prev->x, -prev->y, -prev->x, -prev->y);
2156 d2d_outline_vertex_set(&v[2], p0->x, p0->y, -prev->x, -prev->y, next->x, next->y);
2157 d2d_outline_vertex_set(&v[3], p0->x, p0->y, next->x, next->y, next->x, next->y);
2159 geometry->outline.vertex_count += 4;
2161 d2d_face_set(&f[0], base_idx + 1, base_idx + 0, base_idx + 2);
2162 d2d_face_set(&f[1], base_idx + 2, base_idx + 0, base_idx + 3);
2163 geometry->outline.face_count += 2;
2165 return TRUE;
2168 static BOOL d2d_geometry_outline_add_line_segment(struct d2d_geometry *geometry,
2169 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *next)
2171 struct d2d_outline_vertex *v;
2172 D2D1_POINT_2F q_next;
2173 struct d2d_face *f;
2174 size_t base_idx;
2176 if (!d2d_array_reserve((void **)&geometry->outline.vertices, &geometry->outline.vertices_size,
2177 geometry->outline.vertex_count + 4, sizeof(*geometry->outline.vertices)))
2179 ERR("Failed to grow outline vertices array.\n");
2180 return FALSE;
2182 base_idx = geometry->outline.vertex_count;
2183 v = &geometry->outline.vertices[base_idx];
2185 if (!d2d_array_reserve((void **)&geometry->outline.faces, &geometry->outline.faces_size,
2186 geometry->outline.face_count + 2, sizeof(*geometry->outline.faces)))
2188 ERR("Failed to grow outline faces array.\n");
2189 return FALSE;
2191 f = &geometry->outline.faces[geometry->outline.face_count];
2193 d2d_point_subtract(&q_next, next, p0);
2194 d2d_point_normalise(&q_next);
2196 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_next.x, q_next.y, q_next.x, q_next.y);
2197 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2198 d2d_outline_vertex_set(&v[2], next->x, next->y, q_next.x, q_next.y, q_next.x, q_next.y);
2199 d2d_outline_vertex_set(&v[3], next->x, next->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2200 geometry->outline.vertex_count += 4;
2202 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2203 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2204 geometry->outline.face_count += 2;
2206 return TRUE;
2209 static BOOL d2d_geometry_outline_add_bezier_segment(struct d2d_geometry *geometry,
2210 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
2212 struct d2d_curve_outline_vertex *b;
2213 D2D1_POINT_2F r0, r1, r2;
2214 D2D1_POINT_2F q0, q1, q2;
2215 struct d2d_face *f;
2216 size_t base_idx;
2218 if (!d2d_array_reserve((void **)&geometry->outline.beziers, &geometry->outline.beziers_size,
2219 geometry->outline.bezier_count + 7, sizeof(*geometry->outline.beziers)))
2221 ERR("Failed to grow outline beziers array.\n");
2222 return FALSE;
2224 base_idx = geometry->outline.bezier_count;
2225 b = &geometry->outline.beziers[base_idx];
2227 if (!d2d_array_reserve((void **)&geometry->outline.bezier_faces, &geometry->outline.bezier_faces_size,
2228 geometry->outline.bezier_face_count + 5, sizeof(*geometry->outline.bezier_faces)))
2230 ERR("Failed to grow outline faces array.\n");
2231 return FALSE;
2233 f = &geometry->outline.bezier_faces[geometry->outline.bezier_face_count];
2235 d2d_point_lerp(&q0, p0, p1, 0.5f);
2236 d2d_point_lerp(&q1, p1, p2, 0.5f);
2237 d2d_point_lerp(&q2, &q0, &q1, 0.5f);
2239 d2d_point_subtract(&r0, &q0, p0);
2240 d2d_point_subtract(&r1, &q1, &q0);
2241 d2d_point_subtract(&r2, p2, &q1);
2243 d2d_point_normalise(&r0);
2244 d2d_point_normalise(&r1);
2245 d2d_point_normalise(&r2);
2247 if (d2d_point_ccw(p0, p1, p2) > 0.0f)
2249 d2d_point_scale(&r0, -1.0f);
2250 d2d_point_scale(&r1, -1.0f);
2251 d2d_point_scale(&r2, -1.0f);
2254 d2d_curve_outline_vertex_set(&b[0], p0, p0, p1, p2, r0.x, r0.y, r0.x, r0.y);
2255 d2d_curve_outline_vertex_set(&b[1], p0, p0, p1, p2, -r0.x, -r0.y, -r0.x, -r0.y);
2256 d2d_curve_outline_vertex_set(&b[2], &q0, p0, p1, p2, r0.x, r0.y, r1.x, r1.y);
2257 d2d_curve_outline_vertex_set(&b[3], &q2, p0, p1, p2, -r1.x, -r1.y, -r1.x, -r1.y);
2258 d2d_curve_outline_vertex_set(&b[4], &q1, p0, p1, p2, r1.x, r1.y, r2.x, r2.y);
2259 d2d_curve_outline_vertex_set(&b[5], p2, p0, p1, p2, -r2.x, -r2.y, -r2.x, -r2.y);
2260 d2d_curve_outline_vertex_set(&b[6], p2, p0, p1, p2, r2.x, r2.y, r2.x, r2.y);
2261 geometry->outline.bezier_count += 7;
2263 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2264 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2265 d2d_face_set(&f[2], base_idx + 3, base_idx + 4, base_idx + 2);
2266 d2d_face_set(&f[3], base_idx + 5, base_idx + 4, base_idx + 3);
2267 d2d_face_set(&f[4], base_idx + 5, base_idx + 6, base_idx + 4);
2268 geometry->outline.bezier_face_count += 5;
2270 return TRUE;
2273 static BOOL d2d_geometry_outline_add_arc_quadrant(struct d2d_geometry *geometry,
2274 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
2276 struct d2d_curve_outline_vertex *a;
2277 D2D1_POINT_2F r0, r1;
2278 struct d2d_face *f;
2279 size_t base_idx;
2281 if (!d2d_array_reserve((void **)&geometry->outline.arcs, &geometry->outline.arcs_size,
2282 geometry->outline.arc_count + 5, sizeof(*geometry->outline.arcs)))
2284 ERR("Failed to grow outline arcs array.\n");
2285 return FALSE;
2287 base_idx = geometry->outline.arc_count;
2288 a = &geometry->outline.arcs[base_idx];
2290 if (!d2d_array_reserve((void **)&geometry->outline.arc_faces, &geometry->outline.arc_faces_size,
2291 geometry->outline.arc_face_count + 3, sizeof(*geometry->outline.arc_faces)))
2293 ERR("Failed to grow outline faces array.\n");
2294 return FALSE;
2296 f = &geometry->outline.arc_faces[geometry->outline.arc_face_count];
2298 d2d_point_subtract(&r0, p1, p0);
2299 d2d_point_subtract(&r1, p2, p1);
2301 d2d_point_normalise(&r0);
2302 d2d_point_normalise(&r1);
2304 if (d2d_point_ccw(p0, p1, p2) > 0.0f)
2306 d2d_point_scale(&r0, -1.0f);
2307 d2d_point_scale(&r1, -1.0f);
2310 d2d_curve_outline_vertex_set(&a[0], p0, p0, p1, p2, r0.x, r0.y, r0.x, r0.y);
2311 d2d_curve_outline_vertex_set(&a[1], p0, p0, p1, p2, -r0.x, -r0.y, -r0.x, -r0.y);
2312 d2d_curve_outline_vertex_set(&a[2], p1, p0, p1, p2, r0.x, r0.y, r1.x, r1.y);
2313 d2d_curve_outline_vertex_set(&a[3], p2, p0, p1, p2, -r1.x, -r1.y, -r1.x, -r1.y);
2314 d2d_curve_outline_vertex_set(&a[4], p2, p0, p1, p2, r1.x, r1.y, r1.x, r1.y);
2315 geometry->outline.arc_count += 5;
2317 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2318 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2319 d2d_face_set(&f[2], base_idx + 2, base_idx + 4, base_idx + 3);
2320 geometry->outline.arc_face_count += 3;
2322 return TRUE;
2325 static BOOL d2d_geometry_add_figure_outline(struct d2d_geometry *geometry,
2326 struct d2d_figure *figure, D2D1_FIGURE_END figure_end)
2328 const D2D1_POINT_2F *prev, *p0, *next;
2329 enum d2d_vertex_type prev_type, type;
2330 size_t bezier_idx, i;
2332 for (i = 0, bezier_idx = 0; i < figure->vertex_count; ++i)
2334 type = figure->vertex_types[i];
2335 if (type == D2D_VERTEX_TYPE_NONE)
2336 continue;
2338 p0 = &figure->vertices[i];
2340 if (!i)
2342 prev_type = figure->vertex_types[figure->vertex_count - 1];
2343 if (d2d_vertex_type_is_bezier(prev_type))
2344 prev = &figure->bezier_controls[figure->bezier_control_count - 1];
2345 else
2346 prev = &figure->vertices[figure->vertex_count - 1];
2348 else
2350 prev_type = figure->vertex_types[i - 1];
2351 if (d2d_vertex_type_is_bezier(prev_type))
2352 prev = &figure->bezier_controls[bezier_idx - 1];
2353 else
2354 prev = &figure->vertices[i - 1];
2357 if (d2d_vertex_type_is_bezier(type))
2358 next = &figure->bezier_controls[bezier_idx++];
2359 else if (i == figure->vertex_count - 1)
2360 next = &figure->vertices[0];
2361 else
2362 next = &figure->vertices[i + 1];
2364 if (figure_end == D2D1_FIGURE_END_CLOSED || (i && i < figure->vertex_count - 1))
2366 D2D1_POINT_2F q_next, q_prev;
2368 d2d_point_subtract(&q_prev, prev, p0);
2369 d2d_point_subtract(&q_next, next, p0);
2371 d2d_point_normalise(&q_prev);
2372 d2d_point_normalise(&q_next);
2374 if (!d2d_geometry_outline_add_join(geometry, &q_prev, p0, &q_next))
2376 ERR("Failed to add join.\n");
2377 return FALSE;
2381 if (type == D2D_VERTEX_TYPE_LINE && (figure_end == D2D1_FIGURE_END_CLOSED || i < figure->vertex_count - 1)
2382 && !d2d_geometry_outline_add_line_segment(geometry, p0, next))
2384 ERR("Failed to add line segment.\n");
2385 return FALSE;
2387 else if (d2d_vertex_type_is_bezier(type))
2389 const D2D1_POINT_2F *p2;
2391 if (i == figure->vertex_count - 1)
2392 p2 = &figure->vertices[0];
2393 else
2394 p2 = &figure->vertices[i + 1];
2396 if (!d2d_geometry_outline_add_bezier_segment(geometry, p0, next, p2))
2398 ERR("Failed to add bezier segment.\n");
2399 return FALSE;
2404 return TRUE;
2407 static BOOL d2d_geometry_fill_add_arc_triangle(struct d2d_geometry *geometry,
2408 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
2410 struct d2d_curve_vertex *a;
2412 if (!d2d_array_reserve((void **)&geometry->fill.arc_vertices, &geometry->fill.arc_vertices_size,
2413 geometry->fill.arc_vertex_count + 3, sizeof(*geometry->fill.arc_vertices)))
2414 return FALSE;
2416 a = &geometry->fill.arc_vertices[geometry->fill.arc_vertex_count];
2417 d2d_curve_vertex_set(&a[0], p0, 0.0f, 1.0f, -1.0f);
2418 d2d_curve_vertex_set(&a[1], p1, 1.0f, 1.0f, -1.0f);
2419 d2d_curve_vertex_set(&a[2], p2, 1.0f, 0.0f, -1.0f);
2420 geometry->fill.arc_vertex_count += 3;
2422 return TRUE;
2425 static void d2d_geometry_cleanup(struct d2d_geometry *geometry)
2427 heap_free(geometry->outline.arc_faces);
2428 heap_free(geometry->outline.arcs);
2429 heap_free(geometry->outline.bezier_faces);
2430 heap_free(geometry->outline.beziers);
2431 heap_free(geometry->outline.faces);
2432 heap_free(geometry->outline.vertices);
2433 heap_free(geometry->fill.arc_vertices);
2434 heap_free(geometry->fill.bezier_vertices);
2435 heap_free(geometry->fill.faces);
2436 heap_free(geometry->fill.vertices);
2437 ID2D1Factory_Release(geometry->factory);
2440 static void d2d_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
2441 const D2D1_MATRIX_3X2_F *transform, const struct ID2D1GeometryVtbl *vtbl)
2443 geometry->ID2D1Geometry_iface.lpVtbl = vtbl;
2444 geometry->refcount = 1;
2445 ID2D1Factory_AddRef(geometry->factory = factory);
2446 geometry->transform = *transform;
2449 static inline struct d2d_geometry *impl_from_ID2D1GeometrySink(ID2D1GeometrySink *iface)
2451 return CONTAINING_RECORD(iface, struct d2d_geometry, u.path.ID2D1GeometrySink_iface);
2454 static HRESULT STDMETHODCALLTYPE d2d_geometry_sink_QueryInterface(ID2D1GeometrySink *iface, REFIID iid, void **out)
2456 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
2458 if (IsEqualGUID(iid, &IID_ID2D1GeometrySink)
2459 || IsEqualGUID(iid, &IID_ID2D1SimplifiedGeometrySink)
2460 || IsEqualGUID(iid, &IID_IUnknown))
2462 ID2D1GeometrySink_AddRef(iface);
2463 *out = iface;
2464 return S_OK;
2467 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
2469 *out = NULL;
2470 return E_NOINTERFACE;
2473 static ULONG STDMETHODCALLTYPE d2d_geometry_sink_AddRef(ID2D1GeometrySink *iface)
2475 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2477 TRACE("iface %p.\n", iface);
2479 return ID2D1Geometry_AddRef(&geometry->ID2D1Geometry_iface);
2482 static ULONG STDMETHODCALLTYPE d2d_geometry_sink_Release(ID2D1GeometrySink *iface)
2484 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2486 TRACE("iface %p.\n", iface);
2488 return ID2D1Geometry_Release(&geometry->ID2D1Geometry_iface);
2491 static void STDMETHODCALLTYPE d2d_geometry_sink_SetFillMode(ID2D1GeometrySink *iface, D2D1_FILL_MODE mode)
2493 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2495 TRACE("iface %p, mode %#x.\n", iface, mode);
2497 if (geometry->u.path.state == D2D_GEOMETRY_STATE_CLOSED)
2498 return;
2499 geometry->u.path.fill_mode = mode;
2502 static void STDMETHODCALLTYPE d2d_geometry_sink_SetSegmentFlags(ID2D1GeometrySink *iface, D2D1_PATH_SEGMENT flags)
2504 FIXME("iface %p, flags %#x stub!\n", iface, flags);
2507 static void STDMETHODCALLTYPE d2d_geometry_sink_BeginFigure(ID2D1GeometrySink *iface,
2508 D2D1_POINT_2F start_point, D2D1_FIGURE_BEGIN figure_begin)
2510 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2511 struct d2d_figure *figure;
2513 TRACE("iface %p, start_point %s, figure_begin %#x.\n",
2514 iface, debug_d2d_point_2f(&start_point), figure_begin);
2516 if (geometry->u.path.state != D2D_GEOMETRY_STATE_OPEN)
2518 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2519 return;
2522 if (!d2d_path_geometry_add_figure(geometry))
2524 ERR("Failed to add figure.\n");
2525 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2526 return;
2529 figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2530 if (figure_begin == D2D1_FIGURE_BEGIN_HOLLOW)
2531 figure->flags |= D2D_FIGURE_FLAG_HOLLOW;
2533 if (!d2d_figure_add_vertex(figure, start_point))
2535 ERR("Failed to add vertex.\n");
2536 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2537 return;
2540 geometry->u.path.state = D2D_GEOMETRY_STATE_FIGURE;
2543 static void STDMETHODCALLTYPE d2d_geometry_sink_AddLines(ID2D1GeometrySink *iface,
2544 const D2D1_POINT_2F *points, UINT32 count)
2546 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2547 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2548 unsigned int i;
2550 TRACE("iface %p, points %p, count %u.\n", iface, points, count);
2552 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2554 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2555 return;
2558 for (i = 0; i < count; ++i)
2560 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_LINE;
2561 if (!d2d_figure_add_vertex(figure, points[i]))
2563 ERR("Failed to add vertex.\n");
2564 return;
2568 geometry->u.path.segment_count += count;
2571 static void STDMETHODCALLTYPE d2d_geometry_sink_AddBeziers(ID2D1GeometrySink *iface,
2572 const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
2574 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2575 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2576 D2D1_POINT_2F p;
2577 unsigned int i;
2579 TRACE("iface %p, beziers %p, count %u.\n", iface, beziers, count);
2581 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2583 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2584 return;
2587 for (i = 0; i < count; ++i)
2589 D2D1_RECT_F bezier_bounds;
2591 /* FIXME: This tries to approximate a cubic bezier with a quadratic one. */
2592 p.x = (beziers[i].point1.x + beziers[i].point2.x) * 0.75f;
2593 p.y = (beziers[i].point1.y + beziers[i].point2.y) * 0.75f;
2594 p.x -= (figure->vertices[figure->vertex_count - 1].x + beziers[i].point3.x) * 0.25f;
2595 p.y -= (figure->vertices[figure->vertex_count - 1].y + beziers[i].point3.y) * 0.25f;
2596 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_BEZIER;
2598 d2d_rect_get_bezier_bounds(&bezier_bounds, &figure->vertices[figure->vertex_count - 1],
2599 &p, &beziers[i].point3);
2601 if (!d2d_figure_add_bezier_controls(figure, 1, &p))
2603 ERR("Failed to add bezier control.\n");
2604 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2605 return;
2608 if (!d2d_figure_add_vertex(figure, beziers[i].point3))
2610 ERR("Failed to add bezier vertex.\n");
2611 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2612 return;
2615 d2d_rect_union(&figure->bounds, &bezier_bounds);
2618 geometry->u.path.segment_count += count;
2621 static void STDMETHODCALLTYPE d2d_geometry_sink_EndFigure(ID2D1GeometrySink *iface, D2D1_FIGURE_END figure_end)
2623 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2624 struct d2d_figure *figure;
2626 TRACE("iface %p, figure_end %#x.\n", iface, figure_end);
2628 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2630 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2631 return;
2634 figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2635 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_LINE;
2636 if (figure_end == D2D1_FIGURE_END_CLOSED)
2638 ++geometry->u.path.segment_count;
2639 figure->flags |= D2D_FIGURE_FLAG_CLOSED;
2640 if (!memcmp(&figure->vertices[0], &figure->vertices[figure->vertex_count - 1], sizeof(*figure->vertices)))
2641 --figure->vertex_count;
2644 if (!d2d_geometry_add_figure_outline(geometry, figure, figure_end))
2646 ERR("Failed to add figure outline.\n");
2647 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2648 return;
2651 geometry->u.path.state = D2D_GEOMETRY_STATE_OPEN;
2654 static void d2d_path_geometry_free_figures(struct d2d_geometry *geometry)
2656 size_t i;
2658 if (!geometry->u.path.figures)
2659 return;
2661 for (i = 0; i < geometry->u.path.figure_count; ++i)
2663 heap_free(geometry->u.path.figures[i].bezier_controls);
2664 heap_free(geometry->u.path.figures[i].original_bezier_controls);
2665 heap_free(geometry->u.path.figures[i].vertices);
2667 heap_free(geometry->u.path.figures);
2668 geometry->u.path.figures = NULL;
2669 geometry->u.path.figures_size = 0;
2672 static BOOL d2d_geometry_get_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx, BOOL next)
2674 if (next)
2676 ++idx->vertex_idx;
2677 ++idx->control_idx;
2680 for (; idx->figure_idx < geometry->u.path.figure_count; ++idx->figure_idx, idx->vertex_idx = idx->control_idx = 0)
2682 struct d2d_figure *figure = &geometry->u.path.figures[idx->figure_idx];
2684 if (!figure->bezier_control_count || figure->flags & D2D_FIGURE_FLAG_HOLLOW)
2685 continue;
2687 for (; idx->vertex_idx < figure->vertex_count; ++idx->vertex_idx)
2689 if (d2d_vertex_type_is_bezier(figure->vertex_types[idx->vertex_idx]))
2690 return TRUE;
2694 return FALSE;
2697 static BOOL d2d_geometry_get_first_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx)
2699 memset(idx, 0, sizeof(*idx));
2701 return d2d_geometry_get_bezier_segment_idx(geometry, idx, FALSE);
2704 static BOOL d2d_geometry_get_next_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx)
2706 return d2d_geometry_get_bezier_segment_idx(geometry, idx, TRUE);
2709 static BOOL d2d_geometry_check_bezier_overlap(struct d2d_geometry *geometry,
2710 const struct d2d_segment_idx *idx_p, const struct d2d_segment_idx *idx_q)
2712 const D2D1_POINT_2F *a[3], *b[3], *p[2], *q;
2713 const struct d2d_figure *figure;
2714 D2D1_POINT_2F v_q[3], v_p, v_qp;
2715 unsigned int i, j, score;
2716 float det, t;
2718 figure = &geometry->u.path.figures[idx_p->figure_idx];
2719 a[0] = &figure->vertices[idx_p->vertex_idx];
2720 a[1] = &figure->bezier_controls[idx_p->control_idx];
2721 if (idx_p->vertex_idx == figure->vertex_count - 1)
2722 a[2] = &figure->vertices[0];
2723 else
2724 a[2] = &figure->vertices[idx_p->vertex_idx + 1];
2726 figure = &geometry->u.path.figures[idx_q->figure_idx];
2727 b[0] = &figure->vertices[idx_q->vertex_idx];
2728 b[1] = &figure->bezier_controls[idx_q->control_idx];
2729 if (idx_q->vertex_idx == figure->vertex_count - 1)
2730 b[2] = &figure->vertices[0];
2731 else
2732 b[2] = &figure->vertices[idx_q->vertex_idx + 1];
2734 if (d2d_point_ccw(a[0], a[1], a[2]) == 0.0f || d2d_point_ccw(b[0], b[1], b[2]) == 0.0f)
2735 return FALSE;
2737 d2d_point_subtract(&v_q[0], b[1], b[0]);
2738 d2d_point_subtract(&v_q[1], b[2], b[0]);
2739 d2d_point_subtract(&v_q[2], b[1], b[2]);
2741 /* Check for intersections between the edges. Strictly speaking we'd only
2742 * need to check 8 of the 9 possible intersections, since if there's any
2743 * intersection there has to be a second intersection as well. */
2744 for (i = 0; i < 3; ++i)
2746 d2d_point_subtract(&v_p, a[(i & 1) + 1], a[i & 2]);
2747 for (j = 0; j < 3; ++j)
2749 det = v_p.x * v_q[j].y - v_p.y * v_q[j].x;
2750 if (det == 0.0f)
2751 continue;
2753 d2d_point_subtract(&v_qp, a[i & 2], b[j & 2]);
2754 t = (v_q[j].x * v_qp.y - v_q[j].y * v_qp.x) / det;
2755 if (t <= 0.0f || t >= 1.0f)
2756 continue;
2758 t = (v_p.x * v_qp.y - v_p.y * v_qp.x) / det;
2759 if (t <= 0.0f || t >= 1.0f)
2760 continue;
2762 return TRUE;
2766 /* Check if one triangle is contained within the other. */
2767 for (j = 0, score = 0, q = a[1], p[0] = b[2]; j < 3; ++j)
2769 p[1] = b[j];
2770 d2d_point_subtract(&v_p, p[1], p[0]);
2771 d2d_point_subtract(&v_qp, q, p[0]);
2773 if ((q->y < p[0]->y) != (q->y < p[1]->y) && v_qp.x < v_p.x * (v_qp.y / v_p.y))
2774 ++score;
2776 p[0] = p[1];
2779 if (score & 1)
2780 return TRUE;
2782 for (j = 0, score = 0, q = b[1], p[0] = a[2]; j < 3; ++j)
2784 p[1] = a[j];
2785 d2d_point_subtract(&v_p, p[1], p[0]);
2786 d2d_point_subtract(&v_qp, q, p[0]);
2788 if ((q->y < p[0]->y) != (q->y < p[1]->y) && v_qp.x < v_p.x * (v_qp.y / v_p.y))
2789 ++score;
2791 p[0] = p[1];
2794 return score & 1;
2797 static float d2d_geometry_bezier_ccw(struct d2d_geometry *geometry, const struct d2d_segment_idx *idx)
2799 const struct d2d_figure *figure = &geometry->u.path.figures[idx->figure_idx];
2800 size_t next = idx->vertex_idx + 1;
2802 if (next == figure->vertex_count)
2803 next = 0;
2805 return d2d_point_ccw(&figure->vertices[idx->vertex_idx],
2806 &figure->bezier_controls[idx->control_idx], &figure->vertices[next]);
2809 static BOOL d2d_geometry_split_bezier(struct d2d_geometry *geometry, const struct d2d_segment_idx *idx)
2811 const D2D1_POINT_2F *p[3];
2812 struct d2d_figure *figure;
2813 D2D1_POINT_2F q[3];
2814 size_t next;
2816 figure = &geometry->u.path.figures[idx->figure_idx];
2817 p[0] = &figure->vertices[idx->vertex_idx];
2818 p[1] = &figure->bezier_controls[idx->control_idx];
2819 next = idx->vertex_idx + 1;
2820 if (next == figure->vertex_count)
2821 next = 0;
2822 p[2] = &figure->vertices[next];
2824 d2d_point_lerp(&q[0], p[0], p[1], 0.5f);
2825 d2d_point_lerp(&q[1], p[1], p[2], 0.5f);
2826 d2d_point_lerp(&q[2], &q[0], &q[1], 0.5f);
2828 figure->bezier_controls[idx->control_idx] = q[0];
2829 if (!(d2d_figure_insert_bezier_controls(figure, idx->control_idx + 1, 1, &q[1])))
2830 return FALSE;
2831 if (!(d2d_figure_insert_vertex(figure, idx->vertex_idx + 1, q[2])))
2832 return FALSE;
2833 figure->vertex_types[idx->vertex_idx + 1] = D2D_VERTEX_TYPE_SPLIT_BEZIER;
2835 return TRUE;
2838 static HRESULT d2d_geometry_resolve_beziers(struct d2d_geometry *geometry)
2840 struct d2d_segment_idx idx_p, idx_q;
2841 struct d2d_curve_vertex *b;
2842 const D2D1_POINT_2F *p[3];
2843 struct d2d_figure *figure;
2844 size_t bezier_idx, i;
2846 if (!d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_p))
2847 return S_OK;
2849 /* Split overlapping bezier control triangles. */
2850 while (d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_p))
2852 d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_q);
2853 while (idx_q.figure_idx < idx_p.figure_idx || idx_q.vertex_idx < idx_p.vertex_idx)
2855 while (d2d_geometry_check_bezier_overlap(geometry, &idx_p, &idx_q))
2857 if (fabsf(d2d_geometry_bezier_ccw(geometry, &idx_q)) > fabsf(d2d_geometry_bezier_ccw(geometry, &idx_p)))
2859 if (!d2d_geometry_split_bezier(geometry, &idx_q))
2860 return E_OUTOFMEMORY;
2861 if (idx_p.figure_idx == idx_q.figure_idx)
2863 ++idx_p.vertex_idx;
2864 ++idx_p.control_idx;
2867 else
2869 if (!d2d_geometry_split_bezier(geometry, &idx_p))
2870 return E_OUTOFMEMORY;
2873 d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_q);
2877 for (i = 0; i < geometry->u.path.figure_count; ++i)
2879 if (geometry->u.path.figures[i].flags & D2D_FIGURE_FLAG_HOLLOW)
2880 continue;
2881 geometry->fill.bezier_vertex_count += 3 * geometry->u.path.figures[i].bezier_control_count;
2884 if (!(geometry->fill.bezier_vertices = heap_calloc(geometry->fill.bezier_vertex_count,
2885 sizeof(*geometry->fill.bezier_vertices))))
2887 ERR("Failed to allocate bezier vertices array.\n");
2888 geometry->fill.bezier_vertex_count = 0;
2889 return E_OUTOFMEMORY;
2892 bezier_idx = 0;
2893 d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_p);
2894 for (;;)
2896 float sign = -1.0f;
2898 figure = &geometry->u.path.figures[idx_p.figure_idx];
2899 p[0] = &figure->vertices[idx_p.vertex_idx];
2900 p[1] = &figure->bezier_controls[idx_p.control_idx];
2902 i = idx_p.vertex_idx + 1;
2903 if (d2d_path_geometry_point_inside(geometry, p[1], FALSE))
2905 sign = 1.0f;
2906 d2d_figure_insert_vertex(figure, i, *p[1]);
2907 /* Inserting a vertex potentially invalidates p[0]. */
2908 p[0] = &figure->vertices[idx_p.vertex_idx];
2909 ++i;
2912 if (i == figure->vertex_count)
2913 i = 0;
2914 p[2] = &figure->vertices[i];
2916 b = &geometry->fill.bezier_vertices[bezier_idx * 3];
2917 d2d_curve_vertex_set(&b[0], p[0], 0.0f, 0.0f, sign);
2918 d2d_curve_vertex_set(&b[1], p[1], 0.5f, 0.0f, sign);
2919 d2d_curve_vertex_set(&b[2], p[2], 1.0f, 1.0f, sign);
2921 if (!d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_p))
2922 break;
2923 ++bezier_idx;
2926 return S_OK;
2929 static HRESULT STDMETHODCALLTYPE d2d_geometry_sink_Close(ID2D1GeometrySink *iface)
2931 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2932 HRESULT hr = E_FAIL;
2933 size_t i;
2935 TRACE("iface %p.\n", iface);
2937 if (geometry->u.path.state != D2D_GEOMETRY_STATE_OPEN)
2939 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
2940 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2941 return D2DERR_WRONG_STATE;
2943 geometry->u.path.state = D2D_GEOMETRY_STATE_CLOSED;
2945 for (i = 0; i < geometry->u.path.figure_count; ++i)
2947 struct d2d_figure *figure = &geometry->u.path.figures[i];
2948 size_t size = figure->bezier_control_count * sizeof(*figure->original_bezier_controls);
2949 if (!(figure->original_bezier_controls = heap_alloc(size)))
2950 goto done;
2951 memcpy(figure->original_bezier_controls, figure->bezier_controls, size);
2954 if (!d2d_geometry_intersect_self(geometry))
2955 goto done;
2956 if (FAILED(hr = d2d_geometry_resolve_beziers(geometry)))
2957 goto done;
2958 if (FAILED(hr = d2d_path_geometry_triangulate(geometry)))
2959 goto done;
2961 done:
2962 if (FAILED(hr))
2964 heap_free(geometry->fill.bezier_vertices);
2965 geometry->fill.bezier_vertex_count = 0;
2966 d2d_path_geometry_free_figures(geometry);
2967 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2969 return hr;
2972 static void STDMETHODCALLTYPE d2d_geometry_sink_AddLine(ID2D1GeometrySink *iface, D2D1_POINT_2F point)
2974 TRACE("iface %p, point %s.\n", iface, debug_d2d_point_2f(&point));
2976 d2d_geometry_sink_AddLines(iface, &point, 1);
2979 static void STDMETHODCALLTYPE d2d_geometry_sink_AddBezier(ID2D1GeometrySink *iface, const D2D1_BEZIER_SEGMENT *bezier)
2981 TRACE("iface %p, bezier %p.\n", iface, bezier);
2983 d2d_geometry_sink_AddBeziers(iface, bezier, 1);
2986 static void STDMETHODCALLTYPE d2d_geometry_sink_AddQuadraticBezier(ID2D1GeometrySink *iface,
2987 const D2D1_QUADRATIC_BEZIER_SEGMENT *bezier)
2989 TRACE("iface %p, bezier %p.\n", iface, bezier);
2991 ID2D1GeometrySink_AddQuadraticBeziers(iface, bezier, 1);
2994 static void STDMETHODCALLTYPE d2d_geometry_sink_AddQuadraticBeziers(ID2D1GeometrySink *iface,
2995 const D2D1_QUADRATIC_BEZIER_SEGMENT *beziers, UINT32 bezier_count)
2997 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2998 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2999 unsigned int i;
3001 TRACE("iface %p, beziers %p, bezier_count %u.\n", iface, beziers, bezier_count);
3003 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
3005 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
3006 return;
3009 for (i = 0; i < bezier_count; ++i)
3011 D2D1_RECT_F bezier_bounds;
3013 d2d_rect_get_bezier_bounds(&bezier_bounds, &figure->vertices[figure->vertex_count - 1],
3014 &beziers[i].point1, &beziers[i].point2);
3016 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_BEZIER;
3017 if (!d2d_figure_add_bezier_controls(figure, 1, &beziers[i].point1))
3019 ERR("Failed to add bezier.\n");
3020 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
3021 return;
3024 if (!d2d_figure_add_vertex(figure, beziers[i].point2))
3026 ERR("Failed to add bezier vertex.\n");
3027 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
3028 return;
3031 d2d_rect_union(&figure->bounds, &bezier_bounds);
3034 geometry->u.path.segment_count += bezier_count;
3037 static void STDMETHODCALLTYPE d2d_geometry_sink_AddArc(ID2D1GeometrySink *iface, const D2D1_ARC_SEGMENT *arc)
3039 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
3041 FIXME("iface %p, arc %p stub!\n", iface, arc);
3043 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
3045 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
3046 return;
3049 if (!d2d_figure_add_vertex(&geometry->u.path.figures[geometry->u.path.figure_count - 1], arc->point))
3051 ERR("Failed to add vertex.\n");
3052 return;
3055 ++geometry->u.path.segment_count;
3058 static const struct ID2D1GeometrySinkVtbl d2d_geometry_sink_vtbl =
3060 d2d_geometry_sink_QueryInterface,
3061 d2d_geometry_sink_AddRef,
3062 d2d_geometry_sink_Release,
3063 d2d_geometry_sink_SetFillMode,
3064 d2d_geometry_sink_SetSegmentFlags,
3065 d2d_geometry_sink_BeginFigure,
3066 d2d_geometry_sink_AddLines,
3067 d2d_geometry_sink_AddBeziers,
3068 d2d_geometry_sink_EndFigure,
3069 d2d_geometry_sink_Close,
3070 d2d_geometry_sink_AddLine,
3071 d2d_geometry_sink_AddBezier,
3072 d2d_geometry_sink_AddQuadraticBezier,
3073 d2d_geometry_sink_AddQuadraticBeziers,
3074 d2d_geometry_sink_AddArc,
3077 static inline struct d2d_geometry *impl_from_ID2D1PathGeometry(ID2D1PathGeometry *iface)
3079 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3082 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_QueryInterface(ID2D1PathGeometry *iface, REFIID iid, void **out)
3084 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3086 if (IsEqualGUID(iid, &IID_ID2D1PathGeometry)
3087 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3088 || IsEqualGUID(iid, &IID_ID2D1Resource)
3089 || IsEqualGUID(iid, &IID_IUnknown))
3091 ID2D1PathGeometry_AddRef(iface);
3092 *out = iface;
3093 return S_OK;
3096 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3098 *out = NULL;
3099 return E_NOINTERFACE;
3102 static ULONG STDMETHODCALLTYPE d2d_path_geometry_AddRef(ID2D1PathGeometry *iface)
3104 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3105 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3107 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3109 return refcount;
3112 static ULONG STDMETHODCALLTYPE d2d_path_geometry_Release(ID2D1PathGeometry *iface)
3114 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3115 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3117 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3119 if (!refcount)
3121 d2d_path_geometry_free_figures(geometry);
3122 d2d_geometry_cleanup(geometry);
3123 heap_free(geometry);
3126 return refcount;
3129 static void STDMETHODCALLTYPE d2d_path_geometry_GetFactory(ID2D1PathGeometry *iface, ID2D1Factory **factory)
3131 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3133 TRACE("iface %p, factory %p.\n", iface, factory);
3135 ID2D1Factory_AddRef(*factory = geometry->factory);
3138 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetBounds(ID2D1PathGeometry *iface,
3139 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3141 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3142 size_t i;
3144 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3146 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3147 return D2DERR_WRONG_STATE;
3149 bounds->left = FLT_MAX;
3150 bounds->top = FLT_MAX;
3151 bounds->right = -FLT_MAX;
3152 bounds->bottom = -FLT_MAX;
3154 if (!transform)
3156 if (geometry->u.path.bounds.left > geometry->u.path.bounds.right
3157 && !isinf(geometry->u.path.bounds.left))
3159 for (i = 0; i < geometry->u.path.figure_count; ++i)
3161 if (geometry->u.path.figures[i].flags & D2D_FIGURE_FLAG_HOLLOW)
3162 continue;
3163 d2d_rect_union(&geometry->u.path.bounds, &geometry->u.path.figures[i].bounds);
3165 if (geometry->u.path.bounds.left > geometry->u.path.bounds.right)
3167 geometry->u.path.bounds.left = INFINITY;
3168 geometry->u.path.bounds.right = FLT_MAX;
3169 geometry->u.path.bounds.top = INFINITY;
3170 geometry->u.path.bounds.bottom = FLT_MAX;
3174 *bounds = geometry->u.path.bounds;
3175 return S_OK;
3178 for (i = 0; i < geometry->u.path.figure_count; ++i)
3180 const struct d2d_figure *figure = &geometry->u.path.figures[i];
3181 enum d2d_vertex_type type = D2D_VERTEX_TYPE_NONE;
3182 D2D1_RECT_F bezier_bounds;
3183 D2D1_POINT_2F p, p1, p2;
3184 size_t j, bezier_idx;
3186 if (figure->flags & D2D_FIGURE_FLAG_HOLLOW)
3187 continue;
3189 /* Single vertex figures are reduced by CloseFigure(). */
3190 if (figure->vertex_count == 0)
3192 d2d_point_transform(&p, transform, figure->bounds.left, figure->bounds.top);
3193 d2d_rect_expand(bounds, &p);
3194 continue;
3197 for (j = 0; j < figure->vertex_count; ++j)
3199 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
3200 continue;
3202 p = figure->vertices[j];
3203 type = figure->vertex_types[j];
3204 d2d_point_transform(&p, transform, p.x, p.y);
3205 d2d_rect_expand(bounds, &p);
3206 break;
3209 for (bezier_idx = 0, ++j; j < figure->vertex_count; ++j)
3211 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE
3212 || d2d_vertex_type_is_split_bezier(figure->vertex_types[j]))
3213 continue;
3215 switch (type)
3217 case D2D_VERTEX_TYPE_LINE:
3218 p = figure->vertices[j];
3219 d2d_point_transform(&p, transform, p.x, p.y);
3220 d2d_rect_expand(bounds, &p);
3221 break;
3223 case D2D_VERTEX_TYPE_BEZIER:
3224 p1 = figure->original_bezier_controls[bezier_idx++];
3225 d2d_point_transform(&p1, transform, p1.x, p1.y);
3226 p2 = figure->vertices[j];
3227 d2d_point_transform(&p2, transform, p2.x, p2.y);
3228 d2d_rect_get_bezier_bounds(&bezier_bounds, &p, &p1, &p2);
3229 d2d_rect_union(bounds, &bezier_bounds);
3230 p = p2;
3231 break;
3233 default:
3234 FIXME("Unhandled vertex type %#x.\n", type);
3235 p = figure->vertices[j];
3236 d2d_point_transform(&p, transform, p.x, p.y);
3237 d2d_rect_expand(bounds, &p);
3238 break;
3241 type = figure->vertex_types[j];
3244 if (d2d_vertex_type_is_bezier(type))
3246 p1 = figure->original_bezier_controls[bezier_idx++];
3247 d2d_point_transform(&p1, transform, p1.x, p1.y);
3248 p2 = figure->vertices[0];
3249 d2d_point_transform(&p2, transform, p2.x, p2.y);
3250 d2d_rect_get_bezier_bounds(&bezier_bounds, &p, &p1, &p2);
3251 d2d_rect_union(bounds, &bezier_bounds);
3255 if (bounds->left > bounds->right)
3257 bounds->left = INFINITY;
3258 bounds->right = FLT_MAX;
3259 bounds->top = INFINITY;
3260 bounds->bottom = FLT_MAX;
3263 return S_OK;
3266 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetWidenedBounds(ID2D1PathGeometry *iface, float stroke_width,
3267 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_RECT_F *bounds)
3269 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3270 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3272 return E_NOTIMPL;
3275 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_StrokeContainsPoint(ID2D1PathGeometry *iface,
3276 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3277 float tolerance, BOOL *contains)
3279 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, "
3280 "transform %p, tolerance %.8e, contains %p stub!\n",
3281 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
3283 return E_NOTIMPL;
3286 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_FillContainsPoint(ID2D1PathGeometry *iface,
3287 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3289 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3290 D2D1_MATRIX_3X2_F g_i;
3292 TRACE("iface %p, point %s, transform %p, tolerance %.8e, contains %p.\n",
3293 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
3295 if (transform)
3297 if (!d2d_matrix_invert(&g_i, transform))
3298 return D2DERR_UNSUPPORTED_OPERATION;
3299 d2d_point_transform(&point, &g_i, point.x, point.y);
3302 *contains = !!d2d_path_geometry_point_inside(geometry, &point, FALSE);
3304 TRACE("-> %#x.\n", *contains);
3306 return S_OK;
3309 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_CompareWithGeometry(ID2D1PathGeometry *iface,
3310 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3312 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3313 iface, geometry, transform, tolerance, relation);
3315 return E_NOTIMPL;
3318 static void d2d_geometry_flatten_cubic(ID2D1SimplifiedGeometrySink *sink, const D2D1_POINT_2F *p0,
3319 const D2D1_BEZIER_SEGMENT *b, float tolerance)
3321 D2D1_BEZIER_SEGMENT b0, b1;
3322 D2D1_POINT_2F q;
3323 float d;
3325 /* It's certainly possible to calculate the maximum deviation of the
3326 * approximation from the curve, but it's a little involved. Instead, note
3327 * that if the control points were evenly spaced and collinear, p1 would
3328 * be exactly between p0 and p2, and p2 would be exactly between p1 and
3329 * p3. The deviation is a decent enough approximation, and much easier to
3330 * calculate.
3332 * p1' = (p0 + p2) / 2
3333 * p2' = (p1 + p3) / 2
3334 * d = ‖p1 - p1'‖₁ + ‖p2 - p2'‖₁ */
3335 d2d_point_lerp(&q, p0, &b->point2, 0.5f);
3336 d2d_point_subtract(&q, &b->point1, &q);
3337 d = fabsf(q.x) + fabsf(q.y);
3338 d2d_point_lerp(&q, &b->point1, &b->point3, 0.5f);
3339 d2d_point_subtract(&q, &b->point2, &q);
3340 d += fabsf(q.x) + fabsf(q.y);
3341 if (d < tolerance)
3343 ID2D1SimplifiedGeometrySink_AddLines(sink, &b->point3, 1);
3344 return;
3347 d2d_point_lerp(&q, &b->point1, &b->point2, 0.5f);
3349 b1.point3 = b->point3;
3350 d2d_point_lerp(&b1.point2, &b1.point3, &b->point2, 0.5f);
3351 d2d_point_lerp(&b1.point1, &b1.point2, &q, 0.5f);
3353 d2d_point_lerp(&b0.point1, p0, &b->point1, 0.5f);
3354 d2d_point_lerp(&b0.point2, &b0.point1, &q, 0.5f);
3355 d2d_point_lerp(&b0.point3, &b0.point2, &b1.point1, 0.5f);
3357 d2d_geometry_flatten_cubic(sink, p0, &b0, tolerance);
3358 ID2D1SimplifiedGeometrySink_SetSegmentFlags(sink, D2D1_PATH_SEGMENT_FORCE_ROUND_LINE_JOIN);
3359 d2d_geometry_flatten_cubic(sink, &b0.point3, &b1, tolerance);
3360 ID2D1SimplifiedGeometrySink_SetSegmentFlags(sink, D2D1_PATH_SEGMENT_NONE);
3363 static void d2d_geometry_simplify_quadratic(ID2D1SimplifiedGeometrySink *sink,
3364 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_POINT_2F *p0,
3365 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float tolerance)
3367 D2D1_BEZIER_SEGMENT b;
3369 d2d_point_lerp(&b.point1, p0, p1, 2.0f / 3.0f);
3370 d2d_point_lerp(&b.point2, p2, p1, 2.0f / 3.0f);
3371 b.point3 = *p2;
3373 if (option == D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES)
3374 d2d_geometry_flatten_cubic(sink, p0, &b, tolerance);
3375 else
3376 ID2D1SimplifiedGeometrySink_AddBeziers(sink, &b, 1);
3379 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Simplify(ID2D1PathGeometry *iface,
3380 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3381 ID2D1SimplifiedGeometrySink *sink)
3383 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3384 enum d2d_vertex_type type = D2D_VERTEX_TYPE_NONE;
3385 unsigned int i, j, bezier_idx;
3386 D2D1_FIGURE_BEGIN begin;
3387 D2D1_POINT_2F p, p1, p2;
3388 D2D1_FIGURE_END end;
3390 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
3391 iface, option, transform, tolerance, sink);
3393 ID2D1SimplifiedGeometrySink_SetFillMode(sink, geometry->u.path.fill_mode);
3394 for (i = 0; i < geometry->u.path.figure_count; ++i)
3396 const struct d2d_figure *figure = &geometry->u.path.figures[i];
3398 for (j = 0; j < figure->vertex_count; ++j)
3400 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
3401 continue;
3403 p = figure->vertices[j];
3404 if (transform)
3405 d2d_point_transform(&p, transform, p.x, p.y);
3406 begin = figure->flags & D2D_FIGURE_FLAG_HOLLOW ? D2D1_FIGURE_BEGIN_HOLLOW : D2D1_FIGURE_BEGIN_FILLED;
3407 ID2D1SimplifiedGeometrySink_BeginFigure(sink, p, begin);
3408 type = figure->vertex_types[j];
3409 break;
3412 for (bezier_idx = 0, ++j; j < figure->vertex_count; ++j)
3414 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE
3415 || d2d_vertex_type_is_split_bezier(figure->vertex_types[j]))
3416 continue;
3418 switch (type)
3420 case D2D_VERTEX_TYPE_LINE:
3421 p = figure->vertices[j];
3422 if (transform)
3423 d2d_point_transform(&p, transform, p.x, p.y);
3424 ID2D1SimplifiedGeometrySink_AddLines(sink, &p, 1);
3425 break;
3427 case D2D_VERTEX_TYPE_BEZIER:
3428 p1 = figure->original_bezier_controls[bezier_idx++];
3429 if (transform)
3430 d2d_point_transform(&p1, transform, p1.x, p1.y);
3431 p2 = figure->vertices[j];
3432 if (transform)
3433 d2d_point_transform(&p2, transform, p2.x, p2.y);
3434 d2d_geometry_simplify_quadratic(sink, option, &p, &p1, &p2, tolerance);
3435 p = p2;
3436 break;
3438 default:
3439 FIXME("Unhandled vertex type %#x.\n", type);
3440 p = figure->vertices[j];
3441 if (transform)
3442 d2d_point_transform(&p, transform, p.x, p.y);
3443 ID2D1SimplifiedGeometrySink_AddLines(sink, &p, 1);
3444 break;
3447 type = figure->vertex_types[j];
3450 if (d2d_vertex_type_is_bezier(type))
3452 p1 = figure->original_bezier_controls[bezier_idx++];
3453 if (transform)
3454 d2d_point_transform(&p1, transform, p1.x, p1.y);
3455 p2 = figure->vertices[0];
3456 if (transform)
3457 d2d_point_transform(&p2, transform, p2.x, p2.y);
3458 d2d_geometry_simplify_quadratic(sink, option, &p, &p1, &p2, tolerance);
3461 end = figure->flags & D2D_FIGURE_FLAG_CLOSED ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN;
3462 ID2D1SimplifiedGeometrySink_EndFigure(sink, end);
3465 return S_OK;
3468 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Tessellate(ID2D1PathGeometry *iface,
3469 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3471 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3473 return E_NOTIMPL;
3476 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_CombineWithGeometry(ID2D1PathGeometry *iface,
3477 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3478 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3480 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3481 iface, geometry, combine_mode, transform, tolerance, sink);
3483 return E_NOTIMPL;
3486 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Outline(ID2D1PathGeometry *iface,
3487 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
3489 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3491 return E_NOTIMPL;
3494 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputeArea(ID2D1PathGeometry *iface,
3495 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
3497 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
3499 return E_NOTIMPL;
3502 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputeLength(ID2D1PathGeometry *iface,
3503 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
3505 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
3507 return E_NOTIMPL;
3510 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputePointAtLength(ID2D1PathGeometry *iface, float length,
3511 const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point, D2D1_POINT_2F *tangent)
3513 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
3514 iface, length, transform, tolerance, point, tangent);
3516 return E_NOTIMPL;
3519 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Widen(ID2D1PathGeometry *iface, float stroke_width,
3520 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3521 ID2D1SimplifiedGeometrySink *sink)
3523 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
3524 iface, stroke_width, stroke_style, transform, tolerance, sink);
3526 return E_NOTIMPL;
3529 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Open(ID2D1PathGeometry *iface, ID2D1GeometrySink **sink)
3531 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3533 TRACE("iface %p, sink %p.\n", iface, sink);
3535 if (geometry->u.path.state != D2D_GEOMETRY_STATE_INITIAL)
3536 return D2DERR_WRONG_STATE;
3538 *sink = &geometry->u.path.ID2D1GeometrySink_iface;
3539 ID2D1GeometrySink_AddRef(*sink);
3541 geometry->u.path.state = D2D_GEOMETRY_STATE_OPEN;
3543 return S_OK;
3546 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Stream(ID2D1PathGeometry *iface, ID2D1GeometrySink *sink)
3548 FIXME("iface %p, sink %p stub!\n", iface, sink);
3550 return E_NOTIMPL;
3553 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetSegmentCount(ID2D1PathGeometry *iface, UINT32 *count)
3555 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3557 TRACE("iface %p, count %p.\n", iface, count);
3559 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3560 return D2DERR_WRONG_STATE;
3562 *count = geometry->u.path.segment_count;
3564 return S_OK;
3567 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetFigureCount(ID2D1PathGeometry *iface, UINT32 *count)
3569 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3571 TRACE("iface %p, count %p.\n", iface, count);
3573 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3574 return D2DERR_WRONG_STATE;
3576 *count = geometry->u.path.figure_count;
3578 return S_OK;
3581 static const struct ID2D1PathGeometryVtbl d2d_path_geometry_vtbl =
3583 d2d_path_geometry_QueryInterface,
3584 d2d_path_geometry_AddRef,
3585 d2d_path_geometry_Release,
3586 d2d_path_geometry_GetFactory,
3587 d2d_path_geometry_GetBounds,
3588 d2d_path_geometry_GetWidenedBounds,
3589 d2d_path_geometry_StrokeContainsPoint,
3590 d2d_path_geometry_FillContainsPoint,
3591 d2d_path_geometry_CompareWithGeometry,
3592 d2d_path_geometry_Simplify,
3593 d2d_path_geometry_Tessellate,
3594 d2d_path_geometry_CombineWithGeometry,
3595 d2d_path_geometry_Outline,
3596 d2d_path_geometry_ComputeArea,
3597 d2d_path_geometry_ComputeLength,
3598 d2d_path_geometry_ComputePointAtLength,
3599 d2d_path_geometry_Widen,
3600 d2d_path_geometry_Open,
3601 d2d_path_geometry_Stream,
3602 d2d_path_geometry_GetSegmentCount,
3603 d2d_path_geometry_GetFigureCount,
3606 void d2d_path_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory)
3608 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl);
3609 geometry->u.path.ID2D1GeometrySink_iface.lpVtbl = &d2d_geometry_sink_vtbl;
3610 geometry->u.path.bounds.left = FLT_MAX;
3611 geometry->u.path.bounds.right = -FLT_MAX;
3612 geometry->u.path.bounds.top = FLT_MAX;
3613 geometry->u.path.bounds.bottom = -FLT_MAX;
3616 static inline struct d2d_geometry *impl_from_ID2D1EllipseGeometry(ID2D1EllipseGeometry *iface)
3618 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3621 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_QueryInterface(ID2D1EllipseGeometry *iface,
3622 REFIID iid, void **out)
3624 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3626 if (IsEqualGUID(iid, &IID_ID2D1EllipseGeometry)
3627 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3628 || IsEqualGUID(iid, &IID_ID2D1Resource)
3629 || IsEqualGUID(iid, &IID_IUnknown))
3631 ID2D1EllipseGeometry_AddRef(iface);
3632 *out = iface;
3633 return S_OK;
3636 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3638 *out = NULL;
3639 return E_NOINTERFACE;
3642 static ULONG STDMETHODCALLTYPE d2d_ellipse_geometry_AddRef(ID2D1EllipseGeometry *iface)
3644 struct d2d_geometry *geometry = impl_from_ID2D1EllipseGeometry(iface);
3645 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3647 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3649 return refcount;
3652 static ULONG STDMETHODCALLTYPE d2d_ellipse_geometry_Release(ID2D1EllipseGeometry *iface)
3654 struct d2d_geometry *geometry = impl_from_ID2D1EllipseGeometry(iface);
3655 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3657 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3659 if (!refcount)
3661 d2d_geometry_cleanup(geometry);
3662 heap_free(geometry);
3665 return refcount;
3668 static void STDMETHODCALLTYPE d2d_ellipse_geometry_GetFactory(ID2D1EllipseGeometry *iface, ID2D1Factory **factory)
3670 struct d2d_geometry *geometry = impl_from_ID2D1EllipseGeometry(iface);
3672 TRACE("iface %p, factory %p.\n", iface, factory);
3674 ID2D1Factory_AddRef(*factory = geometry->factory);
3677 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_GetBounds(ID2D1EllipseGeometry *iface,
3678 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3680 FIXME("iface %p, transform %p, bounds %p stub!\n", iface, transform, bounds);
3682 return E_NOTIMPL;
3685 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_GetWidenedBounds(ID2D1EllipseGeometry *iface,
3686 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3687 float tolerance, D2D1_RECT_F *bounds)
3689 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3690 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3692 return E_NOTIMPL;
3695 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_StrokeContainsPoint(ID2D1EllipseGeometry *iface,
3696 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3697 float tolerance, BOOL *contains)
3699 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p stub!\n",
3700 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
3702 return E_NOTIMPL;
3705 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_FillContainsPoint(ID2D1EllipseGeometry *iface,
3706 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3708 FIXME("iface %p, point %s, transform %p, tolerance %.8e, contains %p stub!\n",
3709 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
3711 return E_NOTIMPL;
3714 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_CompareWithGeometry(ID2D1EllipseGeometry *iface,
3715 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3717 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3718 iface, geometry, transform, tolerance, relation);
3720 return E_NOTIMPL;
3723 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_Simplify(ID2D1EllipseGeometry *iface,
3724 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3725 ID2D1SimplifiedGeometrySink *sink)
3727 FIXME("iface %p, option %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3728 iface, option, transform, tolerance, sink);
3730 return E_NOTIMPL;
3733 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_Tessellate(ID2D1EllipseGeometry *iface,
3734 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3736 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3738 return E_NOTIMPL;
3741 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_CombineWithGeometry(ID2D1EllipseGeometry *iface,
3742 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3743 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3745 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3746 iface, geometry, combine_mode, transform, tolerance, sink);
3748 return E_NOTIMPL;
3751 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_Outline(ID2D1EllipseGeometry *iface,
3752 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
3754 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3756 return E_NOTIMPL;
3759 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_ComputeArea(ID2D1EllipseGeometry *iface,
3760 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
3762 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
3764 return E_NOTIMPL;
3767 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_ComputeLength(ID2D1EllipseGeometry *iface,
3768 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
3770 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
3772 return E_NOTIMPL;
3775 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_ComputePointAtLength(ID2D1EllipseGeometry *iface,
3776 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
3777 D2D1_POINT_2F *tangent)
3779 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
3780 iface, length, transform, tolerance, point, tangent);
3782 return E_NOTIMPL;
3785 static HRESULT STDMETHODCALLTYPE d2d_ellipse_geometry_Widen(ID2D1EllipseGeometry *iface, float stroke_width,
3786 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3787 ID2D1SimplifiedGeometrySink *sink)
3789 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
3790 iface, stroke_width, stroke_style, transform, tolerance, sink);
3792 return E_NOTIMPL;
3795 static void STDMETHODCALLTYPE d2d_ellipse_geometry_GetEllipse(ID2D1EllipseGeometry *iface, D2D1_ELLIPSE *ellipse)
3797 struct d2d_geometry *geometry = impl_from_ID2D1EllipseGeometry(iface);
3799 TRACE("iface %p, ellipse %p.\n", iface, ellipse);
3801 *ellipse = geometry->u.ellipse.ellipse;
3804 static const struct ID2D1EllipseGeometryVtbl d2d_ellipse_geometry_vtbl =
3806 d2d_ellipse_geometry_QueryInterface,
3807 d2d_ellipse_geometry_AddRef,
3808 d2d_ellipse_geometry_Release,
3809 d2d_ellipse_geometry_GetFactory,
3810 d2d_ellipse_geometry_GetBounds,
3811 d2d_ellipse_geometry_GetWidenedBounds,
3812 d2d_ellipse_geometry_StrokeContainsPoint,
3813 d2d_ellipse_geometry_FillContainsPoint,
3814 d2d_ellipse_geometry_CompareWithGeometry,
3815 d2d_ellipse_geometry_Simplify,
3816 d2d_ellipse_geometry_Tessellate,
3817 d2d_ellipse_geometry_CombineWithGeometry,
3818 d2d_ellipse_geometry_Outline,
3819 d2d_ellipse_geometry_ComputeArea,
3820 d2d_ellipse_geometry_ComputeLength,
3821 d2d_ellipse_geometry_ComputePointAtLength,
3822 d2d_ellipse_geometry_Widen,
3823 d2d_ellipse_geometry_GetEllipse,
3826 HRESULT d2d_ellipse_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory, const D2D1_ELLIPSE *ellipse)
3828 D2D1_POINT_2F *v, v1, v2, v3, v4;
3829 struct d2d_face *f;
3830 float l, r, t, b;
3832 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_ellipse_geometry_vtbl);
3833 geometry->u.ellipse.ellipse = *ellipse;
3835 if (!(geometry->fill.vertices = heap_alloc(4 * sizeof(*geometry->fill.vertices))))
3836 goto fail;
3837 if (!d2d_array_reserve((void **)&geometry->fill.faces,
3838 &geometry->fill.faces_size, 2, sizeof(*geometry->fill.faces)))
3839 goto fail;
3841 l = ellipse->point.x - ellipse->radiusX;
3842 r = ellipse->point.x + ellipse->radiusX;
3843 t = ellipse->point.y - ellipse->radiusY;
3844 b = ellipse->point.y + ellipse->radiusY;
3846 d2d_point_set(&v1, r, t);
3847 d2d_point_set(&v2, r, b);
3848 d2d_point_set(&v3, l, b);
3849 d2d_point_set(&v4, l, t);
3851 v = geometry->fill.vertices;
3852 d2d_point_set(&v[0], ellipse->point.x, t);
3853 d2d_point_set(&v[1], r, ellipse->point.y);
3854 d2d_point_set(&v[2], ellipse->point.x, b);
3855 d2d_point_set(&v[3], l, ellipse->point.y);
3856 geometry->fill.vertex_count = 4;
3858 f = geometry->fill.faces;
3859 d2d_face_set(&f[0], 0, 3, 2);
3860 d2d_face_set(&f[1], 0, 2, 1);
3861 geometry->fill.face_count = 2;
3863 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[0], &v1, &v[1]))
3864 goto fail;
3865 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[1], &v2, &v[2]))
3866 goto fail;
3867 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[2], &v3, &v[3]))
3868 goto fail;
3869 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[3], &v4, &v[0]))
3870 goto fail;
3872 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[0], &v1, &v[1]))
3873 goto fail;
3874 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[1], &v2, &v[2]))
3875 goto fail;
3876 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[2], &v3, &v[3]))
3877 goto fail;
3878 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[3], &v4, &v[0]))
3879 goto fail;
3881 return S_OK;
3883 fail:
3884 d2d_geometry_cleanup(geometry);
3885 return E_OUTOFMEMORY;
3888 static inline struct d2d_geometry *impl_from_ID2D1RectangleGeometry(ID2D1RectangleGeometry *iface)
3890 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3893 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_QueryInterface(ID2D1RectangleGeometry *iface,
3894 REFIID iid, void **out)
3896 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3898 if (IsEqualGUID(iid, &IID_ID2D1RectangleGeometry)
3899 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3900 || IsEqualGUID(iid, &IID_ID2D1Resource)
3901 || IsEqualGUID(iid, &IID_IUnknown))
3903 ID2D1RectangleGeometry_AddRef(iface);
3904 *out = iface;
3905 return S_OK;
3908 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3910 *out = NULL;
3911 return E_NOINTERFACE;
3914 static ULONG STDMETHODCALLTYPE d2d_rectangle_geometry_AddRef(ID2D1RectangleGeometry *iface)
3916 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3917 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3919 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3921 return refcount;
3924 static ULONG STDMETHODCALLTYPE d2d_rectangle_geometry_Release(ID2D1RectangleGeometry *iface)
3926 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3927 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3929 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3931 if (!refcount)
3933 d2d_geometry_cleanup(geometry);
3934 heap_free(geometry);
3937 return refcount;
3940 static void STDMETHODCALLTYPE d2d_rectangle_geometry_GetFactory(ID2D1RectangleGeometry *iface, ID2D1Factory **factory)
3942 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3944 TRACE("iface %p, factory %p.\n", iface, factory);
3946 ID2D1Factory_AddRef(*factory = geometry->factory);
3949 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_GetBounds(ID2D1RectangleGeometry *iface,
3950 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3952 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3953 D2D1_RECT_F *rect;
3954 D2D1_POINT_2F p;
3956 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3958 rect = &geometry->u.rectangle.rect;
3959 if (!transform)
3961 *bounds = *rect;
3962 return S_OK;
3965 bounds->left = FLT_MAX;
3966 bounds->top = FLT_MAX;
3967 bounds->right = -FLT_MAX;
3968 bounds->bottom = -FLT_MAX;
3970 d2d_point_transform(&p, transform, rect->left, rect->top);
3971 d2d_rect_expand(bounds, &p);
3972 d2d_point_transform(&p, transform, rect->left, rect->bottom);
3973 d2d_rect_expand(bounds, &p);
3974 d2d_point_transform(&p, transform, rect->right, rect->bottom);
3975 d2d_rect_expand(bounds, &p);
3976 d2d_point_transform(&p, transform, rect->right, rect->top);
3977 d2d_rect_expand(bounds, &p);
3979 return S_OK;
3982 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_GetWidenedBounds(ID2D1RectangleGeometry *iface,
3983 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3984 float tolerance, D2D1_RECT_F *bounds)
3986 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3987 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3989 return E_NOTIMPL;
3992 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_StrokeContainsPoint(ID2D1RectangleGeometry *iface,
3993 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3994 float tolerance, BOOL *contains)
3996 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p stub!\n",
3997 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
3999 return E_NOTIMPL;
4002 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_FillContainsPoint(ID2D1RectangleGeometry *iface,
4003 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4005 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
4006 D2D1_RECT_F *rect = &geometry->u.rectangle.rect;
4007 float dx, dy;
4009 TRACE("iface %p, point %s, transform %p, tolerance %.8e, contains %p.\n",
4010 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
4012 if (transform)
4014 D2D1_MATRIX_3X2_F g_i;
4016 if (!d2d_matrix_invert(&g_i, transform))
4017 return D2DERR_UNSUPPORTED_OPERATION;
4018 d2d_point_transform(&point, &g_i, point.x, point.y);
4021 if (tolerance == 0.0f)
4022 tolerance = D2D1_DEFAULT_FLATTENING_TOLERANCE;
4024 dx = max(fabsf((rect->right + rect->left) / 2.0f - point.x) - (rect->right - rect->left) / 2.0f, 0.0f);
4025 dy = max(fabsf((rect->bottom + rect->top) / 2.0f - point.y) - (rect->bottom - rect->top) / 2.0f, 0.0f);
4027 *contains = tolerance * tolerance > (dx * dx + dy * dy);
4028 return S_OK;
4031 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_CompareWithGeometry(ID2D1RectangleGeometry *iface,
4032 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
4034 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
4035 iface, geometry, transform, tolerance, relation);
4037 return E_NOTIMPL;
4040 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Simplify(ID2D1RectangleGeometry *iface,
4041 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4042 ID2D1SimplifiedGeometrySink *sink)
4044 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
4045 D2D1_RECT_F *rect = &geometry->u.rectangle.rect;
4046 D2D1_POINT_2F p[4];
4047 unsigned int i;
4049 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
4050 iface, option, transform, tolerance, sink);
4052 d2d_point_set(&p[0], rect->left, rect->top);
4053 d2d_point_set(&p[1], rect->right, rect->top);
4054 d2d_point_set(&p[2], rect->right, rect->bottom);
4055 d2d_point_set(&p[3], rect->left, rect->bottom);
4057 if (transform)
4059 for (i = 0; i < ARRAY_SIZE(p); ++i)
4061 d2d_point_transform(&p[i], transform, p[i].x, p[i].y);
4065 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_ALTERNATE);
4066 ID2D1SimplifiedGeometrySink_BeginFigure(sink, p[0], D2D1_FIGURE_BEGIN_FILLED);
4067 ID2D1SimplifiedGeometrySink_AddLines(sink, &p[1], 3);
4068 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
4070 return S_OK;
4073 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Tessellate(ID2D1RectangleGeometry *iface,
4074 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
4076 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4078 return E_NOTIMPL;
4081 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_CombineWithGeometry(ID2D1RectangleGeometry *iface,
4082 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
4083 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4085 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4086 iface, geometry, combine_mode, transform, tolerance, sink);
4088 return E_NOTIMPL;
4091 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Outline(ID2D1RectangleGeometry *iface,
4092 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4094 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4096 return E_NOTIMPL;
4099 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputeArea(ID2D1RectangleGeometry *iface,
4100 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4102 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4104 return E_NOTIMPL;
4107 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputeLength(ID2D1RectangleGeometry *iface,
4108 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4110 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4112 return E_NOTIMPL;
4115 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputePointAtLength(ID2D1RectangleGeometry *iface,
4116 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
4117 D2D1_POINT_2F *tangent)
4119 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4120 iface, length, transform, tolerance, point, tangent);
4122 return E_NOTIMPL;
4125 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Widen(ID2D1RectangleGeometry *iface, float stroke_width,
4126 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4127 ID2D1SimplifiedGeometrySink *sink)
4129 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4130 iface, stroke_width, stroke_style, transform, tolerance, sink);
4132 return E_NOTIMPL;
4135 static void STDMETHODCALLTYPE d2d_rectangle_geometry_GetRect(ID2D1RectangleGeometry *iface, D2D1_RECT_F *rect)
4137 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
4139 TRACE("iface %p, rect %p.\n", iface, rect);
4141 *rect = geometry->u.rectangle.rect;
4144 static const struct ID2D1RectangleGeometryVtbl d2d_rectangle_geometry_vtbl =
4146 d2d_rectangle_geometry_QueryInterface,
4147 d2d_rectangle_geometry_AddRef,
4148 d2d_rectangle_geometry_Release,
4149 d2d_rectangle_geometry_GetFactory,
4150 d2d_rectangle_geometry_GetBounds,
4151 d2d_rectangle_geometry_GetWidenedBounds,
4152 d2d_rectangle_geometry_StrokeContainsPoint,
4153 d2d_rectangle_geometry_FillContainsPoint,
4154 d2d_rectangle_geometry_CompareWithGeometry,
4155 d2d_rectangle_geometry_Simplify,
4156 d2d_rectangle_geometry_Tessellate,
4157 d2d_rectangle_geometry_CombineWithGeometry,
4158 d2d_rectangle_geometry_Outline,
4159 d2d_rectangle_geometry_ComputeArea,
4160 d2d_rectangle_geometry_ComputeLength,
4161 d2d_rectangle_geometry_ComputePointAtLength,
4162 d2d_rectangle_geometry_Widen,
4163 d2d_rectangle_geometry_GetRect,
4166 HRESULT d2d_rectangle_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory, const D2D1_RECT_F *rect)
4168 struct d2d_face *f;
4169 D2D1_POINT_2F *v;
4170 float l, r, t, b;
4172 static const D2D1_POINT_2F prev[] =
4174 { 1.0f, 0.0f},
4175 { 0.0f, -1.0f},
4176 {-1.0f, 0.0f},
4177 { 0.0f, 1.0f},
4179 static const D2D1_POINT_2F next[] =
4181 { 0.0f, 1.0f},
4182 { 1.0f, 0.0f},
4183 { 0.0f, -1.0f},
4184 {-1.0f, 0.0f},
4187 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl);
4188 geometry->u.rectangle.rect = *rect;
4190 if (!(geometry->fill.vertices = heap_alloc(4 * sizeof(*geometry->fill.vertices))))
4191 goto fail;
4192 if (!d2d_array_reserve((void **)&geometry->fill.faces,
4193 &geometry->fill.faces_size, 2, sizeof(*geometry->fill.faces)))
4194 goto fail;
4196 l = min(rect->left, rect->right);
4197 r = max(rect->left, rect->right);
4198 t = min(rect->top, rect->bottom);
4199 b = max(rect->top, rect->bottom);
4201 v = geometry->fill.vertices;
4202 d2d_point_set(&v[0], l, t);
4203 d2d_point_set(&v[1], l, b);
4204 d2d_point_set(&v[2], r, b);
4205 d2d_point_set(&v[3], r, t);
4206 geometry->fill.vertex_count = 4;
4208 f = geometry->fill.faces;
4209 d2d_face_set(&f[0], 1, 2, 0);
4210 d2d_face_set(&f[1], 0, 2, 3);
4211 geometry->fill.face_count = 2;
4213 if (!d2d_geometry_outline_add_line_segment(geometry, &v[0], &v[1]))
4214 goto fail;
4215 if (!d2d_geometry_outline_add_line_segment(geometry, &v[1], &v[2]))
4216 goto fail;
4217 if (!d2d_geometry_outline_add_line_segment(geometry, &v[2], &v[3]))
4218 goto fail;
4219 if (!d2d_geometry_outline_add_line_segment(geometry, &v[3], &v[0]))
4220 goto fail;
4222 if (!d2d_geometry_outline_add_join(geometry, &prev[0], &v[0], &next[0]))
4223 goto fail;
4224 if (!d2d_geometry_outline_add_join(geometry, &prev[1], &v[1], &next[1]))
4225 goto fail;
4226 if (!d2d_geometry_outline_add_join(geometry, &prev[2], &v[2], &next[2]))
4227 goto fail;
4228 if (!d2d_geometry_outline_add_join(geometry, &prev[3], &v[3], &next[3]))
4229 goto fail;
4231 return S_OK;
4233 fail:
4234 d2d_geometry_cleanup(geometry);
4235 return E_OUTOFMEMORY;
4238 static inline struct d2d_geometry *impl_from_ID2D1RoundedRectangleGeometry(ID2D1RoundedRectangleGeometry *iface)
4240 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
4243 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_QueryInterface(ID2D1RoundedRectangleGeometry *iface,
4244 REFIID iid, void **out)
4246 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
4248 if (IsEqualGUID(iid, &IID_ID2D1RoundedRectangleGeometry)
4249 || IsEqualGUID(iid, &IID_ID2D1Geometry)
4250 || IsEqualGUID(iid, &IID_ID2D1Resource)
4251 || IsEqualGUID(iid, &IID_IUnknown))
4253 ID2D1RoundedRectangleGeometry_AddRef(iface);
4254 *out = iface;
4255 return S_OK;
4258 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
4260 *out = NULL;
4261 return E_NOINTERFACE;
4264 static ULONG STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_AddRef(ID2D1RoundedRectangleGeometry *iface)
4266 struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
4267 ULONG refcount = InterlockedIncrement(&geometry->refcount);
4269 TRACE("%p increasing refcount to %u.\n", iface, refcount);
4271 return refcount;
4274 static ULONG STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Release(ID2D1RoundedRectangleGeometry *iface)
4276 struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
4277 ULONG refcount = InterlockedDecrement(&geometry->refcount);
4279 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
4281 if (!refcount)
4283 d2d_geometry_cleanup(geometry);
4284 heap_free(geometry);
4287 return refcount;
4290 static void STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetFactory(ID2D1RoundedRectangleGeometry *iface,
4291 ID2D1Factory **factory)
4293 struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
4295 TRACE("iface %p, factory %p.\n", iface, factory);
4297 ID2D1Factory_AddRef(*factory = geometry->factory);
4300 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetBounds(ID2D1RoundedRectangleGeometry *iface,
4301 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
4303 FIXME("iface %p, transform %p, bounds %p stub!\n", iface, transform, bounds);
4305 return E_NOTIMPL;
4308 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetWidenedBounds(ID2D1RoundedRectangleGeometry *iface,
4309 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4310 float tolerance, D2D1_RECT_F *bounds)
4312 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
4313 iface, stroke_width, stroke_style, transform, tolerance, bounds);
4315 return E_NOTIMPL;
4318 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_StrokeContainsPoint(
4319 ID2D1RoundedRectangleGeometry *iface, D2D1_POINT_2F point, float stroke_width,
4320 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4322 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p stub!\n",
4323 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
4325 return E_NOTIMPL;
4328 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_FillContainsPoint(ID2D1RoundedRectangleGeometry *iface,
4329 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4331 FIXME("iface %p, point %s, transform %p, tolerance %.8e, contains %p stub!\n",
4332 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
4334 return E_NOTIMPL;
4337 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_CompareWithGeometry(
4338 ID2D1RoundedRectangleGeometry *iface, ID2D1Geometry *geometry,
4339 const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
4341 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
4342 iface, geometry, transform, tolerance, relation);
4344 return E_NOTIMPL;
4347 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Simplify(ID2D1RoundedRectangleGeometry *iface,
4348 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4349 ID2D1SimplifiedGeometrySink *sink)
4351 FIXME("iface %p, option %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4352 iface, option, transform, tolerance, sink);
4354 return E_NOTIMPL;
4357 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Tessellate(ID2D1RoundedRectangleGeometry *iface,
4358 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
4360 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4362 return E_NOTIMPL;
4365 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_CombineWithGeometry(
4366 ID2D1RoundedRectangleGeometry *iface, ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode,
4367 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4369 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4370 iface, geometry, combine_mode, transform, tolerance, sink);
4372 return E_NOTIMPL;
4375 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Outline(ID2D1RoundedRectangleGeometry *iface,
4376 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4378 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4380 return E_NOTIMPL;
4383 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputeArea(ID2D1RoundedRectangleGeometry *iface,
4384 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4386 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4388 return E_NOTIMPL;
4391 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputeLength(ID2D1RoundedRectangleGeometry *iface,
4392 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4394 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4396 return E_NOTIMPL;
4399 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputePointAtLength(
4400 ID2D1RoundedRectangleGeometry *iface, float length, const D2D1_MATRIX_3X2_F *transform,
4401 float tolerance, D2D1_POINT_2F *point, D2D1_POINT_2F *tangent)
4403 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4404 iface, length, transform, tolerance, point, tangent);
4406 return E_NOTIMPL;
4409 static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Widen(ID2D1RoundedRectangleGeometry *iface,
4410 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4411 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4413 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4414 iface, stroke_width, stroke_style, transform, tolerance, sink);
4416 return E_NOTIMPL;
4419 static void STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetRoundedRect(ID2D1RoundedRectangleGeometry *iface,
4420 D2D1_ROUNDED_RECT *rounded_rect)
4422 struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
4424 TRACE("iface %p, rounded_rect %p.\n", iface, rounded_rect);
4426 *rounded_rect = geometry->u.rounded_rectangle.rounded_rect;
4429 static const struct ID2D1RoundedRectangleGeometryVtbl d2d_rounded_rectangle_geometry_vtbl =
4431 d2d_rounded_rectangle_geometry_QueryInterface,
4432 d2d_rounded_rectangle_geometry_AddRef,
4433 d2d_rounded_rectangle_geometry_Release,
4434 d2d_rounded_rectangle_geometry_GetFactory,
4435 d2d_rounded_rectangle_geometry_GetBounds,
4436 d2d_rounded_rectangle_geometry_GetWidenedBounds,
4437 d2d_rounded_rectangle_geometry_StrokeContainsPoint,
4438 d2d_rounded_rectangle_geometry_FillContainsPoint,
4439 d2d_rounded_rectangle_geometry_CompareWithGeometry,
4440 d2d_rounded_rectangle_geometry_Simplify,
4441 d2d_rounded_rectangle_geometry_Tessellate,
4442 d2d_rounded_rectangle_geometry_CombineWithGeometry,
4443 d2d_rounded_rectangle_geometry_Outline,
4444 d2d_rounded_rectangle_geometry_ComputeArea,
4445 d2d_rounded_rectangle_geometry_ComputeLength,
4446 d2d_rounded_rectangle_geometry_ComputePointAtLength,
4447 d2d_rounded_rectangle_geometry_Widen,
4448 d2d_rounded_rectangle_geometry_GetRoundedRect,
4451 HRESULT d2d_rounded_rectangle_geometry_init(struct d2d_geometry *geometry,
4452 ID2D1Factory *factory, const D2D1_ROUNDED_RECT *rounded_rect)
4454 D2D1_POINT_2F *v, v1, v2, v3, v4;
4455 struct d2d_face *f;
4456 float l, r, t, b;
4457 float rx, ry;
4459 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_rounded_rectangle_geometry_vtbl);
4460 geometry->u.rounded_rectangle.rounded_rect = *rounded_rect;
4462 if (!(geometry->fill.vertices = heap_alloc(8 * sizeof(*geometry->fill.vertices))))
4463 goto fail;
4464 if (!d2d_array_reserve((void **)&geometry->fill.faces,
4465 &geometry->fill.faces_size, 6, sizeof(*geometry->fill.faces)))
4466 goto fail;
4468 l = min(rounded_rect->rect.left, rounded_rect->rect.right);
4469 r = max(rounded_rect->rect.left, rounded_rect->rect.right);
4470 t = min(rounded_rect->rect.top, rounded_rect->rect.bottom);
4471 b = max(rounded_rect->rect.top, rounded_rect->rect.bottom);
4473 rx = min(rounded_rect->radiusX, 0.5f * (r - l));
4474 ry = min(rounded_rect->radiusY, 0.5f * (b - t));
4476 d2d_point_set(&v1, r, t);
4477 d2d_point_set(&v2, r, b);
4478 d2d_point_set(&v3, l, b);
4479 d2d_point_set(&v4, l, t);
4481 v = geometry->fill.vertices;
4482 d2d_point_set(&v[0], l + rx, t);
4483 d2d_point_set(&v[1], r - rx, t);
4484 d2d_point_set(&v[2], r, t + ry);
4485 d2d_point_set(&v[3], r, b - ry);
4486 d2d_point_set(&v[4], r - rx, b);
4487 d2d_point_set(&v[5], l + rx, b);
4488 d2d_point_set(&v[6], l, b - ry);
4489 d2d_point_set(&v[7], l, t + ry);
4490 geometry->fill.vertex_count = 8;
4492 f = geometry->fill.faces;
4493 d2d_face_set(&f[0], 0, 7, 6);
4494 d2d_face_set(&f[1], 0, 6, 5);
4495 d2d_face_set(&f[2], 0, 5, 4);
4496 d2d_face_set(&f[3], 0, 4, 1);
4497 d2d_face_set(&f[4], 1, 4, 3);
4498 d2d_face_set(&f[5], 1, 3, 2);
4499 geometry->fill.face_count = 6;
4501 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[1], &v1, &v[2]))
4502 goto fail;
4503 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[3], &v2, &v[4]))
4504 goto fail;
4505 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[5], &v3, &v[6]))
4506 goto fail;
4507 if (!d2d_geometry_fill_add_arc_triangle(geometry, &v[7], &v4, &v[0]))
4508 goto fail;
4510 if (!d2d_geometry_outline_add_line_segment(geometry, &v[0], &v[1]))
4511 goto fail;
4512 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[1], &v1, &v[2]))
4513 goto fail;
4514 if (!d2d_geometry_outline_add_line_segment(geometry, &v[2], &v[3]))
4515 goto fail;
4516 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[3], &v2, &v[4]))
4517 goto fail;
4518 if (!d2d_geometry_outline_add_line_segment(geometry, &v[4], &v[5]))
4519 goto fail;
4520 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[5], &v3, &v[6]))
4521 goto fail;
4522 if (!d2d_geometry_outline_add_line_segment(geometry, &v[6], &v[7]))
4523 goto fail;
4524 if (!d2d_geometry_outline_add_arc_quadrant(geometry, &v[7], &v4, &v[0]))
4525 goto fail;
4527 return S_OK;
4529 fail:
4530 d2d_geometry_cleanup(geometry);
4531 return E_OUTOFMEMORY;
4534 static inline struct d2d_geometry *impl_from_ID2D1TransformedGeometry(ID2D1TransformedGeometry *iface)
4536 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
4539 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_QueryInterface(ID2D1TransformedGeometry *iface,
4540 REFIID iid, void **out)
4542 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
4544 if (IsEqualGUID(iid, &IID_ID2D1TransformedGeometry)
4545 || IsEqualGUID(iid, &IID_ID2D1Geometry)
4546 || IsEqualGUID(iid, &IID_ID2D1Resource)
4547 || IsEqualGUID(iid, &IID_IUnknown))
4549 ID2D1TransformedGeometry_AddRef(iface);
4550 *out = iface;
4551 return S_OK;
4554 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
4556 *out = NULL;
4557 return E_NOINTERFACE;
4560 static ULONG STDMETHODCALLTYPE d2d_transformed_geometry_AddRef(ID2D1TransformedGeometry *iface)
4562 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4563 ULONG refcount = InterlockedIncrement(&geometry->refcount);
4565 TRACE("%p increasing refcount to %u.\n", iface, refcount);
4567 return refcount;
4570 static ULONG STDMETHODCALLTYPE d2d_transformed_geometry_Release(ID2D1TransformedGeometry *iface)
4572 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4573 ULONG refcount = InterlockedDecrement(&geometry->refcount);
4575 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
4577 if (!refcount)
4579 geometry->outline.arc_faces = NULL;
4580 geometry->outline.arcs = NULL;
4581 geometry->outline.bezier_faces = NULL;
4582 geometry->outline.beziers = NULL;
4583 geometry->outline.faces = NULL;
4584 geometry->outline.vertices = NULL;
4585 geometry->fill.arc_vertices = NULL;
4586 geometry->fill.bezier_vertices = NULL;
4587 geometry->fill.faces = NULL;
4588 geometry->fill.vertices = NULL;
4589 ID2D1Geometry_Release(geometry->u.transformed.src_geometry);
4590 d2d_geometry_cleanup(geometry);
4591 heap_free(geometry);
4594 return refcount;
4597 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetFactory(ID2D1TransformedGeometry *iface,
4598 ID2D1Factory **factory)
4600 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4602 TRACE("iface %p, factory %p.\n", iface, factory);
4604 ID2D1Factory_AddRef(*factory = geometry->factory);
4607 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_GetBounds(ID2D1TransformedGeometry *iface,
4608 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
4610 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4611 D2D1_MATRIX_3X2_F g;
4613 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
4615 g = geometry->transform;
4616 if (transform)
4617 d2d_matrix_multiply(&g, transform);
4619 return ID2D1Geometry_GetBounds(geometry->u.transformed.src_geometry, &g, bounds);
4622 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_GetWidenedBounds(ID2D1TransformedGeometry *iface,
4623 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4624 float tolerance, D2D1_RECT_F *bounds)
4626 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
4627 iface, stroke_width, stroke_style, transform, tolerance, bounds);
4629 return E_NOTIMPL;
4632 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_StrokeContainsPoint(ID2D1TransformedGeometry *iface,
4633 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4634 float tolerance, BOOL *contains)
4636 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4637 D2D1_MATRIX_3X2_F g;
4639 TRACE("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p.\n",
4640 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
4642 g = geometry->transform;
4643 if (transform)
4644 d2d_matrix_multiply(&g, transform);
4646 return ID2D1Geometry_StrokeContainsPoint(geometry->u.transformed.src_geometry, point, stroke_width, stroke_style,
4647 &g, tolerance, contains);
4650 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_FillContainsPoint(ID2D1TransformedGeometry *iface,
4651 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4653 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4654 D2D1_MATRIX_3X2_F g;
4656 TRACE("iface %p, point %s, transform %p, tolerance %.8e, contains %p.\n",
4657 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
4659 g = geometry->transform;
4660 if (transform)
4661 d2d_matrix_multiply(&g, transform);
4663 return ID2D1Geometry_FillContainsPoint(geometry->u.transformed.src_geometry, point, &g, tolerance, contains);
4666 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_CompareWithGeometry(ID2D1TransformedGeometry *iface,
4667 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
4669 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
4670 iface, geometry, transform, tolerance, relation);
4672 return E_NOTIMPL;
4675 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Simplify(ID2D1TransformedGeometry *iface,
4676 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4677 ID2D1SimplifiedGeometrySink *sink)
4679 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4680 D2D1_MATRIX_3X2_F g;
4682 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
4683 iface, option, transform, tolerance, sink);
4685 g = geometry->transform;
4686 if (transform)
4687 d2d_matrix_multiply(&g, transform);
4689 return ID2D1Geometry_Simplify(geometry->u.transformed.src_geometry, option, &g, tolerance, sink);
4692 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Tessellate(ID2D1TransformedGeometry *iface,
4693 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
4695 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4697 return E_NOTIMPL;
4700 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_CombineWithGeometry(ID2D1TransformedGeometry *iface,
4701 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
4702 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4704 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4705 iface, geometry, combine_mode, transform, tolerance, sink);
4707 return E_NOTIMPL;
4710 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Outline(ID2D1TransformedGeometry *iface,
4711 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4713 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4715 return E_NOTIMPL;
4718 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputeArea(ID2D1TransformedGeometry *iface,
4719 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4721 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4723 return E_NOTIMPL;
4726 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputeLength(ID2D1TransformedGeometry *iface,
4727 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4729 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4731 return E_NOTIMPL;
4734 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputePointAtLength(ID2D1TransformedGeometry *iface,
4735 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
4736 D2D1_POINT_2F *tangent)
4738 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4739 iface, length, transform, tolerance, point, tangent);
4741 return E_NOTIMPL;
4744 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Widen(ID2D1TransformedGeometry *iface, float stroke_width,
4745 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4746 ID2D1SimplifiedGeometrySink *sink)
4748 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4749 iface, stroke_width, stroke_style, transform, tolerance, sink);
4751 return E_NOTIMPL;
4754 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetSourceGeometry(ID2D1TransformedGeometry *iface,
4755 ID2D1Geometry **src_geometry)
4757 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4759 TRACE("iface %p, src_geometry %p.\n", iface, src_geometry);
4761 ID2D1Geometry_AddRef(*src_geometry = geometry->u.transformed.src_geometry);
4764 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetTransform(ID2D1TransformedGeometry *iface,
4765 D2D1_MATRIX_3X2_F *transform)
4767 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4769 TRACE("iface %p, transform %p.\n", iface, transform);
4771 *transform = geometry->u.transformed.transform;
4774 static const struct ID2D1TransformedGeometryVtbl d2d_transformed_geometry_vtbl =
4776 d2d_transformed_geometry_QueryInterface,
4777 d2d_transformed_geometry_AddRef,
4778 d2d_transformed_geometry_Release,
4779 d2d_transformed_geometry_GetFactory,
4780 d2d_transformed_geometry_GetBounds,
4781 d2d_transformed_geometry_GetWidenedBounds,
4782 d2d_transformed_geometry_StrokeContainsPoint,
4783 d2d_transformed_geometry_FillContainsPoint,
4784 d2d_transformed_geometry_CompareWithGeometry,
4785 d2d_transformed_geometry_Simplify,
4786 d2d_transformed_geometry_Tessellate,
4787 d2d_transformed_geometry_CombineWithGeometry,
4788 d2d_transformed_geometry_Outline,
4789 d2d_transformed_geometry_ComputeArea,
4790 d2d_transformed_geometry_ComputeLength,
4791 d2d_transformed_geometry_ComputePointAtLength,
4792 d2d_transformed_geometry_Widen,
4793 d2d_transformed_geometry_GetSourceGeometry,
4794 d2d_transformed_geometry_GetTransform,
4797 void d2d_transformed_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
4798 ID2D1Geometry *src_geometry, const D2D_MATRIX_3X2_F *transform)
4800 struct d2d_geometry *src_impl;
4801 D2D_MATRIX_3X2_F g;
4803 src_impl = unsafe_impl_from_ID2D1Geometry(src_geometry);
4805 g = src_impl->transform;
4806 d2d_matrix_multiply(&g, transform);
4807 d2d_geometry_init(geometry, factory, &g, (ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl);
4808 ID2D1Geometry_AddRef(geometry->u.transformed.src_geometry = src_geometry);
4809 geometry->u.transformed.transform = *transform;
4810 geometry->fill = src_impl->fill;
4811 geometry->outline = src_impl->outline;
4814 static inline struct d2d_geometry *impl_from_ID2D1GeometryGroup(ID2D1GeometryGroup *iface)
4816 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
4819 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_QueryInterface(ID2D1GeometryGroup *iface,
4820 REFIID iid, void **out)
4822 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
4824 if (IsEqualGUID(iid, &IID_ID2D1GeometryGroup)
4825 || IsEqualGUID(iid, &IID_ID2D1Geometry)
4826 || IsEqualGUID(iid, &IID_ID2D1Resource)
4827 || IsEqualGUID(iid, &IID_IUnknown))
4829 ID2D1GeometryGroup_AddRef(iface);
4830 *out = iface;
4831 return S_OK;
4834 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
4836 *out = NULL;
4837 return E_NOINTERFACE;
4840 static ULONG STDMETHODCALLTYPE d2d_geometry_group_AddRef(ID2D1GeometryGroup *iface)
4842 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4843 ULONG refcount = InterlockedIncrement(&geometry->refcount);
4845 TRACE("%p increasing refcount to %u.\n", iface, refcount);
4847 return refcount;
4850 static ULONG STDMETHODCALLTYPE d2d_geometry_group_Release(ID2D1GeometryGroup *iface)
4852 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4853 ULONG refcount = InterlockedDecrement(&geometry->refcount);
4854 unsigned int i;
4856 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
4858 if (!refcount)
4860 for (i = 0; i < geometry->u.group.geometry_count; ++i)
4861 ID2D1Geometry_Release(geometry->u.group.src_geometries[i]);
4862 heap_free(geometry->u.group.src_geometries);
4863 d2d_geometry_cleanup(geometry);
4864 heap_free(geometry);
4867 return refcount;
4870 static void STDMETHODCALLTYPE d2d_geometry_group_GetFactory(ID2D1GeometryGroup *iface,
4871 ID2D1Factory **factory)
4873 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4875 TRACE("iface %p, factory %p.\n", iface, factory);
4877 ID2D1Factory_AddRef(*factory = geometry->factory);
4880 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_GetBounds(ID2D1GeometryGroup *iface,
4881 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
4883 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
4884 D2D1_RECT_F rect;
4885 unsigned int i;
4887 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
4889 bounds->left = FLT_MAX;
4890 bounds->top = FLT_MAX;
4891 bounds->right = -FLT_MAX;
4892 bounds->bottom = -FLT_MAX;
4894 for (i = 0; i < geometry->u.group.geometry_count; ++i)
4896 if (SUCCEEDED(ID2D1Geometry_GetBounds(geometry->u.group.src_geometries[i], transform, &rect)))
4897 d2d_rect_union(bounds, &rect);
4900 return S_OK;
4903 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_GetWidenedBounds(ID2D1GeometryGroup *iface,
4904 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4905 float tolerance, D2D1_RECT_F *bounds)
4907 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
4908 iface, stroke_width, stroke_style, transform, tolerance, bounds);
4910 return E_NOTIMPL;
4913 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_StrokeContainsPoint(ID2D1GeometryGroup *iface,
4914 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
4915 float tolerance, BOOL *contains)
4917 FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p.\n",
4918 iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
4920 return E_NOTIMPL;
4923 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_FillContainsPoint(ID2D1GeometryGroup *iface,
4924 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
4926 FIXME("iface %p, point %s, transform %p, tolerance %.8e, contains %p stub!.\n",
4927 iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
4929 return E_NOTIMPL;
4932 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_CompareWithGeometry(ID2D1GeometryGroup *iface,
4933 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
4935 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
4936 iface, geometry, transform, tolerance, relation);
4938 return E_NOTIMPL;
4941 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_Simplify(ID2D1GeometryGroup *iface,
4942 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4943 ID2D1SimplifiedGeometrySink *sink)
4945 FIXME("iface %p, option %#x, transform %p, tolerance %.8e, sink %p stub!.\n",
4946 iface, option, transform, tolerance, sink);
4948 return E_NOTIMPL;
4951 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_Tessellate(ID2D1GeometryGroup *iface,
4952 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
4954 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4956 return E_NOTIMPL;
4959 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_CombineWithGeometry(ID2D1GeometryGroup *iface,
4960 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
4961 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4963 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4964 iface, geometry, combine_mode, transform, tolerance, sink);
4966 return E_NOTIMPL;
4969 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_Outline(ID2D1GeometryGroup *iface,
4970 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4972 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4974 return E_NOTIMPL;
4977 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_ComputeArea(ID2D1GeometryGroup *iface,
4978 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4980 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4982 return E_NOTIMPL;
4985 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_ComputeLength(ID2D1GeometryGroup *iface,
4986 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4988 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4990 return E_NOTIMPL;
4993 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_ComputePointAtLength(ID2D1GeometryGroup *iface,
4994 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
4995 D2D1_POINT_2F *tangent)
4997 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4998 iface, length, transform, tolerance, point, tangent);
5000 return E_NOTIMPL;
5003 static HRESULT STDMETHODCALLTYPE d2d_geometry_group_Widen(ID2D1GeometryGroup *iface, float stroke_width,
5004 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
5005 ID2D1SimplifiedGeometrySink *sink)
5007 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
5008 iface, stroke_width, stroke_style, transform, tolerance, sink);
5010 return E_NOTIMPL;
5013 static D2D1_FILL_MODE STDMETHODCALLTYPE d2d_geometry_group_GetFillMode(ID2D1GeometryGroup *iface)
5015 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
5017 TRACE("iface %p.\n", iface);
5019 return geometry->u.group.fill_mode;
5022 static UINT32 STDMETHODCALLTYPE d2d_geometry_group_GetSourceGeometryCount(ID2D1GeometryGroup *iface)
5024 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
5026 TRACE("iface %p.\n", iface);
5028 return geometry->u.group.geometry_count;
5031 static void STDMETHODCALLTYPE d2d_geometry_group_GetSourceGeometries(ID2D1GeometryGroup *iface,
5032 ID2D1Geometry **geometries, UINT32 geometry_count)
5034 struct d2d_geometry *geometry = impl_from_ID2D1GeometryGroup(iface);
5035 unsigned int i;
5037 TRACE("iface %p, geometries %p, geometry_count %u.\n", iface, geometries, geometry_count);
5039 geometry_count = min(geometry_count, geometry->u.group.geometry_count);
5040 for (i = 0; i < geometry_count; ++i)
5041 ID2D1Geometry_AddRef(geometries[i] = geometry->u.group.src_geometries[i]);
5044 static const struct ID2D1GeometryGroupVtbl d2d_geometry_group_vtbl =
5046 d2d_geometry_group_QueryInterface,
5047 d2d_geometry_group_AddRef,
5048 d2d_geometry_group_Release,
5049 d2d_geometry_group_GetFactory,
5050 d2d_geometry_group_GetBounds,
5051 d2d_geometry_group_GetWidenedBounds,
5052 d2d_geometry_group_StrokeContainsPoint,
5053 d2d_geometry_group_FillContainsPoint,
5054 d2d_geometry_group_CompareWithGeometry,
5055 d2d_geometry_group_Simplify,
5056 d2d_geometry_group_Tessellate,
5057 d2d_geometry_group_CombineWithGeometry,
5058 d2d_geometry_group_Outline,
5059 d2d_geometry_group_ComputeArea,
5060 d2d_geometry_group_ComputeLength,
5061 d2d_geometry_group_ComputePointAtLength,
5062 d2d_geometry_group_Widen,
5063 d2d_geometry_group_GetFillMode,
5064 d2d_geometry_group_GetSourceGeometryCount,
5065 d2d_geometry_group_GetSourceGeometries,
5068 HRESULT d2d_geometry_group_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
5069 D2D1_FILL_MODE fill_mode, ID2D1Geometry **geometries, unsigned int geometry_count)
5071 unsigned int i;
5073 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_geometry_group_vtbl);
5075 if (!(geometry->u.group.src_geometries = heap_calloc(geometry_count, sizeof(*geometries))))
5077 d2d_geometry_cleanup(geometry);
5078 return E_OUTOFMEMORY;
5081 for (i = 0; i < geometry_count; ++i)
5083 ID2D1Geometry_AddRef(geometry->u.group.src_geometries[i] = geometries[i]);
5085 geometry->u.group.geometry_count = geometry_count;
5086 geometry->u.group.fill_mode = fill_mode;
5088 return S_OK;
5091 struct d2d_geometry *unsafe_impl_from_ID2D1Geometry(ID2D1Geometry *iface)
5093 if (!iface)
5094 return NULL;
5095 assert(iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_ellipse_geometry_vtbl
5096 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl
5097 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl
5098 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_rounded_rectangle_geometry_vtbl
5099 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl
5100 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_geometry_group_vtbl);
5101 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);