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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
|
/*
winprint.c : implementation file
Time-stamp: "01/01/12 10:50:56 popineau"
Copyright (C) 1999
Fabrice Popineau <Fabrice.Popineau@supelec.fr>
This file is part of Windvi.
Windvi is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Windvi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with Windvi; if not, write to the Free Software Foundation, 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*
Supports 'print setup' and 'print' functions.
Not so easy to implement color printing.
Some remarks :
- it seems no possible to use rasterops
to do tramsparent blt
- a part from doing banding which might be
very expensive
- maybe we could build a list of affected rectangles
(rules and specials)
- and redraw the needed parts using the appropriate mode
*/
#include "wingui.h"
#include "xdvi-config.h"
#include <commdlg.h>
#include "gsdll.h"
#define DEBUG_PRINT 0
HWND hDlgPrint = 0, hWndParent = 0;
CHAR *msgFormat = "file %s (page %d in [%d - %d])";
CHAR msgBuffer[256];
BOOL isPrinting = FALSE, isPrintingDvips = FALSE;
int iFirstPage, iLastPage;
HDC hMemBandDC;
HBITMAP oldprintDIB, printDIB;
void *lpMemBandData;
struct BITMAPINFO_256 bmiMemBand;
BOOL bPrintError = FALSE;
BOOL bUserAbort = FALSE;
/*
Variables to save display configuration
*/
HDC save_maneDC;
int save_page, save_shrink;
struct WindowRec save_mane;
Pixel save_fore, save_back;
Boolean save_grey;
POINT saveScroll;
BOOL savedPS;
int save_maneRC;
int saveBitsPixel;
int save_u_paper_w, save_u_paper_h;
int save_offset_x, save_offset_y;
/*
Printercapabilities
*/
unsigned int iPrintBitsPixel, iPrintSizePal, iPrintColorRes, iPrintRasterCaps;
unsigned int iPrintOffsetX, iPrintOffsetY;
unsigned int nBandHeight, current_band, nBandNumbers;
unsigned int nBandMBSize = 2;
/*
extern definitions
*/
extern void redraw(struct WindowRec *);
extern BOOL IsOpenedDviFile();
extern void ReopenDviFile();
extern void prescan();
BOOL CALLBACK AbortProc(HDC hdc, int nCode)
{
MSG msg;
/* Retrieve and remove messages from the thread's message queue. */
while (!bUserAbort && PeekMessage(&msg, (HWND) NULL, 0, 0, PM_REMOVE)) {
/* Process any messages for the Cancel dialog box. */
if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
/* Return the global isPrinting flag (which is set to FALSE if the
user presses the Cancel button). */
return !bUserAbort;
UNREFERENCED_PARAMETER(hdc);
UNREFERENCED_PARAMETER(nCode);
}
LRESULT CALLBACK PrintDlgProc(HWND hwndDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_INITDIALOG: /* message: initialize dialog box */
/* Initialize the static text control. */
SetDlgItemText(hwndDlg, IDD_FILE, xbasename(dvi_name));
/* Initialize the progress bars. */
SendDlgItemMessage(hwndDlg, IDC_PRINT_PROGRESS_PAGE,
PBM_SETRANGE, (WPARAM) 0, MAKELPARAM(1, total_pages));
SendDlgItemMessage(hwndDlg, IDC_PRINT_PROGRESS_PAGE,
PBM_SETPOS, (WPARAM) 1, (LPARAM) 0);
SendDlgItemMessage(hwndDlg, IDC_PRINT_PROGRESS_PAGE,
PBM_SETBARCOLOR,
(WPARAM) 0, (LPARAM) (COLORREF) MYRGB(0, 255, 40));
SendDlgItemMessage(hwndDlg, IDC_PRINT_PROGRESS_BAND,
PBM_SETRANGE, (WPARAM) 0, MAKELPARAM(1, 100));
SendDlgItemMessage(hwndDlg, IDC_PRINT_PROGRESS_BAND,
PBM_SETPOS, (WPARAM) 1, (LPARAM) 0);
SendDlgItemMessage(hwndDlg, IDC_PRINT_PROGRESS_PAGE,
PBM_SETBARCOLOR,
(WPARAM) 0, (LPARAM) (COLORREF) MYRGB(0, 40, 255));
SetFocus(GetDlgItem(hwndDlg, IDD_CANCEL));
return 0; /* Because it is setting focus */
case WM_COMMAND: /* message: received a command */
/* User pressed "Cancel" button--stop print job. */
bUserAbort = TRUE;
EnableWindow(hWndParent, TRUE);
DestroyWindow(hwndDlg);
hDlgPrint = 0;
return 1;
}
return 0;
UNREFERENCED_PARAMETER(lParam);
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(message);
}
/****************************************************************************
* *
* FUNCTION : InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg) *
* *
* PURPOSE : Makes preliminary driver calls to set up print job. *
* *
* RETURNS : TRUE - if successful. *
* FALSE - otherwise. *
* *
****************************************************************************/
BOOL InitPrinting(HDC hDC, HWND hWnd, HANDLE hInst, LPSTR msg)
{
DOCINFO DocInfo;
bPrintError = FALSE; /* no errors yet */
bUserAbort = FALSE; /* user hasn't aborted */
hWndParent = hWnd; /* save for Enable at Term time */
hDlgPrint = CreateDialog (hInst, "ABORTPRINTDLG", hWndParent,
(DLGPROC)PrintDlgProc);
if (!hDlgPrint)
return FALSE;
EnableWindow (hWndParent, FALSE); /* disable parent */
/*
Use new printing APIs...Petrus Wong 12-May-1993
*/
if (SetAbortProc(hDC, (ABORTPROC)AbortProc) <= 0) {
bPrintError = TRUE;
return FALSE;
}
ZeroMemory(&DocInfo, sizeof(DocInfo));
DocInfo.cbSize = sizeof(DOCINFO);
DocInfo.lpszDocName = (LPTSTR) msg;
DocInfo.lpszOutput = NULL;
if (StartDoc(hDC, &DocInfo) <= 0) {
bPrintError = TRUE;
return FALSE;
}
bPrintError = FALSE;
/* might want to call the abort proc here to allow the user to
* abort just before printing begins */
return TRUE;
}
/****************************************************************************
* *
* FUNCTION : TermPrinting(HDC hDC) *
* *
* PURPOSE : Terminates print job. *
* *
****************************************************************************/
VOID TermPrinting(HDC hDC)
{
/*
Use new printing APIs...Petrus Wong 12-May-1993
*/
if (!bPrintError)
EndDoc(hDC);
if (bUserAbort)
AbortDoc(hDC);
else {
EnableWindow(hWndParent, TRUE);
DestroyWindow(hDlgPrint);
hDlgPrint = 0;
}
}
LRESULT CmdFilePrSetup (HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
{
PAGESETUPDLG pgsdlg;
return 0;
}
/***********************************************************************
*
* GetPrinterDC()
*
* Uses PrinDlg common dialog for printer selection and creates a dc.
* Returns NULL on error.
*
***********************************************************************/
HDC GetPrinterDC(HWND hWnd)
{
PRINTDLG pd;
char gszDevice[256];
char gszOutput[256];
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = hWnd;
pd.Flags = PD_ALLPAGES | PD_RETURNDC /* | PD_PRINTSETUP */;
pd.nFromPage = 1;
pd.nToPage = total_pages;
pd.nMinPage = 1;
pd.nMaxPage = total_pages;
pd.nCopies = 1;
iFirstPage = 0;
iLastPage = total_pages - 1;
if (PrintDlg(&pd) == TRUE) {
DEVNAMES *pDevNames;
DEVMODE *pDevMode;
pDevNames = GlobalLock(pd.hDevNames);
pDevMode = GlobalLock(pd.hDevMode);
lstrcpy((LPSTR)gszDevice,
(LPSTR)((char *)pDevNames+pDevNames->wDeviceOffset));
if(!lstrcmpi((LPSTR)((char *)pDevNames+pDevNames->wDeviceOffset),
(LPSTR)((char *)pDevNames+pDevNames->wOutputOffset)))
lstrcpy((LPSTR)gszOutput, "net:");
else
lstrcpy((LPSTR)gszOutput,
(LPSTR)((char *)pDevNames+pDevNames->wOutputOffset));
#if DEBUG_PRINT
if (pDevMode = pd.hDevMode) {
fprintf(stderr, "devmode: name = %s, colors = %s\n",
pDevMode->dmDeviceName,
pDevMode->dmColor == DMCOLOR_COLOR ? "color" : "mono");
}
fprintf(stderr, "gszDevice = %s\ngszOutput = %s\n",
gszDevice, gszOutput);
#endif
iFirstPage = pd.nFromPage - 1;
iLastPage = pd.nToPage - 1;
GlobalUnlock(pd.hDevNames);
GlobalUnlock(pd.hDevMode);
return pd.hDC;
}
else {
CommDlgExtendedError();
iFirstPage = iLastPage = 0;
return NULL;
}
}
BOOL SetupPageForPrinting(HDC hDC) {
int cxPage, cyPage, cxInch, cyInch;
/* Should we bother about the number of colors the printer
will support ?
Answer : YES !!!
*/
iPrintRasterCaps = GetDeviceCaps(hDC, RASTERCAPS);
/* Examine the raster capabilities of the device identified by hDC
to verify that it supports the BitBlt function. */
if (!(iPrintRasterCaps & RC_STRETCHDIB)) {
DeleteDC(hDC);
MessageBox(hWndParent,
"Printer cannot display bitmaps.",
"Device Error",
MB_OK);
return FALSE;
}
if (!(iPrintRasterCaps & RC_BITMAP64)) {
DeleteDC(hDC);
MessageBox(hWndParent,
"Printer cannot cope with bitmaps > 64k.",
"Device Error",
MB_OK);
return FALSE;
}
/* FIXME: there is a problem if Bits per Pixel is 1 !
Check with the dib palette.
*/
iPrintBitsPixel = GetDeviceCaps(hDC, BITSPIXEL);
iPrintSizePal = GetDeviceCaps(hDC, SIZEPALETTE);
iPrintColorRes = GetDeviceCaps(hDC, COLORRES);
iPrintOffsetX = GetDeviceCaps(hDC, PHYSICALOFFSETX);
iPrintOffsetY = GetDeviceCaps(hDC, PHYSICALOFFSETY);
/* There is a mess there with device not reporting colors ! */
iPrintBitsPixel = 24;
cxPage = GetDeviceCaps(hDC, HORZRES); /* Width of printr page - pixels */
cyPage = GetDeviceCaps(hDC, VERTRES); /* Height of printr page - pixels */
cxInch = GetDeviceCaps(hDC, LOGPIXELSX);/* Printer pixels per inch - X */
cyInch = GetDeviceCaps(hDC, LOGPIXELSY);/* Printer pixels per inch - Y */
SetStretchBltMode(hDC, COLORONCOLOR);
save_mane = mane;
save_shrink = shrink_factor;
save_fore = fore_Pixel;
save_back = back_Pixel;
save_grey = use_grey;
saveScroll.x = xCurrentScroll;
saveScroll.y = yCurrentScroll;
save_page = current_page;
save_maneDC = maneDC;
savedPS = resource._postscript;
save_maneRC = maneRasterCaps;
saveBitsPixel = maneBitsPixel;
save_u_paper_w = unshrunk_paper_w;
save_u_paper_h = unshrunk_paper_h;
save_offset_x = offset_x;
save_offset_y = offset_y;
offset_x -= iPrintOffsetX;
offset_y -= iPrintOffsetY;
xCurrentScroll = yCurrentScroll = 0;
shrink_factor = 1;
use_grey = False;
maneRasterCaps = iPrintRasterCaps;
maneBitsPixel = iPrintBitsPixel;
unshrunk_paper_w = cxPage;
unshrunk_paper_h = cyPage;
unshrunk_page_w = unshrunk_dvifile_page_w;
unshrunk_page_h = unshrunk_dvifile_page_h;
if (unshrunk_page_h < unshrunk_paper_h)
unshrunk_page_h = unshrunk_paper_h;
if (unshrunk_page_w < unshrunk_paper_w)
unshrunk_page_w = unshrunk_paper_w;
mane.shrinkfactor = 1;
mane.base_x = 0;
mane.base_y = 0;
mane.min_x = 0;
mane.min_y = 0;
mane.max_x = unshrunk_paper_w;
mane.max_y = unshrunk_paper_h;
mane.width = mane.max_x - mane.min_x;
mane.height = mane.max_y - mane.min_y;
init_page();
#if DEBUG_PRINT
fprintf(stderr, "printer page (%d x %d), cx = %d, cy = %d\n",
cxPage, cyPage, cxInch, cyInch);
fprintf(stderr, "bits/pixel = %d, colorres = %d, size pal = %d\n",
iPrintBitsPixel, iPrintColorRes, iPrintSizePal);
fprintf(stderr, "dvi paper (%d x %d)\n", unshrunk_paper_w, unshrunk_paper_h);
fprintf(stderr, "dvi unshrunk page (%d x %d)\n",
unshrunk_page_w, unshrunk_page_h);
fprintf(stderr, "dvi shrunk page (%d x %d)\n", page_w, page_h);
fprintf(stderr, "offset printer (%d , %d)\n", iPrintOffsetX, iPrintOffsetY);
#endif
#if 0
/* FIXME: change mode and dpi */
resource._pixels_per_inch = cxInch;
resource.mfmode = ?;
#endif
if (!IsOpenedDviFile())
ReopenDviFile();
scanned_page_bak = scanned_page = -1;
initcolor();
reset_fonts();
#ifdef XFORM
reset_xform_stack();
#endif
return TRUE;
}
BOOL RestorePageForDisplay()
{
fprintf(stderr, "Restoring for display...\n");
mane = save_mane;
shrink_factor = save_shrink;
current_page = save_page;
use_grey = save_grey;
xCurrentScroll = saveScroll.x;
yCurrentScroll = saveScroll.y;
fore_Pixel = save_fore;
back_Pixel = save_back;
offset_x = save_offset_x;
offset_y = save_offset_y;
unshrunk_paper_w = save_u_paper_w;
unshrunk_paper_h = save_u_paper_h;
unshrunk_page_w = unshrunk_dvifile_page_w;
unshrunk_page_h = unshrunk_dvifile_page_h;
if (unshrunk_page_h < unshrunk_paper_h)
unshrunk_page_h = unshrunk_paper_h;
if (unshrunk_page_w < unshrunk_paper_w)
unshrunk_page_w = unshrunk_paper_w;
init_page();
/*
User might abort before config has been changed
*/
maneBitsPixel = saveBitsPixel;
maneRasterCaps = save_maneRC;
foreGC = ruleGC = highGC = maneDC = save_maneDC;
scanned_page = scanned_page_reset = -1;
#ifdef XFORM
reset_xform_stack();
#endif
initcolor();
reset_fonts();
resource._postscript = savedPS;
return TRUE;
}
void PrepareForBanding(HDC hDC)
{
RGBQUAD QuadWhite = { 255, 255, 255, 0};
RGBQUAD QuadBlack = { 0, 0, 0, 0};
/* Create a memory DC that is compatible with the printer DC */
hMemBandDC = CreateCompatibleDC(hDC);
SetStretchBltMode(hMemBandDC, COLORONCOLOR);
#ifdef TRANSFORM
if (IS_WIN98 || IS_NT)
SetGraphicsMode(hMemBandDC, GM_ADVANCED);
#endif
/*
Do banding. We can't afford more than say 6Mb at a time.
Calculate the number of rows at the current dpi.
Print as many bands as needed.
*/
nBandNumbers = ROUNDUP(unshrunk_paper_w * unshrunk_paper_h * iPrintBitsPixel,
nBandMBSize * 1024 * 1024 * 8) + 1;
nBandHeight = ROUNDUP(unshrunk_paper_h, nBandNumbers);
SendDlgItemMessage(hDlgPrint, IDC_PRINT_PROGRESS_BAND,
PBM_SETRANGE,
(WPARAM) 0, (LPARAM) MAKELPARAM(1, nBandNumbers));
SendDlgItemMessage(hDlgPrint, IDC_PRINT_PROGRESS_BAND,
PBM_SETPOS,
(WPARAM) 1, (LPARAM) 0);
mane.base_y = 0;
mane.max_y = nBandHeight;
mane.height = mane.max_y - mane.min_y;
#if DEBUG_PRINT
fprintf(stderr, "Band numbers = %d, Band height = %d, page_h = %d\n",
nBandNumbers, nBandHeight, unshrunk_paper_h);
#endif
/* Create a DIB for printing into it. We might need maneDIB to do redraw
while printing */
printDIB = CreateDIB(hMemBandDC, unshrunk_paper_w + 1, nBandHeight + 1,
iPrintBitsPixel,
&bmiMemBand, &lpMemBandData);
/* FIXME : there is a problem with monochrome printing. */
if (iPrintBitsPixel == 1) {
bmiMemBand.bmiColors[1] = QuadWhite;
bmiMemBand.bmiColors[0] = QuadBlack;
}
/* Put the DIB into the memory DC */
oldprintDIB = SelectObject(hMemBandDC, printDIB);
if (resource._postscript) {
ps_destroy();
initGS();
}
}
static BOOL bBandInit = FALSE;
void FreeBandingRc()
{
SelectObject(hMemBandDC, oldprintDIB);
DeleteObject(printDIB);
DeleteObject(hMemBandDC);
if (resource._postscript) {
ps_destroy();
initGS();
}
bBandInit = FALSE;
}
void DrawPage(HDC hPrinterDC, int page)
{
if (!IsOpenedDviFile())
ReopenDviFile();
current_page = page;
bColorPage = psToDisplay = FALSE;
#if 1
/* FIXME: really needed ? */
init_colors();
#endif
#ifdef TRANSFORM
/* FIXME : there should be a global stack in case
such transformations would span over several pages. */
if (resource.use_xform)
reset_xfrm_stack();
#endif
if (!bBandInit) {
bBandInit = TRUE;
PrepareForBanding(hPrinterDC);
}
if (scanned_page < current_page)
prescan();
#if DEBUG_PRINT
fprintf(stderr, "page %d, bColorPage = %d psToDisplay = %d\n",
page, bColorPage, psToDisplay);
#endif
if (bColorPage || psToDisplay) {
/* Specials, do banding */
maneDC = foreGC = ruleGC = highGC = hMemBandDC;
for (current_band = 0; current_band < nBandNumbers; current_band++) {
#if DEBUG_PRINT
fprintf(stderr, "Printing page %d band %d\n",
page, current_band);
#endif
mane.base_y = current_band*nBandHeight;
mane.max_y = min(nBandHeight, unshrunk_paper_h - current_band * nBandHeight) + 1;
mane.height = mane.max_y - mane.min_y;
if (resource._postscript && psToDisplay && gs_device) {
extern void
init_ps_page(int llx, int lly, int urx, int ury, int height);
extern void gs_io(char *);
gs_io("H initgraphics\n");
init_ps_page(0,0,
mane.max_x - mane.min_x,
mane.max_y - mane.min_y,
mane.base_y + mane.max_y);
gs_io("\nstop\n%%xdvimark\n");
}
/* FIXME : paint background color */
if (get_back_color(current_page) != MYRGB(255,255,255))
fprintf(stderr, "this page is white, but should be %-8x\n",
get_back_color(current_page));
/* FIXME: check about the + 1 on each dimension ! */
if (PatBlt(foreGC, 0, 0, unshrunk_paper_w + 1, nBandHeight + 1,
WHITENESS) == 0)
Win32Error("Redraw Page background");
/* First pass for specials */
allowDrawingChars = FALSE;
redraw(&mane);
#if DEBUG_PRINT
fprintf(stderr, "PS state: resource._postscript = %d, psToDisplay = %d, gs_device = %d\n",
resource._postscript, psToDisplay, gs_device);
#endif
if (resource._postscript && psToDisplay && gs_device) {
/* if gs is active, try to make it paint its picture */
BITMAPINFO_256 bmih;
LPBYTE ppbyte;
(*pgsdll_lock_device)(gs_device, 1);
/* Get a pointer to the bitmap in gs interpreter */
(*pgsdll_get_bitmap_row)(gs_device, &(bmih.bmiHeader), bmih.bmiColors, &ppbyte, 0);
#if DEBUG_PRINT
fprintf(stderr,
"bmih : %d x %d x %d at %x (colors : used %d at %x)\n",
bmih.bmiHeader.biWidth,
bmih.bmiHeader.biHeight,
bmih.bmiHeader.biBitCount,
ppbyte,
bmih.bmiHeader.biClrUsed,
bmih.bmiColors);
#endif
if (StretchDIBits(foreGC,
mane.min_x,
mane.min_y,
mane.max_x - mane.min_x,
mane.max_y - mane.min_y,
0,
0,
mane.max_x - mane.min_x,
mane.max_y - mane.min_y,
ppbyte, (LPBITMAPINFO)&bmih,
(bmih.bmiHeader.biClrUsed ? DIB_PAL_COLORS : DIB_RGB_COLORS),
SRCCOPY) == GDI_ERROR)
Win32Error("Print/GS/StretchDIBits");
(*pgsdll_lock_device)(gs_device, 0);
}
/* Second pass for glyphs */
allowDrawingChars = TRUE;
redraw(&mane);
#if 0
fprintf(stderr, "before bitblt ...");
#endif
#if DEBUG_PRINT
fprintf(stderr, "Printing band to (%d, %d) - (%d x %d)\n",
mane.base_x + mane.min_x,
mane.base_y + mane.min_y,
mane.max_x - mane.min_x,
mane.max_y - mane.min_y);
#endif
#if 0
SelectObject(hMemBandDC, oldPrintDIB);
#endif
if (StretchDIBits(hPrinterDC,
mane.base_x + mane.min_x,
mane.base_y + mane.min_y,
mane.max_x - mane.min_x,
mane.max_y - mane.min_y,
0, 0,
mane.max_x - mane.min_x,
mane.max_y - mane.min_y,
lpMemBandData, (LPBITMAPINFO)&bmiMemBand,
(bmiMemBand.bmiHeader.biClrUsed ? DIB_PAL_COLORS : DIB_RGB_COLORS),
SRCCOPY) == GDI_ERROR)
Win32Error("WinPrint/StretchDIBits()");
if (hDlgPrint)
SendDlgItemMessage(hDlgPrint, IDC_PRINT_PROGRESS_BAND,
PBM_SETPOS, (WPARAM) current_band, (LPARAM) 0);
}
}
else {
/* No color or PS specials on this page,
draw directly onto the hPrinterDC */
mane.base_x = 0; mane.base_y = 0;
mane.min_x = 0; mane.max_x = unshrunk_paper_w;
mane.min_y = 0; mane.max_y = unshrunk_paper_h;
maneDC = foreGC = ruleGC = highGC = hPrinterDC;
allowDrawingChars = TRUE;
redraw(&mane);
}
if (hDlgPrint)
SendDlgItemMessage(hDlgPrint, IDC_PRINT_PROGRESS_PAGE,
PBM_SETPOS, (WPARAM) page, (LPARAM) 0);
}
LRESULT CmdFilePrint (HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
{
HDC hPrinterDC;
int page;
if (!(hPrinterDC = GetPrinterDC(hwnd)))
return 0;
#if 0
__asm int 3;
#endif
/* Set the flag used by the AbortPrintJob dialog procedure. */
if (InitPrinting(hPrinterDC, hwnd, hInst, dvi_name)) {
/* Reset pre-scanning */
scanned_page = scanned_page_reset = -1;
SendDlgItemMessage(hDlgPrint, IDC_PRINT_PROGRESS_PAGE,
PBM_SETRANGE,
(WPARAM) 0, (LPARAM) MAKELPARAM(iFirstPage, iLastPage));
SendDlgItemMessage(hDlgPrint, IDC_PRINT_PROGRESS_PAGE,
PBM_SETSTEP,
(WPARAM) 1, (LPARAM) 0);
isPrinting = TRUE;
if (!SetupPageForPrinting(hPrinterDC))
goto Abort;
for (page = iFirstPage; page <= iLastPage; page++) {
if (StartPage(hPrinterDC) <= 0) {
Win32Error("CmdFilePrint/StartPage");
break;
}
if (bUserAbort) {
break;
}
else {
DrawPage(hPrinterDC, page);
}
if (EndPage(hPrinterDC) <= 0) {
Win32Error("CmdFilePrint/StartPage");
break;
}
}
Abort:
RestorePageForDisplay();
if (bBandInit)
FreeBandingRc();
isPrinting = FALSE;
TermPrinting(hPrinterDC);
}
DeleteDC(hPrinterDC);
return 0;
}
#if 0
BOOL GuessPostScriptPrinter()
{
bFound = FALSE;
for (each device in [Devices] section of win.ini) {
/* extract the necessary fields from the ini line */
szDriverName = driver name extracted from ini line
szModelName = left side of ini line (the key)
szPort = port name extracted from ini line.
hIC = CreateIC(szDriverName, szModelName, szPort, NULL);
if (hIC) {
/* see if driver supports GETTECHNOLOGY escape */
wEscape = GETTECHNOLOGY;
if (Escape(hIC, QUERYESCSUPPORT, sizeof(WORD), &wEscape, NULL))
{
Escape(hIC, GETTECHNOLOGY, 0, NULL,
&szTechnology);
/* Check that the string starts with PostScript
* by doing a case-insensitive search. Allow
* for the possibility that the string could be
* longer, like "PostScript level 2" or some other
* extension.
*/
if (beginning of string is "PostScript")
bFound = TRUE;
}
DeleteDC(hIC);
}
/* if the driver has been found break out */
if (bFound)
break;
}
if (bFound) {
PostScript driver is szDriverName, model is szModelName, port is
szPort.
}
}
NOTE: In the event that GETTECHNOLOGY is not supported by some printer drivers, another method need to be used to determine if the printer is a PostScript printer. One possible method is to use QUERYESCSUPPORT on escapes that are only implemented by PostScript printers. For example:
EPSPRINTING
SETLINEJOIN
SETMITERLIMIT
SET_POLY_MODE
Similarly, you can determine a PCL printer by calling QUERYESCSUPPORT on the following escape:
DRAWPATTERNRECT
============================================================================================================
This will print a file on a PS printer for example.
/* FILE: spool.c */
#include <windows.h>
#include <print.h>
#include <commdlg.h>
#include <string.h>
// Function prototypes
BOOL FAR PASCAL PrintFile(LPSTR, HDC, HGLOBAL, HGLOBAL);
VOID SendFile(HDC, LPSTR);
HDC GetPrinterDC(HGLOBAL, HGLOBAL);
BOOL CALLBACK __export PrintAbortProc(HDC, int);
// Play with this number
#define BUFSIZE 2048
// Convenient structure for use with PASSTHROUGH escape
typedef struct
{
WORD wSize;
BYTE bData[2]; // placeholder
} PASSTHROUGHSTRUCT, FAR *LPPTS;
BOOL bAbort; // Global printing abort flag
//*************************************************************
//
// PrintFile()
//
// Purpose:
// Reads a file and copies it to a printer using the
// PASSTHROUGH escape.
//
// Parameters:
// LPSTR szFile - Pointer to path/filename to print
// HDC hPrnDC - Handle to printer DC or NULL
// HGLOBAL hDevNames - Handle to DEVNAMES struct or NULL
// HGLOBAL hDevMode - Handle to DEVMODE struct or NULL
//
// Return:
// Returns nonzero for success or zero for failure.
//
// Comments:
// hDevNames and hDevMode are only used if hPrnDC is NULL.
// If both hPrnDC and hDevNames are NULL, the default
// printer is used.
//
// History: Date Author Comment
// 6/03/93 JMS Created
//
//*************************************************************
BOOL FAR PASCAL PrintFile ( LPSTR szFile,
HDC hPrnDC,
HGLOBAL hDevNames,
HGLOBAL hDevMode )
{
int iEsc;
BOOL bLocalDC = TRUE; // Assume we must create a DC (hPrnDC == NULL)
bAbort = FALSE; // Haven't aborted yet
// Make sure we have a printer DC
if (!hPrnDC)
hPrnDC = GetPrinterDC(hDevNames, hDevMode);
else
bLocalDC = FALSE; // Use passed in hPrnDC
if (!hPrnDC)
return FALSE;
// PASSTHROUGH is required. If driver doesn't support it, bail out.
iEsc = PASSTHROUGH;
if (!Escape(hPrnDC, QUERYESCSUPPORT, sizeof(int), (LPSTR)&iEsc, NULL))
{
bAbort = TRUE;
goto MSFCleanUp;
}
// If we created the DC, install an abort procedure. We don't have
// a Cancel dialog box, but the abort proc enables multitasking.
// (Use __export and compile with -GA or -GD so we don't need
// a MakeProcInstance.)
if (bLocalDC)
Escape (hPrnDC, SETABORTPROC, 0, (LPSTR) PrintAbortProc, NULL);
// Call EPSPRINTING if it is supported (that is, if we're on a
// PostScript printer) to suppress downloading the pscript header.
iEsc = EPSPRINTING;
if (Escape(hPrnDC, QUERYESCSUPPORT, sizeof(int), (LPSTR)&iEsc, NULL))
{
iEsc = 1; // 1 == enable PASSTHROUGH (disable pscript header)
Escape(hPrnDC, EPSPRINTING, sizeof(int), (LPSTR)&iEsc, NULL);
}
SendFile(hPrnDC, szFile); // Send file to printer (could do multiple
// files)
MSFCleanUp: // Done
if (bLocalDC) // Only delete DC if we created it
DeleteDC(hPrnDC);
return !bAbort;
} /* PrintFile() */
VOID SendFile(HDC hPrnDC, LPSTR szFile)
{
static LPPTS lpPTS=NULL; // Pointer to PASSTHROUGHSTRUCT
OFSTRUCT ofs;
HFILE hFile;
hFile = OpenFile((LPSTR) szFile, &ofs, OF_READ);
if (hFile == HFILE_ERROR)
{
bAbort = TRUE; // Can't open file!
return;
}
if (!lpPTS &&
!(lpPTS = (LPPTS)GlobalLock(GlobalAlloc(GPTR, sizeof(WORD) +
BUFSIZE))))
{
bAbort = TRUE; // Can't allocate memory for buffer!
return;
}
Escape (hPrnDC, STARTDOC, 0, "", NULL);
// Loop through the file, reading a chunk at a time and passing
// it to the printer. QueryAbort calls the abort procedure, which
// processes messages so we don't tie up the whole system.
// We could skip the QueryAbort, in which case we wouldn't need
// to set an abort proc at all.
do {
if ((lpPTS->wSize=_lread(hFile, lpPTS->bData, BUFSIZE)) ==
HFILE_ERROR)
{
bAbort = TRUE; // error reading file
break;
}
Escape(hPrnDC, PASSTHROUGH, NULL, (LPSTR)lpPTS, NULL);
}
while ((lpPTS->wSize == BUFSIZE) && QueryAbort(hPrnDC, 0));
if (!bAbort)
Escape(hPrnDC, ENDDOC, NULL, NULL, NULL);
_lclose(hFile);
} /* SendFile() */
HDC GetPrinterDC(HGLOBAL hDevNames, HGLOBAL hDevMode)
{
HDC hdc;
char szPrinter[64];
LPSTR szDevice=NULL, szDriver=NULL, szOutput=NULL;
LPDEVMODE lpdm;
if (hDevNames)
{
LPDEVNAMES lpdn = (LPDEVNAMES) GlobalLock(hDevNames);
szDriver = (LPSTR) lpdn + lpdn->wDriverOffset;
szDevice = (LPSTR) lpdn + lpdn->wDeviceOffset;
szOutput = (LPSTR) lpdn + lpdn->wOutputOffset;
if (hDevMode)
lpdm = (LPDEVMODE) GlobalLock(hDevMode);
}
else
{ // Get default printer info
GetProfileString ("windows", "device", "", szPrinter, 64);
if (!((szDevice = strtok (szPrinter, "," )) &&
(szDriver = strtok (NULL, ", ")) &&
(szOutput = strtok (NULL, ", "))))
return NULL; // No default printer
lpdm = NULL; // Don't use DEVMODE with default printer
}
hdc = CreateDC(szDriver, szDevice, szOutput, lpdm);
if (hDevMode && lpdm)
GlobalUnlock(hDevMode);
if (hDevNames)
GlobalUnlock(hDevNames);
return hdc;
} /* GetPrinterDC() */
BOOL CALLBACK __export PrintAbortProc(HDC hdc, int code)
{
MSG msg;
while (!bAbort && PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (!bAbort);
} /* PrintAbortProc() */
/*** EOF: spool.c ***/
=============================================================================================================
To identify the printer as a PostScript printer, use this code:
int gPrCode = 0; // Set according to platform.
if( Win32s ) // Using the Win16 driver.
{
gPrCode = PASSTHROUGH;
if((Escape(printerIC, GETTECHNOLOGY, NULL, NULL, (LPSTR)szTech) &&
!lstrcmp(szTech, "PostScript")) &&
Escape(printerIC, QUERYESCSUPPORT, sizeof(int),
(LPSTR)gPrCode, NULL )
{
// The printer is PostScript.
...
}
}
else // Using Win32 driver under Windows NT.
{
gPrCode = POSTSCRIPT_PASSTHROUGH; // Fails with Win16 driver
if( Escape(printerIC, QUERYESCSUPPORT, sizeof(int), (LPSTR)gPrCode,
NULL))
{
// The printer is PostScript.
...
}
}
Printing
To send PostScript data to the printer on either platform, use this code:
// Assuming a buffer, szPSBuf, of max size MAX_PSBUF containing
// nPSData bytes of PostScript data.
char szBuf[MAX_PSBUF+sizeof(short)];
// Store length in buffer.
*((short *)szBuf) = nPSData;
// Store data in buffer.
memcpy( (char *)szBuf + sizeof(short), szPSBuf, nPSData );
// Note that gPrCode (set when identifying the printer) depends on
// the platform.
Escape( printerDC, gPrCode, (int) nPSData, szBuf, NULL );
However, your output may appear scaled or translated incorrectly or data may be transformed off the page under Win32s.
The origin and scale for Windows printer drivers is not the PostScript default (bottom left/72 dpi) but is instead at the upper left and at the device scale(300 dpi). Therefore, before sending data to the printer, you may need to send a couple of PostScript commands to scale or translate the matrix. For example, for scaling, send the following escape to scale the PostScript transform to 72 dpi:
xres = GetDeviceCaps(printerDC, LOGPIXELSX);
yres = GetDeviceCaps(printerDC, LOGPIXELSY);
// Two leading spaces for the following operation.
wsprintf(szBuf, " %d 72 div %d 72 div scale\n", xres, yres);
// Put actual size into buffer
*((short *)szBuf) = strlen(szBuf)-2;
Escape( printerDC, gPrCode, strlen(szBuf)-2, szBuf, NULL );
#endif
#if 0
/* ******************************************************************** */
/* */
/* print.c */
/* */
/* Source file for Device-Independent Bitmap (DIB) API. Provides */
/* the following functions: */
/* */
/* PrintWindow() - Prints all or part of a window */
/* PrintScreen() - Prints the entire screen */
/* PrintDIB() - Prints the specified DIB */
/* */
/* Written by Microsoft Product Support Services, Developer Support. */
/* Copyright 1991-1998 Microsoft Corporation. All rights reserved. */
/* ******************************************************************** */
#define STRICT /* enable strict type checking */
#include <windows.h>
#include <string.h>
#include "dibdll.h" /* Header for printing dialog & DLL instance handle */
#include "dibapi.h" /* Header for DIB functions */
#include "dibutil.h" /* Auxiliary functions */
extern HANDLE ghDLLInst; /* Global handle to DLL's instance */
/***************************************************************
* Typedefs
**************************************************************/
/* Structure used for Banding */
typedef struct
{
BOOL bGraphics;
BOOL bText;
RECT GraphicsRect;
} BANDINFOSTRUCT;
/****************************************************************
* Variables
***************************************************************/
HWND hDlgAbort; /* Handle to Abort Dialog */
char szPrintDlg[] = "PRINTING"; /* Name of Print dialog from .RC */
BOOL bAbort = FALSE; /* Abort a print operation? */
char gszDevice[50]; /* Keeps track out device (e.g. "HP LaserJet") */
char gszOutput[50]; /* Output device (e.g. "LPT1:") */
/***************************************************************
* Function prototypes for functions local to this module
**************************************************************/
BOOL CALLBACK PrintAbortProc(HDC, int);
int CALLBACK PrintAbortDlg(HWND, UINT, WPARAM, LPARAM);
WORD PrintBand(HDC, LPRECT, LPRECT, BOOL, BOOL, LPBITMAPINFOHEADER, LPSTR);
HDC GetPrinterDC(void);
void CalculatePrintRect(HDC, LPRECT, WORD, DWORD, DWORD);
/**********************************************************************
*
* PrintDIB()
*
* Description:
*
* This routine prints the specified DIB. The actual printing is done
* in the PrintBand() routine (see below), this procedure drives the
* printing operation. PrintDIB() has the code to handle both banding
* and non-banding printers. A banding printer can be distinguished by
* the GetDeviceCaps() API (see the code below). On banding devices,
* must repeatedly call the NEXTBAND escape to get the next banding
* rectangle to print into. If the device supports the BANDINFO escape,
* it should be used to determine whether the band "wants" text or
* graphics (or both). On non-banding devices, we can ignore all this
* and call PrintBand() on the entire page.
*
* Parameters:
*
* HDIB hDib - Handle to dib to be printed
*
* WORD fPrintOpt - tells which print option to use (PW_BESTFIT,
* PW_STRETCHTOPAGE, OR PW_SCALE)
*
* WORD wXScale, wYScale - X and Y scaling factors (integers) for
* printed output if the PW_SCALE option is used.
*
* LPSTR szJobName - Name that you would like to give to this print job (this
* name shows up in the Print Manager as well as the
* "Now Printing..." dialog box).
*
* Return Value: (see errors.h for description)
*
* One of: ERR_INVALIDHANDLE
* ERR_LOCK
* ERR_SETABORTPROC
* ERR_STARTDOC
* ERR_NEWFRAME
* ERR_ENDDOC
* ERR_GETDC
* ERR_STRETCHDIBITS
*
********************************************************************/
WORD PrintDIB(HDIB hDib, WORD fPrintOpt, WORD wXScale, WORD wYScale,
LPSTR szJobName)
{
HDC hPrnDC; /* DC to the printer */
RECT rect; /* Rect structure used for banding */
LPSTR lpBits; /* pointer to the DIB bits */
LPBITMAPINFOHEADER lpDIBHdr; /* Pointer to DIB header */
int nBandCount = 0; /* used for print dialog box to count bands */
WORD wErrorCode = 0; /* Error code to return */
RECT rPrintRect; /* specifies the area on the printer */
/* (in printer coordinates) which we */
/* want the DIB to go to */
char szBuffer[70]; /* Buffer to hold message for "Printing" dlg box */
char szJobNameTrunc[35]; /* szJobName truncated to 31 */
/* characters, since STARTDOC can't */
/* accept a string longer than 31 */
DOCINFO DocInfo; /* structure for StartDoc */
int nTemp; /* used to check banding capability */
CHAR lpBuffer[128]; /* Buffer for strings retrieved from resources */
/* Paramter validation */
if (!hDib)
return ERR_INVALIDHANDLE;
/* Get pointer to DIB header */
lpDIBHdr = (LPBITMAPINFOHEADER)GlobalLock(hDib);
if (!lpDIBHdr) /* Check that we have a valid pointer */
return ERR_LOCK;
lpBits = FindDIBBits((LPSTR)lpDIBHdr); /* Find pointer to DIB bits */
if (hPrnDC = GetPrinterDC())
{
SetStretchBltMode(hPrnDC, COLORONCOLOR);
/* Determine rPrintRect (printer area to print to) from the */
/* fPrintOpt. Fill in rPrintRect.left and .top from wXScale and */
/* wYScale just in case we use PW_SCALE (see the function */
/* CalculatePrintRect). */
rPrintRect.left = wXScale;
rPrintRect.top = wYScale;
CalculatePrintRect(hPrnDC, &rPrintRect, fPrintOpt, lpDIBHdr->biWidth,
lpDIBHdr->biHeight);
/* Initialize the abort procedure. */
hDlgAbort = CreateDialog(ghDLLInst, szPrintDlg, GetFocus(),
(DLGPROC)PrintAbortDlg);
/* ISet the text inside the dialog to the name of our print job */
lstrcpy(szJobNameTrunc, szJobName);
szJobNameTrunc[31] = '\0'; /* Truncate string to 31 chars */
LoadString(ghDLLInst, IDS_PRINTMSG, lpBuffer, sizeof(lpBuffer));
wsprintf(szBuffer, lpBuffer, (LPSTR)szJobNameTrunc);
SetDlgItemText(hDlgAbort, IDC_PRINTTEXT1, (LPSTR)szBuffer);
/* Set global variable bAbort to FALSE. This will get set to TRUE */
/* in our PrintAbortDlg() procedure if the user selects the */
/* CANCEL button in our dialog box */
bAbort = FALSE;
/* set up the Abort Procedure */
if (SetAbortProc(hPrnDC, (ABORTPROC)PrintAbortProc) < 0)
return ERR_SETABORTPROC;
/* start print job */
ZeroMemory(&DocInfo, sizeof(DOCINFO));
DocInfo.cbSize = sizeof(DOCINFO);
DocInfo.lpszDocName = (LPTSTR)szJobNameTrunc;
DocInfo.lpszOutput = NULL;
if (StartDoc(hPrnDC, &DocInfo) <= 0)
{
/* Oops, something happened, let's clean up here and return */
DestroyWindow(hDlgAbort); /* Remove abort dialog box */
DeleteDC(hPrnDC);
GlobalUnlock(hDib);
return ERR_STARTDOC;
}
/* Note: the following banding code applies to Windows 3.1. With the new */
/* printing architecture of Win32, send out both the graphics and */
/* text in one band (like a non-banding device). This code is used */
/* for Win32s since Win32s depends on Windows 3.1 printing architecture. */
/* */
/* Check if need to do banding. If we do, loop through */
/* each band in the page, calling NEXTBAND and BANDINFO */
/* (if supported) calling PrintBand() on the band. Else, */
/* call PrintBand() with the entire page as our clipping */
/* rectangle! */
/* If Wincap32 is running on Win32s, then use banding */
nTemp = NEXTBAND;
if (Escape(hPrnDC, QUERYESCSUPPORT, sizeof(int), (LPSTR)&nTemp, NULL) &&
(GetVersion() & 0x80000000) && (LOWORD(GetVersion()) == 3))
{
BOOL bBandInfoDevice;
BANDINFOSTRUCT biBandInfo; /* Used for banding */
/* Fill in initial values for our BandInfo Structure to */
/* tell driver we can want to do graphics and text, and */
/* also which area we want the graphics to go in. */
biBandInfo.bGraphics = TRUE;
biBandInfo.bText = TRUE;
biBandInfo.GraphicsRect = rPrintRect;
/* Check if device supports the BANDINFO escape. */
nTemp = BANDINFO;
bBandInfoDevice = Escape(hPrnDC, QUERYESCSUPPORT, sizeof(int),
(LPSTR)&nTemp, NULL);
/* Do each band -- Call Escape() with NEXTBAND, then the */
/* rect structure returned is the area where we are to */
/* print in. This loop exits when the rect area is empty. */
while (Escape(hPrnDC, NEXTBAND, 0, NULL, (LPSTR)&rect) && !
IsRectEmpty(&rect))
{
char szTmpBuf[100];
/* Do the BANDINFO, if needed. */
if (bBandInfoDevice)
Escape(hPrnDC, BANDINFO, sizeof(BANDINFOSTRUCT), (LPSTR)&
biBandInfo, (LPSTR)&biBandInfo);
LoadString(ghDLLInst, IDS_BANDNMBR, lpBuffer, sizeof(lpBuffer));
wsprintf(szTmpBuf, lpBuffer, ++nBandCount);
SetDlgItemText(hDlgAbort, IDC_PERCENTAGE, (LPSTR)szTmpBuf);
/* Call PrintBand() to do actual output into band. */
/* Pass in our band-info flags to tell what sort */
/* of data to output into the band. Note that on */
/* non-banding devices, we pass in the default bandinfo */
/* stuff set above (i.e. bText=TRUE, bGraphics=TRUE). */
wErrorCode = PrintBand(hPrnDC, &rPrintRect, &rect,
biBandInfo.bText, biBandInfo.bGraphics, lpDIBHdr,
lpBits);
}
}
else
{
/* Print the whole page -- non-banding device. */
if (StartPage(hPrnDC) <= 0)
return ERR_STARTPAGE;
rect = rPrintRect;
LoadString(ghDLLInst, IDS_SENDINGBAND, lpBuffer, sizeof(lpBuffer));
SetDlgItemText(hDlgAbort, IDC_PERCENTAGE, lpBuffer);
wErrorCode = PrintBand(hPrnDC, &rPrintRect, &rect, TRUE, TRUE,
lpDIBHdr, lpBits);
/* Non-banding devices need a NEWFRAME */
if (EndPage(hPrnDC) <= 0)
return ERR_ENDPAGE;
}
/* End the print operation. Only send the ENDDOC if */
/* we didn't abort or error. */
if (!bAbort)
{
/* We errored out on ENDDOC, but don't return here - we still */
/* need to close the dialog box, free proc instances, etc. */
if (EndDoc(hPrnDC) <= 0)
wErrorCode = ERR_ENDDOC;
DestroyWindow(hDlgAbort);
}
/* All done, clean up. */
DeleteDC(hPrnDC);
}
else
wErrorCode = ERR_GETDC; /* Couldn't get Printer DC! */
GlobalUnlock(hDib);
return wErrorCode;
}
/* ******************************************************************* */
/* Auxiliary Functions */
/* -- Local to this module only */
/* ******************************************************************* */
/*********************************************************************
*
* CalculatePrintRect()
*
* Given fPrintOpt and a size of the DIB, return the area on the
* printer where the image should go (in printer coordinates). If
* fPrintOpt is PW_SCALE, then lpPrintRect.left and .top should
* contain WORDs which specify the scaling factor for the X and
* Y directions, respecively.
*
********************************************************************/
void CalculatePrintRect(HDC hDC, LPRECT lpPrintRect, WORD fPrintOpt,
DWORD cxDIB, DWORD cyDIB)
{
int cxPage, cyPage, cxInch, cyInch;
if (!hDC)
return;
/* Get some info from printer driver */
cxPage = GetDeviceCaps(hDC, HORZRES); /* Width of printr page - pixels */
cyPage = GetDeviceCaps(hDC, VERTRES); /* Height of printr page - pixels */
cxInch = GetDeviceCaps(hDC, LOGPIXELSX);/* Printer pixels per inch - X */
cyInch = GetDeviceCaps(hDC, LOGPIXELSY);/* Printer pixels per inch - Y */
switch (fPrintOpt)
{
/* Best Fit case -- create a rectangle which preserves */
/* the DIB's aspect ratio, and fills the page horizontally. */
/* The formula in the "->bottom" field below calculates the Y */
/* position of the printed bitmap, based on the size of the */
/* bitmap, the width of the page, and the relative size of */
/* a printed pixel (cyInch / cxInch). */
case PW_BESTFIT:
lpPrintRect->top = 0;
lpPrintRect->left = 0;
lpPrintRect->bottom = (int)(((double)cyDIB * cxPage * cyInch) /
((double)cxDIB * cxInch));
lpPrintRect->right = cxPage;
break;
/* Scaling option -- lpPrintRect's top/left contain */
/* multipliers to multiply the DIB's height/width by. */
case PW_SCALE:
{
int cxMult, cyMult;
cxMult = lpPrintRect->left;
cyMult = lpPrintRect->top;
lpPrintRect->top = 0;
lpPrintRect->left = 0;
lpPrintRect->bottom = (int)(cyDIB * cyMult);
lpPrintRect->right = (int)(cxDIB * cxMult);
break;
}
/* Stretch To Page case -- create a rectangle */
/* which covers the entire printing page (note that this */
/* is also the default). */
case PW_STRETCHTOPAGE:
default:
lpPrintRect->top = 0;
lpPrintRect->left = 0;
lpPrintRect->bottom = cyPage;
lpPrintRect->right = cxPage;
break;
}
}
/*********************************************************************
*
* PrintBand()
*
* This routine does ALL output to the printer. It is called from
* the PrintDIB() routine. It is called for both banding and non-
* banding printing devices. lpRectClip contains the rectangular
* area we should do our output into (i.e. we should clip our output
* to this area). The flags fDoText and fDoGraphics should be set
* appropriately (if we want any text output to the rectangle, set
* fDoText to true). Normally these flags are returned on banding
* devices which support the BANDINFO escape.
*
********************************************************************/
WORD PrintBand(HDC hDC, LPRECT lpRectOut, LPRECT lpRectClip, BOOL fDoText,
BOOL fDoGraphics, LPBITMAPINFOHEADER lpDIBHdr, LPSTR lpDIBBits)
{
RECT rect; /* Temporary rectangle */
double dblXScaling, /* X and Y scaling factors */
dblYScaling;
WORD wReturn = 0; /* Return code */
if (fDoGraphics)
{
dblXScaling = ((double)lpRectOut->right - lpRectOut->left) / (double)
lpDIBHdr->biWidth;
dblYScaling = ((double)lpRectOut->bottom - lpRectOut->top) / (double)
lpDIBHdr->biHeight;
/* Now we set up a temporary rectangle -- this rectangle */
/* holds the coordinates on the paper where our bitmap */
/* WILL be output. We can intersect this rectangle with */
/* the lpClipRect to see what we NEED to output to this */
/* band. Then, we determine the coordinates in the DIB */
/* to which this rectangle corresponds (using dbl?Scaling). */
IntersectRect(&rect, lpRectOut, lpRectClip);
if (!IsRectEmpty(&rect))
{
RECT rectIn;
rectIn.left = (int)((rect.left - lpRectOut->left) / dblXScaling +
0.5
);
rectIn.top = (int)((rect.top - lpRectOut->top) / dblYScaling + 0.5);
rectIn.right = (int)(rectIn.left + (rect.right - rect.left) /
dblXScaling + 0.5);
rectIn.bottom = (int)(rectIn.top + (rect.bottom - rect.top) /
dblYScaling + 0.5);
if (!StretchDIBits(hDC, rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
rectIn.left, (int)(lpDIBHdr->biHeight) -
rectIn.top - (rectIn.bottom - rectIn.top),
rectIn.right - rectIn.left, rectIn.bottom - rectIn.top,
lpDIBBits, (LPBITMAPINFO)lpDIBHdr, DIB_RGB_COLORS,
SRCCOPY))
wReturn = ERR_STRETCHDIBITS; /* StretchDIBits() failed! */
}
}
return wReturn;
}
/***********************************************************************
*
* GetPrinterDC()
*
* Uses PrinDlg common dialog for printer selection and creates a dc.
* Returns NULL on error.
*
***********************************************************************/
HDC GetPrinterDC() {
PRINTDLG pd;
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(PRINTDLG);
pd.Flags = PD_RETURNDC;
if (PrintDlg(&pd) == TRUE)
{
DEVNAMES *pDevNames = GlobalLock(pd.hDevNames);
lstrcpy((LPSTR)gszDevice,
(LPSTR)((char *)pDevNames+pDevNames->wDeviceOffset));
if(!lstrcmpi((LPSTR)((char *)pDevNames+pDevNames->wDeviceOffset),
(LPSTR)((char *)pDevNames+pDevNames->wOutputOffset)))
lstrcpy((LPSTR)gszOutput, "net:");
else
lstrcpy((LPSTR)gszOutput,
(LPSTR)((char *)pDevNames+pDevNames->wOutputOffset));
GlobalUnlock(pd.hDevNames);
return pd.hDC;
}
else
return NULL;
}
/**********************************************************************
* PrintAbortProc()
*
* Abort procedure - contains the message loop while printing is
* in progress. By using a PeekMessage() loop, multitasking
* can occur during printing.
*
**********************************************************************/
BOOL CALLBACK PrintAbortProc(HDC hDC, int code)
{
MSG msg;
while (!bAbort && PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
if (!IsDialogMessage(hDlgAbort, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return !bAbort;
}
/***********************************************************************
*
* PrintAbortDlg()
*
*
* This is the Dialog Procedure which will handle the "Now Printing"
* dialog box. When the user presses the "Cancel" button, the
* global variable bAbort is set to TRUE, which causes the
* PrintAbortProc to exit, which in turn causes the printing
* operation to terminate.
*
***********************************************************************/
int CALLBACK PrintAbortDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
{
char szBuffer[100];
/* Fill in the text which specifies where this bitmap */
/* is going ("on HP LaserJet on LPT1", for example) */
LoadString(ghDLLInst, IDS_ABORTSTRING, msgBuffer, sizeof(msgBuffer));
wsprintf(szBuffer, msgBuffer, (LPSTR)gszDevice,
(LPSTR)gszOutput);
SetDlgItemText(hWnd, IDC_PRINTTEXT2, (LPSTR)szBuffer);
SetFocus(GetDlgItem(hWnd, IDCANCEL));
return TRUE; /* Return TRUE because we called SetFocus() */
}
case WM_COMMAND:
bAbort = TRUE;
DestroyWindow(hWnd);
return TRUE;
break;
}
return FALSE;
}
#endif
#define NEW_DIB_FORMAT(lpbih) (lpbih->biSize != sizeof(BITMAPCOREHEADER))
/******************************************************************************
* *
* FUNCTION : DIBNumColors(LPVOID lpv) *
* *
* PURPOSE : Determines the number of colors in the DIB by looking at *
* the BitCount and ClrUsed fields in the info block. *
* *
* RETURNS : The number of colors in the DIB. With DIBS with more than *
* 8-bits-per-pixel that have a color table table included, *
* then the return value will be the number of colors in the *
* color table rather than the number of colors in the DIB. *
* *
* *
*****************************************************************************/
WORD DIBNumColors (LPVOID lpv)
{
INT bits;
LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpv;
LPBITMAPCOREHEADER lpbch = (LPBITMAPCOREHEADER)lpv;
/* With the BITMAPINFO format headers, the size of the palette
* is in biClrUsed, whereas in the BITMAPCORE - style headers, it
* is dependent on the bits per pixel ( = 2 raised to the power of
* bits/pixel).
*/
if (NEW_DIB_FORMAT(lpbih)) {
if (lpbih->biClrUsed != 0)
return (WORD)lpbih->biClrUsed;
bits = lpbih->biBitCount;
}
else
bits = lpbch->bcBitCount;
if (bits > 8)
return 0; /* Since biClrUsed is 0, we dont have a an optimal palette */
else
return (1 << bits);
}
/******************************************************************************
* *
* FUNCTION : ColorTableSize(LPVOID lpv) *
* *
* PURPOSE : Calculates the palette size in bytes. If the info. block *
* is of the BITMAPCOREHEADER type, the number of colors is *
* multiplied by 3 to give the palette size, otherwise the *
* number of colors is multiplied by 4. *
* *
* RETURNS : Color table size in number of bytes. *
* *
*****************************************************************************/
WORD ColorTableSize (LPVOID lpv)
{
LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpv;
if (NEW_DIB_FORMAT(lpbih))
{
if (((LPBITMAPINFOHEADER)(lpbih))->biCompression == BI_BITFIELDS)
/* Remember that 16/32bpp dibs can still have a color table */
return (sizeof(DWORD) * 3) + (DIBNumColors (lpbih) * sizeof (RGBQUAD));
else
return (DIBNumColors (lpbih) * sizeof (RGBQUAD));
}
else
return (DIBNumColors (lpbih) * sizeof (RGBTRIPLE));
}
|