Graphviz 13.0.0~dev.20250607.1528
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 <util/unreachable.h>
25#include <util/xml.h>
26
27#include <xdot/xdot.h>
28#include "viewport.h"
29#include "topfisheyeview.h"
30#include "appmouse.h"
31#include "hotkeymap.h"
32#include "polytess.h"
33#include <glcomp/glcompimage.h>
34
35
36//delta values
37static float dx = 0.0;
38static float dy = 0.0;
39#define LAYER_DIFF 0.001
40
41static void DrawBezier(xdot_point* pts, int filled, int param)
42{
43 /*copied from NEHE */
44 /*Written by: David Nikdel ( ogapo@ithink.net ) */
45 double Ax = pts[0].x;
46 double Ay = pts[0].y;
47 double Az = pts[0].z;
48 double Bx = pts[1].x;
49 double By = pts[1].y;
50 double Bz = pts[1].z;
51 double Cx = pts[2].x;
52 double Cy = pts[2].y;
53 double Cz = pts[2].z;
54 double Dx = pts[3].x;
55 double Dy = pts[3].y;
56 double Dz = pts[3].z;
57 double X;
58 double Y;
59 double Z;
60 int i = 0; //loop index
61 // Variable
62 double a = 1.0;
63 double b = 1.0 - a;
64 /* Tell OGL to start drawing a line strip */
65 glLineWidth(view->LineWidth);
66 if (!filled) {
67
68 if (param == 0)
69 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
70 view->penColor.A);
71 else if (param == 1) //selected
75 glBegin(GL_LINE_STRIP);
76 } else {
77 if (param == 0)
78 glColor4f(view->fillColor.R, view->fillColor.G,
80 else if (param == 1) //selected
84 glBegin(GL_POLYGON);
85 }
86 /* We will not actually draw a curve, but we will divide the curve into small
87 points and draw a line between each point. If the points are close enough, it
88 will appear as a curved line. 20 points are plenty, and since the variable goes
89 from 1.0 to 0.0 we must change it by 1/20 = 0.05 each time */
90 for (i = 0; i <= 20; i++) {
91 // Get a point on the curve
92 X = Ax * a * a * a + Bx * 3 * a * a * b + Cx * 3 * a * b * b +
93 Dx * b * b * b;
94 Y = Ay * a * a * a + By * 3 * a * a * b + Cy * 3 * a * b * b +
95 Dy * b * b * b;
96 Z = Az * a * a * a + Bz * 3 * a * a * b + Cz * 3 * a * b * b +
97 Dz * b * b * b;
98 // Draw the line from point to point (assuming OGL is set up properly)
99 glVertex3d(X, Y, Z + view->Topview->global_z);
100 // Change the variable
101 a -= 0.05;
102 b = 1.0 - a;
103 }
104// Tell OGL to stop drawing the line strip
105 glEnd();
106}
107
108static void set_options(int param)
109{
110
111 int a=get_mode(view);
112 if (param == 1 && a == 10 && view->mouse.down) // selected, if there is move, move it
113 {
116 } else {
117 dx = 0;
118 dy = 0;
119 }
120
121}
122
123static void DrawBeziers(xdot_op *op, int param) {
124 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
125 xdot_point* ps = op->u.bezier.pts;
127
128 const int filled = op->kind == xd_filled_bezier;
129
130 for (size_t i = 1; i < op->u.bezier.cnt; i += 3) {
131 DrawBezier(ps, filled, param);
132 ps += 3;
133 }
134}
135
136//Draws an ellipse made out of points.
137static void DrawEllipse(xdot_op *op, int param) {
138 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
139 int filled;
141 set_options(param);
142 double x = op->u.ellipse.x - dx;
143 double y = op->u.ellipse.y - dy;
144 double xradius = op->u.ellipse.w;
145 double yradius = op->u.ellipse.h;
146 if (op->kind == xd_filled_ellipse) {
147 if (param == 0)
148 glColor4f(view->fillColor.R, view->fillColor.G,
150 if (param == 1) //selected
154
155 filled = 1;
156 } else {
157 if (param == 0)
158 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
159 view->penColor.A);
160 if (param == 1) //selected
164
165 filled = 0;
166 }
167
168 if (!filled)
169 glBegin(GL_LINE_LOOP);
170 else
171 glBegin(GL_POLYGON);
172 for (int i = 0; i < 360; ++i) {
173 //convert degrees into radians
174 float degInRad = (float) (i * DEG2RAD);
175 glVertex3f((float)(x + cos(degInRad) * xradius),
176 (float)(y + sin(degInRad) * yradius), (float)view->Topview->global_z);
177 }
178 glEnd();
179}
180
181static void DrawPolygon(xdot_op *op, int param) {
182 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
184
185 set_options(param);
186
187 if (op->kind == xd_filled_polygon) {
188 if (param == 0)
189 glColor4f(view->fillColor.R, view->fillColor.G,
191 if (param == 1) //selected
195 } else {
196 if (param == 0)
197 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
198 view->penColor.A);
199 if (param == 1) //selected
203
204 }
205 glLineWidth(view->LineWidth);
207}
208
209static void DrawPolyline(xdot_op *op, int param) {
210 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
212
213 if (param == 0)
214 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
215 view->penColor.A);
216 if (param == 1) //selected
219 set_options(param);
220 glLineWidth(view->LineWidth);
221 glBegin(GL_LINE_STRIP);
222 for (size_t i = 0; i < op->u.polyline.cnt; ++i) {
223 glVertex3f((float)op->u.polyline.pts[i].x - dx,
224 (float)op->u.polyline.pts[i].y - dy,
225 (float)(op->u.polyline.pts[i].z + view->Topview->global_z));
226 }
227 glEnd();
228}
229
230static glCompColor GetglCompColor(const char *color) {
231 gvcolor_t cl;
232 glCompColor c;
233 if (color != NULL) {
235 c.R = (float) cl.u.RGBA[0];
236 c.G = (float) cl.u.RGBA[1];
237 c.B = (float) cl.u.RGBA[2];
238 c.A = (float) cl.u.RGBA[3];
239 } else {
240 c = view->penColor;
241 }
242 return c;
243}
244
245static void SetFillColor(xdot_op *op, int param) {
246 (void)param;
248}
249
250static void SetPenColor(xdot_op *op, int param) {
251 (void)param;
253}
254
256
257static void SetFont(xdot_op *op, int param) {
258 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
259 (void)param;
260
261 font_op=o;
262}
263
264/*for now we only support png files in 2d space, no image rotation*/
265static void InsertImage(xdot_op *op, int param) {
266 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
267 (void)param;
268
269 if(!o->obj)
270 return;
271
272
273 if(!o->img) {
274 const float x = o->op.u.image.pos.x;
275 const float y = o->op.u.image.pos.y;
276 glCompImage *const i = o->img = glCompImageNewFile(x, y, o->op.u.image.name);
277 if (!o->img) {
278 fprintf (stderr, "Could not open file \"%s\" to read image.\n", o->op.u.image.name);
279 return;
280 }
281 i->width = o->op.u.image.pos.w;
282 i->height = o->op.u.image.pos.h;
284 }
285}
286
287// see usage in EmbedText
288static int put(void *buffer, const char *s) {
289 char **b = buffer;
290
291 for (; *s != '\0'; ++s) {
292 **b = *s;
293 ++(*b);
294 }
295
296 return 0;
297}
298
299static void EmbedText(xdot_op *op, int param) {
300 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
301 (void)param;
302
303 float x, y;
305 view->Topview->global_z += o->layer * LAYER_DIFF + 0.05;
306 switch (o->op.u.text.align)
307 {
308 case xd_left:
309 x=o->op.u.text.x ;
310 break;
311 case xd_center:
312 x=o->op.u.text.x - o->op.u.text.width / 2.0;
313 break;
314 case xd_right:
315 x=o->op.u.text.x - o->op.u.text.width;
316 break;
317 default:
318 UNREACHABLE();
319 }
320 y=o->op.u.text.y;
321 if (o->font.fontdesc == NULL) {
322 // allocate a buffer large enough to hold the maximum escaped version of the
323 // text
324 char *escaped = calloc(sizeof(char), strlen(o->op.u.text.text) *
325 sizeof("&#xFFFFFFFF;") + 1);
326 if (escaped == NULL)
327 return;
328
329 // XML-escape the text
330 const xml_flags_t flags = {.dash = 1, .nbsp = 1};
331 char *ptr = escaped;
332 (void)gv_xml_escape(o->op.u.text.text, flags, put, &ptr);
333
334 o->font = glNewFont(view->widgets, escaped, &view->penColor,
336 false);
337
338 free(escaped);
339 }
341 font_op->op.u.font.size);
342}
343
345{
346 if (vi->bdVisible) {
347 glColor4f(vi->borderColor.R, vi->borderColor.G,
348 vi->borderColor.B, vi->borderColor.A);
349 glLineWidth(2);
350 glBegin(GL_LINE_STRIP);
351 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
352 glVertex3d(vi->bdxRight, vi->bdyBottom,-0.001);
353 glVertex3d(vi->bdxRight, vi->bdyTop,-0.001);
354 glVertex3d(vi->bdxLeft, vi->bdyTop,-0.001);
355 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
356 glEnd();
357 glLineWidth(1);
358 }
359}
360
361void drawCircle(float x, float y, float radius, float zdepth)
362{
363 if (radius < 0.3)
364 radius = 0.4f;
365 glBegin(GL_POLYGON);
366 for (int i = 0; i < 360; i += 36) {
367 float degInRad = (float) (i * DEG2RAD);
368 glVertex3f(x + cosf(degInRad) * radius, y + sinf(degInRad) * radius,
369 (float)(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 < glCompPoly_size(selPoly); ++i) {
394 const glCompPoint pt = glCompPoly_get(selPoly, i);
395 glVertex3f(pt.x, pt.y, pt.z);
396 }
397 glEnd();
398 if (!glCompPoly_is_empty(selPoly)) {
399 const glCompPoint last = *glCompPoly_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:255
static void SetFont(xdot_op *op, int param)
Definition draw.c:257
void draw_selpoly(glCompPoly_t *selPoly)
Definition draw.c:390
static int put(void *buffer, const char *s)
Definition draw.c:288
static void DrawBezier(xdot_point *pts, int filled, int param)
Definition draw.c:41
static void EmbedText(xdot_op *op, int param)
Definition draw.c:299
static void DrawEllipse(xdot_op *op, int param)
Definition draw.c:137
static float dy
Definition draw.c:38
void drawBorders(ViewInfo *vi)
Definition draw.c:344
#define LAYER_DIFF
Definition draw.c:39
static void SetPenColor(xdot_op *op, int param)
Definition draw.c:250
static float dx
Definition draw.c:37
static glCompColor GetglCompColor(const char *color)
Definition draw.c:230
static void DrawPolyline(xdot_op *op, int param)
Definition draw.c:209
void drawCircle(float x, float y, float radius, float zdepth)
Definition draw.c:361
static void InsertImage(xdot_op *op, int param)
Definition draw.c:265
drawfunc_t OpFns[]
Definition draw.c:375
static void set_options(int param)
Definition draw.c:108
static void DrawBeziers(xdot_op *op, int param)
Definition draw.c:123
static void DrawPolygon(xdot_op *op, int param)
Definition draw.c:181
static void SetFillColor(xdot_op *op, int param)
Definition draw.c:245
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
void free(void *)
node NULL
Definition grammar.y:180
static void color(Agraph_t *g)
Definition gvcolor.c:129
int get_mode(ViewInfo *v)
Definition hotkeymap.c:166
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:56
float bdxLeft
Definition smyrnadefs.h:289
topview * Topview
Definition smyrnadefs.h:312
float LineWidth
Definition smyrnadefs.h:278
float bdyTop
Definition smyrnadefs.h:289
glCompSet * widgets
Definition smyrnadefs.h:335
glCompColor fillColor
Definition smyrnadefs.h:267
glCompColor borderColor
Definition smyrnadefs.h:271
glCompColor selectedNodeColor
Definition smyrnadefs.h:273
float bdxRight
Definition smyrnadefs.h:290
float bdyBottom
Definition smyrnadefs.h:290
int bdVisible
Definition smyrnadefs.h:286
glCompMouse mouse
Definition smyrnadefs.h:302
glCompColor penColor
Definition smyrnadefs.h:265
union _xdot_op::@131 u
xdot_font font
Definition xdot.h:157
xdot_rect ellipse
Definition xdot.h:149
xdot_image image
Definition xdot.h:154
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:86
glCompImage * img
Definition smyrnadefs.h:90
void * obj
Definition smyrnadefs.h:87
int layer
Definition smyrnadefs.h:89
glCompFont font
Definition smyrnadefs.h:88
double global_z
Definition smyrnadefs.h:238
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:30
unsigned dash
escape '-'
Definition xml.h:34
Definition grammar.c:89
#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.