Graphviz 12.0.1~dev.20240716.0800
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 <cgraph/unreachable.h>
18#include <common/colorprocs.h>
19#include <common/types.h>
20#include <common/utils.h>
21#include "smyrna_utils.h"
22#include <glcomp/glutils.h>
23#include <math.h>
24#include <stdbool.h>
25#include <stdlib.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 ellpise made out of points.
142//void DrawEllipse(xdot_point* xpoint,GLfloat xradius, GLfloat yradius,int filled)
143static void DrawEllipse(sdot_op* o, int param)
144{
145 int i = 0;
146 int filled;
147 xdot_op * op=&o->op;
149 set_options(param);
150 double x = op->u.ellipse.x - dx;
151 double y = op->u.ellipse.y - dy;
152 double xradius = op->u.ellipse.w;
153 double yradius = op->u.ellipse.h;
154 if (op->kind == xd_filled_ellipse) {
155 if (param == 0)
156 glColor4f(view->fillColor.R, view->fillColor.G,
158 if (param == 1) //selected
162
163 filled = 1;
164 } else {
165 if (param == 0)
166 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
167 view->penColor.A);
168 if (param == 1) //selected
172
173 filled = 0;
174 }
175
176 if (!filled)
177 glBegin(GL_LINE_LOOP);
178 else
179 glBegin(GL_POLYGON);
180 for (i = 0; i < 360; i = i + 1) {
181 //convert degrees into radians
182 float degInRad = (float) (i * DEG2RAD);
183 glVertex3f((float)(x + cos(degInRad) * xradius),
184 (float)(y + sin(degInRad) * yradius), (float)view->Topview->global_z);
185 }
186 glEnd();
187}
188
189static void DrawPolygon(sdot_op * o, int param)
190{
191 xdot_op * op=&o->op;
193
194 set_options(param);
195
196 if (op->kind == xd_filled_polygon) {
197 if (param == 0)
198 glColor4f(view->fillColor.R, view->fillColor.G,
200 if (param == 1) //selected
204 } else {
205 if (param == 0)
206 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
207 view->penColor.A);
208 if (param == 1) //selected
212
213 }
214 glLineWidth(view->LineWidth);
216}
217
218
219static void DrawPolyline(sdot_op* o, int param)
220{
221 xdot_op * op=&o->op;
223
224 if (param == 0)
225 glColor4f(view->penColor.R, view->penColor.G, view->penColor.B,
226 view->penColor.A);
227 if (param == 1) //selected
230 set_options(param);
231 glLineWidth(view->LineWidth);
232 glBegin(GL_LINE_STRIP);
233 for (size_t i = 0; i < op->u.polyline.cnt; ++i) {
234 glVertex3f((float)op->u.polyline.pts[i].x - dx,
235 (float)op->u.polyline.pts[i].y - dy,
236 (float)(op->u.polyline.pts[i].z + view->Topview->global_z));
237 }
238 glEnd();
239}
240
242{
243 gvcolor_t cl;
244 glCompColor c;
245 if (color != NULL) {
247 c.R = (float) cl.u.RGBA[0];
248 c.G = (float) cl.u.RGBA[1];
249 c.B = (float) cl.u.RGBA[2];
250 c.A = (float) cl.u.RGBA[3];
251 } else {
252 c = view->penColor;
253 }
254 return c;
255}
256static void SetFillColor(sdot_op* o, int param)
257{
258 (void)param;
259
260 xdot_op * op=&o->op;
262}
263static void SetPenColor(sdot_op* o, int param)
264{
265 (void)param;
266
267 xdot_op * op=&o->op;
269}
270
271static void SetStyle(sdot_op* o, int param)
272{
273 (void)o;
274 (void)param;
275}
276
278
279static void SetFont(sdot_op * o, int param)
280{
281 (void)param;
282
283 font_op=o;
284}
285
286/*for now we only support png files in 2d space, no image rotation*/
287static void InsertImage(sdot_op * o, int param)
288{
289 (void)param;
290
291 float x,y;
292 glCompImage *i;
293
294 if(!o->obj)
295 return;
296
297
298 if(!o->img) {
299 x = o->op.u.image.pos.x;
300 y = o->op.u.image.pos.y;
301 i = o->img = glCompImageNewFile(x, y, o->op.u.image.name);
302 if (!o->img) {
303 fprintf (stderr, "Could not open file \"%s\" to read image.\n", o->op.u.image.name);
304 return;
305 }
306 i->width = o->op.u.image.pos.w;
307 i->height = o->op.u.image.pos.h;
308 i->common.functions.draw(i);
309 }
310}
311
312// see usage in EmbedText
313static int put(void *buffer, const char *s) {
314 char **b = buffer;
315
316 for (; *s != '\0'; ++s) {
317 **b = *s;
318 ++(*b);
319 }
320
321 return 0;
322}
323
324static void EmbedText(sdot_op* o, int param)
325{
326 (void)param;
327
328 float x, y;
330 view->Topview->global_z += o->layer * LAYER_DIFF + 0.05;
331 switch (o->op.u.text.align)
332 {
333 case xd_left:
334 x=o->op.u.text.x ;
335 break;
336 case xd_center:
337 x=o->op.u.text.x - o->op.u.text.width / 2.0;
338 break;
339 case xd_right:
340 x=o->op.u.text.x - o->op.u.text.width;
341 break;
342 default:
343 UNREACHABLE();
344 }
345 y=o->op.u.text.y;
346 if (!o->font)
347 {
348 // allocate a buffer large enough to hold the maximum escaped version of the
349 // text
350 char *escaped = calloc(sizeof(char), strlen(o->op.u.text.text) *
351 sizeof("&#xFFFFFFFF;") + 1);
352 if (escaped == NULL)
353 return;
354
355 // XML-escape the text
356 const xml_flags_t flags = {.dash = 1, .nbsp = 1};
357 char **ptr = &escaped;
358 (void)xml_escape(o->op.u.text.text, flags, put, ptr);
359
360 o->font = glNewFont(view->widgets, escaped, &view->penColor,
362 false);
363
364 free(escaped);
365 }
367
368}
369
371{
372 if (vi->bdVisible) {
373 glColor4f(vi->borderColor.R, vi->borderColor.G,
374 vi->borderColor.B, vi->borderColor.A);
375 glLineWidth(2);
376 glBegin(GL_LINE_STRIP);
377 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
378 glVertex3d(vi->bdxRight, vi->bdyBottom,-0.001);
379 glVertex3d(vi->bdxRight, vi->bdyTop,-0.001);
380 glVertex3d(vi->bdxLeft, vi->bdyTop,-0.001);
381 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
382 glEnd();
383 glLineWidth(1);
384 }
385}
386
387void drawCircle(float x, float y, float radius, float zdepth)
388{
389 int i;
390 if (radius < 0.3)
391 radius = 0.4f;
392 glBegin(GL_POLYGON);
393 for (i = 0; i < 360; i = i + 36) {
394 float degInRad = (float) (i * DEG2RAD);
395 glVertex3f((float)(x + cos(degInRad) * radius),
396 (float)(y + sin(degInRad) * radius),
397 (float)(zdepth + view->Topview->global_z));
398 }
399
400 glEnd();
401}
402
415
417{
418 int i;
419 glColor4f(1,0,0,1);
420 glBegin(GL_LINE_STRIP);
421 for (i = 0;i < selPoly->cnt ; i++)
422 {
423 glVertex3f(selPoly->pts[i].x,selPoly->pts[i].y,selPoly->pts[i].z);
424 }
425 glEnd();
426 if(selPoly->cnt >0)
427 {
428 glBegin(GL_LINE_STRIP);
429 glVertex3f(selPoly->pts[selPoly->cnt-1].x,selPoly->pts[selPoly->cnt-1].y,selPoly->pts[selPoly->cnt-1].z);
430 glVertex3f(view->mouse.GLpos.x,view->mouse.GLpos.y,0);
431 glEnd();
432 }
433}
@ RGBA_DOUBLE
Definition color.h:27
void colorxlate(char *str, agxbuf *buf)
Definition colxlate.c:48
static sdot_op * font_op
Definition draw.c:277
static void DrawBeziers(sdot_op *o, int param)
Definition draw.c:123
static int put(void *buffer, const char *s)
Definition draw.c:313
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:271
static void EmbedText(sdot_op *o, int param)
Definition draw.c:324
static float dy
Definition draw.c:38
void drawBorders(ViewInfo *vi)
Definition draw.c:370
static void DrawPolygon(sdot_op *o, int param)
Definition draw.c:189
#define LAYER_DIFF
Definition draw.c:39
static float dx
Definition draw.c:37
void drawCircle(float x, float y, float radius, float zdepth)
Definition draw.c:387
drawfunc_t OpFns[]
Definition draw.c:403
static void set_options(int param)
Definition draw.c:108
static void SetFillColor(sdot_op *o, int param)
Definition draw.c:256
static void SetPenColor(sdot_op *o, int param)
Definition draw.c:263
static void InsertImage(sdot_op *o, int param)
Definition draw.c:287
static void SetFont(sdot_op *o, int param)
Definition draw.c:279
static void DrawPolyline(sdot_op *o, int param)
Definition draw.c:219
void draw_selpoly(glCompPoly *selPoly)
Definition draw.c:416
static glCompColor GetglCompColor(char *color)
Definition draw.c:241
static void DrawEllipse(sdot_op *o, int param)
Definition draw.c:143
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:47
void glCompDrawText3D(glCompFont *f, float x, float y, double z, float w, float h)
Definition glcompfont.c:117
glCompImage * glCompImageNewFile(float x, float y, char *imgfile)
Definition glcompimage.c:36
void free(void *)
node NULL
Definition grammar.y:149
static void color(Agraph_t *g)
Definition gvcolor.c:128
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:38
#define DEG2RAD
Definition smyrnadefs.h:56
float bdxLeft
Definition smyrnadefs.h:307
topview * Topview
Definition smyrnadefs.h:334
float LineWidth
Definition smyrnadefs.h:296
float bdyTop
Definition smyrnadefs.h:307
glCompSet * widgets
Definition smyrnadefs.h:358
glCompColor fillColor
Definition smyrnadefs.h:285
glCompColor borderColor
Definition smyrnadefs.h:289
glCompColor selectedNodeColor
Definition smyrnadefs.h:291
float bdxRight
Definition smyrnadefs.h:308
float bdyBottom
Definition smyrnadefs.h:308
int bdVisible
Definition smyrnadefs.h:304
glCompMouse mouse
Definition smyrnadefs.h:321
glCompColor penColor
Definition smyrnadefs.h:283
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::@126 u
double RGBA[4]
Definition color.h:32
union color_s::@72 u
glcompdrawfunc_t draw
Definition glcompdefs.h:190
glCompCallBacks functions
Definition glcompdefs.h:223
glCompCommon common
Definition glcompdefs.h:230
glCompPoint GLfinalPos
Definition glcompdefs.h:282
glCompPoint GLpos
Definition glcompdefs.h:280
glCompPoint GLinitPos
Definition glcompdefs.h:281
glCompPoint * pts
Definition glcompdefs.h:145
xdot_op op
Definition smyrnadefs.h:95
glCompFont * font
Definition smyrnadefs.h:97
glCompImage * img
Definition smyrnadefs.h:99
void * obj
Definition smyrnadefs.h:96
int layer
Definition smyrnadefs.h:98
double global_z
Definition smyrnadefs.h:255
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