~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to dotneato/common/mpgen.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This software may only be used by you under license from AT&T Corp.
 
3
    ("AT&T").  A copy of AT&T's Source Code Agreement is available at
 
4
    AT&T's Internet website having the URL:
 
5
    <http://www.research.att.com/sw/tools/graphviz/license/source.html>
 
6
    If you received this software without first entering into a license
 
7
    with AT&T, you have an infringing copy of this software and cannot use
 
8
    it without violating AT&T's intellectual property rights.
 
9
*/
 
10
#pragma prototyped
 
11
/* mpgen.c 1999-Feb-23 Jim Hefferon jim@joshua.smcvt.edu 
 
12
 *  Adapted from psgen.c.  See 1st_read.mp.
 
13
 */
 
14
#include        "render.h"
 
15
#ifndef MSWIN32
 
16
#include <unistd.h>
 
17
#endif
 
18
#include <sys/stat.h>
 
19
 
 
20
#ifdef DMALLOC
 
21
#include "dmalloc.h"
 
22
#endif
 
23
 
 
24
#define NONE    0
 
25
#define NODE    1
 
26
#define EDGE    2
 
27
#define CLST    3
 
28
 
 
29
static  FILE    *Outfile;
 
30
static  int             Obj;
 
31
/* static       point   Pages; */
 
32
/* static       box             PB; */
 
33
static int              onetime = TRUE;
 
34
 
 
35
/* static char  **U_lib; */
 
36
 
 
37
typedef struct grcontext_t {
 
38
        char    *color,*font;
 
39
        double  size;
 
40
} grcontext_t;
 
41
 
 
42
#define STACKSIZE 32  /* essentially infinite? */
 
43
static grcontext_t S[STACKSIZE];
 
44
static int SP = 0;
 
45
 
 
46
static void
 
47
mp_reset(void)
 
48
{
 
49
        onetime = TRUE;
 
50
}
 
51
 
 
52
static void
 
53
mp_begin_job(FILE *ofp,graph_t *g, char **lib, char *user, char *info[],
 
54
point pages)
 
55
{
 
56
        Outfile = ofp;
 
57
        /* pages and libraries not here (yet?) */
 
58
        /* Pages = pages; */
 
59
        /* U_lib = lib; */
 
60
        /* N_pages = pages.x * pages.y; */
 
61
        /* Cur_page = 0; */
 
62
 
 
63
        fprintf(Outfile,"%%--- graphviz MetaPost input\n");
 
64
        fprintf(Outfile,"%% Created by program: %s version %s (%s)\n",info[0],info[1], info[2]);
 
65
        fprintf(Outfile,"%% For user: %s\n",user);
 
66
        fprintf(Outfile,"%% Title: %s\n",g->name);
 
67
        fprintf(Outfile,"%%  Put this between beginfig and endfig.  See 1st_read.mp.\n");
 
68
        fprintf(Outfile,"%% \n");
 
69
}
 
70
 
 
71
static  void
 
72
mp_end_job(void)
 
73
{
 
74
        fprintf(Outfile,"%%  End of graphviz MetaPost input\n");
 
75
        fprintf(Outfile,"%%  \n");
 
76
}
 
77
 
 
78
static void
 
79
mp_comment(void* obj, attrsym_t* sym)
 
80
{
 
81
        char    *str;
 
82
        str = late_string(obj,sym,"");
 
83
        if (str[0]) fprintf(Outfile,"%% %s\n",str);
 
84
}
 
85
 
 
86
static void
 
87
mp_begin_graph(graph_t* g, box bb, point pb)
 
88
{
 
89
        /* PB = bb; */
 
90
        if (onetime) {
 
91
                fprintf(Outfile,"%% BoundingBox: %d %d %d %d\n",
 
92
                        bb.LL.x,bb.LL.y,bb.UR.x+1,bb.UR.y+1);
 
93
                mp_comment(g,agfindattr(g,"comment"));
 
94
                /*      cat_libfile(Outfile,U_lib,mp_lib); */
 
95
                onetime = FALSE;
 
96
        }
 
97
}
 
98
 
 
99
static void
 
100
mp_end_graph(void)
 
101
{
 
102
}
 
103
 
 
104
static void
 
105
mp_begin_page(point page, double scale, int rot, point offset)
 
106
{
 
107
        assert(SP == 0);
 
108
        S[SP].font = "";
 
109
        S[SP].color = "black"; 
 
110
        S[SP].size = 0.0;
 
111
}
 
112
 
 
113
static void
 
114
mp_end_page(void)
 
115
{
 
116
}
 
117
 
 
118
static void
 
119
mp_begin_cluster(graph_t* g)
 
120
{
 
121
        Obj = CLST;
 
122
}
 
123
 
 
124
static void
 
125
mp_end_cluster(void)
 
126
{
 
127
        Obj = NONE;
 
128
}
 
129
 
 
130
static void
 
131
mp_begin_nodes(void)        
 
132
{
 
133
    Obj = NONE;
 
134
}
 
135
    
 
136
static void
 
137
mp_end_nodes(void)  
 
138
{
 
139
    Obj = NONE;
 
140
}
 
141
 
 
142
static void 
 
143
mp_begin_edges(void)        
 
144
{
 
145
    Obj = NONE;
 
146
}            
 
147
 
 
148
static void
 
149
mp_end_edges(void)  
 
150
{            
 
151
    Obj = NONE;
 
152
}            
 
153
 
 
154
 
 
155
static void
 
156
mp_begin_node(node_t* n)
 
157
{
 
158
        Obj = NODE;
 
159
        fprintf(Outfile,"%% GV node: \n%%  %s\n",n->name); 
 
160
        mp_comment(n,N_comment);
 
161
}
 
162
 
 
163
static void
 
164
mp_end_node (void)
 
165
{
 
166
        Obj = NONE;
 
167
}
 
168
 
 
169
static void
 
170
mp_begin_edge (edge_t* e)
 
171
{
 
172
        Obj = EDGE;
 
173
        fprintf(Outfile,"%% GV edge: \n%%  %s -> %s\n",e->tail->name,e->head->name); 
 
174
        mp_comment(e,E_comment);
 
175
}
 
176
 
 
177
static void
 
178
mp_end_edge (void)
 
179
{
 
180
        Obj = NONE;
 
181
}
 
182
 
 
183
static void
 
184
mp_begin_context(void)
 
185
{
 
186
        if (SP == STACKSIZE - 1) fprintf(stderr,"warning: mpgen stack overflow\n");
 
187
        else {SP++; S[SP] = S[SP-1];}
 
188
}
 
189
 
 
190
static void
 
191
mp_end_context(void)
 
192
{
 
193
        if (SP == 0) fprintf(stderr,"warning: mpgen stack underflow\n");
 
194
        else SP--;
 
195
}
 
196
 
 
197
static void
 
198
mp_set_font(char* name, double size)
 
199
{
 
200
        if (strcmp(S[SP].font,name) || (size != S[SP].size)) {
 
201
                fprintf(Outfile,"%% GV set font: %.2f /%s ignored\n",size,name); 
 
202
                S[SP].font = name;
 
203
                S[SP].size = size;
 
204
        }
 
205
}
 
206
 
 
207
static void
 
208
mp_set_color(char* name)
 
209
{
 
210
        static char *op[] = {"graph","node","edge","sethsb"};
 
211
        color_t color;
 
212
 
 
213
        if (strcmp(name,S[SP].color)) {
 
214
                colorxlate(name,&color,HSV_DOUBLE);
 
215
                fprintf(Outfile,"%% GV set color: %.3f %.3f %.3f %scolor\n",
 
216
                        color.u.HSV[0],color.u.HSV[1],color.u.HSV[2],op[Obj]); 
 
217
        }
 
218
        S[SP].color = name;
 
219
}
 
220
 
 
221
static void
 
222
mp_set_style(char** s)
 
223
{
 
224
        char    *line,*p;
 
225
 
 
226
        while ((p = line = *s++)) {
 
227
                while (*p) p++; p++;
 
228
                while (*p) {
 
229
                        fprintf(Outfile,"%% GV set style: %s \n",p); 
 
230
                        while (*p) p++; p++;
 
231
                }
 
232
                fprintf(Outfile,"%% GV set style:: %s\n",line);
 
233
        }
 
234
}
 
235
 
 
236
static char *
 
237
mp_string(char *s)
 
238
{
 
239
        static char     *buf = NULL;
 
240
        static int      bufsize = 0;
 
241
        int             pos = 0;
 
242
        char            *p;
 
243
 
 
244
        if (!buf) {
 
245
                bufsize = 64;
 
246
                buf = malloc(bufsize);
 
247
        }
 
248
 
 
249
        p = buf;
 
250
        while (*s)  {
 
251
                if (pos > (bufsize-8)) {
 
252
                        bufsize *= 2;
 
253
                        buf = realloc(buf,bufsize);
 
254
                        p = buf + pos;
 
255
                }
 
256
                if ((*s == LPAREN) || (*s == RPAREN)) {
 
257
                        *p++ = '\\';
 
258
                        pos++;
 
259
                }
 
260
                *p++ = *s++;
 
261
                pos++;
 
262
        }
 
263
        *p = '\0';
 
264
        return buf;
 
265
}
 
266
 
 
267
static void
 
268
mp_textline(point p, textline_t *line)
 
269
{
 
270
        fprintf(Outfile,"label(btex %s etex,(%dbp,%dbp)) withcolor %s;\n",
 
271
                mp_string(line->str),p.x,p.y,S[SP].color);
 
272
}
 
273
 
 
274
static void
 
275
mp_bezier(point *A, int n, int arrow_at_start, int arrow_at_end)
 
276
{
 
277
        int             j;
 
278
        if (arrow_at_start || arrow_at_end)
 
279
                fprintf(stderr," mp_bezier illegal arrow args\n");
 
280
        fprintf(Outfile,"draw (%dbp,%dbp) ",A[0].x,A[0].y);
 
281
        for (j = 1; j < n; j += 3)
 
282
                fprintf(Outfile,"\n  ..controls (%dbp,%dbp) and (%dbp,%dbp).. (%dbp,%dbp)",
 
283
                        A[j].x,A[j].y,A[j+1].x,A[j+1].y,A[j+2].x,A[j+2].y);
 
284
        fprintf(Outfile," withcolor %s;\n",S[SP].color);
 
285
}
 
286
 
 
287
static void
 
288
mp_polygon(point *A, int n, int filled)
 
289
{
 
290
        int             j;
 
291
        if (filled) {
 
292
          fprintf(Outfile,"  fill (%dbp,%dbp)",A[0].x,A[0].y);
 
293
          for (j = 1; j < n; j++) 
 
294
            fprintf(Outfile,"\n  --(%dbp,%dbp)",A[j].x,A[j].y);
 
295
          fprintf(Outfile,"\n  --cycle withcolor %s;\n",S[SP].color);
 
296
        }
 
297
        fprintf(Outfile,"draw (%dbp,%dbp)  ",A[0].x,A[0].y);
 
298
        for (j = 1; j < n; j++) 
 
299
          fprintf(Outfile,"\n  --(%dbp,%dbp)",A[j].x,A[j].y);
 
300
        fprintf(Outfile,"\n  --cycle withcolor %s;\n",S[SP].color);
 
301
}
 
302
 
 
303
static void
 
304
mp_ellipse(point p, int rx, int ry, int filled)
 
305
{
 
306
        if (filled) 
 
307
           fprintf(Outfile,"  fill fullcircle xscaled %dbp yscaled %dbp shifted (%dbp,%dbp) withcolor %s;\n",
 
308
                 2*rx,2*ry,p.x,p.y,S[SP].color);
 
309
        fprintf(Outfile,"draw fullcircle xscaled %dbp yscaled %dbp shifted (%dbp,%dbp);\n",
 
310
                 2*rx,2*ry,p.x,p.y);
 
311
}
 
312
 
 
313
static void
 
314
mp_polyline(point* A, int n)
 
315
{
 
316
        int             j;
 
317
 
 
318
        fprintf(Outfile,"draw (%dbp,%dbp) ",A[0].x,A[0].y);
 
319
        for (j = 1; j < n; j ++) fprintf(Outfile,"\n  --(%dbp,%dbp)",A[j].x,A[j].y);
 
320
        fprintf(Outfile," withcolor %s;\n",S[SP].color);
 
321
}
 
322
 
 
323
static void
 
324
mp_user_shape(char *name, point *A, int sides, int filled)
 
325
{
 
326
        int             j;
 
327
        fprintf(Outfile,"%%GV USER SHAPE [ ");
 
328
        for (j = 0; j < sides; j++) fprintf(Outfile,"%d %d ",A[j].x,A[j].y);
 
329
        fprintf(Outfile,"%d %d ",A[0].x,A[0].y);
 
330
        fprintf(Outfile,"]  %d %s %s ignored\n",sides,(filled?"true":"false"),name);
 
331
}
 
332
 
 
333
 
 
334
codegen_t       MP_CodeGen = {
 
335
        mp_reset,
 
336
        mp_begin_job, mp_end_job,
 
337
        mp_begin_graph, mp_end_graph,
 
338
        mp_begin_page, mp_end_page,
 
339
        mp_begin_cluster, mp_end_cluster,
 
340
        mp_begin_nodes, mp_end_nodes,
 
341
        mp_begin_edges, mp_end_edges,
 
342
        mp_begin_node, mp_end_node,
 
343
        mp_begin_edge, mp_end_edge,
 
344
        mp_begin_context, mp_end_context,
 
345
        mp_set_font, mp_textline,
 
346
        mp_set_color, mp_set_color, mp_set_style,
 
347
        mp_ellipse, mp_polygon,
 
348
        mp_bezier, mp_polyline,
 
349
        0 /* mp_arrowhead */, mp_user_shape,
 
350
        mp_comment, 0 /* mp_textsize */
 
351
};