static void _add_edge (struct reduce *r, const cairo_point_t *p1, const cairo_point_t *p2) { int top, bottom; int top_y, bot_y; int n; if (p1->y < p2->y) { top = p1->y; bottom = p2->y; } else { top = p2->y; bottom = p1->y; } if (bottom < r->limit.p1.y || top > r->limit.p2.y) return; if (p1->x > p2->x) { const cairo_point_t *t = p1; p1 = p2; p2 = t; } if (p2->x <= r->limit.p1.x || p1->x >= r->limit.p2.x) return; for (n = 0; n < r->clip->num_boxes; n++) { const cairo_box_t *limits = &r->clip->boxes[n]; if (bottom < limits->p1.y || top > limits->p2.y) continue; if (p2->x <= limits->p1.x || p1->x >= limits->p2.x) continue; if (p1->x >= limits->p1.x && p2->x <= limits->p1.x) { top_y = top; bot_y = bottom; } else { int p1_y, p2_y; p1_y = _cairo_edge_compute_intersection_y_for_x (p1, p2, limits->p1.x); p2_y = _cairo_edge_compute_intersection_y_for_x (p1, p2, limits->p2.x); if (p1_y < p2_y) { top_y = p1_y; bot_y = p2_y; } else { top_y = p2_y; bot_y = p1_y; } if (top_y < top) top_y = top; if (bot_y > bottom) bot_y = bottom; } if (top_y < limits->p1.y) top_y = limits->p1.y; if (bot_y > limits->p2.y) bot_y = limits->p2.y; if (bot_y > top_y) _add_clipped_edge (r, p1, p2, top_y, bot_y); } }
static void _add_clipped_edge (cairo_polygon_t *polygon, const cairo_point_t *p1, const cairo_point_t *p2, const int top, const int bottom, const int dir) { cairo_point_t bot_left, top_right; cairo_fixed_t top_y, bot_y; int n; for (n = 0; n < polygon->num_limits; n++) { const cairo_box_t *limits = &polygon->limits[n]; cairo_fixed_t pleft, pright; if (top >= limits->p2.y) continue; if (bottom <= limits->p1.y) continue; bot_left.x = limits->p1.x; bot_left.y = limits->p2.y; top_right.x = limits->p2.x; top_right.y = limits->p1.y; /* The useful region */ top_y = MAX (top, limits->p1.y); bot_y = MIN (bottom, limits->p2.y); /* The projection of the edge on the horizontal axis */ pleft = MIN (p1->x, p2->x); pright = MAX (p1->x, p2->x); if (limits->p1.x <= pleft && pright <= limits->p2.x) { /* Projection of the edge completely contained in the box: * clip vertically by restricting top and bottom */ _add_edge (polygon, p1, p2, top_y, bot_y, dir); assert_last_edge_is_valid (polygon, limits); } else if (pright <= limits->p1.x) { /* Projection of the edge to the left of the box: * replace with the left side of the box (clipped top/bottom) */ _add_edge (polygon, &limits->p1, &bot_left, top_y, bot_y, dir); assert_last_edge_is_valid (polygon, limits); } else if (limits->p2.x <= pleft) { /* Projection of the edge to the right of the box: * replace with the right side of the box (clipped top/bottom) */ _add_edge (polygon, &top_right, &limits->p2, top_y, bot_y, dir); assert_last_edge_is_valid (polygon, limits); } else { /* The edge and the box intersect in a generic way */ cairo_fixed_t left_y, right_y; cairo_bool_t top_left_to_bottom_right; /* * The edge intersects the lines corresponding to the left * and right sides of the limit box at left_y and right_y, * but we need to add edges for the range from top_y to * bot_y. * * For both intersections, there are three cases: * * 1) It is outside the vertical range of the limit * box. In this case we can simply further clip the * edge we will be emitting (i.e. restrict its * top/bottom limits to those of the limit box). * * 2) It is inside the vertical range of the limit * box. In this case, we need to add the vertical edge * connecting the correct vertex to the intersection, * in order to preserve the winding count. * * 3) It is exactly on the box. In this case, do nothing. * * These operations restrict the active range (stored in * top_y/bot_y) so that the p1-p2 edge is completely * inside the box if it is clipped to this vertical range. */ top_left_to_bottom_right = (p1->x <= p2->x) == (p1->y <= p2->y); if (top_left_to_bottom_right) { if (pleft >= limits->p1.x) { left_y = top_y; } else { left_y = _cairo_edge_compute_intersection_y_for_x (p1, p2, limits->p1.x); if (_cairo_edge_compute_intersection_x_for_y (p1, p2, left_y) < limits->p1.x) left_y++; } left_y = MIN (left_y, bot_y); if (top_y < left_y) { _add_edge (polygon, &limits->p1, &bot_left, top_y, left_y, dir); assert_last_edge_is_valid (polygon, limits); top_y = left_y; } if (pright <= limits->p2.x) { right_y = bot_y; } else { right_y = _cairo_edge_compute_intersection_y_for_x (p1, p2, limits->p2.x); if (_cairo_edge_compute_intersection_x_for_y (p1, p2, right_y) > limits->p2.x) right_y--; } right_y = MAX (right_y, top_y); if (bot_y > right_y) { _add_edge (polygon, &top_right, &limits->p2, right_y, bot_y, dir); assert_last_edge_is_valid (polygon, limits); bot_y = right_y; } } else { if (pright <= limits->p2.x) { right_y = top_y; } else { right_y = _cairo_edge_compute_intersection_y_for_x (p1, p2, limits->p2.x); if (_cairo_edge_compute_intersection_x_for_y (p1, p2, right_y) > limits->p2.x) right_y++; } right_y = MIN (right_y, bot_y); if (top_y < right_y) { _add_edge (polygon, &top_right, &limits->p2, top_y, right_y, dir); assert_last_edge_is_valid (polygon, limits); top_y = right_y; } if (pleft >= limits->p1.x) { left_y = bot_y; } else { left_y = _cairo_edge_compute_intersection_y_for_x (p1, p2, limits->p1.x); if (_cairo_edge_compute_intersection_x_for_y (p1, p2, left_y) < limits->p1.x) left_y--; } left_y = MAX (left_y, top_y); if (bot_y > left_y) { _add_edge (polygon, &limits->p1, &bot_left, left_y, bot_y, dir); assert_last_edge_is_valid (polygon, limits); bot_y = left_y; } } if (top_y != bot_y) { _add_edge (polygon, p1, p2, top_y, bot_y, dir); assert_last_edge_is_valid (polygon, limits); } } } }
static void _add_clipped_edge (cairo_polygon_t *polygon, const cairo_point_t *p1, const cairo_point_t *p2, const int top, const int bottom, const int dir) { cairo_point_t p[2]; int top_y, bot_y; int n; for (n = 0; n < polygon->num_limits; n++) { const cairo_box_t *limits = &polygon->limits[n]; if (top >= limits->p2.y) continue; if (bottom <= limits->p1.y) continue; if (p1->x >= limits->p1.x && p2->x >= limits->p1.x && p1->x <= limits->p2.x && p2->x <= limits->p2.x) { top_y = top; if (top_y < limits->p1.y) top_y = limits->p1.y; bot_y = bottom; if (bot_y > limits->p2.y) bot_y = limits->p2.y; _add_edge (polygon, p1, p2, top_y, bot_y, dir); } else if (p1->x <= limits->p1.x && p2->x <= limits->p1.x) { p[0].x = limits->p1.x; p[0].y = limits->p1.y; top_y = top; if (top_y < p[0].y) top_y = p[0].y; p[1].x = limits->p1.x; p[1].y = limits->p2.y; bot_y = bottom; if (bot_y > p[1].y) bot_y = p[1].y; _add_edge (polygon, &p[0], &p[1], top_y, bot_y, dir); } else if (p1->x >= limits->p2.x && p2->x >= limits->p2.x) { p[0].x = limits->p2.x; p[0].y = limits->p1.y; top_y = top; if (top_y < p[0].y) top_y = p[0].y; p[1].x = limits->p2.x; p[1].y = limits->p2.y; bot_y = bottom; if (bot_y > p[1].y) bot_y = p[1].y; _add_edge (polygon, &p[0], &p[1], top_y, bot_y, dir); } else { int left_y, right_y; int p1_y, p2_y; left_y = _cairo_edge_compute_intersection_y_for_x (p1, p2, limits->p1.x); right_y = _cairo_edge_compute_intersection_y_for_x (p1, p2, limits->p2.x); if (left_y == right_y) /* horizontal within bounds */ continue; p1_y = top; p2_y = bottom; if (left_y < right_y) { if (p1->x < limits->p1.x && left_y > limits->p1.y) { p[0].x = limits->p1.x; p[0].y = limits->p1.y; top_y = p1_y; if (top_y < p[0].y) top_y = p[0].y; p[1].x = limits->p1.x; p[1].y = limits->p2.y; bot_y = left_y; if (bot_y > p[1].y) bot_y = p[1].y; if (bot_y > top_y) _add_edge (polygon, &p[0], &p[1], top_y, bot_y, dir); p1_y = bot_y; } if (p2->x > limits->p2.x && right_y < limits->p2.y) { p[0].x = limits->p2.x; p[0].y = limits->p1.y; top_y = right_y; if (top_y < p[0].y) top_y = p[0].y; p[1].x = limits->p2.x; p[1].y = limits->p2.y; bot_y = p2_y; if (bot_y > p[1].y) bot_y = p[1].y; if (bot_y > top_y) _add_edge (polygon, &p[0], &p[1], top_y, bot_y, dir); p2_y = top_y; } } else { if (p1->x > limits->p2.x && right_y > limits->p1.y) { p[0].x = limits->p2.x; p[0].y = limits->p1.y; top_y = p1_y; if (top_y < p[0].y) top_y = p[0].y; p[1].x = limits->p2.x; p[1].y = limits->p2.y; bot_y = right_y; if (bot_y > p[1].y) bot_y = p[1].y; if (bot_y > top_y) _add_edge (polygon, &p[0], &p[1], top_y, bot_y, dir); p1_y = bot_y; } if (p2->x < limits->p1.x && left_y < limits->p2.y) { p[0].x = limits->p1.x; p[0].y = limits->p1.y; top_y = left_y; if (top_y < p[0].y) top_y = p[0].y; p[1].x = limits->p1.x; p[1].y = limits->p2.y; bot_y = p2_y; if (bot_y > p[1].y) bot_y = p[1].y; if (bot_y > top_y) _add_edge (polygon, &p[0], &p[1], top_y, bot_y, dir); p2_y = top_y; } } if (p1_y < limits->p1.y) p1_y = limits->p1.y; if (p2_y > limits->p2.y) p2_y = limits->p2.y; if (p2_y > p1_y) _add_edge (polygon, p1, p2, p1_y, p2_y, dir); } } }