aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/grap/label.c
blob: e788dd9d9d21102ec53f62e8955cd5e7e489e0c6 (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
#include <stdio.h>
#include <string.h>
#include "grap.h"
#include "y.tab.h"

int	pointsize	= 10;	/* assumed pointsize to start */
int	ps_set		= 0;	/* someone has set pointsize explicitly */

double	textht	= 1.0/6.0;	/* 6 lines/inch */
double	textwid = 1;		/* width of text box for vertical */

double	lab_up	= 0.0;		/* extra motion for label */
double	lab_rt	= 0.0;		/* extra motion for label */
double	lab_wid	= 0.0;		/* override default width computation */

void labelwid(double amt)
{
	lab_wid = amt + .00001;
}

void labelmove(int dir, double amt)	/* record direction & motion of position corr */
{
	switch (dir) {
	case UP:	lab_up += amt; break;
	case DOWN:	lab_up -= amt; break;
	case LEFT:	lab_rt -= amt; break;
	case RIGHT:	lab_rt += amt; break;
	}
}

void label(int label_side, Attr *stringlist)	/* stick label on label_side */
{
	int m;
	Attr *ap;

	fprintf(tfd, "\ttextht = %g\n", textht);
	if (lab_wid != 0.0) {
		fprintf(tfd, "\ttextwid = %g\n", lab_wid);
		lab_wid = 0;
	} else if (label_side == LEFT || label_side == RIGHT) {
		textwid = 0;
		for (ap = stringlist; ap != NULL; ap = ap->next)
			if ((m = strlen(ap->sval)) > textwid)
				textwid = m;
		textwid /= 15;	/* estimate width at 15 chars/inch */
		fprintf(tfd, "\ttextwid = %g\n", textwid);
	}
	fprintf(tfd, "Label:\t%s", slprint(stringlist));
	freeattr(stringlist);
	switch (label_side) {
	case BOT:
	case 0:
		fprintf(tfd, " with .n at Frame.s - (0,2 * textht)");
		break;
	case LEFT:
		fprintf(tfd, " wid textwid with .e at Frame.w - (0.2,0)");
		break;
	case RIGHT:
		fprintf(tfd, " wid textwid with .w at Frame.e + (0.2,0)");
		break;
	case TOP:
		fprintf(tfd, " with .s at Frame.n + (0,2 * textht)");
		break;
	}
	lab_adjust();
	fprintf(tfd, "\n");
	label_side = BOT;
}

void lab_adjust(void)	/* add a string to adjust labels, ticks, etc. */
{
	if (lab_up != 0.0 || lab_rt != 0.0)
		fprintf(tfd, " + (%g,%g)", lab_rt, lab_up);
}

char *sizeit(Attr *ap)		/* add \s..\s to ap->sval */
{
	int n;
	static char buf[1000];

	if (!ap->op) {	/* no explicit size command */
		if (ps_set) {
			sprintf(buf, "\\s%d%s\\s0", pointsize, ap->sval);
			return buf;
		} else
			return ap->sval;
	} else if (!ps_set) {	/* explicit size but no global size */
		n = (int) ap->fval;
		switch (ap->op) {
		case ' ':	/* absolute size */
			sprintf(buf, "\\s%d%s\\s0", n, ap->sval);
			break;
		case '+':	/* better be only one digit! */
			sprintf(buf, "\\s+%d%s\\s-%d", n, ap->sval, n);
			break;
		case '-':
			sprintf(buf, "\\s-%d%s\\s+%d", n, ap->sval, n);
			break;
		case '*':
		case '/':
			return ap->sval;	/* ignore for now */
		}
		return buf;
	} else {
		/* explicit size and a global background size */
		n = (int) ap->fval;
		switch (ap->op) {
		case ' ':	/* absolute size */
			sprintf(buf, "\\s%d%s\\s0", n, ap->sval);
			break;
		case '+':
			sprintf(buf, "\\s%d%s\\s0", pointsize+n, ap->sval);
			break;
		case '-':
			sprintf(buf, "\\s%d%s\\s0", pointsize-n, ap->sval);
			break;
		case '*':
		case '/':
			return ap->sval;	/* ignore for now */
		}
		return buf;
	}
}