Graphviz 13.1.3~dev.20250829.0113
Loading...
Searching...
No Matches
draw.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11/*
12
13XDOT DRAWING FUNCTIONS, maybe need to move them somewhere else
14 for now keep them at the bottom
15*/
16#include "draw.h"
17#include <common/colorprocs.h>
18#include "smyrna_utils.h"
19#include <glcomp/glutils.h>
20#include <math.h>
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdlib.h>
24#include <string.h>
25#include <util/unreachable.h>
26#include <util/list.h>
27#include <util/xml.h>
28
29#include <xdot/xdot.h>
30#include "viewport.h"
31#include "topfisheyeview.h"
32#include "gui/appmouse.h"
33#include "hotkeymap.h"
34#include "polytess.h"
35#include <glcomp/glcompimage.h>
36
37
38//delta values
39static float dx = 0.0;
40static float dy = 0.0;
41#define LAYER_DIFF 0.001
42
43static void DrawBezier(xdot_point* pts, int filled, int param)
44{
45 /*copied from NEHE */
46 /*Written by: David Nikdel ( ogapo@ithink.net ) */
47 double Ax = pts[0].x;
48 double Ay = pts[0].y;
49 double Az = pts[0].z;
50 double Bx = pts[1].x;
51 double By = pts[1].y;
52 double Bz = pts[1].z;
53 double Cx = pts[2].x;
54 double Cy = pts[2].y;
55 double Cz = pts[2].z;
56 double Dx = pts[3].x;
57 double Dy = pts[3].y;
58 double Dz = pts[3].z;
59 double X;
60 double Y;
61 double Z;
62 int i = 0; //loop index
63 // Variable
64 double a = 1.0;
65 double b = 1.0 - a;
66 /* Tell OGL to start drawing a line strip */
67 glLineWidth(view->LineWidth);
68 if (!filled) {
69
70 if (param == 0)
71 glColor4d(view->penColor.R, view->penColor.G, view->penColor.B,
72 view->penColor.A);
73 else if (param == 1) //selected
77 glBegin(GL_LINE_STRIP);
78 } else {
79 if (param == 0)
80 glColor4d(view->fillColor.R, view->fillColor.G,
82 else if (param == 1) //selected
86 glBegin(GL_POLYGON);
87 }
88 /* We will not actually draw a curve, but we will divide the curve into small
89 points and draw a line between each point. If the points are close enough, it
90 will appear as a curved line. 20 points are plenty, and since the variable goes
91 from 1.0 to 0.0 we must change it by 1/20 = 0.05 each time */
92 for (i = 0; i <= 20; i++) {
93 // Get a point on the curve
94 X = Ax * a * a * a + Bx * 3 * a * a * b + Cx * 3 * a * b * b +
95 Dx * b * b * b;
96 Y = Ay * a * a * a + By * 3 * a * a * b + Cy * 3 * a * b * b +
97 Dy * b * b * b;
98 Z = Az * a * a * a + Bz * 3 * a * a * b + Cz * 3 * a * b * b +
99 Dz * b * b * b;
100 // Draw the line from point to point (assuming OGL is set up properly)
101 glVertex3d(X, Y, Z + view->Topview->global_z);
102 // Change the variable
103 a -= 0.05;
104 b = 1.0 - a;
105 }
106// Tell OGL to stop drawing the line strip
107 glEnd();
108}
109
110static void set_options(int param)
111{
112
113 int a=get_mode(view);
114 if (param == 1 && a == 10 && view->mouse.down) // selected, if there is move, move it
115 {
118 } else {
119 dx = 0;
120 dy = 0;
121 }
122
123}
124
125static void DrawBeziers(xdot_op *op, int param) {
126 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
127 xdot_point* ps = op->u.bezier.pts;
129
130 const int filled = op->kind == xd_filled_bezier;
131
132 for (size_t i = 1; i < op->u.bezier.cnt; i += 3) {
133 DrawBezier(ps, filled, param);
134 ps += 3;
135 }
136}
137
138//Draws an ellipse made out of points.
139static void DrawEllipse(xdot_op *op, int param) {
140 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
141 int filled;
143 set_options(param);
144 double x = op->u.ellipse.x - dx;
145 double y = op->u.ellipse.y - dy;
146 double xradius = op->u.ellipse.w;
147 double yradius = op->u.ellipse.h;
148 if (op->kind == xd_filled_ellipse) {
149 if (param == 0)
150 glColor4d(view->fillColor.R, view->fillColor.G,
152 if (param == 1) //selected
156
157 filled = 1;
158 } else {
159 if (param == 0)
160 glColor4d(view->penColor.R, view->penColor.G, view->penColor.B,
161 view->penColor.A);
162 if (param == 1) //selected
166
167 filled = 0;
168 }
169
170 if (!filled)
171 glBegin(GL_LINE_LOOP);
172 else
173 glBegin(GL_POLYGON);
174 for (int i = 0; i < 360; ++i) {
175 //convert degrees into radians
176 const double degInRad = i * DEG2RAD;
177 glVertex3d(x + cos(degInRad) * xradius,
178 y + sin(degInRad) * yradius, view->Topview->global_z);
179 }
180 glEnd();
181}
182
183static void DrawPolygon(xdot_op *op, int param) {
184 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
186
187 set_options(param);
188
189 if (op->kind == xd_filled_polygon) {
190 if (param == 0)
191 glColor4d(view->fillColor.R, view->fillColor.G,
193 if (param == 1) //selected
197 } else {
198 if (param == 0)
199 glColor4d(view->penColor.R, view->penColor.G, view->penColor.B,
200 view->penColor.A);
201 if (param == 1) //selected
205
206 }
207 glLineWidth(view->LineWidth);
209}
210
211static void DrawPolyline(xdot_op *op, int param) {
212 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
214
215 if (param == 0)
216 glColor4d(view->penColor.R, view->penColor.G, view->penColor.B,
217 view->penColor.A);
218 if (param == 1) //selected
221 set_options(param);
222 glLineWidth(view->LineWidth);
223 glBegin(GL_LINE_STRIP);
224 for (size_t i = 0; i < op->u.polyline.cnt; ++i) {
225 glVertex3d(op->u.polyline.pts[i].x - dx,
226 op->u.polyline.pts[i].y - dy,
227 op->u.polyline.pts[i].z + view->Topview->global_z);
228 }
229 glEnd();
230}
231
232static glCompColor GetglCompColor(const char *color) {
233 gvcolor_t cl;
234 glCompColor c;
235 if (color != NULL) {
237 c.R = cl.u.RGBA[0];
238 c.G = cl.u.RGBA[1];
239 c.B = cl.u.RGBA[2];
240 c.A = cl.u.RGBA[3];
241 } else {
242 c = view->penColor;
243 }
244 return c;
245}
246
247static void SetFillColor(xdot_op *op, int param) {
248 (void)param;
250}
251
252static void SetPenColor(xdot_op *op, int param) {
253 (void)param;
255}
256
258
259static void SetFont(xdot_op *op, int param) {
260 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
261 (void)param;
262
263 font_op=o;
264}
265
266/*for now we only support png files in 2d space, no image rotation*/
267static void InsertImage(xdot_op *op, int param) {
268 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
269 (void)param;
270
271 if(!o->obj)
272 return;
273
274
275 if(!o->img) {
276 const float x = o->op.u.image.pos.x;
277 const float y = o->op.u.image.pos.y;
278 glCompImage *const i = o->img = glCompImageNewFile(x, y, o->op.u.image.name);
279 if (!o->img) {
280 fprintf (stderr, "Could not open file \"%s\" to read image.\n", o->op.u.image.name);
281 return;
282 }
283 i->width = o->op.u.image.pos.w;
284 i->height = o->op.u.image.pos.h;
286 }
287}
288
289// see usage in EmbedText
290static int put(void *buffer, const char *s) {
291 char **b = buffer;
292
293 const size_t len = strlen(s);
294 memcpy(*b, s, len);
295 *b += len;
296
297 return 0;
298}
299
300static void EmbedText(xdot_op *op, int param) {
301 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
302 (void)param;
303
304 float x, y;
306 view->Topview->global_z += o->layer * LAYER_DIFF + 0.05;
307 switch (o->op.u.text.align)
308 {
309 case xd_left:
310 x=o->op.u.text.x ;
311 break;
312 case xd_center:
313 x=o->op.u.text.x - o->op.u.text.width / 2.0;
314 break;
315 case xd_right:
316 x=o->op.u.text.x - o->op.u.text.width;
317 break;
318 default:
319 UNREACHABLE();
320 }
321 y=o->op.u.text.y;
322 if (o->font.fontdesc == NULL) {
323 // allocate a buffer large enough to hold the maximum escaped version of the
324 // text
325 char *escaped = calloc(sizeof(char), strlen(o->op.u.text.text) *
326 sizeof("&#xFFFFFFFF;") + 1);
327 if (escaped == NULL)
328 return;
329
330 // XML-escape the text
331 const xml_flags_t flags = {.dash = 1, .nbsp = 1};
332 char *ptr = escaped;
333 (void)gv_xml_escape(o->op.u.text.text, flags, put, &ptr);
334
335 o->font = glNewFont(view->widgets, escaped, &view->penColor,
337 false);
338
339 free(escaped);
340 }
342 font_op->op.u.font.size);
343}
344
346{
347 if (vi->bdVisible) {
348 glColor4d(vi->borderColor.R, vi->borderColor.G,
349 vi->borderColor.B, vi->borderColor.A);
350 glLineWidth(2);
351 glBegin(GL_LINE_STRIP);
352 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
353 glVertex3d(vi->bdxRight, vi->bdyBottom,-0.001);
354 glVertex3d(vi->bdxRight, vi->bdyTop,-0.001);
355 glVertex3d(vi->bdxLeft, vi->bdyTop,-0.001);
356 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
357 glEnd();
358 glLineWidth(1);
359 }
360}
361
362void drawCircle(double x, double y, double radius, double zdepth) {
363 if (radius < 0.3)
364 radius = 0.4;
365 glBegin(GL_POLYGON);
366 for (int i = 0; i < 360; i += 36) {
367 const double degInRad = i * DEG2RAD;
368 glVertex3d(x + cos(degInRad) * radius, y + sin(degInRad) * radius,
369 zdepth + view->Topview->global_z);
370 }
371
372 glEnd();
373}
374
389
390void draw_selpoly(glCompPoly_t *selPoly) {
391 glColor4f(1,0,0,1);
392 glBegin(GL_LINE_STRIP);
393 for (size_t i = 0; i < LIST_SIZE(selPoly); ++i) {
394 const glCompPoint pt = LIST_GET(selPoly, i);
395 glVertex3f(pt.x, pt.y, pt.z);
396 }
397 glEnd();
398 if (!LIST_IS_EMPTY(selPoly)) {
399 const glCompPoint last = *LIST_BACK(selPoly);
400 glBegin(GL_LINE_STRIP);
401 glVertex3f(last.x, last.y, last.z);
402 glVertex3f(view->mouse.GLpos.x,view->mouse.GLpos.y,0);
403 glEnd();
404 }
405}
static agxbuf last
last message
Definition agerror.c:29
@ RGBA_DOUBLE
Definition color.h:27
void colorxlate(char *str, agxbuf *buf)
Definition colxlate.c:46
static sdot_op * font_op
Definition draw.c:257
static void SetFont(xdot_op *op, int param)
Definition draw.c:259
void drawCircle(double x, double y, double radius, double zdepth)
Definition draw.c:362
void draw_selpoly(glCompPoly_t *selPoly)
Definition draw.c:390
static int put(void *buffer, const char *s)
Definition draw.c:290
static void DrawBezier(xdot_point *pts, int filled, int param)
Definition draw.c:43
static void EmbedText(xdot_op *op, int param)
Definition draw.c:300
static void DrawEllipse(xdot_op *op, int param)
Definition draw.c:139
static float dy
Definition draw.c:40
void drawBorders(ViewInfo *vi)
Definition draw.c:345
#define LAYER_DIFF
Definition draw.c:41
static void SetPenColor(xdot_op *op, int param)
Definition draw.c:252
static float dx
Definition draw.c:39
static glCompColor GetglCompColor(const char *color)
Definition draw.c:232
static void DrawPolyline(xdot_op *op, int param)
Definition draw.c:211
static void InsertImage(xdot_op *op, int param)
Definition draw.c:267
drawfunc_t OpFns[]
Definition draw.c:375
static void set_options(int param)
Definition draw.c:110
static void DrawBeziers(xdot_op *op, int param)
Definition draw.c:125
static void DrawPolygon(xdot_op *op, int param)
Definition draw.c:183
static void SetFillColor(xdot_op *op, int param)
Definition draw.c:247
static int flags
Definition gc.c:61
#define Y(i)
Definition gdefs.h:3
#define X(prefix, name, str, type, subtype,...)
Definition gdefs.h:14
void glCompDrawText3D(glCompFont f, float x, float y, double z, float w, float h)
Definition glcompfont.c:109
glCompFont glNewFont(glCompSet *s, const char *text, glCompColor *c, char *fontdesc, int fs, bool is2D)
Definition glcompfont.c:46
glCompImage * glCompImageNewFile(float x, float y, const char *imgfile)
Definition glcompimage.c:33
static double len(glCompPoint p)
Definition glutils.c:136
void free(void *)
node NULL
Definition grammar.y:181
static void color(Agraph_t *g)
Definition gvcolor.c:129
int get_mode(ViewInfo *v)
Definition hotkeymap.c:166
type-generic dynamically expanding list
#define LIST_BACK(list)
Definition list.h:201
#define LIST_SIZE(list)
Definition list.h:80
#define LIST_IS_EMPTY(list)
Definition list.h:90
#define LIST_GET(list, index)
Definition list.h:165
static int * ps
Definition lu.c:51
void drawTessPolygon(sdot_op *p)
Definition polytess.c:109
ViewInfo * view
Definition viewport.c:37
#define DEG2RAD
Definition smyrnadefs.h:54
topview * Topview
Definition smyrnadefs.h:307
double bdxRight
Definition smyrnadefs.h:285
float LineWidth
Definition smyrnadefs.h:273
double bdyBottom
Definition smyrnadefs.h:285
double bdyTop
Definition smyrnadefs.h:284
glCompSet * widgets
Definition smyrnadefs.h:326
glCompColor fillColor
Definition smyrnadefs.h:262
glCompColor borderColor
Definition smyrnadefs.h:266
glCompColor selectedNodeColor
Definition smyrnadefs.h:268
double bdxLeft
Definition smyrnadefs.h:284
int bdVisible
Definition smyrnadefs.h:281
glCompMouse mouse
Definition smyrnadefs.h:297
glCompColor penColor
Definition smyrnadefs.h:260
xdot_font font
Definition xdot.h:157
xdot_rect ellipse
Definition xdot.h:149
xdot_image image
Definition xdot.h:154
union _xdot_op::@137 u
char * color
Definition xdot.h:155
xdot_kind kind
Definition xdot.h:147
xdot_text text
Definition xdot.h:153
xdot_polyline bezier
Definition xdot.h:152
xdot_polyline polyline
Definition xdot.h:151
double RGBA[4]
Definition color.h:32
union color_s::@74 u
glcompdrawfunc_t draw
Definition glcompdefs.h:150
glCompCallBacks functions
Definition glcompdefs.h:179
char * fontdesc
Definition glcompdefs.h:139
glCompObj base
Definition glcompdefs.h:190
glCompPoint GLfinalPos
Definition glcompdefs.h:227
glCompPoint GLpos
Definition glcompdefs.h:225
glCompPoint GLinitPos
Definition glcompdefs.h:226
glCompCommon common
Definition glcompdefs.h:185
xdot_op op
Definition smyrnadefs.h:84
glCompImage * img
Definition smyrnadefs.h:88
void * obj
Definition smyrnadefs.h:85
int layer
Definition smyrnadefs.h:87
glCompFont font
Definition smyrnadefs.h:86
double global_z
Definition smyrnadefs.h:233
double size
Definition xdot.h:104
char * name
Definition xdot.h:105
char * name
Definition xdot.h:100
xdot_rect pos
Definition xdot.h:99
double x
Definition xdot.h:79
double z
Definition xdot.h:79
double y
Definition xdot.h:79
size_t cnt
Definition xdot.h:87
xdot_point * pts
Definition xdot.h:88
double x
Definition xdot.h:83
double w
Definition xdot.h:83
double y
Definition xdot.h:83
double h
Definition xdot.h:83
double width
Definition xdot.h:94
char * text
Definition xdot.h:95
double x
Definition xdot.h:92
xdot_align align
Definition xdot.h:93
double y
Definition xdot.h:92
options to tweak the behavior of XML escaping
Definition xml.h:13
unsigned dash
escape '-'
Definition xml.h:17
Definition grammar.c:90
#define UNREACHABLE()
Definition unreachable.h:30
parsing and deparsing of xdot operations
@ xop_polygon
Definition xdot.h:129
@ xop_font
Definition xdot.h:135
@ xop_image
Definition xdot.h:137
@ xop_bezier
Definition xdot.h:130
@ xop_fill_color
Definition xdot.h:133
@ xop_text
Definition xdot.h:132
@ xop_fontchar
Definition xdot.h:139
@ xop_style
Definition xdot.h:136
@ xop_grad_color
Definition xdot.h:138
@ xop_pen_color
Definition xdot.h:134
@ xop_ellipse
Definition xdot.h:128
@ xop_polyline
Definition xdot.h:131
@ xd_left
Definition xdot.h:76
@ xd_right
Definition xdot.h:76
@ xd_center
Definition xdot.h:76
void(* drawfunc_t)(xdot_op *, int)
Definition xdot.h:143
@ xd_filled_polygon
Definition xdot.h:111
@ xd_filled_ellipse
Definition xdot.h:109
@ xd_filled_bezier
Definition xdot.h:113
int gv_xml_escape(const char *s, xml_flags_t flags, int(*cb)(void *state, const char *s), void *state)
Definition xml.c:181
XML escaping functionality.