This repository has been archived by the owner on Jan 10, 2018. It is now read-only.
/
WPrinterJob.java
2242 lines (1936 loc) · 76.1 KB
/
WPrinterJob.java
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 (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.awt.windows;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.BasicStroke;
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FileDialog;
import java.awt.Dialog;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.print.Pageable;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.awt.print.PrinterException;
import javax.print.PrintService;
import java.io.File;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import sun.print.PeekGraphics;
import sun.print.PeekMetrics;
import java.net.URI;
import java.net.URISyntaxException;
import javax.print.PrintServiceLookup;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.Attribute;
import javax.print.attribute.standard.Sides;
import javax.print.attribute.standard.Chromaticity;
import javax.print.attribute.standard.PrintQuality;
import javax.print.attribute.standard.PrinterResolution;
import javax.print.attribute.standard.SheetCollate;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Destination;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.PageRanges;
import sun.awt.Win32FontManager;
import sun.print.RasterPrinterJob;
import sun.print.SunAlternateMedia;
import sun.print.SunPageSelection;
import sun.print.Win32MediaTray;
import sun.print.Win32PrintService;
import sun.print.Win32PrintServiceLookup;
import sun.print.ServiceDialog;
import sun.print.DialogOwner;
import java.awt.Frame;
import java.io.FilePermission;
import sun.java2d.Disposer;
import sun.java2d.DisposerRecord;
import sun.java2d.DisposerTarget;
/**
* A class which initiates and executes a Win32 printer job.
*
* @author Richard Blanchard
*/
public final class WPrinterJob extends RasterPrinterJob
implements DisposerTarget {
/* Class Constants */
/* Instance Variables */
/**
* These are Windows' ExtCreatePen End Cap Styles
* and must match the values in <WINGDI.h>
*/
protected static final long PS_ENDCAP_ROUND = 0x00000000;
protected static final long PS_ENDCAP_SQUARE = 0x00000100;
protected static final long PS_ENDCAP_FLAT = 0x00000200;
/**
* These are Windows' ExtCreatePen Line Join Styles
* and must match the values in <WINGDI.h>
*/
protected static final long PS_JOIN_ROUND = 0x00000000;
protected static final long PS_JOIN_BEVEL = 0x00001000;
protected static final long PS_JOIN_MITER = 0x00002000;
/**
* This is the Window's Polygon fill rule which
* Selects alternate mode (fills the area between odd-numbered
* and even-numbered polygon sides on each scan line).
* It must match the value in <WINGDI.h> It can be passed
* to setPolyFillMode().
*/
protected static final int POLYFILL_ALTERNATE = 1;
/**
* This is the Window's Polygon fill rule which
* Selects winding mode which fills any region
* with a nonzero winding value). It must match
* the value in <WINGDI.h> It can be passed
* to setPolyFillMode().
*/
protected static final int POLYFILL_WINDING = 2;
/**
* The maximum value for a Window's color component
* as passed to selectSolidBrush.
*/
private static final int MAX_WCOLOR = 255;
/**
* Flags for setting values from devmode in native code.
* Values must match those defined in awt_PrintControl.cpp
*/
private static final int SET_DUP_VERTICAL = 0x00000010;
private static final int SET_DUP_HORIZONTAL = 0x00000020;
private static final int SET_RES_HIGH = 0x00000040;
private static final int SET_RES_LOW = 0x00000080;
private static final int SET_COLOR = 0x00000200;
private static final int SET_ORIENTATION = 0x00004000;
private static final int SET_COLLATED = 0x00008000;
/**
* Values must match those defined in wingdi.h & commdlg.h
*/
private static final int PD_COLLATE = 0x00000010;
private static final int PD_PRINTTOFILE = 0x00000020;
private static final int DM_ORIENTATION = 0x00000001;
private static final int DM_PAPERSIZE = 0x00000002;
private static final int DM_COPIES = 0x00000100;
private static final int DM_DEFAULTSOURCE = 0x00000200;
private static final int DM_PRINTQUALITY = 0x00000400;
private static final int DM_COLOR = 0x00000800;
private static final int DM_DUPLEX = 0x00001000;
private static final int DM_YRESOLUTION = 0x00002000;
private static final int DM_COLLATE = 0x00008000;
private static final short DMCOLLATE_FALSE = 0;
private static final short DMCOLLATE_TRUE = 1;
private static final short DMORIENT_PORTRAIT = 1;
private static final short DMORIENT_LANDSCAPE = 2;
private static final short DMCOLOR_MONOCHROME = 1;
private static final short DMCOLOR_COLOR = 2;
private static final short DMRES_DRAFT = -1;
private static final short DMRES_LOW = -2;
private static final short DMRES_MEDIUM = -3;
private static final short DMRES_HIGH = -4;
private static final short DMDUP_SIMPLEX = 1;
private static final short DMDUP_VERTICAL = 2;
private static final short DMDUP_HORIZONTAL = 3;
/**
* Pageable MAX pages
*/
private static final int MAX_UNKNOWN_PAGES = 9999;
/* Collation and copy flags.
* The Windows PRINTDLG struct has a nCopies field which on return
* indicates how many copies of a print job an application must render.
* There is also a PD_COLLATE member of the flags field which if
* set on return indicates the application generated copies should be
* collated.
* Windows printer drivers typically - but not always - support
* generating multiple copies themselves, but uncollated is more
* universal than collated copies.
* When they do, they read the initial values from the PRINTDLG structure
* and set them into the driver's DEVMODE structure and intialise
* the printer DC based on that, so that when printed those settings
* will be used.
* For drivers supporting both these capabilities via DEVMODE, then on
* return from the Print Dialog, nCopies is set to 1 and the PD_COLLATE is
* cleared, so that the application will only render 1 copy and the
* driver takes care of the rest.
*
* Applications which want to know what's going on have to be DEVMODE
* savvy and peek at that.
* DM_COPIES flag indicates support for multiple driver copies
* and dmCopies is the number of copies the driver will print
* DM_COLLATE flag indicates support for collated driver copies and
* dmCollate == DMCOLLATE_TRUE indicates the option is in effect.
*
* Multiple copies from Java applications:
* We provide API to get & set the number of copies as well as allowing the
* user to choose it, so we need to be savvy about DEVMODE, so that
* we can accurately report back the number of copies selected by
* the user, as well as make use of the driver to render multiple copies.
*
* Collation and Java applications:
* We presently provide no API for specifying collation, but its
* present on the Windows Print Dialog, and when a user checks it
* they expect it to be obeyed.
* The best thing to do is to detect exactly the cases where the
* driver doesn't support this and render multiple copies ourselves.
* To support all this we need several flags which signal the
* printer's capabilities and the user's requests.
* Its questionable if we (yet) need to make a distinction between
* the user requesting collation and the driver supporting it.
* Since for now we only need to know whether we need to render the
* copies. However it allows the logic to be clearer.
* These fields are changed by native code which detects the driver's
* capabilities and the user's choices.
*/
//initialize to false because the Flags that we initialized in PRINTDLG
// tells GDI that we can handle our own collation and multiple copies
private boolean driverDoesMultipleCopies = false;
private boolean driverDoesCollation = false;
private boolean userRequestedCollation = false;
private boolean noDefaultPrinter = false;
/* The HandleRecord holds the native resources that need to be freed
* when this WPrinterJob is GC'd.
*/
static class HandleRecord implements DisposerRecord {
/**
* The Windows device context we will print into.
* This variable is set after the Print dialog
* is okayed by the user. If the user cancels
* the print dialog, then this variable is 0.
* Much of the configuration information for a printer is
* obtained through printer device specific handles.
* We need to associate these with, and free with, the mPrintDC.
*/
private long mPrintDC;
private long mPrintHDevMode;
private long mPrintHDevNames;
@Override
public void dispose() {
WPrinterJob.deleteDC(mPrintDC, mPrintHDevMode, mPrintHDevNames);
}
}
private HandleRecord handleRecord = new HandleRecord();
private int mPrintPaperSize;
/* These fields are directly set in upcalls from the values
* obtained from calling DeviceCapabilities()
*/
private int mPrintXRes; // pixels per inch in x direction
private int mPrintYRes; // pixels per inch in y direction
private int mPrintPhysX; // x offset in pixels of printable area
private int mPrintPhysY; // y offset in pixels of printable area
private int mPrintWidth; // width in pixels of printable area
private int mPrintHeight; // height in pixels of printable area
private int mPageWidth; // width in pixels of entire page
private int mPageHeight; // height in pixels of entire page
/* The values of the following variables are pulled directly
* into native code (even bypassing getter methods) when starting a doc.
* So these need to be synced up from any resulting native changes
* by a user dialog.
* But the native changes call up to into the attributeset, and that
* should be sufficient, since before heading down to native either
* to (re-)display a dialog, or to print the doc, these are all
* re-populated from the AttributeSet,
* Nonetheless having them in sync with the attributeset and native
* state is probably safer.
* Also whereas the startDoc native code pulls the variables directly,
* the dialog code does use getter to pull some of these values.
* That's an inconsistency we should fix if it causes problems.
*/
private int mAttSides;
private int mAttChromaticity;
private int mAttXRes;
private int mAttYRes;
private int mAttQuality;
private int mAttCollate;
private int mAttCopies;
private int mAttMediaSizeName;
private int mAttMediaTray;
private String mDestination = null;
/**
* The last color set into the print device context or
* <code>null</code> if no color has been set.
*/
private Color mLastColor;
/**
* The last text color set into the print device context or
* <code>null</code> if no color has been set.
*/
private Color mLastTextColor;
/**
* Define the most recent java font set as a GDI font in the printer
* device context. mLastFontFamily will be NULL if no
* GDI font has been set.
*/
private String mLastFontFamily;
private float mLastFontSize;
private int mLastFontStyle;
private int mLastRotation;
private float mLastAwScale;
// for AwtPrintControl::InitPrintDialog
private PrinterJob pjob;
private java.awt.peer.ComponentPeer dialogOwnerPeer = null;
/* Static Initializations */
static {
// AWT has to be initialized for the native code to function correctly.
Toolkit.getDefaultToolkit();
initIDs();
Win32FontManager.registerJREFontsForPrinting();
}
/* Constructors */
public WPrinterJob()
{
Disposer.addRecord(disposerReferent,
handleRecord = new HandleRecord());
initAttributeMembers();
}
/* Implement DisposerTarget. Weak references to an Object can delay
* its storage reclaimation marginally.
* It won't make the native resources be release any more quickly, but
* by pointing the reference held by Disposer at an object which becomes
* no longer strongly reachable when this WPrinterJob is no longer
* strongly reachable, we allow the WPrinterJob to be freed more promptly
* than if it were the referenced object.
*/
private Object disposerReferent = new Object();
@Override
public Object getDisposerReferent() {
return disposerReferent;
}
/* Instance Methods */
/**
* Display a dialog to the user allowing the modification of a
* PageFormat instance.
* The <code>page</code> argument is used to initialize controls
* in the page setup dialog.
* If the user cancels the dialog, then the method returns the
* original <code>page</code> object unmodified.
* If the user okays the dialog then the method returns a new
* PageFormat object with the indicated changes.
* In either case the original <code>page</code> object will
* not be modified.
* @param page the default PageFormat presented to the user
* for modification
* @return the original <code>page</code> object if the dialog
* is cancelled, or a new PageFormat object containing
* the format indicated by the user if the dialog is
* acknowledged
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
* @since JDK1.2
*/
@Override
public PageFormat pageDialog(PageFormat page) throws HeadlessException {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
if (!(getPrintService() instanceof Win32PrintService)) {
return super.pageDialog(page);
}
PageFormat pageClone = (PageFormat) page.clone();
boolean result = false;
/*
* Fix for 4507585: show the native modal dialog the same way printDialog() does so
* that it won't block event dispatching when called on EventDispatchThread.
*/
WPageDialog dialog = new WPageDialog((Frame)null, this,
pageClone, null);
dialog.setRetVal(false);
dialog.setVisible(true);
result = dialog.getRetVal();
dialog.dispose();
// myService => current PrintService
if (result && (myService != null)) {
// It's possible that current printer is changed through
// the "Printer..." button so we query again from native.
String printerName = getNativePrintService();
if (!myService.getName().equals(printerName)) {
// native printer is different !
// we update the current PrintService
try {
setPrintService(Win32PrintServiceLookup.
getWin32PrintLUS().
getPrintServiceByName(printerName));
} catch (PrinterException e) {
}
}
// Update attributes, this will preserve the page settings.
// - same code as in RasterPrinterJob.java
updatePageAttributes(myService, pageClone);
return pageClone;
} else {
return page;
}
}
private boolean displayNativeDialog() {
// "attributes" is required for getting the updated attributes
if (attributes == null) {
return false;
}
DialogOwner dlgOwner = (DialogOwner)attributes.get(DialogOwner.class);
Frame ownerFrame = (dlgOwner != null) ? dlgOwner.getOwner() : null;
WPrintDialog dialog = new WPrintDialog(ownerFrame, this);
dialog.setRetVal(false);
dialog.setVisible(true);
boolean prv = dialog.getRetVal();
dialog.dispose();
Destination dest =
(Destination)attributes.get(Destination.class);
if ((dest == null) || !prv){
return prv;
} else {
String title = null;
String strBundle = "sun.print.resources.serviceui";
ResourceBundle rb = ResourceBundle.getBundle(strBundle);
try {
title = rb.getString("dialog.printtofile");
} catch (MissingResourceException e) {
}
FileDialog fileDialog = new FileDialog(ownerFrame, title,
FileDialog.SAVE);
URI destURI = dest.getURI();
// Old code destURI.getPath() would return null for "file:out.prn"
// so we use getSchemeSpecificPart instead.
String pathName = (destURI != null) ?
destURI.getSchemeSpecificPart() : null;
if (pathName != null) {
File file = new File(pathName);
fileDialog.setFile(file.getName());
File parent = file.getParentFile();
if (parent != null) {
fileDialog.setDirectory(parent.getPath());
}
} else {
fileDialog.setFile("out.prn");
}
fileDialog.setVisible(true);
String fileName = fileDialog.getFile();
if (fileName == null) {
fileDialog.dispose();
return false;
}
String fullName = fileDialog.getDirectory() + fileName;
File f = new File(fullName);
File pFile = f.getParentFile();
while ((f.exists() &&
(!f.isFile() || !f.canWrite())) ||
((pFile != null) &&
(!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
(new PrintToFileErrorDialog(ownerFrame,
ServiceDialog.getMsg("dialog.owtitle"),
ServiceDialog.getMsg("dialog.writeerror")+" "+fullName,
ServiceDialog.getMsg("button.ok"))).setVisible(true);
fileDialog.setVisible(true);
fileName = fileDialog.getFile();
if (fileName == null) {
fileDialog.dispose();
return false;
}
fullName = fileDialog.getDirectory() + fileName;
f = new File(fullName);
pFile = f.getParentFile();
}
fileDialog.dispose();
attributes.add(new Destination(f.toURI()));
return true;
}
}
/**
* Presents the user a dialog for changing properties of the
* print job interactively.
* @returns false if the user cancels the dialog and
* true otherwise.
* @exception HeadlessException if GraphicsEnvironment.isHeadless()
* returns true.
* @see java.awt.GraphicsEnvironment#isHeadless
*/
@Override
public boolean printDialog() throws HeadlessException {
if (GraphicsEnvironment.isHeadless()) {
throw new HeadlessException();
}
// current request attribute set should be reflected to the print dialog.
// If null, create new instance of HashPrintRequestAttributeSet.
if (attributes == null) {
attributes = new HashPrintRequestAttributeSet();
}
if (!(getPrintService() instanceof Win32PrintService)) {
return super.printDialog(attributes);
}
if (noDefaultPrinter == true) {
return false;
} else {
return displayNativeDialog();
}
}
/**
* Associate this PrinterJob with a new PrintService.
*
* Throws <code>PrinterException</code> if the specified service
* cannot support the <code>Pageable</code> and
* </code>Printable</code> interfaces necessary to support 2D printing.
* @param a print service which supports 2D printing.
*
* @throws PrinterException if the specified service does not support
* 2D printing.
*/
@Override
public void setPrintService(PrintService service)
throws PrinterException {
super.setPrintService(service);
if (!(service instanceof Win32PrintService)) {
return;
}
driverDoesMultipleCopies = false;
driverDoesCollation = false;
setNativePrintServiceIfNeeded(service.getName());
}
/* associates this job with the specified native service */
private native void setNativePrintService(String name)
throws PrinterException;
private String lastNativeService = null;
private void setNativePrintServiceIfNeeded(String name)
throws PrinterException {
if (name != null && !(name.equals(lastNativeService))) {
setNativePrintService(name);
lastNativeService = name;
}
}
@Override
public PrintService getPrintService() {
if (myService == null) {
String printerName = getNativePrintService();
if (printerName != null) {
myService = Win32PrintServiceLookup.getWin32PrintLUS().
getPrintServiceByName(printerName);
// no need to call setNativePrintService as this name is
// currently set in native
if (myService != null) {
return myService;
}
}
myService = PrintServiceLookup.lookupDefaultPrintService();
if (myService instanceof Win32PrintService) {
try {
setNativePrintServiceIfNeeded(myService.getName());
} catch (Exception e) {
myService = null;
}
}
}
return myService;
}
private native String getNativePrintService();
private void initAttributeMembers() {
mAttSides = 0;
mAttChromaticity = 0;
mAttXRes = 0;
mAttYRes = 0;
mAttQuality = 0;
mAttCollate = -1;
mAttCopies = 0;
mAttMediaTray = 0;
mAttMediaSizeName = 0;
mDestination = null;
}
/**
* copy the attributes to the native print job
* Note that this method, and hence the re-initialisation
* of the GDI values is done on each entry to the print dialog since
* an app could redisplay the print dialog for the same job and
* 1) the application may have changed attribute settings
* 2) the application may have changed the printer.
* In the event that the user changes the printer using the
dialog, then it is up to GDI to report back all changed values.
*/
@Override
protected void setAttributes(PrintRequestAttributeSet attributes)
throws PrinterException {
// initialize attribute values
initAttributeMembers();
super.setAttributes(attributes);
mAttCopies = getCopiesInt();
mDestination = destinationAttr;
if (attributes == null) {
return; // now always use attributes, so this shouldn't happen.
}
Attribute[] attrs = attributes.toArray();
for (int i=0; i< attrs.length; i++) {
Attribute attr = attrs[i];
try {
if (attr.getCategory() == Sides.class) {
setSidesAttrib(attr);
}
else if (attr.getCategory() == Chromaticity.class) {
setColorAttrib(attr);
}
else if (attr.getCategory() == PrinterResolution.class) {
setResolutionAttrib(attr);
}
else if (attr.getCategory() == PrintQuality.class) {
setQualityAttrib(attr);
}
else if (attr.getCategory() == SheetCollate.class) {
setCollateAttrib(attr);
} else if (attr.getCategory() == Media.class ||
attr.getCategory() == SunAlternateMedia.class) {
/* SunAlternateMedia is used if its a tray, and
* any Media that is specified is not a tray.
*/
if (attr.getCategory() == SunAlternateMedia.class) {
Media media = (Media)attributes.get(Media.class);
if (media == null ||
!(media instanceof MediaTray)) {
attr = ((SunAlternateMedia)attr).getMedia();
}
}
if (attr instanceof MediaSizeName) {
setWin32MediaAttrib(attr);
}
if (attr instanceof MediaTray) {
setMediaTrayAttrib(attr);
}
}
} catch (ClassCastException e) {
}
}
}
/**
* Alters the orientation and Paper to match defaults obtained
* from a printer.
*/
private native void getDefaultPage(PageFormat page);
/**
* The passed in PageFormat will be copied and altered to describe
* the default page size and orientation of the PrinterJob's
* current printer.
* Note: PageFormat.getPaper() returns a clone and getDefaultPage()
* gets that clone so it won't overwrite the original paper.
*/
@Override
public PageFormat defaultPage(PageFormat page) {
PageFormat newPage = (PageFormat)page.clone();
getDefaultPage(newPage);
return newPage;
}
/**
* validate the paper size against the current printer.
*/
@Override
protected native void validatePaper(Paper origPaper, Paper newPaper );
/**
* Examine the metrics captured by the
* <code>PeekGraphics</code> instance and
* if capable of directly converting this
* print job to the printer's control language
* or the native OS's graphics primitives, then
* return a <code>PathGraphics</code> to perform
* that conversion. If there is not an object
* capable of the conversion then return
* <code>null</code>. Returning <code>null</code>
* causes the print job to be rasterized.
*/
@Override
protected Graphics2D createPathGraphics(PeekGraphics peekGraphics,
PrinterJob printerJob,
Printable painter,
PageFormat pageFormat,
int pageIndex) {
WPathGraphics pathGraphics;
PeekMetrics metrics = peekGraphics.getMetrics();
/* If the application has drawn anything that
* out PathGraphics class can not handle then
* return a null PathGraphics. If the property
* to force the raster pipeline has been set then
* we also want to avoid the path (pdl) pipeline
* and return null.
*/
if (forcePDL == false && (forceRaster == true
|| metrics.hasNonSolidColors()
|| metrics.hasCompositing()
)) {
pathGraphics = null;
} else {
BufferedImage bufferedImage = new BufferedImage(8, 8,
BufferedImage.TYPE_INT_RGB);
Graphics2D bufferedGraphics = bufferedImage.createGraphics();
boolean canRedraw = peekGraphics.getAWTDrawingOnly() == false;
pathGraphics = new WPathGraphics(bufferedGraphics, printerJob,
painter, pageFormat, pageIndex,
canRedraw);
}
return pathGraphics;
}
@Override
protected double getXRes() {
if (mAttXRes != 0) {
return mAttXRes;
} else {
return mPrintXRes;
}
}
@Override
protected double getYRes() {
if (mAttYRes != 0) {
return mAttYRes;
} else {
return mPrintYRes;
}
}
@Override
protected double getPhysicalPrintableX(Paper p) {
return mPrintPhysX;
}
@Override
protected double getPhysicalPrintableY(Paper p) {
return mPrintPhysY;
}
@Override
protected double getPhysicalPrintableWidth(Paper p) {
return mPrintWidth;
}
@Override
protected double getPhysicalPrintableHeight(Paper p) {
return mPrintHeight;
}
@Override
protected double getPhysicalPageWidth(Paper p) {
return mPageWidth;
}
@Override
protected double getPhysicalPageHeight(Paper p) {
return mPageHeight;
}
/**
* We don't (yet) provide API to support collation, and
* when we do the logic here will require adjustment, but
* this method is currently necessary to honour user-originated
* collation requests - which can only originate from the print dialog.
* REMIND: check if this can be deleted already.
*/
@Override
protected boolean isCollated() {
return userRequestedCollation;
}
/**
* Returns how many times the entire book should
* be printed by the PrintJob. If the printer
* itself supports collation then this method
* should return 1 indicating that the entire
* book need only be printed once and the copies
* will be collated and made in the printer.
*/
@Override
protected int getCollatedCopies() {
debug_println("driverDoesMultipleCopies="+driverDoesMultipleCopies
+" driverDoesCollation="+driverDoesCollation);
if (super.isCollated() && !driverDoesCollation) {
// we will do our own collation so we need to
// tell the printer to not collate
mAttCollate = 0;
mAttCopies = 1;
return getCopies();
}
return 1;
}
/**
* Returns how many times each page in the book
* should be consecutively printed by PrinterJob.
* If the underlying Window's driver will
* generate the copies, rather than having RasterPrinterJob
* iterate over the number of copies, this method always returns
* 1.
*/
@Override
protected int getNoncollatedCopies() {
if (driverDoesMultipleCopies || super.isCollated()) {
return 1;
} else {
return getCopies();
}
}
/* These getter/setters are called from native code */
/**
* Return the Window's device context that we are printing
* into.
*/
private long getPrintDC() {
return handleRecord.mPrintDC;
}
private void setPrintDC(long mPrintDC) {
handleRecord.mPrintDC = mPrintDC;
}
private long getDevMode() {
return handleRecord.mPrintHDevMode;
}
private void setDevMode(long mPrintHDevMode) {
handleRecord.mPrintHDevMode = mPrintHDevMode;
}
private long getDevNames() {
return handleRecord.mPrintHDevNames;
}
private void setDevNames(long mPrintHDevNames) {
handleRecord.mPrintHDevNames = mPrintHDevNames;
}
protected void beginPath() {
beginPath(getPrintDC());
}
protected void endPath() {
endPath(getPrintDC());
}
protected void closeFigure() {
closeFigure(getPrintDC());
}
protected void fillPath() {
fillPath(getPrintDC());
}
protected void moveTo(float x, float y) {
moveTo(getPrintDC(), x, y);
}
protected void lineTo(float x, float y) {
lineTo(getPrintDC(), x, y);
}
protected void polyBezierTo(float control1x, float control1y,
float control2x, float control2y,
float endX, float endY) {
polyBezierTo(getPrintDC(), control1x, control1y,
control2x, control2y,
endX, endY);
}
/**
* Set the current polgon fill rule into the printer device context.
* The <code>fillRule</code> should
* be one of the following Windows constants:
* <code>ALTERNATE</code> or <code>WINDING</code>.
*/
protected void setPolyFillMode(int fillRule) {
setPolyFillMode(getPrintDC(), fillRule);
}
/*
* Create a Window's solid brush for the color specified
* by <code>(red, green, blue)</code>. Once the brush
* is created, select it in the current printing device
* context and free the old brush.
*/
protected void selectSolidBrush(Color color) {
/* We only need to select a brush if the color has changed.
*/
if (color.equals(mLastColor) == false) {
mLastColor = color;
float[] rgb = color.getRGBColorComponents(null);
selectSolidBrush(getPrintDC(),
(int) (rgb[0] * MAX_WCOLOR),