forked from sumatrapdfreader/sumatrapdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint.cpp
executable file
·1158 lines (1020 loc) · 40.2 KB
/
Print.cpp
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
/* Copyright 2021 the SumatraPDF project authors (see AUTHORS file).
License: GPLv3 */
#include "utils/BaseUtil.h"
#include "utils/ScopedWin.h"
#include "utils/FileUtil.h"
#include "utils/UITask.h"
#include "utils/WinUtil.h"
#include "utils/Log.h"
#include "wingui/TreeModel.h"
#include "DisplayMode.h"
#include "Controller.h"
#include "EngineBase.h"
#include "EngineAll.h"
#include "SettingsStructs.h"
#include "GlobalPrefs.h"
#include "ChmModel.h"
#include "DisplayModel.h"
#include "ProgressUpdateUI.h"
#include "TextSelection.h"
#include "TextSearch.h"
#include "Notifications.h"
#include "SumatraPDF.h"
#include "WindowInfo.h"
#include "TabInfo.h"
#include "AppUtil.h"
#include "Print.h"
#include "Selection.h"
#include "SumatraDialogs.h"
#include "SumatraProperties.h"
#include "Translations.h"
class AbortCookieManager {
CRITICAL_SECTION cookieAccess;
public:
AbortCookie* cookie = nullptr;
AbortCookieManager() {
InitializeCriticalSection(&cookieAccess);
}
~AbortCookieManager() {
Clear();
DeleteCriticalSection(&cookieAccess);
}
void Abort() {
ScopedCritSec scope(&cookieAccess);
if (cookie) {
cookie->Abort();
}
Clear();
}
void Clear() {
ScopedCritSec scope(&cookieAccess);
if (cookie) {
delete cookie;
cookie = nullptr;
}
}
};
struct PrintData {
Printer* printer{nullptr};
EngineBase* engine{nullptr};
Vec<PRINTPAGERANGE> ranges; // empty when printing a selection
Vec<SelectionOnPage> sel; // empty when printing a page range
Print_Advanced_Data advData;
int rotation{0};
ProgressUpdateUI* progressUI{nullptr};
AbortCookieManager* abortCookie{nullptr};
PrintData(EngineBase* engine, Printer* printer, Vec<PRINTPAGERANGE>& ranges, Print_Advanced_Data& advData,
int rotation = 0, Vec<SelectionOnPage>* sel = nullptr) {
this->printer = printer;
this->advData = advData;
this->rotation = rotation;
if (engine) {
this->engine = engine->Clone();
}
if (!sel) {
this->ranges = ranges;
} else {
this->sel = *sel;
}
}
~PrintData() {
delete printer;
delete engine;
}
};
void Printer::SetDevMode(DEVMODEW* dm) {
free((void*)devMode);
devMode = dm;
}
Printer::~Printer() {
str::Free(name);
free((void*)devMode);
for (int i = 0; i < nPaperSizes; i++) {
str::Free(paperNames[i]);
}
for (int i = 0; i < nBins; i++) {
str::Free(binNames[i]);
}
free((void*)papers);
free((void*)paperNames);
free((void*)paperSizes);
free((void*)bins);
free((void*)binNames);
}
// get all the important info about a printer
Printer* NewPrinter(WCHAR* printerName) {
HANDLE hPrinter{nullptr};
LONG ret{0};
Printer* printer{nullptr};
BOOL ok = OpenPrinterW(printerName, &hPrinter, nullptr);
if (!ok) {
return nullptr;
}
LONG structSize = 0;
LPDEVMODE devMode = nullptr;
DWORD needed = 0;
GetPrinterW(hPrinter, 2, nullptr, 0, &needed);
PRINTER_INFO_2* info = (PRINTER_INFO_2*)AllocArray<BYTE>(needed);
if (info) {
ok = GetPrinterW(hPrinter, 2, (LPBYTE)info, needed, &needed);
}
if (!ok || !info || needed <= sizeof(PRINTER_INFO_2)) {
goto Exit;
}
/* ask for the size of DEVMODE struct */
structSize = DocumentPropertiesW(nullptr, hPrinter, printerName, nullptr, nullptr, 0);
if (structSize < sizeof(DEVMODEW)) {
// if (displayErrors) {
// MessageBoxWarning(nullptr, _TR("Could not obtain Printer properties"), _TR("Printing problem."));
//}
goto Exit;
}
devMode = (DEVMODEW*)Allocator::AllocZero(nullptr, structSize);
// Get the default DevMode for the printer and modify it for your needs.
ret = DocumentPropertiesW(nullptr, hPrinter, printerName, devMode, nullptr, DM_OUT_BUFFER);
if (IDOK != ret) {
// if (displayErrors) {
// MessageBoxWarning(nullptr, _TR("Could not obtain Printer properties"), _TR("Printing problem."));
//}
goto Exit;
}
printer = new Printer();
printer->name = str::Dup(printerName);
printer->devMode = devMode;
printer->info = info;
{
DWORD n = DeviceCapabilitiesW(printerName, nullptr, DC_PAPERS, nullptr, nullptr);
DWORD n2 = DeviceCapabilitiesW(printerName, nullptr, DC_PAPERNAMES, nullptr, nullptr);
DWORD n3 = DeviceCapabilitiesW(printerName, nullptr, DC_PAPERSIZE, nullptr, nullptr);
if (n != n2 || n != n3 || 0 == n || ((DWORD)-1 == n)) {
delete printer;
return nullptr;
}
printer->nPaperSizes = (int)n;
size_t paperNameSize = 64;
printer->papers = AllocArray<WORD>(n);
WCHAR* paperNamesSeq = AllocArray<WCHAR>(paperNameSize * (size_t)n + 1); // +1 is "just in case"
printer->paperSizes = AllocArray<POINT>(n);
printer->paperNames = AllocArray<WCHAR*>(n);
DeviceCapabilitiesW(printerName, nullptr, DC_PAPERS, (WCHAR*)printer->papers, nullptr);
DeviceCapabilitiesW(printerName, nullptr, DC_PAPERNAMES, paperNamesSeq, nullptr);
DeviceCapabilitiesW(printerName, nullptr, DC_PAPERSIZE, (WCHAR*)printer->paperSizes, nullptr);
WCHAR* paperName = paperNamesSeq;
for (int i = 0; i < (int)n; i++) {
printer->paperNames[i] = str::Dup(paperName);
paperName += paperNameSize;
}
str::Free(paperNamesSeq);
}
{
DWORD n = DeviceCapabilitiesW(printerName, nullptr, DC_BINS, nullptr, nullptr);
DWORD n2 = DeviceCapabilitiesW(printerName, nullptr, DC_BINNAMES, nullptr, nullptr);
if (n != n2 || ((DWORD)-1 == n)) {
delete printer;
return nullptr;
}
printer->nBins = n;
// it's ok for nBins to be 0, it means there's only one, default bin
if (n > 0) {
size_t binNameSize = 24;
printer->bins = AllocArray<WORD>(n);
printer->binNames = AllocArray<WCHAR*>(n);
WCHAR* binNamesSeq = AllocArray<WCHAR>(binNameSize * n + 1); // +1 is "just in case"
DeviceCapabilitiesW(printerName, nullptr, DC_BINS, (WCHAR*)printer->bins, nullptr);
DeviceCapabilitiesW(printerName, nullptr, DC_BINNAMES, binNamesSeq, nullptr);
WCHAR* binName = binNamesSeq;
for (int i = 0; i < (int)n; i++) {
printer->binNames[i] = str::Dup(binName);
binName += binNameSize;
}
str::Free(binNamesSeq);
}
}
{
DWORD n;
n = DeviceCapabilitiesW(printerName, nullptr, DC_COLLATE, nullptr, nullptr);
printer->canCallate = (n > 0);
n = DeviceCapabilitiesW(printerName, nullptr, DC_COLORDEVICE, nullptr, nullptr);
printer->isColor = (n > 0);
n = DeviceCapabilitiesW(printerName, nullptr, DC_DUPLEX, nullptr, nullptr);
printer->isDuplex = (n > 0);
n = DeviceCapabilitiesW(printerName, nullptr, DC_STAPLE, nullptr, nullptr);
printer->canStaple = (n > 0);
n = DeviceCapabilitiesW(printerName, nullptr, DC_ORIENTATION, nullptr, nullptr);
printer->orientation = n;
}
Exit:
ClosePrinter(hPrinter);
return printer;
}
static void SetCustomPaperSize(Printer* printer, SizeF size) {
auto devMode = printer->devMode;
devMode->dmPaperSize = 0;
devMode->dmPaperWidth = size.dx;
devMode->dmPaperLength = size.dy;
devMode->dmFields |= DM_PAPERSIZE | DM_PAPERWIDTH | DM_PAPERLENGTH;
}
// Make sure dy > dx i.e. it's tall not wide
static Size NormalizePaperSize(Size s) {
if (s.dy > s.dx) {
return Size(s.dx, s.dy);
}
return Size(s.dy, s.dx);
}
static void MessageBoxWarningCond(bool show, const WCHAR* msg, const WCHAR* title) {
logf(L"%s: %s\n", title, msg);
if (!show) {
return;
}
MessageBoxWarning(nullptr, msg, title);
}
static RectF BoundSelectionOnPage(const Vec<SelectionOnPage>& sel, int pageNo) {
RectF bounds;
for (size_t i = 0; i < sel.size(); i++) {
if (sel.at(i).pageNo == pageNo) {
bounds = bounds.Union(sel.at(i).rect);
}
}
return bounds;
}
static bool PrintToDevice(const PrintData& pd) {
CrashIf(!pd.engine);
if (!pd.engine) {
return false;
}
CrashIf(!pd.printer);
if (!pd.printer) {
return false;
}
auto progressUI = pd.progressUI;
auto abortCookie = pd.abortCookie;
EngineBase& engine = *pd.engine;
AutoFreeWstr fileName;
DOCINFOW di{};
di.cbSize = sizeof(DOCINFO);
if (gPluginMode) {
fileName.Set(url::GetFileName(gPluginURL));
// fall back to a generic "filename" instead of the more confusing temporary filename
di.lpszDocName = fileName ? fileName.Get() : L"filename";
} else {
di.lpszDocName = engine.FileName();
}
int current = 1, total = 0;
if (pd.sel.size() == 0) {
for (size_t i = 0; i < pd.ranges.size(); i++) {
if (pd.ranges.at(i).nToPage < pd.ranges.at(i).nFromPage) {
total += pd.ranges.at(i).nFromPage - pd.ranges.at(i).nToPage + 1;
} else {
total += pd.ranges.at(i).nToPage - pd.ranges.at(i).nFromPage + 1;
}
}
} else {
for (int pageNo = 1; pageNo <= engine.PageCount(); pageNo++) {
if (!BoundSelectionOnPage(pd.sel, pageNo).IsEmpty()) {
total++;
}
}
}
CrashIf(total <= 0);
if (0 == total) {
return false;
}
if (progressUI) {
progressUI->UpdateProgress(current, total);
}
auto devMode = pd.printer->devMode;
// http://blogs.msdn.com/b/oldnewthing/archive/2012/11/09/10367057.aspx
AutoDeleteDC hdc(CreateDCW(nullptr, pd.printer->name, nullptr, devMode));
if (!hdc) {
return false;
}
// for PDF Printer, this shows a file dialog to pick file name for destination PDF
if (StartDoc(hdc, &di) <= 0) {
return false;
}
// MM_TEXT: Each logical unit is mapped to one device pixel.
// Positive x is to the right; positive y is down.
SetMapMode(hdc, MM_TEXT);
const Size paperSize(GetDeviceCaps(hdc, PHYSICALWIDTH), GetDeviceCaps(hdc, PHYSICALHEIGHT));
const Rect printable(GetDeviceCaps(hdc, PHYSICALOFFSETX), GetDeviceCaps(hdc, PHYSICALOFFSETY),
GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES));
float fileDPI = engine.GetFileDPI();
float px = (float)GetDeviceCaps(hdc, LOGPIXELSX);
float py = (float)GetDeviceCaps(hdc, LOGPIXELSY);
float dpiFactor = std::min(px / fileDPI, py / fileDPI);
bool bPrintPortrait = paperSize.dx < paperSize.dy;
if (devMode && (devMode->dmFields & DM_ORIENTATION)) {
bPrintPortrait = DMORIENT_PORTRAIT == devMode->dmOrientation;
}
if (pd.advData.rotation == PrintRotationAdv::Portrait) {
bPrintPortrait = true;
} else if (pd.advData.rotation == PrintRotationAdv::Landscape) {
bPrintPortrait = false;
}
if (pd.sel.size() > 0) {
for (int pageNo = 1; pageNo <= engine.PageCount(); pageNo++) {
RectF bounds = BoundSelectionOnPage(pd.sel, pageNo);
if (bounds.IsEmpty()) {
continue;
}
if (progressUI) {
progressUI->UpdateProgress(current, total);
}
StartPage(hdc);
SizeF bSize = bounds.Size();
float zoom = std::min((float)printable.dx / bSize.dx, (float)printable.dy / bSize.dy);
// use the correct zoom values, if the page fits otherwise
// and the user didn't ask for anything else (default setting)
if (PrintScaleAdv::Shrink == pd.advData.scale) {
zoom = std::min(dpiFactor, zoom);
} else if (PrintScaleAdv::None == pd.advData.scale) {
zoom = dpiFactor;
}
for (size_t i = 0; i < pd.sel.size(); i++) {
if (pd.sel.at(i).pageNo != pageNo) {
continue;
}
RectF* clipRegion = &pd.sel.at(i).rect;
Point offset((int)((clipRegion->x - bounds.x) * zoom), (int)((clipRegion->y - bounds.y) * zoom));
if (pd.advData.scale != PrintScaleAdv::None) {
// center the selection on the physical paper
offset.x += (int)(printable.dx - bSize.dx * zoom) / 2;
offset.y += (int)(printable.dy - bSize.dy * zoom) / 2;
}
bool ok = false;
short shrink = 1;
do {
RenderPageArgs args(pd.sel.at(i).pageNo, zoom / shrink, pd.rotation, clipRegion,
RenderTarget::Print);
if (abortCookie) {
args.cookie_out = &abortCookie->cookie;
}
RenderedBitmap* bmp = engine.RenderPage(args);
if (abortCookie) {
abortCookie->Clear();
}
if (bmp && bmp->GetBitmap()) {
Rect rc(offset.x, offset.y, bmp->Size().dx * shrink, bmp->Size().dy * shrink);
ok = bmp->StretchDIBits(hdc, rc);
}
delete bmp;
shrink *= 2;
} while (!ok && shrink < 32 && !(progressUI && progressUI->WasCanceled()));
}
// TODO: abort if !ok?
if (EndPage(hdc) <= 0 || (progressUI && progressUI->WasCanceled())) {
AbortDoc(hdc);
return false;
}
current++;
}
EndDoc(hdc);
return false;
}
// print all the pages the user requested
for (size_t i = 0; i < pd.ranges.size(); i++) {
int dir = pd.ranges.at(i).nFromPage > pd.ranges.at(i).nToPage ? -1 : 1;
for (DWORD pageNo = pd.ranges.at(i).nFromPage; pageNo != pd.ranges.at(i).nToPage + dir; pageNo += dir) {
if ((PrintRangeAdv::Even == pd.advData.range && pageNo % 2 != 0) ||
(PrintRangeAdv::Odd == pd.advData.range && pageNo % 2 == 0)) {
continue;
}
if (progressUI) {
progressUI->UpdateProgress(current, total);
}
StartPage(hdc);
SizeF pSize = engine.PageMediabox(pageNo).Size();
int rotation = 0;
// Turn the document by 90 deg if it isn't in portrait mode
if (pSize.dx > pSize.dy) {
rotation += 90;
std::swap(pSize.dx, pSize.dy);
}
// make sure not to print upside-down
rotation = (rotation % 180) == 0 ? 0 : 270;
// finally turn the page by (another) 90 deg in landscape mode
if (!bPrintPortrait) {
rotation = (rotation + 90) % 360;
std::swap(pSize.dx, pSize.dy);
}
// dpiFactor means no physical zoom
float zoom = dpiFactor;
// offset of the top-left corner of the page from the printable area
// (negative values move the page into the left/top margins, etc.);
// offset adjustments are needed because the GDI coordinate system
// starts at the corner of the printable area and we rather want to
// center the page on the physical paper (except for PrintScaleNone
// where the page starts at the very top left of the physical paper so
// that printing forms/labels of varying size remains reliably possible)
Point offset(-printable.x, -printable.y);
if (pd.advData.scale != PrintScaleAdv::None) {
// make sure to fit all content into the printable area when scaling
// and the whole document page on the physical paper
RectF rect = engine.PageContentBox(pageNo, RenderTarget::Print);
RectF cbox = engine.Transform(rect, pageNo, 1.0, rotation);
zoom = std::min((float)printable.dx / cbox.dx,
std::min((float)printable.dy / cbox.dy,
std::min((float)paperSize.dx / pSize.dx, (float)paperSize.dy / pSize.dy)));
// use the correct zoom values, if the page fits otherwise
// and the user didn't ask for anything else (default setting)
if (PrintScaleAdv::Shrink == pd.advData.scale && dpiFactor < zoom) {
zoom = dpiFactor;
}
// center the page on the physical paper
offset.x += (int)(paperSize.dx - pSize.dx * zoom) / 2;
offset.y += (int)(paperSize.dy - pSize.dy * zoom) / 2;
// make sure that no content lies in the non-printable paper margins
RectF onPaper(printable.x + offset.x + cbox.x * zoom, printable.y + offset.y + cbox.y * zoom,
cbox.dx * zoom, cbox.dy * zoom);
if (onPaper.x < printable.x) {
offset.x += (int)(printable.x - onPaper.x);
} else if (onPaper.BR().x > printable.BR().x) {
offset.x -= (int)(onPaper.BR().x - printable.BR().x);
}
if (onPaper.y < printable.y) {
offset.y += (int)(printable.y - onPaper.y);
} else if (onPaper.BR().y > printable.BR().y) {
offset.y -= (int)(onPaper.BR().y - printable.BR().y);
}
}
bool ok = false;
short shrink = 1;
do {
RenderPageArgs args(pageNo, zoom / shrink, rotation, nullptr, RenderTarget::Print);
if (abortCookie) {
args.cookie_out = &abortCookie->cookie;
}
RenderedBitmap* bmp = engine.RenderPage(args);
if (abortCookie) {
abortCookie->Clear();
}
if (bmp && bmp->GetBitmap()) {
auto size = bmp->Size();
Rect rc(offset.x, offset.y, size.dx * shrink, size.dy * shrink);
ok = bmp->StretchDIBits(hdc, rc);
}
delete bmp;
shrink *= 2;
} while (!ok && shrink < 32 && !(progressUI && progressUI->WasCanceled()));
// TODO: abort if !ok?
if (EndPage(hdc) <= 0 || (progressUI && progressUI->WasCanceled())) {
AbortDoc(hdc);
return false;
}
current++;
}
}
EndDoc(hdc);
return true;
}
class PrintThreadData : public ProgressUpdateUI {
public:
NotificationWnd* wnd = nullptr;
AbortCookieManager cookie;
bool isCanceled = false;
WindowInfo* win = nullptr;
PrintData* data = nullptr;
HANDLE thread = nullptr; // close the print thread handle after execution
PrintThreadData(WindowInfo* win, PrintData* data) {
this->win = win;
this->data = data;
wnd = new NotificationWnd(win->hwndCanvas, 0);
wnd->wndRemovedCb = [this](NotificationWnd* wnd) { this->RemoveNotification(wnd); };
wnd->Create(L"", _TR("Printing page %d of %d..."));
// don't use a groupId for this notification so that
// multiple printing notifications could coexist between tabs
win->notifications->Add(wnd, nullptr);
}
PrintThreadData(PrintThreadData const&) = delete;
PrintThreadData& operator=(PrintThreadData const&) = delete;
~PrintThreadData() override {
CloseHandle(thread);
delete data;
RemoveNotification(wnd);
}
// called when printing has been canceled
void RemoveNotification(NotificationWnd* wnd) {
isCanceled = true;
cookie.Abort();
this->wnd = nullptr;
if (WindowInfoStillValid(win)) {
win->notifications->RemoveNotification(wnd);
}
}
void UpdateProgress(int current, int total) override {
uitask::Post([=] {
if (WindowInfoStillValid(win) && win->notifications->Contains(wnd)) {
wnd->UpdateProgress(current, total);
}
});
}
bool WasCanceled() override {
return isCanceled || !WindowInfoStillValid(win) || win->printCanceled;
}
};
static DWORD WINAPI PrintThread(LPVOID data) {
PrintThreadData* threadData = (PrintThreadData*)data;
WindowInfo* win = threadData->win;
// wait for PrintToDeviceOnThread to return so that we
// close the correct handle to the current printing thread
while (!win->printThread) {
Sleep(1);
}
HANDLE thread = threadData->thread = win->printThread;
PrintData* pd = threadData->data;
pd->progressUI = threadData;
pd->abortCookie = &threadData->cookie;
PrintToDevice(*pd);
uitask::Post([=] {
if (WindowInfoStillValid(win) && thread == win->printThread) {
win->printThread = nullptr;
}
delete threadData;
});
DestroyTempAllocator();
return 0;
}
static void PrintToDeviceOnThread(WindowInfo* win, PrintData* data) {
CrashIf(win->printThread);
PrintThreadData* threadData = new PrintThreadData(win, data);
win->printThread = nullptr;
win->printThread = CreateThread(nullptr, 0, PrintThread, threadData, 0, nullptr);
}
void AbortPrinting(WindowInfo* win) {
if (win->printThread) {
win->printCanceled = true;
WaitForSingleObject(win->printThread, INFINITE);
}
win->printCanceled = false;
}
static HGLOBAL GlobalMemDup(const void* data, size_t len) {
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, len);
if (!hGlobal) {
return nullptr;
}
void* globalData = GlobalLock(hGlobal);
if (!globalData) {
GlobalFree(hGlobal);
return nullptr;
}
memcpy(globalData, data, len);
GlobalUnlock(hGlobal);
return hGlobal;
}
/* Show Print Dialog box to allow user to select the printer
and the pages to print.
For reference: In order to print with Adobe Reader instead: ViewWithAcrobat(win, L"/P");
Note: The following only applies for printing as image
Creates a new dummy page for each page with a large zoom factor,
and then uses StretchDIBits to copy this to the printer's dc.
So far have tested printing from XP to
- Acrobat Professional 6 (note that acrobat is usually set to
downgrade the resolution of its bitmaps to 150dpi)
- HP Laserjet 2300d
- HP Deskjet D4160
- Lexmark Z515 inkjet, which should cover most bases.
*/
enum { MAXPAGERANGES = 10 };
void OnMenuPrint(WindowInfo* win, bool waitForCompletion) {
// we remember some printer settings per process
static ScopedMem<DEVMODE> defaultDevMode;
static PrintScaleAdv defaultScaleAdv = PrintScaleAdv::Shrink;
static bool hasDefaults = false;
Printer* printer{nullptr};
if (!hasDefaults) {
hasDefaults = true;
if (str::EqI(gGlobalPrefs->printerDefaults.printScale, "fit")) {
defaultScaleAdv = PrintScaleAdv::Fit;
} else if (str::EqI(gGlobalPrefs->printerDefaults.printScale, "none")) {
defaultScaleAdv = PrintScaleAdv::None;
}
}
bool printSelection = false;
Vec<PRINTPAGERANGE> ranges;
Vec<SelectionOnPage>* sel;
if (!HasPermission(Perm::PrinterAccess)) {
return;
}
if (!win->IsDocLoaded()) {
return;
}
if (win->AsChm()) {
// the Print dialog allows access to the file system, so fall back
// to printing the entire document without dialog if that isn't desired
bool showUI = HasPermission(Perm::DiskAccess);
win->AsChm()->PrintCurrentPage(showUI);
return;
}
CrashIf(!win->AsFixed());
if (!win->AsFixed()) {
return;
}
DisplayModel* dm = win->AsFixed();
int rotation = dm->GetRotation();
auto engine = dm->GetEngine();
#ifndef DISABLE_DOCUMENT_RESTRICTIONS
if (!dm->GetEngine()->AllowsPrinting()) {
return;
}
#endif
if (win->printThread) {
uint type = MB_ICONEXCLAMATION | MB_YESNO | MbRtlReadingMaybe();
const WCHAR* title = _TR("Printing in progress.");
const WCHAR* msg = _TR("Printing is still in progress. Abort and start over?");
int res = MessageBox(win->hwndFrame, msg, title, type);
if (res == IDNO) {
return;
}
}
AbortPrinting(win);
// the Print dialog allows access to the file system, so fall back
// to printing the entire document without dialog if that isn't desired
if (!HasPermission(Perm::DiskAccess)) {
PrintFile(dm->GetEngine());
return;
}
PRINTDLGEXW pdex{};
pdex.lStructSize = sizeof(PRINTDLGEXW);
pdex.hwndOwner = win->hwndFrame;
pdex.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_COLLATE;
if (!win->currentTab->selectionOnPage) {
pdex.Flags |= PD_NOSELECTION;
}
pdex.nCopies = 1;
/* by default print all pages */
pdex.nPageRanges = 1;
pdex.nMaxPageRanges = MAXPAGERANGES;
PRINTPAGERANGE* ppr = AllocArray<PRINTPAGERANGE>(MAXPAGERANGES);
pdex.lpPageRanges = ppr;
ppr->nFromPage = 1;
ppr->nToPage = dm->PageCount();
pdex.nMinPage = 1;
pdex.nMaxPage = dm->PageCount();
pdex.nStartPage = START_PAGE_GENERAL;
Print_Advanced_Data advanced(PrintRangeAdv::All, defaultScaleAdv);
ScopedMem<DLGTEMPLATE> dlgTemplate; // needed for RTL languages
HPROPSHEETPAGE hPsp = CreatePrintAdvancedPropSheet(&advanced, dlgTemplate);
pdex.lphPropertyPages = &hPsp;
pdex.nPropertyPages = 1;
bool failedEngineClone{false};
PrintData* pd{nullptr};
DEVMODE* devMode{nullptr};
// restore remembered settings
if (defaultDevMode) {
DEVMODE* p = defaultDevMode.Get();
pdex.hDevMode = GlobalMemDup(p, p->dmSize + p->dmDriverExtra);
}
if (PrintDlgEx(&pdex) != S_OK) {
if (CommDlgExtendedError() != 0) {
/* if PrintDlg was cancelled then
CommDlgExtendedError is zero, otherwise it returns the
error code, which we could look at here if we wanted.
for now just warn the user that printing has stopped
becasue of an error */
MessageBoxWarning(win->hwndFrame, _TR("Couldn't initialize printer"), _TR("Printing problem."));
}
goto Exit;
}
if (!pdex.hDevNames) {
MessageBoxWarning(win->hwndFrame, _TR("Couldn't get printer name"), _TR("Printing problem."));
goto Exit;
}
{
DEVNAMES* devNames = (DEVNAMES*)GlobalLock(pdex.hDevNames);
if (devNames) {
// printerInfo.pDriverName = (LPWSTR)devNames + devNames->wDriverOffset;
WCHAR* printerName = (WCHAR*)devNames + devNames->wDeviceOffset;
printer = NewPrinter(printerName);
// printerInfo.pPortName = (LPWSTR)devNames + devNames->wOutputOffset;
GlobalUnlock(pdex.hDevNames);
}
}
if (!printer) {
MessageBoxWarning(win->hwndFrame, _TR("Couldn't initialize printer"), _TR("Printing problem."));
goto Exit;
}
devMode = (DEVMODEW*)GlobalLock(pdex.hDevMode);
if (pdex.dwResultAction == PD_RESULT_PRINT || pdex.dwResultAction == PD_RESULT_APPLY) {
// remember settings for this process
if (devMode) {
defaultDevMode.Set((DEVMODEW*)memdup(devMode, devMode->dmSize + devMode->dmDriverExtra));
}
defaultScaleAdv = advanced.scale;
}
if (devMode) {
auto dmCopy = (DEVMODEW*)memdup(devMode, devMode->dmSize + devMode->dmDriverExtra);
printer->SetDevMode(dmCopy);
GlobalUnlock(pdex.hDevMode);
}
if (pdex.dwResultAction != PD_RESULT_PRINT) {
goto Exit;
}
if (pdex.Flags & PD_CURRENTPAGE) {
PRINTPAGERANGE pr = {(DWORD)dm->CurrentPageNo(), (DWORD)dm->CurrentPageNo()};
ranges.Append(pr);
} else if (win->currentTab->selectionOnPage && (pdex.Flags & PD_SELECTION)) {
printSelection = true;
} else if (!(pdex.Flags & PD_PAGENUMS)) {
PRINTPAGERANGE pr = {1, (DWORD)dm->PageCount()};
ranges.Append(pr);
} else {
CrashIf(pdex.nPageRanges <= 0);
for (DWORD i = 0; i < pdex.nPageRanges; i++) {
ranges.Append(pdex.lpPageRanges[i]);
}
}
sel = printSelection ? win->currentTab->selectionOnPage : nullptr;
pd = new PrintData(engine, printer, ranges, advanced, rotation, sel);
// if a file is missing and the engine can't thus be cloned,
// we print using the original engine on the main thread
// so that the document can't be closed and the original engine
// unexpectedly deleted
// TODO: instead prevent closing the document so that printing
// can still happen on a separate thread and be interruptible
failedEngineClone = dm->GetEngine() && !pd->engine;
if (failedEngineClone) {
pd->engine = dm->GetEngine();
}
if (!waitForCompletion && !failedEngineClone) {
PrintToDeviceOnThread(win, pd);
} else {
PrintToDevice(*pd);
if (failedEngineClone) {
pd->engine = nullptr;
}
delete pd;
}
Exit:
free(ppr);
GlobalFree(pdex.hDevNames);
GlobalFree(pdex.hDevMode);
}
static short GetPaperSize(EngineBase* engine) {
RectF mediabox = engine->PageMediabox(1);
SizeF size = engine->Transform(mediabox, 1, 1.0f / engine->GetFileDPI(), 0).Size();
switch (GetPaperFormat(size)) {
case PaperFormat::A2:
return DMPAPER_A2;
case PaperFormat::A3:
return DMPAPER_A3;
case PaperFormat::A4:
return DMPAPER_A4;
case PaperFormat::A5:
return DMPAPER_A5;
case PaperFormat::A6:
return DMPAPER_A6;
case PaperFormat::Letter:
return DMPAPER_LETTER;
case PaperFormat::Legal:
return DMPAPER_LEGAL;
case PaperFormat::Tabloid:
return DMPAPER_TABLOID;
case PaperFormat::Statement:
return DMPAPER_STATEMENT;
default:
return 0;
}
}
#if 0
static short GetPaperByName(const WCHAR* papername) {
if (str::EqI(papername, L"letter")) {
return DMPAPER_LETTER;
}
if (str::EqI(papername, L"legal")) {
return DMPAPER_LEGAL;
}
if (str::EqI(papername, L"tabloid")) {
return DMPAPER_TABLOID;
}
if (str::EqI(papername, L"statement")) {
return DMPAPER_STATEMENT;
}
if (str::EqI(papername, L"A2")) {
return DMPAPER_A2;
}
if (str::EqI(papername, L"A3")) {
return DMPAPER_A3;
}
if (str::EqI(papername, L"A4")) {
return DMPAPER_A4;
}
if (str::EqI(papername, L"A5")) {
return DMPAPER_A5;
}
if (str::EqI(papername, L"A6")) {
return DMPAPER_A6;
}
return 0;
}
#endif
// wantedName can be a paper name, like "A6" or number for DMPAPER_* contstants like DMPAPER_LETTER
static short GetPaperByName(Printer* printer, const WCHAR* wantedName) {
auto devMode = printer->devMode;
CrashIf(!(devMode->dmFields & DM_PAPERSIZE));
if (!(devMode->dmFields & DM_PAPERSIZE)) {
return devMode->dmPaperSize;
}
int n = printer->nPaperSizes;
for (int i = 0; i < n; i++) {
auto paperName = printer->paperNames[i];
if (str::EqIS(wantedName, paperName)) {
return printer->papers[i];
}
}
// alternatively allow indicating the paper directly by number
DWORD paperId{0};
if (str::Parse(wantedName, L"%u%$", &paperId)) {
return (short)paperId;
}
return devMode->dmPaperSize;
}
static short GetPaperKind(const WCHAR* kindName) {
DWORD kind;
if (str::Parse(kindName, L"%u%$", &kind)) {
return (short)kind;
}
return DMPAPER_USER;
}
static short GetPaperSourceByName(Printer* printer, const WCHAR* binName) {
auto devMode = printer->devMode;
CrashIf(!(devMode->dmFields & DM_DEFAULTSOURCE));
if (!(devMode->dmFields & DM_DEFAULTSOURCE)) {
return devMode->dmDefaultSource;
}
int n = printer->nBins;
if (n == 0) {
return devMode->dmDefaultSource;
}
for (int i = 0; i < n; i++) {
WCHAR* currName = printer->binNames[i];
if (str::EqIS(currName, binName)) {
return printer->bins[i];
}
}
DWORD count{0};
// alternatively allow indicating the paper bin directly by number
if (str::Parse(binName, L"%u%$", &count)) {
return (short)count;
}
return devMode->dmDefaultSource;
}
static void ApplyPrintSettings(Printer* printer, const WCHAR* settings, int pageCount, Vec<PRINTPAGERANGE>& ranges,
Print_Advanced_Data& advanced) {
auto devMode = printer->devMode;
auto printerName = printer->name;
WStrVec rangeList;
if (settings) {
rangeList.Split(settings, L",", true);
}
for (size_t i = 0; i < rangeList.size(); i++) {
int val;
PRINTPAGERANGE pr{};
WCHAR* s = rangeList.at(i);
if (str::Parse(s, L"%d-%d%$", &pr.nFromPage, &pr.nToPage)) {
pr.nFromPage = limitValue(pr.nFromPage, (DWORD)1, (DWORD)pageCount);
pr.nToPage = limitValue(pr.nToPage, (DWORD)1, (DWORD)pageCount);
ranges.Append(pr);
} else if (str::Parse(s, L"%d%$", &pr.nFromPage)) {
pr.nFromPage = pr.nToPage = limitValue(pr.nFromPage, (DWORD)1, (DWORD)pageCount);