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

« back to all changes in this revision

Viewing changes to dotneato/common/mifgen.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
 
 
12
#include        "render.h"
 
13
 
 
14
#ifdef DMALLOC
 
15
#include "dmalloc.h"
 
16
#endif
 
17
 
 
18
#define NONE    0
 
19
#define NODE    1
 
20
#define EDGE    2
 
21
#define CLST    3
 
22
 
 
23
/* MIF font modifiers */
 
24
#define REGULAR 0
 
25
#define BOLD    1
 
26
#define ITALIC  2
 
27
 
 
28
/* MIF patterns */
 
29
#define P_SOLID 0
 
30
#define P_NONE  15
 
31
#define P_DOTTED 4      /* i wasn't sure about this */
 
32
#define P_DASHED 11 /* or this */
 
33
 
 
34
/* MIF bold line constant */
 
35
#define WIDTH_NORMAL 1
 
36
#define WIDTH_BOLD 3
 
37
 
 
38
static  FILE    *Outfile;
 
39
/* static       int             Obj; */
 
40
static  int             N_pages;
 
41
/* static       point   Pages; */
 
42
static  double  Scale;
 
43
static  int             Rot;
 
44
static  box             PB;
 
45
static int              onetime = TRUE;
 
46
 
 
47
typedef struct context_t {
 
48
        char    color_ix,*fontfam,fontopt,font_was_set;
 
49
        char    pen,fill,penwidth,style_was_set;
 
50
        float   fontsz;
 
51
} context_t;
 
52
 
 
53
#define MAXNEST 4
 
54
static context_t cstk[MAXNEST];
 
55
static int SP;
 
56
 
 
57
static char *FillStr = "<Fill 3>";
 
58
static char *NoFillStr = "<Fill 15>";
 
59
 
 
60
static void mif_reset(void)
 
61
{
 
62
        onetime = TRUE;
 
63
}
 
64
 
 
65
 
 
66
static void init_mif(void)
 
67
{
 
68
        SP = 0;
 
69
        cstk[0].color_ix = 0;           /* MIF color index 0-7 */
 
70
        cstk[0].fontfam = "Times";      /* font family name */
 
71
        cstk[0].fontopt = REGULAR;      /* modifier: REGULAR, BOLD or ITALIC */
 
72
        cstk[0].pen = P_SOLID;          /* pen pattern style, default is solid */
 
73
        cstk[0].fill = P_NONE;
 
74
        cstk[0].penwidth = WIDTH_NORMAL;
 
75
}
 
76
 
 
77
static pointf
 
78
mifpt(pointf p)
 
79
{
 
80
        pointf  tmp,rv;
 
81
        tmp.x = p.x * Scale; tmp.y = Scale * p.y;
 
82
        if (Rot == 0) { rv.x = tmp.x; rv.y = PB.UR.y - PB.LL.y - tmp.y; }
 
83
        else {rv.x = PB.UR.x - PB.LL.x - tmp.y; rv.y = tmp.x; }
 
84
        return rv;
 
85
}
 
86
 
 
87
static void
 
88
mifptarray(point* A, int n)
 
89
{
 
90
        int             i;
 
91
        pointf  p;
 
92
 
 
93
        fprintf(Outfile," <NumPoints %d>\n",n);
 
94
        for (i = 0; i < n; i++) {
 
95
                p.x = A[i].x; p.y = A[i].y;
 
96
                p = mifpt(p);
 
97
                fprintf(Outfile," <Point %.2f %.2f>\n",p.x,p.y);
 
98
        }
 
99
}
 
100
 
 
101
static void mif_font(context_t* cp)
 
102
{
 
103
        char    *fw,*fa;
 
104
 
 
105
        fw = fa = "Regular";
 
106
        switch (cp->fontopt) {
 
107
                case BOLD: fw = "Bold"; break;
 
108
                case ITALIC: fa = "Italic"; break;
 
109
        }
 
110
        fprintf(Outfile,"<Font <FFamily `%s'> <FSize %.1f pt> <FWeight %s> <FAngle %s>>\n",cp->fontfam,Scale*cp->fontsz,fw,fa);
 
111
}
 
112
 
 
113
static void mif_color(int i)
 
114
{
 
115
        static char     *mifcolor[]= {
 
116
                "black","white","red","green","blue","cyan",
 
117
                "magenta","yellow","comment",
 
118
                "aquamarine","plum","peru","pink","mediumpurple","grey", 
 
119
                "lightgrey","lightskyblue","lightcoral","yellowgreen",
 
120
                (char*)0};
 
121
        if (i <= 8) fprintf(Outfile,"<Separation %d>\n",i);
 
122
        if (i > 8) fprintf(Outfile, "<ObColor `%s'>\n", mifcolor[i]);
 
123
}
 
124
 
 
125
static void mif_style(context_t* cp)
 
126
{
 
127
        fprintf(Outfile,"<Pen %d> <Fill %d> <PenWidth %d>\n",
 
128
                cp->pen,cp->fill,cp->penwidth);
 
129
}
 
130
 
 
131
static void mif_comment(void* obj, attrsym_t* sym)
 
132
{
 
133
        char    *str;
 
134
        str = late_string(obj,sym,"");
 
135
        if (str[0]) fprintf(Outfile,"# %s\n",str);
 
136
}
 
137
 
 
138
static void
 
139
mif_begin_job(FILE *ofp, graph_t *g, char **lib, char *user, char *info[], point pages)
 
140
{
 
141
        Outfile = ofp;
 
142
        /* Pages = pages; */
 
143
        N_pages = pages.x * pages.y;
 
144
        fprintf(Outfile,"<MIFFile 3.00> # Generated by %s version %s (%s)\n",
 
145
                info[0],info[1],info[2]);
 
146
        fprintf(Outfile,"# For: %s\n",user);
 
147
        fprintf(Outfile,"# Title: %s\n",g->name);
 
148
        fprintf(Outfile,"# Pages: %d\n",N_pages);
 
149
        fprintf(Outfile,"<Units Upt>\n");
 
150
        fprintf(Outfile, "<ColorCatalog \n");
 
151
fprintf(Outfile, " <Color \n");
 
152
fprintf(Outfile, "  <ColorTag `Black'>\n");
 
153
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
154
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
155
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
156
fprintf(Outfile, "  <ColorBlack  100.000000>\n");
 
157
fprintf(Outfile, "  <ColorAttribute ColorIsBlack>\n");
 
158
fprintf(Outfile, "  <ColorAttribute ColorIsReserved>\n");
 
159
fprintf(Outfile, " > # end of Color\n");
 
160
fprintf(Outfile, " <Color \n");
 
161
fprintf(Outfile, "  <ColorTag `White'>\n");
 
162
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
163
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
164
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
165
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
166
fprintf(Outfile, "  <ColorAttribute ColorIsWhite>\n");
 
167
fprintf(Outfile, "  <ColorAttribute ColorIsReserved>\n");
 
168
fprintf(Outfile, " > # end of Color\n");
 
169
fprintf(Outfile, " <Color \n");
 
170
fprintf(Outfile, "  <ColorTag `Red'>\n");
 
171
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
172
fprintf(Outfile, "  <ColorMagenta  100.000000>\n");
 
173
fprintf(Outfile, "  <ColorYellow  100.000000>\n");
 
174
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
175
fprintf(Outfile, "  <ColorAttribute ColorIsRed>\n");
 
176
fprintf(Outfile, "  <ColorAttribute ColorIsReserved>\n");
 
177
fprintf(Outfile, " > # end of Color\n");
 
178
fprintf(Outfile, " <Color \n");
 
179
fprintf(Outfile, "  <ColorTag `Green'>\n");
 
180
fprintf(Outfile, "  <ColorCyan  100.000000>\n");
 
181
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
182
fprintf(Outfile, "  <ColorYellow  100.000000>\n");
 
183
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
184
fprintf(Outfile, "  <ColorAttribute ColorIsGreen>\n");
 
185
fprintf(Outfile, "  <ColorAttribute ColorIsReserved>\n");
 
186
fprintf(Outfile, " > # end of Color\n");
 
187
fprintf(Outfile, " <Color \n");
 
188
fprintf(Outfile, "  <ColorTag `Blue'>\n");
 
189
fprintf(Outfile, "  <ColorCyan  100.000000>\n");
 
190
fprintf(Outfile, "  <ColorMagenta  100.000000>\n");
 
191
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
192
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
193
fprintf(Outfile, "  <ColorAttribute ColorIsBlue>\n");
 
194
fprintf(Outfile, "  <ColorAttribute ColorIsReserved>\n");
 
195
fprintf(Outfile, " > # end of Color\n");
 
196
fprintf(Outfile, " <Color \n");
 
197
fprintf(Outfile, "  <ColorTag `Cyan'>\n");
 
198
fprintf(Outfile, "  <ColorCyan  100.000000>\n");
 
199
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
200
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
201
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
202
fprintf(Outfile, "  <ColorAttribute ColorIsCyan>\n");
 
203
fprintf(Outfile, "  <ColorAttribute ColorIsReserved>\n");
 
204
fprintf(Outfile, " > # end of Color\n");
 
205
fprintf(Outfile, " <Color \n");
 
206
fprintf(Outfile, "  <ColorTag `Magenta'>\n");
 
207
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
208
fprintf(Outfile, "  <ColorMagenta  100.000000>\n");
 
209
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
210
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
211
fprintf(Outfile, "  <ColorAttribute ColorIsMagenta>\n");
 
212
fprintf(Outfile, "  <ColorAttribute ColorIsReserved>\n");
 
213
fprintf(Outfile, " > # end of Color\n");
 
214
fprintf(Outfile, " <Color \n");
 
215
fprintf(Outfile, "  <ColorTag `Yellow'>\n");
 
216
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
217
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
218
fprintf(Outfile, "  <ColorYellow  100.000000>\n");
 
219
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
220
fprintf(Outfile, "  <ColorAttribute ColorIsYellow>\n");
 
221
fprintf(Outfile, "  <ColorAttribute ColorIsReserved>\n");
 
222
fprintf(Outfile, " > # end of Color\n");
 
223
fprintf(Outfile, " <Color \n");
 
224
fprintf(Outfile, "  <ColorTag `aquamarine'>\n");
 
225
fprintf(Outfile, "  <ColorCyan  100.000000>\n");
 
226
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
227
fprintf(Outfile, "  <ColorYellow  18.000000>\n");
 
228
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
229
fprintf(Outfile, " > # end of Color\n");
 
230
fprintf(Outfile, " <Color \n");
 
231
fprintf(Outfile, "  <ColorTag `plum'>\n");
 
232
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
233
fprintf(Outfile, "  <ColorMagenta  100.000000>\n");
 
234
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
235
fprintf(Outfile, "  <ColorBlack  33.000000>\n");
 
236
fprintf(Outfile, " > # end of Color\n");
 
237
fprintf(Outfile, " <Color \n");
 
238
fprintf(Outfile, "  <ColorTag `peru'>\n");
 
239
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
240
fprintf(Outfile, "  <ColorMagenta  24.000000>\n");
 
241
fprintf(Outfile, "  <ColorYellow  100.000000>\n");
 
242
fprintf(Outfile, "  <ColorBlack  32.000000>\n");
 
243
fprintf(Outfile, " > # end of Color\n");
 
244
fprintf(Outfile, " <Color \n");
 
245
fprintf(Outfile, "  <ColorTag `pink'>\n");
 
246
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
247
fprintf(Outfile, "  <ColorMagenta  50.000000>\n");
 
248
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
249
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
250
fprintf(Outfile, " > # end of Color\n");
 
251
fprintf(Outfile, " <Color \n");
 
252
fprintf(Outfile, "  <ColorTag `mediumpurple'>\n");
 
253
fprintf(Outfile, "  <ColorCyan  40.000000>\n");
 
254
fprintf(Outfile, "  <ColorMagenta  100.000000>\n");
 
255
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
256
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
257
fprintf(Outfile, " > # end of Color\n");
 
258
fprintf(Outfile, " <Color \n");
 
259
fprintf(Outfile, "  <ColorTag `grey'>\n");
 
260
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
261
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
262
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
263
fprintf(Outfile, "  <ColorBlack  50.000000>\n");
 
264
fprintf(Outfile, " > # end of Color\n");
 
265
fprintf(Outfile, " <Color \n");
 
266
fprintf(Outfile, "  <ColorTag `lightgrey'>\n");
 
267
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
268
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
269
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
270
fprintf(Outfile, "  <ColorBlack  25.000000>\n");
 
271
fprintf(Outfile, " > # end of Color\n");
 
272
fprintf(Outfile, " <Color \n");
 
273
fprintf(Outfile, "  <ColorTag `lightskyblue'>\n");
 
274
fprintf(Outfile, "  <ColorCyan  38.000000>\n");
 
275
fprintf(Outfile, "  <ColorMagenta  33.000000>\n");
 
276
fprintf(Outfile, "  <ColorYellow  0.000000>\n");
 
277
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
278
fprintf(Outfile, " > # end of Color\n");
 
279
fprintf(Outfile, " <Color \n");
 
280
fprintf(Outfile, "  <ColorTag `lightcoral'>\n");
 
281
fprintf(Outfile, "  <ColorCyan  0.000000>\n");
 
282
fprintf(Outfile, "  <ColorMagenta  50.000000>\n");
 
283
fprintf(Outfile, "  <ColorYellow  60.000000>\n");
 
284
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
285
fprintf(Outfile, " > # end of Color\n");
 
286
fprintf(Outfile, " <Color \n");
 
287
fprintf(Outfile, "  <ColorTag `yellowgreen'>\n");
 
288
fprintf(Outfile, "  <ColorCyan  31.000000>\n");
 
289
fprintf(Outfile, "  <ColorMagenta  0.000000>\n");
 
290
fprintf(Outfile, "  <ColorYellow  100.000000>\n");
 
291
fprintf(Outfile, "  <ColorBlack  0.000000>\n");
 
292
fprintf(Outfile, " > # end of Color\n");
 
293
fprintf(Outfile, "> # end of ColorCatalog\n");
 
294
 
 
295
}
 
296
 
 
297
static  void
 
298
mif_end_job(void)
 
299
{
 
300
        fprintf(Outfile,"# end of MIFFile\n");
 
301
}
 
302
 
 
303
static void
 
304
mif_begin_graph(graph_t* g, box bb, point pb)
 
305
{
 
306
        g = g;
 
307
        PB = bb;
 
308
        if (onetime) {
 
309
                fprintf(Outfile,"<BRect %d %d %d %d>\n",
 
310
                        PB.LL.x,PB.UR.y,PB.UR.x - PB.LL.x, PB.UR.y - PB.LL.y);
 
311
                init_mif();
 
312
                mif_comment(g,agfindattr(g,"comment"));
 
313
                onetime = FALSE;
 
314
        }
 
315
}
 
316
 
 
317
static void
 
318
mif_end_graph(void)
 
319
{
 
320
}
 
321
 
 
322
static void
 
323
mif_begin_page(point page, double scale, int rot, point offset)
 
324
{
 
325
        /* int          page_number; */
 
326
        /* point        sz; */
 
327
 
 
328
        Scale = scale;
 
329
        Rot = rot;
 
330
        /* page_number =  page.x + page.y * Pages.x + 1; */
 
331
        /* sz = sub_points(PB.UR,PB.LL); */
 
332
        fprintf(Outfile," <ArrowStyle <TipAngle 15> <BaseAngle 90> <Length %.1f> <HeadType Filled>>\n",14*Scale);
 
333
}
 
334
 
 
335
static  void
 
336
mif_end_page(void)
 
337
{
 
338
}
 
339
 
 
340
static  void
 
341
mif_begin_cluster(graph_t* g)
 
342
{
 
343
        /* Obj = CLST; */
 
344
}
 
345
 
 
346
static  void
 
347
mif_end_cluster (void)
 
348
{
 
349
        /* Obj = NONE; */
 
350
}
 
351
 
 
352
static void
 
353
mif_begin_nodes(void)        
 
354
{
 
355
        /* Obj = NONE; */
 
356
}
 
357
    
 
358
static void
 
359
mif_end_nodes(void)  
 
360
{
 
361
        /* Obj = NONE; */
 
362
}
 
363
 
 
364
static void 
 
365
mif_begin_edges(void)        
 
366
{
 
367
        /* Obj = NONE; */
 
368
}            
 
369
 
 
370
static void
 
371
mif_end_edges(void)  
 
372
{            
 
373
        /* Obj = NONE; */
 
374
}            
 
375
 
 
376
static  void
 
377
mif_begin_node(node_t* n)
 
378
{
 
379
        /* Obj = NODE; */
 
380
        fprintf(Outfile,"# %s\n",n->name);
 
381
        mif_comment(n,N_comment);
 
382
}
 
383
 
 
384
static  void
 
385
mif_end_node (void)
 
386
{
 
387
        /* Obj = NONE; */
 
388
}
 
389
 
 
390
static  void
 
391
mif_begin_edge (edge_t* e)
 
392
{
 
393
        /* Obj = EDGE; */
 
394
        fprintf(Outfile,"# %s -> %s\n",e->tail->name,e->head->name);
 
395
        mif_comment(e,E_comment);
 
396
}
 
397
 
 
398
static  void
 
399
mif_end_edge (void)
 
400
{
 
401
        /* Obj = NONE; */
 
402
}
 
403
 
 
404
static  void
 
405
mif_begin_context(void)
 
406
{
 
407
        assert(SP + 1 < MAXNEST);
 
408
        cstk[SP+1] = cstk[SP];
 
409
        SP++;
 
410
}
 
411
 
 
412
static  void 
 
413
mif_end_context(void)
 
414
{
 
415
        int                     c, psp = SP - 1;
 
416
        assert(SP > 0);
 
417
        if (cstk[SP].color_ix != (c = cstk[psp].color_ix)) mif_color(c);
 
418
        if (cstk[SP].font_was_set) mif_font(&(cstk[psp]));
 
419
        if (cstk[SP].style_was_set) mif_style(&(cstk[psp]));
 
420
        /*free(cstk[psp].fontfam);*/
 
421
        SP = psp;
 
422
}
 
423
 
 
424
static void 
 
425
mif_set_font(char* name, double size)
 
426
{
 
427
        char    *p,*q;
 
428
        context_t       *cp;
 
429
 
 
430
        cp = &(cstk[SP]);
 
431
        cp->font_was_set = TRUE;
 
432
        cp->fontsz = size;
 
433
        p = strdup(name);
 
434
        if ((q = strchr(p,'-'))) {
 
435
                *q++ = 0;
 
436
                if (strcasecmp(q,"italic") == 0)
 
437
                        cp->fontopt = ITALIC;
 
438
                else if (strcasecmp(q,"bold") == 0)
 
439
                        cp->fontopt = BOLD;
 
440
        }
 
441
        cp->fontfam = p;
 
442
        mif_font(&cstk[SP]);
 
443
}
 
444
 
 
445
static  void
 
446
mif_set_color(char* name)
 
447
{
 
448
        int             i;
 
449
        char            *tok;
 
450
        
 
451
        static char     *mifcolor[]= {
 
452
                "black","white","red","green","blue","cyan",
 
453
                "magenta","yellow","comment",
 
454
                "aquamarine","plum","peru","pink","mediumpurple","grey", 
 
455
                "lightgrey", "lightskyblue","lightcoral","yellowgreen",
 
456
                (char*)0};
 
457
        
 
458
        tok = canontoken(name);
 
459
        for (i = 0; mifcolor[i]; i++) {
 
460
                if (strcasecmp(mifcolor[i],tok) == 0) {
 
461
                        cstk[SP].color_ix = i;
 
462
                        mif_color(i);
 
463
                        return;
 
464
                }
 
465
        }
 
466
        fprintf(stderr,"color %s not supported in MIF\n",name);
 
467
}
 
468
 
 
469
static  void
 
470
mif_set_style(char** s)
 
471
{
 
472
        char            *line;
 
473
        context_t       *cp;
 
474
 
 
475
        cp = &(cstk[SP]);
 
476
        while ((line = *s++)) {
 
477
                if (streq(line,"solid")) { /* no-op */ }
 
478
                else if (streq(line,"dashed")) cp->pen = P_DASHED;
 
479
                else if (streq(line,"dotted")) cp->pen = P_DOTTED;
 
480
                else if (streq(line,"invis")) cp->pen = P_NONE;
 
481
                else if (streq(line,"bold")) cp->penwidth = WIDTH_BOLD;
 
482
                else if (streq(line,"filled")) cp->fill = P_SOLID;
 
483
                else if (streq(line,"unfilled")) { /* no-op */ }
 
484
                else {
 
485
            fprintf(stderr, "mif_set_style: unsupported style %s - ignoring\n",
 
486
                line); 
 
487
        }
 
488
                cp->style_was_set = TRUE;
 
489
        }
 
490
        if (cp->style_was_set) mif_style(cp);
 
491
}
 
492
 
 
493
static char *
 
494
mif_string(char *s)
 
495
{
 
496
        static char     *buf = NULL;
 
497
        static int      bufsize = 0;
 
498
        int             pos = 0;
 
499
        char            *p, esc;
 
500
 
 
501
        if (!buf) {
 
502
                bufsize = 64;
 
503
                buf = malloc(bufsize);
 
504
        }
 
505
        
 
506
        p = buf;
 
507
        while (*s) {
 
508
                if (pos > (bufsize-8)) {
 
509
                        bufsize *= 2;
 
510
                        buf = realloc(buf,bufsize);
 
511
                        p = buf + pos;
 
512
                }
 
513
                esc = 0;
 
514
                switch (*s) {
 
515
                        case '\t':      esc = 't'; break;
 
516
                        case '>': case '\'': case '`': case '\\': esc = *s; break;
 
517
                }
 
518
                if (esc) {
 
519
                        *p++ = '\\';
 
520
                        *p++ = esc;
 
521
                        pos += 2;
 
522
                }
 
523
                else {
 
524
                        *p++ = *s;
 
525
                        pos++;
 
526
                }
 
527
                s++;
 
528
        }
 
529
        *p = '\0';
 
530
        return buf;
 
531
}
 
532
 
 
533
static void mif_textline(point  p, textline_t *line)
 
534
{
 
535
        pointf  mp;
 
536
        char    *str = line->str;
 
537
        char    *anchor;
 
538
 
 
539
        mp.x = p.x;
 
540
        mp.y = p.y - cstk[SP].fontsz/2 + 2;
 
541
        switch(line->just) {
 
542
                case 'l':
 
543
                        anchor="Left";
 
544
                        break;
 
545
                case 'r':
 
546
                        anchor="Right";
 
547
                        break;
 
548
                default:
 
549
                case 'n':
 
550
                        anchor="Center";
 
551
                        break;
 
552
        }
 
553
        mp = mifpt(mp);
 
554
        fprintf(Outfile,
 
555
                "<TextLine <Angle %d> <TLOrigin %.2f %.2f> <TLAlignment %s>",
 
556
                Rot,mp.x,mp.y,anchor);
 
557
        fprintf(Outfile," <String `%s'>>\n",mif_string(str));
 
558
}
 
559
 
 
560
static void mif_bezier(point* A, int n, int arrow_at_start, int arrow_at_end)
 
561
{
 
562
        fprintf(Outfile,"<PolyLine <Fill 15> <Smoothed Yes> <HeadCap Square>\n");
 
563
        mifptarray(A,n);
 
564
        fprintf(Outfile,">\n");
 
565
}
 
566
 
 
567
static void mif_polygon(point *A, int n, int filled)
 
568
{
 
569
        fprintf(Outfile,"<Polygon %s\n",(filled? FillStr : NoFillStr));
 
570
        mifptarray(A,n);
 
571
        fprintf(Outfile,">\n");
 
572
}
 
573
 
 
574
static void mif_ellipse(point p, int rx, int ry, int filled)
 
575
{
 
576
        pointf          tl,mp;
 
577
        tl.x = p.x - rx; tl.y = p.y + ry;
 
578
        if (Rot) {int t; t = rx; rx = ry; ry = t;}
 
579
        mp = mifpt(tl);
 
580
        fprintf(Outfile,"<Ellipse %s <BRect %.2f %.2f %.1f %.1f>>\n",
 
581
                filled?FillStr:NoFillStr,
 
582
                mp.x,mp.y,Scale*(rx + rx),Scale*(ry + ry));
 
583
}
 
584
 
 
585
static void mif_polyline(point* A, int n)
 
586
{
 
587
        fprintf(Outfile,"<PolyLine <HeadCap Square>\n");
 
588
        mifptarray(A,n);
 
589
        fprintf(Outfile,">\n");
 
590
}
 
591
 
 
592
static void mif_user_shape(char *name, point *A, int n, int filled)
 
593
{
 
594
        static boolean onetime = TRUE;
 
595
        if (onetime) {fprintf(stderr,"custom shapes not available with this driver\n"); onetime = FALSE;}
 
596
        mif_polygon(A,n,filled);
 
597
}
 
598
 
 
599
codegen_t       MIF_CodeGen = {
 
600
        mif_reset,
 
601
        mif_begin_job, mif_end_job,
 
602
        mif_begin_graph, mif_end_graph,
 
603
        mif_begin_page, mif_end_page,
 
604
        mif_begin_cluster, mif_end_cluster,
 
605
        mif_begin_nodes, mif_end_nodes,
 
606
        mif_begin_edges, mif_end_edges,
 
607
        mif_begin_node, mif_end_node,
 
608
        mif_begin_edge, mif_end_edge,
 
609
        mif_begin_context, mif_end_context,
 
610
        mif_set_font, mif_textline,
 
611
        mif_set_color, mif_set_color, mif_set_style,
 
612
        mif_ellipse, mif_polygon,
 
613
        mif_bezier, mif_polyline,
 
614
        0 /* mif_arrowhead */, mif_user_shape,
 
615
        0 /* mif_comment */, 0 /* mif_textsize */
 
616
};