/* * Curve.c -- Implementation of curve item. * * Authors : Patrick Lecoanet. * Creation date : Fri Mar 25 15:32:17 1994 * * $Id$ */ /* * Copyright (c) 1993 - 1999 CENA, Patrick Lecoanet -- * * This code is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this code; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ #include "Draw.h" #include "Item.h" #include "Geo.h" #include "Types.h" #include "WidgetInfo.h" #include "Image.h" #include "Color.h" #include "tkZinc.h" #include #include static const char rcsid[] = "$Id$"; static const char compile_id[]="$Compile: " __FILE__ " " __DATE__ " " __TIME__ " $"; /* * Bit offset of flags. */ #define FILLED_BIT 1<<0 /* If the item is filled with color/pattern */ #define MARKED_BIT 1<<1 /* If the vertices are marked by a symbol */ #define CLOSED_BIT 1<<2 /* If the outline should be closed automatically */ #define SMOOTH_RELIEF_BIT 1<<3 /* If the relief should be continuous (arc) or discrete (angle) */ #define FIRST_END_OK 1<<6 #define LAST_END_OK 1<<7 #define FILLED_OK 1<<8 #define RELIEF_OK 1<<10 #define MARKER_OK 1<<12 /* ********************************************************************************** * * Specific Curve item record * ********************************************************************************** */ typedef struct _CurveItemStruct { ItemStruct header; /* Public data */ ZnPoly shape; unsigned short flags; ZnImage marker; ZnLineEnd first_end; /* These two are considered only if relief is flat */ ZnLineEnd last_end; LineStyle line_style; /* This is considered only if relief is flat */ int cap_style; int join_style; ReliefStyle relief; ZnDim line_width; /* If 0 the path is not drawn, if <2 relief is flat */ ZnImage fill_pattern; ZnGradient *fill_color; ZnImage line_pattern; ZnGradient *line_color; ZnGradient *marker_color; ZnImage tile; /* Private data */ ZnPoly outlines; ZnGradient *gradient; ZnTriStrip tristrip; ZnPoint *grad_geo; } CurveItemStruct, *CurveItem; static ZnAttrConfig cv_attrs[] = { { ZN_CONFIG_CAP_STYLE, "-capstyle", NULL, Tk_Offset(CurveItemStruct, cap_style), 0, ZN_COORDS_FLAG, False }, { ZN_CONFIG_BOOL, "-closed", NULL, Tk_Offset(CurveItemStruct, flags), CLOSED_BIT, ZN_COORDS_FLAG, False }, { ZN_CONFIG_BOOL, "-composealpha", NULL, Tk_Offset(CurveItemStruct, header.flags), COMPOSE_ALPHA_BIT, ZN_DRAW_FLAG, False }, { ZN_CONFIG_BOOL, "-composerotation", NULL, Tk_Offset(CurveItemStruct, header.flags), COMPOSE_ROTATION_BIT, ZN_COORDS_FLAG, False }, { ZN_CONFIG_BOOL, "-composescale", NULL, Tk_Offset(CurveItemStruct, header.flags), COMPOSE_SCALE_BIT, ZN_COORDS_FLAG, False }, { ZN_CONFIG_GRADIENT, "-fillcolor", NULL, Tk_Offset(CurveItemStruct, fill_color), 0, ZN_COORDS_FLAG|ZN_BORDER_FLAG, False }, { ZN_CONFIG_BITMAP, "-fillpattern", NULL, Tk_Offset(CurveItemStruct, fill_pattern), 0, ZN_DRAW_FLAG, False }, { ZN_CONFIG_BOOL, "-filled", NULL, Tk_Offset(CurveItemStruct, flags), FILLED_BIT, ZN_COORDS_FLAG, False }, { ZN_CONFIG_LINE_END, "-firstend", NULL, Tk_Offset(CurveItemStruct, first_end), 0, ZN_COORDS_FLAG, False }, { ZN_CONFIG_JOIN_STYLE, "-joinstyle", NULL, Tk_Offset(CurveItemStruct, join_style), 0, ZN_COORDS_FLAG, False }, { ZN_CONFIG_LINE_END, "-lastend", NULL, Tk_Offset(CurveItemStruct, last_end), 0, ZN_COORDS_FLAG, False }, { ZN_CONFIG_GRADIENT, "-linecolor", NULL, Tk_Offset(CurveItemStruct, line_color), 0, ZN_DRAW_FLAG, False }, { ZN_CONFIG_BITMAP, "-linepattern", NULL, Tk_Offset(CurveItemStruct, line_pattern), 0, ZN_DRAW_FLAG, False }, { ZN_CONFIG_LINE_STYLE, "-linestyle", NULL, Tk_Offset(CurveItemStruct, line_style), 0, ZN_DRAW_FLAG, False }, { ZN_CONFIG_DIM, "-linewidth", NULL, Tk_Offset(CurveItemStruct, line_width), 0, ZN_COORDS_FLAG, False }, { ZN_CONFIG_PRI, "-priority", NULL, Tk_Offset(CurveItemStruct, header.priority), 0, ZN_DRAW_FLAG|ZN_REPICK_FLAG, False }, { ZN_CONFIG_BITMAP, "-marker", NULL, Tk_Offset(CurveItemStruct, marker), 0, ZN_COORDS_FLAG, False }, { ZN_CONFIG_GRADIENT, "-markercolor", NULL, Tk_Offset(CurveItemStruct, marker_color), 0, ZN_DRAW_FLAG, False }, { ZN_CONFIG_RELIEF, "-relief", NULL, Tk_Offset(CurveItemStruct, relief), 0, ZN_COORDS_FLAG, False }, { ZN_CONFIG_BOOL, "-sensitive", NULL, Tk_Offset(CurveItemStruct, header.flags), SENSITIVE_BIT, ZN_DRAW_FLAG, False }, { ZN_CONFIG_BOOL, "-smoothrelief", NULL, Tk_Offset(CurveItemStruct, flags), SMOOTH_RELIEF_BIT, ZN_REPICK_FLAG, False }, { ZN_CONFIG_TAG_LIST, "-tags", NULL, Tk_Offset(CurveItemStruct, header.tags), 0, 0, False }, { ZN_CONFIG_IMAGE, "-tile", NULL, Tk_Offset(CurveItemStruct, tile), 0, ZN_DRAW_FLAG, False }, { ZN_CONFIG_BOOL, "-visible", NULL, Tk_Offset(CurveItemStruct, header.flags), VISIBLE_BIT, ZN_DRAW_FLAG|ZN_REPICK_FLAG|ZN_VIS_FLAG, False }, { ZN_CONFIG_END, NULL, NULL, 0, 0, 0 } }; /* ********************************************************************************** * * Init -- * ********************************************************************************** */ static int Init(Item item, int *argc, Tcl_Obj *CONST *args[]) { WidgetInfo *wi = item->wi; CurveItem cv = (CurveItem) item; int i, num_points, count; ZnPoint *p, *points; char *controls; cv->outlines.num_contours = 0; cv->tristrip.num_strips = 0; cv->gradient = NULL; cv->grad_geo = NULL; /* Init attributes */ SET(item->flags, VISIBLE_BIT); SET(item->flags, SENSITIVE_BIT); SET(item->flags, COMPOSE_ALPHA_BIT); SET(item->flags, COMPOSE_ROTATION_BIT); SET(item->flags, COMPOSE_SCALE_BIT); CLEAR(cv->flags, CLOSED_BIT); CLEAR(cv->flags, SMOOTH_RELIEF_BIT); item->priority = DEFAULT_CURVE_PRIORITY; if (*argc < 1) { Tcl_AppendResult(wi->interp, " curve coords expected", NULL); return ZN_ERROR; } if (ZnParseCoordList(wi, (*args)[0], &points, &controls, &num_points) == ZN_ERROR) { return ZN_ERROR; } if (num_points == 0) { POLY_INIT(&cv->shape); } else { /* * Scan the control array (if any) to detect malformed * curves (more than 2 consecutive control points). */ if (controls) { count = 0; if ((controls[0]) || (controls[num_points-1])) { goto control_err; } for (i = 1; i < num_points-1; i++) { switch (controls[i]) { case 'c': count++; if (count > 2) { goto control_err; } break; case 0: count = 0; break; default: control_err: ZnFree(controls); Tcl_AppendResult(wi->interp, " curve coords expected", NULL); return ZN_ERROR; } } } p = ZnMalloc(num_points * sizeof(ZnPoint)); memcpy(p, points, num_points * sizeof(ZnPoint)); POLY_CONTOUR1(&cv->shape, p, num_points); cv->shape.contours[0].controls = controls; } (*args)++; (*argc)--; CLEAR(cv->flags, FILLED_BIT); cv->first_end = NULL; cv->last_end = NULL; cv->line_style = LINE_SIMPLE; cv->relief = RELIEF_FLAT; cv->line_width = 1; cv->tile = ZnUnspecifiedImage; cv->fill_pattern = ZnUnspecifiedImage; cv->line_pattern = ZnUnspecifiedImage; cv->cap_style = CapRound; cv->join_style = JoinRound; /* * In Tk marker visibility is controlled by the bitmap * being unspecified. */ SET(cv->flags, MARKED_BIT); cv->marker = ZnUnspecifiedImage; cv->fill_color = ZnGetGradientByValue(wi->fore_color); cv->line_color = ZnGetGradientByValue(wi->fore_color); cv->marker_color = ZnGetGradientByValue(wi->fore_color); return ZN_OK; } /* ********************************************************************************** * * Clone -- * ********************************************************************************** */ static void Clone(Item item) { CurveItem cv = (CurveItem) item; int i; ZnContour *conts; if (cv->grad_geo) { ZnPoint *grad_geo = ZnMalloc(4*sizeof(ZnPoint)); memcpy(grad_geo, cv->grad_geo, 4*sizeof(ZnPoint)); cv->grad_geo = grad_geo; } if (cv->shape.num_contours) { conts = cv->shape.contours; if (cv->shape.contours != &cv->shape.contour1) { cv->shape.contours = (ZnContour *) ZnMalloc(cv->shape.num_contours*sizeof(ZnContour)); } for (i = 0; i < cv->shape.num_contours; i++) { cv->shape.contours[i].num_points = conts[i].num_points; cv->shape.contours[i].points = ZnMalloc(conts[i].num_points*sizeof(ZnPoint)); memcpy(cv->shape.contours[i].points, conts[i].points, conts[i].num_points*sizeof(ZnPoint)); cv->shape.contours[i].controls = NULL; if (conts[i].controls) { cv->shape.contours[i].controls = ZnMalloc(conts[i].num_points*sizeof(char)); memcpy(cv->shape.contours[i].controls, conts[i].controls, conts[i].num_points*sizeof(char)); } } } if (cv->gradient) { cv->gradient = ZnGetGradientByValue(cv->gradient); } if (cv->first_end) { LineEndDuplicate(cv->first_end); } if (cv->last_end) { LineEndDuplicate(cv->last_end); } if (cv->tile != ZnUnspecifiedImage) { cv->tile = ZnGetImageByValue(cv->tile); } if (cv->line_pattern != ZnUnspecifiedImage) { cv->line_pattern = ZnGetImageByValue(cv->line_pattern); } if (cv->fill_pattern != ZnUnspecifiedImage) { cv->fill_pattern = ZnGetImageByValue(cv->fill_pattern); } if (cv->marker != ZnUnspecifiedImage) { cv->marker = ZnGetImageByValue(cv->marker); } cv->line_color = ZnGetGradientByValue(cv->line_color); cv->fill_color = ZnGetGradientByValue(cv->fill_color); cv->marker_color = ZnGetGradientByValue(cv->marker_color); cv->tristrip.num_strips = 0; cv->outlines.num_contours = 0; } /* ********************************************************************************** * * Destroy -- * ********************************************************************************** */ static void Destroy(Item item) { CurveItem cv = (CurveItem) item; int i; /* * Need to free the control array here, it is only known * by the Curve code. */ for (i = 0; i < cv->shape.num_contours; i++) { if (cv->shape.contours[i].controls) { ZnFree(cv->shape.contours[i].controls); } } POLY_FREE(&cv->shape); if (cv->grad_geo) { ZnFree(cv->grad_geo); } if (cv->first_end) { LineEndDelete(cv->first_end); } if (cv->last_end) { LineEndDelete(cv->last_end); } if (cv->gradient) { ZnFreeGradient(cv->gradient); } if (cv->tile != ZnUnspecifiedImage) { ZnFreeImage(cv->tile); cv->tile = ZnUnspecifiedImage; } if (cv->line_pattern != ZnUnspecifiedImage) { ZnFreeImage(cv->line_pattern); cv->line_pattern = ZnUnspecifiedImage; } if (cv->fill_pattern != ZnUnspecifiedImage) { ZnFreeImage(cv->fill_pattern); cv->fill_pattern = ZnUnspecifiedImage; } if (cv->marker != ZnUnspecifiedImage) { ZnFreeImage(cv->marker); cv->marker = ZnUnspecifiedImage; } ZnFreeGradient(cv->fill_color); ZnFreeGradient(cv->line_color); ZnFreeGradient(cv->marker_color); if (cv->tristrip.num_strips) { TRI_FREE(&cv->tristrip); } if (cv->outlines.num_contours) { POLY_FREE(&cv->outlines); } } /* ********************************************************************************** * * Setup flags to control the precedence between the * graphical attributes. * ********************************************************************************** */ static void SetRenderFlags(CurveItem cv) { ASSIGN(cv->flags, FILLED_OK, ISSET(cv->flags, FILLED_BIT) && (cv->shape.num_contours >= 1)); ASSIGN(cv->flags, RELIEF_OK, (cv->relief != RELIEF_FLAT) && (cv->shape.num_contours >= 1) && (cv->line_width > 1)); ASSIGN(cv->flags, MARKER_OK, (cv->marker != ZnUnspecifiedImage) && ISCLEAR(cv->flags, RELIEF_OK)); ASSIGN(cv->flags, FIRST_END_OK, (cv->first_end != NULL) && (cv->shape.num_contours == 1) && (cv->shape.contours[0].num_points > 1) && ISCLEAR(cv->flags, FILLED_BIT) && cv->line_width && ISCLEAR(cv->flags, RELIEF_OK) && ISCLEAR(cv->flags, CLOSED_BIT)); ASSIGN(cv->flags, LAST_END_OK, (cv->last_end != NULL) && (cv->shape.num_contours == 1) && (cv->shape.contours[0].num_points > 1) && ISCLEAR(cv->flags, FILLED_BIT) && cv->line_width && ISCLEAR(cv->flags, RELIEF_OK) && ISCLEAR(cv->flags, CLOSED_BIT)); } /* ********************************************************************************** * * Configure -- * ********************************************************************************** */ static int Configure(Item item, int argc, Tcl_Obj *CONST argv[], int *flags) { WidgetInfo *wi = item->wi; CurveItem cv = (CurveItem) item; int status = ZN_OK; XColor *color; int alpha; status = ZnConfigureAttributes(wi, item, cv_attrs, argc, argv, flags); if (cv->gradient && (ISSET(*flags, ZN_BORDER_FLAG) || (cv->relief == RELIEF_FLAT))) { ZnFreeGradient(cv->gradient); cv->gradient = NULL; } if ((cv->relief != RELIEF_FLAT) && !cv->gradient) { color = ZnGetGradientColor(cv->line_color, 51.0, &alpha); cv->gradient = ZnGetReliefGradient(wi->interp, wi->win, ZnNameOfColor(color), alpha); if (cv->gradient == NULL) { status = ZN_ERROR; } } return status; } /* ********************************************************************************** * * Query -- * ********************************************************************************** */ static int Query(Item item, int argc, Tcl_Obj *CONST argv[]) { if (ZnQueryAttribute(item->wi, item, cv_attrs, argv[0]) == ZN_ERROR) { return ZN_ERROR; } return ZN_OK; } static void CurveTessBegin(GLenum type, void *data) { CurveItem cv = data; WidgetInfo *wi = ((Item) data)->wi; ZnListEmpty(wi->work_pts); wi->tess_type = type; if (type == GL_LINE_LOOP) { cv->outlines.num_contours++; cv->outlines.contours = ZnRealloc(cv->outlines.contours, cv->outlines.num_contours * sizeof(ZnContour)); } else { cv->tristrip.num_strips++; cv->tristrip.strips = ZnRealloc(cv->tristrip.strips, cv->tristrip.num_strips * sizeof(ZnStrip)); cv->tristrip.strips[cv->tristrip.num_strips-1].fan = (type==GL_TRIANGLE_FAN); } /*printf("Début de fragment de type: %s\n", (type == GL_TRIANGLE_FAN) ? "FAN" : (type == GL_TRIANGLE_STRIP) ? "STRIP" : (type == GL_TRIANGLES) ? "TRIANGLES" : (type == GL_LINE_LOOP) ? "LINE LOOP" : "");*/ } static void CurveTessVertex(void *vertex_data, void *data) { CurveItem cv = data; WidgetInfo *wi = ((Item) data)->wi; ZnPoint p; int size; p.x = ((GLdouble *) vertex_data)[0]; p.y = ((GLdouble *) vertex_data)[1]; /*printf("Sommet en %g %g\n", p.x, p.y);*/ size = ZnListSize(wi->work_pts); if ((wi->tess_type == GL_TRIANGLES) && (size == 3)) { cv->tristrip.strips[cv->tristrip.num_strips-1].num_points = size; cv->tristrip.strips[cv->tristrip.num_strips-1].points = ZnMalloc(size * sizeof(ZnPoint)); memcpy(cv->tristrip.strips[cv->tristrip.num_strips-1].points, ZnListArray(wi->work_pts), size * sizeof(ZnPoint)); /* Allocate a new fragment */ ZnListEmpty(wi->work_pts); cv->tristrip.num_strips++; cv->tristrip.strips = ZnRealloc(cv->tristrip.strips, cv->tristrip.num_strips * sizeof(ZnStrip)); cv->tristrip.strips[cv->tristrip.num_strips-1].fan = GL_TRIANGLES; } ZnListAdd(wi->work_pts, &p, ZnListTail); } static void CurveTessEnd(void *data) { CurveItem cv = data; WidgetInfo *wi = ((Item) data)->wi; int size = ZnListSize(wi->work_pts); int num; if (wi->tess_type == GL_LINE_LOOP) { /* Add the last point to close the outline */ size++; num = cv->outlines.num_contours; cv->outlines.contours[num-1].num_points = size; cv->outlines.contours[num-1].points = ZnMalloc(size * sizeof(ZnPoint)); memcpy(cv->outlines.contours[num-1].points, ZnListArray(wi->work_pts), size * sizeof(ZnPoint)); cv->outlines.contours[num-1].points[size-1] = cv->outlines.contours[num-1].points[0]; cv->outlines.contours[num-1].cw = !TestCCW(cv->outlines.contours[num-1].points, size); } else { num = cv->tristrip.num_strips; cv->tristrip.strips[num-1].num_points = size; cv->tristrip.strips[num-1].points = ZnMalloc(size * sizeof(ZnPoint)); memcpy(cv->tristrip.strips[num-1].points, ZnListArray(wi->work_pts), size * sizeof(ZnPoint)); } /* printf("Fin de fragment %d\n", num);*/ } static void CurveTessCombine(GLdouble coords[3], void *vertex_data[4], GLfloat weight[4], void **out_data, void *data) { WidgetInfo *wi = ((Item) data)->wi; ZnCombineData *cdata; cdata = ZnMalloc(sizeof(ZnCombineData)); cdata->v[0] = coords[0]; cdata->v[1] = coords[1]; cdata->next = wi->tess_combine_list; wi->tess_combine_list = cdata; *out_data = &cdata->v; /*printf("Création d'un nouveau sommet en %g %g\n", cdata->v[0], cdata->v[1]);*/ } static void CurveTessError(GLenum errno, void *data) { const GLubyte *msg; msg = gluErrorString(errno); fprintf(stderr, "Tesselation error in curve item: %s\n", msg); } static void UpdateTristrip(CurveItem cv, ZnPoly *poly) { WidgetInfo *wi = ((Item) cv)->wi; ZnCombineData *cdata, *cnext; GLdouble v[3]; int i, j; gluTessCallback(wi->tess, GLU_TESS_BEGIN_DATA, (void (*)()) CurveTessBegin); gluTessCallback(wi->tess, GLU_TESS_VERTEX_DATA, (void (*)()) CurveTessVertex); gluTessCallback(wi->tess, GLU_TESS_END_DATA, (void (*)()) CurveTessEnd); gluTessCallback(wi->tess, GLU_TESS_COMBINE_DATA, (void (*)()) CurveTessCombine); gluTessCallback(wi->tess, GLU_TESS_ERROR_DATA, (void (*)()) CurveTessError); gluTessProperty(wi->tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE); gluTessNormal(wi->tess, 0, 0, -1); if (cv->tristrip.num_strips == 0) { gluTessProperty(wi->tess, GLU_TESS_BOUNDARY_ONLY, GL_FALSE); gluTessBeginPolygon(wi->tess, cv); for (j = 0; j < poly->num_contours; j++){ gluTessBeginContour(wi->tess); /*printf("Début contour %d num_points %d %d\n", j, poly->contours[j].num_points, poly->contours[j].cw);*/ for (i = 0; i < poly->contours[j].num_points; i++) { /*printf("%g@%g ", poly->contours[j].points[i].x, poly->contours[j].points[i].y);*/ v[0] = poly->contours[j].points[i].x; v[1] = poly->contours[j].points[i].y; v[2] = 0; gluTessVertex(wi->tess, v, &poly->contours[j].points[i]); } /*printf("\n");*/ gluTessEndContour(wi->tess); } gluTessEndPolygon(wi->tess); cdata = wi->tess_combine_list; while (cdata) { cnext = cdata->next; ZnFree(cdata); cdata = cnext; } wi->tess_combine_list = NULL; } } static void UpdateOutlines(CurveItem cv, ZnPoly *poly) { WidgetInfo *wi = ((Item) cv)->wi; ZnCombineData *cdata, *cnext; GLdouble v[3]; int i, j; gluTessCallback(wi->tess, GLU_TESS_BEGIN_DATA, (void (*)()) CurveTessBegin); gluTessCallback(wi->tess, GLU_TESS_VERTEX_DATA, (void (*)()) CurveTessVertex); gluTessCallback(wi->tess, GLU_TESS_END_DATA, (void (*)()) CurveTessEnd); gluTessCallback(wi->tess, GLU_TESS_COMBINE_DATA, (void (*)()) CurveTessCombine); gluTessCallback(wi->tess, GLU_TESS_ERROR_DATA, (void (*)()) CurveTessError); gluTessProperty(wi->tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE); gluTessNormal(wi->tess, 0, 0, -1); if (cv->outlines.num_contours == 0) { gluTessProperty(wi->tess, GLU_TESS_BOUNDARY_ONLY, GL_TRUE); gluTessBeginPolygon(wi->tess, cv); for (j = 0; j < poly->num_contours; j++){ gluTessBeginContour(wi->tess); for (i = 0; i < poly->contours[j].num_points; i++) { v[0] = poly->contours[j].points[i].x; v[1] = poly->contours[j].points[i].y; v[2] = 0; gluTessVertex(wi->tess, v, &poly->contours[j].points[i]); } gluTessEndContour(wi->tess); } gluTessEndPolygon(wi->tess); cdata = wi->tess_combine_list; while (cdata) { cnext = cdata->next; ZnFree(cdata); cdata = cnext; } wi->tess_combine_list = NULL; } } /* ********************************************************************************** * * ComputeCoordinates -- * ********************************************************************************** */ static void ComputeCoordinates(Item item, ZnBool force) { WidgetInfo *wi = item->wi; CurveItem cv = (CurveItem) item; int i, j; ZnPoint end_points[LINE_END_POINTS]; ZnPoint *points; int num_points, num_contours, segment_start; ZnBBox bbox; int lw; ZnContour *c1, *c2; ZnPoly dev; ResetBBox(&item->item_bounding_box); /* printf("Curve CC: flags %x\n", cv->flags);*/ SetRenderFlags(cv); num_contours = cv->shape.num_contours; if (num_contours == 0) { return; } if (cv->tristrip.num_strips) { TRI_FREE(&cv->tristrip); } if (cv->outlines.num_contours) { POLY_FREE(&cv->outlines); }; POLY_INIT(&dev); if (num_contours != 1) { dev.contours = ZnMalloc(num_contours * sizeof(ZnContour)); dev.num_contours = num_contours; } else { dev.contours = &dev.contour1; dev.num_contours = 1; } for (c1 = cv->shape.contours, c2 = dev.contours, i = 0; i < cv->shape.num_contours; i++, c1++, c2++) { c2->cw = c1->cw; c2->num_points = c1->num_points; /* * Add a point at the end of the contour to close it * if needed. Works only if a single contour is given, in * this case it can be used as a path. In the case of * multiple contours, the outline is automatically * closed by the tesselator. */ if ((num_contours == 1) && (c1->num_points > 2) && ISSET(cv->flags, CLOSED_BIT) && ((c1->points[0].x != c1->points[c2->num_points-1].x) || (c1->points[0].y != c1->points[c2->num_points-1].y)) && (c1->num_points > 2)) { c2->num_points++; } c2->points = ZnMalloc((c2->num_points)*sizeof(ZnPoint)); /*printf("CC: \"%d\" num_points %d\n", item->id, c1->num_points);*/ ZnTransformPoints(wi->current_transfo, c1->points, c2->points, c1->num_points); if (c1->num_points != c2->num_points) { c2->points[c2->num_points-1] = c2->points[0]; } /* * Now expand the bezier segments into polylines. */ if (c1->controls) { segment_start = 0; ZnListEmpty(wi->work_pts); ZnListAdd(wi->work_pts, &c2->points[0], ZnListTail); /*printf("moveto %g@%g\n", c2->points[0].x, c2->points[0].y);*/ for (j = 1; j < c1->num_points; j++) { if (!c1->controls[j]) { if (segment_start != j-1) { /* traitement bezier */ /* printf("arcto %g@%g %g@%g %g@%g\n", c2->points[segment_start+1].x, c2->points[segment_start+1].y, c2->points[j-1].x, c2->points[j-1].y, c2->points[j].x, c2->points[j].y);*/ GetBezierPoints(&c2->points[segment_start], &c2->points[segment_start+1], &c2->points[j-1], &c2->points[j], wi->work_pts, 2.0); } else { /*printf("lineto %g@%g\n", c2->points[j].x, c2->points[j].y);*/ ZnListAdd(wi->work_pts, &c2->points[j], ZnListTail); } segment_start = j; } } /* * Replce the original path by the expanded, closing it as * needed (one open contour). */ num_points =ZnListSize(wi->work_pts); if (c2->num_points != c1->num_points) { num_points++; } c2->points = ZnRealloc(c2->points, num_points*sizeof(ZnPoint)); memcpy(c2->points, ZnListArray(wi->work_pts), num_points*sizeof(ZnPoint)); if (c2->num_points != c1->num_points) { c2->points[num_points-1] = c2->points[0]; } c2->num_points = num_points; } } UpdateTristrip(cv, &dev); if (num_contours == 1) { POLY_CONTOUR1(&cv->outlines, dev.contours[0].points, dev.contours[0].num_points); cv->outlines.contours[0].cw = dev.contours[0].cw; } else { UpdateOutlines(cv, &dev); POLY_FREE(&dev); } lw = cv->line_width; num_contours = cv->outlines.num_contours; if (ISSET(cv->flags, RELIEF_OK)) { c2 = cv->outlines.contours; for (i = 0; i < num_contours; i++, c2++) { /* * Add to bounding box. */ ZnGetPolygonReliefBBox(c2->points, c2->num_points, lw, &bbox); AddBBoxToBBox(&item->item_bounding_box, &bbox); } } else { c2 = cv->outlines.contours; for (i = 0; i < num_contours; i++, c2++) { AddPointsToBBox(&item->item_bounding_box, c2->points, c2->num_points); } /* * Add the line width in all directions. * This overestimates the space needed to draw the polyline * but is simple. */ item->item_bounding_box.orig.x -= lw; item->item_bounding_box.orig.y -= lw; item->item_bounding_box.corner.x += lw; item->item_bounding_box.corner.y += lw; /* * Take care of miters, markers and arrows. */ c2 = cv->outlines.contours; for (j = 0; j < num_contours; j++, c2++) { if (c2->cw) { continue; } if (cv->join_style == JoinMiter) { ZnPoint miter_i, miter_o; for (i = c2->num_points-1, points = c2->points; i >= 3; i--, points++) { GetMiterPoints(points, points+1, points+2, lw, &miter_i, &miter_o); AddPointToBBox(&item->item_bounding_box, miter_i.x, miter_i.y); AddPointToBBox(&item->item_bounding_box, miter_o.x, miter_o.y); } } /* * Add the markers. */ if (ISSET(cv->flags, MARKER_OK)) { int w, h; ZnBBox bbox; ZnSizeOfImage(cv->marker, &w, &h); w = w/2 + 2; h = h/2 + 2; num_points = c2->num_points; for (i = 0, points = c2->points; i < num_points; i++, points++) { bbox.orig.x = points->x - w; bbox.orig.y = points->y - h; bbox.corner.x = points->x + w; bbox.corner.y = points->y + h; AddBBoxToBBox(&item->item_bounding_box, &bbox); } } /* * Process arrows. */ num_points = c2->num_points; points = c2->points; if (ISSET(cv->flags, FIRST_END_OK)) { GetLineEnd(&points[0], &points[1], lw, cv->cap_style, cv->first_end, end_points); AddPointsToBBox(&item->item_bounding_box, end_points, LINE_END_POINTS); } if (ISSET(cv->flags, LAST_END_OK)) { GetLineEnd(&points[num_points-1], &points[num_points-2], lw, cv->cap_style, cv->last_end, end_points); AddPointsToBBox(&item->item_bounding_box, end_points, LINE_END_POINTS); } } /* * Expand again the bounding box by one pixel in all * directions to take care of rounding errors. */ item->item_bounding_box.orig.x -= 1; item->item_bounding_box.orig.y -= 1; item->item_bounding_box.corner.x += 1; item->item_bounding_box.corner.y += 1; } if (!ZnGradientFlat(cv->fill_color)) { if (!cv->grad_geo) { cv->grad_geo = ZnMalloc(4*sizeof(ZnPoint)); } if (cv->fill_color->type == ZN_AXIAL_GRADIENT) { ZnComputeAxialGradient(wi, &cv->shape, cv->fill_color->g.angle, cv->grad_geo); } else if (cv->fill_color->type == ZN_RADIAL_GRADIENT) { ZnComputeRadialGradient(wi, &cv->outlines, &item->item_bounding_box, &cv->fill_color->g.p, cv->grad_geo); } else if (cv->fill_color->type == ZN_PATH_GRADIENT) { ZnTransformPoint(wi->current_transfo, &cv->fill_color->g.p, &cv->grad_geo[0]); } } else { if (cv->grad_geo) { ZnFree(cv->grad_geo); cv->grad_geo = NULL; } } } /* ********************************************************************************** * * ToArea -- * Tell if the object is entirely outside (-1), * entirely inside (1) or in between (0). * ********************************************************************************** */ static int ToArea(Item item, ZnToArea ta) { CurveItem cv = (CurveItem) item; ZnBBox bbox, *area = ta->area; ZnPoint *points; ZnPoint triangle[3]; ZnPoint end_points[LINE_END_POINTS]; int i, j, num_points, result=-1, result2; int width, height; ZnBool first_done = False; if (cv->outlines.num_contours == 0) { return -1; } /*printf("============== poly %d ==============\n", item->id);*/ if (ISSET(cv->flags, FILLED_OK)) { /*printf("testing surfaces\n");*/ for (i = 0; i < cv->tristrip.num_strips; i++) { num_points = cv->tristrip.strips[i].num_points; points = cv->tristrip.strips[i].points; j = 0; if (cv->tristrip.strips[i].fan) { triangle[0] = points[0]; j++; } for (; j < num_points-2; j++, points++) { if (cv->tristrip.strips[i].fan) { triangle[1] = points[0]; triangle[2] = points[1]; } else { triangle[0] = points[0]; triangle[1] = points[1]; triangle[2] = points[2]; } if (!first_done) { first_done = True; result = PolygonInBBox(triangle, 3, area, NULL); } else { result2 = PolygonInBBox(triangle, 3, area, NULL); if (result2 != result) { return 0; } } } } } if (cv->line_width > 0) { /*printf("testing lines\n");*/ for (i = 0; i < cv->outlines.num_contours; i++) { num_points = cv->outlines.contours[i].num_points; points = cv->outlines.contours[i].points; if (!first_done) { first_done = True; if (ISCLEAR(cv->flags, RELIEF_OK)) { result = PolylineInBBox(points, num_points, cv->line_width, cv->cap_style, cv->join_style, area); } else { result = ZnPolygonReliefInBBox(points, num_points, cv->line_width, area); } if (result == 0) { return 0; } } else { if (ISCLEAR(cv->flags, RELIEF_OK)) { result2 = PolylineInBBox(points, num_points, cv->line_width, cv->cap_style, cv->join_style, area); } else { result2 = ZnPolygonReliefInBBox(points, num_points, cv->line_width, area); } if (result2 != result) { return 0; } } } /* * Check line ends (only on first contour). */ points = cv->outlines.contours[0].points; num_points = cv->outlines.contours[0].num_points; if (ISSET(cv->flags, FIRST_END_OK)) { GetLineEnd(&points[0], &points[1], cv->line_width, cv->cap_style, cv->first_end, end_points); if (PolygonInBBox(end_points, LINE_END_POINTS, area, NULL) != result) { return 0; } } if (ISSET(cv->flags, LAST_END_OK)) { GetLineEnd(&points[num_points-1], &points[num_points-2], cv->line_width, cv->cap_style, cv->last_end, end_points); if (PolygonInBBox(end_points, LINE_END_POINTS, area, NULL) != result) { return 0; } } } /* * Last, check markers */ if (ISSET(cv->flags, MARKER_OK)) { for (i = 0; i < cv->outlines.num_contours; i++) { points = cv->outlines.contours[i].points; num_points = cv->outlines.contours[i].num_points; if (ISSET(cv->flags, FIRST_END_OK)) { num_points--; points++; } if (ISSET(cv->flags, LAST_END_OK)) { num_points--; } ZnSizeOfImage(cv->marker, &width, &height); for (; num_points > 0; num_points--, points++) { bbox.orig.x = points->x - (width+1)/2; bbox.orig.y = points->y - (height+1)/2; bbox.corner.x = bbox.orig.x + width; bbox.corner.y = bbox.orig.y + height; if (BBoxInBBox(&bbox, area) != result) { return 0; } } } } return result; } /* ********************************************************************************** * * Draw -- * ********************************************************************************** */ static void Draw(Item item) { WidgetInfo *wi = item->wi; CurveItem cv = (CurveItem) item; XGCValues values; int i, j, num_points=0, num2; unsigned int gc_mask; ZnPoint *points=NULL; XPoint *xpoints=NULL; if ((cv->outlines.num_contours == 0) || (ISCLEAR(cv->flags, FILLED_OK) && !cv->line_width && ISCLEAR(cv->flags, MARKER_OK))) { return; } /* * Fill if requested. */ if (ISSET(cv->flags, FILLED_OK)) { values.foreground = ZnPixel(ZnGetGradientColor(cv->fill_color, 0.0, NULL)); gc_mask = GCFillStyle; if (cv->tile != ZnUnspecifiedImage) { /* Fill tiled */ values.fill_style = FillTiled; values.tile = ZnImagePixmap(cv->tile, NULL); values.ts_x_origin = REAL_TO_INT(item->item_bounding_box.orig.x); values.ts_y_origin = REAL_TO_INT(item->item_bounding_box.orig.y); gc_mask |= GCTileStipXOrigin|GCTileStipYOrigin|GCTile; } else if (cv->fill_pattern != ZnUnspecifiedImage) { /* Fill stippled */ values.fill_style = FillStippled; values.stipple = ZnImagePixmap(cv->fill_pattern, NULL); values.ts_x_origin = REAL_TO_INT(item->item_bounding_box.orig.x); values.ts_y_origin = REAL_TO_INT(item->item_bounding_box.orig.y); gc_mask |= GCTileStipXOrigin|GCTileStipYOrigin|GCStipple|GCForeground; } else { /* Fill solid */ values.fill_style = FillSolid; gc_mask |= GCForeground; } XChangeGC(wi->dpy, wi->gc, gc_mask, &values); for (i = 0; i < cv->tristrip.num_strips; i++) { num_points = cv->tristrip.strips[i].num_points; points = cv->tristrip.strips[i].points; ZnListAssertSize(wi->work_xpts, num_points); xpoints = (XPoint *) ZnListArray(wi->work_xpts); for (j = 0; j < num_points; j++) { xpoints[j].x = REAL_TO_INT(points[j].x); xpoints[j].y = REAL_TO_INT(points[j].y); } for (j = 0; j < num_points-2; j++) { XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc, &xpoints[j], 3, Convex, CoordModeOrigin); } } } /* * Draw the lines between points */ if (cv->line_width) { ZnPoint end_points[LINE_END_POINTS]; XPoint xp[LINE_END_POINTS]; /* * Drawing with relief disables: ends, line style and line pattern. */ if (ISSET(cv->flags, RELIEF_OK)) { for (j = 0; j < cv->outlines.num_contours; j++) { num_points = cv->outlines.contours[j].num_points; points = cv->outlines.contours[j].points; /*printf("Draw: item %d, num_points %d %g@%g %g@%g, cw %d i/o %d\n", item->id, num_points, points[0].x, points[0].y, points[num_points-1].x, points[num_points-1].y, cv->outlines.contours[j].cw);*/ ZnDrawPolygonRelief(wi, cv->relief, cv->gradient, points, num_points, cv->line_width); } } else { ZnSetLineStyle(wi, cv->line_style); values.foreground = ZnPixel(ZnGetGradientColor(cv->line_color, 0, NULL)); values.line_width = (cv->line_width == 1) ? 0 : cv->line_width; values.join_style = cv->join_style; values.cap_style = cv->cap_style; if (cv->line_pattern == ZnUnspecifiedImage) { values.fill_style = FillSolid; XChangeGC(wi->dpy, wi->gc, GCFillStyle|GCLineWidth|GCJoinStyle|GCCapStyle|GCForeground, &values); } else { values.fill_style = FillStippled; values.stipple = ZnImagePixmap(cv->line_pattern, NULL); XChangeGC(wi->dpy, wi->gc, GCFillStyle|GCStipple|GCLineWidth|GCJoinStyle|GCCapStyle|GCForeground, &values); } for (j = 0; j < cv->outlines.num_contours; j++) { num2 = num_points = cv->outlines.contours[j].num_points; points = cv->outlines.contours[j].points; ZnListAssertSize(wi->work_xpts, num_points); xpoints = (XPoint *) ZnListArray(wi->work_xpts); for (i = 0; i < num2; i++) { xpoints[i].x = REAL_TO_INT(points[i].x); xpoints[i].y = REAL_TO_INT(points[i].y); } if (ISSET(cv->flags, CLOSED_BIT)) { xpoints[num2] = xpoints[0]; } XDrawLines(wi->dpy, wi->draw_buffer, wi->gc, xpoints, num_points, CoordModeOrigin); } if (ISSET(cv->flags, FIRST_END_OK)) { GetLineEnd(&points[0], &points[1], cv->line_width, cv->cap_style, cv->first_end, end_points); for (i = 0; i < LINE_END_POINTS; i++) { xp[i].x = end_points[i].x; xp[i].y = end_points[i].y; } XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc, xp, LINE_END_POINTS, Nonconvex, CoordModeOrigin); } if (ISSET(cv->flags, LAST_END_OK)) { GetLineEnd(&points[num_points-1], &points[num_points-2], cv->line_width, cv->cap_style, cv->last_end, end_points); for (i = 0; i < LINE_END_POINTS; i++) { xp[i].x = end_points[i].x; xp[i].y = end_points[i].y; } XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc, xp, LINE_END_POINTS, Nonconvex, CoordModeOrigin); } } } /* * Draw the marks at each point. If arrows are specified or * if last point join first point suppress markers at end points. */ if (ISSET(cv->flags, MARKER_OK)) { int h_width, h_height, width, height; ZnPoint ptmp; ZnSizeOfImage(cv->marker, &width, &height); h_width = (width+1)/2; h_height = (height+1)/2; values.fill_style = FillStippled; values.stipple = ZnImagePixmap(cv->marker, NULL); values.foreground = ZnPixel(ZnGetGradientColor(cv->marker_color, 0, NULL)); XChangeGC(wi->dpy, wi->gc, GCFillStyle|GCStipple|GCForeground, &values); for (j = 0; j < cv->outlines.num_contours; j++) { num_points = cv->outlines.contours[j].num_points; points = cv->outlines.contours[j].points; ZnListAssertSize(wi->work_xpts, num_points); xpoints = (XPoint *) ZnListArray(wi->work_xpts); for (i = 0; i < num_points; i++) { xpoints[i].x = REAL_TO_INT(points[i].x); xpoints[i].y = REAL_TO_INT(points[i].y); } if (ISSET(cv->flags, FIRST_END_OK)) { num_points--; points++; } if (ISSET(cv->flags, LAST_END_OK)) { num_points--; } for (; num_points > 0; num_points--, points++) { ptmp.x = points->x - h_width; ptmp.y = points->y - h_height; values.ts_x_origin = ptmp.x; values.ts_y_origin = ptmp.y; XChangeGC(wi->dpy, wi->gc, GCTileStipXOrigin|GCTileStipYOrigin|GCForeground, &values); XFillRectangle(wi->dpy, wi->draw_buffer, wi->gc, ptmp.x, ptmp.y, width, height); } } } } /* ********************************************************************************** * * Render -- * ********************************************************************************** */ #ifdef GLX static void CurveRenderCB(void *closure) { CurveItem cv = (CurveItem) closure; int i, j, num_points; ZnPoint *points; for (i = 0; i < cv->tristrip.num_strips; i++) { num_points = cv->tristrip.strips[i].num_points; points = cv->tristrip.strips[i].points; if (cv->tristrip.strips[i].fan) { glBegin(GL_TRIANGLE_FAN); } else { glBegin(GL_TRIANGLE_STRIP); } for (j = 0; j < num_points; j++, points++) { glVertex2f(points->x, points->y); } glEnd(); } } #endif static void Render(Item item) { #ifdef GLX WidgetInfo *wi = item->wi; CurveItem cv = (CurveItem) item; int j, num_points; ZnPoint *points; XColor *color; int alpha; if ((cv->outlines.num_contours == 0) || (ISCLEAR(cv->flags, FILLED_OK) && !cv->line_width && ISCLEAR(cv->flags, MARKER_OK))) { return; } /* * Fill if requested. */ if (ISSET(cv->flags, FILLED_OK)) { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); if (!ZnGradientFlat(cv->fill_color)) { ZnRenderGradient(wi, cv->fill_color, CurveRenderCB, cv, cv->grad_geo, &cv->outlines); } else if (cv->tile != ZnUnspecifiedImage) { /* Fill tiled */ ZnRenderTile(wi, cv->tile, cv->fill_color, CurveRenderCB, cv, (ZnPoint *) &item->item_bounding_box); } else { if (cv->fill_pattern != ZnUnspecifiedImage) { /* Fill stippled */ /* * Setup polygon stippling. */ glEnable(GL_POLYGON_STIPPLE); glPolygonStipple(ZnImagePattern(cv->fill_pattern, NULL)); } color = ZnGetGradientColor(cv->fill_color, 0.0, &alpha); alpha = ZnComposeAlpha(alpha, wi->alpha); glColor4us(color->red, color->green, color->blue, alpha); CurveRenderCB(cv); glDisable(GL_POLYGON_STIPPLE); } } /* * Draw the lines between points */ if (cv->line_width) { /* * Drawing with relief disables: ends, line style and line pattern. */ ZnGetGradientColor(cv->line_color, 0, &alpha); alpha = ZnComposeAlpha(alpha, wi->alpha); if (ISSET(cv->flags, RELIEF_OK)) { for (j = 0; j < cv->outlines.num_contours; j++) { num_points = cv->outlines.contours[j].num_points; points = cv->outlines.contours[j].points; /*printf("Render: item %d, num_points %d %g@%g %g@%g, cw %d i/o %d\n", item->id, num_points, points[0].x, points[0].y, points[num_points-1].x, points[num_points-1].y, cv->outlines.contours[j].cw);*/ ZnRenderPolygonRelief(wi, cv->relief, cv->gradient, ISSET(cv->flags, SMOOTH_RELIEF_BIT), points, num_points, cv->line_width); } } else { ZnLineEnd first = ISSET(cv->flags, FIRST_END_OK) ? cv->first_end : NULL; ZnLineEnd last = ISSET(cv->flags, LAST_END_OK) ? cv->last_end : NULL; for (j = 0; j < cv->outlines.num_contours; j++) { ZnRenderPolyline(wi, cv->outlines.contours[j].points, cv->outlines.contours[j].num_points, cv->line_width, cv->line_style, cv->cap_style, cv->join_style, first, last, cv->line_color); } } } /* * Draw the marks at each point. If arrows are specified or * if last point join first point suppress markers at end points. */ if (ISSET(cv->flags, MARKER_OK)) { int h_width, h_height; ZnPoint ptmp; ZnSizeOfImage(cv->marker, &h_width, & h_height); h_width = (h_width+1.0)/2.0; h_height = (h_height+1.0)/2.0; for (j = 0; j < cv->outlines.num_contours; j++) { num_points = cv->outlines.contours[j].num_points; points = cv->outlines.contours[j].points; if (ISSET(cv->flags, FIRST_END_OK)) { num_points--; points++; } if (ISSET(cv->flags, LAST_END_OK)) { num_points--; } for (; num_points > 0; num_points--, points++) { ptmp.x = points->x - h_width; ptmp.y = points->y - h_height; ZnRenderIcon(wi, cv->marker, cv->marker_color, &ptmp, True); } } } #endif } /* ********************************************************************************** * * IsSensitive -- * ********************************************************************************** */ static ZnBool IsSensitive(Item item, int item_part) { return (ISSET(item->flags, SENSITIVE_BIT) && item->parent->class->IsSensitive(item->parent, ZN_NO_PART)); } /* ********************************************************************************** * * Pick -- * ********************************************************************************** */ static double Pick(Item item, ZnPick ps) { CurveItem cv = (CurveItem) item; ZnBBox bbox; double dist=1.0e40, new_dist; ZnPoint *points, *p = ps->point; ZnPoint end_points[LINE_END_POINTS]; int num_points; int width, height; int i; if (cv->outlines.num_contours == 0) { return dist; } /*printf("Pick in curve\n");*/ if (ISSET(cv->flags, FILLED_OK)) { double hole_dist=1.0e40, new_hole_dist; /* * Check all contours. Compute distance to holes * in the same pass. */ for (i = 0; i < cv->outlines.num_contours; i++) { if (cv->outlines.contours[i].cw) { new_hole_dist = PolygonToPointDist(cv->outlines.contours[i].points, cv->outlines.contours[i].num_points, p); if (new_hole_dist < hole_dist) { hole_dist = new_hole_dist; } } else { new_dist = PolygonToPointDist(cv->outlines.contours[i].points, cv->outlines.contours[i].num_points, p); if (new_dist < dist) { dist = new_dist; } } } if ((dist <= 0.0) && (hole_dist >= 0.0)) { /*printf("dist %g hole dist: %g\n", dist, hole_dist);*/ return 0.0; } if (hole_dist < 0.0) { hole_dist = -hole_dist; } if (dist <= 0.0) { dist = hole_dist; } else if (hole_dist < dist) { dist = hole_dist; } } if (cv->line_width > 0) { /* * Check all contours. */ for (i = 0; i < cv->outlines.num_contours; i++) { points = cv->outlines.contours[i].points; num_points = cv->outlines.contours[i].num_points; if (ISCLEAR(cv->flags, RELIEF_OK)) { new_dist = PolylineToPointDist(points, num_points, cv->line_width, cv->cap_style, cv->join_style, p); if (new_dist < dist) { dist = new_dist; } if (dist <= 0.0) { /*printf("dist %g\n", dist);*/ return 0.0; } } else { new_dist = ZnPolygonReliefToPointDist(points, num_points, cv->line_width, p); if (new_dist < dist) { dist = new_dist; } if (dist <= 0.0) { /*printf("dist %g\n", dist);*/ return 0.0; } } } } /* * Line ends are checked only on the first contour. */ points = cv->outlines.contours[0].points; num_points = cv->outlines.contours[0].num_points; /* * Check line ends. */ if (ISSET(cv->flags, FIRST_END_OK)) { GetLineEnd(&points[0], &points[1], cv->line_width, cv->cap_style, cv->first_end, end_points); new_dist = PolygonToPointDist(end_points, LINE_END_POINTS, p); if (new_dist < dist) { dist = new_dist; } if (dist <= 0.0) { /*printf("dist %g\n", dist);*/ return 0.0; } } if (ISSET(cv->flags, LAST_END_OK)) { GetLineEnd(&points[num_points-1], &points[num_points-2], cv->line_width, cv->cap_style, cv->last_end, end_points); new_dist = PolygonToPointDist(end_points, LINE_END_POINTS, p); if (new_dist < dist) { dist = new_dist; } if (dist <= 0.0) { /*printf("dist %g\n", dist);*/ return 0.0; } } /* * Last, check markers on all contours. */ if (ISSET(cv->flags, MARKER_OK)) { for (i = 0; i < cv->outlines.num_contours; i++) { points = cv->outlines.contours[i].points; num_points = cv->outlines.contours[i].num_points; if (ISSET(cv->flags, FIRST_END_OK)) { num_points--; points++; } if (ISSET(cv->flags, LAST_END_OK)) { num_points--; } ZnSizeOfImage(cv->marker, &width, &height); for (; num_points > 0; num_points--, points++) { bbox.orig.x = points->x - (width+1)/2; bbox.orig.y = points->y - (height+1)/2; bbox.corner.x = bbox.orig.x + width; bbox.corner.y = bbox.orig.y + height; new_dist = RectangleToPointDist(&bbox, p); if (new_dist < dist) { dist = new_dist; } if (dist <= 0.0) { /*printf("dist %g\n", dist);*/ return 0.0; } } } } /*printf("dist %g\n", dist);*/ return dist; } /* ********************************************************************************** * * PostScript -- * ********************************************************************************** */ static void PostScript(Item item, PostScriptInfo ps_info) { } /* ********************************************************************************** * * GetClipVertices -- * Get the clipping shape. * Never ever call TRI_FREE on the tristrip returned by GetClipVertices. * ********************************************************************************** */ static ZnBool GetClipVertices(Item item, ZnTriStrip *tristrip) { CurveItem cv = (CurveItem) item; tristrip->num_strips = 0; if (cv->tristrip.num_strips == 1) { TRI_STRIP1(tristrip, cv->tristrip.strips[0].points, cv->tristrip.strips[0].num_points, cv->tristrip.strips[0].fan); } else if (cv->tristrip.num_strips > 1) { tristrip->num_strips = cv->tristrip.num_strips; tristrip->strips = cv->tristrip.strips; } return False; } /* ********************************************************************************** * * GetContours -- * Get the external contour(s). * Never ever call POLY_FREE on the poly returned by GetContours. * ********************************************************************************** */ static ZnBool GetContours(Item item, ZnPoly *poly) { CurveItem cv = (CurveItem) item; if (cv->outlines.num_contours == 1) { POLY_CONTOUR1(poly, cv->outlines.contours[0].points, cv->outlines.contours[0].num_points); } else if (cv->outlines.num_contours > 1) { poly->num_contours = cv->outlines.num_contours; poly->contours = cv->outlines.contours; } return False; } /* ********************************************************************************** * * Coords -- * Return or edit the item vertices. * ********************************************************************************** */ static int Coords(Item item, int contour, int index, int cmd, ZnPoint **pts, char **controls, int *num_pts) { CurveItem cv = (CurveItem) item; int i, num_controls; ZnContour *c=NULL; /*printf("contour %d, num_pts %d, index %d, cmd %d\n", contour, *num_pts, index, cmd);*/ /*printf("nb contours: %d\n", cv->shape.num_contours);*/ if (contour < 0) { contour += cv->shape.num_contours; } if ((contour < 0) || ((cv->shape.num_contours == 0) && (contour != 0)) || ((cv->shape.num_contours != 0) && (contour >= cv->shape.num_contours))) { Tcl_AppendResult(item->wi->interp, " curve contour index out of range", NULL); return ZN_ERROR; } if (cv->shape.num_contours != 0) { c = &cv->shape.contours[contour]; } /* REPLACE */ if ((cmd == COORDS_REPLACE) || (cmd == COORDS_REPLACE_ALL)) { if (cmd == COORDS_REPLACE_ALL) { /* * Replacing all coordinates of an empty curve is legal, resulting * in a curve with one contour. * Replacing all the coordinates of a contour by no coordinates * is also legal, resulting in the contour being removed. */ if ((cv->shape.num_contours == 0) && (*num_pts)) { POLY_CONTOUR1(&cv->shape, NULL, 0); c = &cv->shape.contours[0]; } if (*num_pts) { if (c->points) { ZnFree(c->points); } c->points = (ZnPoint *) ZnMalloc(*num_pts*sizeof(ZnPoint)); c->num_points = *num_pts; memcpy(c->points, *pts, *num_pts*sizeof(ZnPoint)); if (*controls) { c->controls = ZnRealloc(c->controls, *num_pts*sizeof(char)); memcpy(c->controls, *controls, *num_pts*sizeof(char)); } } else { goto cont_remove; } } else { if (cv->shape.num_contours == 0) { Tcl_AppendResult(item->wi->interp, " coords replace command cannot be performed on an empty curve", NULL); return ZN_ERROR; } if (*num_pts == 0) { Tcl_AppendResult(item->wi->interp, " coords replace command need at least 1 point on curves", NULL); return ZN_ERROR; } if (index < 0) { index += c->num_points; } if ((index < 0) || (index >= c->num_points)) { range_err: Tcl_AppendResult(item->wi->interp, " coord index out of range", NULL); return ZN_ERROR; } c->points[index] = (*pts)[0]; if (!c->controls && *controls && (*controls)[0]) { c->controls = ZnMalloc(c->num_points*sizeof(char)); memset(c->controls, 0, c->num_points*sizeof(char)); } if (c->controls) { if (!*controls) { c->controls[index] = 0; } else { if ((*controls)[0]) { /* Check if the edit is allowable, there should be * no more than 2 consecutive control points . */ for (num_controls = 0, i = index-1; !c->controls[i]; i--, num_controls++); for (i = index+1; !c->controls[i]; i++, num_controls++); if (num_controls > 1) { control_err: Tcl_AppendResult(item->wi->interp, " too many consecutive control points in a curve", NULL); return ZN_ERROR; } } c->controls[index] = (*controls)[0]; } } } ITEM.Invalidate(item, ZN_COORDS_FLAG); } /* READ */ else if ((cmd == COORDS_READ) || (cmd == COORDS_READ_ALL)) { if (cv->shape.num_contours == 0) { Tcl_AppendResult(item->wi->interp, " coords read command cannot be performed on an empty curve", NULL); return ZN_ERROR; } if (cmd == COORDS_READ_ALL) { *num_pts = c->num_points; *pts = c->points; if (c->controls) { *controls = c->controls; } } else { if (index < 0) { index += c->num_points; } if ((index < 0) || (index >= c->num_points)) { goto range_err; } *num_pts = 1; *pts = &c->points[index]; if (c->controls) { *controls = &c->controls[index]; } } } /* ADD */ else if ((cmd == COORDS_ADD) || (cmd == COORDS_ADD_LAST)) { if (*num_pts == 0) { return ZN_OK; } /* * Adding to an empty curve is possible. A contour zero is * created. */ if (cv->shape.num_contours == 0) { POLY_CONTOUR1(&cv->shape, NULL, 0); c = &cv->shape.contours[0]; } if (cmd == COORDS_ADD_LAST) { index = c->num_points; } if (index < 0) { index += c->num_points; } if ((index < 0) || (index > c->num_points)) { goto range_err; } if (*controls) { /* Check if the edit is allowable, there should be * no more than 2 consecutive control points . */ if (c->controls) { for (num_controls = 0, i = index-1; !c->controls[i]; i--, num_controls++); } for (i = 0; i < *num_pts; i++) { if (!(*controls)[i]) { num_controls = 0; } else { num_controls++; if (num_controls > 2) { goto control_err; } } } if (c->controls) { for (i = index+1; !c->controls[i]; i++, num_controls++); } if (num_controls > 2) { goto control_err; } } c->points = ZnRealloc(c->points, (c->num_points+*num_pts)*sizeof(ZnPoint)); if (*controls || c->controls) { c->controls = ZnRealloc(c->controls, (c->num_points+*num_pts)*sizeof(char)); } /* * Make a hole if needed. */ for (i = c->num_points-1; i >= index; i--) { c->points[i+*num_pts] = c->points[i]; if (c->controls) { c->controls[i+*num_pts] = c->controls[i]; } } for (i = 0; i < *num_pts; i++, index++) { c->points[index] = (*pts)[i]; if (c->controls) { c->controls[index] = (*controls)?(*controls)[i]:0; } } c->num_points += *num_pts; ITEM.Invalidate(item, ZN_COORDS_FLAG); } /* REMOVE */ else if (cmd == COORDS_REMOVE) { if (cv->shape.num_contours == 0) { Tcl_AppendResult(item->wi->interp, " coords remove command cannot be performed on an empty curve", NULL); return ZN_ERROR; } if (index < 0) { index += c->num_points; } if ((index < 0) || (index >= c->num_points)) { goto range_err; } if (c->controls) { /* Check if the edit is allowable, there should be * no more than 2 consecutive control points . */ for (num_controls = 0, i = index-1; !c->controls[i]; i--, num_controls++); for (i = index+1; !c->controls[i]; i++, num_controls++); if (num_controls > 2) { goto control_err; } } c->num_points--; if (c->num_points == 0) { /* * Contour is removed. */ cont_remove: if (c->controls) { ZnFree(c->controls); } ZnFree(c->points); if (cv->shape.num_contours == 1) { POLY_FREE(&cv->shape); } else { for (i = contour; i < cv->shape.num_contours; i++) { cv->shape.contours[i] = cv->shape.contours[i+1]; } } } else if (index != c->num_points) { for (i = index; i < c->num_points; i++) { c->points[i] = c->points[i+1]; if (c->controls) { c->controls[i] = c->controls[i+1]; } } } ITEM.Invalidate(item, ZN_COORDS_FLAG); } return ZN_OK; } /* ********************************************************************************** * * Contour -- * Perform geometric operations on curve contours. * ********************************************************************************** */ static int Contour(Item item, int cmd, int index, ZnPoly *poly) { CurveItem cv = (CurveItem) item; int i, num_contours; switch (cmd) { case ZN_CONTOUR_ADD: if (index < 0) { index += cv->shape.num_contours; } if (index > cv->shape.num_contours) { index = cv->shape.num_contours; } if (index < 0) { contour_err: Tcl_AppendResult(item->wi->interp, " contour index out of range", NULL); return ZN_ERROR; } num_contours = cv->shape.num_contours + poly->num_contours; if (cv->shape.contours == &cv->shape.contour1) { cv->shape.contours = ZnMalloc(num_contours*sizeof(ZnContour)); cv->shape.contours[0].num_points = cv->shape.contour1.num_points; cv->shape.contours[0].cw = cv->shape.contour1.cw; cv->shape.contours[0].points = cv->shape.contour1.points; cv->shape.contours[0].controls = cv->shape.contour1.controls; } else { cv->shape.contours = ZnRealloc(cv->shape.contours, num_contours*sizeof(ZnContour)); /*printf("Reallocating shape contours (%d) 0x%X\n", num_contours, cv->shape.contours);*/ } /* * Make a hole if needed. */ /*printf("index : %d, i : %d\n", index, cv->shape.num_contours-1);*/ for (i = cv->shape.num_contours-1; i >= index; i--) { cv->shape.contours[i+poly->num_contours] = cv->shape.contours[i]; } for (i = 0; i < poly->num_contours; i++, index++) { cv->shape.contours[index].num_points = poly->contours[i].num_points; cv->shape.contours[index].cw = poly->contours[i].cw; cv->shape.contours[index].points = ZnMalloc(poly->contours[i].num_points * sizeof(ZnPoint)); memcpy(cv->shape.contours[index].points, poly->contours[i].points, poly->contours[i].num_points * sizeof(ZnPoint)); cv->shape.contours[index].controls = NULL; if (poly->contours[i].controls) { /* * The controls array in poly is shared, duplicate it * to keep a locally owned copy. */ cv->shape.contours[index].controls = ZnMalloc(poly->contours[i].num_points * sizeof(char)); memcpy(cv->shape.contours[index].controls, poly->contours[i].controls, poly->contours[i].num_points * sizeof(char)); } } cv->shape.num_contours = num_contours; ITEM.Invalidate(item, ZN_COORDS_FLAG); break; case ZN_CONTOUR_REMOVE: if (index < 0) { index += cv->shape.num_contours; } if (index >= cv->shape.num_contours) { index = cv->shape.num_contours - 1; } if (index < 0) { goto contour_err; } cv->shape.num_contours--; if (cv->shape.num_contours == 0) { POLY_FREE(&cv->shape); } else { ZnFree(cv->shape.contours[index].points); if (cv->shape.contours[index].controls) { ZnFree(cv->shape.contours[index].controls); } for (i = index; i < cv->shape.num_contours; i++) { cv->shape.contours[i] = cv->shape.contours[i+1]; } } ITEM.Invalidate(item, ZN_COORDS_FLAG); break; } return cv->shape.num_contours; } /* ********************************************************************************** * * PickVertex -- * Return in 'vertex' the vertex closest to p and in 'o_vertex' the * opposite vertex on the closest edge, if such an edge exists or -1 * in the other case. * ********************************************************************************** */ static void PickVertex(Item item, ZnPoint *p, int *contour, int *vertex, int *o_vertex) { CurveItem cv = (CurveItem) item; int i, j, k, num_points; ZnPoint *points, po; ZnReal dist=1.0e40, new_dist, dist2; ZnTransfo t, inv; *contour = *vertex = *o_vertex = -1; if ((cv->line_width > 0) || ISSET(cv->flags, FILLED_OK) || ISSET(cv->flags, MARKER_OK)) { /* * Get the point in the item coordinate space. */ ITEM.GetItemTransform(item, &t); ZnTransfoInvert(&t, &inv); ZnTransformPoint(&inv, p, &po); /* * Check all contours. */ for (i = 0; i < cv->shape.num_contours; i++) { points = cv->shape.contours[i].points; num_points = cv->shape.contours[i].num_points; for (j = 0; j < num_points; j++) { new_dist = hypot(points[j].x - po.x, points[j].y - po.y); if (new_dist < dist) { dist = new_dist; *contour = i; *vertex = j; } } /* * If the closest vertex is in the current contour update * the opposite vertex. */ if (i == *contour) { j = (*vertex+1) % num_points; new_dist = LineToPointDist(&points[*vertex], &points[j], &po); k = ((unsigned)(*vertex-1)) % num_points; dist2 = LineToPointDist(&points[*vertex], &points[k], &po); if (dist2 < new_dist) { *o_vertex = k; } else { *o_vertex = j; } } } } } /* ********************************************************************************** * * Exported functions struct -- * ********************************************************************************** */ static ItemClassStruct CURVE_ITEM_CLASS = { sizeof(CurveItemStruct), 0, /* num_parts */ False, /* has_anchors */ "curve", cv_attrs, Init, Clone, Destroy, Configure, Query, NULL, /* GetFieldSet */ NULL, /* GetAnchor */ GetClipVertices, GetContours, Coords, NULL, /* InsertChars */ NULL, /* DeleteChars */ NULL, /* Cursor */ NULL, /* Index */ NULL, /* Part */ NULL, /* Selection */ Contour, ComputeCoordinates, ToArea, Draw, Render, IsSensitive, Pick, PickVertex, /* PickVertex */ PostScript }; ZnItemClassId ZnCurve = (ZnItemClassId) &CURVE_ITEM_CLASS;