use of java.awt.print.PageFormat in project jdk8u_jdk by JetBrains.
the class PSPathGraphics method redrawRegion.
/** Redraw a rectanglular area using a proxy graphics
* To do this we need to know the rectangular area to redraw and
* the transform & clip in effect at the time of the original drawImage
*
*/
public void redrawRegion(Rectangle2D region, double scaleX, double scaleY, Shape savedClip, AffineTransform savedTransform) throws PrinterException {
PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
Printable painter = getPrintable();
PageFormat pageFormat = getPageFormat();
int pageIndex = getPageIndex();
/* Create a buffered image big enough to hold the portion
* of the source image being printed.
*/
BufferedImage deepImage = new BufferedImage((int) region.getWidth(), (int) region.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
/* Get a graphics for the application to render into.
* We initialize the buffer to white in order to
* match the paper and then we shift the BufferedImage
* so that it covers the area on the page where the
* caller's Image will be drawn.
*/
Graphics2D g = deepImage.createGraphics();
ProxyGraphics2D proxy = new ProxyGraphics2D(g, psPrinterJob);
proxy.setColor(Color.white);
proxy.fillRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
proxy.clipRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
proxy.translate(-region.getX(), -region.getY());
/* Calculate the resolution of the source image.
*/
float sourceResX = (float) (psPrinterJob.getXRes() / scaleX);
float sourceResY = (float) (psPrinterJob.getYRes() / scaleY);
/* The application expects to see user space at 72 dpi.
* so change user space from image source resolution to
* 72 dpi.
*/
proxy.scale(sourceResX / DEFAULT_USER_RES, sourceResY / DEFAULT_USER_RES);
proxy.translate(-psPrinterJob.getPhysicalPrintableX(pageFormat.getPaper()) / psPrinterJob.getXRes() * DEFAULT_USER_RES, -psPrinterJob.getPhysicalPrintableY(pageFormat.getPaper()) / psPrinterJob.getYRes() * DEFAULT_USER_RES);
/* NB User space now has to be at 72 dpi for this calc to be correct */
proxy.transform(new AffineTransform(getPageFormat().getMatrix()));
proxy.setPaint(Color.black);
painter.print(proxy, pageFormat, pageIndex);
g.dispose();
/* In PSPrinterJob images are printed in device space
* and therefore we need to set a device space clip.
*/
psPrinterJob.setClip(savedTransform.createTransformedShape(savedClip));
/* Scale the bounding rectangle by the scale transform.
* Because the scaling transform has only x and y
* scaling components it is equivalent to multiply
* the x components of the bounding rectangle by
* the x scaling factor and to multiply the y components
* by the y scaling factor.
*/
Rectangle2D.Float scaledBounds = new Rectangle2D.Float((float) (region.getX() * scaleX), (float) (region.getY() * scaleY), (float) (region.getWidth() * scaleX), (float) (region.getHeight() * scaleY));
/* Pull the raster data from the buffered image
* and pass it along to PS.
*/
ByteComponentRaster tile = (ByteComponentRaster) deepImage.getRaster();
psPrinterJob.drawImageBGR(tile.getDataStorage(), scaledBounds.x, scaledBounds.y, scaledBounds.width, scaledBounds.height, 0f, 0f, deepImage.getWidth(), deepImage.getHeight(), deepImage.getWidth(), deepImage.getHeight());
}
use of java.awt.print.PageFormat in project jdk8u_jdk by JetBrains.
the class PSPrinterJob method startDoc.
/**
* Invoked by the RasterPrinterJob super class
* this method is called to mark the start of a
* document.
*/
protected void startDoc() throws PrinterException {
// A security check has been performed in the
// java.awt.print.printerJob.getPrinterJob method.
// We use an inner class to execute the privilged open operations.
// Note that we only open a file if it has been nominated by
// the end-user in a dialog that we ouselves put up.
OutputStream output;
if (epsPrinter == null) {
if (getPrintService() instanceof PSStreamPrintService) {
StreamPrintService sps = (StreamPrintService) getPrintService();
mDestType = RasterPrinterJob.STREAM;
if (sps.isDisposed()) {
throw new PrinterException("service is disposed");
}
output = sps.getOutputStream();
if (output == null) {
throw new PrinterException("Null output stream");
}
} else {
/* REMIND: This needs to be more maintainable */
mNoJobSheet = super.noJobSheet;
if (super.destinationAttr != null) {
mDestType = RasterPrinterJob.FILE;
mDestination = super.destinationAttr;
}
if (mDestType == RasterPrinterJob.FILE) {
try {
spoolFile = new File(mDestination);
output = new FileOutputStream(spoolFile);
} catch (IOException ex) {
throw new PrinterIOException(ex);
}
} else {
PrinterOpener po = new PrinterOpener();
java.security.AccessController.doPrivileged(po);
if (po.pex != null) {
throw po.pex;
}
output = po.result;
}
}
mPSStream = new PrintStream(new BufferedOutputStream(output));
mPSStream.println(ADOBE_PS_STR);
}
mPSStream.println("%%BeginProlog");
mPSStream.println(READIMAGEPROC);
mPSStream.println("/BD {bind def} bind def");
mPSStream.println("/D {def} BD");
mPSStream.println("/C {curveto} BD");
mPSStream.println("/L {lineto} BD");
mPSStream.println("/M {moveto} BD");
mPSStream.println("/R {grestore} BD");
mPSStream.println("/G {gsave} BD");
mPSStream.println("/N {newpath} BD");
mPSStream.println("/P {closepath} BD");
mPSStream.println("/EC {eoclip} BD");
mPSStream.println("/WC {clip} BD");
mPSStream.println("/EF {eofill} BD");
mPSStream.println("/WF {fill} BD");
mPSStream.println("/SG {setgray} BD");
mPSStream.println("/SC {setrgbcolor} BD");
mPSStream.println("/ISOF {");
mPSStream.println(" dup findfont dup length 1 add dict begin {");
mPSStream.println(" 1 index /FID eq {pop pop} {D} ifelse");
mPSStream.println(" } forall /Encoding ISOLatin1Encoding D");
mPSStream.println(" currentdict end definefont");
mPSStream.println("} BD");
mPSStream.println("/NZ {dup 1 lt {pop 1} if} BD");
/* The following procedure takes args: string, x, y, desiredWidth.
* It calculates using stringwidth the width of the string in the
* current font and subtracts it from the desiredWidth and divides
* this by stringLen-1. This gives us a per-glyph adjustment in
* the spacing needed (either +ve or -ve) to make the string
* print at the desiredWidth. The ashow procedure call takes this
* per-glyph adjustment as an argument. This is necessary for WYSIWYG
*/
mPSStream.println("/" + DrawStringName + " {");
mPSStream.println(" moveto 1 index stringwidth pop NZ sub");
mPSStream.println(" 1 index length 1 sub NZ div 0");
mPSStream.println(" 3 2 roll ashow newpath} BD");
mPSStream.println("/FL [");
if (mFontProps == null) {
mPSStream.println(" /Helvetica ISOF");
mPSStream.println(" /Helvetica-Bold ISOF");
mPSStream.println(" /Helvetica-Oblique ISOF");
mPSStream.println(" /Helvetica-BoldOblique ISOF");
mPSStream.println(" /Times-Roman ISOF");
mPSStream.println(" /Times-Bold ISOF");
mPSStream.println(" /Times-Italic ISOF");
mPSStream.println(" /Times-BoldItalic ISOF");
mPSStream.println(" /Courier ISOF");
mPSStream.println(" /Courier-Bold ISOF");
mPSStream.println(" /Courier-Oblique ISOF");
mPSStream.println(" /Courier-BoldOblique ISOF");
} else {
int cnt = Integer.parseInt(mFontProps.getProperty("font.num", "9"));
for (int i = 0; i < cnt; i++) {
mPSStream.println(" /" + mFontProps.getProperty("font." + String.valueOf(i), "Courier ISOF"));
}
}
mPSStream.println("] D");
mPSStream.println("/" + SetFontName + " {");
mPSStream.println(" FL exch get exch scalefont");
mPSStream.println(" [1 0 0 -1 0 0] makefont setfont} BD");
mPSStream.println("%%EndProlog");
mPSStream.println("%%BeginSetup");
if (epsPrinter == null) {
// Set Page Size using first page's format.
PageFormat pageFormat = getPageable().getPageFormat(0);
double paperHeight = pageFormat.getPaper().getHeight();
double paperWidth = pageFormat.getPaper().getWidth();
/* PostScript printers can always generate uncollated copies.
*/
mPSStream.print("<< /PageSize [" + paperWidth + " " + paperHeight + "]");
final PrintService pservice = getPrintService();
Boolean isPS = (Boolean) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
public Object run() {
try {
Class psClass = Class.forName("sun.print.IPPPrintService");
if (psClass.isInstance(pservice)) {
Method isPSMethod = psClass.getMethod("isPostscript", (Class[]) null);
return (Boolean) isPSMethod.invoke(pservice, (Object[]) null);
}
} catch (Throwable t) {
}
return Boolean.TRUE;
}
});
if (isPS) {
mPSStream.print(" /DeferredMediaSelection true");
}
mPSStream.print(" /ImagingBBox null /ManualFeed false");
mPSStream.print(isCollated() ? " /Collate true" : "");
mPSStream.print(" /NumCopies " + getCopiesInt());
if (sidesAttr != Sides.ONE_SIDED) {
if (sidesAttr == Sides.TWO_SIDED_LONG_EDGE) {
mPSStream.print(" /Duplex true ");
} else if (sidesAttr == Sides.TWO_SIDED_SHORT_EDGE) {
mPSStream.print(" /Duplex true /Tumble true ");
}
}
mPSStream.println(" >> setpagedevice ");
}
mPSStream.println("%%EndSetup");
}
use of java.awt.print.PageFormat in project megameklab by MegaMek.
the class UnitPrintManager method printAllUnits.
public static boolean printAllUnits(Vector<Entity> loadedUnits, boolean singlePrint) {
Book book = new Book();
List<Infantry> infList = new ArrayList<>();
List<BattleArmor> baList = new ArrayList<>();
List<Protomech> protoList = new ArrayList<>();
List<Entity> unprintable = new ArrayList<>();
HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.NA_LETTER);
aset.add(new MediaPrintableArea(0, 0, 8.5f, 11, MediaPrintableArea.INCH));
PrinterJob masterPrintJob = PrinterJob.getPrinterJob();
if (!masterPrintJob.printDialog(aset)) {
return true;
}
PageFormat pageFormat = new PageFormat();
pageFormat = masterPrintJob.getPageFormat(null);
Paper p = pageFormat.getPaper();
p.setImageableArea(0, 0, p.getWidth(), p.getHeight());
pageFormat.setPaper(p);
Tank tank1 = null;
Tank wige1 = null;
Tank dualTurret1 = null;
for (Entity unit : loadedUnits) {
if (unit instanceof Mech) {
UnitUtil.removeOneShotAmmo(unit);
UnitUtil.expandUnitMounts((Mech) unit);
book.append(new PrintMech((Mech) unit, book.getNumberOfPages()), pageFormat);
} else if ((unit instanceof LargeSupportTank) || ((unit instanceof Tank) && (unit.getMovementMode() != EntityMovementMode.VTOL) && ((Tank) unit).isSuperHeavy())) {
book.append(new PrintLargeSupportVehicle((Tank) unit), pageFormat);
} else if (unit instanceof VTOL) {
book.append(new PrintVTOL((VTOL) unit), pageFormat);
} else if (unit.getMovementMode() == EntityMovementMode.WIGE) {
if (singlePrint) {
book.append(new PrintVehicle((Tank) unit, null), pageFormat);
} else if (null != wige1) {
book.append(new PrintVehicle(wige1, (Tank) unit), pageFormat);
wige1 = null;
} else {
wige1 = (Tank) unit;
}
} else if ((unit instanceof Tank) && ((unit.getMovementMode() == EntityMovementMode.NAVAL) || (unit.getMovementMode() == EntityMovementMode.SUBMARINE) || (unit.getMovementMode() == EntityMovementMode.HYDROFOIL))) {
unprintable.add(unit);
// book.append(new PrintNavalVehicle((Tank) unit), pageFormat);
} else if (unit instanceof Tank) {
if (!((Tank) unit).hasNoDualTurret()) {
if (singlePrint) {
book.append(new PrintDualTurretVehicle((Tank) unit, null), pageFormat);
} else if (null != dualTurret1) {
book.append(new PrintDualTurretVehicle(dualTurret1, (Tank) unit), pageFormat);
dualTurret1 = null;
} else {
dualTurret1 = (Tank) unit;
}
} else {
if (singlePrint) {
book.append(new PrintVehicle((Tank) unit, null), pageFormat);
} else if (null != tank1) {
book.append(new PrintVehicle(tank1, (Tank) unit), pageFormat);
tank1 = null;
} else {
tank1 = (Tank) unit;
}
}
} else if (unit.hasETypeFlag(Entity.ETYPE_AERO) && !unit.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) {
if (unit instanceof Dropship) {
if (unit.getMovementMode() == EntityMovementMode.AERODYNE) {
book.append(new PrintAerodyne((Dropship) unit), pageFormat);
} else {
book.append(new PrintSpheroid((Dropship) unit), pageFormat);
}
} else if (unit instanceof FixedWingSupport) {
book.append(new PrintFixedWingSupport((FixedWingSupport) unit), pageFormat);
} else if (unit instanceof ConvFighter) {
book.append(new PrintConventionalFighter((ConvFighter) unit), pageFormat);
} else if (unit instanceof SmallCraft) {
if (unit.getMovementMode() == EntityMovementMode.AERODYNE) {
book.append(new PrintSmallCraftAerodyne((SmallCraft) unit), pageFormat);
} else {
book.append(new PrintSmallCraftSpheroid((SmallCraft) unit), pageFormat);
}
} else {
book.append(new PrintAero((Aero) unit), pageFormat);
}
} else if (unit instanceof BattleArmor) {
baList.add((BattleArmor) unit);
if (singlePrint || baList.size() > 4) {
book.append(new PrintBattleArmor(baList), pageFormat);
baList = new ArrayList<>();
}
} else if (unit instanceof Infantry) {
infList.add((Infantry) unit);
if (singlePrint || infList.size() > 3) {
book.append(new PrintInfantry(infList), pageFormat);
infList = new ArrayList<>();
}
} else if (unit instanceof Protomech) {
protoList.add((Protomech) unit);
if (singlePrint || protoList.size() > 4) {
book.append(new PrintProtomech(protoList), pageFormat);
protoList = new ArrayList<>();
}
} else {
// TODO: show a message dialog that lists the unprintable units
unprintable.add(unit);
}
}
if (unprintable.size() > 0) {
JOptionPane.showMessageDialog(null, "Printing is not currently supported for the following units:\n" + unprintable.stream().map(en -> en.getChassis() + " " + en.getModel()).collect(Collectors.joining("\n")));
}
if (null != wige1) {
book.append(new PrintVehicle(wige1, null), pageFormat);
}
if (null != tank1) {
book.append(new PrintVehicle(tank1, null), pageFormat);
}
if (null != dualTurret1) {
book.append(new PrintDualTurretVehicle(dualTurret1, null), pageFormat);
}
if (baList.size() > 0) {
book.append(new PrintBattleArmor(baList), pageFormat);
}
if (infList.size() > 0) {
book.append(new PrintInfantry(infList), pageFormat);
}
if (protoList.size() > 0) {
book.append(new PrintProtomech(protoList), pageFormat);
}
masterPrintJob.setPageable(book);
if (loadedUnits.size() > 1) {
masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw() + " etc");
} else if (loadedUnits.size() > 0) {
masterPrintJob.setJobName(loadedUnits.get(0).getShortNameRaw());
}
PrintTask task = new PrintTask(masterPrintJob, aset);
task.execute();
return true;
}
use of java.awt.print.PageFormat in project freeplane by freeplane.
the class Preview method resize.
public void resize() {
final PageFormat pageFormat = getPageFormat();
int width = getPageWidth(pageFormat);
int height = getPageHeight(pageFormat);
setPreferredSize(new Dimension(width, height));
previewPageImage = null;
revalidate();
}
use of java.awt.print.PageFormat in project blue by kunstmusik.
the class PrintPreview method createComponents.
protected void createComponents() {
try {
PrinterJob prnJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = prnJob.defaultPage();
if (pageFormat.getHeight() == 0 || pageFormat.getWidth() == 0) {
System.err.println("Unable to determine default page size");
return;
}
m_wPage = (int) (pageFormat.getWidth());
m_hPage = (int) (pageFormat.getHeight());
m_scale = 10;
int w = (m_wPage * m_scale / 100);
int h = (m_hPage * m_scale / 100);
int pageIndex = 0;
while (true) {
BufferedImage img = new BufferedImage(m_wPage, m_hPage, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, m_wPage, m_hPage);
if (m_target.print(g, pageFormat, pageIndex) != Printable.PAGE_EXISTS) {
break;
}
PagePreview pp = new PagePreview(w, h, img);
m_preview.add(pp);
pageIndex++;
}
repaint();
} catch (PrinterException e) {
e.printStackTrace();
}
}
Aggregations