File: ps.c

package info (click to toggle)
acm 6.0%2B20200416-1.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,972 kB
  • sloc: ansic: 49,164; tcl: 941; makefile: 644; sh: 594
file content (282 lines) | stat: -rw-r--r-- 5,662 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "../../src/util/error.h"
#include "../../src/util/memory.h"

#define ps_IMPORT
#include "ps.h"

struct ps_Type {
	
	/**
	 * Current line width (pt). Here we check the current set value to avoid
	 * redundant changes of the setting, making the generated PS file shorter.
	 */
	double curr_line_width;

	/**
	 * Current gray level [0,1]. 0 = black, 1 = white.  Here we check the
	 * current set value to avoid redundant changes of the setting, making the
	 * generated PS file shorter.
	 */
	double curr_gray_level;
};


ps_Type * ps_new()
{
	ps_Type * this;

	this = memory_allocate(sizeof(ps_Type), NULL);
	this->curr_gray_level = 0;
	this->curr_line_width = 1;
	
	printf("%%!PS-Adobe-2.0\n");
	
	return this;
}


void ps_setClipRect(ps_Type *this, double x, double y, double w, double h)
{
	printf("newpath\n");
	ps_moveTo(this, x, y);
	ps_relativeLineTo(this, w, 0);
	ps_relativeLineTo(this, 0, h);
	ps_relativeLineTo(this, -w, 0);
	ps_relativeLineTo(this, 0, -h);
	printf("closepath\n");
	printf("clip\n");
}


void ps_setFontHeight(ps_Type *this, int h)
{
	printf("/Helvetica findfont %d scalefont setfont\n", h);
}


void ps_setLineWidth(ps_Type *this, double w)
{
	w = (int)(8 * w + 0.5) / 8.0;
	if( w == this->curr_line_width )
		return;
	this->curr_line_width = w;
	printf("%.1f setlinewidth\n", w);
}


void ps_moveTo(ps_Type *this, double x, double y)
{
	printf("%.1f %.1f moveto\n", x, y);
}


/*
void rmoveto(ps_Type *this, double x, double y)
{
	printf("%.1f %.1f rmoveto\n", x, y);
}
*/


void ps_stroke(ps_Type *this)
{
	printf("stroke\n");
}


void ps_lineTo(ps_Type *this, double x, double y)
{
	printf("%.1f %.1f lineto\n", x, y);
}


void ps_drawLine(ps_Type *this, double x1, double y1, double x2, double y2)
{
	ps_moveTo(this, x1, y1);
	ps_lineTo(this, x2, y2);
	printf("stroke\n");
}


void ps_relativeLineTo(ps_Type *this, double x, double y)
{
	printf("%.1f %.1f rlineto\n", x, y);
}


void ps_setGray(ps_Type *this, double brightness)
{
	if( !(0 <= brightness && brightness <= 1.0) )
		error_internal("gray brightness out of the range: %g", brightness);
	brightness = (int)(brightness * 256) / 256.0;
	if( brightness == this->curr_gray_level )
		return;
	this->curr_gray_level = brightness;
	printf("%.3f setgray\n", brightness); 
}


void ps_drawRect(ps_Type *this, double x, double y, double w, double h)
{
	printf("newpath\n");
	printf("%.1f %.1f moveto\n", x, y);
	printf("%.1f 0 rlineto\n", w);
	printf("0 %.1f rlineto\n", h);
	printf("%.1f 0 rlineto\n", -w);
	printf("0 %.1f rlineto\n", -h);
	printf("closepath\n");
	printf("stroke\n");
}


void ps_fillRect(ps_Type *this, double x, double y, double w, double h)
{
	printf("newpath\n");
	printf("%.1f %.1f moveto\n", x, y);
	printf("%.1f 0 rlineto\n", w);
	printf("0 %.1f rlineto\n", h);
	printf("%.1f 0 rlineto\n", -w);
	printf("0 %.1f rlineto\n", -h);
	printf("closepath\n");
	printf("fill\n");
}


/**
 * Draws a rectangular white box with black border.
 */
void ps_drawFramedRect(ps_Type *this, double x, double y, double w, double h)
{
	ps_setGray(this, 1.0);
	ps_fillRect(this, x, y, w, h);
	ps_setGray(this, 0.0);
	ps_setLineWidth(this, 1);
	ps_moveTo(this, x, y);
	ps_drawRect(this, x, y, w, h);
	//printf("stroke\n");
}


void ps_drawCircumference(ps_Type *this, double x, double y, double r)
{
	printf("newpath\n");
	printf("%.1f %.1f %.1f 0 360 arc\n", x, y, r);
	printf("closepath\n");
	printf("stroke\n");
}


void ps_drawCircle(ps_Type *this, double x, double y, double r)
{
	printf("newpath\n");
	printf("%.1f %.1f %.1f 0 360 arc\n", x, y, r);
	printf("closepath\n");
	printf("fill\n");
}


/*
static int indexOfChar(char *s, int c)
{
	char *found = strchr(s, c);
	if( found == NULL )
		return -1;
	else
		return found - s;
}
*/


static int indexOfSubstring(char *s, char *target)
{
	char *found = strstr(s, target);
	if( found == NULL )
		return -1;
	else
		return found - s;
}


/**
 * Print ASCII printable only string properly PostScript escaped.
 * @param s
 */
void ps_printEscapedString(ps_Type *this, char *s)
{
	putchar('(');
	while(*s){
		char c = *s;
		if( c == '(' ){
			puts("\\(");
		} else if( c == ')' ){
			puts("\\)");
		} else if( c == '\\' ){
			puts("\\");
		} else if( 32 <= c && c <= 126 ){
			putchar(c);
		} else {
			fprintf(stderr, "Warning: non-ASCII printable code %d in string\n", c & 255);
			putchar('?');
		}
		s++;
	}
	printf(") show\n");
}


typedef struct {
	char *name;
	char *glyph;
} ratnest_CharEntity;


static ratnest_CharEntity entities[] = {
	{"&degree;", "/degree"},
	{"&multiply;", "/multiply"},
	{NULL, NULL}
};


/**
 * Draws a string at the current location. To display the degree symbol,
 * caller may use the pseudo-HTML entity "&degree;" instead which is the
 * only one handled specially.
 * @param s ASCII printable chars only supported.
 */
void ps_drawString(ps_Type *this, char *s)
{
	while( *s != 0 ){
		int found_index = -1;
		ratnest_CharEntity *found_entity = entities;
		while(found_entity->name != NULL){
			found_index = indexOfSubstring(s, found_entity->name);
			if( found_index >= 0 )
				break;
			found_entity++;
		}
		if( found_index < 0 ){
			ps_printEscapedString(this, s);
			return;
		}
		// Print chunk from beginning up to the entity (excluded):
		s[found_index] = 0;
		//ps_printEscapedString(this, s);
		ps_drawString(this, s); // recursive!
		s[found_index] = found_entity->name[0];
		// Print the symbol:
		printf("%s glyphshow\n", found_entity->glyph);
		// Move past the entity:
		s = s + found_index + strlen(found_entity->name);
	}
}


void ps_close(ps_Type *this)
{
	printf("showpage\n");
}