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

📄 cairo-path-stroke.c

📁 按照官方的说法:Cairo is a vector graphics library with cross-device output support. 翻译过来
💻 C
📖 第 1 页 / 共 2 页
字号:
/* cairo - a vector graphics library with display and print output * * Copyright © 2002 University of Southern California * * This library is free software; you can redistribute it and/or * modify it either under the terms of the GNU Lesser General Public * License version 2.1 as published by the Free Software Foundation * (the "LGPL") or, at your option, under the terms of the Mozilla * Public License Version 1.1 (the "MPL"). If you do not alter this * notice, a recipient may use your version of this file under either * the MPL or the LGPL. * * You should have received a copy of the LGPL along with this library * in the file COPYING-LGPL-2.1; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * You should have received a copy of the MPL along with this library * in the file COPYING-MPL-1.1 * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY * OF ANY KIND, either express or implied. See the LGPL or the MPL for * the specific language governing rights and limitations. * * The Original Code is the cairo graphics library. * * The Initial Developer of the Original Code is University of Southern * California. * * Contributor(s): *	Carl D. Worth <cworth@cworth.org> */#include "cairoint.h"typedef struct cairo_stroker {    cairo_stroke_style_t	*style;    cairo_matrix_t *ctm;    cairo_matrix_t *ctm_inverse;    double tolerance;    cairo_traps_t *traps;    cairo_pen_t	  pen;    cairo_point_t current_point;    cairo_point_t first_point;    cairo_bool_t has_sub_path;    cairo_bool_t has_current_face;    cairo_stroke_face_t current_face;    cairo_bool_t has_first_face;    cairo_stroke_face_t first_face;    cairo_bool_t dashed;    int dash_index;    int dash_on;    double dash_remain;} cairo_stroker_t;/* private functions */static void_cairo_stroker_init (cairo_stroker_t		*stroker,		     cairo_stroke_style_t	*stroke_style,		     cairo_matrix_t		*ctm,		     cairo_matrix_t		*ctm_inverse,		     double			 tolerance,		     cairo_traps_t		*traps);static void_cairo_stroker_fini (cairo_stroker_t *stroker);static cairo_status_t_cairo_stroker_move_to (void *closure, cairo_point_t *point);static cairo_status_t_cairo_stroker_line_to (void *closure, cairo_point_t *point);static cairo_status_t_cairo_stroker_line_to_dashed (void *closure, cairo_point_t *point);static cairo_status_t_cairo_stroker_curve_to (void *closure,			 cairo_point_t *b,			 cairo_point_t *c,			 cairo_point_t *d);static cairo_status_t_cairo_stroker_curve_to_dashed (void *closure,				cairo_point_t *b,				cairo_point_t *c,				cairo_point_t *d);static cairo_status_t_cairo_stroker_close_path (void *closure);static void_translate_point (cairo_point_t *point, cairo_point_t *offset);static int_cairo_stroker_face_clockwise (cairo_stroke_face_t *in, cairo_stroke_face_t *out);static cairo_status_t_cairo_stroker_join (cairo_stroker_t *stroker, cairo_stroke_face_t *in, cairo_stroke_face_t *out);static void_cairo_stroker_start_dash (cairo_stroker_t *stroker){    double offset;    int	on = 1;    int	i = 0;    offset = stroker->style->dash_offset;    /* We stop searching for a starting point as soon as the       offset reaches zero.  Otherwise when an initial dash       segment shrinks to zero it will be skipped over. */    while (offset > 0.0 && offset >= stroker->style->dash[i]) {	offset -= stroker->style->dash[i];	on = 1-on;	if (++i == stroker->style->num_dashes)	    i = 0;    }    stroker->dashed = TRUE;    stroker->dash_index = i;    stroker->dash_on = on;    stroker->dash_remain = stroker->style->dash[i] - offset;}static void_cairo_stroker_step_dash (cairo_stroker_t *stroker, double step){    stroker->dash_remain -= step;    if (stroker->dash_remain <= 0) {	stroker->dash_index++;	if (stroker->dash_index == stroker->style->num_dashes)	    stroker->dash_index = 0;	stroker->dash_on = 1-stroker->dash_on;	stroker->dash_remain = stroker->style->dash[stroker->dash_index];    }}static void_cairo_stroker_init (cairo_stroker_t		*stroker,		     cairo_stroke_style_t	*stroke_style,		     cairo_matrix_t		*ctm,		     cairo_matrix_t		*ctm_inverse,		     double			 tolerance,		     cairo_traps_t		*traps){    stroker->style = stroke_style;    stroker->ctm = ctm;    stroker->ctm_inverse = ctm_inverse;    stroker->tolerance = tolerance;    stroker->traps = traps;    _cairo_pen_init (&stroker->pen,		     stroke_style->line_width / 2.0,		     tolerance, ctm);    stroker->has_current_face = FALSE;    stroker->has_first_face = FALSE;    stroker->has_sub_path = FALSE;    if (stroker->style->dash)	_cairo_stroker_start_dash (stroker);    else	stroker->dashed = FALSE;}static void_cairo_stroker_fini (cairo_stroker_t *stroker){    _cairo_pen_fini (&stroker->pen);}static void_translate_point (cairo_point_t *point, cairo_point_t *offset){    point->x += offset->x;    point->y += offset->y;}static int_cairo_stroker_face_clockwise (cairo_stroke_face_t *in, cairo_stroke_face_t *out){    cairo_slope_t in_slope, out_slope;    _cairo_slope_init (&in_slope, &in->point, &in->cw);    _cairo_slope_init (&out_slope, &out->point, &out->cw);    return _cairo_slope_clockwise (&in_slope, &out_slope);}static cairo_status_t_cairo_stroker_join (cairo_stroker_t *stroker, cairo_stroke_face_t *in, cairo_stroke_face_t *out){    cairo_status_t	status;    int			clockwise = _cairo_stroker_face_clockwise (out, in);    cairo_point_t	*inpt, *outpt;    if (in->cw.x == out->cw.x	&& in->cw.y == out->cw.y	&& in->ccw.x == out->ccw.x	&& in->ccw.y == out->ccw.y)    {	return CAIRO_STATUS_SUCCESS;    }    if (clockwise) {    	inpt = &in->ccw;    	outpt = &out->ccw;    } else {    	inpt = &in->cw;    	outpt = &out->cw;    }    switch (stroker->style->line_join) {    case CAIRO_LINE_JOIN_ROUND: {	int i;	int start, step, stop;	cairo_point_t tri[3];	cairo_pen_t *pen = &stroker->pen;	tri[0] = in->point;	if (clockwise) {	    _cairo_pen_find_active_ccw_vertex_index (pen, &in->dev_vector, &start);	    step = -1;	    _cairo_pen_find_active_ccw_vertex_index (pen, &out->dev_vector, &stop);	} else {	    _cairo_pen_find_active_cw_vertex_index (pen, &in->dev_vector, &start);	    step = +1;	    _cairo_pen_find_active_cw_vertex_index (pen, &out->dev_vector, &stop);	}	i = start;	tri[1] = *inpt;	while (i != stop) {	    tri[2] = in->point;	    _translate_point (&tri[2], &pen->vertices[i].point);	    _cairo_traps_tessellate_triangle (stroker->traps, tri);	    tri[1] = tri[2];	    i += step;	    if (i < 0)		i = pen->num_vertices - 1;	    if (i >= pen->num_vertices)		i = 0;	}	tri[2] = *outpt;	return _cairo_traps_tessellate_triangle (stroker->traps, tri);    }    case CAIRO_LINE_JOIN_MITER:    default: {	/* dot product of incoming slope vector with outgoing slope vector */	double	in_dot_out = ((-in->usr_vector.x * out->usr_vector.x)+			      (-in->usr_vector.y * out->usr_vector.y));	double	ml = stroker->style->miter_limit;	/*	 * Check the miter limit -- lines meeting at an acute angle	 * can generate long miters, the limit converts them to bevel	 *	 * We want to know when the miter is within the miter limit.	 * That's straightforward to specify:	 *	 *	secant (psi / 2) <= ml	 *	 * where psi is the angle between in and out	 *	 *				secant(psi/2) = 1/sin(psi/2)	 *	1/sin(psi/2) <= ml	 *	1 <= ml sin(psi/2)	 *	1 <= ml² sin²(psi/2)	 *	2 <= ml² 2 sin²(psi/2)	 *				2·sin²(psi/2) = 1-cos(psi)	 *	2 <= ml² (1-cos(psi))	 *	 *				in · out = |in| |out| cos (psi)	 *	 * in and out are both unit vectors, so:	 *	 *				in · out = cos (psi)	 *	 *	2 <= ml² (1 - in · out)	 *	 */	if (2 <= ml * ml * (1 - in_dot_out)) {	    double		x1, y1, x2, y2;	    double		mx, my;	    double		dx1, dx2, dy1, dy2;	    cairo_polygon_t	polygon;	    cairo_point_t	outer;	    /*	     * we've got the points already transformed to device	     * space, but need to do some computation with them and	     * also need to transform the slope from user space to	     * device space	     */	    /* outer point of incoming line face */	    x1 = _cairo_fixed_to_double (inpt->x);	    y1 = _cairo_fixed_to_double (inpt->y);	    dx1 = in->usr_vector.x;	    dy1 = in->usr_vector.y;	    cairo_matrix_transform_distance (stroker->ctm, &dx1, &dy1);	    /* outer point of outgoing line face */	    x2 = _cairo_fixed_to_double (outpt->x);	    y2 = _cairo_fixed_to_double (outpt->y);	    dx2 = out->usr_vector.x;	    dy2 = out->usr_vector.y;	    cairo_matrix_transform_distance (stroker->ctm, &dx2, &dy2);	    /*	     * Compute the location of the outer corner of the miter.	     * That's pretty easy -- just the intersection of the two	     * outer edges.  We've got slopes and points on each	     * of those edges.  Compute my directly, then compute	     * mx by using the edge with the larger dy; that avoids	     * dividing by values close to zero.	     */	    my = (((x2 - x1) * dy1 * dy2 - y2 * dx2 * dy1 + y1 * dx1 * dy2) /		  (dx1 * dy2 - dx2 * dy1));	    if (fabs (dy1) >= fabs (dy2))		mx = (my - y1) * dx1 / dy1 + x1;	    else		mx = (my - y2) * dx2 / dy2 + x2;	    /*	     * Draw the quadrilateral	     */	    outer.x = _cairo_fixed_from_double (mx);	    outer.y = _cairo_fixed_from_double (my);	    _cairo_polygon_init (&polygon);	    _cairo_polygon_move_to (&polygon, &in->point);	    _cairo_polygon_line_to (&polygon, inpt);	    _cairo_polygon_line_to (&polygon, &outer);	    _cairo_polygon_line_to (&polygon, outpt);	    _cairo_polygon_close (&polygon);	    status = _cairo_traps_tessellate_polygon (stroker->traps,						      &polygon,						      CAIRO_FILL_RULE_WINDING);	    _cairo_polygon_fini (&polygon);	    return status;	}	/* fall through ... */    }    case CAIRO_LINE_JOIN_BEVEL: {	cairo_point_t tri[3];	tri[0] = in->point;	tri[1] = *inpt;	tri[2] = *outpt;	return _cairo_traps_tessellate_triangle (stroker->traps, tri);    }    }}static cairo_status_t_cairo_stroker_add_cap (cairo_stroker_t *stroker, cairo_stroke_face_t *f){    cairo_status_t	    status;    if (stroker->style->line_cap == CAIRO_LINE_CAP_BUTT)	return CAIRO_STATUS_SUCCESS;    switch (stroker->style->line_cap) {    case CAIRO_LINE_CAP_ROUND: {	int i;	int start, stop;	cairo_slope_t slope;	cairo_point_t tri[3];	cairo_pen_t *pen = &stroker->pen;	slope = f->dev_vector;	_cairo_pen_find_active_cw_vertex_index (pen, &slope, &start);	slope.dx = -slope.dx;	slope.dy = -slope.dy;	_cairo_pen_find_active_cw_vertex_index (pen, &slope, &stop);	tri[0] = f->point;	tri[1] = f->cw;	for (i=start; i != stop; i = (i+1) % pen->num_vertices) {	    tri[2] = f->point;	    _translate_point (&tri[2], &pen->vertices[i].point);	    _cairo_traps_tessellate_triangle (stroker->traps, tri);	    tri[1] = tri[2];	}	tri[2] = f->ccw;	return _cairo_traps_tessellate_triangle (stroker->traps, tri);    }    case CAIRO_LINE_CAP_SQUARE: {	double dx, dy;	cairo_slope_t	fvector;	cairo_point_t	occw, ocw;	cairo_polygon_t	polygon;	dx = f->usr_vector.x;	dy = f->usr_vector.y;	dx *= stroker->style->line_width / 2.0;	dy *= stroker->style->line_width / 2.0;	cairo_matrix_transform_distance (stroker->ctm, &dx, &dy);	fvector.dx = _cairo_fixed_from_double (dx);	fvector.dy = _cairo_fixed_from_double (dy);	occw.x = f->ccw.x + fvector.dx;	occw.y = f->ccw.y + fvector.dy;	ocw.x = f->cw.x + fvector.dx;	ocw.y = f->cw.y + fvector.dy;	_cairo_polygon_init (&polygon);	_cairo_polygon_move_to (&polygon, &f->cw);	_cairo_polygon_line_to (&polygon, &ocw);	_cairo_polygon_line_to (&polygon, &occw);	_cairo_polygon_line_to (&polygon, &f->ccw);	_cairo_polygon_close (&polygon);	status = _cairo_traps_tessellate_polygon (stroker->traps, &polygon, CAIRO_FILL_RULE_WINDING);	_cairo_polygon_fini (&polygon);	return status;    }    case CAIRO_LINE_CAP_BUTT:    default:	return CAIRO_STATUS_SUCCESS;    }}static cairo_status_t_cairo_stroker_add_leading_cap (cairo_stroker_t     *stroker,				cairo_stroke_face_t *face){    cairo_stroke_face_t reversed;    cairo_point_t t;    reversed = *face;    /* The initial cap needs an outward facing vector. Reverse everything */    reversed.usr_vector.x = -reversed.usr_vector.x;    reversed.usr_vector.y = -reversed.usr_vector.y;    reversed.dev_vector.dx = -reversed.dev_vector.dx;    reversed.dev_vector.dy = -reversed.dev_vector.dy;    t = reversed.cw;    reversed.cw = reversed.ccw;    reversed.ccw = t;    return _cairo_stroker_add_cap (stroker, &reversed);}static cairo_status_t_cairo_stroker_add_trailing_cap (cairo_stroker_t     *stroker,				 cairo_stroke_face_t *face){    return _cairo_stroker_add_cap (stroker, face);}static void_compute_face (cairo_point_t *point, cairo_slope_t *slope, cairo_stroker_t *stroker, cairo_stroke_face_t *face);static cairo_status_t_cairo_stroker_add_caps (cairo_stroker_t *stroker){    cairo_status_t status;    /* check for a degenerative sub_path */    if (stroker->has_sub_path	&& !stroker->has_first_face	&& !stroker->has_current_face	&& stroker->style->line_cap == CAIRO_LINE_JOIN_ROUND)    {	/* pick an arbitrary slope to use */	cairo_slope_t slope = {1, 0};	_compute_face (&stroker->first_point, &slope, stroker, &stroker->first_face);	stroker->has_first_face = stroker->has_current_face = TRUE;	stroker->current_face = stroker->first_face;    }    if (stroker->has_first_face) {	status = _cairo_stroker_add_leading_cap (stroker, &stroker->first_face);	if (status)	    return status;    }    if (stroker->has_current_face) {	status = _cairo_stroker_add_trailing_cap (stroker, &stroker->current_face);	if (status)	    return status;    }    return CAIRO_STATUS_SUCCESS;}static void_compute_face (cairo_point_t *point, cairo_slope_t *slope, cairo_stroker_t *stroker, cairo_stroke_face_t *face){    double mag, det;

⌨️ 快捷键说明

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