⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cairo-path-stroke.c

📁 按照官方的说法:Cairo is a vector graphics library with cross-device output support. 翻译过来
💻 C
📖 第 1 页 / 共 2 页
字号:
    double line_dx, line_dy;    double face_dx, face_dy;    cairo_point_double_t usr_vector;    cairo_point_t offset_ccw, offset_cw;    line_dx = _cairo_fixed_to_double (slope->dx);    line_dy = _cairo_fixed_to_double (slope->dy);    /* faces are normal in user space, not device space */    cairo_matrix_transform_distance (stroker->ctm_inverse, &line_dx, &line_dy);    mag = sqrt (line_dx * line_dx + line_dy * line_dy);    if (mag == 0) {	/* XXX: Can't compute other face points. Do we want a tag in the face for this case? */	return;    }    /* normalize to unit length */    line_dx /= mag;    line_dy /= mag;    usr_vector.x = line_dx;    usr_vector.y = line_dy;    /*     * rotate to get a line_width/2 vector along the face, note that     * the vector must be rotated the right direction in device space,     * but by 90° in user space. So, the rotation depends on     * whether the ctm reflects or not, and that can be determined     * by looking at the determinant of the matrix.     */    _cairo_matrix_compute_determinant (stroker->ctm, &det);    if (det >= 0)    {	face_dx = - line_dy * (stroker->style->line_width / 2.0);	face_dy = line_dx * (stroker->style->line_width / 2.0);    }    else    {	face_dx = line_dy * (stroker->style->line_width / 2.0);	face_dy = - line_dx * (stroker->style->line_width / 2.0);    }    /* back to device space */    cairo_matrix_transform_distance (stroker->ctm, &face_dx, &face_dy);    offset_ccw.x = _cairo_fixed_from_double (face_dx);    offset_ccw.y = _cairo_fixed_from_double (face_dy);    offset_cw.x = -offset_ccw.x;    offset_cw.y = -offset_ccw.y;    face->ccw = *point;    _translate_point (&face->ccw, &offset_ccw);    face->point = *point;    face->cw = *point;    _translate_point (&face->cw, &offset_cw);    face->usr_vector.x = usr_vector.x;    face->usr_vector.y = usr_vector.y;    face->dev_vector = *slope;}static cairo_status_t_cairo_stroker_add_sub_edge (cairo_stroker_t *stroker, cairo_point_t *p1, cairo_point_t *p2,			     cairo_slope_t *slope, cairo_stroke_face_t *start,			     cairo_stroke_face_t *end){    cairo_status_t status;    cairo_polygon_t polygon;    _compute_face (p1, slope, stroker, start);    /* XXX: This could be optimized slightly by not calling       _compute_face again but rather  translating the relevant       fields from start. */    _compute_face (p2, slope, stroker, end);    if (p1->x == p2->x && p1->y == p2->y)	return CAIRO_STATUS_SUCCESS;    /* XXX: I should really check the return value of the       move_to/line_to functions here to catch out of memory       conditions. But since that would be ugly, I'd prefer to add a       status flag to the polygon object that I could check only once       at then end of this sequence, (like we do with cairo_t       already). */    _cairo_polygon_init (&polygon);    _cairo_polygon_move_to (&polygon, &start->cw);    _cairo_polygon_line_to (&polygon, &start->ccw);    _cairo_polygon_line_to (&polygon, &end->ccw);    _cairo_polygon_line_to (&polygon, &end->cw);    _cairo_polygon_close (&polygon);    /* XXX: We can't use tessellate_rectangle as the matrix may have       skewed this into a non-rectangular shape. Perhaps it would be       worth checking the matrix for skew so that the common case       could use the faster tessellate_rectangle rather than       tessellate_polygon? */    status = _cairo_traps_tessellate_polygon (stroker->traps,					      &polygon, CAIRO_FILL_RULE_WINDING);    _cairo_polygon_fini (&polygon);    return status;}static cairo_status_t_cairo_stroker_move_to (void *closure, cairo_point_t *point){    cairo_status_t status;    cairo_stroker_t *stroker = closure;    status = _cairo_stroker_add_caps (stroker);    if (status)	return status;    stroker->first_point = *point;    stroker->current_point = *point;    stroker->has_first_face = FALSE;    stroker->has_current_face = FALSE;    stroker->has_sub_path = FALSE;    return CAIRO_STATUS_SUCCESS;}static cairo_status_t_cairo_stroker_move_to_dashed (void *closure, cairo_point_t *point){    /* reset the dash pattern for new sub paths */    cairo_stroker_t *stroker = closure;    _cairo_stroker_start_dash (stroker);    return _cairo_stroker_move_to (closure, point);}static cairo_status_t_cairo_stroker_line_to (void *closure, cairo_point_t *point){    cairo_status_t status;    cairo_stroker_t *stroker = closure;    cairo_stroke_face_t start, end;    cairo_point_t *p1 = &stroker->current_point;    cairo_point_t *p2 = point;    cairo_slope_t slope;    stroker->has_sub_path = TRUE;    if (p1->x == p2->x && p1->y == p2->y)	return CAIRO_STATUS_SUCCESS;    _cairo_slope_init (&slope, p1, p2);    status = _cairo_stroker_add_sub_edge (stroker, p1, p2, &slope, &start, &end);    if (status)	return status;    if (stroker->has_current_face) {	status = _cairo_stroker_join (stroker, &stroker->current_face, &start);	if (status)	    return status;    } else {	if (!stroker->has_first_face) {	    stroker->first_face = start;	    stroker->has_first_face = TRUE;	}    }    stroker->current_face = end;    stroker->has_current_face = TRUE;    stroker->current_point = *point;    return CAIRO_STATUS_SUCCESS;}/* * Dashed lines.  Cap each dash end, join around turns when on */static cairo_status_t_cairo_stroker_line_to_dashed (void *closure, cairo_point_t *point){    cairo_status_t status = CAIRO_STATUS_SUCCESS;    cairo_stroker_t *stroker = closure;    double mag, remain, tmp;    double dx, dy;    double dx2, dy2;    cairo_point_t fd1, fd2;    cairo_bool_t first = TRUE;    cairo_stroke_face_t sub_start, sub_end;    cairo_point_t *p1 = &stroker->current_point;    cairo_point_t *p2 = point;    cairo_slope_t slope;    if (p1->x == p2->x && p1->y == p2->y)	return CAIRO_STATUS_SUCCESS;    _cairo_slope_init (&slope, p1, p2);    dx = _cairo_fixed_to_double (p2->x - p1->x);    dy = _cairo_fixed_to_double (p2->y - p1->y);    cairo_matrix_transform_distance (stroker->ctm_inverse, &dx, &dy);    mag = sqrt (dx *dx + dy * dy);    remain = mag;    fd1 = *p1;    while (remain) {	tmp = stroker->dash_remain;	if (tmp > remain)	    tmp = remain;	remain -= tmp;        dx2 = dx * (mag - remain)/mag;	dy2 = dy * (mag - remain)/mag;	cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);	fd2.x = _cairo_fixed_from_double (dx2);	fd2.y = _cairo_fixed_from_double (dy2);	fd2.x += p1->x;	fd2.y += p1->y;	/*	 * XXX simplify this case analysis	 */	if (stroker->dash_on) {	    status = _cairo_stroker_add_sub_edge (stroker, &fd1, &fd2, &slope, &sub_start, &sub_end);	    if (status)		return status;	    if (!first) {		/*		 * Not first dash in this segment, cap start		 */		status = _cairo_stroker_add_leading_cap (stroker, &sub_start);		if (status)		    return status;	    } else {		/*		 * First in this segment, join to any current_face, else		 * if at start of sub-path, mark position, else		 * cap		 */		if (stroker->has_current_face) {		    status = _cairo_stroker_join (stroker, &stroker->current_face, &sub_start);		    if (status)			return status;		} else {		    if (!stroker->has_first_face) {			stroker->first_face = sub_start;			stroker->has_first_face = TRUE;		    } else {			status = _cairo_stroker_add_leading_cap (stroker, &sub_start);			if (status)			    return status;		    }		}	    }	    if (remain) {		/*		 * Cap if not at end of segment		 */		status = _cairo_stroker_add_trailing_cap (stroker, &sub_end);		if (status)		    return status;	    } else {		/*		 * Mark previous line face and fix up next time		 * through		 */		stroker->current_face = sub_end;		stroker->has_current_face = TRUE;	    }	} else {	    /*	     * If starting with off dash, check previous face	     * and cap if necessary	     */	    if (first) {		if (stroker->has_current_face) {		    status = _cairo_stroker_add_trailing_cap (stroker, &stroker->current_face);		    if (status)			return status;		}	    }	    if (!remain)		stroker->has_current_face = FALSE;	}	_cairo_stroker_step_dash (stroker, tmp);	fd1 = fd2;	first = FALSE;    }    stroker->current_point = *point;    return status;}static cairo_status_t_cairo_stroker_curve_to (void *closure,			 cairo_point_t *b,			 cairo_point_t *c,			 cairo_point_t *d){    cairo_status_t status = CAIRO_STATUS_SUCCESS;    cairo_stroker_t *stroker = closure;    cairo_spline_t spline;    cairo_pen_t pen;    cairo_stroke_face_t start, end;    cairo_point_t extra_points[4];    cairo_point_t *a = &stroker->current_point;    status = _cairo_spline_init (&spline, a, b, c, d);    if (status == CAIRO_INT_STATUS_DEGENERATE)	return CAIRO_STATUS_SUCCESS;    status = _cairo_pen_init_copy (&pen, &stroker->pen);    if (status)	goto CLEANUP_SPLINE;    _compute_face (a, &spline.initial_slope, stroker, &start);    _compute_face (d, &spline.final_slope, stroker, &end);    if (stroker->has_current_face) {	status = _cairo_stroker_join (stroker, &stroker->current_face, &start);	if (status)	    return status;    } else {	if (!stroker->has_first_face) {	    stroker->first_face = start;	    stroker->has_first_face = TRUE;	}    }    stroker->current_face = end;    stroker->has_current_face = TRUE;    extra_points[0] = start.cw;    extra_points[0].x -= start.point.x;    extra_points[0].y -= start.point.y;    extra_points[1] = start.ccw;    extra_points[1].x -= start.point.x;    extra_points[1].y -= start.point.y;    extra_points[2] = end.cw;    extra_points[2].x -= end.point.x;    extra_points[2].y -= end.point.y;    extra_points[3] = end.ccw;    extra_points[3].x -= end.point.x;    extra_points[3].y -= end.point.y;    status = _cairo_pen_add_points (&pen, extra_points, 4);    if (status)	goto CLEANUP_PEN;    status = _cairo_pen_stroke_spline (&pen, &spline, stroker->tolerance, stroker->traps);    if (status)	goto CLEANUP_PEN;  CLEANUP_PEN:    _cairo_pen_fini (&pen);  CLEANUP_SPLINE:    _cairo_spline_fini (&spline);    stroker->current_point = *d;    return status;}/* We're using two different algorithms here for dashed and un-dashed * splines. The dashed alogorithm uses the existing line dashing * code. It's linear in path length, but gets subtly wrong results for * self-intersecting paths (an outstanding but for self-intersecting * non-curved paths as well). The non-dashed algorithm tessellates a * single polygon for the whole curve. It handles the * self-intersecting problem, but it's (unsurprisingly) not O(n) and * more significantly, it doesn't yet handle dashes. * * The only reason we're doing split algortihms here is to * minimize the impact of fixing the splines-aren't-dashed bug for * 1.0.2. Long-term the right answer is to rewrite the whole pile * of stroking code so that the entire result is computed as a * single polygon that is tessellated, (that is, stroking can be * built on top of filling). That will solve the self-intersecting * problem. It will also increase the importance of implementing * an efficient and more robust tessellator. */static cairo_status_t_cairo_stroker_curve_to_dashed (void *closure,				cairo_point_t *b,				cairo_point_t *c,				cairo_point_t *d){    cairo_status_t status = CAIRO_STATUS_SUCCESS;    cairo_stroker_t *stroker = closure;    cairo_spline_t spline;    cairo_point_t *a = &stroker->current_point;    cairo_line_join_t line_join_save;    int i;    status = _cairo_spline_init (&spline, a, b, c, d);    if (status == CAIRO_INT_STATUS_DEGENERATE)	return CAIRO_STATUS_SUCCESS;    /* If the line width is so small that the pen is reduced to a       single point, then we have nothing to do. */    if (stroker->pen.num_vertices <= 1)	goto CLEANUP_SPLINE;    /* Temporarily modify the stroker to use round joins to guarantee     * smooth stroked curves. */    line_join_save = stroker->style->line_join;    stroker->style->line_join = CAIRO_LINE_JOIN_ROUND;    status = _cairo_spline_decompose (&spline, stroker->tolerance);    if (status)	goto CLEANUP_GSTATE;    for (i = 1; i < spline.num_points; i++) {	if (stroker->dashed)	    status = _cairo_stroker_line_to_dashed (stroker, &spline.points[i]);	else	    status = _cairo_stroker_line_to (stroker, &spline.points[i]);	if (status)	    break;    }  CLEANUP_GSTATE:    stroker->style->line_join = line_join_save;  CLEANUP_SPLINE:    _cairo_spline_fini (&spline);    return status;}static cairo_status_t_cairo_stroker_close_path (void *closure){    cairo_status_t status;    cairo_stroker_t *stroker = closure;    if (stroker->dashed)	status = _cairo_stroker_line_to_dashed (stroker, &stroker->first_point);    else	status = _cairo_stroker_line_to (stroker, &stroker->first_point);    if (status)	return status;    if (stroker->has_first_face && stroker->has_current_face) {	status = _cairo_stroker_join (stroker, &stroker->current_face, &stroker->first_face);	if (status)	    return status;    } else {	status = _cairo_stroker_add_caps (stroker);	if (status)	    return status;    }    stroker->has_sub_path = FALSE;    stroker->has_first_face = FALSE;    stroker->has_current_face = FALSE;    return CAIRO_STATUS_SUCCESS;}cairo_status_t_cairo_path_fixed_stroke_to_traps (cairo_path_fixed_t	*path,				   cairo_stroke_style_t	*stroke_style,				   cairo_matrix_t	*ctm,				   cairo_matrix_t	*ctm_inverse,				   double		 tolerance,				   cairo_traps_t	*traps){    cairo_status_t status = CAIRO_STATUS_SUCCESS;    cairo_stroker_t stroker;    _cairo_stroker_init (&stroker, stroke_style,			 ctm, ctm_inverse, tolerance,			 traps);    if (stroker.style->dash)	status = _cairo_path_fixed_interpret (path,					      CAIRO_DIRECTION_FORWARD,					      _cairo_stroker_move_to_dashed,					      _cairo_stroker_line_to_dashed,					      _cairo_stroker_curve_to_dashed,					      _cairo_stroker_close_path,					      &stroker);    else	status = _cairo_path_fixed_interpret (path,					      CAIRO_DIRECTION_FORWARD,					      _cairo_stroker_move_to,					      _cairo_stroker_line_to,					      _cairo_stroker_curve_to,					      _cairo_stroker_close_path,					      &stroker);    if (status)	goto BAIL;    status = _cairo_stroker_add_caps (&stroker);BAIL:    _cairo_stroker_fini (&stroker);    return status;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -