summaryrefslogtreecommitdiffstats
path: root/libmscgen/mscgen_ps_out.c
blob: 32f9eae48ef37c5db2628112db800ff1d234c043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/***************************************************************************
 *
 * $Id: ps_out.c 186 2011-03-01 21:18:19Z Michael.McTernan $
 *
 * This file is part of mscgen, a message sequence chart renderer.
 * Copyright (C) 2005 Michael C McTernan, Michael.McTernan.2001@cs.bris.ac.uk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 **************************************************************************/

#include "mscgen_config.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "mscgen_adraw_int.h"
#include "mscgen_utf8.h"
#include "mscgen_safe.h"

/***************************************************************************
 * Manifest Constants
 ***************************************************************************/

/** Overall scaling of the Postscript output.
 */
#define PS_OUT_SCALE  0.7f

/***************************************************************************
 * Local types
 ***************************************************************************/

typedef struct PsContextTag
{
    /** Output file. */
    FILE        *of;

    /** Point size of the current font. */
    int          fontPoints;

    /** Current pen colour. */
    ADrawColour  penColour;

    /** Background colour for the pen. */
    ADrawColour  penBgColour;
}
PsContext;

typedef struct
{
    int capheight, xheight, ascender, descender;
    int widths[256];
}
PsCharMetric;

/** Helvetica character widths.
 * This gives the width of each character is 1/1000ths of a point.
 * The values are taken from the Adobe Font Metric file for Hevletica.
 */
static const PsCharMetric PsHelvetica =
{
    718, 523, 718, -207,
    {
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
      278,  278,  355,  556,  556,  889,  667,  222,
      333,  333,  389,  584,  278,  333,  278,  278,
      556,  556,  556,  556,  556,  556,  556,  556,
      556,  556,  278,  278,  584,  584,  584,  556,
     1015,  667,  667,  722,  722,  667,  611,  778,
      722,  278,  500,  667,  556,  833,  722,  778,
      667,  778,  722,  667,  611,  722,  667,  944,
      667,  667,  611,  278,  278,  278,  469,  556,
      222,  556,  556,  500,  556,  556,  278,  556,
      556,  222,  222,  500,  222,  833,  556,  556,
      556,  556,  333,  500,  278,  556,  500,  722,
      500,  500,  500,  334,  260,  334,  584,   -1,
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1,  333,  556,  556,  167,  556,  556,  556,
      556,  191,  333,  556,  333,  333,  500,  500,
       -1,  556,  556,  556,  278,   -1,  537,  350,
      222,  333,  333,  556, 1000, 1000,   -1,  611,
       -1,  333,  333,  333,  333,  333,  333,  333,
      333,   -1,  333,  333,   -1,  333,  333,  333,
     1000,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
       -1, 1000,   -1,  370,   -1,   -1,   -1,   -1,
      556,  778, 1000,  365,   -1,   -1,   -1,   -1,
       -1,  889,   -1,   -1,   -1,  278,   -1,   -1,
      222,  611,  944,  611,   -1,   -1,   -1,   -1
    }
};

/***************************************************************************
 * Helper functions
 ***************************************************************************/


/** Get the context pointer from an ADraw structure.
 */
static PsContext *getPsCtx(struct ADrawTag *ctx)
{
    return (PsContext *)ctx->internal;
}

/** Get the context pointer from an ADraw structure.
 */
static FILE *getPsFile(struct ADrawTag *ctx)
{
    return getPsCtx(ctx)->of;
}

/** Given a font metric measurement, return device dependent units.
 * Font metric data is stored as 1/1000th of a point, and therefore
 * needs to be multiplied by the font point size and divided by
 * 1000 to give a value in device dependent units.
 */
static int getSpace(struct ADrawTag *ctx, long thousanths)
{
    return ((thousanths * getPsCtx(ctx)->fontPoints) + 500) / 1000;
}

/** Write out a line of text, escaping special characters.
 */
static void writeEscaped(struct ADrawTag *ctx, const char *string)
{
    FILE *f = getPsFile(ctx);

    while(*string != '\0')
    {
        unsigned int code, bytes;

        switch(*string)
        {
            case '(': fprintf(f, "\\("); break;
            case ')': fprintf(f, "\\)"); break;
            default:
                if(Utf8Decode(string, &code, &bytes))
                {
                    fprintf(f, "\\%o", code);
                    string += bytes - 1;
                }
                else
                {
                    fputc(*string, f);
                }
                break;
        }

        string++;
    }
}

static void setColour(struct ADrawTag *ctx,
                      ADrawColour      col)
{
    float r, g, b;

    /* Extract RGB values */
    r = (float)((col & 0xff0000) >> 16);
    g = (float)((col & 0x00ff00) >>  8);
    b = (float)((col & 0x0000ff) >>  0);

    /* Normalise */
    r /= 255.0f;
    g /= 255.0f;
    b /= 255.0f;

    /* Generate output command */
    fprintf(getPsFile(ctx), "%f %f %f setrgbcolor\n", r ,g ,b);
}

/***************************************************************************
 * API Functions
 ***************************************************************************/

unsigned int PsTextWidth(struct ADrawTag *ctx,
                          const char *string)
{
    unsigned long width = 0;

    while(*string != '\0')
    {
        int           i = *string & 0xff;
        unsigned long w = PsHelvetica.widths[i];

        /* Ignore undefined characters */
        width += w > 0 ? w : 0;

        string++;
    }

    return getSpace(ctx, width);
}


int PsTextHeight(struct ADrawTag *ctx)
{
    return getSpace(ctx, PsHelvetica.ascender - PsHelvetica.descender);
}


void PsLine(struct ADrawTag *ctx,
             unsigned int     x1,
             unsigned int     y1,
             unsigned int     x2,
             unsigned int     y2)
{
    fprintf(getPsFile(ctx),
            "newpath %d %d moveto %d %d lineto stroke\n",
            x1, 0-y1, x2, 0-y2);

}


void PsDottedLine(struct ADrawTag *ctx,
                   unsigned int     x1,
                   unsigned int     y1,
                   unsigned int     x2,
                   unsigned int     y2)
{
    fprintf(getPsFile(ctx), "[2] 0 setdash\n");
    PsLine(ctx, x1, y1, x2, y2);
    fprintf(getPsFile(ctx), "[] 0 setdash\n");
}


void PsFilledRectangle(struct ADrawTag *ctx,
                       unsigned int     x1,
                       unsigned int     y1,
                       unsigned int     x2,
                       unsigned int     y2)
{
    fprintf(getPsFile(ctx),
            "newpath "
            "%d %d moveto "
            "%d %d lineto "
            "%d %d lineto "
            "%d %d lineto "
            "closepath "
            "fill\n",
            x1, 0-y1,
            x2, 0-y1,
            x2, 0-y2,
            x1, 0-y2);
}


void PsTextR(struct ADrawTag *ctx,
              unsigned int     x,
              unsigned int     y,
              const char      *string)
{
    PsContext *context = getPsCtx(ctx);

    /* Push the string and get its width */
    fprintf(getPsFile(ctx), "(");
    writeEscaped(ctx, string);
    fprintf(getPsFile(ctx), ") dup stringwidth\n");

    /* Draw the background box */
    setColour(ctx, context->penBgColour);
    fprintf(getPsFile(ctx), "pop "                /* Ignore y-value */
                            "dup "                /* Duplicate string width */
                            "newpath "
                            "%d %d moveto "       /* Bottom left of the box */
                            "0 rlineto "          /* Move to bottom right of the box */
                            "0 %d rlineto "       /* To top right */
                            "neg 0 rlineto "      /* Back to bottom left */
                            "closepath fill\n",   /* Done */
                            x, 0-y - getSpace(ctx, PsHelvetica.descender),
                            getSpace(ctx, PsHelvetica.ascender));

    /* Restore pen and show the string */
    setColour(ctx, context->penColour);
    fprintf(getPsFile(ctx), "%d %d moveto show\n",
                             x, 0-y - getSpace(ctx, PsHelvetica.descender));
}


void PsTextL(struct ADrawTag *ctx,
              unsigned int     x,
              unsigned int     y,
              const char      *string)
{
    PsContext *context = getPsCtx(ctx);

    /* Draw the background box */
    setColour(ctx, context->penBgColour);
    PsFilledRectangle(ctx, x, 0-y, x + 10, 0-y + 10);
    setColour(ctx, context->penColour);

    fprintf(getPsFile(ctx),
            "%d %d moveto "
            "(",
            x, 0-y - getSpace(ctx, PsHelvetica.descender));
    writeEscaped(ctx, string);
    fprintf(getPsFile(ctx),
            ") dup stringwidth "
            "pop "  /* Ignore y value */
            "neg "  /* Invert x value */
            "0 "
            "rmoveto "
            "show\n");
}


void PsTextC(struct ADrawTag *ctx,
              unsigned int     x,
              unsigned int     y,
              const char      *string)
{
    PsContext *context = getPsCtx(ctx);

    /* Push the string and get its width */
    fprintf(getPsFile(ctx), "(");
    writeEscaped(ctx, string);
    fprintf(getPsFile(ctx), ") dup stringwidth\n");

    /* Draw the background box */
    setColour(ctx, context->penBgColour);
    fprintf(getPsFile(ctx), "pop "                     /* Ignore y-value */
                            "dup dup "                 /* Duplicate string width twice */
                            "newpath "
                            "%d %d moveto "            /* Starting point, centre bottom of box */
                            "2 div neg 0 rmoveto "     /* Move to bottom left */
                            "0 rlineto "               /* Move to bottom right of the box */
                            "0 %d rlineto "            /* To top right */
                            "neg 0 rlineto "           /* Back to bottom left */
                            "closepath fill\n",        /* Done */
                            x, 0-y,
                            getSpace(ctx, PsHelvetica.ascender));

    /* Restore pen and show the string */
    setColour(ctx, context->penColour);
    fprintf(getPsFile(ctx), "%d %d moveto dup stringwidth pop 2 div neg 0 rmoveto show\n",
                             x, 0-y - getSpace(ctx, PsHelvetica.descender));
}


void PsFilledTriangle(struct ADrawTag *ctx,
                       unsigned int x1,
                       unsigned int y1,
                       unsigned int x2,
                       unsigned int y2,
                       unsigned int x3,
                       unsigned int y3)
{
    fprintf(getPsFile(ctx),
            "newpath "
            "%d %d moveto "
            "%d %d lineto "
            "%d %d lineto "
            "closepath "
            "fill\n",
            x1, 0-y1,
            x2, 0-y2,
            x3, 0-y3);
}


void PsFilledCircle(struct ADrawTag *ctx,
                     unsigned int x,
                     unsigned int y,
                     unsigned int r)
{
    fprintf(getPsFile(ctx),
            "newpath "
            "%d %d %d 0 360 arc "
            "closepath "
            "fill\n",
            x, 0-y, r);
}


void PsArc(struct ADrawTag *ctx,
            unsigned int cx,
            unsigned int cy,
            unsigned int w,
            unsigned int h,
            unsigned int s,
            unsigned int e)
{
    fprintf(getPsFile(ctx),
            "newpath "
            "%d %d %d %d %d %d ellipse "
            "stroke\n",
            cx, 0-cy, w, h, s, e);
}


void PsDottedArc(struct ADrawTag *ctx,
                  unsigned int cx,
                  unsigned int cy,
                  unsigned int w,
                  unsigned int h,
                  unsigned int s,
                  unsigned int e)
{
    fprintf(getPsFile(ctx), "[2] 0 setdash\n");
    PsArc(ctx, cx, cy, w, h, s, e);
    fprintf(getPsFile(ctx), "[] 0 setdash\n");
}


void PsSetPen(struct ADrawTag *ctx,
              ADrawColour      col)
{
    PsContext *context = getPsCtx(ctx);

    assert(col != ADRAW_COL_INVALID);

    /* Check if the pen colour has changed */
    if(context->penColour != col)
    {
        setColour(ctx, col);

        /* Store the pen colour */
        context->penColour = col;
    }
}


void PsSetBgPen(struct ADrawTag *ctx,
                ADrawColour      col)
{
    PsContext *context = getPsCtx(ctx);

    context->penBgColour = col;
}


void PsSetFontSize(struct ADrawTag *ctx,
                   ADrawFontSize    size)
{
    PsContext *context = getPsCtx(ctx);

    switch(size)
    {
        case ADRAW_FONT_TINY:
            getPsCtx(ctx)->fontPoints = 8;
            break;

        case ADRAW_FONT_SMALL:
            getPsCtx(ctx)->fontPoints = 12;
            break;

        default:
            assert(0);
    }

    fprintf(context->of, "/Helvetica findfont\n");
    fprintf(context->of, "%d scalefont\n", getPsCtx(ctx)->fontPoints);
    fprintf(context->of, "setfont\n");
}


Boolean PsClose(struct ADrawTag *ctx)
{
    PsContext *context = getPsCtx(ctx);

    /* Close the output file */
    if(context->of != stdout)
    {
        fclose(context->of);
    }

    /* Free and destroy context */
    free(context);
    ctx->internal = NULL;

    return TRUE;
}



Boolean PsInit(unsigned int     w,
               unsigned int     h,
               const char      *file,
               struct ADrawTag *outContext)
{
    PsContext *context;

    /* Create context */
    context = outContext->internal = malloc_s(sizeof(PsContext));
    if(context == NULL)
    {
        return FALSE;
    }

    /* Open the output file */
    if(strcmp(file, "-") == 0)
    {
        context->of = stdout;
    }
    else
    {
        context->of = fopen(file, "wb");
        if(!context->of)
        {
            fprintf(stderr, "PsInit: Failed to open output file '%s': %s\n", file, strerror(errno));
            return FALSE;
        }
    }

    /* Write the header */
    fprintf(context->of, "%%!PS-Adobe-3.0 EPSF-2.0\n"
                         "%%%%BoundingBox: 0 0 %.0f %.0f\n", w * PS_OUT_SCALE, h * PS_OUT_SCALE);
    fprintf(context->of, "%%%%Creator: mscgen %s\n", PACKAGE_VERSION);
    fprintf(context->of, "%%%%EndComments\n");

    /* Shrink everything by 70% */
    fprintf(context->of, "%f %f scale\n", PS_OUT_SCALE, PS_OUT_SCALE);

    /* Create clipping rectangle to constrain dimensions */
    fprintf(context->of, "0 0 moveto\n");
    fprintf(context->of, "0 %u lineto\n", h);
    fprintf(context->of, "%u %u lineto\n", w, h);
    fprintf(context->of, "%u 0 lineto\n", w);
    fprintf(context->of, "closepath\n");
    fprintf(context->of, "clip\n");
    fprintf(context->of, "%%PageTrailer\n");
    fprintf(context->of, "%%Page: 1 1\n");

    /* Set default font */
    fprintf(context->of, "/Helvetica findfont\n");
    fprintf(context->of, "10 scalefont\n");
    fprintf(context->of, "setfont\n");

    /* Get the default font size */
    PsSetFontSize(outContext, ADRAW_FONT_SMALL);

    /* Translate up by the height, y-axis will be inverted */
    fprintf(context->of, "0 %d translate\n", h);

    /* Arc drawing function */
    fprintf(context->of, "/mtrx matrix def\n"
                         "/ellipse\n"
                         "  { /endangle exch def\n"
                         "    /startangle exch def\n"
                         "    /ydia exch def\n"
                         "    /xdia exch def\n"
                         "    /y exch def\n"
                         "    /x exch def\n"
                         "    /savematrix mtrx currentmatrix def\n"
                         "    x y translate\n"
                         "    xdia 2 div ydia 2 div scale\n"
                         "    1 -1 scale\n"
                         "    0 0 1 startangle endangle arc\n"
                         "    savematrix setmatrix\n"
                         "} def\n");

    /* Set the current pen colours */
    context->penColour = ADRAW_COL_BLACK;
    context->penBgColour = ADRAW_COL_WHITE;

    /* Now fill in the function pointers */
    outContext->line            = PsLine;
    outContext->dottedLine      = PsDottedLine;
    outContext->textL           = PsTextL;
    outContext->textC           = PsTextC;
    outContext->textR           = PsTextR;
    outContext->textWidth       = PsTextWidth;
    outContext->textHeight      = PsTextHeight;
    outContext->filledRectangle = PsFilledRectangle;
    outContext->filledTriangle  = PsFilledTriangle;
    outContext->filledCircle    = PsFilledCircle;
    outContext->arc             = PsArc;
    outContext->dottedArc       = PsDottedArc;
    outContext->setPen          = PsSetPen;
    outContext->setBgPen        = PsSetBgPen;
    outContext->setFontSize     = PsSetFontSize;
    outContext->close           = PsClose;

    return TRUE;
}

/* END OF FILE */