-
Notifications
You must be signed in to change notification settings - Fork 61
/
tkWinGDI.c
3897 lines (3421 loc) · 97.4 KB
/
tkWinGDI.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
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
/*
* tkWinGDI.c --
*
* This module implements access to the Win32 GDI API.
*
* Copyright © 1991-2018 Microsoft Corp.
* Copyright © 2009, Michael I. Schwartz.
* Copyright © 1998-2019 Harald Oehlmann, Elmicron GmbH
* Copyright © 2021 Kevin Walzer/WordTech Communications LLC.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include <windows.h>
#include <math.h>
#include <wtypes.h>
#include <winspool.h>
#include <commdlg.h>
#include <wingdi.h>
#include <tcl.h>
#include "tkWinInt.h"
/*
* Create a standard "DrawFunc" to make this more workable....
*/
#ifdef _MSC_VER
typedef BOOL (WINAPI *DrawFunc) (
HDC, int, int, int, int, int, int, int, int); /* Arc, Chord, Pie. */
#else
typedef BOOL WINAPI (*DrawFunc) (
HDC, int, int, int, int, int, int, int, int); /* Arc, Chord, Pie. */
#endif
/* Real functions. */
static Tcl_ObjCmdProc GdiArc;
static Tcl_ObjCmdProc GdiBitmap;
static Tcl_ObjCmdProc GdiCharWidths;
static Tcl_ObjCmdProc GdiImage;
static Tcl_ObjCmdProc GdiPhoto;
static Tcl_ObjCmdProc GdiLine;
static Tcl_ObjCmdProc GdiOval;
static Tcl_ObjCmdProc GdiPolygon;
static Tcl_ObjCmdProc GdiRectangle;
static Tcl_ObjCmdProc GdiText;
static Tcl_ObjCmdProc GdiMap;
static Tcl_ObjCmdProc GdiCopyBits;
/* Local copies of similar routines elsewhere in Tcl/Tk. */
static int GdiGetColor(Tcl_Obj *nameObj, COLORREF *color);
/*
* Helper functions.
*/
static int GdiMakeLogFont(Tcl_Interp *interp, const char *str,
LOGFONTW *lf, HDC hDC);
static int GdiMakePen(Tcl_Interp *interp, int width,
int dashstyle, const char *dashstyledata,
int capstyle, int joinstyle,
int stipplestyle, const char *stippledata,
unsigned long color, HDC hDC, HGDIOBJ *oldPen);
static int GdiFreePen(Tcl_Interp *interp, HDC hDC, HGDIOBJ oldPen);
static int GdiMakeBrush(unsigned long color, long hatch,
LOGBRUSH *lb, HDC hDC, HBRUSH *oldBrush);
static void GdiFreeBrush(Tcl_Interp *interp, HDC hDC,
HGDIOBJ oldBrush);
static int GdiGetHdcInfo(HDC hdc,
LPPOINT worigin, LPSIZE wextent,
LPPOINT vorigin, LPSIZE vextent);
/* Helper functions for printing the window client area. */
enum PrintType { PTWindow = 0, PTClient = 1, PTScreen = 2 };
static HANDLE CopyToDIB(HWND wnd, enum PrintType type);
static HBITMAP CopyScreenToBitmap(LPRECT lpRect);
static HANDLE BitmapToDIB(HBITMAP hb, HPALETTE hp);
static HANDLE CopyScreenToDIB(LPRECT lpRect);
static int DIBNumColors(LPBITMAPINFOHEADER lpDIB);
static int PalEntriesOnDevice(HDC hDC);
static HPALETTE GetSystemPalette(void);
static void GetDisplaySize(LONG *width, LONG *height);
static int GdiWordToWeight(const char *str);
static int GdiParseFontWords(Tcl_Interp *interp, LOGFONTW *lf,
const char *str[], int numargs);
static int PrintSelectPrinter(void *clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static int PrintOpenPrinter(void *clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static int PrintClosePrinter(void *clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static int PrintOpenDoc(void *clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *const objv[]);
static int PrintCloseDoc(void *clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *const objv[]);
static int PrintOpenPage(void *clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *const objv[]);
static int PrintClosePage(void *clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
/*
* Global state.
*/
static PRINTDLGW pd;
static DOCINFOW di;
static WCHAR *localPrinterName = NULL;
static int copies, paper_width, paper_height, dpi_x, dpi_y;
static LPDEVNAMES devnames;
static HDC printDC;
/*
* To make the "subcommands" follow a standard convention, add them to this
* array. The first element is the subcommand name, and the second a standard
* Tcl command handler.
*/
static const struct gdi_command {
const char *command_string;
Tcl_ObjCmdProc *command;
} gdi_commands[] = {
{ "arc", GdiArc },
{ "bitmap", GdiBitmap },
{ "characters", GdiCharWidths },
{ "image", GdiImage },
{ "line", GdiLine },
{ "map", GdiMap },
{ "oval", GdiOval },
{ "photo", GdiPhoto },
{ "polygon", GdiPolygon },
{ "rectangle", GdiRectangle },
{ "text", GdiText },
{ "copybits", GdiCopyBits },
};
/*
*----------------------------------------------------------------------
*
* GdiArc --
*
* Map canvas arcs to GDI context.
*
* Results:
* Renders arcs.
*
*----------------------------------------------------------------------
*/
static int GdiArc(
TCL_UNUSED(void *),
Tcl_Interp *interp,
int argc,
Tcl_Obj *const *objv)
{
static const char usage_message[] =
"::tk::print::_gdi arc hdc x1 y1 x2 y2 "
"-extent angle -start angle -style arcstyle "
"-fill color -outline color "
"-width dimension -dash dashrule "
"-outlinestipple ignored -stipple ignored\n" ;
int x1, y1, x2, y2;
int xr0, yr0, xr1, yr1;
HDC hDC;
double extent = 0.0, start = 0.0;
DrawFunc drawfunc;
int width = 0;
HPEN hPen;
COLORREF linecolor = 0, fillcolor = BS_NULL;
int dolinecolor = 0, dofillcolor = 0;
HBRUSH hBrush = NULL;
LOGBRUSH lbrush;
HGDIOBJ oldobj = NULL;
int dodash = 0;
const char *dashdata = 0;
drawfunc = Pie;
/* Verrrrrry simple for now.... */
if (argc < 6) {
Tcl_AppendResult(interp, usage_message, NULL);
return TCL_ERROR;
}
hDC = printDC;
if ((Tcl_GetIntFromObj(interp, objv[2], &x1) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[3], &y1) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[4], &x2) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[5], &y2) != TCL_OK)) {
return TCL_ERROR;
}
argc -= 6;
objv += 6;
while (argc >= 2) {
if (strcmp(Tcl_GetString(objv[0]), "-extent") == 0) {
extent = atof(Tcl_GetString(objv[1]));
} else if (strcmp(Tcl_GetString(objv[0]), "-start") == 0) {
start = atof(Tcl_GetString(objv[1]));
} else if (strcmp(Tcl_GetString(objv[0]), "-style") == 0) {
if (strcmp(Tcl_GetString(objv[1]), "pieslice") == 0) {
drawfunc = Pie;
} else if (strcmp(Tcl_GetString(objv[1]), "arc") == 0) {
drawfunc = Arc;
} else if (strcmp(Tcl_GetString(objv[1]), "chord") == 0) {
drawfunc = Chord;
}
} else if (strcmp(Tcl_GetString(objv[0]), "-fill") == 0) {
/* Handle all args, even if we don't use them yet. */
if (GdiGetColor(objv[1], &fillcolor)) {
dofillcolor = 1;
}
} else if (strcmp(Tcl_GetString(objv[0]), "-outline") == 0) {
if (GdiGetColor(objv[1], &linecolor)) {
dolinecolor = 1;
}
} else if (strcmp(Tcl_GetString(objv[0]), "-outlinestipple") == 0) {
/* ignored */
} else if (strcmp(Tcl_GetString(objv[0]), "-stipple") == 0) {
/* ignored */
} else if (strcmp(Tcl_GetString(objv[0]), "-width") == 0) {
if (Tcl_GetIntFromObj(interp, objv[1], &width)) {
return TCL_ERROR;
}
} else if (strcmp(Tcl_GetString(objv[0]), "-dash") == 0) {
if (Tcl_GetString(objv[1])) {
dodash = 1;
dashdata = Tcl_GetString(objv[1]);
}
} else {
/* Don't know that option! */
Tcl_AppendResult(interp, usage_message, NULL);
return TCL_ERROR;
}
argc -= 2;
objv += 2;
}
xr0 = xr1 = (x1 + x2) / 2;
yr0 = yr1 = (y1 + y2) / 2;
/*
* The angle used by the arc must be "warped" by the eccentricity of the
* ellipse. Thanks to Nigel Dodd <nigel.dodd@avellino.com> for bringing a
* nice example.
*/
xr0 += (int)(100.0 * (x2 - x1) * cos((start * 2.0 * 3.14159265) / 360.0));
yr0 -= (int)(100.0 * (y2 - y1) * sin((start * 2.0 * 3.14159265) / 360.0));
xr1 += (int)(100.0 * (x2 - x1) * cos(((start+extent) * 2.0 * 3.14159265) / 360.0));
yr1 -= (int)(100.0 * (y2 - y1) * sin(((start+extent) * 2.0 * 3.14159265) / 360.0));
/*
* Under Win95, SetArcDirection isn't implemented--so we have to assume
* that arcs are drawn counterclockwise (e.g., positive extent) So if it's
* negative, switch the coordinates!
*/
if (extent < 0) {
int xr2 = xr0;
int yr2 = yr0;
xr0 = xr1;
xr1 = xr2;
yr0 = yr1;
yr1 = yr2;
}
if (dofillcolor) {
GdiMakeBrush(fillcolor, 0, &lbrush, hDC, &hBrush);
} else {
oldobj = SelectObject(hDC, GetStockObject(HOLLOW_BRUSH));
}
if (width || dolinecolor) {
GdiMakePen(interp, width, dodash, dashdata,
0, 0, 0, 0, linecolor, hDC, (HGDIOBJ *)&hPen);
}
(*drawfunc)(hDC, x1, y1, x2, y2, xr0, yr0, xr1, yr1);
if (width || dolinecolor) {
GdiFreePen(interp, hDC, hPen);
}
if (hBrush) {
GdiFreeBrush(interp, hDC, hBrush);
} else {
SelectObject(hDC, oldobj);
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GdiBitmap --
*
* Unimplemented for now. Should use the same techniques as
* CanvasPsBitmap (tkCanvPs.c).
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
static int GdiBitmap(
TCL_UNUSED(void *),
Tcl_Interp *interp,
TCL_UNUSED(int),
Tcl_Obj *const *objv)
{
/*
* Skip this for now. Should be based on common code with the copybits
* command.
*/
Tcl_WrongNumArgs(interp, 1, objv, "hdc x y "
"-anchor [center|n|e|s|w] -background color "
"-bitmap bitmap -foreground color\n"
"Not implemented yet. Sorry!");
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* GdiImage --
*
* Unimplemented for now. Unimplemented for now. Should switch on image
* type and call either GdiPhoto or GdiBitmap. This code is similar to
* that in tkWinImage.c.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
static int GdiImage(
TCL_UNUSED(void *),
Tcl_Interp *interp,
TCL_UNUSED(int),
Tcl_Obj *const *objv)
{
/* Skip this for now..... */
/* Should be based on common code with the copybits command. */
Tcl_WrongNumArgs(interp, 1, objv, "hdc x y -anchor [center|n|e|s|w] -image name\n"
"Not implemented yet. Sorry!");
/* Normally, usage results in TCL_ERROR--but wait til' it's implemented. */
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GdiPhoto --
*
* Contributed by Lukas Rosenthaler <lukas.rosenthaler@balcab.ch>
*
* Note: The canvas doesn't directly support photos (only as images), so
* this is the first ::tk::print::_gdi command without an equivalent
* canvas command. This code may be modified to support photo images on
* the canvas.
*
* Results:
* Renders a photo.
*
*----------------------------------------------------------------------
*/
static int GdiPhoto(
TCL_UNUSED(void *),
Tcl_Interp *interp,
int argc,
Tcl_Obj *const *objv)
{
static const char usage_message[] =
"::tk::print::_gdi photo hdc [-destination x y [w [h]]] -photo name\n";
HDC dst;
int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
int nx, ny, sll;
const char *photoname = 0; /* For some reason Tk_FindPhoto takes a char *. */
Tk_PhotoHandle photo_handle;
Tk_PhotoImageBlock img_block;
BITMAPINFO bitmapinfo; /* Since we don't need the bmiColors table,
* there is no need for dynamic allocation. */
int oldmode; /* For saving the old stretch mode. */
POINT pt; /* For saving the brush org. */
char *pbuf = NULL;
int i, j, k;
int retval = TCL_OK;
/*
* Parse the arguments.
*/
/* HDC is required. */
if (argc < 2) {
Tcl_AppendResult(interp, usage_message, NULL);
return TCL_ERROR;
}
dst = printDC;
/*
* Next, check to see if 'dst' can support BitBlt.
* If not, raise an error.
*/
if ((GetDeviceCaps(dst, RASTERCAPS) & RC_STRETCHDIB) == 0) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"::tk::print::_gdi photo not supported on device context (0x%s)",
Tcl_GetString(objv[1])));
return TCL_ERROR;
}
/* Parse the command line arguments. */
for (j = 2; j < argc; j++) {
if (strcmp(Tcl_GetString(objv[j]), "-destination") == 0) {
double x, y, w, h;
int count = 0;
char dummy;
if (j < argc) {
count = sscanf(Tcl_GetString(objv[++j]), "%lf%lf%lf%lf%c",
&x, &y, &w, &h, &dummy);
}
if (count < 2 || count > 4) {
/* Destination must provide at least 2 arguments. */
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"-destination requires a list of 2 to 4 numbers\n%s",
usage_message));
return TCL_ERROR;
}
dst_x = (int) x;
dst_y = (int) y;
if (count == 3) {
dst_w = (int) w;
dst_h = -1;
} else if (count == 4) {
dst_w = (int) w;
dst_h = (int) h;
}
} else if (strcmp(Tcl_GetString(objv[j]), "-photo") == 0) {
photoname = Tcl_GetString(objv[++j]);
}
}
if (photoname == 0) { /* No photo provided. */
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"No photo name provided to ::tk::print::_gdi photo\n%s",
usage_message));
return TCL_ERROR;
}
photo_handle = Tk_FindPhoto(interp, photoname);
if (photo_handle == 0) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"::tk::print::_gdi photo: Photo name %s can't be located\n%s",
photoname, usage_message));
return TCL_ERROR;
}
Tk_PhotoGetImage(photo_handle, &img_block);
nx = img_block.width;
ny = img_block.height;
sll = ((3*nx + 3) / 4)*4; /* Must be multiple of 4. */
/*
* Buffer is potentially large enough that failure to allocate might be
* recoverable.
*/
pbuf = (char *)attemptckalloc(sll * ny * sizeof(char));
if (pbuf == 0) { /* Memory allocation failure. */
Tcl_AppendResult(interp,
"::tk::print::_gdi photo failed--out of memory", NULL);
return TCL_ERROR;
}
/* After this, all returns must go through retval. */
/* BITMAP expects BGR; photo provides RGB. */
for (k = 0; k < ny; k++) {
for (i = 0; i < nx; i++) {
pbuf[k*sll + 3*i] = img_block.pixelPtr[
k*img_block.pitch + i*img_block.pixelSize + img_block.offset[2]];
pbuf[k*sll + 3*i + 1] = img_block.pixelPtr[
k*img_block.pitch + i*img_block.pixelSize + img_block.offset[1]];
pbuf[k*sll + 3*i + 2] = img_block.pixelPtr[
k*img_block.pitch + i*img_block.pixelSize + img_block.offset[0]];
}
}
memset(&bitmapinfo, 0L, sizeof(BITMAPINFO));
bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapinfo.bmiHeader.biWidth = nx;
bitmapinfo.bmiHeader.biHeight = -ny;
bitmapinfo.bmiHeader.biPlanes = 1;
bitmapinfo.bmiHeader.biBitCount = 24;
bitmapinfo.bmiHeader.biCompression = BI_RGB;
bitmapinfo.bmiHeader.biSizeImage = 0; /* sll*ny;. */
bitmapinfo.bmiHeader.biXPelsPerMeter = 0;
bitmapinfo.bmiHeader.biYPelsPerMeter = 0;
bitmapinfo.bmiHeader.biClrUsed = 0;
bitmapinfo.bmiHeader.biClrImportant = 0;
oldmode = SetStretchBltMode(dst, HALFTONE);
/*
* According to the Win32 Programmer's Manual, we have to set the brush
* org, now.
*/
SetBrushOrgEx(dst, 0, 0, &pt);
if (dst_w <= 0) {
dst_w = nx;
dst_h = ny;
} else if (dst_h <= 0) {
dst_h = ny*dst_w / nx;
}
if (StretchDIBits(dst, dst_x, dst_y, dst_w, dst_h, 0, 0, nx, ny,
pbuf, &bitmapinfo, DIB_RGB_COLORS, SRCCOPY) == (int)GDI_ERROR) {
int errcode = GetLastError();
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"::tk::print::_gdi photo internal failure: "
"StretchDIBits error code %d", errcode));
retval = TCL_ERROR;
}
/* Clean up the hDC. */
if (oldmode != 0) {
SetStretchBltMode(dst, oldmode);
SetBrushOrgEx(dst, pt.x, pt.y, &pt);
}
ckfree(pbuf);
if (retval == TCL_OK) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"%d %d %d %d", dst_x, dst_y, dst_w, dst_h));
}
return retval;
}
/*
*----------------------------------------------------------------------
*
* Bezierize --
*
* Interface to Tk's line smoother, used for lines and pollies.
* Provided by Jasper Taylor <jasper.taylor@ed.ac.uk>.
*
* Results:
* Smooths lines.
*
*----------------------------------------------------------------------
*/
static int Bezierize(
POINT* polypoints,
int npoly,
int nStep,
POINT* bpointptr)
{
/* First, translate my points into a list of doubles. */
double *inPointList, *outPointList;
int n;
int nbpoints = 0;
POINT* bpoints;
inPointList = (double *)attemptckalloc(2 * sizeof(double) * npoly);
if (inPointList == 0) {
/* TODO: unreachable */
return nbpoints; /* 0. */
}
for (n=0; n<npoly; n++) {
inPointList[2*n] = polypoints[n].x;
inPointList[2*n + 1] = polypoints[n].y;
}
nbpoints = 1 + npoly * nStep; /* this is the upper limit. */
outPointList = (double *)attemptckalloc(2 * sizeof(double) * nbpoints);
if (outPointList == 0) {
/* TODO: unreachable */
ckfree((void *) inPointList);
return 0;
}
nbpoints = TkMakeBezierCurve(NULL, inPointList, npoly, nStep,
NULL, outPointList);
ckfree((void *) inPointList);
bpoints = (POINT *)attemptckalloc(sizeof(POINT)*nbpoints);
if (bpoints == 0) {
/* TODO: unreachable */
ckfree((void *) outPointList);
return 0;
}
for (n=0; n<nbpoints; n++) {
bpoints[n].x = (long) outPointList[2*n];
bpoints[n].y = (long) outPointList[2*n + 1];
}
ckfree((void *) outPointList);
*bpointptr = *bpoints;
return nbpoints;
}
/*
*----------------------------------------------------------------------
*
* GdiLine --
*
* Maps lines to GDI context.
*
* Results:
* Renders lines.
*
*----------------------------------------------------------------------
*/
static int GdiLine(
TCL_UNUSED(void *),
Tcl_Interp *interp,
int argc,
Tcl_Obj *const *objv)
{
static const char usage_message[] =
"::tk::print::_gdi line hdc x1 y1 ... xn yn "
"-arrow [first|last|both|none] -arrowshape {d1 d2 d3} "
"-dash dashlist "
"-capstyle [butt|projecting|round] -fill color "
"-joinstyle [bevel|miter|round] -smooth [true|false|bezier] "
"-splinesteps number -stipple bitmap -width linewid";
char *strend;
POINT *polypoints;
int npoly;
int x, y;
HDC hDC;
HPEN hPen;
LOGBRUSH lbrush;
HBRUSH hBrush = NULL;
int width = 0;
COLORREF linecolor = 0;
int dolinecolor = 0;
int dosmooth = 0;
int doarrow = 0; /* 0=none; 1=end; 2=start; 3=both. */
int arrowshape[3];
int nStep = 12;
int dodash = 0;
const char *dashdata = 0;
arrowshape[0] = 8;
arrowshape[1] = 10;
arrowshape[2] = 3;
/* Verrrrrry simple for now.... */
if (argc < 6) {
Tcl_AppendResult(interp, usage_message, NULL);
return TCL_ERROR;
}
hDC = printDC;
polypoints = (POINT *)attemptckalloc((argc - 1) * sizeof(POINT));
if (polypoints == 0) {
Tcl_AppendResult(interp, "Out of memory in GdiLine", NULL);
return TCL_ERROR;
}
if ((Tcl_GetIntFromObj(interp, objv[2], (int *)&polypoints[0].x) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[3], (int *)&polypoints[0].y) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[4], (int *)&polypoints[1].x) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[5], (int *)&polypoints[1].y) != TCL_OK)
) {
return TCL_ERROR;
}
argc -= 6;
objv += 6;
npoly = 2;
while (argc >= 2) {
/* Check for a number. */
x = strtoul(Tcl_GetString(objv[0]), &strend, 0);
if (strend > Tcl_GetString(objv[0])) {
/* One number.... */
y = strtoul(Tcl_GetString(objv[1]), &strend, 0);
if (strend > Tcl_GetString(objv[1])) {
/* TWO numbers!. */
polypoints[npoly].x = x;
polypoints[npoly].y = y;
npoly++;
argc -= 2;
objv += 2;
} else {
/* Only one number... Assume a usage error. */
ckfree((void *)polypoints);
Tcl_AppendResult(interp, usage_message, NULL);
return TCL_ERROR;
}
} else {
if (strcmp(Tcl_GetString(*objv), "-arrow") == 0) {
if (strcmp(Tcl_GetString(objv[1]), "none") == 0) {
doarrow = 0;
} else if (strcmp(Tcl_GetString(objv[1]), "both") == 0) {
doarrow = 3;
} else if (strcmp(Tcl_GetString(objv[1]), "first") == 0) {
doarrow = 2;
} else if (strcmp(Tcl_GetString(objv[1]), "last") == 0) {
doarrow = 1;
}
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-arrowshape") == 0) {
/* List of 3 numbers--set arrowshape array. */
int a1, a2, a3;
char dummy;
if (sscanf(Tcl_GetString(objv[1]), "%d%d%d%c", &a1, &a2, &a3, &dummy) == 3
&& a1 > 0 && a2 > 0 && a3 > 0) {
arrowshape[0] = a1;
arrowshape[1] = a2;
arrowshape[2] = a3;
}
/* Else the argument was bad. */
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-capstyle") == 0) {
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-fill") == 0) {
if (GdiGetColor(objv[1], &linecolor)) {
dolinecolor = 1;
}
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-joinstyle") == 0) {
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-smooth") == 0) {
/* Argument is true/false or 1/0 or bezier. */
if (Tcl_GetString(objv[1])) {
switch (Tcl_GetString(objv[1])[0]) {
case 't': case 'T':
case '1':
case 'b': case 'B': /* bezier. */
dosmooth = 1;
break;
default:
dosmooth = 0;
break;
}
objv += 2;
argc -= 2;
}
} else if (strcmp(Tcl_GetString(*objv), "-splinesteps") == 0) {
if (Tcl_GetIntFromObj(interp, objv[1], &nStep) != TCL_OK) {
return TCL_ERROR;
}
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-dash") == 0) {
if (Tcl_GetString(objv[1])) {
dodash = 1;
dashdata = Tcl_GetString(objv[1]);
}
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-dashoffset") == 0) {
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-stipple") == 0) {
objv += 2;
argc -= 2;
} else if (strcmp(Tcl_GetString(*objv), "-width") == 0) {
if (Tcl_GetIntFromObj(interp, objv[1], &width) != TCL_OK) {
return TCL_ERROR;
}
objv += 2;
argc -= 2;
} else { /* It's an unknown argument!. */
argc--;
objv++;
}
/* Check for arguments
* Most of the arguments affect the "Pen"
*/
}
}
if (width || dolinecolor || dodash) {
GdiMakePen(interp, width, dodash, dashdata,
0, 0, 0, 0, linecolor, hDC, (HGDIOBJ *)&hPen);
}
if (doarrow != 0) {
GdiMakeBrush(linecolor, 0, &lbrush, hDC, &hBrush);
}
if (dosmooth) { /* Use PolyBezier. */
int nbpoints;
POINT *bpoints = 0;
nbpoints = Bezierize(polypoints,npoly,nStep,bpoints);
if (nbpoints > 0) {
Polyline(hDC, bpoints, nbpoints);
} else {
Polyline(hDC, polypoints, npoly); /* Out of memory? Just draw a regular line. */
}
if (bpoints != 0) {
ckfree((void *)bpoints);
}
} else {
Polyline(hDC, polypoints, npoly);
}
if (dodash && doarrow) { /* Don't use dashed or thick pen for the arrows! */
GdiFreePen(interp, hDC, hPen);
GdiMakePen(interp, width, 0, 0, 0, 0, 0, 0,
linecolor, hDC, (HGDIOBJ *)&hPen);
}
/* Now the arrowheads, if any. */
if (doarrow & 1) {
/* Arrowhead at end = polypoints[npoly-1].x, polypoints[npoly-1].y. */
POINT ahead[6];
double dx, dy, length;
double sinTheta, cosTheta;
double vertX, vertY, temp;
double fracHeight;
fracHeight = 2.0 / arrowshape[2];
ahead[0].x = ahead[5].x = polypoints[npoly-1].x;
ahead[0].y = ahead[5].y = polypoints[npoly-1].y;
dx = ahead[0].x - polypoints[npoly-2].x;
dy = ahead[0].y - polypoints[npoly-2].y;
if ((length = hypot(dx, dy)) == 0) {
sinTheta = cosTheta = 0.0;
} else {
sinTheta = dy / length;
cosTheta = dx / length;
}
vertX = ahead[0].x - arrowshape[0]*cosTheta;
vertY = ahead[0].y - arrowshape[0]*sinTheta;
temp = arrowshape[2]*sinTheta;
ahead[1].x = (long)(ahead[0].x - arrowshape[1]*cosTheta + temp);
ahead[4].x = (long)(ahead[1].x - 2 * temp);
temp = arrowshape[2]*cosTheta;
ahead[1].y = (long)(ahead[0].y - arrowshape[1]*sinTheta - temp);
ahead[4].y = (long)(ahead[1].y + 2 * temp);
ahead[2].x = (long)(ahead[1].x*fracHeight + vertX*(1.0-fracHeight));
ahead[2].y = (long)(ahead[1].y*fracHeight + vertY*(1.0-fracHeight));
ahead[3].x = (long)(ahead[4].x*fracHeight + vertX*(1.0-fracHeight));
ahead[3].y = (long)(ahead[4].y*fracHeight + vertY*(1.0-fracHeight));
Polygon(hDC, ahead, 6);
}
if (doarrow & 2) {
/* Arrowhead at end = polypoints[0].x, polypoints[0].y. */
POINT ahead[6];
double dx, dy, length;
double sinTheta, cosTheta;
double vertX, vertY, temp;
double fracHeight;
fracHeight = 2.0 / arrowshape[2];
ahead[0].x = ahead[5].x = polypoints[0].x;
ahead[0].y = ahead[5].y = polypoints[0].y;
dx = ahead[0].x - polypoints[1].x;
dy = ahead[0].y - polypoints[1].y;
if ((length = hypot(dx, dy)) == 0) {
sinTheta = cosTheta = 0.0;
} else {
sinTheta = dy / length;
cosTheta = dx / length;
}
vertX = ahead[0].x - arrowshape[0]*cosTheta;
vertY = ahead[0].y - arrowshape[0]*sinTheta;
temp = arrowshape[2]*sinTheta;
ahead[1].x = (long)(ahead[0].x - arrowshape[1]*cosTheta + temp);
ahead[4].x = (long)(ahead[1].x - 2 * temp);
temp = arrowshape[2]*cosTheta;
ahead[1].y = (long)(ahead[0].y - arrowshape[1]*sinTheta - temp);
ahead[4].y = (long)(ahead[1].y + 2 * temp);
ahead[2].x = (long)(ahead[1].x*fracHeight + vertX*(1.0-fracHeight));
ahead[2].y = (long)(ahead[1].y*fracHeight + vertY*(1.0-fracHeight));
ahead[3].x = (long)(ahead[4].x*fracHeight + vertX*(1.0-fracHeight));
ahead[3].y = (long)(ahead[4].y*fracHeight + vertY*(1.0-fracHeight));
Polygon(hDC, ahead, 6);
}
if (width || dolinecolor || dodash) {
GdiFreePen(interp, hDC, hPen);
}
if (hBrush) {
GdiFreeBrush(interp, hDC, hBrush);
}
ckfree((void *)polypoints);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* GdiOval --
*
* Maps ovals to GDI context.
*
* Results:
* Renders ovals.
*
*----------------------------------------------------------------------
*/
static int GdiOval(
TCL_UNUSED(void *),
Tcl_Interp *interp,
int argc,
Tcl_Obj *const *objv)
{
static const char usage_message[] =
"::tk::print::_gdi oval hdc x1 y1 x2 y2 -fill color -outline color "
"-stipple bitmap -width linewid";
int x1, y1, x2, y2;
HDC hDC;
HPEN hPen;
int width = 0;
COLORREF linecolor = 0, fillcolor = 0;
int dolinecolor = 0, dofillcolor = 0;
HBRUSH hBrush = NULL;
LOGBRUSH lbrush;
HGDIOBJ oldobj = NULL;
int dodash = 0;
const char *dashdata = 0;
/* Verrrrrry simple for now.... */
if (argc < 6) {
Tcl_AppendResult(interp, usage_message, NULL);
return TCL_ERROR;
}
hDC = printDC;
if ((Tcl_GetIntFromObj(interp, objv[2], &x1) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[2], &y1) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[3], &x2) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[4], &y2) != TCL_OK)) {
return TCL_ERROR;
}
if (x1 > x2) {
int x3 = x1;
x1 = x2;
x2 = x3;
}
if (y1 > y2) {
int y3 = y1;
y1 = y2;
y2 = y3;
}
argc -= 6;
objv += 6;
while (argc > 0) {
/* Now handle any other arguments that occur. */
if (strcmp(Tcl_GetString(objv[0]), "-fill") == 0) {
if (Tcl_GetString(objv[1]) && GdiGetColor(objv[1], &fillcolor)) {
dofillcolor = 1;
}
} else if (strcmp(Tcl_GetString(objv[0]), "-outline") == 0) {
if (Tcl_GetString(objv[1]) && GdiGetColor(objv[1], &linecolor)) {
dolinecolor = 1;
}
} else if (strcmp(Tcl_GetString(objv[0]), "-stipple") == 0) {
/* Not actually implemented */
} else if (strcmp(Tcl_GetString(objv[0]), "-width") == 0) {
if (Tcl_GetString(objv[1])) {