summaryrefslogtreecommitdiff
path: root/dviware/dvisirule/src/dvisirule-bin.c
blob: d5c06d419d90a6d4dc9fea048c02a4b227a3cbf5 (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
/*
 * Copyright (C) 2012-2022 Jiro Senju
 *
 * This package 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
 * any later version.
 *
 * This package 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 package.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#elif defined(HAVE_NETINET_IN_H)
#include <netinet/in.h>
#else
/* What can I do? */
#endif

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
/* What can I do? */
#endif

#ifdef HAVE_LIMITS_H
#include <limits.h>
#else
/* What can I do? */
#endif

#include <stdio.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else
/* What can I do? */
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#else
/* What can I do? */
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
/* What can I do? */
#endif

#if HAVE_MALLOC != 1 || HAVE_MEMSET != 1 || HAVE_MMAP != 1 || HAVE_REALLOC != 1
#error malloc/memset/mmap/realloc unimplemented?
#endif

#include "dvi.h"

/* ---------------------------------------------------------------------- */

#if 0
#define Dpri(fmt, ...)	fprintf(stderr, fmt, ##__VA_ARGS__)
#else
#define Dpri(fmt, ...)	do {} while (0)
#endif

#define Errx(cond, fmt, args...) \
	if (cond) \
		errx(errno, "%s:%d: " fmt, __func__, __LINE__, ##args)
#define Err(cond, str) Errx(cond, "%s %m", str)

#define ExpandStep	25

struct dvifile {
	int fd;
	unsigned char *p;
} src, dst;

/* ---------------------------------------------------------------------- */

/* single SETRULE/PUTRULE with its color and coordinate */
union path {
	unsigned char a[64];
	struct {
		int n, color;
		unsigned char p[];
	};
};

#define PathCk(path) \
	Errx(path->n >= sizeof(*path) - sizeof(path->n), "%d >= %lu", \
		path->n, (unsigned long)sizeof(*path) - sizeof(path->n));

static void p_push(union path *path)
{
	unsigned char *p;

	p = path->p + path->n;
	*p = PUSH;
	path->n++;
	PathCk(path);
}

static void p_pop(union path *path)
{
	unsigned char *p;

	p = path->p + path->n;
	*p = POP;
	path->n++;
	PathCk(path);
}

static int dvi_move(unsigned char *p, unsigned char op, int v)
{
	int n;
	union {
		int v;
		unsigned char a[sizeof(int)];
	} u;

	Dpri("%p, 0x%x, %d\n", p, op, v);
	if (!v)
		return 0;

	u.v = htonl(v);
	n = 5;
	*p++ = op + 3;
	memcpy(p, u.a + 0, 4);

	return n;
}

static void p_moveto(union path *path, int h, int v)
{
	int n;

	n = dvi_move(path->p + path->n, RIGHT1, h);
	path->n += n;
	PathCk(path);
	n = dvi_move(path->p + path->n, DOWN1, v);
	path->n += n;
	PathCk(path);
}

static void p_pushcolor(union path *path, unsigned int offset)
{
	//XXX1
	static unsigned char cmd[] = "\xef\x11" "color push gray 0";
	unsigned char *p, *s;
	unsigned int len;

	path->color = path->n;
	p = path->p + path->n;
	if (offset) {
		s = src.p + offset;
		len = 1 + 1 + src.p[offset + 1];
	} else {
		s = cmd;
		len = sizeof(cmd) - 1;
	}
	path->n += len;
	PathCk(path);
	memcpy(p, s, len);
}

static void p_rule(union path *path, unsigned int offset)
{
	unsigned char *p;
	const unsigned int len = 1 + 4 + 4;

	p = path->p + path->n;
	path->n += len;
	PathCk(path);
	/* no setrule since it moves the current-point */
	*p++ = PUTRULE;
	memcpy(p, src.p + offset + 1, len - 1);
}

static void p_popcolor(union path *path)
{
	//XXX1
	static char cmd[] = "\xef\x09" "color pop";
	const unsigned int len = sizeof(cmd) - 1;
	unsigned char *p;

	p = path->p + path->n;
	path->n += len;
	PathCk(path);
	memcpy(p, cmd, len);
}

/* ---------------------------------------------------------------------- */

/* all SETRULE/PUTRULE in a page */
struct page {
	unsigned int total, cur;
	int h, v;
	/* to be qsort-able, use array instead of list */
	union path **path;
};

static int page_init(struct page *page)
{
	page->total = ExpandStep;
	page->cur = 0;
	page->path = malloc(sizeof(page->path) * page->total);
	return !!page->path;
}

static void page_reinit(struct page *page)
{
	page->cur = 0;
}

static int path_append(struct page *page, union path *path)
{
	union path **p;

	if (page->cur + 1 >= page->total) {
		/* expand */
		page->total += ExpandStep;
		p = realloc(page->path, sizeof(page->path) * page->total);
		Err(!p, "realloc");
		page->path = p;
	}
	page->path[page->cur++] = path;

	return 0;
}

static int color_sort(const void *__a, const void *__b)
{
	const union path **_a = (const union path **)__a,
		**_b = (const union path **)__b;
	const union path *a = *_a, *b = *_b;
	char ca[32], cb[32];
	int l;

	l = a->p[a->color + 1];
	memcpy(ca, a->p + a->color + 2, l);
	ca[l] = 0;

	l = b->p[b->color + 1];
	memcpy(cb, b->p + b->color + 2, l);
	cb[l] = 0;

	l = strcmp(ca, cb);
	if (l > 0)
		l = -1;
	else if (l < 0)
		l = 1;
	return l;
}

static void page_write(int fd, struct page *page)
{
	int i, n;
	union path *path, origin;
	ssize_t ssz;

	origin.n = 0;
	p_push(&origin);
	p_moveto(&origin, -page->h, -page->v);
	ssz = write(fd, origin.p, origin.n);
	Err(ssz != origin.n, "write");

	n = page->cur;
	qsort(page->path, n, sizeof(*page->path), color_sort);
	for (i = 0; i < n; i++) {
		path = page->path[i];
		ssz = write(fd, path->p, path->n);
		Err(ssz != path->n, "write");
	}

	origin.n = 0;
	p_pop(&origin);
	ssz = write(fd, origin.p, origin.n);
	Err(ssz != origin.n, "write");
}

/* ---------------------------------------------------------------------- */

static void postamble(unsigned int last, unsigned int ofbop, unsigned int srcsz)
{
	unsigned int post, prev;
	ssize_t ssz;
	int n;

	Errx(src.p[last] != POST, "last %u, 0x%x expects 0x%x",
	     last, src.p[last], POST);
	Dpri("last %u, 0x%x\n", last, last);
	post = lseek(dst.fd, 0, SEEK_CUR);
	ssz = write(dst.fd, src.p + last, 1);
	Err(ssz != 1, "write");
	last++;
	ofbop = htonl(ofbop);
	ssz = write(dst.fd, &ofbop, 4);
	Err(ssz != 4, "write");
	last += 4;
	ssz = write(dst.fd, src.p + last, 4 * 6);
	Err(ssz != 4 * 6, "write");
	last += 4 * 6;

	prev = last;
	while (src.p[last] != POSTPOST) {
		n = 0;
		switch (src.p[last]) {
		case FNTDEF4:
			n++;
		case FNTDEF3:
			n++;
		case FNTDEF2:
			n++;
		case FNTDEF1:
			n++;
			break;
		default:
			Errx(src.p[last], "0x%x at %u(0x%x)",
			     src.p[last], last, last);
		}
		Dpri("last %u(0x%x), n %d\n", last, last, n);
		n += 4 * 3 + 1 + 1;
		n += src.p[last + n] + 1;
		Dpri("n %d\n", n);
		last += n;
	}
	ssz = write(dst.fd, src.p + prev, last - prev);
	Err(ssz != last - prev, "write");

	/* post_post */
	Dpri("last %u, 0x%x\n", last, last);
	Errx(src.p[last] != POSTPOST, "last %u, 0x%x expects 0x%x",
	     last, src.p[last], POSTPOST);
	ssz = write(dst.fd, src.p + last, 1);
	Err(ssz != 1, "write");
	last++;
	post = htonl(post);
	ssz = write(dst.fd, &post, 4);
	Err(ssz != 4, "write");
	last += 4;
	ssz = write(dst.fd, src.p + last, srcsz - last);
	Err(ssz != srcsz - last, "write");
}

/*
 * in dvi, all offsets are represented in 32 bits.
 * it means no 'long', 'long long', or 'loff_t' are necessary.
 */

int main(int argc, char *argv[])
{
	int e, n, h, v;
	unsigned int height, width, hpx, wpx, rule_op, last;
	/* offsets */
	unsigned int ofbop, ofcolor, ofrule, ofeop, ofline;
	float gray;
	char cmd[BUFSIZ], op[BUFSIZ], colorpush[64], *cmdp, c;
	struct stat st;
	union path *path;
	struct page page;
	ssize_t ssz;

	src.fd = open(argv[1], O_RDWR);
	Err(src.fd < 0, argv[1]);

	last = 0;
	e = fstat(src.fd, &st);
	Err(e, argv[1]);
	src.p = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
		     src.fd, 0);
	Err(src.p == MAP_FAILED, argv[1]);

	dst.fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
	Err(dst.fd < 0, argv[2]);

	page_init(&page);
	while (1) {
		/* read the output of marker.awk */
		cmdp = fgets(cmd, sizeof(cmd), stdin);
		if (!cmdp)
			break;

		if (*cmdp == 'h') {
			/*
			 * depends on the TeX version or variant?
			 * h=-31 v=176 <offset>:{239}color_push_gray_0
			 * <offset>: putrule {137} height 26214, width 2290850
			 * (2x146 pixels)
			 */
			n = sscanf(cmdp, "h=%d v=%d "
				   "%u:{239}color_push_%s "
				   "%u: %s {%u} height %u, width %u "
				   "(%ux%u pixels)",
				   &h, &v,
				   &ofcolor, colorpush,
				   &ofrule, op, &rule_op,
				   &height, &width,
				   &hpx, &wpx);
			if (n != 11)
				n = sscanf(cmdp, "h=%d v=%d "
					   "%u:{239}pdf:bcolor [%f] "
					   "%u: %s {%u} height %u, width %u "
					   "(%ux%u pixels)",
					   &h, &v,
					   &ofcolor, &gray,
					   &ofrule, op, &rule_op,
					   &height, &width,
					   &hpx, &wpx);
			Errx(n != 11, "n %d, %s", n, cmdp);

			path = malloc(sizeof(*path));
			Err(!path, "malloc");

			path->n = 0;
			p_push(path);
			p_moveto(path, h, v);
			p_pushcolor(path, ofcolor);
			p_rule(path, ofrule);
			p_popcolor(path);
			p_pop(path);
			path_append(&page, path);
		} else if (!strncmp(cmdp, "bop", 3)) {
			/* bop <offset>: beginning of page 1 */
			n = sscanf(cmdp, "bop %u: beginning %s",
				   &ofbop, op);
			Errx(n != 2, "n %d, %s", n, cmdp);
		} else if (!strncmp(cmdp, "bol", 3)
			   || !strncmp(cmdp, "eol", 3)) {
			/* bol <offset>: xxx ' {239}sirule BOL' */
			/* eol <offset>: xxx ' {239}sirule EOL' */
			n = sscanf(cmdp, "%col %u: %s",
				   &c, &ofline, op);
			Errx(n != 3, "n %d, %s", n, cmdp);
			memset(src.p + ofline, NOP, src.p[ofline + 1] + 2);
		} else {
			/* <offset>: eop {140}  h=0 v=2206 */
			n = sscanf(cmdp, "%u: eop {140}  h=%d v=%d",
				   &ofeop, &page.h, &page.v);
			Errx(n != 3, "n %d, %s", n, cmdp);

			Dpri("last %u\n", last);
			ssz = write(dst.fd, src.p + last, ofeop);
			Errx(ssz != ofeop, "write, %p, %u, %u",
			     src.p, last, ofeop);
			last = ofeop;
			Dpri("last %u, 0x%x\n", last, last);
			page_write(dst.fd, &page);
			ssz = write(dst.fd, src.p + last, 1);
			Err(ssz != 1, "write");
			last++;
			page_reinit(&page);
		}
	}

	postamble(last, ofbop, st.st_size);

	return 0;
}