-
Notifications
You must be signed in to change notification settings - Fork 4
/
a6cardlabels.c
198 lines (163 loc) · 4.57 KB
/
a6cardlabels.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
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
/* a6cardlabels --- plot labelling for A6 postcards 2020-08-23 */
/* Copyright (c) 2020 John Honniball, Froods Software Development */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "hpgllib.h"
void postcard_port(const double x, const double y, const double wd, const double ht, const double maxx, const double maxy);
void postcard_land(const double x, const double y, const double wd, const double ht, const double maxx, const double maxy);
int main(int argc, char * const argv[])
{
int opt;
int i, j;
bool portrait;
double x, y;
double dx, dy;
int nx, ny;
double maxx, maxy;
double trueWidthmm;
double trueHeightmm;
double borderx;
double bordery;
PlotInfo_t info;
while ((opt = getopt(argc, argv, "no:p:q:s:t:v:")) != -1) {
switch (opt) {
case 's':
case 'n':
case 'o':
case 'p':
case 'q':
case 'v':
plotopt(opt, optarg);
break;
case 't': /* Suppress title, which would spoil the lower-left postcard */
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-p pen] [-q plotter] [-s <size>] [-t title]\n", argv[0]);
fprintf(stderr, " <size> ::= A1 | A2 | A3 | A4 | A5 | A6\n");
exit(EXIT_FAILURE);
}
}
if (plotbegin(0) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
if (getplotinfo(&info, sizeof (info)) < 0) {
fputs("Failed to get plot information\n", stderr);
exit(EXIT_FAILURE);
}
maxx = info.widthInUnits;
maxy = info.heightInUnits;
switch (info.paperSize) {
case ISO_A3:
nx = 4;
ny = 2;
trueWidthmm = 420;
trueHeightmm = 297;
portrait = true;
break;
case ISO_A4:
nx = 2;
ny = 2;
trueWidthmm = 297;
trueHeightmm = 210;
portrait = false;
break;
case ISO_A5:
nx = 2;
ny = 1;
trueWidthmm = 210;
trueHeightmm = 148;
portrait = true;
break;
case ISO_A6:
nx = 1;
ny = 1;
trueWidthmm = 184;
trueHeightmm = 105;
portrait = false;
break;
}
borderx = (trueWidthmm - info.widthInmm) / 2.0;
bordery = (trueHeightmm - info.heightInmm) / 2.0;
dx = (trueWidthmm * info.unitsPermm) / (double)nx;
dy = (trueHeightmm * info.unitsPermm) / (double)ny;
for (i = 1; i < nx; i++) {
x = (dx * (double)i) - (borderx * info.unitsPermm);
moveto(x, 0);
lineto(x, maxy);
}
for (i = 1; i < ny; i++) {
y = (dy * (double)i) - (bordery * info.unitsPermm);
moveto(0, y);
lineto(maxx, y);
}
for (i = 0; i < nx; i++)
for (j = 0; j < ny; j++) {
x = (dx * (double)i) - (borderx * info.unitsPermm);
y = (dy * (double)j) - (bordery * info.unitsPermm);
if (portrait)
postcard_port(x, y, dx, dy, maxx, maxy);
else
postcard_land(x, y, dx, dy, maxx, maxy);
}
plotend();
return (0);
}
/* postcard_port --- draw a postcard label at (x, y) in portrait */
void postcard_port(const double x, const double y, const double wd, const double ht, const double maxx, const double maxy)
{
int i;
bool right = true;
bool bottom = true;
double yc = y + (ht / 2.0);
double x1 = x + (wd / 16.0);
double x2 = x + wd - (wd / 16.0);
double sx2 = x + wd - (5.0 * 40.0);
double sy2 = y + (5.0 * 40.0);
double sx1 = sx2 - (22.0 * 40.0);
double sy1 = sy2 + (19.0 * 40.0);
double ax = sx1 - (12.0 * 40.0);
double ay1 = yc - (5.0 * 40.0);
double ay2 = y + (5.0 * 40.0);
if (x1 < 0.0)
x1 = 0.0;
if (x2 > maxx)
x2 = maxx;
if (sx2 > maxx) {
sx2 = maxx;
right = false;
}
if (sy2 > maxy)
sy2 = maxy;
if (sy2 < 0.0) {
sy2 = 0.0;
bottom = false;
}
if (ay2 < 0.0)
ay2 = 0.0;
moveto(x1, yc);
lineto(x2, yc);
moveto(sx1, sy1);
lineto(sx1, sy2); /* Left side */
if (bottom)
lineto(sx2, sy2); /* Bottom */
else
moveto(sx2, sy2);
if (right)
lineto(sx2, sy1); /* Right side */
else
moveto(sx2, sy1);
lineto(sx1, sy1); /* Top */
for (i = 0; i < 5; i++) {
moveto(ax - (12.0 * 40.0) * (double)i, ay1);
lineto(ax - (12.0 * 40.0) * (double)i, ay2);
}
}
/* postcard_land --- draw a postcard label at (x, y) in landscape */
void postcard_land(const double x, const double y, const double wd, const double ht, const double maxx, const double maxy)
{
}