wined3d: Factor out acquire_graphics_pipeline_resources().
[wine.git] / dlls / d2d1 / geometry.c
blobab5a0b59298f5e323720a0f305628931aac2517e
1 /*
2 * Copyright 2015 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include "d2d1_private.h"
23 #ifdef HAVE_FLOAT_H
24 #include <float.h>
25 #endif
27 WINE_DEFAULT_DEBUG_CHANNEL(d2d);
29 #define D2D_FIGURE_FLAG_CLOSED 0x00000001u
30 #define D2D_FIGURE_FLAG_HOLLOW 0x00000002u
32 #define D2D_CDT_EDGE_FLAG_FREED 0x80000000u
33 #define D2D_CDT_EDGE_FLAG_VISITED(r) (1u << (r))
35 #define D2D_FP_EPS (1.0f / (1 << FLT_MANT_DIG))
37 static const D2D1_MATRIX_3X2_F identity =
39 1.0f, 0.0f,
40 0.0f, 1.0f,
41 0.0f, 0.0f,
44 enum d2d_cdt_edge_next
46 D2D_EDGE_NEXT_ORIGIN = 0,
47 D2D_EDGE_NEXT_ROT = 1,
48 D2D_EDGE_NEXT_SYM = 2,
49 D2D_EDGE_NEXT_TOR = 3,
52 enum d2d_vertex_type
54 D2D_VERTEX_TYPE_NONE,
55 D2D_VERTEX_TYPE_LINE,
56 D2D_VERTEX_TYPE_BEZIER,
57 D2D_VERTEX_TYPE_SPLIT_BEZIER,
60 struct d2d_segment_idx
62 size_t figure_idx;
63 size_t vertex_idx;
64 size_t control_idx;
67 struct d2d_figure
69 D2D1_POINT_2F *vertices;
70 size_t vertices_size;
71 enum d2d_vertex_type *vertex_types;
72 size_t vertex_types_size;
73 size_t vertex_count;
75 D2D1_POINT_2F *bezier_controls;
76 size_t bezier_controls_size;
77 size_t bezier_control_count;
79 D2D1_POINT_2F *original_bezier_controls;
80 size_t original_bezier_control_count;
82 D2D1_RECT_F bounds;
83 unsigned int flags;
86 struct d2d_cdt_edge_ref
88 size_t idx;
89 enum d2d_cdt_edge_next r;
92 struct d2d_cdt_edge
94 struct d2d_cdt_edge_ref next[4];
95 size_t vertex[2];
96 unsigned int flags;
99 struct d2d_cdt
101 struct d2d_cdt_edge *edges;
102 size_t edges_size;
103 size_t edge_count;
104 size_t free_edge;
106 const D2D1_POINT_2F *vertices;
109 struct d2d_geometry_intersection
111 size_t figure_idx;
112 size_t vertex_idx;
113 size_t control_idx;
114 float t;
115 D2D1_POINT_2F p;
118 struct d2d_geometry_intersections
120 struct d2d_geometry_intersection *intersections;
121 size_t intersections_size;
122 size_t intersection_count;
125 struct d2d_fp_two_vec2
127 float x[2];
128 float y[2];
131 struct d2d_fp_fin
133 float *now, *other;
134 size_t length;
137 static void d2d_bezier_vertex_set(struct d2d_bezier_vertex *b,
138 const D2D1_POINT_2F *p, float u, float v, float sign)
140 b->position = *p;
141 b->texcoord.u = u;
142 b->texcoord.v = v;
143 b->texcoord.sign = sign;
146 static void d2d_face_set(struct d2d_face *f, UINT16 v0, UINT16 v1, UINT16 v2)
148 f->v[0] = v0;
149 f->v[1] = v1;
150 f->v[2] = v2;
153 static void d2d_outline_vertex_set(struct d2d_outline_vertex *v, float x, float y,
154 float prev_x, float prev_y, float next_x, float next_y)
156 d2d_point_set(&v->position, x, y);
157 d2d_point_set(&v->prev, prev_x, prev_y);
158 d2d_point_set(&v->next, next_x, next_y);
161 static void d2d_bezier_outline_vertex_set(struct d2d_bezier_outline_vertex *b, const D2D1_POINT_2F *position,
162 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2,
163 float prev_x, float prev_y, float next_x, float next_y)
165 b->position = *position;
166 b->p0 = *p0;
167 b->p1 = *p1;
168 b->p2 = *p2;
169 d2d_point_set(&b->prev, prev_x, prev_y);
170 d2d_point_set(&b->next, next_x, next_y);
173 static void d2d_fp_two_sum(float *out, float a, float b)
175 float a_virt, a_round, b_virt, b_round;
177 out[1] = a + b;
178 b_virt = out[1] - a;
179 a_virt = out[1] - b_virt;
180 b_round = b - b_virt;
181 a_round = a - a_virt;
182 out[0] = a_round + b_round;
185 static void d2d_fp_fast_two_sum(float *out, float a, float b)
187 float b_virt;
189 out[1] = a + b;
190 b_virt = out[1] - a;
191 out[0] = b - b_virt;
194 static void d2d_fp_two_two_sum(float *out, const float *a, const float *b)
196 float sum[2];
198 d2d_fp_two_sum(out, a[0], b[0]);
199 d2d_fp_two_sum(sum, a[1], out[1]);
200 d2d_fp_two_sum(&out[1], sum[0], b[1]);
201 d2d_fp_two_sum(&out[2], sum[1], out[2]);
204 static void d2d_fp_two_diff_tail(float *out, float a, float b, float x)
206 float a_virt, a_round, b_virt, b_round;
208 b_virt = a - x;
209 a_virt = x + b_virt;
210 b_round = b_virt - b;
211 a_round = a - a_virt;
212 *out = a_round + b_round;
215 static void d2d_fp_two_two_diff(float *out, const float *a, const float *b)
217 float sum[2], diff;
219 diff = a[0] - b[0];
220 d2d_fp_two_diff_tail(out, a[0], b[0], diff);
221 d2d_fp_two_sum(sum, a[1], diff);
222 diff = sum[0] - b[1];
223 d2d_fp_two_diff_tail(&out[1], sum[0], b[1], diff);
224 d2d_fp_two_sum(&out[2], sum[1], diff);
227 static void d2d_fp_split(float *out, float a)
229 float a_big, c;
231 c = a * ((1 << (FLT_MANT_DIG / 2)) + 1.0f);
232 a_big = c - a;
233 out[1] = c - a_big;
234 out[0] = a - out[1];
237 static void d2d_fp_two_product_presplit(float *out, float a, float b, const float *b_split)
239 float a_split[2], err1, err2, err3;
241 out[1] = a * b;
242 d2d_fp_split(a_split, a);
243 err1 = out[1] - (a_split[1] * b_split[1]);
244 err2 = err1 - (a_split[0] * b_split[1]);
245 err3 = err2 - (a_split[1] * b_split[0]);
246 out[0] = (a_split[0] * b_split[0]) - err3;
249 static void d2d_fp_two_product(float *out, float a, float b)
251 float b_split[2];
253 d2d_fp_split(b_split, b);
254 d2d_fp_two_product_presplit(out, a, b, b_split);
257 static void d2d_fp_square(float *out, float a)
259 float a_split[2], err1, err2;
261 out[1] = a * a;
262 d2d_fp_split(a_split, a);
263 err1 = out[1] - (a_split[1] * a_split[1]);
264 err2 = err1 - ((a_split[1] + a_split[1]) * a_split[0]);
265 out[0] = (a_split[0] * a_split[0]) - err2;
268 static float d2d_fp_estimate(float *a, size_t len)
270 float out = a[0];
271 size_t idx = 1;
273 while (idx < len)
274 out += a[idx++];
276 return out;
279 static void d2d_fp_fast_expansion_sum_zeroelim(float *out, size_t *out_len,
280 const float *a, size_t a_len, const float *b, size_t b_len)
282 float sum[2], q, a_curr, b_curr;
283 size_t a_idx, b_idx, out_idx;
285 a_curr = a[0];
286 b_curr = b[0];
287 a_idx = b_idx = 0;
288 if ((b_curr > a_curr) == (b_curr > -a_curr))
290 q = a_curr;
291 a_curr = a[++a_idx];
293 else
295 q = b_curr;
296 b_curr = b[++b_idx];
298 out_idx = 0;
299 if (a_idx < a_len && b_idx < b_len)
301 if ((b_curr > a_curr) == (b_curr > -a_curr))
303 d2d_fp_fast_two_sum(sum, a_curr, q);
304 a_curr = a[++a_idx];
306 else
308 d2d_fp_fast_two_sum(sum, b_curr, q);
309 b_curr = b[++b_idx];
311 if (sum[0] != 0.0f)
312 out[out_idx++] = sum[0];
313 q = sum[1];
314 while (a_idx < a_len && b_idx < b_len)
316 if ((b_curr > a_curr) == (b_curr > -a_curr))
318 d2d_fp_two_sum(sum, q, a_curr);
319 a_curr = a[++a_idx];
321 else
323 d2d_fp_two_sum(sum, q, b_curr);
324 b_curr = b[++b_idx];
326 if (sum[0] != 0.0f)
327 out[out_idx++] = sum[0];
328 q = sum[1];
331 while (a_idx < a_len)
333 d2d_fp_two_sum(sum, q, a_curr);
334 a_curr = a[++a_idx];
335 if (sum[0] != 0.0f)
336 out[out_idx++] = sum[0];
337 q = sum[1];
339 while (b_idx < b_len)
341 d2d_fp_two_sum(sum, q, b_curr);
342 b_curr = b[++b_idx];
343 if (sum[0] != 0.0f)
344 out[out_idx++] = sum[0];
345 q = sum[1];
347 if (q != 0.0f || !out_idx)
348 out[out_idx++] = q;
350 *out_len = out_idx;
353 static void d2d_fp_scale_expansion_zeroelim(float *out, size_t *out_len, const float *a, size_t a_len, float b)
355 float product[2], sum[2], b_split[2], q[2], a_curr;
356 size_t a_idx, out_idx;
358 d2d_fp_split(b_split, b);
359 d2d_fp_two_product_presplit(q, a[0], b, b_split);
360 out_idx = 0;
361 if (q[0] != 0.0f)
362 out[out_idx++] = q[0];
363 for (a_idx = 1; a_idx < a_len; ++a_idx)
365 a_curr = a[a_idx];
366 d2d_fp_two_product_presplit(product, a_curr, b, b_split);
367 d2d_fp_two_sum(sum, q[1], product[0]);
368 if (sum[0] != 0.0f)
369 out[out_idx++] = sum[0];
370 d2d_fp_fast_two_sum(q, product[1], sum[1]);
371 if (q[0] != 0.0f)
372 out[out_idx++] = q[0];
374 if (q[1] != 0.0f || !out_idx)
375 out[out_idx++] = q[1];
377 *out_len = out_idx;
380 static void d2d_point_subtract(D2D1_POINT_2F *out,
381 const D2D1_POINT_2F *a, const D2D1_POINT_2F *b)
383 out->x = a->x - b->x;
384 out->y = a->y - b->y;
387 static void d2d_point_scale(D2D1_POINT_2F *p, float scale)
389 p->x *= scale;
390 p->y *= scale;
393 static void d2d_point_lerp(D2D1_POINT_2F *out,
394 const D2D1_POINT_2F *a, const D2D1_POINT_2F *b, float t)
396 out->x = a->x * (1.0f - t) + b->x * t;
397 out->y = a->y * (1.0f - t) + b->y * t;
400 static void d2d_point_calculate_bezier(D2D1_POINT_2F *out, const D2D1_POINT_2F *p0,
401 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float t)
403 float t_c = 1.0f - t;
405 out->x = t_c * (t_c * p0->x + t * p1->x) + t * (t_c * p1->x + t * p2->x);
406 out->y = t_c * (t_c * p0->y + t * p1->y) + t * (t_c * p1->y + t * p2->y);
409 static void d2d_point_normalise(D2D1_POINT_2F *p)
411 float l;
413 if ((l = sqrtf(d2d_point_dot(p, p))) != 0.0f)
414 d2d_point_scale(p, 1.0f / l);
417 /* This implementation is based on the paper "Adaptive Precision
418 * Floating-Point Arithmetic and Fast Robust Geometric Predicates" and
419 * associated (Public Domain) code by Jonathan Richard Shewchuk. */
420 static float d2d_point_ccw(const D2D1_POINT_2F *a, const D2D1_POINT_2F *b, const D2D1_POINT_2F *c)
422 static const float err_bound_result = (3.0f + 8.0f * D2D_FP_EPS) * D2D_FP_EPS;
423 static const float err_bound_a = (3.0f + 16.0f * D2D_FP_EPS) * D2D_FP_EPS;
424 static const float err_bound_b = (2.0f + 12.0f * D2D_FP_EPS) * D2D_FP_EPS;
425 static const float err_bound_c = (9.0f + 64.0f * D2D_FP_EPS) * D2D_FP_EPS * D2D_FP_EPS;
426 float det_d[16], det_c2[12], det_c1[8], det_b[4], temp4[4], temp2a[2], temp2b[2], abxacy[2], abyacx[2];
427 size_t det_d_len, det_c2_len, det_c1_len;
428 float det, det_sum, err_bound;
429 struct d2d_fp_two_vec2 ab, ac;
431 ab.x[1] = b->x - a->x;
432 ab.y[1] = b->y - a->y;
433 ac.x[1] = c->x - a->x;
434 ac.y[1] = c->y - a->y;
436 abxacy[1] = ab.x[1] * ac.y[1];
437 abyacx[1] = ab.y[1] * ac.x[1];
438 det = abxacy[1] - abyacx[1];
440 if (abxacy[1] > 0.0f)
442 if (abyacx[1] <= 0.0f)
443 return det;
444 det_sum = abxacy[1] + abyacx[1];
446 else if (abxacy[1] < 0.0f)
448 if (abyacx[1] >= 0.0f)
449 return det;
450 det_sum = -abxacy[1] - abyacx[1];
452 else
454 return det;
457 err_bound = err_bound_a * det_sum;
458 if (det >= err_bound || -det >= err_bound)
459 return det;
461 d2d_fp_two_product(abxacy, ab.x[1], ac.y[1]);
462 d2d_fp_two_product(abyacx, ab.y[1], ac.x[1]);
463 d2d_fp_two_two_diff(det_b, abxacy, abyacx);
465 det = d2d_fp_estimate(det_b, 4);
466 err_bound = err_bound_b * det_sum;
467 if (det >= err_bound || -det >= err_bound)
468 return det;
470 d2d_fp_two_diff_tail(&ab.x[0], b->x, a->x, ab.x[1]);
471 d2d_fp_two_diff_tail(&ab.y[0], b->y, a->y, ab.y[1]);
472 d2d_fp_two_diff_tail(&ac.x[0], c->x, a->x, ac.x[1]);
473 d2d_fp_two_diff_tail(&ac.y[0], c->y, a->y, ac.y[1]);
475 if (ab.x[0] == 0.0f && ab.y[0] == 0.0f && ac.x[0] == 0.0f && ac.y[0] == 0.0f)
476 return det;
478 err_bound = err_bound_c * det_sum + err_bound_result * fabsf(det);
479 det += (ab.x[1] * ac.y[0] + ac.y[1] * ab.x[0]) - (ab.y[1] * ac.x[0] + ac.x[1] * ab.y[0]);
480 if (det >= err_bound || -det >= err_bound)
481 return det;
483 d2d_fp_two_product(temp2a, ab.x[0], ac.y[1]);
484 d2d_fp_two_product(temp2b, ab.y[0], ac.x[1]);
485 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
486 d2d_fp_fast_expansion_sum_zeroelim(det_c1, &det_c1_len, det_b, 4, temp4, 4);
488 d2d_fp_two_product(temp2a, ab.x[1], ac.y[0]);
489 d2d_fp_two_product(temp2b, ab.y[1], ac.x[0]);
490 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
491 d2d_fp_fast_expansion_sum_zeroelim(det_c2, &det_c2_len, det_c1, det_c1_len, temp4, 4);
493 d2d_fp_two_product(temp2a, ab.x[0], ac.y[0]);
494 d2d_fp_two_product(temp2b, ab.y[0], ac.x[0]);
495 d2d_fp_two_two_diff(temp4, temp2a, temp2b);
496 d2d_fp_fast_expansion_sum_zeroelim(det_d, &det_d_len, det_c2, det_c2_len, temp4, 4);
498 return det_d[det_d_len - 1];
501 static void d2d_rect_union(D2D1_RECT_F *l, const D2D1_RECT_F *r)
503 l->left = min(l->left, r->left);
504 l->top = min(l->top, r->top);
505 l->right = max(l->right, r->right);
506 l->bottom = max(l->bottom, r->bottom);
509 static BOOL d2d_rect_check_overlap(const D2D_RECT_F *p, const D2D_RECT_F *q)
511 return p->left < q->right && p->top < q->bottom && p->right > q->left && p->bottom > q->top;
514 static void d2d_rect_get_bezier_bounds(D2D_RECT_F *bounds, const D2D1_POINT_2F *p0,
515 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
517 D2D1_POINT_2F p;
518 float root;
520 bounds->left = p0->x;
521 bounds->top = p0->y;
522 bounds->right = p0->x;
523 bounds->bottom = p0->y;
525 d2d_rect_expand(bounds, p2);
527 /* f(t) = (1 - t)²P₀ + 2(1 - t)tP₁ + t²P₂
528 * f'(t) = 2(1 - t)(P₁ - P₀) + 2t(P₂ - P₁)
529 * = 2(P₂ - 2P₁ + P₀)t + 2(P₁ - P₀)
531 * f'(t) = 0
532 * t = (P₀ - P₁) / (P₂ - 2P₁ + P₀) */
533 root = (p0->x - p1->x) / (p2->x - 2.0f * p1->x + p0->x);
534 if (root > 0.0f && root < 1.0f)
536 d2d_point_calculate_bezier(&p, p0, p1, p2, root);
537 d2d_rect_expand(bounds, &p);
540 root = (p0->y - p1->y) / (p2->y - 2.0f * p1->y + p0->y);
541 if (root > 0.0f && root < 1.0f)
543 d2d_point_calculate_bezier(&p, p0, p1, p2, root);
544 d2d_rect_expand(bounds, &p);
548 static void d2d_rect_get_bezier_segment_bounds(D2D_RECT_F *bounds, const D2D1_POINT_2F *p0,
549 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float start, float end)
551 D2D1_POINT_2F q[3], r[2];
553 d2d_point_lerp(&r[0], p0, p1, start);
554 d2d_point_lerp(&r[1], p1, p2, start);
555 d2d_point_lerp(&q[0], &r[0], &r[1], start);
557 end = (end - start) / (1.0f - start);
558 d2d_point_lerp(&q[1], &q[0], &r[1], end);
559 d2d_point_lerp(&r[0], &r[1], p2, end);
560 d2d_point_lerp(&q[2], &q[1], &r[0], end);
562 d2d_rect_get_bezier_bounds(bounds, &q[0], &q[1], &q[2]);
565 static BOOL d2d_array_reserve(void **elements, size_t *capacity, size_t element_count, size_t element_size)
567 size_t new_capacity, max_capacity;
568 void *new_elements;
570 if (element_count <= *capacity)
571 return TRUE;
573 max_capacity = ~(size_t)0 / element_size;
574 if (max_capacity < element_count)
575 return FALSE;
577 new_capacity = max(*capacity, 4);
578 while (new_capacity < element_count && new_capacity <= max_capacity / 2)
579 new_capacity *= 2;
581 if (new_capacity < element_count)
582 new_capacity = max_capacity;
584 if (*elements)
585 new_elements = HeapReAlloc(GetProcessHeap(), 0, *elements, new_capacity * element_size);
586 else
587 new_elements = HeapAlloc(GetProcessHeap(), 0, new_capacity * element_size);
589 if (!new_elements)
590 return FALSE;
592 *elements = new_elements;
593 *capacity = new_capacity;
594 return TRUE;
597 static BOOL d2d_figure_insert_vertex(struct d2d_figure *figure, size_t idx, D2D1_POINT_2F vertex)
599 if (!d2d_array_reserve((void **)&figure->vertices, &figure->vertices_size,
600 figure->vertex_count + 1, sizeof(*figure->vertices)))
602 ERR("Failed to grow vertices array.\n");
603 return FALSE;
606 if (!d2d_array_reserve((void **)&figure->vertex_types, &figure->vertex_types_size,
607 figure->vertex_count + 1, sizeof(*figure->vertex_types)))
609 ERR("Failed to grow vertex types array.\n");
610 return FALSE;
613 memmove(&figure->vertices[idx + 1], &figure->vertices[idx],
614 (figure->vertex_count - idx) * sizeof(*figure->vertices));
615 memmove(&figure->vertex_types[idx + 1], &figure->vertex_types[idx],
616 (figure->vertex_count - idx) * sizeof(*figure->vertex_types));
617 figure->vertices[idx] = vertex;
618 figure->vertex_types[idx] = D2D_VERTEX_TYPE_NONE;
619 d2d_rect_expand(&figure->bounds, &vertex);
620 ++figure->vertex_count;
621 return TRUE;
624 static BOOL d2d_figure_add_vertex(struct d2d_figure *figure, D2D1_POINT_2F vertex)
626 size_t last = figure->vertex_count - 1;
628 if (figure->vertex_count && figure->vertex_types[last] == D2D_VERTEX_TYPE_LINE
629 && !memcmp(&figure->vertices[last], &vertex, sizeof(vertex)))
630 return TRUE;
632 if (!d2d_array_reserve((void **)&figure->vertices, &figure->vertices_size,
633 figure->vertex_count + 1, sizeof(*figure->vertices)))
635 ERR("Failed to grow vertices array.\n");
636 return FALSE;
639 if (!d2d_array_reserve((void **)&figure->vertex_types, &figure->vertex_types_size,
640 figure->vertex_count + 1, sizeof(*figure->vertex_types)))
642 ERR("Failed to grow vertex types array.\n");
643 return FALSE;
646 figure->vertices[figure->vertex_count] = vertex;
647 figure->vertex_types[figure->vertex_count] = D2D_VERTEX_TYPE_NONE;
648 d2d_rect_expand(&figure->bounds, &vertex);
649 ++figure->vertex_count;
650 return TRUE;
653 static BOOL d2d_figure_insert_bezier_control(struct d2d_figure *figure, size_t idx, const D2D1_POINT_2F *p)
655 if (!d2d_array_reserve((void **)&figure->bezier_controls, &figure->bezier_controls_size,
656 figure->bezier_control_count + 1, sizeof(*figure->bezier_controls)))
658 ERR("Failed to grow bezier controls array.\n");
659 return FALSE;
662 memmove(&figure->bezier_controls[idx + 1], &figure->bezier_controls[idx],
663 (figure->bezier_control_count - idx) * sizeof(*figure->bezier_controls));
664 figure->bezier_controls[idx] = *p;
665 ++figure->bezier_control_count;
667 return TRUE;
670 static BOOL d2d_figure_add_bezier_control(struct d2d_figure *figure, const D2D1_POINT_2F *p)
672 if (!d2d_array_reserve((void **)&figure->bezier_controls, &figure->bezier_controls_size,
673 figure->bezier_control_count + 1, sizeof(*figure->bezier_controls)))
675 ERR("Failed to grow bezier controls array.\n");
676 return FALSE;
679 figure->bezier_controls[figure->bezier_control_count++] = *p;
681 return TRUE;
684 static void d2d_cdt_edge_rot(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
686 dst->idx = src->idx;
687 dst->r = (src->r + D2D_EDGE_NEXT_ROT) & 3;
690 static void d2d_cdt_edge_sym(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
692 dst->idx = src->idx;
693 dst->r = (src->r + D2D_EDGE_NEXT_SYM) & 3;
696 static void d2d_cdt_edge_tor(struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
698 dst->idx = src->idx;
699 dst->r = (src->r + D2D_EDGE_NEXT_TOR) & 3;
702 static void d2d_cdt_edge_next_left(const struct d2d_cdt *cdt,
703 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
705 d2d_cdt_edge_rot(dst, &cdt->edges[src->idx].next[(src->r + D2D_EDGE_NEXT_TOR) & 3]);
708 static void d2d_cdt_edge_next_origin(const struct d2d_cdt *cdt,
709 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
711 *dst = cdt->edges[src->idx].next[src->r];
714 static void d2d_cdt_edge_prev_origin(const struct d2d_cdt *cdt,
715 struct d2d_cdt_edge_ref *dst, const struct d2d_cdt_edge_ref *src)
717 d2d_cdt_edge_rot(dst, &cdt->edges[src->idx].next[(src->r + D2D_EDGE_NEXT_ROT) & 3]);
720 static size_t d2d_cdt_edge_origin(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
722 return cdt->edges[e->idx].vertex[e->r >> 1];
725 static size_t d2d_cdt_edge_destination(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
727 return cdt->edges[e->idx].vertex[!(e->r >> 1)];
730 static void d2d_cdt_edge_set_origin(const struct d2d_cdt *cdt,
731 const struct d2d_cdt_edge_ref *e, size_t vertex)
733 cdt->edges[e->idx].vertex[e->r >> 1] = vertex;
736 static void d2d_cdt_edge_set_destination(const struct d2d_cdt *cdt,
737 const struct d2d_cdt_edge_ref *e, size_t vertex)
739 cdt->edges[e->idx].vertex[!(e->r >> 1)] = vertex;
742 static float d2d_cdt_ccw(const struct d2d_cdt *cdt, size_t a, size_t b, size_t c)
744 return d2d_point_ccw(&cdt->vertices[a], &cdt->vertices[b], &cdt->vertices[c]);
747 static BOOL d2d_cdt_rightof(const struct d2d_cdt *cdt, size_t p, const struct d2d_cdt_edge_ref *e)
749 return d2d_cdt_ccw(cdt, p, d2d_cdt_edge_destination(cdt, e), d2d_cdt_edge_origin(cdt, e)) > 0.0f;
752 static BOOL d2d_cdt_leftof(const struct d2d_cdt *cdt, size_t p, const struct d2d_cdt_edge_ref *e)
754 return d2d_cdt_ccw(cdt, p, d2d_cdt_edge_origin(cdt, e), d2d_cdt_edge_destination(cdt, e)) > 0.0f;
757 /* |ax ay|
758 * |bx by| */
759 static void d2d_fp_four_det2x2(float *out, float ax, float ay, float bx, float by)
761 float axby[2], aybx[2];
763 d2d_fp_two_product(axby, ax, by);
764 d2d_fp_two_product(aybx, ay, bx);
765 d2d_fp_two_two_diff(out, axby, aybx);
768 /* (a->x² + a->y²) * det2x2 */
769 static void d2d_fp_sub_det3x3(float *out, size_t *out_len, const struct d2d_fp_two_vec2 *a, const float *det2x2)
771 size_t axd_len, ayd_len, axxd_len, ayyd_len;
772 float axd[8], ayd[8], axxd[16], ayyd[16];
774 d2d_fp_scale_expansion_zeroelim(axd, &axd_len, det2x2, 4, a->x[1]);
775 d2d_fp_scale_expansion_zeroelim(axxd, &axxd_len, axd, axd_len, a->x[1]);
776 d2d_fp_scale_expansion_zeroelim(ayd, &ayd_len, det2x2, 4, a->y[1]);
777 d2d_fp_scale_expansion_zeroelim(ayyd, &ayyd_len, ayd, ayd_len, a->y[1]);
778 d2d_fp_fast_expansion_sum_zeroelim(out, out_len, axxd, axxd_len, ayyd, ayyd_len);
781 /* det_abt = det_ab * c[0]
782 * fin += c[0] * (az * b - bz * a + c[1] * det_ab * 2.0f) */
783 static void d2d_cdt_incircle_refine1(struct d2d_fp_fin *fin, float *det_abt, size_t *det_abt_len,
784 const float *det_ab, float a, const float *az, float b, const float *bz, const float *c)
786 size_t temp48_len, temp32_len, temp16a_len, temp16b_len, temp16c_len, temp8_len;
787 float temp48[48], temp32[32], temp16a[16], temp16b[16], temp16c[16], temp8[8];
788 float *swap;
790 d2d_fp_scale_expansion_zeroelim(det_abt, det_abt_len, det_ab, 4, c[0]);
791 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, det_abt, *det_abt_len, 2.0f * c[1]);
792 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, az, 4, c[0]);
793 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, temp8, temp8_len, b);
794 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, bz, 4, c[0]);
795 d2d_fp_scale_expansion_zeroelim(temp16c, &temp16c_len, temp8, temp8_len, -a);
796 d2d_fp_fast_expansion_sum_zeroelim(temp32, &temp32_len, temp16a, temp16a_len, temp16b, temp16b_len);
797 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16c, temp16c_len, temp32, temp32_len);
798 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
799 swap = fin->now; fin->now = fin->other; fin->other = swap;
802 static void d2d_cdt_incircle_refine2(struct d2d_fp_fin *fin, const struct d2d_fp_two_vec2 *a,
803 const struct d2d_fp_two_vec2 *b, const float *bz, const struct d2d_fp_two_vec2 *c, const float *cz,
804 const float *axt_det_bc, size_t axt_det_bc_len, const float *ayt_det_bc, size_t ayt_det_bc_len)
806 size_t temp64_len, temp48_len, temp32a_len, temp32b_len, temp16a_len, temp16b_len, temp8_len;
807 float temp64[64], temp48[48], temp32a[32], temp32b[32], temp16a[16], temp16b[16], temp8[8];
808 float bct[8], bctt[4], temp4a[4], temp4b[4], temp2a[2], temp2b[2];
809 size_t bct_len, bctt_len;
810 float *swap;
812 /* 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]) */
813 /* bctt = b->x[0] * c->y[0] + c->x[0] * b->y[0] */
814 if (b->x[0] != 0.0f || b->y[0] != 0.0f || c->x[0] != 0.0f || c->y[0] != 0.0f)
816 d2d_fp_two_product(temp2a, b->x[0], c->y[1]);
817 d2d_fp_two_product(temp2b, b->x[1], c->y[0]);
818 d2d_fp_two_two_sum(temp4a, temp2a, temp2b);
819 d2d_fp_two_product(temp2a, c->x[0], -b->y[1]);
820 d2d_fp_two_product(temp2b, c->x[1], -b->y[0]);
821 d2d_fp_two_two_sum(temp4b, temp2a, temp2b);
822 d2d_fp_fast_expansion_sum_zeroelim(bct, &bct_len, temp4a, 4, temp4b, 4);
824 d2d_fp_two_product(temp2a, b->x[0], c->y[0]);
825 d2d_fp_two_product(temp2b, c->x[0], b->y[0]);
826 d2d_fp_two_two_diff(bctt, temp2a, temp2b);
827 bctt_len = 4;
829 else
831 bct[0] = 0.0f;
832 bct_len = 1;
833 bctt[0] = 0.0f;
834 bctt_len = 1;
837 if (a->x[0] != 0.0f)
839 size_t axt_bct_len, axt_bctt_len;
840 float axt_bct[16], axt_bctt[8];
842 /* fin += a->x[0] * (axt_det_bc + bct * 2.0f * a->x[1]) */
843 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, axt_det_bc, axt_det_bc_len, a->x[0]);
844 d2d_fp_scale_expansion_zeroelim(axt_bct, &axt_bct_len, bct, bct_len, a->x[0]);
845 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, axt_bct, axt_bct_len, 2.0f * a->x[1]);
846 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16a, temp16a_len, temp32a, temp32a_len);
847 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
848 swap = fin->now; fin->now = fin->other; fin->other = swap;
850 if (b->y[0] != 0.0f)
852 /* fin += a->x[0] * cz * b->y[0] */
853 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, cz, 4, a->x[0]);
854 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, temp8, temp8_len, b->y[0]);
855 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp16a, temp16a_len);
856 swap = fin->now; fin->now = fin->other; fin->other = swap;
859 if (c->y[0] != 0.0f)
861 /* fin -= a->x[0] * bz * c->y[0] */
862 d2d_fp_scale_expansion_zeroelim(temp8, &temp8_len, bz, 4, -a->x[0]);
863 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, temp8, temp8_len, c->y[0]);
864 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp16a, temp16a_len);
865 swap = fin->now; fin->now = fin->other; fin->other = swap;
868 /* fin += a->x[0] * (bct * a->x[0] + bctt * (2.0f * a->x[1] + a->x[0])) */
869 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, axt_bct, axt_bct_len, a->x[0]);
870 d2d_fp_scale_expansion_zeroelim(axt_bctt, &axt_bctt_len, bctt, bctt_len, a->x[0]);
871 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, axt_bctt, axt_bctt_len, 2.0f * a->x[1]);
872 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, axt_bctt, axt_bctt_len, a->x[0]);
873 d2d_fp_fast_expansion_sum_zeroelim(temp32b, &temp32b_len, temp16a, temp16a_len, temp16b, temp16b_len);
874 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, temp32a, temp32a_len, temp32b, temp32b_len);
875 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp64, temp64_len);
876 swap = fin->now; fin->now = fin->other; fin->other = swap;
879 if (a->y[0] != 0.0f)
881 size_t ayt_bct_len, ayt_bctt_len;
882 float ayt_bct[16], ayt_bctt[8];
884 /* fin += a->y[0] * (ayt_det_bc + bct * 2.0f * a->y[1]) */
885 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, ayt_det_bc, ayt_det_bc_len, a->y[0]);
886 d2d_fp_scale_expansion_zeroelim(ayt_bct, &ayt_bct_len, bct, bct_len, a->y[0]);
887 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, ayt_bct, ayt_bct_len, 2.0f * a->y[1]);
888 d2d_fp_fast_expansion_sum_zeroelim(temp48, &temp48_len, temp16a, temp16a_len, temp32a, temp32a_len);
889 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp48, temp48_len);
890 swap = fin->now; fin->now = fin->other; fin->other = swap;
892 /* fin += a->y[0] * (bct * a->y[0] + bctt * (2.0f * a->y[1] + a->y[0])) */
893 d2d_fp_scale_expansion_zeroelim(temp32a, &temp32a_len, ayt_bct, ayt_bct_len, a->y[0]);
894 d2d_fp_scale_expansion_zeroelim(ayt_bctt, &ayt_bctt_len, bctt, bctt_len, a->y[0]);
895 d2d_fp_scale_expansion_zeroelim(temp16a, &temp16a_len, ayt_bctt, ayt_bctt_len, 2.0f * a->y[1]);
896 d2d_fp_scale_expansion_zeroelim(temp16b, &temp16b_len, ayt_bctt, ayt_bctt_len, a->y[0]);
897 d2d_fp_fast_expansion_sum_zeroelim(temp32b, &temp32b_len, temp16a, temp16a_len, temp16b, temp16b_len);
898 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, temp32a, temp32a_len, temp32b, temp32b_len);
899 d2d_fp_fast_expansion_sum_zeroelim(fin->other, &fin->length, fin->now, fin->length, temp64, temp64_len);
900 swap = fin->now; fin->now = fin->other; fin->other = swap;
904 /* Determine if point D is inside or outside the circle defined by points A,
905 * B, C. As explained in the paper by Guibas and Stolfi, this is equivalent to
906 * calculating the signed volume of the tetrahedron defined by projecting the
907 * points onto the paraboloid of revolution x = x² + y²,
908 * λ:(x, y) → (x, y, x² + y²). I.e., D is inside the cirlce if
910 * |λ(A) 1|
911 * |λ(B) 1| > 0
912 * |λ(C) 1|
913 * |λ(D) 1|
915 * After translating D to the origin, that becomes:
917 * |λ(A-D)|
918 * |λ(B-D)| > 0
919 * |λ(C-D)|
921 * This implementation is based on the paper "Adaptive Precision
922 * Floating-Point Arithmetic and Fast Robust Geometric Predicates" and
923 * associated (Public Domain) code by Jonathan Richard Shewchuk. */
924 static BOOL d2d_cdt_incircle(const struct d2d_cdt *cdt, size_t a, size_t b, size_t c, size_t d)
926 static const float err_bound_result = (3.0f + 8.0f * D2D_FP_EPS) * D2D_FP_EPS;
927 static const float err_bound_a = (10.0f + 96.0f * D2D_FP_EPS) * D2D_FP_EPS;
928 static const float err_bound_b = (4.0f + 48.0f * D2D_FP_EPS) * D2D_FP_EPS;
929 static const float err_bound_c = (44.0f + 576.0f * D2D_FP_EPS) * D2D_FP_EPS * D2D_FP_EPS;
931 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;
932 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];
933 float fin1[1152], fin2[1152], temp64[64], sub_det_a[32], sub_det_b[32], sub_det_c[32];
934 float det_bc[4], det_ca[4], det_ab[4], daz[4], dbz[4], dcz[4], temp2a[2], temp2b[2];
935 size_t temp64_len, sub_det_a_len, sub_det_b_len, sub_det_c_len;
936 float dbxdcy, dbydcx, dcxday, dcydax, daxdby, daydbx;
937 const D2D1_POINT_2F *p = cdt->vertices;
938 struct d2d_fp_two_vec2 da, db, dc;
939 float permanent, err_bound, det;
940 struct d2d_fp_fin fin;
942 da.x[1] = p[a].x - p[d].x;
943 da.y[1] = p[a].y - p[d].y;
944 db.x[1] = p[b].x - p[d].x;
945 db.y[1] = p[b].y - p[d].y;
946 dc.x[1] = p[c].x - p[d].x;
947 dc.y[1] = p[c].y - p[d].y;
949 daz[3] = da.x[1] * da.x[1] + da.y[1] * da.y[1];
950 dbxdcy = db.x[1] * dc.y[1];
951 dbydcx = db.y[1] * dc.x[1];
953 dbz[3] = db.x[1] * db.x[1] + db.y[1] * db.y[1];
954 dcxday = dc.x[1] * da.y[1];
955 dcydax = dc.y[1] * da.x[1];
957 dcz[3] = dc.x[1] * dc.x[1] + dc.y[1] * dc.y[1];
958 daxdby = da.x[1] * db.y[1];
959 daydbx = da.y[1] * db.x[1];
961 det = daz[3] * (dbxdcy - dbydcx) + dbz[3] * (dcxday - dcydax) + dcz[3] * (daxdby - daydbx);
962 permanent = daz[3] * (fabsf(dbxdcy) + fabsf(dbydcx))
963 + dbz[3] * (fabsf(dcxday) + fabsf(dcydax))
964 + dcz[3] * (fabsf(daxdby) + fabsf(daydbx));
965 err_bound = err_bound_a * permanent;
966 if (det > err_bound || -det > err_bound)
967 return det > 0.0f;
969 fin.now = fin1;
970 fin.other = fin2;
972 d2d_fp_four_det2x2(det_bc, db.x[1], db.y[1], dc.x[1], dc.y[1]);
973 d2d_fp_sub_det3x3(sub_det_a, &sub_det_a_len, &da, det_bc);
975 d2d_fp_four_det2x2(det_ca, dc.x[1], dc.y[1], da.x[1], da.y[1]);
976 d2d_fp_sub_det3x3(sub_det_b, &sub_det_b_len, &db, det_ca);
978 d2d_fp_four_det2x2(det_ab, da.x[1], da.y[1], db.x[1], db.y[1]);
979 d2d_fp_sub_det3x3(sub_det_c, &sub_det_c_len, &dc, det_ab);
981 d2d_fp_fast_expansion_sum_zeroelim(temp64, &temp64_len, sub_det_a, sub_det_a_len, sub_det_b, sub_det_b_len);
982 d2d_fp_fast_expansion_sum_zeroelim(fin.now, &fin.length, temp64, temp64_len, sub_det_c, sub_det_c_len);
983 det = d2d_fp_estimate(fin.now, fin.length);
984 err_bound = err_bound_b * permanent;
985 if (det >= err_bound || -det >= err_bound)
986 return det > 0.0f;
988 d2d_fp_two_diff_tail(&da.x[0], p[a].x, p[d].x, da.x[1]);
989 d2d_fp_two_diff_tail(&da.y[0], p[a].y, p[d].y, da.y[1]);
990 d2d_fp_two_diff_tail(&db.x[0], p[b].x, p[d].x, db.x[1]);
991 d2d_fp_two_diff_tail(&db.y[0], p[b].y, p[d].y, db.y[1]);
992 d2d_fp_two_diff_tail(&dc.x[0], p[c].x, p[d].x, dc.x[1]);
993 d2d_fp_two_diff_tail(&dc.y[0], p[c].y, p[d].y, dc.y[1]);
994 if (da.x[0] == 0.0f && db.x[0] == 0.0f && dc.x[0] == 0.0f
995 && da.y[0] == 0.0f && db.y[0] == 0.0f && dc.y[0] == 0.0f)
996 return det > 0.0f;
998 err_bound = err_bound_c * permanent + err_bound_result * fabsf(det);
999 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]))
1000 + 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]))
1001 + (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]))
1002 + 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]))
1003 + (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]))
1004 + 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]));
1005 if (det >= err_bound || -det >= err_bound)
1006 return det > 0.0f;
1008 if (db.x[0] != 0.0f || db.y[0] != 0.0f || dc.x[0] != 0.0f || dc.y[0] != 0.0f)
1010 d2d_fp_square(temp2a, da.x[1]);
1011 d2d_fp_square(temp2b, da.y[1]);
1012 d2d_fp_two_two_sum(daz, temp2a, temp2b);
1014 if (dc.x[0] != 0.0f || dc.y[0] != 0.0f || da.x[0] != 0.0f || da.y[0] != 0.0f)
1016 d2d_fp_square(temp2a, db.x[1]);
1017 d2d_fp_square(temp2b, db.y[1]);
1018 d2d_fp_two_two_sum(dbz, temp2a, temp2b);
1020 if (da.x[0] != 0.0f || da.y[0] != 0.0f || db.x[0] != 0.0f || db.y[0] != 0.0f)
1022 d2d_fp_square(temp2a, dc.x[1]);
1023 d2d_fp_square(temp2b, dc.y[1]);
1024 d2d_fp_two_two_sum(dcz, temp2a, temp2b);
1027 if (da.x[0] != 0.0f)
1028 d2d_cdt_incircle_refine1(&fin, axt_det_bc, &axt_det_bc_len, det_bc, dc.y[1], dcz, db.y[1], dbz, da.x);
1029 if (da.y[0] != 0.0f)
1030 d2d_cdt_incircle_refine1(&fin, ayt_det_bc, &ayt_det_bc_len, det_bc, db.x[1], dbz, dc.x[1], dcz, da.y);
1031 if (db.x[0] != 0.0f)
1032 d2d_cdt_incircle_refine1(&fin, bxt_det_ca, &bxt_det_ca_len, det_ca, da.y[1], daz, dc.y[1], dcz, db.x);
1033 if (db.y[0] != 0.0f)
1034 d2d_cdt_incircle_refine1(&fin, byt_det_ca, &byt_det_ca_len, det_ca, dc.x[1], dcz, da.x[1], daz, db.y);
1035 if (dc.x[0] != 0.0f)
1036 d2d_cdt_incircle_refine1(&fin, cxt_det_ab, &cxt_det_ab_len, det_ab, db.y[1], dbz, da.y[1], daz, dc.x);
1037 if (dc.y[0] != 0.0f)
1038 d2d_cdt_incircle_refine1(&fin, cyt_det_ab, &cyt_det_ab_len, det_ab, da.x[1], daz, db.x[1], dbz, dc.y);
1040 if (da.x[0] != 0.0f || da.y[0] != 0.0f)
1041 d2d_cdt_incircle_refine2(&fin, &da, &db, dbz, &dc, dcz,
1042 axt_det_bc, axt_det_bc_len, ayt_det_bc, ayt_det_bc_len);
1043 if (db.x[0] != 0.0f || db.y[0] != 0.0f)
1044 d2d_cdt_incircle_refine2(&fin, &db, &dc, dcz, &da, daz,
1045 bxt_det_ca, bxt_det_ca_len, byt_det_ca, byt_det_ca_len);
1046 if (dc.x[0] != 0.0f || dc.y[0] != 0.0f)
1047 d2d_cdt_incircle_refine2(&fin, &dc, &da, daz, &db, dbz,
1048 cxt_det_ab, cxt_det_ab_len, cyt_det_ab, cyt_det_ab_len);
1050 return fin.now[fin.length - 1] > 0.0f;
1053 static void d2d_cdt_splice(const struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *a,
1054 const struct d2d_cdt_edge_ref *b)
1056 struct d2d_cdt_edge_ref ta, tb, alpha, beta;
1058 ta = cdt->edges[a->idx].next[a->r];
1059 tb = cdt->edges[b->idx].next[b->r];
1060 cdt->edges[a->idx].next[a->r] = tb;
1061 cdt->edges[b->idx].next[b->r] = ta;
1063 d2d_cdt_edge_rot(&alpha, &ta);
1064 d2d_cdt_edge_rot(&beta, &tb);
1066 ta = cdt->edges[alpha.idx].next[alpha.r];
1067 tb = cdt->edges[beta.idx].next[beta.r];
1068 cdt->edges[alpha.idx].next[alpha.r] = tb;
1069 cdt->edges[beta.idx].next[beta.r] = ta;
1072 static BOOL d2d_cdt_create_edge(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *e)
1074 struct d2d_cdt_edge *edge;
1076 if (cdt->free_edge != ~0u)
1078 e->idx = cdt->free_edge;
1079 cdt->free_edge = cdt->edges[e->idx].next[D2D_EDGE_NEXT_ORIGIN].idx;
1081 else
1083 if (!d2d_array_reserve((void **)&cdt->edges, &cdt->edges_size, cdt->edge_count + 1, sizeof(*cdt->edges)))
1085 ERR("Failed to grow edges array.\n");
1086 return FALSE;
1088 e->idx = cdt->edge_count++;
1090 e->r = 0;
1092 edge = &cdt->edges[e->idx];
1093 edge->next[D2D_EDGE_NEXT_ORIGIN] = *e;
1094 d2d_cdt_edge_tor(&edge->next[D2D_EDGE_NEXT_ROT], e);
1095 d2d_cdt_edge_sym(&edge->next[D2D_EDGE_NEXT_SYM], e);
1096 d2d_cdt_edge_rot(&edge->next[D2D_EDGE_NEXT_TOR], e);
1097 edge->flags = 0;
1099 return TRUE;
1102 static void d2d_cdt_destroy_edge(struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *e)
1104 struct d2d_cdt_edge_ref next, sym, prev;
1106 d2d_cdt_edge_next_origin(cdt, &next, e);
1107 if (next.idx != e->idx || next.r != e->r)
1109 d2d_cdt_edge_prev_origin(cdt, &prev, e);
1110 d2d_cdt_splice(cdt, e, &prev);
1113 d2d_cdt_edge_sym(&sym, e);
1115 d2d_cdt_edge_next_origin(cdt, &next, &sym);
1116 if (next.idx != sym.idx || next.r != sym.r)
1118 d2d_cdt_edge_prev_origin(cdt, &prev, &sym);
1119 d2d_cdt_splice(cdt, &sym, &prev);
1122 cdt->edges[e->idx].flags |= D2D_CDT_EDGE_FLAG_FREED;
1123 cdt->edges[e->idx].next[D2D_EDGE_NEXT_ORIGIN].idx = cdt->free_edge;
1124 cdt->free_edge = e->idx;
1127 static BOOL d2d_cdt_connect(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *e,
1128 const struct d2d_cdt_edge_ref *a, const struct d2d_cdt_edge_ref *b)
1130 struct d2d_cdt_edge_ref tmp;
1132 if (!d2d_cdt_create_edge(cdt, e))
1133 return FALSE;
1134 d2d_cdt_edge_set_origin(cdt, e, d2d_cdt_edge_destination(cdt, a));
1135 d2d_cdt_edge_set_destination(cdt, e, d2d_cdt_edge_origin(cdt, b));
1136 d2d_cdt_edge_next_left(cdt, &tmp, a);
1137 d2d_cdt_splice(cdt, e, &tmp);
1138 d2d_cdt_edge_sym(&tmp, e);
1139 d2d_cdt_splice(cdt, &tmp, b);
1141 return TRUE;
1144 static BOOL d2d_cdt_merge(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *left_outer,
1145 struct d2d_cdt_edge_ref *left_inner, struct d2d_cdt_edge_ref *right_inner,
1146 struct d2d_cdt_edge_ref *right_outer)
1148 struct d2d_cdt_edge_ref base_edge, tmp;
1150 /* Create the base edge between both parts. */
1151 for (;;)
1153 if (d2d_cdt_leftof(cdt, d2d_cdt_edge_origin(cdt, right_inner), left_inner))
1155 d2d_cdt_edge_next_left(cdt, left_inner, left_inner);
1157 else if (d2d_cdt_rightof(cdt, d2d_cdt_edge_origin(cdt, left_inner), right_inner))
1159 d2d_cdt_edge_sym(&tmp, right_inner);
1160 d2d_cdt_edge_next_origin(cdt, right_inner, &tmp);
1162 else
1164 break;
1168 d2d_cdt_edge_sym(&tmp, right_inner);
1169 if (!d2d_cdt_connect(cdt, &base_edge, &tmp, left_inner))
1170 return FALSE;
1171 if (d2d_cdt_edge_origin(cdt, left_inner) == d2d_cdt_edge_origin(cdt, left_outer))
1172 d2d_cdt_edge_sym(left_outer, &base_edge);
1173 if (d2d_cdt_edge_origin(cdt, right_inner) == d2d_cdt_edge_origin(cdt, right_outer))
1174 *right_outer = base_edge;
1176 for (;;)
1178 struct d2d_cdt_edge_ref left_candidate, right_candidate, sym_base_edge;
1179 BOOL left_valid, right_valid;
1181 /* Find the left candidate. */
1182 d2d_cdt_edge_sym(&sym_base_edge, &base_edge);
1183 d2d_cdt_edge_next_origin(cdt, &left_candidate, &sym_base_edge);
1184 if ((left_valid = d2d_cdt_leftof(cdt, d2d_cdt_edge_destination(cdt, &left_candidate), &sym_base_edge)))
1186 d2d_cdt_edge_next_origin(cdt, &tmp, &left_candidate);
1187 while (d2d_cdt_edge_destination(cdt, &tmp) != d2d_cdt_edge_destination(cdt, &sym_base_edge)
1188 && d2d_cdt_incircle(cdt,
1189 d2d_cdt_edge_origin(cdt, &sym_base_edge), d2d_cdt_edge_destination(cdt, &sym_base_edge),
1190 d2d_cdt_edge_destination(cdt, &left_candidate), d2d_cdt_edge_destination(cdt, &tmp)))
1192 d2d_cdt_destroy_edge(cdt, &left_candidate);
1193 left_candidate = tmp;
1194 d2d_cdt_edge_next_origin(cdt, &tmp, &left_candidate);
1197 d2d_cdt_edge_sym(&left_candidate, &left_candidate);
1199 /* Find the right candidate. */
1200 d2d_cdt_edge_prev_origin(cdt, &right_candidate, &base_edge);
1201 if ((right_valid = d2d_cdt_rightof(cdt, d2d_cdt_edge_destination(cdt, &right_candidate), &base_edge)))
1203 d2d_cdt_edge_prev_origin(cdt, &tmp, &right_candidate);
1204 while (d2d_cdt_edge_destination(cdt, &tmp) != d2d_cdt_edge_destination(cdt, &base_edge)
1205 && d2d_cdt_incircle(cdt,
1206 d2d_cdt_edge_origin(cdt, &sym_base_edge), d2d_cdt_edge_destination(cdt, &sym_base_edge),
1207 d2d_cdt_edge_destination(cdt, &right_candidate), d2d_cdt_edge_destination(cdt, &tmp)))
1209 d2d_cdt_destroy_edge(cdt, &right_candidate);
1210 right_candidate = tmp;
1211 d2d_cdt_edge_prev_origin(cdt, &tmp, &right_candidate);
1215 if (!left_valid && !right_valid)
1216 break;
1218 /* Connect the appropriate candidate with the base edge. */
1219 if (!left_valid || (right_valid && d2d_cdt_incircle(cdt,
1220 d2d_cdt_edge_origin(cdt, &left_candidate), d2d_cdt_edge_destination(cdt, &left_candidate),
1221 d2d_cdt_edge_origin(cdt, &right_candidate), d2d_cdt_edge_destination(cdt, &right_candidate))))
1223 if (!d2d_cdt_connect(cdt, &base_edge, &right_candidate, &sym_base_edge))
1224 return FALSE;
1226 else
1228 if (!d2d_cdt_connect(cdt, &base_edge, &sym_base_edge, &left_candidate))
1229 return FALSE;
1233 return TRUE;
1236 /* Create a Delaunay triangulation from a set of vertices. This is an
1237 * implementation of the divide-and-conquer algorithm described by Guibas and
1238 * Stolfi. Should be called with at least two vertices. */
1239 static BOOL d2d_cdt_triangulate(struct d2d_cdt *cdt, size_t start_vertex, size_t vertex_count,
1240 struct d2d_cdt_edge_ref *left_edge, struct d2d_cdt_edge_ref *right_edge)
1242 struct d2d_cdt_edge_ref left_inner, left_outer, right_inner, right_outer, tmp;
1243 size_t cut;
1245 /* Only two vertices, create a single edge. */
1246 if (vertex_count == 2)
1248 struct d2d_cdt_edge_ref a;
1250 if (!d2d_cdt_create_edge(cdt, &a))
1251 return FALSE;
1252 d2d_cdt_edge_set_origin(cdt, &a, start_vertex);
1253 d2d_cdt_edge_set_destination(cdt, &a, start_vertex + 1);
1255 *left_edge = a;
1256 d2d_cdt_edge_sym(right_edge, &a);
1258 return TRUE;
1261 /* Three vertices, create a triangle. */
1262 if (vertex_count == 3)
1264 struct d2d_cdt_edge_ref a, b, c;
1265 float det;
1267 if (!d2d_cdt_create_edge(cdt, &a))
1268 return FALSE;
1269 if (!d2d_cdt_create_edge(cdt, &b))
1270 return FALSE;
1271 d2d_cdt_edge_sym(&tmp, &a);
1272 d2d_cdt_splice(cdt, &tmp, &b);
1274 d2d_cdt_edge_set_origin(cdt, &a, start_vertex);
1275 d2d_cdt_edge_set_destination(cdt, &a, start_vertex + 1);
1276 d2d_cdt_edge_set_origin(cdt, &b, start_vertex + 1);
1277 d2d_cdt_edge_set_destination(cdt, &b, start_vertex + 2);
1279 det = d2d_cdt_ccw(cdt, start_vertex, start_vertex + 1, start_vertex + 2);
1280 if (det != 0.0f && !d2d_cdt_connect(cdt, &c, &b, &a))
1281 return FALSE;
1283 if (det < 0.0f)
1285 d2d_cdt_edge_sym(left_edge, &c);
1286 *right_edge = c;
1288 else
1290 *left_edge = a;
1291 d2d_cdt_edge_sym(right_edge, &b);
1294 return TRUE;
1297 /* More than tree vertices, divide. */
1298 cut = vertex_count / 2;
1299 if (!d2d_cdt_triangulate(cdt, start_vertex, cut, &left_outer, &left_inner))
1300 return FALSE;
1301 if (!d2d_cdt_triangulate(cdt, start_vertex + cut, vertex_count - cut, &right_inner, &right_outer))
1302 return FALSE;
1303 /* Merge the left and right parts. */
1304 if (!d2d_cdt_merge(cdt, &left_outer, &left_inner, &right_inner, &right_outer))
1305 return FALSE;
1307 *left_edge = left_outer;
1308 *right_edge = right_outer;
1309 return TRUE;
1312 static int d2d_cdt_compare_vertices(const void *a, const void *b)
1314 const D2D1_POINT_2F *p0 = a;
1315 const D2D1_POINT_2F *p1 = b;
1316 float diff = p0->x - p1->x;
1318 if (diff == 0.0f)
1319 diff = p0->y - p1->y;
1321 return diff == 0.0f ? 0 : (diff > 0.0f ? 1 : -1);
1324 /* Determine whether a given point is inside the geometry, using the current
1325 * fill mode rule. */
1326 static BOOL d2d_path_geometry_point_inside(const struct d2d_geometry *geometry,
1327 const D2D1_POINT_2F *probe, BOOL triangles_only)
1329 const D2D1_POINT_2F *p0, *p1;
1330 D2D1_POINT_2F v_p, v_probe;
1331 unsigned int score;
1332 size_t i, j, last;
1334 for (i = 0, score = 0; i < geometry->u.path.figure_count; ++i)
1336 const struct d2d_figure *figure = &geometry->u.path.figures[i];
1338 if (probe->x < figure->bounds.left || probe->x > figure->bounds.right
1339 || probe->y < figure->bounds.top || probe->y > figure->bounds.bottom)
1340 continue;
1342 last = figure->vertex_count - 1;
1343 if (!triangles_only)
1345 while (last && figure->vertex_types[last] == D2D_VERTEX_TYPE_NONE)
1346 --last;
1348 p0 = &figure->vertices[last];
1349 for (j = 0; j <= last; ++j)
1351 if (!triangles_only && figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
1352 continue;
1354 p1 = &figure->vertices[j];
1355 d2d_point_subtract(&v_p, p1, p0);
1356 d2d_point_subtract(&v_probe, probe, p0);
1358 if ((probe->y < p0->y) != (probe->y < p1->y) && v_probe.x < v_p.x * (v_probe.y / v_p.y))
1360 if (geometry->u.path.fill_mode == D2D1_FILL_MODE_ALTERNATE || (probe->y < p0->y))
1361 ++score;
1362 else
1363 --score;
1366 p0 = p1;
1370 return geometry->u.path.fill_mode == D2D1_FILL_MODE_ALTERNATE ? score & 1 : score;
1373 static BOOL d2d_path_geometry_add_fill_face(struct d2d_geometry *geometry, const struct d2d_cdt *cdt,
1374 const struct d2d_cdt_edge_ref *base_edge)
1376 struct d2d_cdt_edge_ref tmp;
1377 struct d2d_face *face;
1378 D2D1_POINT_2F probe;
1380 if (cdt->edges[base_edge->idx].flags & D2D_CDT_EDGE_FLAG_VISITED(base_edge->r))
1381 return TRUE;
1383 if (!d2d_array_reserve((void **)&geometry->fill.faces, &geometry->fill.faces_size,
1384 geometry->fill.face_count + 1, sizeof(*geometry->fill.faces)))
1386 ERR("Failed to grow faces array.\n");
1387 return FALSE;
1390 face = &geometry->fill.faces[geometry->fill.face_count];
1392 /* It may seem tempting to use the center of the face as probe origin, but
1393 * multiplying by powers of two works much better for preserving accuracy. */
1395 tmp = *base_edge;
1396 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1397 face->v[0] = d2d_cdt_edge_origin(cdt, &tmp);
1398 probe.x = cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.25f;
1399 probe.y = cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.25f;
1401 d2d_cdt_edge_next_left(cdt, &tmp, &tmp);
1402 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1403 face->v[1] = d2d_cdt_edge_origin(cdt, &tmp);
1404 probe.x += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.25f;
1405 probe.y += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.25f;
1407 d2d_cdt_edge_next_left(cdt, &tmp, &tmp);
1408 cdt->edges[tmp.idx].flags |= D2D_CDT_EDGE_FLAG_VISITED(tmp.r);
1409 face->v[2] = d2d_cdt_edge_origin(cdt, &tmp);
1410 probe.x += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].x * 0.50f;
1411 probe.y += cdt->vertices[d2d_cdt_edge_origin(cdt, &tmp)].y * 0.50f;
1413 if (d2d_cdt_leftof(cdt, face->v[2], base_edge) && d2d_path_geometry_point_inside(geometry, &probe, TRUE))
1414 ++geometry->fill.face_count;
1416 return TRUE;
1419 static BOOL d2d_cdt_generate_faces(const struct d2d_cdt *cdt, struct d2d_geometry *geometry)
1421 struct d2d_cdt_edge_ref base_edge;
1422 size_t i;
1424 for (i = 0; i < cdt->edge_count; ++i)
1426 if (cdt->edges[i].flags & D2D_CDT_EDGE_FLAG_FREED)
1427 continue;
1429 base_edge.idx = i;
1430 base_edge.r = 0;
1431 if (!d2d_path_geometry_add_fill_face(geometry, cdt, &base_edge))
1432 goto fail;
1433 d2d_cdt_edge_sym(&base_edge, &base_edge);
1434 if (!d2d_path_geometry_add_fill_face(geometry, cdt, &base_edge))
1435 goto fail;
1438 return TRUE;
1440 fail:
1441 HeapFree(GetProcessHeap(), 0, geometry->fill.faces);
1442 geometry->fill.faces = NULL;
1443 geometry->fill.faces_size = 0;
1444 geometry->fill.face_count = 0;
1445 return FALSE;
1448 static BOOL d2d_cdt_fixup(struct d2d_cdt *cdt, const struct d2d_cdt_edge_ref *base_edge)
1450 struct d2d_cdt_edge_ref candidate, next, new_base;
1451 unsigned int count = 0;
1453 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1454 if (next.idx == base_edge->idx)
1456 ERR("Degenerate face.\n");
1457 return FALSE;
1460 candidate = next;
1461 while (d2d_cdt_edge_destination(cdt, &next) != d2d_cdt_edge_origin(cdt, base_edge))
1463 if (d2d_cdt_incircle(cdt, d2d_cdt_edge_origin(cdt, base_edge), d2d_cdt_edge_destination(cdt, base_edge),
1464 d2d_cdt_edge_destination(cdt, &candidate), d2d_cdt_edge_destination(cdt, &next)))
1465 candidate = next;
1466 d2d_cdt_edge_next_left(cdt, &next, &next);
1467 ++count;
1470 if (count > 1)
1472 d2d_cdt_edge_next_left(cdt, &next, &candidate);
1473 if (d2d_cdt_edge_destination(cdt, &next) == d2d_cdt_edge_origin(cdt, base_edge))
1474 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1475 else
1476 next = *base_edge;
1477 if (!d2d_cdt_connect(cdt, &new_base, &candidate, &next))
1478 return FALSE;
1479 if (!d2d_cdt_fixup(cdt, &new_base))
1480 return FALSE;
1481 d2d_cdt_edge_sym(&new_base, &new_base);
1482 if (!d2d_cdt_fixup(cdt, &new_base))
1483 return FALSE;
1486 return TRUE;
1489 static void d2d_cdt_cut_edges(struct d2d_cdt *cdt, struct d2d_cdt_edge_ref *end_edge,
1490 const struct d2d_cdt_edge_ref *base_edge, size_t start_vertex, size_t end_vertex)
1492 struct d2d_cdt_edge_ref next;
1493 float ccw;
1495 d2d_cdt_edge_next_left(cdt, &next, base_edge);
1496 if (d2d_cdt_edge_destination(cdt, &next) == end_vertex)
1498 *end_edge = next;
1499 return;
1502 ccw = d2d_cdt_ccw(cdt, d2d_cdt_edge_destination(cdt, &next), end_vertex, start_vertex);
1503 if (ccw == 0.0f)
1505 *end_edge = next;
1506 return;
1509 if (ccw > 0.0f)
1510 d2d_cdt_edge_next_left(cdt, &next, &next);
1512 d2d_cdt_edge_sym(&next, &next);
1513 d2d_cdt_cut_edges(cdt, end_edge, &next, start_vertex, end_vertex);
1514 d2d_cdt_destroy_edge(cdt, &next);
1517 static BOOL d2d_cdt_insert_segment(struct d2d_cdt *cdt, struct d2d_geometry *geometry,
1518 const struct d2d_cdt_edge_ref *origin, struct d2d_cdt_edge_ref *edge, size_t end_vertex)
1520 struct d2d_cdt_edge_ref base_edge, current, new_origin, next, target;
1521 size_t current_destination, current_origin;
1523 for (current = *origin;; current = next)
1525 d2d_cdt_edge_next_origin(cdt, &next, &current);
1527 current_destination = d2d_cdt_edge_destination(cdt, &current);
1528 if (current_destination == end_vertex)
1530 d2d_cdt_edge_sym(edge, &current);
1531 return TRUE;
1534 current_origin = d2d_cdt_edge_origin(cdt, &current);
1535 if (d2d_cdt_ccw(cdt, end_vertex, current_origin, current_destination) == 0.0f
1536 && (cdt->vertices[current_destination].x > cdt->vertices[current_origin].x)
1537 == (cdt->vertices[end_vertex].x > cdt->vertices[current_origin].x)
1538 && (cdt->vertices[current_destination].y > cdt->vertices[current_origin].y)
1539 == (cdt->vertices[end_vertex].y > cdt->vertices[current_origin].y))
1541 d2d_cdt_edge_sym(&new_origin, &current);
1542 return d2d_cdt_insert_segment(cdt, geometry, &new_origin, edge, end_vertex);
1545 if (d2d_cdt_rightof(cdt, end_vertex, &next) && d2d_cdt_leftof(cdt, end_vertex, &current))
1547 d2d_cdt_edge_next_left(cdt, &base_edge, &current);
1549 d2d_cdt_edge_sym(&base_edge, &base_edge);
1550 d2d_cdt_cut_edges(cdt, &target, &base_edge, d2d_cdt_edge_origin(cdt, origin), end_vertex);
1551 d2d_cdt_destroy_edge(cdt, &base_edge);
1553 if (!d2d_cdt_connect(cdt, &base_edge, &target, &current))
1554 return FALSE;
1555 *edge = base_edge;
1556 if (!d2d_cdt_fixup(cdt, &base_edge))
1557 return FALSE;
1558 d2d_cdt_edge_sym(&base_edge, &base_edge);
1559 if (!d2d_cdt_fixup(cdt, &base_edge))
1560 return FALSE;
1562 if (d2d_cdt_edge_origin(cdt, edge) == end_vertex)
1563 return TRUE;
1564 new_origin = *edge;
1565 return d2d_cdt_insert_segment(cdt, geometry, &new_origin, edge, end_vertex);
1568 if (next.idx == origin->idx)
1570 ERR("Triangle not found.\n");
1571 return FALSE;
1576 static BOOL d2d_cdt_insert_segments(struct d2d_cdt *cdt, struct d2d_geometry *geometry)
1578 size_t start_vertex, end_vertex, i, j, k;
1579 struct d2d_cdt_edge_ref edge, new_edge;
1580 const struct d2d_figure *figure;
1581 const D2D1_POINT_2F *p;
1582 BOOL found;
1584 for (i = 0; i < geometry->u.path.figure_count; ++i)
1586 figure = &geometry->u.path.figures[i];
1588 /* Degenerate figure. */
1589 if (figure->vertex_count < 2)
1590 continue;
1592 p = bsearch(&figure->vertices[figure->vertex_count - 1], cdt->vertices,
1593 geometry->fill.vertex_count, sizeof(*p), d2d_cdt_compare_vertices);
1594 start_vertex = p - cdt->vertices;
1596 for (k = 0, found = FALSE; k < cdt->edge_count; ++k)
1598 if (cdt->edges[k].flags & D2D_CDT_EDGE_FLAG_FREED)
1599 continue;
1601 edge.idx = k;
1602 edge.r = 0;
1604 if (d2d_cdt_edge_origin(cdt, &edge) == start_vertex)
1606 found = TRUE;
1607 break;
1609 d2d_cdt_edge_sym(&edge, &edge);
1610 if (d2d_cdt_edge_origin(cdt, &edge) == start_vertex)
1612 found = TRUE;
1613 break;
1617 if (!found)
1619 ERR("Edge not found.\n");
1620 return FALSE;
1623 for (j = 0; j < figure->vertex_count; start_vertex = end_vertex, ++j)
1625 p = bsearch(&figure->vertices[j], cdt->vertices,
1626 geometry->fill.vertex_count, sizeof(*p), d2d_cdt_compare_vertices);
1627 end_vertex = p - cdt->vertices;
1629 if (start_vertex == end_vertex)
1630 continue;
1632 if (!d2d_cdt_insert_segment(cdt, geometry, &edge, &new_edge, end_vertex))
1633 return FALSE;
1634 edge = new_edge;
1638 return TRUE;
1641 static BOOL d2d_geometry_intersections_add(struct d2d_geometry_intersections *i,
1642 const struct d2d_segment_idx *segment_idx, float t, D2D1_POINT_2F p)
1644 struct d2d_geometry_intersection *intersection;
1646 if (!d2d_array_reserve((void **)&i->intersections, &i->intersections_size,
1647 i->intersection_count + 1, sizeof(*i->intersections)))
1649 ERR("Failed to grow intersections array.\n");
1650 return FALSE;
1653 intersection = &i->intersections[i->intersection_count++];
1654 intersection->figure_idx = segment_idx->figure_idx;
1655 intersection->vertex_idx = segment_idx->vertex_idx;
1656 intersection->control_idx = segment_idx->control_idx;
1657 intersection->t = t;
1658 intersection->p = p;
1660 return TRUE;
1663 static int d2d_geometry_intersections_compare(const void *a, const void *b)
1665 const struct d2d_geometry_intersection *i0 = a;
1666 const struct d2d_geometry_intersection *i1 = b;
1668 if (i0->figure_idx != i1->figure_idx)
1669 return i0->figure_idx - i1->figure_idx;
1670 if (i0->vertex_idx != i1->vertex_idx)
1671 return i0->vertex_idx - i1->vertex_idx;
1672 if (i0->t != i1->t)
1673 return i0->t > i1->t ? 1 : -1;
1674 return 0;
1677 static BOOL d2d_geometry_intersect_line_line(struct d2d_geometry *geometry,
1678 struct d2d_geometry_intersections *intersections, const struct d2d_segment_idx *idx_p,
1679 const struct d2d_segment_idx *idx_q)
1681 D2D1_POINT_2F v_p, v_q, v_qp, intersection;
1682 const D2D1_POINT_2F *p[2], *q[2];
1683 const struct d2d_figure *figure;
1684 float s, t, det;
1685 size_t next;
1687 figure = &geometry->u.path.figures[idx_p->figure_idx];
1688 p[0] = &figure->vertices[idx_p->vertex_idx];
1689 next = idx_p->vertex_idx + 1;
1690 if (next == figure->vertex_count)
1691 next = 0;
1692 p[1] = &figure->vertices[next];
1694 figure = &geometry->u.path.figures[idx_q->figure_idx];
1695 q[0] = &figure->vertices[idx_q->vertex_idx];
1696 next = idx_q->vertex_idx + 1;
1697 if (next == figure->vertex_count)
1698 next = 0;
1699 q[1] = &figure->vertices[next];
1701 d2d_point_subtract(&v_p, p[1], p[0]);
1702 d2d_point_subtract(&v_q, q[1], q[0]);
1703 d2d_point_subtract(&v_qp, p[0], q[0]);
1705 det = v_p.x * v_q.y - v_p.y * v_q.x;
1706 if (det == 0.0f)
1707 return TRUE;
1709 s = (v_q.x * v_qp.y - v_q.y * v_qp.x) / det;
1710 t = (v_p.x * v_qp.y - v_p.y * v_qp.x) / det;
1712 if (s < 0.0f || s > 1.0f || t < 0.0f || t > 1.0f)
1713 return TRUE;
1715 intersection.x = p[0]->x + v_p.x * s;
1716 intersection.y = p[0]->y + v_p.y * s;
1718 if (s > 0.0f && s < 1.0f && !d2d_geometry_intersections_add(intersections, idx_p, s, intersection))
1719 return FALSE;
1721 if (t > 0.0f && t < 1.0f && !d2d_geometry_intersections_add(intersections, idx_q, t, intersection))
1722 return FALSE;
1724 return TRUE;
1727 static BOOL d2d_geometry_add_bezier_line_intersections(struct d2d_geometry *geometry,
1728 struct d2d_geometry_intersections *intersections, const struct d2d_segment_idx *idx_p,
1729 const D2D1_POINT_2F **p, const struct d2d_segment_idx *idx_q, const D2D1_POINT_2F **q, float s)
1731 D2D1_POINT_2F intersection;
1732 float t;
1734 d2d_point_calculate_bezier(&intersection, p[0], p[1], p[2], s);
1735 if (fabsf(q[1]->x - q[0]->x) > fabsf(q[1]->y - q[0]->y))
1736 t = (intersection.x - q[0]->x) / (q[1]->x - q[0]->x);
1737 else
1738 t = (intersection.y - q[0]->y) / (q[1]->y - q[0]->y);
1739 if (t < 0.0f || t > 1.0f)
1740 return TRUE;
1742 d2d_point_lerp(&intersection, q[0], q[1], t);
1744 if (s > 0.0f && s < 1.0f && !d2d_geometry_intersections_add(intersections, idx_p, s, intersection))
1745 return FALSE;
1747 if (t > 0.0f && t < 1.0f && !d2d_geometry_intersections_add(intersections, idx_q, t, intersection))
1748 return FALSE;
1750 return TRUE;
1753 static BOOL d2d_geometry_intersect_bezier_line(struct d2d_geometry *geometry,
1754 struct d2d_geometry_intersections *intersections,
1755 const struct d2d_segment_idx *idx_p, const struct d2d_segment_idx *idx_q)
1757 const D2D1_POINT_2F *p[3], *q[2];
1758 const struct d2d_figure *figure;
1759 float y[3], root, theta, d, e;
1760 size_t next;
1762 figure = &geometry->u.path.figures[idx_p->figure_idx];
1763 p[0] = &figure->vertices[idx_p->vertex_idx];
1764 p[1] = &figure->bezier_controls[idx_p->control_idx];
1765 next = idx_p->vertex_idx + 1;
1766 if (next == figure->vertex_count)
1767 next = 0;
1768 p[2] = &figure->vertices[next];
1770 figure = &geometry->u.path.figures[idx_q->figure_idx];
1771 q[0] = &figure->vertices[idx_q->vertex_idx];
1772 next = idx_q->vertex_idx + 1;
1773 if (next == figure->vertex_count)
1774 next = 0;
1775 q[1] = &figure->vertices[next];
1777 /* Align the line with x-axis. */
1778 theta = -atan2f(q[1]->y - q[0]->y, q[1]->x - q[0]->x);
1779 y[0] = (p[0]->x - q[0]->x) * sinf(theta) + (p[0]->y - q[0]->y) * cosf(theta);
1780 y[1] = (p[1]->x - q[0]->x) * sinf(theta) + (p[1]->y - q[0]->y) * cosf(theta);
1781 y[2] = (p[2]->x - q[0]->x) * sinf(theta) + (p[2]->y - q[0]->y) * cosf(theta);
1783 /* Intersect the transformed curve with the x-axis.
1785 * f(t) = (1 - t)²P₀ + 2(1 - t)tP₁ + t²P₂
1786 * = (P₀ - 2P₁ + P₂)t² + 2(P₁ - P₀)t + P₀
1788 * a = P₀ - 2P₁ + P₂
1789 * b = 2(P₁ - P₀)
1790 * c = P₀
1792 * f(t) = 0
1793 * t = (-b ± √(b² - 4ac)) / 2a
1794 * = (-2(P₁ - P₀) ± √((2(P₁ - P₀))² - 4((P₀ - 2P₁ + P₂)P₀))) / 2(P₀ - 2P₁ + P₂)
1795 * = (2P₀ - 2P₁ ± √(4P₀² + 4P₁² - 8P₀P₁ - 4P₀² + 8P₀P₁ - 4P₀P₂)) / (2P₀ - 4P₁ + 2P₂)
1796 * = (P₀ - P₁ ± √(P₁² - P₀P₂)) / (P₀ - 2P₁ + P₂) */
1798 d = y[0] - 2 * y[1] + y[2];
1799 if (d == 0.0f)
1801 /* P₀ - 2P₁ + P₂ = 0
1802 * f(t) = (P₀ - 2P₁ + P₂)t² + 2(P₁ - P₀)t + P₀ = 0
1803 * t = -P₀ / 2(P₁ - P₀) */
1804 root = -y[0] / (2.0f * (y[1] - y[0]));
1805 if (root < 0.0f || root > 1.0f)
1806 return TRUE;
1808 return d2d_geometry_add_bezier_line_intersections(geometry, intersections, idx_p, p, idx_q, q, root);
1811 e = y[1] * y[1] - y[0] * y[2];
1812 if (e < 0.0f)
1813 return TRUE;
1815 root = (y[0] - y[1] + sqrtf(e)) / d;
1816 if (root >= 0.0f && root <= 1.0f && !d2d_geometry_add_bezier_line_intersections(geometry,
1817 intersections, idx_p, p, idx_q, q, root))
1818 return FALSE;
1820 root = (y[0] - y[1] - sqrtf(e)) / d;
1821 if (root >= 0.0f && root <= 1.0f && !d2d_geometry_add_bezier_line_intersections(geometry,
1822 intersections, idx_p, p, idx_q, q, root))
1823 return FALSE;
1825 return TRUE;
1828 static BOOL d2d_geometry_intersect_bezier_bezier(struct d2d_geometry *geometry,
1829 struct d2d_geometry_intersections *intersections,
1830 const struct d2d_segment_idx *idx_p, float start_p, float end_p,
1831 const struct d2d_segment_idx *idx_q, float start_q, float end_q)
1833 const D2D1_POINT_2F *p[3], *q[3];
1834 const struct d2d_figure *figure;
1835 D2D_RECT_F p_bounds, q_bounds;
1836 D2D1_POINT_2F intersection;
1837 float centre_p, centre_q;
1838 size_t next;
1840 figure = &geometry->u.path.figures[idx_p->figure_idx];
1841 p[0] = &figure->vertices[idx_p->vertex_idx];
1842 p[1] = &figure->bezier_controls[idx_p->control_idx];
1843 next = idx_p->vertex_idx + 1;
1844 if (next == figure->vertex_count)
1845 next = 0;
1846 p[2] = &figure->vertices[next];
1848 figure = &geometry->u.path.figures[idx_q->figure_idx];
1849 q[0] = &figure->vertices[idx_q->vertex_idx];
1850 q[1] = &figure->bezier_controls[idx_q->control_idx];
1851 next = idx_q->vertex_idx + 1;
1852 if (next == figure->vertex_count)
1853 next = 0;
1854 q[2] = &figure->vertices[next];
1856 d2d_rect_get_bezier_segment_bounds(&p_bounds, p[0], p[1], p[2], start_p, end_p);
1857 d2d_rect_get_bezier_segment_bounds(&q_bounds, q[0], q[1], q[2], start_q, end_q);
1859 if (!d2d_rect_check_overlap(&p_bounds, &q_bounds))
1860 return TRUE;
1862 centre_p = (start_p + end_p) / 2.0f;
1863 centre_q = (start_q + end_q) / 2.0f;
1865 if (end_p - start_p < 1e-3f)
1867 d2d_point_calculate_bezier(&intersection, p[0], p[1], p[2], centre_p);
1868 if (start_p > 0.0f && end_p < 1.0f && !d2d_geometry_intersections_add(intersections,
1869 idx_p, centre_p, intersection))
1870 return FALSE;
1871 if (start_q > 0.0f && end_q < 1.0f && !d2d_geometry_intersections_add(intersections,
1872 idx_q, centre_q, intersection))
1873 return FALSE;
1874 return TRUE;
1877 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1878 idx_p, start_p, centre_p, idx_q, start_q, centre_q))
1879 return FALSE;
1880 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1881 idx_p, start_p, centre_p, idx_q, centre_q, end_q))
1882 return FALSE;
1883 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1884 idx_p, centre_p, end_p, idx_q, start_q, centre_q))
1885 return FALSE;
1886 if (!d2d_geometry_intersect_bezier_bezier(geometry, intersections,
1887 idx_p, centre_p, end_p, idx_q, centre_q, end_q))
1888 return FALSE;
1890 return TRUE;
1893 static BOOL d2d_geometry_apply_intersections(struct d2d_geometry *geometry,
1894 struct d2d_geometry_intersections *intersections)
1896 size_t vertex_offset, control_offset, next, i;
1897 struct d2d_geometry_intersection *inter;
1898 enum d2d_vertex_type vertex_type;
1899 const D2D1_POINT_2F *p[3];
1900 struct d2d_figure *figure;
1901 D2D1_POINT_2F q[2];
1902 float t, t_prev;
1904 for (i = 0; i < intersections->intersection_count; ++i)
1906 inter = &intersections->intersections[i];
1907 if (!i || inter->figure_idx != intersections->intersections[i - 1].figure_idx)
1908 vertex_offset = control_offset = 0;
1910 figure = &geometry->u.path.figures[inter->figure_idx];
1911 vertex_type = figure->vertex_types[inter->vertex_idx + vertex_offset];
1912 if (vertex_type != D2D_VERTEX_TYPE_BEZIER && vertex_type != D2D_VERTEX_TYPE_SPLIT_BEZIER)
1914 if (!d2d_figure_insert_vertex(&geometry->u.path.figures[inter->figure_idx],
1915 inter->vertex_idx + vertex_offset + 1, inter->p))
1916 return FALSE;
1917 ++vertex_offset;
1918 continue;
1921 t = inter->t;
1922 if (i && inter->figure_idx == intersections->intersections[i - 1].figure_idx
1923 && inter->vertex_idx == intersections->intersections[i - 1].vertex_idx)
1925 t_prev = intersections->intersections[i - 1].t;
1926 if (t - t_prev < 1e-3f)
1928 inter->t = intersections->intersections[i - 1].t;
1929 continue;
1931 t = (t - t_prev) / (1.0f - t_prev);
1934 p[0] = &figure->vertices[inter->vertex_idx + vertex_offset];
1935 p[1] = &figure->bezier_controls[inter->control_idx + control_offset];
1936 next = inter->vertex_idx + vertex_offset + 1;
1937 if (next == figure->vertex_count)
1938 next = 0;
1939 p[2] = &figure->vertices[next];
1941 d2d_point_lerp(&q[0], p[0], p[1], t);
1942 d2d_point_lerp(&q[1], p[1], p[2], t);
1944 figure->bezier_controls[inter->control_idx + control_offset] = q[0];
1945 if (!(d2d_figure_insert_bezier_control(figure, inter->control_idx + control_offset + 1, &q[1])))
1946 return FALSE;
1947 ++control_offset;
1949 if (!(d2d_figure_insert_vertex(figure, inter->vertex_idx + vertex_offset + 1, inter->p)))
1950 return FALSE;
1951 figure->vertex_types[inter->vertex_idx + vertex_offset + 1] = D2D_VERTEX_TYPE_SPLIT_BEZIER;
1952 ++vertex_offset;
1955 return TRUE;
1958 /* Intersect the geometry's segments with themselves. This uses the
1959 * straightforward approach of testing everything against everything, but
1960 * there certainly exist more scalable algorithms for this. */
1961 static BOOL d2d_geometry_intersect_self(struct d2d_geometry *geometry)
1963 struct d2d_geometry_intersections intersections = {0};
1964 const struct d2d_figure *figure_p, *figure_q;
1965 struct d2d_segment_idx idx_p, idx_q;
1966 enum d2d_vertex_type type_p, type_q;
1967 BOOL ret = FALSE;
1968 size_t max_q;
1970 if (!geometry->u.path.figure_count)
1971 return TRUE;
1973 for (idx_p.figure_idx = 0; idx_p.figure_idx < geometry->u.path.figure_count; ++idx_p.figure_idx)
1975 figure_p = &geometry->u.path.figures[idx_p.figure_idx];
1976 idx_p.control_idx = 0;
1977 for (idx_p.vertex_idx = 0; idx_p.vertex_idx < figure_p->vertex_count; ++idx_p.vertex_idx)
1979 type_p = figure_p->vertex_types[idx_p.vertex_idx];
1980 for (idx_q.figure_idx = 0; idx_q.figure_idx <= idx_p.figure_idx; ++idx_q.figure_idx)
1982 figure_q = &geometry->u.path.figures[idx_q.figure_idx];
1983 if (idx_q.figure_idx != idx_p.figure_idx)
1985 if (!d2d_rect_check_overlap(&figure_p->bounds, &figure_q->bounds))
1986 continue;
1987 max_q = figure_q->vertex_count;
1989 else
1991 max_q = idx_p.vertex_idx;
1994 idx_q.control_idx = 0;
1995 for (idx_q.vertex_idx = 0; idx_q.vertex_idx < max_q; ++idx_q.vertex_idx)
1997 type_q = figure_q->vertex_types[idx_q.vertex_idx];
1998 if (type_q == D2D_VERTEX_TYPE_BEZIER)
2000 if (type_p == D2D_VERTEX_TYPE_BEZIER)
2002 if (!d2d_geometry_intersect_bezier_bezier(geometry, &intersections,
2003 &idx_p, 0.0f, 1.0f, &idx_q, 0.0f, 1.0f))
2004 goto done;
2006 else
2008 if (!d2d_geometry_intersect_bezier_line(geometry, &intersections, &idx_q, &idx_p))
2009 goto done;
2011 ++idx_q.control_idx;
2013 else
2015 if (type_p == D2D_VERTEX_TYPE_BEZIER)
2017 if (!d2d_geometry_intersect_bezier_line(geometry, &intersections, &idx_p, &idx_q))
2018 goto done;
2020 else
2022 if (!d2d_geometry_intersect_line_line(geometry, &intersections, &idx_p, &idx_q))
2023 goto done;
2028 if (type_p == D2D_VERTEX_TYPE_BEZIER)
2029 ++idx_p.control_idx;
2033 qsort(intersections.intersections, intersections.intersection_count,
2034 sizeof(*intersections.intersections), d2d_geometry_intersections_compare);
2035 ret = d2d_geometry_apply_intersections(geometry, &intersections);
2037 done:
2038 HeapFree(GetProcessHeap(), 0, intersections.intersections);
2039 return ret;
2042 static HRESULT d2d_path_geometry_triangulate(struct d2d_geometry *geometry)
2044 struct d2d_cdt_edge_ref left_edge, right_edge;
2045 size_t vertex_count, i, j;
2046 struct d2d_cdt cdt = {0};
2047 D2D1_POINT_2F *vertices;
2049 for (i = 0, vertex_count = 0; i < geometry->u.path.figure_count; ++i)
2051 vertex_count += geometry->u.path.figures[i].vertex_count;
2054 if (vertex_count < 3)
2056 WARN("Geometry has %lu vertices.\n", (long)vertex_count);
2057 return S_OK;
2060 if (!(vertices = HeapAlloc(GetProcessHeap(), 0, vertex_count * sizeof(*vertices))))
2061 return E_OUTOFMEMORY;
2063 for (i = 0, j = 0; i < geometry->u.path.figure_count; ++i)
2065 memcpy(&vertices[j], geometry->u.path.figures[i].vertices,
2066 geometry->u.path.figures[i].vertex_count * sizeof(*vertices));
2067 j += geometry->u.path.figures[i].vertex_count;
2070 /* Sort vertices, eliminate duplicates. */
2071 qsort(vertices, vertex_count, sizeof(*vertices), d2d_cdt_compare_vertices);
2072 for (i = 1; i < vertex_count; ++i)
2074 if (!memcmp(&vertices[i - 1], &vertices[i], sizeof(*vertices)))
2076 --vertex_count;
2077 memmove(&vertices[i], &vertices[i + 1], (vertex_count - i) * sizeof(*vertices));
2078 --i;
2082 geometry->fill.vertices = vertices;
2083 geometry->fill.vertex_count = vertex_count;
2085 cdt.free_edge = ~0u;
2086 cdt.vertices = vertices;
2087 if (!d2d_cdt_triangulate(&cdt, 0, vertex_count, &left_edge, &right_edge))
2088 goto fail;
2089 if (!d2d_cdt_insert_segments(&cdt, geometry))
2090 goto fail;
2091 if (!d2d_cdt_generate_faces(&cdt, geometry))
2092 goto fail;
2094 HeapFree(GetProcessHeap(), 0, cdt.edges);
2095 return S_OK;
2097 fail:
2098 geometry->fill.vertices = NULL;
2099 geometry->fill.vertex_count = 0;
2100 HeapFree(GetProcessHeap(), 0, vertices);
2101 HeapFree(GetProcessHeap(), 0, cdt.edges);
2102 return E_FAIL;
2105 static BOOL d2d_path_geometry_add_figure(struct d2d_geometry *geometry)
2107 struct d2d_figure *figure;
2109 if (!d2d_array_reserve((void **)&geometry->u.path.figures, &geometry->u.path.figures_size,
2110 geometry->u.path.figure_count + 1, sizeof(*geometry->u.path.figures)))
2112 ERR("Failed to grow figures array.\n");
2113 return FALSE;
2116 figure = &geometry->u.path.figures[geometry->u.path.figure_count];
2117 memset(figure, 0, sizeof(*figure));
2118 figure->bounds.left = FLT_MAX;
2119 figure->bounds.top = FLT_MAX;
2120 figure->bounds.right = -FLT_MAX;
2121 figure->bounds.bottom = -FLT_MAX;
2123 ++geometry->u.path.figure_count;
2124 return TRUE;
2127 static BOOL d2d_geometry_outline_add_join(struct d2d_geometry *geometry,
2128 const D2D1_POINT_2F *prev, const D2D1_POINT_2F *p0, const D2D1_POINT_2F *next)
2130 D2D1_POINT_2F q_prev, q_next;
2131 struct d2d_outline_vertex *v;
2132 struct d2d_face *f;
2133 size_t base_idx;
2134 float ccw;
2136 if (!d2d_array_reserve((void **)&geometry->outline.vertices, &geometry->outline.vertices_size,
2137 geometry->outline.vertex_count + 4, sizeof(*geometry->outline.vertices)))
2139 ERR("Failed to grow outline vertices array.\n");
2140 return FALSE;
2142 base_idx = geometry->outline.vertex_count;
2143 v = &geometry->outline.vertices[base_idx];
2145 if (!d2d_array_reserve((void **)&geometry->outline.faces, &geometry->outline.faces_size,
2146 geometry->outline.face_count + 2, sizeof(*geometry->outline.faces)))
2148 ERR("Failed to grow outline faces array.\n");
2149 return FALSE;
2151 f = &geometry->outline.faces[geometry->outline.face_count];
2153 d2d_point_subtract(&q_prev, p0, prev);
2154 d2d_point_subtract(&q_next, next, p0);
2156 d2d_point_normalise(&q_prev);
2157 d2d_point_normalise(&q_next);
2159 ccw = d2d_point_ccw(p0, prev, next);
2160 if (ccw == 0.0f)
2162 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2163 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2164 d2d_outline_vertex_set(&v[2], p0->x + 25.0f * q_prev.x, p0->y + 25.0f * q_prev.y,
2165 -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2166 d2d_outline_vertex_set(&v[3], p0->x + 25.0f * q_prev.x, p0->y + 25.0f * q_prev.y,
2167 q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2169 else if (ccw < 0.0f)
2171 d2d_outline_vertex_set(&v[0], p0->x, p0->y, 0.0f, 0.0f, 0.0f, 0.0f);
2172 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2173 d2d_outline_vertex_set(&v[2], p0->x, p0->y, -q_next.x, -q_next.y, -q_prev.x, -q_prev.y);
2174 d2d_outline_vertex_set(&v[3], p0->x, p0->y, -q_prev.x, -q_prev.y, -q_prev.x, -q_prev.y);
2176 else
2178 d2d_outline_vertex_set(&v[0], p0->x, p0->y, 0.0f, 0.0f, 0.0f, 0.0f);
2179 d2d_outline_vertex_set(&v[1], p0->x, p0->y, q_prev.x, q_prev.y, q_prev.x, q_prev.y);
2180 d2d_outline_vertex_set(&v[2], p0->x, p0->y, q_prev.x, q_prev.y, q_next.x, q_next.y);
2181 d2d_outline_vertex_set(&v[3], p0->x, p0->y, q_next.x, q_next.y, q_next.x, q_next.y);
2183 geometry->outline.vertex_count += 4;
2185 d2d_face_set(&f[0], base_idx + 1, base_idx + 0, base_idx + 2);
2186 d2d_face_set(&f[1], base_idx + 2, base_idx + 0, base_idx + 3);
2187 geometry->outline.face_count += 2;
2189 return TRUE;
2192 static BOOL d2d_geometry_outline_add_line_segment(struct d2d_geometry *geometry,
2193 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *next)
2195 struct d2d_outline_vertex *v;
2196 D2D1_POINT_2F q_next;
2197 struct d2d_face *f;
2198 size_t base_idx;
2200 if (!d2d_array_reserve((void **)&geometry->outline.vertices, &geometry->outline.vertices_size,
2201 geometry->outline.vertex_count + 4, sizeof(*geometry->outline.vertices)))
2203 ERR("Failed to grow outline vertices array.\n");
2204 return FALSE;
2206 base_idx = geometry->outline.vertex_count;
2207 v = &geometry->outline.vertices[base_idx];
2209 if (!d2d_array_reserve((void **)&geometry->outline.faces, &geometry->outline.faces_size,
2210 geometry->outline.face_count + 2, sizeof(*geometry->outline.faces)))
2212 ERR("Failed to grow outline faces array.\n");
2213 return FALSE;
2215 f = &geometry->outline.faces[geometry->outline.face_count];
2217 d2d_point_subtract(&q_next, next, p0);
2218 d2d_point_normalise(&q_next);
2220 d2d_outline_vertex_set(&v[0], p0->x, p0->y, q_next.x, q_next.y, q_next.x, q_next.y);
2221 d2d_outline_vertex_set(&v[1], p0->x, p0->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2222 d2d_outline_vertex_set(&v[2], next->x, next->y, q_next.x, q_next.y, q_next.x, q_next.y);
2223 d2d_outline_vertex_set(&v[3], next->x, next->y, -q_next.x, -q_next.y, -q_next.x, -q_next.y);
2224 geometry->outline.vertex_count += 4;
2226 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2227 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2228 geometry->outline.face_count += 2;
2230 return TRUE;
2233 static BOOL d2d_geometry_outline_add_bezier_segment(struct d2d_geometry *geometry,
2234 const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2)
2236 struct d2d_bezier_outline_vertex *b;
2237 D2D1_POINT_2F r0, r1, r2;
2238 D2D1_POINT_2F q0, q1, q2;
2239 struct d2d_face *f;
2240 size_t base_idx;
2242 if (!d2d_array_reserve((void **)&geometry->outline.beziers, &geometry->outline.beziers_size,
2243 geometry->outline.bezier_count + 7, sizeof(*geometry->outline.beziers)))
2245 ERR("Failed to grow outline beziers array.\n");
2246 return FALSE;
2248 base_idx = geometry->outline.bezier_count;
2249 b = &geometry->outline.beziers[base_idx];
2251 if (!d2d_array_reserve((void **)&geometry->outline.bezier_faces, &geometry->outline.bezier_faces_size,
2252 geometry->outline.bezier_face_count + 5, sizeof(*geometry->outline.bezier_faces)))
2254 ERR("Failed to grow outline faces array.\n");
2255 return FALSE;
2257 f = &geometry->outline.bezier_faces[geometry->outline.bezier_face_count];
2259 d2d_point_lerp(&q0, p0, p1, 0.5f);
2260 d2d_point_lerp(&q1, p1, p2, 0.5f);
2261 d2d_point_lerp(&q2, &q0, &q1, 0.5f);
2263 d2d_point_subtract(&r0, &q0, p0);
2264 d2d_point_subtract(&r1, &q1, &q0);
2265 d2d_point_subtract(&r2, p2, &q1);
2267 d2d_point_normalise(&r0);
2268 d2d_point_normalise(&r1);
2269 d2d_point_normalise(&r2);
2271 if (d2d_point_ccw(p0, p1, p2) > 0.0f)
2273 d2d_point_scale(&r0, -1.0f);
2274 d2d_point_scale(&r1, -1.0f);
2275 d2d_point_scale(&r2, -1.0f);
2278 d2d_bezier_outline_vertex_set(&b[0], p0, p0, p1, p2, r0.x, r0.y, r0.x, r0.y);
2279 d2d_bezier_outline_vertex_set(&b[1], p0, p0, p1, p2, -r0.x, -r0.y, -r0.x, -r0.y);
2280 d2d_bezier_outline_vertex_set(&b[2], &q0, p0, p1, p2, r0.x, r0.y, r1.x, r1.y);
2281 d2d_bezier_outline_vertex_set(&b[3], &q2, p0, p1, p2, -r1.x, -r1.y, -r1.x, -r1.y);
2282 d2d_bezier_outline_vertex_set(&b[4], &q1, p0, p1, p2, r1.x, r1.y, r2.x, r2.y);
2283 d2d_bezier_outline_vertex_set(&b[5], p2, p0, p1, p2, -r2.x, -r2.y, -r2.x, -r2.y);
2284 d2d_bezier_outline_vertex_set(&b[6], p2, p0, p1, p2, r2.x, r2.y, r2.x, r2.y);
2285 geometry->outline.bezier_count += 7;
2287 d2d_face_set(&f[0], base_idx + 0, base_idx + 1, base_idx + 2);
2288 d2d_face_set(&f[1], base_idx + 2, base_idx + 1, base_idx + 3);
2289 d2d_face_set(&f[2], base_idx + 3, base_idx + 4, base_idx + 2);
2290 d2d_face_set(&f[3], base_idx + 5, base_idx + 4, base_idx + 3);
2291 d2d_face_set(&f[4], base_idx + 5, base_idx + 6, base_idx + 4);
2292 geometry->outline.bezier_face_count += 5;
2294 return TRUE;
2297 static BOOL d2d_geometry_add_figure_outline(struct d2d_geometry *geometry,
2298 struct d2d_figure *figure, D2D1_FIGURE_END figure_end)
2300 const D2D1_POINT_2F *prev, *p0, *next;
2301 enum d2d_vertex_type prev_type, type;
2302 size_t bezier_idx, i;
2304 for (i = 0, bezier_idx = 0; i < figure->vertex_count; ++i)
2306 type = figure->vertex_types[i];
2307 if (type == D2D_VERTEX_TYPE_NONE)
2308 continue;
2310 p0 = &figure->vertices[i];
2312 if (!i)
2314 prev_type = figure->vertex_types[figure->vertex_count - 1];
2315 if (prev_type == D2D_VERTEX_TYPE_BEZIER)
2316 prev = &figure->bezier_controls[figure->bezier_control_count - 1];
2317 else
2318 prev = &figure->vertices[figure->vertex_count - 1];
2320 else
2322 prev_type = figure->vertex_types[i - 1];
2323 if (prev_type == D2D_VERTEX_TYPE_BEZIER)
2324 prev = &figure->bezier_controls[bezier_idx - 1];
2325 else
2326 prev = &figure->vertices[i - 1];
2329 if (type == D2D_VERTEX_TYPE_BEZIER)
2330 next = &figure->bezier_controls[bezier_idx++];
2331 else if (i == figure->vertex_count - 1)
2332 next = &figure->vertices[0];
2333 else
2334 next = &figure->vertices[i + 1];
2336 if ((figure_end == D2D1_FIGURE_END_CLOSED || (i && i < figure->vertex_count - 1))
2337 && !d2d_geometry_outline_add_join(geometry, prev, p0, next))
2339 ERR("Failed to add join.\n");
2340 return FALSE;
2343 if (type == D2D_VERTEX_TYPE_LINE && (figure_end == D2D1_FIGURE_END_CLOSED || i < figure->vertex_count - 1)
2344 && !d2d_geometry_outline_add_line_segment(geometry, p0, next))
2346 ERR("Failed to add line segment.\n");
2347 return FALSE;
2349 else if (type == D2D_VERTEX_TYPE_BEZIER)
2351 const D2D1_POINT_2F *p2;
2353 if (i == figure->vertex_count - 1)
2354 p2 = &figure->vertices[0];
2355 else
2356 p2 = &figure->vertices[i + 1];
2358 if (!d2d_geometry_outline_add_bezier_segment(geometry, p0, next, p2))
2360 ERR("Failed to add bezier segment.\n");
2361 return FALSE;
2366 return TRUE;
2369 static void d2d_geometry_cleanup(struct d2d_geometry *geometry)
2371 HeapFree(GetProcessHeap(), 0, geometry->outline.bezier_faces);
2372 HeapFree(GetProcessHeap(), 0, geometry->outline.beziers);
2373 HeapFree(GetProcessHeap(), 0, geometry->outline.faces);
2374 HeapFree(GetProcessHeap(), 0, geometry->outline.vertices);
2375 HeapFree(GetProcessHeap(), 0, geometry->fill.bezier_vertices);
2376 HeapFree(GetProcessHeap(), 0, geometry->fill.faces);
2377 HeapFree(GetProcessHeap(), 0, geometry->fill.vertices);
2378 ID2D1Factory_Release(geometry->factory);
2381 static void d2d_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
2382 const D2D1_MATRIX_3X2_F *transform, const struct ID2D1GeometryVtbl *vtbl)
2384 geometry->ID2D1Geometry_iface.lpVtbl = vtbl;
2385 geometry->refcount = 1;
2386 ID2D1Factory_AddRef(geometry->factory = factory);
2387 geometry->transform = *transform;
2390 static inline struct d2d_geometry *impl_from_ID2D1GeometrySink(ID2D1GeometrySink *iface)
2392 return CONTAINING_RECORD(iface, struct d2d_geometry, u.path.ID2D1GeometrySink_iface);
2395 static HRESULT STDMETHODCALLTYPE d2d_geometry_sink_QueryInterface(ID2D1GeometrySink *iface, REFIID iid, void **out)
2397 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
2399 if (IsEqualGUID(iid, &IID_ID2D1GeometrySink)
2400 || IsEqualGUID(iid, &IID_ID2D1SimplifiedGeometrySink)
2401 || IsEqualGUID(iid, &IID_IUnknown))
2403 ID2D1GeometrySink_AddRef(iface);
2404 *out = iface;
2405 return S_OK;
2408 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
2410 *out = NULL;
2411 return E_NOINTERFACE;
2414 static ULONG STDMETHODCALLTYPE d2d_geometry_sink_AddRef(ID2D1GeometrySink *iface)
2416 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2418 TRACE("iface %p.\n", iface);
2420 return ID2D1Geometry_AddRef(&geometry->ID2D1Geometry_iface);
2423 static ULONG STDMETHODCALLTYPE d2d_geometry_sink_Release(ID2D1GeometrySink *iface)
2425 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2427 TRACE("iface %p.\n", iface);
2429 return ID2D1Geometry_Release(&geometry->ID2D1Geometry_iface);
2432 static void STDMETHODCALLTYPE d2d_geometry_sink_SetFillMode(ID2D1GeometrySink *iface, D2D1_FILL_MODE mode)
2434 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2436 TRACE("iface %p, mode %#x.\n", iface, mode);
2438 if (geometry->u.path.state == D2D_GEOMETRY_STATE_CLOSED)
2439 return;
2440 geometry->u.path.fill_mode = mode;
2443 static void STDMETHODCALLTYPE d2d_geometry_sink_SetSegmentFlags(ID2D1GeometrySink *iface, D2D1_PATH_SEGMENT flags)
2445 FIXME("iface %p, flags %#x stub!\n", iface, flags);
2448 static void STDMETHODCALLTYPE d2d_geometry_sink_BeginFigure(ID2D1GeometrySink *iface,
2449 D2D1_POINT_2F start_point, D2D1_FIGURE_BEGIN figure_begin)
2451 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2452 struct d2d_figure *figure;
2454 TRACE("iface %p, start_point {%.8e, %.8e}, figure_begin %#x.\n",
2455 iface, start_point.x, start_point.y, figure_begin);
2457 if (geometry->u.path.state != D2D_GEOMETRY_STATE_OPEN)
2459 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2460 return;
2463 if (figure_begin != D2D1_FIGURE_BEGIN_FILLED)
2464 FIXME("Ignoring figure_begin %#x.\n", figure_begin);
2466 if (!d2d_path_geometry_add_figure(geometry))
2468 ERR("Failed to add figure.\n");
2469 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2470 return;
2473 figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2474 if (figure_begin == D2D1_FIGURE_BEGIN_HOLLOW)
2475 figure->flags |= D2D_FIGURE_FLAG_HOLLOW;
2477 if (!d2d_figure_add_vertex(figure, start_point))
2479 ERR("Failed to add vertex.\n");
2480 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2481 return;
2484 geometry->u.path.state = D2D_GEOMETRY_STATE_FIGURE;
2487 static void STDMETHODCALLTYPE d2d_geometry_sink_AddLines(ID2D1GeometrySink *iface,
2488 const D2D1_POINT_2F *points, UINT32 count)
2490 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2491 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2492 unsigned int i;
2494 TRACE("iface %p, points %p, count %u.\n", iface, points, count);
2496 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2498 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2499 return;
2502 for (i = 0; i < count; ++i)
2504 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_LINE;
2505 if (!d2d_figure_add_vertex(figure, points[i]))
2507 ERR("Failed to add vertex.\n");
2508 return;
2512 geometry->u.path.segment_count += count;
2515 static void STDMETHODCALLTYPE d2d_geometry_sink_AddBeziers(ID2D1GeometrySink *iface,
2516 const D2D1_BEZIER_SEGMENT *beziers, UINT32 count)
2518 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2519 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2520 D2D1_POINT_2F p;
2521 unsigned int i;
2523 TRACE("iface %p, beziers %p, count %u.\n", iface, beziers, count);
2525 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2527 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2528 return;
2531 for (i = 0; i < count; ++i)
2533 D2D1_RECT_F bezier_bounds;
2535 /* FIXME: This tries to approximate a cubic bezier with a quadratic one. */
2536 p.x = (beziers[i].point1.x + beziers[i].point2.x) * 0.75f;
2537 p.y = (beziers[i].point1.y + beziers[i].point2.y) * 0.75f;
2538 p.x -= (figure->vertices[figure->vertex_count - 1].x + beziers[i].point3.x) * 0.25f;
2539 p.y -= (figure->vertices[figure->vertex_count - 1].y + beziers[i].point3.y) * 0.25f;
2540 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_BEZIER;
2542 d2d_rect_get_bezier_bounds(&bezier_bounds, &figure->vertices[figure->vertex_count - 1],
2543 &p, &beziers[i].point3);
2545 if (!d2d_figure_add_bezier_control(figure, &p))
2547 ERR("Failed to add bezier control.\n");
2548 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2549 return;
2552 if (!d2d_figure_add_vertex(figure, beziers[i].point3))
2554 ERR("Failed to add bezier vertex.\n");
2555 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2556 return;
2559 d2d_rect_union(&figure->bounds, &bezier_bounds);
2562 geometry->u.path.segment_count += count;
2565 static void STDMETHODCALLTYPE d2d_geometry_sink_EndFigure(ID2D1GeometrySink *iface, D2D1_FIGURE_END figure_end)
2567 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2568 struct d2d_figure *figure;
2570 TRACE("iface %p, figure_end %#x.\n", iface, figure_end);
2572 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2574 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2575 return;
2578 figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2579 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_LINE;
2580 if (figure_end == D2D1_FIGURE_END_CLOSED)
2582 ++geometry->u.path.segment_count;
2583 figure->flags |= D2D_FIGURE_FLAG_CLOSED;
2584 if (!memcmp(&figure->vertices[0], &figure->vertices[figure->vertex_count - 1], sizeof(*figure->vertices)))
2585 --figure->vertex_count;
2588 if (!d2d_geometry_add_figure_outline(geometry, figure, figure_end))
2590 ERR("Failed to add figure outline.\n");
2591 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2592 return;
2595 geometry->u.path.state = D2D_GEOMETRY_STATE_OPEN;
2598 static void d2d_path_geometry_free_figures(struct d2d_geometry *geometry)
2600 size_t i;
2602 if (!geometry->u.path.figures)
2603 return;
2605 for (i = 0; i < geometry->u.path.figure_count; ++i)
2607 HeapFree(GetProcessHeap(), 0, geometry->u.path.figures[i].bezier_controls);
2608 HeapFree(GetProcessHeap(), 0, geometry->u.path.figures[i].original_bezier_controls);
2609 HeapFree(GetProcessHeap(), 0, geometry->u.path.figures[i].vertices);
2611 HeapFree(GetProcessHeap(), 0, geometry->u.path.figures);
2612 geometry->u.path.figures = NULL;
2613 geometry->u.path.figures_size = 0;
2616 static BOOL d2d_geometry_get_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx, BOOL next)
2618 if (next)
2620 ++idx->vertex_idx;
2621 ++idx->control_idx;
2624 for (; idx->figure_idx < geometry->u.path.figure_count; ++idx->figure_idx, idx->vertex_idx = idx->control_idx = 0)
2626 struct d2d_figure *figure = &geometry->u.path.figures[idx->figure_idx];
2628 if (!figure->bezier_control_count)
2629 continue;
2631 for (; idx->vertex_idx < figure->vertex_count; ++idx->vertex_idx)
2633 if (figure->vertex_types[idx->vertex_idx] == D2D_VERTEX_TYPE_BEZIER
2634 || figure->vertex_types[idx->vertex_idx] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
2635 return TRUE;
2639 return FALSE;
2642 static BOOL d2d_geometry_get_first_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx)
2644 memset(idx, 0, sizeof(*idx));
2646 return d2d_geometry_get_bezier_segment_idx(geometry, idx, FALSE);
2649 static BOOL d2d_geometry_get_next_bezier_segment_idx(struct d2d_geometry *geometry, struct d2d_segment_idx *idx)
2651 return d2d_geometry_get_bezier_segment_idx(geometry, idx, TRUE);
2654 static BOOL d2d_geometry_check_bezier_overlap(struct d2d_geometry *geometry,
2655 const struct d2d_segment_idx *idx_p, const struct d2d_segment_idx *idx_q)
2657 const D2D1_POINT_2F *a[3], *b[3], *p[2], *q;
2658 const struct d2d_figure *figure;
2659 D2D1_POINT_2F v_q[3], v_p, v_qp;
2660 unsigned int i, j, score;
2661 float det, t;
2663 figure = &geometry->u.path.figures[idx_p->figure_idx];
2664 a[0] = &figure->vertices[idx_p->vertex_idx];
2665 a[1] = &figure->bezier_controls[idx_p->control_idx];
2666 if (idx_p->vertex_idx == figure->vertex_count - 1)
2667 a[2] = &figure->vertices[0];
2668 else
2669 a[2] = &figure->vertices[idx_p->vertex_idx + 1];
2671 figure = &geometry->u.path.figures[idx_q->figure_idx];
2672 b[0] = &figure->vertices[idx_q->vertex_idx];
2673 b[1] = &figure->bezier_controls[idx_q->control_idx];
2674 if (idx_q->vertex_idx == figure->vertex_count - 1)
2675 b[2] = &figure->vertices[0];
2676 else
2677 b[2] = &figure->vertices[idx_q->vertex_idx + 1];
2679 if (d2d_point_ccw(a[0], a[1], a[2]) == 0.0f || d2d_point_ccw(b[0], b[1], b[2]) == 0.0f)
2680 return FALSE;
2682 d2d_point_subtract(&v_q[0], b[1], b[0]);
2683 d2d_point_subtract(&v_q[1], b[2], b[0]);
2684 d2d_point_subtract(&v_q[2], b[1], b[2]);
2686 /* Check for intersections between the edges. Strictly speaking we'd only
2687 * need to check 8 of the 9 possible intersections, since if there's any
2688 * intersection there has to be a second intersection as well. */
2689 for (i = 0; i < 3; ++i)
2691 d2d_point_subtract(&v_p, a[(i & 1) + 1], a[i & 2]);
2692 for (j = 0; j < 3; ++j)
2694 det = v_p.x * v_q[j].y - v_p.y * v_q[j].x;
2695 if (det == 0.0f)
2696 continue;
2698 d2d_point_subtract(&v_qp, a[i & 2], b[j & 2]);
2699 t = (v_q[j].x * v_qp.y - v_q[j].y * v_qp.x) / det;
2700 if (t <= 0.0f || t >= 1.0f)
2701 continue;
2703 t = (v_p.x * v_qp.y - v_p.y * v_qp.x) / det;
2704 if (t <= 0.0f || t >= 1.0f)
2705 continue;
2707 return TRUE;
2711 /* Check if one triangle is contained within the other. */
2712 for (j = 0, score = 0, q = a[1], p[0] = b[2]; j < 3; ++j)
2714 p[1] = b[j];
2715 d2d_point_subtract(&v_p, p[1], p[0]);
2716 d2d_point_subtract(&v_qp, q, p[0]);
2718 if ((q->y < p[0]->y) != (q->y < p[1]->y) && v_qp.x < v_p.x * (v_qp.y / v_p.y))
2719 ++score;
2721 p[0] = p[1];
2724 if (score & 1)
2725 return TRUE;
2727 for (j = 0, score = 0, q = b[1], p[0] = a[2]; j < 3; ++j)
2729 p[1] = a[j];
2730 d2d_point_subtract(&v_p, p[1], p[0]);
2731 d2d_point_subtract(&v_qp, q, p[0]);
2733 if ((q->y < p[0]->y) != (q->y < p[1]->y) && v_qp.x < v_p.x * (v_qp.y / v_p.y))
2734 ++score;
2736 p[0] = p[1];
2739 return score & 1;
2742 static float d2d_geometry_bezier_ccw(struct d2d_geometry *geometry, const struct d2d_segment_idx *idx)
2744 const struct d2d_figure *figure = &geometry->u.path.figures[idx->figure_idx];
2745 size_t next = idx->vertex_idx + 1;
2747 if (next == figure->vertex_count)
2748 next = 0;
2750 return d2d_point_ccw(&figure->vertices[idx->vertex_idx],
2751 &figure->bezier_controls[idx->control_idx], &figure->vertices[next]);
2754 static BOOL d2d_geometry_split_bezier(struct d2d_geometry *geometry, const struct d2d_segment_idx *idx)
2756 const D2D1_POINT_2F *p[3];
2757 struct d2d_figure *figure;
2758 D2D1_POINT_2F q[3];
2759 size_t next;
2761 figure = &geometry->u.path.figures[idx->figure_idx];
2762 p[0] = &figure->vertices[idx->vertex_idx];
2763 p[1] = &figure->bezier_controls[idx->control_idx];
2764 next = idx->vertex_idx + 1;
2765 if (next == figure->vertex_count)
2766 next = 0;
2767 p[2] = &figure->vertices[next];
2769 d2d_point_lerp(&q[0], p[0], p[1], 0.5f);
2770 d2d_point_lerp(&q[1], p[1], p[2], 0.5f);
2771 d2d_point_lerp(&q[2], &q[0], &q[1], 0.5f);
2773 figure->bezier_controls[idx->control_idx] = q[0];
2774 if (!(d2d_figure_insert_bezier_control(figure, idx->control_idx + 1, &q[1])))
2775 return FALSE;
2776 if (!(d2d_figure_insert_vertex(figure, idx->vertex_idx + 1, q[2])))
2777 return FALSE;
2778 figure->vertex_types[idx->vertex_idx + 1] = D2D_VERTEX_TYPE_SPLIT_BEZIER;
2780 return TRUE;
2783 static HRESULT d2d_geometry_resolve_beziers(struct d2d_geometry *geometry)
2785 struct d2d_segment_idx idx_p, idx_q;
2786 struct d2d_bezier_vertex *b;
2787 const D2D1_POINT_2F *p[3];
2788 struct d2d_figure *figure;
2789 size_t bezier_idx, i;
2791 if (!d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_p))
2792 return S_OK;
2794 /* Split overlapping bezier control triangles. */
2795 while (d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_p))
2797 d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_q);
2798 while (idx_q.figure_idx < idx_p.figure_idx || idx_q.vertex_idx < idx_p.vertex_idx)
2800 while (d2d_geometry_check_bezier_overlap(geometry, &idx_p, &idx_q))
2802 if (fabsf(d2d_geometry_bezier_ccw(geometry, &idx_q)) > fabsf(d2d_geometry_bezier_ccw(geometry, &idx_p)))
2804 if (!d2d_geometry_split_bezier(geometry, &idx_q))
2805 return E_OUTOFMEMORY;
2806 if (idx_p.figure_idx == idx_q.figure_idx)
2808 ++idx_p.vertex_idx;
2809 ++idx_p.control_idx;
2812 else
2814 if (!d2d_geometry_split_bezier(geometry, &idx_p))
2815 return E_OUTOFMEMORY;
2818 d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_q);
2822 for (i = 0; i < geometry->u.path.figure_count; ++i)
2824 geometry->fill.bezier_vertex_count += 3 * geometry->u.path.figures[i].bezier_control_count;
2827 if (!(geometry->fill.bezier_vertices = HeapAlloc(GetProcessHeap(), 0,
2828 geometry->fill.bezier_vertex_count * sizeof(*geometry->fill.bezier_vertices))))
2830 ERR("Failed to allocate bezier vertices array.\n");
2831 geometry->fill.bezier_vertex_count = 0;
2832 return E_OUTOFMEMORY;
2835 bezier_idx = 0;
2836 d2d_geometry_get_first_bezier_segment_idx(geometry, &idx_p);
2837 for (;;)
2839 float sign = -1.0f;
2841 figure = &geometry->u.path.figures[idx_p.figure_idx];
2842 p[0] = &figure->vertices[idx_p.vertex_idx];
2843 p[1] = &figure->bezier_controls[idx_p.control_idx];
2845 i = idx_p.vertex_idx + 1;
2846 if (d2d_path_geometry_point_inside(geometry, p[1], FALSE))
2848 sign = 1.0f;
2849 d2d_figure_insert_vertex(figure, i, *p[1]);
2850 /* Inserting a vertex potentially invalidates p[0]. */
2851 p[0] = &figure->vertices[idx_p.vertex_idx];
2852 ++i;
2855 if (i == figure->vertex_count)
2856 i = 0;
2857 p[2] = &figure->vertices[i];
2859 b = &geometry->fill.bezier_vertices[bezier_idx * 3];
2860 d2d_bezier_vertex_set(&b[0], p[0], 0.0f, 0.0f, sign);
2861 d2d_bezier_vertex_set(&b[1], p[1], 0.5f, 0.0f, sign);
2862 d2d_bezier_vertex_set(&b[2], p[2], 1.0f, 1.0f, sign);
2864 if (!d2d_geometry_get_next_bezier_segment_idx(geometry, &idx_p))
2865 break;
2866 ++bezier_idx;
2869 return S_OK;
2872 static HRESULT STDMETHODCALLTYPE d2d_geometry_sink_Close(ID2D1GeometrySink *iface)
2874 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2875 HRESULT hr = E_FAIL;
2876 size_t i;
2878 TRACE("iface %p.\n", iface);
2880 if (geometry->u.path.state != D2D_GEOMETRY_STATE_OPEN)
2882 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
2883 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2884 return D2DERR_WRONG_STATE;
2886 geometry->u.path.state = D2D_GEOMETRY_STATE_CLOSED;
2888 for (i = 0; i < geometry->u.path.figure_count; ++i)
2890 struct d2d_figure *figure = &geometry->u.path.figures[i];
2891 size_t size = figure->bezier_control_count * sizeof(*figure->original_bezier_controls);
2892 if (!(figure->original_bezier_controls = HeapAlloc(GetProcessHeap(), 0, size)))
2893 goto done;
2894 memcpy(figure->original_bezier_controls, figure->bezier_controls, size);
2897 if (!d2d_geometry_intersect_self(geometry))
2898 goto done;
2899 if (FAILED(hr = d2d_geometry_resolve_beziers(geometry)))
2900 goto done;
2901 if (FAILED(hr = d2d_path_geometry_triangulate(geometry)))
2902 goto done;
2904 done:
2905 if (FAILED(hr))
2907 HeapFree(GetProcessHeap(), 0, geometry->fill.bezier_vertices);
2908 geometry->fill.bezier_vertex_count = 0;
2909 d2d_path_geometry_free_figures(geometry);
2910 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2912 return hr;
2915 static void STDMETHODCALLTYPE d2d_geometry_sink_AddLine(ID2D1GeometrySink *iface, D2D1_POINT_2F point)
2917 TRACE("iface %p, point {%.8e, %.8e}.\n", iface, point.x, point.y);
2919 d2d_geometry_sink_AddLines(iface, &point, 1);
2922 static void STDMETHODCALLTYPE d2d_geometry_sink_AddBezier(ID2D1GeometrySink *iface, const D2D1_BEZIER_SEGMENT *bezier)
2924 TRACE("iface %p, bezier %p.\n", iface, bezier);
2926 d2d_geometry_sink_AddBeziers(iface, bezier, 1);
2929 static void STDMETHODCALLTYPE d2d_geometry_sink_AddQuadraticBezier(ID2D1GeometrySink *iface,
2930 const D2D1_QUADRATIC_BEZIER_SEGMENT *bezier)
2932 TRACE("iface %p, bezier %p.\n", iface, bezier);
2934 ID2D1GeometrySink_AddQuadraticBeziers(iface, bezier, 1);
2937 static void STDMETHODCALLTYPE d2d_geometry_sink_AddQuadraticBeziers(ID2D1GeometrySink *iface,
2938 const D2D1_QUADRATIC_BEZIER_SEGMENT *beziers, UINT32 bezier_count)
2940 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2941 struct d2d_figure *figure = &geometry->u.path.figures[geometry->u.path.figure_count - 1];
2942 unsigned int i;
2944 TRACE("iface %p, beziers %p, bezier_count %u.\n", iface, beziers, bezier_count);
2946 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2948 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2949 return;
2952 for (i = 0; i < bezier_count; ++i)
2954 D2D1_RECT_F bezier_bounds;
2956 d2d_rect_get_bezier_bounds(&bezier_bounds, &figure->vertices[figure->vertex_count - 1],
2957 &beziers[i].point1, &beziers[i].point2);
2959 figure->vertex_types[figure->vertex_count - 1] = D2D_VERTEX_TYPE_BEZIER;
2960 if (!d2d_figure_add_bezier_control(figure, &beziers[i].point1))
2962 ERR("Failed to add bezier.\n");
2963 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2964 return;
2967 if (!d2d_figure_add_vertex(figure, beziers[i].point2))
2969 ERR("Failed to add bezier vertex.\n");
2970 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2971 return;
2974 d2d_rect_union(&figure->bounds, &bezier_bounds);
2977 geometry->u.path.segment_count += bezier_count;
2980 static void STDMETHODCALLTYPE d2d_geometry_sink_AddArc(ID2D1GeometrySink *iface, const D2D1_ARC_SEGMENT *arc)
2982 struct d2d_geometry *geometry = impl_from_ID2D1GeometrySink(iface);
2984 FIXME("iface %p, arc %p stub!\n", iface, arc);
2986 if (geometry->u.path.state != D2D_GEOMETRY_STATE_FIGURE)
2988 geometry->u.path.state = D2D_GEOMETRY_STATE_ERROR;
2989 return;
2992 if (!d2d_figure_add_vertex(&geometry->u.path.figures[geometry->u.path.figure_count - 1], arc->point))
2994 ERR("Failed to add vertex.\n");
2995 return;
2998 ++geometry->u.path.segment_count;
3001 static const struct ID2D1GeometrySinkVtbl d2d_geometry_sink_vtbl =
3003 d2d_geometry_sink_QueryInterface,
3004 d2d_geometry_sink_AddRef,
3005 d2d_geometry_sink_Release,
3006 d2d_geometry_sink_SetFillMode,
3007 d2d_geometry_sink_SetSegmentFlags,
3008 d2d_geometry_sink_BeginFigure,
3009 d2d_geometry_sink_AddLines,
3010 d2d_geometry_sink_AddBeziers,
3011 d2d_geometry_sink_EndFigure,
3012 d2d_geometry_sink_Close,
3013 d2d_geometry_sink_AddLine,
3014 d2d_geometry_sink_AddBezier,
3015 d2d_geometry_sink_AddQuadraticBezier,
3016 d2d_geometry_sink_AddQuadraticBeziers,
3017 d2d_geometry_sink_AddArc,
3020 static inline struct d2d_geometry *impl_from_ID2D1PathGeometry(ID2D1PathGeometry *iface)
3022 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3025 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_QueryInterface(ID2D1PathGeometry *iface, REFIID iid, void **out)
3027 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3029 if (IsEqualGUID(iid, &IID_ID2D1PathGeometry)
3030 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3031 || IsEqualGUID(iid, &IID_ID2D1Resource)
3032 || IsEqualGUID(iid, &IID_IUnknown))
3034 ID2D1PathGeometry_AddRef(iface);
3035 *out = iface;
3036 return S_OK;
3039 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3041 *out = NULL;
3042 return E_NOINTERFACE;
3045 static ULONG STDMETHODCALLTYPE d2d_path_geometry_AddRef(ID2D1PathGeometry *iface)
3047 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3048 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3050 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3052 return refcount;
3055 static ULONG STDMETHODCALLTYPE d2d_path_geometry_Release(ID2D1PathGeometry *iface)
3057 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3058 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3060 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3062 if (!refcount)
3064 d2d_path_geometry_free_figures(geometry);
3065 d2d_geometry_cleanup(geometry);
3066 HeapFree(GetProcessHeap(), 0, geometry);
3069 return refcount;
3072 static void STDMETHODCALLTYPE d2d_path_geometry_GetFactory(ID2D1PathGeometry *iface, ID2D1Factory **factory)
3074 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3076 TRACE("iface %p, factory %p.\n", iface, factory);
3078 ID2D1Factory_AddRef(*factory = geometry->factory);
3081 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetBounds(ID2D1PathGeometry *iface,
3082 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3084 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3085 size_t i;
3087 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3089 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3090 return D2DERR_WRONG_STATE;
3092 bounds->left = FLT_MAX;
3093 bounds->top = FLT_MAX;
3094 bounds->right = -FLT_MAX;
3095 bounds->bottom = -FLT_MAX;
3097 if (!transform)
3099 if (geometry->u.path.bounds.left > geometry->u.path.bounds.right)
3101 for (i = 0; i < geometry->u.path.figure_count; ++i)
3102 d2d_rect_union(&geometry->u.path.bounds, &geometry->u.path.figures[i].bounds);
3105 *bounds = geometry->u.path.bounds;
3106 return S_OK;
3109 for (i = 0; i < geometry->u.path.figure_count; ++i)
3111 const struct d2d_figure *figure = &geometry->u.path.figures[i];
3112 enum d2d_vertex_type type = D2D_VERTEX_TYPE_NONE;
3113 D2D1_RECT_F bezier_bounds;
3114 D2D1_POINT_2F p, p1, p2;
3115 size_t j, bezier_idx;
3117 /* Single vertex figures are reduced by CloseFigure(). */
3118 if (figure->vertex_count == 0)
3120 d2d_point_transform(&p, transform, figure->bounds.left, figure->bounds.top);
3121 d2d_rect_expand(bounds, &p);
3122 continue;
3125 for (j = 0; j < figure->vertex_count; ++j)
3127 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
3128 continue;
3130 p = figure->vertices[j];
3131 type = figure->vertex_types[j];
3132 d2d_point_transform(&p, transform, p.x, p.y);
3133 d2d_rect_expand(bounds, &p);
3134 break;
3137 for (bezier_idx = 0, ++j; j < figure->vertex_count; ++j)
3139 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE
3140 || figure->vertex_types[j] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
3141 continue;
3143 switch (type)
3145 case D2D_VERTEX_TYPE_LINE:
3146 p = figure->vertices[j];
3147 d2d_point_transform(&p, transform, p.x, p.y);
3148 d2d_rect_expand(bounds, &p);
3149 break;
3151 case D2D_VERTEX_TYPE_BEZIER:
3152 p1 = figure->original_bezier_controls[bezier_idx++];
3153 d2d_point_transform(&p1, transform, p1.x, p1.y);
3154 p2 = figure->vertices[j];
3155 d2d_point_transform(&p2, transform, p2.x, p2.y);
3156 d2d_rect_get_bezier_bounds(&bezier_bounds, &p, &p1, &p2);
3157 d2d_rect_union(bounds, &bezier_bounds);
3158 p = p2;
3159 break;
3161 default:
3162 FIXME("Unhandled vertex type %#x.\n", type);
3163 p = figure->vertices[j];
3164 d2d_point_transform(&p, transform, p.x, p.y);
3165 d2d_rect_expand(bounds, &p);
3166 break;
3169 type = figure->vertex_types[j];
3172 if (type == D2D_VERTEX_TYPE_BEZIER)
3174 p1 = figure->original_bezier_controls[bezier_idx++];
3175 d2d_point_transform(&p1, transform, p1.x, p1.y);
3176 p2 = figure->vertices[0];
3177 d2d_point_transform(&p2, transform, p2.x, p2.y);
3178 d2d_rect_get_bezier_bounds(&bezier_bounds, &p, &p1, &p2);
3179 d2d_rect_union(bounds, &bezier_bounds);
3183 return S_OK;
3186 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetWidenedBounds(ID2D1PathGeometry *iface, float stroke_width,
3187 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_RECT_F *bounds)
3189 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3190 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3192 return E_NOTIMPL;
3195 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_StrokeContainsPoint(ID2D1PathGeometry *iface,
3196 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3197 float tolerance, BOOL *contains)
3199 FIXME("iface %p, point {%.8e, %.8e}, stroke_width %.8e, stroke_style %p, "
3200 "transform %p, tolerance %.8e, contains %p stub!\n",
3201 iface, point.x, point.y, stroke_width, stroke_style, transform, tolerance, contains);
3203 return E_NOTIMPL;
3206 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_FillContainsPoint(ID2D1PathGeometry *iface,
3207 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3209 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3210 D2D1_MATRIX_3X2_F g_i;
3212 TRACE("iface %p, point {%.8e, %.8e}, transform %p, tolerance %.8e, contains %p.\n",
3213 iface, point.x, point.y, transform, tolerance, contains);
3215 if (transform)
3217 if (!d2d_matrix_invert(&g_i, transform))
3218 return D2DERR_UNSUPPORTED_OPERATION;
3219 d2d_point_transform(&point, &g_i, point.x, point.y);
3222 *contains = !!d2d_path_geometry_point_inside(geometry, &point, FALSE);
3224 TRACE("-> %#x.\n", *contains);
3226 return S_OK;
3229 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_CompareWithGeometry(ID2D1PathGeometry *iface,
3230 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3232 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3233 iface, geometry, transform, tolerance, relation);
3235 return E_NOTIMPL;
3238 static void d2d_geometry_flatten_cubic(ID2D1SimplifiedGeometrySink *sink, const D2D1_POINT_2F *p0,
3239 const D2D1_BEZIER_SEGMENT *b, float tolerance)
3241 D2D1_BEZIER_SEGMENT b0, b1;
3242 D2D1_POINT_2F q;
3243 float d;
3245 /* It's certainly possible to calculate the maximum deviation of the
3246 * approximation from the curve, but it's a little involved. Instead, note
3247 * that if the control points were evenly spaced and collinear, p1 would
3248 * be exactly between p0 and p2, and p2 would be exactly between p1 and
3249 * p3. The deviation is a decent enough approximation, and much easier to
3250 * calculate.
3252 * p1' = (p0 + p2) / 2
3253 * p2' = (p1 + p3) / 2
3254 * d = ‖p1 - p1'‖₁ + ‖p2 - p2'‖₁ */
3255 d2d_point_lerp(&q, p0, &b->point2, 0.5f);
3256 d2d_point_subtract(&q, &b->point1, &q);
3257 d = fabsf(q.x) + fabsf(q.y);
3258 d2d_point_lerp(&q, &b->point1, &b->point3, 0.5f);
3259 d2d_point_subtract(&q, &b->point2, &q);
3260 d += fabsf(q.x) + fabsf(q.y);
3261 if (d < tolerance)
3263 ID2D1SimplifiedGeometrySink_AddLines(sink, &b->point3, 1);
3264 return;
3267 d2d_point_lerp(&q, &b->point1, &b->point2, 0.5f);
3269 b1.point3 = b->point3;
3270 d2d_point_lerp(&b1.point2, &b1.point3, &b->point2, 0.5f);
3271 d2d_point_lerp(&b1.point1, &b1.point2, &q, 0.5f);
3273 d2d_point_lerp(&b0.point1, p0, &b->point1, 0.5f);
3274 d2d_point_lerp(&b0.point2, &b0.point1, &q, 0.5f);
3275 d2d_point_lerp(&b0.point3, &b0.point2, &b1.point1, 0.5f);
3277 d2d_geometry_flatten_cubic(sink, p0, &b0, tolerance);
3278 ID2D1SimplifiedGeometrySink_SetSegmentFlags(sink, D2D1_PATH_SEGMENT_FORCE_ROUND_LINE_JOIN);
3279 d2d_geometry_flatten_cubic(sink, &b0.point3, &b1, tolerance);
3280 ID2D1SimplifiedGeometrySink_SetSegmentFlags(sink, D2D1_PATH_SEGMENT_NONE);
3283 static void d2d_geometry_simplify_quadratic(ID2D1SimplifiedGeometrySink *sink,
3284 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_POINT_2F *p0,
3285 const D2D1_POINT_2F *p1, const D2D1_POINT_2F *p2, float tolerance)
3287 D2D1_BEZIER_SEGMENT b;
3289 d2d_point_lerp(&b.point1, p0, p1, 2.0f / 3.0f);
3290 d2d_point_lerp(&b.point2, p2, p1, 2.0f / 3.0f);
3291 b.point3 = *p2;
3293 if (option == D2D1_GEOMETRY_SIMPLIFICATION_OPTION_LINES)
3294 d2d_geometry_flatten_cubic(sink, p0, &b, tolerance);
3295 else
3296 ID2D1SimplifiedGeometrySink_AddBeziers(sink, &b, 1);
3299 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Simplify(ID2D1PathGeometry *iface,
3300 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3301 ID2D1SimplifiedGeometrySink *sink)
3303 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3304 enum d2d_vertex_type type = D2D_VERTEX_TYPE_NONE;
3305 unsigned int i, j, bezier_idx;
3306 D2D1_FIGURE_BEGIN begin;
3307 D2D1_POINT_2F p, p1, p2;
3308 D2D1_FIGURE_END end;
3310 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
3311 iface, option, transform, tolerance, sink);
3313 ID2D1SimplifiedGeometrySink_SetFillMode(sink, geometry->u.path.fill_mode);
3314 for (i = 0; i < geometry->u.path.figure_count; ++i)
3316 const struct d2d_figure *figure = &geometry->u.path.figures[i];
3318 for (j = 0; j < figure->vertex_count; ++j)
3320 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE)
3321 continue;
3323 p = figure->vertices[j];
3324 if (transform)
3325 d2d_point_transform(&p, transform, p.x, p.y);
3326 begin = figure->flags & D2D_FIGURE_FLAG_HOLLOW ? D2D1_FIGURE_BEGIN_HOLLOW : D2D1_FIGURE_BEGIN_FILLED;
3327 ID2D1SimplifiedGeometrySink_BeginFigure(sink, p, begin);
3328 type = figure->vertex_types[j];
3329 break;
3332 for (bezier_idx = 0, ++j; j < figure->vertex_count; ++j)
3334 if (figure->vertex_types[j] == D2D_VERTEX_TYPE_NONE
3335 || figure->vertex_types[j] == D2D_VERTEX_TYPE_SPLIT_BEZIER)
3336 continue;
3338 switch (type)
3340 case D2D_VERTEX_TYPE_LINE:
3341 p = figure->vertices[j];
3342 if (transform)
3343 d2d_point_transform(&p, transform, p.x, p.y);
3344 ID2D1SimplifiedGeometrySink_AddLines(sink, &p, 1);
3345 break;
3347 case D2D_VERTEX_TYPE_BEZIER:
3348 p1 = figure->original_bezier_controls[bezier_idx++];
3349 if (transform)
3350 d2d_point_transform(&p1, transform, p1.x, p1.y);
3351 p2 = figure->vertices[j];
3352 if (transform)
3353 d2d_point_transform(&p2, transform, p2.x, p2.y);
3354 d2d_geometry_simplify_quadratic(sink, option, &p, &p1, &p2, tolerance);
3355 p = p2;
3356 break;
3358 default:
3359 FIXME("Unhandled vertex type %#x.\n", type);
3360 p = figure->vertices[j];
3361 if (transform)
3362 d2d_point_transform(&p, transform, p.x, p.y);
3363 ID2D1SimplifiedGeometrySink_AddLines(sink, &p, 1);
3364 break;
3367 type = figure->vertex_types[j];
3370 if (type == D2D_VERTEX_TYPE_BEZIER)
3372 p1 = figure->original_bezier_controls[bezier_idx++];
3373 if (transform)
3374 d2d_point_transform(&p1, transform, p1.x, p1.y);
3375 p2 = figure->vertices[0];
3376 if (transform)
3377 d2d_point_transform(&p2, transform, p2.x, p2.y);
3378 d2d_geometry_simplify_quadratic(sink, option, &p, &p1, &p2, tolerance);
3381 end = figure->flags & D2D_FIGURE_FLAG_CLOSED ? D2D1_FIGURE_END_CLOSED : D2D1_FIGURE_END_OPEN;
3382 ID2D1SimplifiedGeometrySink_EndFigure(sink, end);
3385 return S_OK;
3388 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Tessellate(ID2D1PathGeometry *iface,
3389 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3391 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3393 return E_NOTIMPL;
3396 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_CombineWithGeometry(ID2D1PathGeometry *iface,
3397 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3398 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3400 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3401 iface, geometry, combine_mode, transform, tolerance, sink);
3403 return E_NOTIMPL;
3406 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Outline(ID2D1PathGeometry *iface,
3407 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
3409 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3411 return E_NOTIMPL;
3414 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputeArea(ID2D1PathGeometry *iface,
3415 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
3417 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
3419 return E_NOTIMPL;
3422 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputeLength(ID2D1PathGeometry *iface,
3423 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
3425 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
3427 return E_NOTIMPL;
3430 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_ComputePointAtLength(ID2D1PathGeometry *iface, float length,
3431 const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point, D2D1_POINT_2F *tangent)
3433 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
3434 iface, length, transform, tolerance, point, tangent);
3436 return E_NOTIMPL;
3439 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Widen(ID2D1PathGeometry *iface, float stroke_width,
3440 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3441 ID2D1SimplifiedGeometrySink *sink)
3443 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
3444 iface, stroke_width, stroke_style, transform, tolerance, sink);
3446 return E_NOTIMPL;
3449 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Open(ID2D1PathGeometry *iface, ID2D1GeometrySink **sink)
3451 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3453 TRACE("iface %p, sink %p.\n", iface, sink);
3455 if (geometry->u.path.state != D2D_GEOMETRY_STATE_INITIAL)
3456 return D2DERR_WRONG_STATE;
3458 *sink = &geometry->u.path.ID2D1GeometrySink_iface;
3459 ID2D1GeometrySink_AddRef(*sink);
3461 geometry->u.path.state = D2D_GEOMETRY_STATE_OPEN;
3463 return S_OK;
3466 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_Stream(ID2D1PathGeometry *iface, ID2D1GeometrySink *sink)
3468 FIXME("iface %p, sink %p stub!\n", iface, sink);
3470 return E_NOTIMPL;
3473 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetSegmentCount(ID2D1PathGeometry *iface, UINT32 *count)
3475 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3477 TRACE("iface %p, count %p.\n", iface, count);
3479 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3480 return D2DERR_WRONG_STATE;
3482 *count = geometry->u.path.segment_count;
3484 return S_OK;
3487 static HRESULT STDMETHODCALLTYPE d2d_path_geometry_GetFigureCount(ID2D1PathGeometry *iface, UINT32 *count)
3489 struct d2d_geometry *geometry = impl_from_ID2D1PathGeometry(iface);
3491 TRACE("iface %p, count %p.\n", iface, count);
3493 if (geometry->u.path.state != D2D_GEOMETRY_STATE_CLOSED)
3494 return D2DERR_WRONG_STATE;
3496 *count = geometry->u.path.figure_count;
3498 return S_OK;
3501 static const struct ID2D1PathGeometryVtbl d2d_path_geometry_vtbl =
3503 d2d_path_geometry_QueryInterface,
3504 d2d_path_geometry_AddRef,
3505 d2d_path_geometry_Release,
3506 d2d_path_geometry_GetFactory,
3507 d2d_path_geometry_GetBounds,
3508 d2d_path_geometry_GetWidenedBounds,
3509 d2d_path_geometry_StrokeContainsPoint,
3510 d2d_path_geometry_FillContainsPoint,
3511 d2d_path_geometry_CompareWithGeometry,
3512 d2d_path_geometry_Simplify,
3513 d2d_path_geometry_Tessellate,
3514 d2d_path_geometry_CombineWithGeometry,
3515 d2d_path_geometry_Outline,
3516 d2d_path_geometry_ComputeArea,
3517 d2d_path_geometry_ComputeLength,
3518 d2d_path_geometry_ComputePointAtLength,
3519 d2d_path_geometry_Widen,
3520 d2d_path_geometry_Open,
3521 d2d_path_geometry_Stream,
3522 d2d_path_geometry_GetSegmentCount,
3523 d2d_path_geometry_GetFigureCount,
3526 void d2d_path_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory)
3528 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl);
3529 geometry->u.path.ID2D1GeometrySink_iface.lpVtbl = &d2d_geometry_sink_vtbl;
3530 geometry->u.path.bounds.left = FLT_MAX;
3531 geometry->u.path.bounds.right = -FLT_MAX;
3532 geometry->u.path.bounds.top = FLT_MAX;
3533 geometry->u.path.bounds.bottom = -FLT_MAX;
3536 static inline struct d2d_geometry *impl_from_ID2D1RectangleGeometry(ID2D1RectangleGeometry *iface)
3538 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3541 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_QueryInterface(ID2D1RectangleGeometry *iface,
3542 REFIID iid, void **out)
3544 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3546 if (IsEqualGUID(iid, &IID_ID2D1RectangleGeometry)
3547 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3548 || IsEqualGUID(iid, &IID_ID2D1Resource)
3549 || IsEqualGUID(iid, &IID_IUnknown))
3551 ID2D1RectangleGeometry_AddRef(iface);
3552 *out = iface;
3553 return S_OK;
3556 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3558 *out = NULL;
3559 return E_NOINTERFACE;
3562 static ULONG STDMETHODCALLTYPE d2d_rectangle_geometry_AddRef(ID2D1RectangleGeometry *iface)
3564 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3565 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3567 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3569 return refcount;
3572 static ULONG STDMETHODCALLTYPE d2d_rectangle_geometry_Release(ID2D1RectangleGeometry *iface)
3574 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3575 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3577 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3579 if (!refcount)
3581 d2d_geometry_cleanup(geometry);
3582 HeapFree(GetProcessHeap(), 0, geometry);
3585 return refcount;
3588 static void STDMETHODCALLTYPE d2d_rectangle_geometry_GetFactory(ID2D1RectangleGeometry *iface, ID2D1Factory **factory)
3590 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3592 TRACE("iface %p, factory %p.\n", iface, factory);
3594 ID2D1Factory_AddRef(*factory = geometry->factory);
3597 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_GetBounds(ID2D1RectangleGeometry *iface,
3598 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3600 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3601 D2D1_RECT_F *rect;
3602 D2D1_POINT_2F p;
3604 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3606 rect = &geometry->u.rectangle.rect;
3607 if (!transform)
3609 *bounds = *rect;
3610 return S_OK;
3613 bounds->left = FLT_MAX;
3614 bounds->top = FLT_MAX;
3615 bounds->right = -FLT_MAX;
3616 bounds->bottom = -FLT_MAX;
3618 d2d_point_transform(&p, transform, rect->left, rect->top);
3619 d2d_rect_expand(bounds, &p);
3620 d2d_point_transform(&p, transform, rect->left, rect->bottom);
3621 d2d_rect_expand(bounds, &p);
3622 d2d_point_transform(&p, transform, rect->right, rect->bottom);
3623 d2d_rect_expand(bounds, &p);
3624 d2d_point_transform(&p, transform, rect->right, rect->top);
3625 d2d_rect_expand(bounds, &p);
3627 return S_OK;
3630 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_GetWidenedBounds(ID2D1RectangleGeometry *iface,
3631 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3632 float tolerance, D2D1_RECT_F *bounds)
3634 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3635 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3637 return E_NOTIMPL;
3640 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_StrokeContainsPoint(ID2D1RectangleGeometry *iface,
3641 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3642 float tolerance, BOOL *contains)
3644 FIXME("iface %p, point {%.8e, %.8e}, stroke_width %.8e, stroke_style %p, "
3645 "transform %p, tolerance %.8e, contains %p stub!\n",
3646 iface, point.x, point.y, stroke_width, stroke_style, transform, tolerance, contains);
3648 return E_NOTIMPL;
3651 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_FillContainsPoint(ID2D1RectangleGeometry *iface,
3652 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3654 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3655 D2D1_RECT_F *rect = &geometry->u.rectangle.rect;
3656 float dx, dy;
3658 TRACE("iface %p, point {%.8e, %.8e}, transform %p, tolerance %.8e, contains %p.\n",
3659 iface, point.x, point.y, transform, tolerance, contains);
3661 if (transform)
3663 D2D1_MATRIX_3X2_F g_i;
3665 if (!d2d_matrix_invert(&g_i, transform))
3666 return D2DERR_UNSUPPORTED_OPERATION;
3667 d2d_point_transform(&point, &g_i, point.x, point.y);
3670 if (tolerance == 0.0f)
3671 tolerance = D2D1_DEFAULT_FLATTENING_TOLERANCE;
3673 dx = max(fabsf((rect->right + rect->left) / 2.0f - point.x) - (rect->right - rect->left) / 2.0f, 0.0f);
3674 dy = max(fabsf((rect->bottom + rect->top) / 2.0f - point.y) - (rect->bottom - rect->top) / 2.0f, 0.0f);
3676 *contains = tolerance * tolerance > (dx * dx + dy * dy);
3677 return S_OK;
3680 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_CompareWithGeometry(ID2D1RectangleGeometry *iface,
3681 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
3683 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
3684 iface, geometry, transform, tolerance, relation);
3686 return E_NOTIMPL;
3689 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Simplify(ID2D1RectangleGeometry *iface,
3690 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3691 ID2D1SimplifiedGeometrySink *sink)
3693 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3694 D2D1_RECT_F *rect = &geometry->u.rectangle.rect;
3695 D2D1_POINT_2F p[4];
3696 unsigned int i;
3698 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
3699 iface, option, transform, tolerance, sink);
3701 d2d_point_set(&p[0], rect->left, rect->top);
3702 d2d_point_set(&p[1], rect->right, rect->top);
3703 d2d_point_set(&p[2], rect->right, rect->bottom);
3704 d2d_point_set(&p[3], rect->left, rect->bottom);
3706 if (transform)
3708 for (i = 0; i < ARRAY_SIZE(p); ++i)
3710 d2d_point_transform(&p[i], transform, p[i].x, p[i].y);
3714 ID2D1SimplifiedGeometrySink_SetFillMode(sink, D2D1_FILL_MODE_ALTERNATE);
3715 ID2D1SimplifiedGeometrySink_BeginFigure(sink, p[0], D2D1_FIGURE_BEGIN_FILLED);
3716 ID2D1SimplifiedGeometrySink_AddLines(sink, &p[1], 3);
3717 ID2D1SimplifiedGeometrySink_EndFigure(sink, D2D1_FIGURE_END_CLOSED);
3719 return S_OK;
3722 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Tessellate(ID2D1RectangleGeometry *iface,
3723 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
3725 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3727 return E_NOTIMPL;
3730 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_CombineWithGeometry(ID2D1RectangleGeometry *iface,
3731 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
3732 float tolerance, ID2D1SimplifiedGeometrySink *sink)
3734 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
3735 iface, geometry, combine_mode, transform, tolerance, sink);
3737 return E_NOTIMPL;
3740 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Outline(ID2D1RectangleGeometry *iface,
3741 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
3743 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
3745 return E_NOTIMPL;
3748 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputeArea(ID2D1RectangleGeometry *iface,
3749 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
3751 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
3753 return E_NOTIMPL;
3756 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputeLength(ID2D1RectangleGeometry *iface,
3757 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
3759 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
3761 return E_NOTIMPL;
3764 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_ComputePointAtLength(ID2D1RectangleGeometry *iface,
3765 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
3766 D2D1_POINT_2F *tangent)
3768 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
3769 iface, length, transform, tolerance, point, tangent);
3771 return E_NOTIMPL;
3774 static HRESULT STDMETHODCALLTYPE d2d_rectangle_geometry_Widen(ID2D1RectangleGeometry *iface, float stroke_width,
3775 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
3776 ID2D1SimplifiedGeometrySink *sink)
3778 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
3779 iface, stroke_width, stroke_style, transform, tolerance, sink);
3781 return E_NOTIMPL;
3784 static void STDMETHODCALLTYPE d2d_rectangle_geometry_GetRect(ID2D1RectangleGeometry *iface, D2D1_RECT_F *rect)
3786 struct d2d_geometry *geometry = impl_from_ID2D1RectangleGeometry(iface);
3788 TRACE("iface %p, rect %p.\n", iface, rect);
3790 *rect = geometry->u.rectangle.rect;
3793 static const struct ID2D1RectangleGeometryVtbl d2d_rectangle_geometry_vtbl =
3795 d2d_rectangle_geometry_QueryInterface,
3796 d2d_rectangle_geometry_AddRef,
3797 d2d_rectangle_geometry_Release,
3798 d2d_rectangle_geometry_GetFactory,
3799 d2d_rectangle_geometry_GetBounds,
3800 d2d_rectangle_geometry_GetWidenedBounds,
3801 d2d_rectangle_geometry_StrokeContainsPoint,
3802 d2d_rectangle_geometry_FillContainsPoint,
3803 d2d_rectangle_geometry_CompareWithGeometry,
3804 d2d_rectangle_geometry_Simplify,
3805 d2d_rectangle_geometry_Tessellate,
3806 d2d_rectangle_geometry_CombineWithGeometry,
3807 d2d_rectangle_geometry_Outline,
3808 d2d_rectangle_geometry_ComputeArea,
3809 d2d_rectangle_geometry_ComputeLength,
3810 d2d_rectangle_geometry_ComputePointAtLength,
3811 d2d_rectangle_geometry_Widen,
3812 d2d_rectangle_geometry_GetRect,
3815 HRESULT d2d_rectangle_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory, const D2D1_RECT_F *rect)
3817 struct d2d_face *f;
3818 D2D1_POINT_2F *v;
3819 float l, r, t, b;
3821 d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl);
3822 geometry->u.rectangle.rect = *rect;
3824 if (!(geometry->fill.vertices = HeapAlloc(GetProcessHeap(), 0, 4 * sizeof(*geometry->fill.vertices))))
3825 goto fail;
3826 if (!d2d_array_reserve((void **)&geometry->fill.faces,
3827 &geometry->fill.faces_size, 2, sizeof(*geometry->fill.faces)))
3828 goto fail;
3830 l = min(rect->left, rect->right);
3831 r = max(rect->left, rect->right);
3832 t = min(rect->top, rect->bottom);
3833 b = max(rect->top, rect->bottom);
3835 v = geometry->fill.vertices;
3836 d2d_point_set(&v[0], l, t);
3837 d2d_point_set(&v[1], l, b);
3838 d2d_point_set(&v[2], r, b);
3839 d2d_point_set(&v[3], r, t);
3840 geometry->fill.vertex_count = 4;
3842 f = geometry->fill.faces;
3843 d2d_face_set(&f[0], 1, 2, 0);
3844 d2d_face_set(&f[1], 0, 2, 3);
3845 geometry->fill.face_count = 2;
3847 if (!d2d_geometry_outline_add_line_segment(geometry, &v[0], &v[1]))
3848 goto fail;
3849 if (!d2d_geometry_outline_add_line_segment(geometry, &v[1], &v[2]))
3850 goto fail;
3851 if (!d2d_geometry_outline_add_line_segment(geometry, &v[2], &v[3]))
3852 goto fail;
3853 if (!d2d_geometry_outline_add_line_segment(geometry, &v[3], &v[0]))
3854 goto fail;
3856 if (!d2d_geometry_outline_add_join(geometry, &v[3], &v[0], &v[1]))
3857 goto fail;
3858 if (!d2d_geometry_outline_add_join(geometry, &v[0], &v[1], &v[2]))
3859 goto fail;
3860 if (!d2d_geometry_outline_add_join(geometry, &v[1], &v[2], &v[3]))
3861 goto fail;
3862 if (!d2d_geometry_outline_add_join(geometry, &v[2], &v[3], &v[0]))
3863 goto fail;
3865 return S_OK;
3867 fail:
3868 d2d_geometry_cleanup(geometry);
3869 return E_OUTOFMEMORY;
3872 static inline struct d2d_geometry *impl_from_ID2D1TransformedGeometry(ID2D1TransformedGeometry *iface)
3874 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
3877 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_QueryInterface(ID2D1TransformedGeometry *iface,
3878 REFIID iid, void **out)
3880 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
3882 if (IsEqualGUID(iid, &IID_ID2D1TransformedGeometry)
3883 || IsEqualGUID(iid, &IID_ID2D1Geometry)
3884 || IsEqualGUID(iid, &IID_ID2D1Resource)
3885 || IsEqualGUID(iid, &IID_IUnknown))
3887 ID2D1TransformedGeometry_AddRef(iface);
3888 *out = iface;
3889 return S_OK;
3892 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
3894 *out = NULL;
3895 return E_NOINTERFACE;
3898 static ULONG STDMETHODCALLTYPE d2d_transformed_geometry_AddRef(ID2D1TransformedGeometry *iface)
3900 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3901 ULONG refcount = InterlockedIncrement(&geometry->refcount);
3903 TRACE("%p increasing refcount to %u.\n", iface, refcount);
3905 return refcount;
3908 static ULONG STDMETHODCALLTYPE d2d_transformed_geometry_Release(ID2D1TransformedGeometry *iface)
3910 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3911 ULONG refcount = InterlockedDecrement(&geometry->refcount);
3913 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
3915 if (!refcount)
3917 geometry->outline.bezier_faces = NULL;
3918 geometry->outline.beziers = NULL;
3919 geometry->outline.faces = NULL;
3920 geometry->outline.vertices = NULL;
3921 geometry->fill.bezier_vertices = NULL;
3922 geometry->fill.faces = NULL;
3923 geometry->fill.vertices = NULL;
3924 ID2D1Geometry_Release(geometry->u.transformed.src_geometry);
3925 d2d_geometry_cleanup(geometry);
3926 HeapFree(GetProcessHeap(), 0, geometry);
3929 return refcount;
3932 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetFactory(ID2D1TransformedGeometry *iface,
3933 ID2D1Factory **factory)
3935 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3937 TRACE("iface %p, factory %p.\n", iface, factory);
3939 ID2D1Factory_AddRef(*factory = geometry->factory);
3942 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_GetBounds(ID2D1TransformedGeometry *iface,
3943 const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
3945 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3946 D2D1_MATRIX_3X2_F g;
3948 TRACE("iface %p, transform %p, bounds %p.\n", iface, transform, bounds);
3950 g = geometry->transform;
3951 if (transform)
3952 d2d_matrix_multiply(&g, transform);
3954 return ID2D1Geometry_GetBounds(geometry->u.transformed.src_geometry, &g, bounds);
3957 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_GetWidenedBounds(ID2D1TransformedGeometry *iface,
3958 float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3959 float tolerance, D2D1_RECT_F *bounds)
3961 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
3962 iface, stroke_width, stroke_style, transform, tolerance, bounds);
3964 return E_NOTIMPL;
3967 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_StrokeContainsPoint(ID2D1TransformedGeometry *iface,
3968 D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
3969 float tolerance, BOOL *contains)
3971 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3972 D2D1_MATRIX_3X2_F g;
3974 TRACE("iface %p, point {%.8e, %.8e}, stroke_width %.8e, stroke_style %p, "
3975 "transform %p, tolerance %.8e, contains %p.\n",
3976 iface, point.x, point.y, stroke_width, stroke_style, transform, tolerance, contains);
3978 g = geometry->transform;
3979 if (transform)
3980 d2d_matrix_multiply(&g, transform);
3982 return ID2D1Geometry_StrokeContainsPoint(geometry->u.transformed.src_geometry, point, stroke_width, stroke_style,
3983 &g, tolerance, contains);
3986 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_FillContainsPoint(ID2D1TransformedGeometry *iface,
3987 D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
3989 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
3990 D2D1_MATRIX_3X2_F g;
3992 TRACE("iface %p, point {%.8e, %.8e}, transform %p, tolerance %.8e, contains %p.\n",
3993 iface, point.x, point.y, transform, tolerance, contains);
3995 g = geometry->transform;
3996 if (transform)
3997 d2d_matrix_multiply(&g, transform);
3999 return ID2D1Geometry_FillContainsPoint(geometry->u.transformed.src_geometry, point, &g, tolerance, contains);
4002 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_CompareWithGeometry(ID2D1TransformedGeometry *iface,
4003 ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
4005 FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
4006 iface, geometry, transform, tolerance, relation);
4008 return E_NOTIMPL;
4011 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Simplify(ID2D1TransformedGeometry *iface,
4012 D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4013 ID2D1SimplifiedGeometrySink *sink)
4015 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4016 D2D1_MATRIX_3X2_F g;
4018 TRACE("iface %p, option %#x, transform %p, tolerance %.8e, sink %p.\n",
4019 iface, option, transform, tolerance, sink);
4021 g = geometry->transform;
4022 if (transform)
4023 d2d_matrix_multiply(&g, transform);
4025 return ID2D1Geometry_Simplify(geometry->u.transformed.src_geometry, option, &g, tolerance, sink);
4028 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Tessellate(ID2D1TransformedGeometry *iface,
4029 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
4031 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4033 return E_NOTIMPL;
4036 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_CombineWithGeometry(ID2D1TransformedGeometry *iface,
4037 ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
4038 float tolerance, ID2D1SimplifiedGeometrySink *sink)
4040 FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
4041 iface, geometry, combine_mode, transform, tolerance, sink);
4043 return E_NOTIMPL;
4046 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Outline(ID2D1TransformedGeometry *iface,
4047 const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
4049 FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
4051 return E_NOTIMPL;
4054 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputeArea(ID2D1TransformedGeometry *iface,
4055 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
4057 FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
4059 return E_NOTIMPL;
4062 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputeLength(ID2D1TransformedGeometry *iface,
4063 const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
4065 FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
4067 return E_NOTIMPL;
4070 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_ComputePointAtLength(ID2D1TransformedGeometry *iface,
4071 float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
4072 D2D1_POINT_2F *tangent)
4074 FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
4075 iface, length, transform, tolerance, point, tangent);
4077 return E_NOTIMPL;
4080 static HRESULT STDMETHODCALLTYPE d2d_transformed_geometry_Widen(ID2D1TransformedGeometry *iface, float stroke_width,
4081 ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
4082 ID2D1SimplifiedGeometrySink *sink)
4084 FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
4085 iface, stroke_width, stroke_style, transform, tolerance, sink);
4087 return E_NOTIMPL;
4090 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetSourceGeometry(ID2D1TransformedGeometry *iface,
4091 ID2D1Geometry **src_geometry)
4093 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4095 TRACE("iface %p, src_geometry %p.\n", iface, src_geometry);
4097 ID2D1Geometry_AddRef(*src_geometry = geometry->u.transformed.src_geometry);
4100 static void STDMETHODCALLTYPE d2d_transformed_geometry_GetTransform(ID2D1TransformedGeometry *iface,
4101 D2D1_MATRIX_3X2_F *transform)
4103 struct d2d_geometry *geometry = impl_from_ID2D1TransformedGeometry(iface);
4105 TRACE("iface %p, transform %p.\n", iface, transform);
4107 *transform = geometry->u.transformed.transform;
4110 static const struct ID2D1TransformedGeometryVtbl d2d_transformed_geometry_vtbl =
4112 d2d_transformed_geometry_QueryInterface,
4113 d2d_transformed_geometry_AddRef,
4114 d2d_transformed_geometry_Release,
4115 d2d_transformed_geometry_GetFactory,
4116 d2d_transformed_geometry_GetBounds,
4117 d2d_transformed_geometry_GetWidenedBounds,
4118 d2d_transformed_geometry_StrokeContainsPoint,
4119 d2d_transformed_geometry_FillContainsPoint,
4120 d2d_transformed_geometry_CompareWithGeometry,
4121 d2d_transformed_geometry_Simplify,
4122 d2d_transformed_geometry_Tessellate,
4123 d2d_transformed_geometry_CombineWithGeometry,
4124 d2d_transformed_geometry_Outline,
4125 d2d_transformed_geometry_ComputeArea,
4126 d2d_transformed_geometry_ComputeLength,
4127 d2d_transformed_geometry_ComputePointAtLength,
4128 d2d_transformed_geometry_Widen,
4129 d2d_transformed_geometry_GetSourceGeometry,
4130 d2d_transformed_geometry_GetTransform,
4133 void d2d_transformed_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
4134 ID2D1Geometry *src_geometry, const D2D_MATRIX_3X2_F *transform)
4136 struct d2d_geometry *src_impl;
4137 D2D_MATRIX_3X2_F g;
4139 src_impl = unsafe_impl_from_ID2D1Geometry(src_geometry);
4141 g = src_impl->transform;
4142 d2d_matrix_multiply(&g, transform);
4143 d2d_geometry_init(geometry, factory, &g, (ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl);
4144 ID2D1Geometry_AddRef(geometry->u.transformed.src_geometry = src_geometry);
4145 geometry->u.transformed.transform = *transform;
4146 geometry->fill = src_impl->fill;
4147 geometry->outline = src_impl->outline;
4150 struct d2d_geometry *unsafe_impl_from_ID2D1Geometry(ID2D1Geometry *iface)
4152 if (!iface)
4153 return NULL;
4154 assert(iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl
4155 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl
4156 || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl);
4157 return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);