-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode128-font
executable file
·305 lines (248 loc) · 4.51 KB
/
code128-font
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
#!/usr/bin/perl
# written by peter stamfest <[email protected]>
#
# this file is in the public domain
use strict;
use utf8;
use POSIX;
=pod
Total symbol: <START>DATA<check-sum><END>
1 Character -> 3 bars / 3 spaces. Total of 11 bar-widths.
For code A and B:
Symbol length for N chars in bars:
l = 11 * N + 35
Minimum bar-width is 7.5mil
1 mil = 1/1000 inch
lb = 7.5mil
Total absolute symbol length thus:
L = l * lb
Minimum symbol height: the larger of 0.25in or 15% of total symbol length:
H = max(0.25in, 0.15*L)
Note:
1pt = 1/72 in
=cut
sub max {
my $a = shift;
my $b = shift;
return $a > $b ? $a : $b;
}
sub usage {
print STDERR <<EOF;
usage:
--maxchars <n> emit a font suitable for printing up to n
characters (conforming to the required height
of the entire barcode symbol)
--barwidth <mils> Change the width of the smalles printable
bar/space from its standard of 7.5mil
--fontsize <n>|<n>pt|<n>mm specify height of font
--extchars <extstring> map 11 "unprintable" characters to the
characters in <extrstring>
EOF
}
# in mils (1 mil = 1/1000 inch)
my $lb = 7.5;
my $H = 250;
my $extchars = '³²°üöä¹ÄÖܧ';
my $pt = 0;
# parse commandline
while ($ARGV[0] ne "") {
my $arg = shift;
if ($arg eq '--maxchars') {
my $maxchars = shift;
my $l = 11 * $maxchars + 35;
my $L = $l * $lb;
$H = max(250, 0.15 * $L); # in mil
} elsif ($arg eq '--barwidth') {
$lb = shift;
} elsif ($arg eq '--fontsize') {
$H = shift;
if ($H =~ m/(.*)pt$/) {
$H = $1 * 1000.0 / 72.0; # turn points into mils
$pt = $1;
} elsif ($H =~ m/(.*)mm$/) {
$H = $1 / 0.0254; # turn mm into mils
$pt = ($H / 1000.0 / (1 / 72.0));
} else {
$pt = $H;
$H *= 1000 / 72; # turn points into mils
}
} elsif ($arg eq '--extchars') {
$extchars = shift;
} else {
die "unknown command-line argument: $arg\n";
}
}
usage() if $pt == 0;
# round up
$pt = POSIX::ceil($pt);
my $Hpt = $pt / 72 * 1000; # in mil
# $H = $pt / 72 * 1000;
print STDERR "Use font at size ${pt}pt\n";
my $unitsem = 1024.0;
### unitsem ~ $pt (rounded up)
### barheight =
my $barwidth = ($unitsem / $H) * $lb;
my $barheight = $unitsem / $Hpt * $H;
#$unitsem / $ratio / 11.0;
# BSBSBS
my @codeset = qw(
212222
222122
222221
121223
121322
131222
122213
122312
132212
221213
221312
231212
112232
122132
122231
113222
123122
123221
223211
221132
221231
213212
223112
312131
311222
321122
321221
312212
322112
322211
212123
212321
232121
111323
131123
131321
112313
132113
132311
211313
231113
231311
112133
112331
132131
113123
113321
133121
313121
211331
231131
213113
213311
213131
311123
311321
331121
312113
312311
332111
314111
221411
431111
111224
111422
121124
121421
141122
141221
112214
112412
122114
122411
142112
142211
241211
221114
413111
241112
134111
111242
121142
121241
114212
124112
124211
411212
421112
421211
212141
214121
412121
111143
111341
131141
114113
114311
411113
411311
113141
114131
311141
411131
211412
211214
211232
2331112
);
my $horizadvx = 11.0 * $barwidth;
print <<EOF;
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg>
<defs>
<font horiz-adv-x="$horizadvx" >
<font-face
font-family="Code128-$pt"
font-size="${pt}pt"
units-per-em="$unitsem"
alphabetic="0"
horiz-adv-x="$horizadvx"
/>
<missing-glyph horiz-adv-x="$horizadvx" />
EOF
my $i = 0;
foreach my $c (@codeset) {
my @f = split //, $c;
my $s = 0;
my $d = "";
my $j = 0;
foreach my $b (@f) {
if ($j % 2 == 0) {
my $x1 = $s * $barwidth;
my $wi = $b * $barwidth;
#my $x2 = ($s + $b) * $barwidth;
# BAR
$d .= "M$x1 0h${wi}v${barheight}h-${wi}v-${barheight}z";
}
$s += $b;
$j++;
}
my $code = 32 + $i;
my $xml = sprintf " <glyph unicode='&#%d;' horiz-adv-x='%g' d='%s'/>\n", $code, $s * $barwidth, $d;
print "$xml";
if ($i >= 96) {
my $x = substr($extchars, $i - 96, 1);
if ($x ne "") {
my $xx = ord($x);
die "duplicated character due to extension character $x\n" if ($xx < 128);
$xml = sprintf " <glyph unicode='&#%d;' horiz-adv-x='%g' d='%s'/>\n", ord($x), $s * $barwidth, $d;
print "$xml";
}
}
$i++;
}
print <<EOF;
</font>
</defs>
</svg>
EOF