Debian Codegraph @

cmd/smyrna/draw.c

raw
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
13 XDOT DRAWING FUNCTIONS, maybe need to move them somewhere else
14 for now keep them at the bottom
15 */
16
17 #include "config.h"
18
19 #include "draw.h"
20 #include <common/colorprocs.h>
21 #include "smyrna_utils.h"
22 #include <glcomp/glutils.h>
23 #include <math.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <util/unreachable.h>
29 #include <util/list.h>
30 #include <util/xml.h>
31
32 #include <xdot/xdot.h>
33 #include "viewport.h"
34 #include "topfisheyeview.h"
35 #include "gui/appmouse.h"
36 #include "hotkeymap.h"
37 #include "polytess.h"
38 #include <glcomp/glcompimage.h>
39
40
41 //delta values
42 static float dx = 0.0;
43 static float dy = 0.0;
44 #define LAYER_DIFF 0.001
45
46 static void DrawBezier(xdot_point* pts, int filled, int param)
47 {
48 /*copied from NEHE */
49 /*Written by: David Nikdel ( [email protected] ) */
50 double Ax = pts[0].x;
51 double Ay = pts[0].y;
52 double Az = pts[0].z;
53 double Bx = pts[1].x;
54 double By = pts[1].y;
55 double Bz = pts[1].z;
56 double Cx = pts[2].x;
57 double Cy = pts[2].y;
58 double Cz = pts[2].z;
59 double Dx = pts[3].x;
60 double Dy = pts[3].y;
61 double Dz = pts[3].z;
62 double X;
63 double Y;
64 double Z;
65 int i = 0; //loop index
66 // Variable
67 double a = 1.0;
68 double b = 1.0 - a;
69 /* Tell OGL to start drawing a line strip */
70 glLineWidth(view->LineWidth);
71 if (!filled) {
72
73 if (param == 0)
74 glColor4d(view->penColor.R, view->penColor.G, view->penColor.B,
75 view->penColor.A);
76 else if (param == 1) //selected
77 glColor4d(view->selectedNodeColor.R, view->selectedNodeColor.G,
78 view->selectedNodeColor.B,
79 view->selectedNodeColor.A);
80 glBegin(GL_LINE_STRIP);
81 } else {
82 if (param == 0)
83 glColor4d(view->fillColor.R, view->fillColor.G,
84 view->fillColor.B, view->penColor.A);
85 else if (param == 1) //selected
86 glColor4d(view->selectedNodeColor.R, view->selectedNodeColor.G,
87 view->selectedNodeColor.B,
88 view->selectedNodeColor.A);
89 glBegin(GL_POLYGON);
90 }
91 /* We will not actually draw a curve, but we will divide the curve into small
92 points and draw a line between each point. If the points are close enough, it
93 will appear as a curved line. 20 points are plenty, and since the variable goes
94 from 1.0 to 0.0 we must change it by 1/20 = 0.05 each time */
95 for (i = 0; i <= 20; i++) {
96 // Get a point on the curve
97 X = Ax * a * a * a + Bx * 3 * a * a * b + Cx * 3 * a * b * b +
98 Dx * b * b * b;
99 Y = Ay * a * a * a + By * 3 * a * a * b + Cy * 3 * a * b * b +
100 Dy * b * b * b;
101 Z = Az * a * a * a + Bz * 3 * a * a * b + Cz * 3 * a * b * b +
102 Dz * b * b * b;
103 // Draw the line from point to point (assuming OGL is set up properly)
104 glVertex3d(X, Y, Z + view->Topview->global_z);
105 // Change the variable
106 a -= 0.05;
107 b = 1.0 - a;
108 }
109 // Tell OGL to stop drawing the line strip
110 glEnd();
111 }
112
113 static void set_options(int param)
114 {
115
116 int a=get_mode(view);
117 if (param == 1 && a == 10 && view->mouse.down) // selected, if there is move, move it
118 {
119 dx = view->mouse.GLinitPos.x-view->mouse.GLfinalPos.x;
120 dy = view->mouse.GLinitPos.y-view->mouse.GLfinalPos.y;
121 } else {
122 dx = 0;
123 dy = 0;
124 }
125
126 }
127
128 static void DrawBeziers(xdot_op *op, int param) {
129 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
130 xdot_point* ps = op->u.bezier.pts;
131 view->Topview->global_z += o->layer * LAYER_DIFF;
132
133 const int filled = op->kind == xd_filled_bezier;
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.
142 static void DrawEllipse(xdot_op *op, int param) {
143 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
144 int filled;
145 view->Topview->global_z += o->layer * LAYER_DIFF;
146 set_options(param);
147 double x = op->u.ellipse.x - dx;
148 double y = op->u.ellipse.y - dy;
149 double xradius = op->u.ellipse.w;
150 double yradius = op->u.ellipse.h;
151 if (op->kind == xd_filled_ellipse) {
152 if (param == 0)
153 glColor4d(view->fillColor.R, view->fillColor.G,
154 view->fillColor.B, view->fillColor.A);
155 if (param == 1) //selected
156 glColor4d(view->selectedNodeColor.R, view->selectedNodeColor.G,
157 view->selectedNodeColor.B,
158 view->selectedNodeColor.A);
159
160 filled = 1;
161 } else {
162 if (param == 0)
163 glColor4d(view->penColor.R, view->penColor.G, view->penColor.B,
164 view->penColor.A);
165 if (param == 1) //selected
166 glColor4d(view->selectedNodeColor.R, view->selectedNodeColor.G,
167 view->selectedNodeColor.B,
168 view->selectedNodeColor.A);
169
170 filled = 0;
171 }
172
173 if (!filled)
174 glBegin(GL_LINE_LOOP);
175 else
176 glBegin(GL_POLYGON);
177 for (int i = 0; i < 360; ++i) {
178 //convert degrees into radians
179 const double degInRad = i * DEG2RAD;
180 glVertex3d(x + cos(degInRad) * xradius,
181 y + sin(degInRad) * yradius, view->Topview->global_z);
182 }
183 glEnd();
184 }
185
186 static void DrawPolygon(xdot_op *op, int param) {
187 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
188 view->Topview->global_z += o->layer * LAYER_DIFF;
189
190 set_options(param);
191
192 if (op->kind == xd_filled_polygon) {
193 if (param == 0)
194 glColor4d(view->fillColor.R, view->fillColor.G,
195 view->fillColor.B, view->fillColor.A);
196 if (param == 1) //selected
197 glColor4d(view->selectedNodeColor.R, view->selectedNodeColor.G,
198 view->selectedNodeColor.B,
199 view->selectedNodeColor.A);
200 } else {
201 if (param == 0)
202 glColor4d(view->penColor.R, view->penColor.G, view->penColor.B,
203 view->penColor.A);
204 if (param == 1) //selected
205 glColor4d(view->selectedNodeColor.R, view->selectedNodeColor.G,
206 view->selectedNodeColor.B,
207 view->selectedNodeColor.A);
208
209 }
210 glLineWidth(view->LineWidth);
211 drawTessPolygon(o);
212 }
213
214 static void DrawPolyline(xdot_op *op, int param) {
215 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
216 view->Topview->global_z += o->layer * LAYER_DIFF;
217
218 if (param == 0)
219 glColor4d(view->penColor.R, view->penColor.G, view->penColor.B,
220 view->penColor.A);
221 if (param == 1) //selected
222 glColor4d(view->selectedNodeColor.R, view->selectedNodeColor.G,
223 view->selectedNodeColor.B, view->selectedNodeColor.A);
224 set_options(param);
225 glLineWidth(view->LineWidth);
226 glBegin(GL_LINE_STRIP);
227 for (size_t i = 0; i < op->u.polyline.cnt; ++i) {
228 glVertex3d(op->u.polyline.pts[i].x - dx,
229 op->u.polyline.pts[i].y - dy,
230 op->u.polyline.pts[i].z + view->Topview->global_z);
231 }
232 glEnd();
233 }
234
235 static glCompColor GetglCompColor(const char *color) {
236 gvcolor_t cl;
237 glCompColor c;
238 if (color != NULL) {
239 colorxlate(color, &cl, RGBA_DOUBLE);
240 c.R = cl.u.RGBA[0];
241 c.G = cl.u.RGBA[1];
242 c.B = cl.u.RGBA[2];
243 c.A = cl.u.RGBA[3];
244 } else {
245 c = view->penColor;
246 }
247 return c;
248 }
249
250 static void SetFillColor(xdot_op *op, int param) {
251 (void)param;
252 view->fillColor = GetglCompColor(op->u.color);
253 }
254
255 static void SetPenColor(xdot_op *op, int param) {
256 (void)param;
257 view->penColor = GetglCompColor(op->u.color);
258 }
259
260 static sdot_op * font_op;
261
262 static void SetFont(xdot_op *op, int param) {
263 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
264 (void)param;
265
266 font_op=o;
267 }
268
269 /*for now we only support png files in 2d space, no image rotation*/
270 static void InsertImage(xdot_op *op, int param) {
271 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
272 (void)param;
273
274 if(!o->obj)
275 return;
276
277
278 if(!o->img) {
279 const float x = o->op.u.image.pos.x;
280 const float y = o->op.u.image.pos.y;
281 glCompImage *const i = o->img = glCompImageNewFile(x, y, o->op.u.image.name);
282 if (!o->img) {
283 fprintf (stderr, "Could not open file \"%s\" to read image.\n", o->op.u.image.name);
284 return;
285 }
286 i->width = o->op.u.image.pos.w;
287 i->height = o->op.u.image.pos.h;
288 i->base.common.functions.draw(i);
289 }
290 }
291
292 // see usage in EmbedText
293 static int put(void *buffer, const char *s) {
294 char **b = buffer;
295
296 const size_t len = strlen(s);
297 memcpy(*b, s, len);
298 *b += len;
299
300 return 0;
301 }
302
303 static void EmbedText(xdot_op *op, int param) {
304 sdot_op *const o = (sdot_op *)((char *)op - offsetof(sdot_op, op));
305 (void)param;
306
307 float x, y;
308 glColor4d(view->penColor.R,view->penColor.G,view->penColor.B,view->penColor.A);
309 view->Topview->global_z += o->layer * LAYER_DIFF + 0.05;
310 switch (o->op.u.text.align)
311 {
312 case xd_left:
313 x=o->op.u.text.x ;
314 break;
315 case xd_center:
316 x=o->op.u.text.x - o->op.u.text.width / 2.0;
317 break;
318 case xd_right:
319 x=o->op.u.text.x - o->op.u.text.width;
320 break;
321 default:
322 UNREACHABLE();
323 }
324 y=o->op.u.text.y;
325 if (o->font.fontdesc == NULL) {
326 // allocate a buffer large enough to hold the maximum escaped version of the
327 // text
328 char *escaped = calloc(sizeof(char), strlen(o->op.u.text.text) *
329 sizeof("&#xFFFFFFFF;") + 1);
330 if (escaped == NULL)
331 return;
332
333 // XML-escape the text
334 const xml_flags_t flags = {.dash = 1, .nbsp = 1};
335 char *ptr = escaped;
336 (void)gv_xml_escape(o->op.u.text.text, flags, put, &ptr);
337
338 o->font = glNewFont(view->widgets, escaped, &view->penColor,
339 font_op->op.u.font.name, font_op->op.u.font.size,
340 false);
341
342 free(escaped);
343 }
344 glCompDrawText3D(o->font, x, y, view->Topview->global_z, o->op.u.text.width,
345 font_op->op.u.font.size);
346 }
347
348 void drawBorders(ViewInfo * vi)
349 {
350 if (vi->bdVisible) {
351 glColor4d(vi->borderColor.R, vi->borderColor.G,
352 vi->borderColor.B, vi->borderColor.A);
353 glLineWidth(2);
354 glBegin(GL_LINE_STRIP);
355 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
356 glVertex3d(vi->bdxRight, vi->bdyBottom,-0.001);
357 glVertex3d(vi->bdxRight, vi->bdyTop,-0.001);
358 glVertex3d(vi->bdxLeft, vi->bdyTop,-0.001);
359 glVertex3d(vi->bdxLeft, vi->bdyBottom,-0.001);
360 glEnd();
361 glLineWidth(1);
362 }
363 }
364
365 void drawCircle(double x, double y, double radius, double zdepth) {
366 if (radius < 0.3)
367 radius = 0.4;
368 glBegin(GL_POLYGON);
369 for (int i = 0; i < 360; i += 36) {
370 const double degInRad = i * DEG2RAD;
371 glVertex3d(x + cos(degInRad) * radius, y + sin(degInRad) * radius,
372 zdepth + view->Topview->global_z);
373 }
374
375 glEnd();
376 }
377
378 drawfunc_t OpFns[] = {
379 [xop_ellipse] = DrawEllipse,
380 [xop_polygon] = DrawPolygon,
381 [xop_bezier] = DrawBeziers,
382 [xop_polyline] = DrawPolyline,
383 [xop_text] = EmbedText,
384 [xop_fill_color] = SetFillColor,
385 [xop_pen_color] = SetPenColor,
386 [xop_font] = SetFont,
387 [xop_style] = NULL,
388 [xop_image] = InsertImage,
389 [xop_grad_color] = NULL,
390 [xop_fontchar] = NULL,
391 };
392
393 void draw_selpoly(glCompPoly_t *selPoly) {
394 glColor4f(1,0,0,1);
395 glBegin(GL_LINE_STRIP);
396 for (size_t i = 0; i < LIST_SIZE(selPoly); ++i) {
397 const glCompPoint pt = LIST_GET(selPoly, i);
398 glVertex3f(pt.x, pt.y, pt.z);
399 }
400 glEnd();
401 if (!LIST_IS_EMPTY(selPoly)) {
402 const glCompPoint last = *LIST_BACK(selPoly);
403 glBegin(GL_LINE_STRIP);
404 glVertex3f(last.x, last.y, last.z);
405 glVertex3f(view->mouse.GLpos.x,view->mouse.GLpos.y,0);
406 glEnd();
407 }
408 }