-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrenmatrix.ps.src
442 lines (398 loc) · 16.1 KB
/
renmatrix.ps.src
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
% Barcode Writer in Pure PostScript
% https://bwipp.terryburton.co.uk
%
% Copyright (c) 2004-2024 Terry Burton
%
% $Id$
%
% Permission is hereby granted, free of charge, to any
% person obtaining a copy of this software and associated
% documentation files (the "Software"), to deal in the
% Software without restriction, including without
% limitation the rights to use, copy, modify, merge,
% publish, distribute, sublicense, and/or sell copies of
% the Software, and to permit persons to whom the Software
% is furnished to do so, subject to the following
% conditions:
%
% The above copyright notice and this permission notice
% shall be included in all copies or substantial portions
% of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
% KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
% THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
% DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
% CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
% IN THE SOFTWARE.
% --BEGIN RENDERER renmatrix--
% --REQUIRES preamble raiseerror processoptions--
/setpacking where {pop currentpacking true setpacking} if
10 dict
dup /raiseerror dup /uk.co.terryburton.bwipp findresource put
dup /processoptions dup /uk.co.terryburton.bwipp findresource put
begin
/renmatrix {
20 dict begin
% Default options
/width 1.0 def
/height 1.0 def
/barcolor (unset) def
/backgroundcolor (unset) def
/bordercolor (unset) def
/colormap (unset) def
/dotty false def
/inkspread null def % real sentinal
/inkspreadh null def % real sentinal
/inkspreadv null def % real sentinal
/txt [] def
/includetext false def
/textcolor (unset) def
/textxalign (unset) def
/textyalign (unset) def
/textfont (Courier) def
/textsize 10.0 def
/textxoffset 0.0 def
/textyoffset 0.0 def
/textgaps 0.0 def
/alttext () def
/extratext () def
/extratextcolor (unset) def
/extratextxalign (left) def
/extratextyalign (above) def
/extratextfont (Courier) def
/extratextsize 10.0 def
/extratextxoffset 0.0 def
/extratextyoffset 0.0 def
/extratextgaps 0.0 def
/showborder false def
/borderleft 0.0 def
/borderright 0.0 def
/bordertop 0.0 def
/borderbottom 0.0 def
/borderwidth 0.25 def
{def} forall
opt currentdict /opt undef //processoptions exec pop
inkspread null eq {/inkspread 0.15 def} if
inkspreadh null eq {/inkspreadh inkspread def} if
inkspreadv null eq {/inkspreadv inkspread def} if
/uk.co.terryburton.bwipp.global_ctx dup where {
exch get /ctx exch def
barcolor (unset) eq { ctx /default_barcolor 2 copy known {get /barcolor exch def} {pop pop} ifelse } if
backgroundcolor (unset) eq { ctx /default_backgroundcolor 2 copy known {get /backgroundcolor exch def} {pop pop} ifelse } if
bordercolor (unset) eq { ctx /default_bordercolor 2 copy known {get /bordercolor exch def} {pop pop} ifelse } if
} { pop } ifelse
textsize 0 le {
/bwipp.renmatrixBadTextsize (The font size must be greater than zero) //raiseerror exec
} if
/xyget { pixx mul add pixs exch get} def
/cget { pixx mul add cache exch get and} def
/cput { % Optimise by only storing "stem" corners
dup 4 mod 0 eq { % dir is up or left
3 1 roll pixx mul add cache exch 2 copy get 4 -1 roll or put
} {
pop pop pop
} ifelse
} def
/abcd {
4 string 0 [
5 -2 roll
pixx mul add dup
pixs exch 2 getinterval aload pop 3 -1 roll % A B
pixs exch pixx add 2 getinterval aload pop % C D
] {3 copy 48 add put pop 1 add} forall pop
} def
/right {dir 1 ne {x y dir cput [x y]} if /x x 1 add def /dir 1 def} def
/down {dir 2 ne {x y dir cput [x y]} if /y y 1 add def /dir 2 def} def
/left {dir 4 ne {x y dir cput [x y]} if /x x 1 sub def /dir 4 def} def
/up {dir 8 ne {x y dir cput [x y]} if /y y 1 sub def /dir 8 def} def
/trace {
% Walk the outline of a region emitting edge coordinates of the path
/y exch def /x exch def
% dir 1:right 2:down 4:left 8:up
/dir x 1 add y 1 add xyget 1 eq {8} {4} ifelse def
/sx x def /sy y def /sdir dir def
% A B
% C D
mark {
x y abcd
1 { % Common exit
dup dup (0001) eq exch dup (0011) eq exch (1011) eq or or {pop right exit} if
dup dup (0010) eq exch dup (1010) eq exch (1110) eq or or {pop down exit} if
dup dup (1000) eq exch dup (1100) eq exch (1101) eq or or {pop left exit} if
dup dup (0100) eq exch dup (0101) eq exch (0111) eq or or {pop up exit} if
dup (1001) eq {
dir 2 eq {pop left exit} {pop right exit} ifelse
} { % 0110
dir 1 eq {pop down exit} {pop up exit} ifelse
} ifelse
} repeat
x sx eq y sy eq and dir sdir eq and {exit} if
} loop
counttomark array astore exch pop
} def
% Set RGB or CMYK color, or some other color by name
/setanycolor {
/anycolor exch def
anycolor length 1 lt {
/bwipp.colorEnpty (Colors must not be empty) //raiseerror exec
} if
anycolor 0 get 95 ne { % RGB or CMYK
anycolor length dup 6 ne exch 8 ne and {
/bwipp.colorBadLength (Colors must be 6 or 8 hexadecimal characters or a defined name beginning with underscore) //raiseerror exec
} if
mark
0 2 anycolor length 1 sub {
anycolor exch 2 getinterval 0 exch {
dup dup 48 ge exch 57 le and {48 sub} { % 0-9
dup dup 65 ge exch 70 le and {55 sub} { % A-F
dup dup 97 ge exch 102 le and {87 sub} { % a-f
cleartomark /bwipp.colorBadCharacter (RGB and CMYK Colors must be in hexadecimal notation) //raiseerror exec
} ifelse } ifelse } ifelse
exch 16 mul add
} forall
255 div
} for
counttomark
dup 3 eq { /DeviceRGB } { /DeviceCMYK } ifelse
exch 2 add -1 roll pop % clear mark
} { % Named color beginning "_..."
/uk.co.terryburton.bwipp.global_ctx dup where {exch get} {0 dict} ifelse
/named_colors 2 copy known {get} {pop pop 0 dict} ifelse
anycolor 2 copy known not {
pop pop /bwipp.colorUnknown (Color is not defined) //raiseerror exec
} if
get mark exch aload pop counttomark 1 add -2 roll exch pop
} ifelse
{ setcolorspace setcolor } stopped {
/bwipp.colorFailedToSet (Failed to set color) //raiseerror exec
} if
} def
% Plot a layer of single colour
/drawlayer {
/pixsorig pixs def
/pixs exch def
% Pad the bitmap on all sides
/pixs [
pixx 2 add {0} repeat
0 pixx pixs length 1 sub {
0 exch
pixs exch pixx getinterval aload pop
0
} for
pixx 2 add {0} repeat
] def
/pixx pixx 2 add def
/pixy pixy 2 add def
% Cache of visited corners for each direction
/cache [ pixs length {0} repeat ] def
% Construct paths by tracing regions avoiding duplication by using the cache
/paths [
0 1 pixy 2 sub {
/j exch def
0 1 pixx 2 sub {
/i exch def
/k i j abcd def
k (0001) eq k (1001) eq or { % Black region stem corners
8 i j cget 0 eq {
i j trace
} if
} if
k (1110) eq { % White region stem corner
4 i j cget 0 eq {
i j trace
} if
} if
} for
} for
] def
% Revert the bitmap size
/pixx pixx 2 sub def
/pixy pixy 2 sub def
% Draw the image
newpath
paths {
/p exch def
/len p length def
p len 1 sub get aload pop
p 0 get aload pop
0 1 len 1 sub { % x1 y1 x2 y2
/i exch def
p i 1 add len mod get aload pop 6 -2 roll % x3 y3 x1 y1 x2 y2
5 index inkspreadh
4 index 4 -1 roll lt {add} {sub} ifelse % y3<y1 ? x2+i : x2-i
4 1 roll
4 index inkspreadv
4 index 4 -1 roll gt {add} {sub} ifelse % x3>x1 ? y2+i : y2-i
4 -1 roll exch pixy exch sub
i 0 eq {moveto} {lineto} ifelse
} for % x2 y2 x3 y3
closepath
pop pop pop pop
} forall
fill
/pixs pixsorig def
} def
/drawlayerdots {
/pixsorig pixs def
/pixs exch def
newpath
0 1 pixs length 1 sub {
dup pixx mod /x exch def
pixx idiv /y exch def
x y xyget 1 eq {
x 0.5 add pixy y sub 0.5 sub moveto
x 0.5 add pixy y sub 0.5 sub 0.5 inkspread sub 0 360 arc
} if
} for
fill
/pixs pixsorig def
} def
gsave
currentpoint translate
/inkspread inkspread 2 div def
/inkspreadh inkspreadh 2 div def
/inkspreadv inkspreadv 2 div def
% Force symbol to given width
width pixx div 72 mul height pixy div 72 mul scale
% Display the border and background
showborder {
/tl [ borderleft borderwidth 2 div add neg pixy bordertop add borderwidth 2 div add ] def
/tr [ pixx borderright add borderwidth 2 div add pixy bordertop add borderwidth 2 div add ] def
/bl [ borderleft borderwidth 2 div add neg borderbottom borderwidth 2 div add neg ] def
/br [ pixx borderright add borderwidth 2 div add borderbottom borderwidth 2 div add neg ] def
} { % No need to extend background when there is no border
/tl [ borderleft neg inkspread add pixy bordertop add inkspread sub ] def
/tr [ pixx borderright add inkspread sub pixy bordertop add inkspread sub ] def
/bl [ borderleft neg inkspread add borderbottom neg inkspread add ] def
/br [ pixx borderright add inkspread sub borderbottom neg inkspread add ] def
} ifelse
backgroundcolor (unset) ne {
gsave
newpath bl aload pop moveto [ br tr tl ] { aload pop lineto } forall closepath
backgroundcolor setanycolor fill
grestore
} if
showborder {
gsave
newpath bl aload pop moveto [ br tr tl ] { aload pop lineto } forall closepath
bordercolor (unset) ne { bordercolor setanycolor } if
borderwidth inkspread 2 mul sub setlinewidth stroke
grestore
} if
% Display the matrix
colormap (unset) eq {
/colormap << 1 barcolor >> def
} if
colormap {
dup (unset) ne { setanycolor } { pop } ifelse
/key exch def
[
pixs { key eq {1} {0} ifelse } forall
] dotty {drawlayerdots} {drawlayer} ifelse
} forall
% Display the text for elements in the text array
includetext {
textcolor (unset) ne { textcolor setanycolor } if
textxalign (unset) eq textyalign (unset) eq and alttext () eq and {
/s 0 def /fn () def
txt {
aload pop
2 copy s ne exch fn ne or {
dup 0 le {
pop pop pop pop pop
/bwipp.renmatrixFontTooSmall (The font size is too small) //raiseerror exec
} if
2 copy /s exch def /fn exch def
selectfont
} {
pop pop
} ifelse
moveto show
} forall
} {
textfont textsize selectfont
alttext () eq {
/txt [ txt { 0 get {} forall } forall ] def
/tstr txt length string def
0 1 txt length 1 sub { dup txt exch get tstr 3 1 roll put } for
} {
/tstr alttext def
} ifelse
% Find true ascent of font
tstr length 0 eq {
0
} {
gsave
newpath 0 0 moveto (0) false charpath pathbbox
4 1 roll pop pop pop
grestore
currentfont /PaintType known {currentfont /PaintType get 2 eq} {false} ifelse
currentfont /StrokeWidth known and {
currentfont /StrokeWidth get 2 div 0 exch
currentfont /FontMatrix get dtransform
dup mul exch dup mul add sqrt
add
} if
} ifelse
/textascent exch def
/textwidth tstr stringwidth pop tstr length 1 sub textgaps mul add def
/textxpos textxoffset pixx textwidth sub 2 div add def
textxalign (left) eq { /textxpos textxoffset def } if
textxalign (right) eq { /textxpos pixx textxoffset sub textwidth sub def } if
textxalign (offleft) eq { /textxpos textwidth textxoffset add neg def } if
textxalign (offright) eq { /textxpos pixx textxoffset add def } if
textxalign (justify) eq textwidth pixx lt and {
/textxpos 0.0 def
/textgaps pixx textwidth sub tstr length 1 sub div def
} if
/textypos textyoffset textascent add 1 add neg def
textyalign (above) eq { /textypos textyoffset pixy add 1 add def } if
textyalign (center) eq { /textypos textyoffset pixy textascent sub 2 div add def } if
textxpos textypos moveto textgaps 0 tstr ashow
} ifelse
} if
% Render any extratext
extratext () ne {
extratextcolor (unset) ne { extratextcolor setanycolor } if
extratextfont extratextsize selectfont
% Find true ascent of font
gsave
newpath 0 0 moveto (0) false charpath pathbbox
4 1 roll pop pop pop
grestore
currentfont /PaintType known {currentfont /PaintType get 2 eq} {false} ifelse
currentfont /StrokeWidth known and {
currentfont /StrokeWidth get 2 div 0 exch
currentfont /FontMatrix get dtransform
dup mul exch dup mul add sqrt
add
} if
/textascent exch def
/textwidth extratext stringwidth pop extratext length 1 sub extratextgaps mul add def
/extratextxpos extratextxoffset pixx textwidth sub 2 div add def
extratextxalign (left) eq { /textxpos extratextxoffset def } if
extratextxalign (right) eq { /textxpos pixx extratextxoffset sub textwidth sub def } if
extratextxalign (offleft) eq { /textxpos textwidth extratextxoffset add neg def } if
extratextxalign (offright) eq { /textxpos pixx extratextxoffset add def } if
extratextxalign (justify) eq textwidth pixx lt and {
/textxpos 0.0 def
/extratextgaps pixx textwidth sub extratext length 1 sub div def
} if
/textypos extratextyoffset textascent add 1 add neg def
extratextyalign (above) eq { /textypos extratextyoffset pixy add 1 add def } if
extratextyalign (center) eq { /textypos extratextyoffset pixy textascent sub 2 div add def } if
textxpos textypos moveto extratextgaps 0 extratext ashow
} if
grestore
end
}
[/barcode] {null def} forall
bind def
/renmatrix dup load /uk.co.terryburton.bwipp defineresource pop
end
/setpacking where {pop setpacking} if
% --END RENDERER renmatrix--