Graphviz 13.0.0~dev.20241220.2304
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 <common/types.h>
19#include <common/utils.h>
20#include "smyrna_utils.h"
21#include <glcomp/glutils.h>
22#include <math.h>
23#include <stdbool.h>
24#include <stdlib.h>
25#include <util/unreachable.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 == 1)) //selected, if there is move, move it
113 {
116 } else {
117 dx = 0;
118 dy = 0;
119 }
120
121}
122
123static void DrawBeziers(sdot_op* o, int param)
124{
125 int filled;
126 xdot_op * op=&o->op;
127 xdot_point* ps = op->u.bezier.pts;
129
130 if (op->kind == xd_filled_bezier)
131 filled = 1;
132 else
133 filled = 0;
134
135 for (size_t i = 1; i < op->u.bezier.cnt; i += 3) {
136 DrawBezier(ps, filled, param);
137 ps += 3;
138 }
139}
140
141//Draws an ellipse made out of points.
142static void DrawEllipse(sdot_op* o, int param)
143{
144 int i = 0;
145 int filled;
146 xdot_op * op=&o->op;
148 set_options(param);
149 double x = op->u.ellipse.x - dx;
150 double y = op->u.ellipse.y - dy;
151 double xradius = op->u.ellipse.w;
152 double yradius = op->u.ellipse.h;
153 if (op->kind == xd_filled_ellipse) {
154 if (param == 0)
155 glColor4f(view->fillColor.R, view->fillColor.G,
157 if (param == 1) //selected
161
162 filled = 1;
163 } else {
164 if (param == 0)
165 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
166 view->penColor.A);
167 if (param == 1) //selected
171
172 filled = 0;
173 }
174
175 if (!filled)
176 glBegin(GL_LINE_LOOP);
177 else
178 glBegin(GL_POLYGON);
179 for (i = 0; i < 360; i = i + 1) {
180 //convert degrees into radians
181 float degInRad = (float) (i * DEG2RAD);
182 glVertex3f((float)(x + cos(degInRad) * xradius),
183 (float)(y + sin(degInRad) * yradius), (float)view->Topview->global_z);
184 }
185 glEnd();
186}
187
188static void DrawPolygon(sdot_op * o, int param)
189{
190 xdot_op * op=&o->op;
192
193 set_options(param);
194
195 if (op->kind == xd_filled_polygon) {
196 if (param == 0)
197 glColor4f(view->fillColor.R, view->fillColor.G,
199 if (param == 1) //selected
203 } else {
204 if (param == 0)
205 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
206 view->penColor.A);
207 if (param == 1) //selected
211
212 }
213 glLineWidth(view->LineWidth);
215}
216
217
218static void DrawPolyline(sdot_op* o, int param)
219{
220 xdot_op * op=&o->op;
222
223 if (param == 0)
224 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
225 view->penColor.A);
226 if (param == 1) //selected
229 set_options(param);
230 glLineWidth(view->LineWidth);
231 glBegin(GL_LINE_STRIP);
232 for (size_t i = 0; i < op->u.polyline.cnt; ++i) {
233 glVertex3f((float)op->u.polyline.pts[i].x - dx,
234 (float)op->u.polyline.pts[i].y - dy,
235 (float)(op->u.polyline.pts[i].z + view->Topview->global_z));
236 }
237 glEnd();
238}
239
240static glCompColor GetglCompColor(const char *color) {
241 gvcolor_t cl;
242 glCompColor c;
243 if (color != NULL) {
245 c.R = (float) cl.u.RGBA[0];
246 c.G = (float) cl.u.RGBA[1];
247 c.B = (float) cl.u.RGBA[2];
248 c.A = (float) cl.u.RGBA[3];
249 } else {
250 c = view->penColor;
251 }
252 return c;
253}
254static void SetFillColor(sdot_op* o, int param)
255{
256 (void)param;
257
258 xdot_op * op=&o->op;
260}
261static void SetPenColor(sdot_op* o, int param)
262{
263 (void)param;
264
265 xdot_op * op=&o->op;
267}
268
269static void SetStyle(sdot_op* o, int param)
270{
271 (void)o;
272 (void)param;
273}
274
276
277static void SetFont(sdot_op * o, int param)
278{
279 (void)param;
280
281 font_op=o;
282}
283
284/*for now we only support png files in 2d space, no image rotation*/
285static void InsertImage(sdot_op * o, int param)
286{
287 (void)param;
288
289 float x,y;
290 glCompImage *i;
291
292 if(!o->obj)
293 return;
294
295
296 if(!o->img) {
297 x = o->op.u.image.pos.x;
298 y = o->op.u.image.pos.y;
299 i = o->img = glCompImageNewFile(x, y, o->op.u.image.name);
300 if (!o->img) {
301 fprintf (stderr, "Could not open file \"%s\" to read image.\n", o->op.u.image.name);
302 return;
303 }
304 i->width = o->op.u.image.pos.w;
305 i->height = o->op.u.image.pos.h;
307 }
308}
309
310// see usage in EmbedText
311static int put(void *buffer, const char *s) {
312 char **b = buffer;
313
314 for (; *s != '\0'; ++s) {
315 **b = *s;
316 ++(*b);
317 }
318
319 return 0;
320}
321
322static void EmbedText(sdot_op* o, int param)
323{
324 (void)param;
325
326 float x, y;
328 view->Topview->global_z += o->layer * LAYER_DIFF + 0.05;
329 switch (o->op.u.text.align)
330 {
331 case xd_left:
332 x=o->op.u.text.x ;
333 break;
334 case xd_center:
335 x=o->op.u.text.x - o->op.u.text.width / 2.0;
336 break;
337 case xd_right:
338 x=o->op.u.text.x - o->op.u.text.width;
339 break;
340 default:
341 UNREACHABLE();
342 }
343 y=o->op.u.text.y;
344 if (o->font.fontdesc == NULL) {
345 // allocate a buffer large enough to hold the maximum escaped version of the
346 // text
347 char *escaped = calloc(sizeof(char), strlen(o->op.u.text.text) *
348 sizeof("&#xFFFFFFFF;") + 1);
349 if (escaped == NULL)
350 return;
351
352 // XML-escape the text
353 const xml_flags_t flags = {.dash = 1, .nbsp = 1};
354 char **ptr = &escaped;
355 (void)xml_escape(o->op.u.text.text, flags, put, ptr);
356
357 o->font = glNewFont(view->widgets, escaped, &view->penColor,
359 false);
360
361 free(escaped);
362 }
364 font_op->op.u.font.size);
365}
366
368{
369 if (vi->bdVisible) {
370 glColor4f(vi->borderColor.R, vi->borderColor.G,
371 vi->borderColor.B, vi->borderColor.A);
372 glLineWidth(2);
373 glBegin(GL_LINE_STRIP);
374 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
375 glVertex3d(vi->bdxRight, vi->bdyBottom,-0.001);
376 glVertex3d(vi->bdxRight, vi->bdyTop,-0.001);
377 glVertex3d(vi->bdxLeft, vi->bdyTop,-0.001);
378 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
379 glEnd();
380 glLineWidth(1);
381 }
382}
383
384void drawCircle(float x, float y, float radius, float zdepth)
385{
386 int i;
387 if (radius < 0.3)
388 radius = 0.4f;
389 glBegin(GL_POLYGON);
390 for (i = 0; i < 360; i = i + 36) {
391 float degInRad = (float) (i * DEG2RAD);
392 glVertex3f((float)(x + cos(degInRad) * radius),
393 (float)(y + sin(degInRad) * radius),
394 (float)(zdepth + view->Topview->global_z));
395 }
396
397 glEnd();
398}
399
412
413void draw_selpoly(glCompPoly_t *selPoly) {
414 glColor4f(1,0,0,1);
415 glBegin(GL_LINE_STRIP);
416 for (size_t i = 0; i < glCompPoly_size(selPoly); ++i) {
417 const glCompPoint pt = glCompPoly_get(selPoly, i);
418 glVertex3f(pt.x, pt.y, pt.z);
419 }
420 glEnd();
421 if (!glCompPoly_is_empty(selPoly)) {
422 const glCompPoint last = *glCompPoly_back(selPoly);
423 glBegin(GL_LINE_STRIP);
424 glVertex3f(last.x, last.y, last.z);
425 glVertex3f(view->mouse.GLpos.x,view->mouse.GLpos.y,0);
426 glEnd();
427 }
428}
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:275
static void DrawBeziers(sdot_op *o, int param)
Definition draw.c:123
void draw_selpoly(glCompPoly_t *selPoly)
Definition draw.c:413
static int put(void *buffer, const char *s)
Definition draw.c:311
static void DrawBezier(xdot_point *pts, int filled, int param)
Definition draw.c:41
static void SetStyle(sdot_op *o, int param)
Definition draw.c:269
static void EmbedText(sdot_op *o, int param)
Definition draw.c:322
static float dy
Definition draw.c:38
void drawBorders(ViewInfo *vi)
Definition draw.c:367
static void DrawPolygon(sdot_op *o, int param)
Definition draw.c:188
#define LAYER_DIFF
Definition draw.c:39
static float dx
Definition draw.c:37
static glCompColor GetglCompColor(const char *color)
Definition draw.c:240
void drawCircle(float x, float y, float radius, float zdepth)
Definition draw.c:384
drawfunc_t OpFns[]
Definition draw.c:400
static void set_options(int param)
Definition draw.c:108
static void SetFillColor(sdot_op *o, int param)
Definition draw.c:254
static void SetPenColor(sdot_op *o, int param)
Definition draw.c:261
static void InsertImage(sdot_op *o, int param)
Definition draw.c:285
static void SetFont(sdot_op *o, int param)
Definition draw.c:277
static void DrawPolyline(sdot_op *o, int param)
Definition draw.c:218
static void DrawEllipse(sdot_op *o, int param)
Definition draw.c:142
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
glCompFont glNewFont(glCompSet *s, char *text, glCompColor *c, char *fontdesc, int fs, bool is2D)
Definition glcompfont.c:46
void glCompDrawText3D(glCompFont f, float x, float y, double z, float w, float h)
Definition glcompfont.c:109
glCompImage * glCompImageNewFile(float x, float y, const char *imgfile)
Definition glcompimage.c:33
void free(void *)
node NULL
Definition grammar.y:163
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:290
topview * Topview
Definition smyrnadefs.h:313
float LineWidth
Definition smyrnadefs.h:279
float bdyTop
Definition smyrnadefs.h:290
glCompSet * widgets
Definition smyrnadefs.h:336
glCompColor fillColor
Definition smyrnadefs.h:268
glCompColor borderColor
Definition smyrnadefs.h:272
glCompColor selectedNodeColor
Definition smyrnadefs.h:274
float bdxRight
Definition smyrnadefs.h:291
float bdyBottom
Definition smyrnadefs.h:291
int bdVisible
Definition smyrnadefs.h:287
glCompMouse mouse
Definition smyrnadefs.h:303
glCompColor penColor
Definition smyrnadefs.h:266
xdot_font font
Definition xdot.h:146
xdot_rect ellipse
Definition xdot.h:138
xdot_image image
Definition xdot.h:143
char * color
Definition xdot.h:144
xdot_kind kind
Definition xdot.h:136
xdot_text text
Definition xdot.h:142
xdot_polyline bezier
Definition xdot.h:141
xdot_polyline polyline
Definition xdot.h:140
union _xdot_op::@125 u
double RGBA[4]
Definition color.h:32
union color_s::@71 u
glcompdrawfunc_t draw
Definition glcompdefs.h:160
glCompCallBacks functions
Definition glcompdefs.h:194
char * fontdesc
Definition glcompdefs.h:149
glCompObj base
Definition glcompdefs.h:205
glCompPoint GLfinalPos
Definition glcompdefs.h:243
glCompPoint GLpos
Definition glcompdefs.h:241
glCompPoint GLinitPos
Definition glcompdefs.h:242
glCompCommon common
Definition glcompdefs.h:200
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:107
char * name
Definition xdot.h:108
char * name
Definition xdot.h:103
xdot_rect pos
Definition xdot.h:102
double x
Definition xdot.h:82
double z
Definition xdot.h:82
double y
Definition xdot.h:82
size_t cnt
Definition xdot.h:90
xdot_point * pts
Definition xdot.h:91
double x
Definition xdot.h:86
double w
Definition xdot.h:86
double y
Definition xdot.h:86
double h
Definition xdot.h:86
double width
Definition xdot.h:97
char * text
Definition xdot.h:98
double x
Definition xdot.h:95
xdot_align align
Definition xdot.h:96
double y
Definition xdot.h:95
unsigned dash
Definition utils.h:42
graphs, nodes and edges info: Agraphinfo_t, Agnodeinfo_t and Agedgeinfo_t
Definition grammar.c:93
#define UNREACHABLE()
Definition unreachable.h:30
int xml_escape(const char *s, xml_flags_t flags, int(*cb)(void *state, const char *s), void *state)
Definition xml.c:179
parsing and deparsing of xdot operations
@ xd_left
Definition xdot.h:78
@ xd_right
Definition xdot.h:78
@ xd_center
Definition xdot.h:78
void(* drawfunc_t)(xdot_op *, int)
Definition xdot.h:132
@ xd_filled_polygon
Definition xdot.h:113
@ xd_filled_ellipse
Definition xdot.h:112
@ xd_filled_bezier
Definition xdot.h:114