-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplt.c
150 lines (128 loc) · 2.92 KB
/
plt.c
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
/* Copyright (C) 2024 Lars Brinkhoff <lars@nocrew.org>
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, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <unistd.h>
#include "dis.h"
#include "svg.h"
static void finish(word_t data, void (*fn)(FILE *))
{
if (data != -1)
return;
if (fn != NULL)
fn(stdout);
exit(0);
}
static int sext(int data)
{
data &= 0377777;
data ^= 0200000;
return data - 0200000;
}
static int posx(word_t data)
{
return sext(data >> 19);
}
static int posy(word_t data)
{
return sext(data >> 1);
}
static void (*plot)(FILE *f, int x, int y);
static void vector(word_t data)
{
int x = posx(data);
int y = posy(data);
plot(stdout, x, y);
}
static word_t vectors(FILE *f, word_t data)
{
plot = svg_polyline_begin;
do {
vector(data);
plot = svg_polyline_point;
data = get_word(f);
finish(data, svg_polyline_end);
} while (data & 1);
svg_polyline_end(stdout);
return data;
}
static void ascii(word_t data)
{
int c1, c2, c3, c4, c5;
c1 = (data >> 29) & 0177;
c2 = (data >> 22) & 0177;
c3 = (data >> 15) & 0177;
c4 = (data >> 8) & 0177;
c5 = (data >> 1) & 0177;
if (c1)
svg_text_character(stdout, c1);
if (c2)
svg_text_character(stdout, c2);
if (c3)
svg_text_character(stdout, c3);
if (c4)
svg_text_character(stdout, c4);
if (c5)
svg_text_character(stdout, c5);
}
static word_t text(FILE *f, word_t data)
{
int x = posx(data);
int y = posy(data);
if ((data & 0777777000000LL) == 0400001000000LL)
finish(-1, NULL);
svg_text_begin(stdout, x, y);
data = get_word(f);
finish(data, svg_text_end);
if ((data & 1) == 0) {
svg_text_end(stdout);
return data;
}
if (data & 0777777000000LL) {
printf("Not text\n");
exit(1);
}
for(;;) {
data = get_word(f);
finish(data, svg_text_end);
if (data & 1)
ascii(data);
else {
svg_text_end(stdout);
return data;
}
}
}
static void plt(FILE *f)
{
word_t data;
data = get_word(f);
finish(data, NULL);
for (;;) {
switch (data & 01000001) {
case 00000000:
data = vectors(f, data);
break;
case 01000000:
data = text(f, data);
break;
default:
fprintf(stderr, "Error in input: %012llo\n", data);
exit(1);
}
}
}
int main(void)
{
svg_file_begin(stdout);
plt(stdin);
return 0;
}