summaryrefslogtreecommitdiff
path: root/utils/hp2ps/Key.c
blob: b7742cf563bb3153110ff113add82e2334abc4ca (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
#include "Main.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "Defines.h"
#include "Dimensions.h"
#include "HpFile.h"
#include "Shade.h"
#include "PsFile.h"
#include "Utilities.h"

/* own stuff */
#include "Key.h"

static void KeyEntry PROTO((floatish, char *, floatish));

void Key(void)
{
    intish i;
    floatish c;
    floatish dc;

    for (i = 0; i < nidents; i++)    /* count identifiers */ 
	;

    c  = multipageflag ? 0 : graphy0;
    dc = graphheight / (floatish) ((i <= 20) ? (i + 1) : 20);

    for (i = 0; i < nidents; i++) {
	c += dc;
	KeyEntry(c, identtable[i]->name, ShadeOf(identtable[i]->name));
        // if we have spit out 20 entries and we're going to output more
        // advance the page
	if (i % DEFAULT_TWENTY == (DEFAULT_TWENTY - 1) && i != nidents - 1) {
	  c = 0;
	  NextPage();
	}
    }
}

static void
escape(char *result, const char *name)
{
    while (*name != '\0')
    {
        // escape parentheses too, because "name" could contain un-matched ones
        if (*name == '\\' || *name == '(' || *name == ')')
        {
            *result++ = '\\';
        }
        *result++ = *name++;
    }
    *result = '\0';
}

static void
KeyEntry(floatish centreline, char *name, floatish colour)
{
    floatish namebase;
    floatish keyboxbase;
    floatish kstart;

    namebase = centreline - (floatish) (NORMAL_FONT / 2);
    keyboxbase = centreline - ((floatish) KEY_BOX_WIDTH / 2.0);

    kstart = graphx0 + (multipageflag ? 0 : graphwidth);

    fprintf(psfp, "%f %f moveto\n", kstart + borderspace, keyboxbase);
    fprintf(psfp, "0 %d rlineto\n", KEY_BOX_WIDTH);
    fprintf(psfp, "%d 0 rlineto\n", KEY_BOX_WIDTH);
    fprintf(psfp, "0 %d rlineto\n", -KEY_BOX_WIDTH);
    fprintf(psfp, "closepath\n");

    fprintf(psfp, "gsave\n"); 
    SetPSColour(colour);
    fprintf(psfp, "fill\n");
    fprintf(psfp, "grestore\n");
    fprintf(psfp, "stroke\n");

    fprintf(psfp, "HE%d setfont\n", NORMAL_FONT);
    fprintf(psfp, "%f %f moveto\n", kstart + (floatish) KEY_BOX_WIDTH + 2 * borderspace, namebase);

    // escape backslashes in 'name'
    char *escaped_name = (char*) xmalloc(strlen(name) * 2 + 1);
    escape(escaped_name, name);
    fprintf(psfp, "(%s) show\n", escaped_name);
    free(escaped_name);
}