-
Notifications
You must be signed in to change notification settings - Fork 5
/
printer-scheme.r
1587 lines (1418 loc) · 37.8 KB
/
printer-scheme.r
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
REBOL [
Name: "Printer Scheme"
Purpose: "Printing support library for Windows/UNIX"
Author: "Nenad Rakocevic"
Email: [email protected]
File: %printer-scheme.r
Version: 0.9.2
Date: 29/09/2008
Needs: [library]
License: {
Copyright (c) 2011, Nenad Rakocevic.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
o Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
o Neither the name of Nenad Rakocevic nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}
History: {
o PS wrapper added, GDI wrapper externalized.
o Fixed 'end-page command. Now working correctly.
}
]
gdi-printer-ctx: [
defs: [
caps [
TECHNOLOGY 2
HORZSIZE 4
VERTSIZE 6
HORZRES 8
VERTRES 10
LOGPIXELSX 88
LOGPIXELSY 90
BITSPIXEL 12
PDEVICESIZE 26
CLIPCAPS 36
RASTERCAPS 38
SIZEPALETTE 104
NUMRESERVED 106
COLORRES 108
PHYSICALWIDTH 110
PHYSICALHEIGHT 111
PHYSICALOFFSETX 112
PHYSICALOFFSETY 113
SCALINGFACTORX 114
SCALINGFACTORY 115
VREFRESH 116
DESKTOPHORZRES 118
DESKTOPVERTRES 117
BLTALIGNMENT 119
]
dt [
LEFT 0
TOP 0
CENTER 1
RIGHT 2
VCENTER 4
BOTTOM 8
WORDBREAK 16
SINGLELINE 32
EXPANDTABS 64
TABSTOP 128
NOCLIP 256
EXTERNALLEADING 512
CALCRECT 1024
NOPREFIX 2048
INTERNAL 4096
EDITCONTROL 8192
PATH_ELLIPSIS 16384
END_ELLIPSIS 32768
MODIFYSTRING 65536
RTLREADING 131072
WORD_ELLIPSIS 262144
]
pen [
solid 0
dash 1
dot 2
dashdot 3
dashdotdot 4
null 5
insideframe 6
userstyle 7
alternate 8
endcap_round 0
endcap_square 256
endcap_flat 512
join-round 0
join-bevel 4096
join-miter 8192
]
stock [
white-brush 0
lt-gray-brush 1
gray-brush 2
dk-gray-brush 3
black-brush 4
null-brush 5
hollow-brush 5
white-pen 6
black-pen 7
null-pen 8
system-font 13
default-palette 15
]
printer-enum [
local 2
connections 4
remote 16
]
misc [
DIB_RGB_COLORS 0
SRCCOPY 13369376
DM_OUT_BUFFER 2
]
]
kernel32: load/library %kernel32.dll
gdi32: load/library %gdi32.dll
user32: load/library %user32.dll
winspool: load/library %winspool.drv
; === General API ===
int: :to-integer
FORMAT_MESSAGE_FROM_SYSTEM: int #{00001000}
FORMAT_MESSAGE_IGNORE_INSERTS: int #{00000200}
fmt-msg-flags:
FORMAT_MESSAGE_FROM_SYSTEM
or FORMAT_MESSAGE_IGNORE_INSERTS
GetLastError: make routine! [
return: [integer!]
] kernel32 "GetLastError"
FormatMessage: make routine! [
dwFlags [integer!]
lpSource [integer!]
dwMessageId [integer!]
dwLanguageId [integer!]
lpBuffer [string!]
nSize [integer!]
Arguments [integer!]
return: [integer!]
] kernel32 "FormatMessageA"
; === Printer API ===
_RECT: make struct! struct-rect: [
left [integer!]
top [integer!]
right [integer!]
bottom [integer!]
] none
DOC_INFO: make struct! struct-doc-info: [
[save]
cbSize [integer!]
lpszDocName [string!]
lpszOutput [integer!]
lpszDatatype [integer!]
fwType [integer!]
][0 "test" 0 0 0]
PRINTER_INFO_4: make struct! [
pPrinterName [string!]
pServerName [string!]
Attributes [integer!]
] none
TEXTMETRIC: make struct! struct-text-metric: [
tmHeight [long]
tmAscent [long]
tmDescent [long]
tmInternalLeading [long]
tmExternalLeading [long]
tmAveCharWidth [long]
tmMaxCharWidth [long]
tmWeight [long]
tmOverhang [long]
tmDigitizedAspectX [long]
tmDigitizedAspectY [long]
tmFirstChar [char]
tmLastChar [char]
tmDefaultChar [char]
tmBreakChar [char]
tmItalic [char]
tmUnderlined [char]
tmStruckOut [char]
tmPitchAndFamily [char]
tmCharSet [char]
] none
BITMAPINFOHEADER: make struct! bih: [
biSize [long]
biWidth [long]
biHeight [long]
biPlanes [short]
biBitCount [short]
biCompression [long]
biSizeImage [long]
biXPelsPerMeter [long]
biYPelsPerMeter [long]
biClrUsed [long]
biClrImportant [long]
] none
BITMAPINFO: make struct! struct-bitmap-info: append copy bih [
bmiColors [integer!]
] none
GetDefaultPrinter: make routine! [
pszBuffer [string!]
pcchBuffer [struct! [low [integer!] high [integer!]]]
return: [integer!]
] winspool "GetDefaultPrinterA"
OpenPrinter: make routine! [
pPrinterName [string!]
phPrinter [struct! [ptr [integer!]]]
pDefault [integer!]
return: [integer!]
] winspool "OpenPrinterA"
EnumPrinters: make routine! [
Flags [integer!]
Name [integer!]
Level [integer!]
pPrinterEnum [string!]
cbBuf [integer!]
pcbNeeded [struct! [n [integer!]]]
pcReturned [struct! [n [integer!]]]
return: [integer!]
] winspool "EnumPrintersA"
ClosePrinter: make routine! compose/deep [
hPrinter [integer!]
return: [integer!]
] winspool "ClosePrinter"
StartDoc: make routine! compose/deep [
hdc [integer!]
lpdi [struct! [(struct-doc-info)]]
return: [integer!]
] gdi32 "StartDocA"
StartPage: make routine! [
hdc [integer!]
return: [integer!]
] gdi32 "StartPage"
EndPage: make routine! [
hdc [integer!]
return: [integer!]
] gdi32 "EndPage"
EndDoc: make routine! [
hdc [integer!]
return: [integer!]
] gdi32 "EndDoc"
CreateDC: make routine! [
lpszDriver [string!]
lpszDevice [string!]
lpszOutput [integer!]
lpInitData [integer!]
return: [integer!]
] gdi32 "CreateDCA"
DeleteDC: make routine! [
hdc [integer!]
return: [integer!]
] gdi32 "DeleteDC"
TextOut: make routine! [
hdc [integer!]
nXStart [integer!]
nYStart [integer!]
lpString [string!]
cbString [integer!]
return: [integer!]
] gdi32 "TextOutA"
DrawText: make routine! compose/deep [
hdc [integer!]
lpString [string!]
nCount [integer!]
lpRect [struct! [(struct-rect)]]
uFormat [integer!]
return: [integer!]
] user32 "DrawTextA"
SetTextColor: make routine! [
hdc [integer!]
crColor [integer!]
return: [integer!]
] gdi32 "SetTextColor"
MoveToEx: make routine! [
hdc [integer!]
X [integer!]
Y [integer!]
lpPoint [integer!]
return: [integer!]
] gdi32 "MoveToEx"
LineTo: make routine! [
hdc [integer!]
nXEnd [integer!]
nYEnd [integer!]
return: [integer!]
] gdi32 "LineTo"
Arc: make routine! [
hdc [integer!]
nLeftRect [integer!]
nTopRect [integer!]
nRightRect [integer!]
nBottomRect [integer!]
nXRadial1 [integer!]
nYRadial1 [integer!]
nXRadial2 [integer!]
nYRadial2 [integer!]
return: [integer!]
] gdi32 "Arc"
Pie: make routine! third :Arc gdi32 "Pie"
Rectangle: make routine! [
hdc [integer!]
nLeftRect [integer!]
nTopRect [integer!]
nRightRect [integer!]
nBottomRect [integer!]
return: [integer!]
] gdi32 "Rectangle"
CreatePen: make routine! [
fnPenStyle [integer!]
nWidth [integer!]
crColor [integer!]
return: [integer!]
] gdi32 "CreatePen"
CreateSolidBrush: make routine! [
crColor [integer!]
return: [integer!]
] gdi32 "CreateSolidBrush"
GetDCBrushColor: make routine! [
hdc [integer!]
return: [integer!]
] gdi32 "GetDCBrushColor"
GetDCPenColor: make routine! [
hdc [integer!]
return: [integer!]
] gdi32 "GetDCPenColor"
GetTextColor: make routine! [
hdc [integer!]
return: [integer!]
] gdi32 "GetTextColor"
GetStockObject: make routine! [
fnObject [integer!]
return: [integer!]
] gdi32 "GetStockObject"
SelectObject: make routine! [
hdc [integer!]
hgdiobj [integer!]
return: [integer!]
] gdi32 "SelectObject"
DeleteObject: make routine! [
hgdiobj [integer!]
return: [integer!]
] gdi32 "DeleteObject"
StretchDIBits: make routine! compose/deep [
hdc [integer!]
XDest [integer!]
YDest [integer!]
nDestWidth [integer!]
nDestHeight [integer!]
XSrc [integer!]
YSrc [integer!]
nSrcWidth [integer!]
nSrcHeight [integer!]
lpBits [string!]
lpBitsInfo [struct! [(struct-bitmap-info)]]
iUsage [integer!]
dwRop [long]
return: [integer!]
] gdi32 "StretchDIBits"
CreateFont: make routine! [
nHeight [integer!]
nWidth [integer!]
nEscapement [integer!]
nOrientation [integer!]
fnWeight [integer!]
fdwItalic [integer!]
fdwUnderline [integer!]
fdwStrikeOut [integer!]
fdwCharSet [integer!]
fdwOutputPrecision [integer!]
fdwClipPrecision [integer!]
fdwQuality [integer!]
fdwPitchAndFamily [integer!]
lpszFace [string!]
return: [integer!]
] gdi32 "CreateFontA"
GetTextMetrics: make routine! compose/deep [
hdc [integer!]
lptm [struct! [(struct-text-metric)]]
return: [integer!]
] gdi32 "GetTextMetricsA"
GetDeviceCaps: make routine! [
hdc [integer!]
nIndex [integer!]
return: [integer!]
] gdi32 "GetDeviceCaps"
; === Helper functions ====
locals: none
rect: make struct! _RECT none
make-null-string!: func [len [integer!]][
head insert/dup make string! len null len
]
get-error-msg: has [out][
out: make-null-string! 256
FormatMessage fmt-msg-flags 0 GetLastError 0 out 256 0
trim/tail out
]
try*: func [body [block!] /quiet /local res][
if all [zero? res: do body not quiet][
print reduce [mold first body "failed :" get-error-msg]
]
res
]
to-xdpi: func [v [integer!]][
to integer! v * locals/caps/LOGPIXELSX / 25.4 * locals/scale/x / 100
]
to-ydpi: func [v [integer!]][
to integer! v * locals/caps/LOGPIXELSY / 25.4 * locals/scale/y / 100
]
to-pt-dpi: func [v][
negate to integer! v * locals/caps/LOGPIXELSY / 72 * locals/scale/y / 100
]
to-bgr: func [c [tuple! word!]][
if word? c [c: get c]
(c/3 * 65536) + (c/2 * 256) + c/1
]
to-tuple: func [bgr [integer!]][
to tuple! reduce [
255 and bgr
255 and shift bgr 8
255 and shift bgr 16
]
]
update-obj: func [type [word!] /local obj][
obj: locals/:type
if obj/dirty? [
if obj/handle [try* [DeleteObject obj/handle]]
obj/handle: switch type [
pen [CreatePen obj/style to-pt-dpi obj/width to-bgr obj/color]
brush [CreateSolidBrush to-bgr obj/color]
]
SelectObject locals/hDC obj/handle
obj/dirty?: no
]
]
reset-obj: func [type [word!] /local obj][
obj: locals/:type
if obj/handle [
SelectObject locals/hDC GetStockObject select defs/stock pick [null-pen null-brush] type = 'pen
try* [DeleteObject obj/handle]
]
obj/handle: none
obj/dirty?: no
]
enum: has [out buf flags needed ret cmd pi4 len buf*][
out: make block! 4
buf: make-null-string! 1024
needed: make struct! [n [integer!]] none
ret: make struct! needed none
flags: defs/printer-enum/local or defs/printer-enum/connections
try* cmd: [EnumPrinters flags 0 4 buf 1024 needed ret]
pi4: make struct! PRINTER_INFO_4 none
len: length? buf*: third pi4
if 1024 < (ret/n * len) [
buf: make string! ret/n * len
try* cmd
]
loop ret/n [
change/part buf* buf at buf len
ret: second pi4
repend out ret/1
append/only out next ret
poke ret 3 pick [remote local] zero? ret/3 and 64
buf: at buf len + 1
]
buf: none
new-line/skip out on 2
]
init: func [/with prn-name /local len buf size][
either prn-name [
locals/name: prn-name
][
size: make struct! [low [integer!] high [integer!]] reduce [128 0]
try* [GetDefaultPrinter locals/name size]
clear find locals/name null
]
locals/hDC: try* [CreateDC "WINSPOOL" locals/name 0 0]
; caps => object with rebol-style names
locals/caps: reduce [
'LOGPIXELSX GetDeviceCaps locals/hDC defs/caps/LOGPIXELSX
'LOGPIXELSY GetDeviceCaps locals/hDC defs/caps/LOGPIXELSY
'PHYSICALWIDTH GetDeviceCaps locals/hDC defs/caps/PHYSICALWIDTH
'PHYSICALHEIGHT GetDeviceCaps locals/hDC defs/caps/PHYSICALHEIGHT
'PHYSICALOFFSETX GetDeviceCaps locals/hDC defs/caps/PHYSICALOFFSETX
'PHYSICALOFFSETY GetDeviceCaps locals/hDC defs/caps/PHYSICALOFFSETY
'HORZRES GetDeviceCaps locals/hDC defs/caps/HORZRES
'VERTRES GetDeviceCaps locals/hDC defs/caps/VERTRES
]
if locals/auto-fit?/x = 1 [
locals/scale/x: to integer! 100 * locals/caps/HORZRES / locals/caps/PHYSICALWIDTH + .5
]
if locals/auto-fit?/y = 1 [
locals/scale/y: to integer! 100 * locals/caps/VERTRES / locals/caps/PHYSICALHEIGHT + .5
]
reset-defaults
]
reset-defaults: does [
clear locals/font-cache
locals/cur-font: face/font
locals/pen: copy [
handle #[none]
style 0
width 1
color 0.0.0
color2 0.0.0
dirty? #[false]
]
locals/brush: copy [
handle #[none]
color 255.255.255
dirty? #[false]
]
locals/session: [
started? [#[false] #[false]]
pages 0
doc-name "no-name"
]
clear locals/pages
locals/pen/color: to-tuple GetDCPenColor locals/hDC
locals/brush/color: to-tuple GetDCBrushColor locals/hDC
SelectObject locals/hDC GetStockObject defs/stock/null-brush
]
draw-image: func [
img [image! word!]
pos [pair! none!]
size [pair! none!]
/local
data out len pad pad-sz
][
if word? img [img: get img]
;-- RGB->GRB conversion
data: img
forall data [change/only data reverse/part data/1 2 2]
data: img/rgb
;-- padding each scanline (4 bytes aligned)
len: 3 * img/size/x
pad: all [
not zero? pad-sz: len // 4
head insert/dup copy #{} null 4 - pad-sz
]
out: make string! img/size/y * pad-sz + length? data
repeat y img/size/y [
insert/part tail out at data (y - 1) * len len
if pad [insert tail out pad]
]
BITMAPINFO/biSize: length? third BITMAPINFOHEADER
BITMAPINFO/biWidth: img/size/x
BITMAPINFO/biHeight: negate img/size/y
BITMAPINFO/biPlanes: 1
BITMAPINFO/biBitCount: 24
; Todo: convert img/size (px) to log.units
try* [
StretchDIBits
locals/hDC
to-xdpi pos/x
to-ydpi pos/y
to-xdpi to integer! img/size/x * locals/scale/x / 400 ; hacked conversion
to-ydpi to integer! img/size/y * locals/scale/y / 400 ; hacked conversion
0 0 img/size/x img/size/y
out
BITMAPINFO
defs/misc/DIB_RGB_COLORS
defs/misc/SRCCOPY
]
]
draw-text: func [txt [string!] pos [pair!]][
try* [TextOut locals/hDC to-xdpi pos/1 to-ydpi pos/2 txt length? txt]
]
draw-text-box: func [txt [string!] pos [pair!] sz [pair!] opts [block! none!] /local v][
rect/left: to-xdpi pos/x
rect/top: to-ydpi pos/y
rect/right: to-xdpi pos/x + sz/x
rect/bottom: to-ydpi pos/y + sz/y
v: defs/dt/NOPREFIX or defs/dt/EXPANDTABS
if not all [opts find opts 'no-wrap][v: v or defs/dt/WORDBREAK]
v: v or any [select defs/dt locals/cur-font/align 0]
v: v or any [
either locals/cur-font/valign = 'center [defs/dt/VCENTER][
select defs/dt locals/cur-font/valign
]
0
]
try* [DrawText locals/hDC txt -1 rect v]
]
draw-line: func [pos [pair!] pos2 [pair!]][
update-obj 'pen
try* [MoveToEx locals/hDC to-xdpi pos/x to-ydpi pos/y 0]
try* [LineTo locals/hDC to-xdpi pos2/x to-ydpi pos2/y]
]
draw-box: func [pos [pair!] pos2 [pair!]][
update-obj 'pen
update-obj 'brush
try* [Rectangle locals/hDC to-xdpi pos/x to-ydpi pos/y to-xdpi pos2/x to-ydpi pos2/y]
]
draw-arc: func [center [pair!] radius [pair!] beg [decimal!] len [decimal!] closed [logic!] /local cmd][
update-obj 'pen
update-obj 'brush
;beg: beg - 180
cmd: [
?
locals/hDC
to-xdpi center/x - radius/x
to-ydpi center/y - radius/y
to-xdpi center/x + radius/x
to-ydpi center/y + radius/y
to-xdpi to integer! center/x + (radius/x * cosine beg)
to-ydpi to integer! center/y - (radius/y * sine beg)
to-xdpi to integer! center/x + (radius/x * cosine beg + len)
to-ydpi to integer! center/y - (radius/y * sine beg + len)
]
poke cmd 1 pick [Pie Arc] to logic! closed
try* cmd
]
set-font: func [obj [word! object!] /local v fnt][
if any-word? obj [obj: get obj]
if not fnt: select locals/font-cache obj [
v: obj/style
fnt: try* [
CreateFont
to-pt-dpi obj/size
0 0 0
either any [v = 'bold all [series? v find v 'bold]][700][400]
either any [v = 'italic all [series? v find v 'italic]][-1][0]
either any [v = 'underline all [series? v find v 'underline]][-1][0]
0 0 0 0 0 0
obj/name
]
repend locals/font-cache [obj fnt]
]
try* [SelectObject locals/hDC fnt]
if locals/cur-font/color <> obj/color [SetTextColor locals/hDC to-bgr obj/color]
locals/cur-font: obj
]
set-pen: func [c1 [tuple! word!] c2 [tuple! none!]][
locals/pen/color: c1
if c2 [locals/pen/color2: c2]
locals/pen/dirty?: yes
]
set-fill: func [c1 [tuple! word!]][
either c1 = 'none [
printer/reset-obj 'brush
][
locals/brush/color: c1
locals/brush/dirty?: yes
]
]
set-line-width: func [size [integer!]][
locals/pen/width: size
locals/pen/dirty?: yes
]
get-font-metrics: has [fnt][
try* [GetTextMetrics locals/hDC TEXTMETRIC]
TEXTMETRIC
]
get-drawable-size: func [/mm /local caps][
caps: locals/caps
as-pair
either mm [caps/HORZRES * 254 / caps/LOGPIXELSX / 10][caps/HORZRES]
either mm [caps/VERTRES * 254 / caps/LOGPIXELSY / 10][caps/VERTRES]
]
start-doc: func [/title name][
if title [DOC_INFO/lpszDocName: name]
DOC_INFO/cbSize: length? third DOC_INFO
try* [StartDoc locals/hDC DOC_INFO]
locals/cur-font: face/font
]
start-page: does [try* [StartPage locals/hDC]]
end-page: does [try* [EndPage locals/hDC]]
end-doc: does [try* [EndDoc locals/hDC]]
make-locals: does [
context [
hDC: none
hPrinter: make struct! [ptr [integer!]] none
name: make-null-string! 128
caps: none ; printer metrics
scale: 100x100 ; scaling X and Y factors
auto-fit?: 1x1 ; auto-scaling in X and Y to fit in drawable area (0x0 disables it)
font-cache: make hash! 8
cur-font: face/font
pen: copy [
handle #[none]
style 0
width 1
color 0.0.0
color2 0.0.0
dirty? #[false]
]
brush: copy [
handle #[none]
color 255.255.255
dirty? #[false]
]
session: [
started? [#[false] #[false]]
pages 0
doc-name "no-name"
]
pages: make block! 1
]
]
close: does [
SelectObject locals/hDC 0 ;-- release last selected handle
foreach [obj hfnt] locals/font-cache [try* [DeleteObject hfnt]]
if locals/pen/handle [try* [DeleteObject locals/pen/handle]]
if locals/brush/handle [try* [DeleteObject locals/brush/handle]]
clear locals/font-cache
try* [DeleteDC locals/hDC]
]
]
;====================================================================================
ps-printer-ctx: [
context [
; -- %addr.r library inlined (author: Romano Paolo Tenca)
; -- original version: http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=addr.r
mode: get-modes system:// 'endian
set 'addr-to-int func [b [binary!] /endian lmode [word!]][
to integer! either 'little = any [lmode mode][head reverse copy b][b]
]
set '& func [b [binary! string! struct!]][
third make struct! [s [string!]] reduce [either struct? b [third b][b]]
]
set 'cast* func [pointer [binary!] type [block!] /local spec n][
spec: copy/deep [inner [struct! []]]
n: 1
if all [integer? type/1 block? type/2][n: type/1 type: type/2]
loop n [
either integer? type/1 [
foreach [size type] type [
insert/dup tail spec/2/2 reduce ['. reduce [type]] size
]
][insert spec/2/2 type]
]
spec: make struct! spec none
change third spec pointer
spec/inner
]
]
libcups: load/library switch system/version/4 [
2 [%/usr/lib/libcups.2.dylib]
4 [%/usr/lib/libcups.so.2]
]
cups_option_s: make struct! struct-option-s: [
name [string!]
value [string!]
] none
cups_dest_s: make struct! struct-dest-s: [
name [string!]
instance [string!]
is_default [integer!]
num_options [integer!]
cups_option_s [integer!]
] none
ppd_size_s: make struct! struct-size-s: [
marked [int]
name1 [decimal!] ; hack : 8 bytes placeholder
name2 [decimal!] ; hack : 8 bytes placeholder
name3 [decimal!] ; hack : 8 bytes placeholder
name4 [decimal!] ; hack : 8 bytes placeholder
name5 [decimal!] ; hack : 8 bytes placeholder
name6 [char] ; hack : 1 byte placeholder
width [float]
length [float]
left [float]
bottom [float]
right [float]
top [float]
] none
cupsGetDefault: make routine! [
return: [char*]
] libcups "cupsGetDefault"
cupsGetDests: make routine! compose/deep [
dests [struct! [dest [char*]]]
return: [integer!]
] libcups "cupsGetDests"
cupsFreeDests: make routine! [
num_dests [integer!]
dests [integer!]
] libcups "cupsFreeDests"
cupsPrintFile: make routine! [
name [string!]
filename [string!]
title [string!]
num_options [integer!]
options [integer!]
return: [integer!]
] libcups "cupsPrintFile"
cupsGetPPD: make routine! [
name [string!]
return: [string!]
] libcups "cupsGetPPD"
ppdOpenFile: make routine! [
filename [string!]
return: [integer!]
] libcups "ppdOpenFile"
ppdPageSize: make routine! compose/deep [
ppd [integer!]
name [string!]
return: [struct! [(struct-size-s)]]
] libcups "ppdPageSize"
ppdClose: make routine! [
ppd [integer!]
] libcups "ppdClose"
header: {%!PS-Adobe-2.0
%%Creator: REBOL Printer scheme
%%Pages: (atend)
%%EndComments
%%BeginSetup
mark
/ISOLatin1Encoding
8#000 1 8#054 {StandardEncoding exch get} for
/minus
8#056 1 8#217 {StandardEncoding exch get} for
/dotlessi
8#301 1 8#317 {StandardEncoding exch get} for
/space /exclamdown /cent /sterling /currency /yen /brokenbar /section
/dieresis /copyright /ordfeminine /guillemotleft /logicalnot /hyphen
/registered /macron /degree /plusminus /twosuperior /threesuperior /acute
/mu /paragraph /periodcentered /cedilla /onesuperior /ordmasculine
/guillemotright /onequarter /onehalf /threequarters /questiondown /Agrave
/Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla /Egrave /Eacute
/Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth /Ntilde
/Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash /Ugrave
/Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls /agrave /aacute
/acircumflex /atilde /adieresis /aring /ae /ccedilla /egrave /eacute
/ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis /eth /ntilde
/ograve /oacute /ocircumflex /otilde /odieresis /divide /oslash /ugrave
/uacute /ucircumflex /udieresis /yacute /thorn /ydieresis
/ISOLatin1Encoding where not {256 array astore def} if
cleartomark
/makeISOEncoded
{ findfont /curfont exch def
/newfont curfont maxlength dict def
/ISOLatin1 (-ISOLatin1) def
/curfontname curfont /FontName get dup length string cvs def
/newfontname curfontname length ISOLatin1 length add string
dup 0 curfontname putinterval
dup curfontname length ISOLatin1 putinterval
def
curfont
{ exch dup /FID ne
{ dup /Encoding eq
{ exch pop ISOLatin1Encoding exch }
if
dup /FontName eq
{ exch pop newfontname exch }
if
exch newfont 3 1 roll put
}
{ pop pop }
ifelse
}
forall
newfontname newfont definefont
} def
/Arial makeISOEncoded pop
/Arial-Bold makeISOEncoded pop
/Arial-Italic makeISOEncoded pop
/Arial-BoldItalic makeISOEncoded pop
/Times-Roman makeISOEncoded pop
/Times-Bold makeISOEncoded pop
/Times-Italic makeISOEncoded pop
/Times-BoldItalic makeISOEncoded pop
/Helvetica makeISOEncoded pop
/Helvetica-Bold makeISOEncoded pop
/Helvetica-Oblique makeISOEncoded pop
/Helvetica-BoldOblique makeISOEncoded pop
/Courier makeISOEncoded pop
/Courier-Bold makeISOEncoded pop
/Courier-Oblique makeISOEncoded pop
/Courier-BoldOblique makeISOEncoded pop
/ushow % linethick lineposition (string) ushow -
{ % underlines text
% call this (ushow) instead of show when placing text
% and the text will be underlined.
% pass line thickness and offset with string
% ie 0.1 -0.8 (Text) ushow
% draws a line 0.1 thick and 0.8 below (-0.8) text position
% ie. 10 10 moveto 0.1 -0.8 (Text) ushow
gsave
exch 0 exch rmoveto
dup stringwidth rlineto
exch setlinewidth stroke