use of javax.print.attribute.standard.MediaPrintableArea in project jdk8u_jdk by JetBrains.
the class RasterPrinterJob method attributeToPageFormat.
private PageFormat attributeToPageFormat(PrintService service, PrintRequestAttributeSet attSet) {
PageFormat page = defaultPage();
if (service == null) {
return page;
}
OrientationRequested orient = (OrientationRequested) attSet.get(OrientationRequested.class);
if (orient == null) {
orient = (OrientationRequested) service.getDefaultAttributeValue(OrientationRequested.class);
}
if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
page.setOrientation(PageFormat.REVERSE_LANDSCAPE);
} else if (orient == OrientationRequested.LANDSCAPE) {
page.setOrientation(PageFormat.LANDSCAPE);
} else {
page.setOrientation(PageFormat.PORTRAIT);
}
Media media = (Media) attSet.get(Media.class);
MediaSize size = getMediaSize(media, service, page);
Paper paper = new Paper();
//units == 1 to avoid FP error
float[] dim = size.getSize(1);
double w = Math.rint((dim[0] * 72.0) / Size2DSyntax.INCH);
double h = Math.rint((dim[1] * 72.0) / Size2DSyntax.INCH);
paper.setSize(w, h);
MediaPrintableArea area = (MediaPrintableArea) attSet.get(MediaPrintableArea.class);
if (area == null) {
area = getDefaultPrintableArea(page, w, h);
}
double ix, iw, iy, ih;
// Should pass in same unit as updatePageAttributes
// to avoid rounding off errors.
ix = Math.rint(area.getX(MediaPrintableArea.INCH) * DPI);
iy = Math.rint(area.getY(MediaPrintableArea.INCH) * DPI);
iw = Math.rint(area.getWidth(MediaPrintableArea.INCH) * DPI);
ih = Math.rint(area.getHeight(MediaPrintableArea.INCH) * DPI);
paper.setImageableArea(ix, iy, iw, ih);
page.setPaper(paper);
return page;
}
use of javax.print.attribute.standard.MediaPrintableArea in project jdk8u_jdk by JetBrains.
the class RasterPrinterJob method updateAttributesWithPageFormat.
protected void updateAttributesWithPageFormat(PrintService service, PageFormat page, PrintRequestAttributeSet pageAttributes) {
if (service == null || page == null || pageAttributes == null) {
return;
}
float x = (float) Math.rint((page.getPaper().getWidth() * Size2DSyntax.INCH) / (72.0)) / (float) Size2DSyntax.INCH;
float y = (float) Math.rint((page.getPaper().getHeight() * Size2DSyntax.INCH) / (72.0)) / (float) Size2DSyntax.INCH;
// We should limit the list where we search the matching
// media, this will prevent mapping to wrong media ex. Ledger
// can be mapped to B. Especially useful when creating
// custom MediaSize.
Media[] mediaList = (Media[]) service.getSupportedAttributeValues(Media.class, null, null);
Media media = null;
try {
media = CustomMediaSizeName.findMedia(mediaList, x, y, Size2DSyntax.INCH);
} catch (IllegalArgumentException iae) {
}
if ((media == null) || !(service.isAttributeValueSupported(media, null, null))) {
media = (Media) service.getDefaultAttributeValue(Media.class);
}
OrientationRequested orient;
switch(page.getOrientation()) {
case PageFormat.LANDSCAPE:
orient = OrientationRequested.LANDSCAPE;
break;
case PageFormat.REVERSE_LANDSCAPE:
orient = OrientationRequested.REVERSE_LANDSCAPE;
break;
default:
orient = OrientationRequested.PORTRAIT;
}
if (media != null) {
pageAttributes.add(media);
}
pageAttributes.add(orient);
float ix = (float) (page.getPaper().getImageableX() / DPI);
float iw = (float) (page.getPaper().getImageableWidth() / DPI);
float iy = (float) (page.getPaper().getImageableY() / DPI);
float ih = (float) (page.getPaper().getImageableHeight() / DPI);
if (ix < 0)
ix = 0f;
if (iy < 0)
iy = 0f;
try {
pageAttributes.add(new MediaPrintableArea(ix, iy, iw, ih, MediaPrintableArea.INCH));
} catch (IllegalArgumentException iae) {
}
}
use of javax.print.attribute.standard.MediaPrintableArea in project jdk8u_jdk by JetBrains.
the class RasterPrinterJob method setAttributes.
/* subclasses may need to pull extra information out of the attribute set
* They can override this method & call super.setAttributes()
*/
protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException {
/* reset all values to defaults */
setCollated(false);
sidesAttr = null;
pageRangesAttr = null;
copiesAttr = 0;
jobNameAttr = null;
userNameAttr = null;
destinationAttr = null;
collateAttReq = false;
PrintService service = getPrintService();
if (attributes == null || service == null) {
return;
}
boolean fidelity = false;
Fidelity attrFidelity = (Fidelity) attributes.get(Fidelity.class);
if (attrFidelity != null && attrFidelity == Fidelity.FIDELITY_TRUE) {
fidelity = true;
}
if (fidelity == true) {
AttributeSet unsupported = service.getUnsupportedAttributes(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
if (unsupported != null) {
throw new PrinterException("Fidelity cannot be satisfied");
}
}
/*
* Since we have verified supported values if fidelity is true,
* we can either ignore unsupported values, or substitute a
* reasonable alternative
*/
SheetCollate collateAttr = (SheetCollate) attributes.get(SheetCollate.class);
if (isSupportedValue(collateAttr, attributes)) {
setCollated(collateAttr == SheetCollate.COLLATED);
}
sidesAttr = (Sides) attributes.get(Sides.class);
if (!isSupportedValue(sidesAttr, attributes)) {
sidesAttr = Sides.ONE_SIDED;
}
pageRangesAttr = (PageRanges) attributes.get(PageRanges.class);
if (!isSupportedValue(pageRangesAttr, attributes)) {
pageRangesAttr = null;
} else {
if ((SunPageSelection) attributes.get(SunPageSelection.class) == SunPageSelection.RANGE) {
// get to, from, min, max page ranges
int[][] range = pageRangesAttr.getMembers();
// setPageRanges uses 0-based indexing so we subtract 1
setPageRange(range[0][0] - 1, range[0][1] - 1);
} else {
setPageRange(-1, -1);
}
}
Copies copies = (Copies) attributes.get(Copies.class);
if (isSupportedValue(copies, attributes) || (!fidelity && copies != null)) {
copiesAttr = copies.getValue();
setCopies(copiesAttr);
} else {
copiesAttr = getCopies();
}
Destination destination = (Destination) attributes.get(Destination.class);
if (isSupportedValue(destination, attributes)) {
try {
// Old code (new File(destination.getURI())).getPath()
// would generate a "URI is not hierarchical" IAE
// for "file:out.prn" so we use getSchemeSpecificPart instead
destinationAttr = "" + new File(destination.getURI().getSchemeSpecificPart());
} catch (Exception e) {
// paranoid exception
Destination defaultDest = (Destination) service.getDefaultAttributeValue(Destination.class);
if (defaultDest != null) {
destinationAttr = "" + new File(defaultDest.getURI().getSchemeSpecificPart());
}
}
}
JobSheets jobSheets = (JobSheets) attributes.get(JobSheets.class);
if (jobSheets != null) {
noJobSheet = jobSheets == JobSheets.NONE;
}
JobName jobName = (JobName) attributes.get(JobName.class);
if (isSupportedValue(jobName, attributes) || (!fidelity && jobName != null)) {
jobNameAttr = jobName.getValue();
setJobName(jobNameAttr);
} else {
jobNameAttr = getJobName();
}
RequestingUserName userName = (RequestingUserName) attributes.get(RequestingUserName.class);
if (isSupportedValue(userName, attributes) || (!fidelity && userName != null)) {
userNameAttr = userName.getValue();
} else {
try {
userNameAttr = getUserName();
} catch (SecurityException e) {
userNameAttr = "";
}
}
/* OpenBook is used internally only when app uses Printable.
* This is the case when we use the values from the attribute set.
*/
Media media = (Media) attributes.get(Media.class);
OrientationRequested orientReq = (OrientationRequested) attributes.get(OrientationRequested.class);
MediaPrintableArea mpa = (MediaPrintableArea) attributes.get(MediaPrintableArea.class);
if ((orientReq != null || media != null || mpa != null) && getPageable() instanceof OpenBook) {
/* We could almost(!) use PrinterJob.getPageFormat() except
* here we need to start with the PageFormat from the OpenBook :
*/
Pageable pageable = getPageable();
Printable printable = pageable.getPrintable(0);
PageFormat pf = (PageFormat) pageable.getPageFormat(0).clone();
Paper paper = pf.getPaper();
/* If there's a media but no media printable area, we can try
* to retrieve the default value for mpa and use that.
*/
if (mpa == null && media != null && service.isAttributeCategorySupported(MediaPrintableArea.class)) {
Object mpaVals = service.getSupportedAttributeValues(MediaPrintableArea.class, null, attributes);
if (mpaVals instanceof MediaPrintableArea[] && ((MediaPrintableArea[]) mpaVals).length > 0) {
mpa = ((MediaPrintableArea[]) mpaVals)[0];
}
}
if (isSupportedValue(orientReq, attributes) || (!fidelity && orientReq != null)) {
int orient;
if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
orient = PageFormat.REVERSE_LANDSCAPE;
} else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
orient = PageFormat.LANDSCAPE;
} else {
orient = PageFormat.PORTRAIT;
}
pf.setOrientation(orient);
}
if (isSupportedValue(media, attributes) || (!fidelity && media != null)) {
if (media instanceof MediaSizeName) {
MediaSizeName msn = (MediaSizeName) media;
MediaSize msz = MediaSize.getMediaSizeForName(msn);
if (msz != null) {
float paperWid = msz.getX(MediaSize.INCH) * 72.0f;
float paperHgt = msz.getY(MediaSize.INCH) * 72.0f;
paper.setSize(paperWid, paperHgt);
if (mpa == null) {
paper.setImageableArea(72.0, 72.0, paperWid - 144.0, paperHgt - 144.0);
}
}
}
}
if (isSupportedValue(mpa, attributes) || (!fidelity && mpa != null)) {
float[] printableArea = mpa.getPrintableArea(MediaPrintableArea.INCH);
for (int i = 0; i < printableArea.length; i++) {
printableArea[i] = printableArea[i] * 72.0f;
}
paper.setImageableArea(printableArea[0], printableArea[1], printableArea[2], printableArea[3]);
}
pf.setPaper(paper);
pf = validatePage(pf);
setPrintable(printable, pf);
} else {
// for AWT where pageable is not an instance of OpenBook,
// we need to save paper info
this.attributes = attributes;
}
}
use of javax.print.attribute.standard.MediaPrintableArea in project jdk8u_jdk by JetBrains.
the class UnixPrintService method getSupportedAttributeValues.
public Object getSupportedAttributeValues(Class<? extends Attribute> category, DocFlavor flavor, AttributeSet attributes) {
if (category == null) {
throw new NullPointerException("null category");
}
if (!Attribute.class.isAssignableFrom(category)) {
throw new IllegalArgumentException(category + " does not implement Attribute");
}
if (flavor != null) {
if (!isDocFlavorSupported(flavor)) {
throw new IllegalArgumentException(flavor + " is an unsupported flavor");
} else if (isAutoSense(flavor)) {
return null;
}
}
if (!isAttributeCategorySupported(category)) {
return null;
}
if (category == Chromaticity.class) {
if (flavor == null || isServiceFormattedFlavor(flavor)) {
Chromaticity[] arr = new Chromaticity[1];
arr[0] = Chromaticity.COLOR;
return (arr);
} else {
return null;
}
} else if (category == Destination.class) {
try {
return new Destination((new File("out.ps")).toURI());
} catch (SecurityException se) {
try {
return new Destination(new URI("file:out.ps"));
} catch (URISyntaxException e) {
return null;
}
}
} else if (category == JobName.class) {
return new JobName("Java Printing", null);
} else if (category == JobSheets.class) {
JobSheets[] arr = new JobSheets[2];
arr[0] = JobSheets.NONE;
arr[1] = JobSheets.STANDARD;
return arr;
} else if (category == RequestingUserName.class) {
String userName = "";
try {
userName = System.getProperty("user.name", "");
} catch (SecurityException se) {
}
return new RequestingUserName(userName, null);
} else if (category == OrientationRequested.class) {
if (flavor == null || isServiceFormattedFlavor(flavor)) {
OrientationRequested[] arr = new OrientationRequested[3];
arr[0] = OrientationRequested.PORTRAIT;
arr[1] = OrientationRequested.LANDSCAPE;
arr[2] = OrientationRequested.REVERSE_LANDSCAPE;
return arr;
} else {
return null;
}
} else if ((category == Copies.class) || (category == CopiesSupported.class)) {
if (flavor == null || !(flavor.equals(DocFlavor.INPUT_STREAM.POSTSCRIPT) || flavor.equals(DocFlavor.URL.POSTSCRIPT) || flavor.equals(DocFlavor.BYTE_ARRAY.POSTSCRIPT))) {
return new CopiesSupported(1, MAXCOPIES);
} else {
return null;
}
} else if (category == Media.class) {
Media[] arr = new Media[mediaSizes.length];
System.arraycopy(mediaSizes, 0, arr, 0, mediaSizes.length);
return arr;
} else if (category == Fidelity.class) {
Fidelity[] arr = new Fidelity[2];
arr[0] = Fidelity.FIDELITY_FALSE;
arr[1] = Fidelity.FIDELITY_TRUE;
return arr;
} else if (category == MediaPrintableArea.class) {
/* The code below implements the behaviour that if no Media or
* MediaSize attribute is specified, return an array of
* MediaPrintableArea, one for each supported Media.
* If a MediaSize is specified, return a MPA consistent for that,
* and if a Media is specified locate its MediaSize and return
* its MPA, and if none is found, return an MPA for the default
* Media for this service.
*/
if (attributes == null) {
return getAllPrintableAreas();
}
MediaSize mediaSize = (MediaSize) attributes.get(MediaSize.class);
Media media = (Media) attributes.get(Media.class);
MediaPrintableArea[] arr = new MediaPrintableArea[1];
if (mediaSize == null) {
if (media instanceof MediaSizeName) {
MediaSizeName msn = (MediaSizeName) media;
mediaSize = MediaSize.getMediaSizeForName(msn);
if (mediaSize == null) {
/* try to get a size from the default media */
media = (Media) getDefaultAttributeValue(Media.class);
if (media instanceof MediaSizeName) {
msn = (MediaSizeName) media;
mediaSize = MediaSize.getMediaSizeForName(msn);
}
if (mediaSize == null) {
/* shouldn't happen, return a default */
arr[0] = new MediaPrintableArea(0.25f, 0.25f, 8f, 10.5f, MediaSize.INCH);
return arr;
}
}
} else {
return getAllPrintableAreas();
}
}
/* If reach here MediaSize is non-null */
assert mediaSize != null;
arr[0] = new MediaPrintableArea(0.25f, 0.25f, mediaSize.getX(MediaSize.INCH) - 0.5f, mediaSize.getY(MediaSize.INCH) - 0.5f, MediaSize.INCH);
return arr;
} else if (category == PageRanges.class) {
if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
PageRanges[] arr = new PageRanges[1];
arr[0] = new PageRanges(1, Integer.MAX_VALUE);
return arr;
} else {
return null;
}
} else if (category == SheetCollate.class) {
if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
SheetCollate[] arr = new SheetCollate[2];
arr[0] = SheetCollate.UNCOLLATED;
arr[1] = SheetCollate.COLLATED;
return arr;
} else {
return null;
}
} else if (category == Sides.class) {
if (flavor == null || flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE) || flavor.equals(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
Sides[] arr = new Sides[3];
arr[0] = Sides.ONE_SIDED;
arr[1] = Sides.TWO_SIDED_LONG_EDGE;
arr[2] = Sides.TWO_SIDED_SHORT_EDGE;
return arr;
} else {
return null;
}
} else {
return null;
}
}
use of javax.print.attribute.standard.MediaPrintableArea in project jdk8u_jdk by JetBrains.
the class PrinterJob method getPageFormat.
/**
* Calculates a <code>PageFormat</code> with values consistent with those
* supported by the current <code>PrintService</code> for this job
* (ie the value returned by <code>getPrintService()</code>) and media,
* printable area and orientation contained in <code>attributes</code>.
* <p>
* Calling this method does not update the job.
* It is useful for clients that have a set of attributes obtained from
* <code>printDialog(PrintRequestAttributeSet attributes)</code>
* and need a PageFormat to print a Pageable object.
* @param attributes a set of printing attributes, for example obtained
* from calling printDialog. If <code>attributes</code> is null a default
* PageFormat is returned.
* @return a <code>PageFormat</code> whose settings conform with
* those of the current service and the specified attributes.
* @since 1.6
*/
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {
PrintService service = getPrintService();
PageFormat pf = defaultPage();
if (service == null || attributes == null) {
return pf;
}
Media media = (Media) attributes.get(Media.class);
MediaPrintableArea mpa = (MediaPrintableArea) attributes.get(MediaPrintableArea.class);
OrientationRequested orientReq = (OrientationRequested) attributes.get(OrientationRequested.class);
if (media == null && mpa == null && orientReq == null) {
return pf;
}
Paper paper = pf.getPaper();
/* If there's a media but no media printable area, we can try
* to retrieve the default value for mpa and use that.
*/
if (mpa == null && media != null && service.isAttributeCategorySupported(MediaPrintableArea.class)) {
Object mpaVals = service.getSupportedAttributeValues(MediaPrintableArea.class, null, attributes);
if (mpaVals instanceof MediaPrintableArea[] && ((MediaPrintableArea[]) mpaVals).length > 0) {
mpa = ((MediaPrintableArea[]) mpaVals)[0];
}
}
if (media != null && service.isAttributeValueSupported(media, null, attributes)) {
if (media instanceof MediaSizeName) {
MediaSizeName msn = (MediaSizeName) media;
MediaSize msz = MediaSize.getMediaSizeForName(msn);
if (msz != null) {
double inch = 72.0;
double paperWid = msz.getX(MediaSize.INCH) * inch;
double paperHgt = msz.getY(MediaSize.INCH) * inch;
paper.setSize(paperWid, paperHgt);
if (mpa == null) {
paper.setImageableArea(inch, inch, paperWid - 2 * inch, paperHgt - 2 * inch);
}
}
}
}
if (mpa != null && service.isAttributeValueSupported(mpa, null, attributes)) {
float[] printableArea = mpa.getPrintableArea(MediaPrintableArea.INCH);
for (int i = 0; i < printableArea.length; i++) {
printableArea[i] = printableArea[i] * 72.0f;
}
paper.setImageableArea(printableArea[0], printableArea[1], printableArea[2], printableArea[3]);
}
if (orientReq != null && service.isAttributeValueSupported(orientReq, null, attributes)) {
int orient;
if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
orient = PageFormat.REVERSE_LANDSCAPE;
} else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
orient = PageFormat.LANDSCAPE;
} else {
orient = PageFormat.PORTRAIT;
}
pf.setOrientation(orient);
}
pf.setPaper(paper);
pf = validatePage(pf);
return pf;
}
Aggregations