28package com.jogamp.opengl.test.junit.jogl.tile;
30import java.awt.Container;
31import java.awt.Graphics;
32import java.awt.Graphics2D;
33import java.awt.Insets;
34import java.awt.RenderingHints;
35import java.awt.image.BufferedImage;
36import java.awt.print.PageFormat;
37import java.awt.print.Paper;
38import java.awt.print.Printable;
39import java.awt.print.PrinterException;
40import java.awt.print.PrinterJob;
42import java.io.IOException;
44import javax.imageio.ImageIO;
46import com.jogamp.common.util.awt.AWTEDTExecutor;
47import com.jogamp.nativewindow.awt.DirectDataBufferInt;
48import com.jogamp.opengl.util.TileRenderer;
80 public int print(
final Graphics g,
final PageFormat pf,
final int page)
throws PrinterException {
87 final Paper paper = pf.getPaper();
88 final double paperWWidthInch = paper.getWidth() / 72.0;
89 final double paperWHeightInch = paper.getHeight() / 72.0;
90 final double paperIWidthInch = paper.getImageableWidth() / 72.0;
91 final double paperIHeightInch = paper.getImageableHeight() / 72.0;
92 final double paperWWidthMM = paperWWidthInch *
MM_PER_INCH;
93 final double paperWHeightMM = paperWHeightInch *
MM_PER_INCH;
94 final double paperIWidthMM = paperIWidthInch *
MM_PER_INCH;
95 final double paperIHeightMM = paperIHeightInch *
MM_PER_INCH;
97 final double pfWWidthInch = pf.getWidth() / 72.0;
98 final double pfWHeightInch = pf.getHeight() / 72.0;
99 final double pfIWidthInch = pf.getImageableWidth() / 72.0;
100 final double pfIHeightInch = pf.getImageableHeight() / 72.0;
101 final double pfWWidthMM = pfWWidthInch *
MM_PER_INCH;
102 final double pfWHeightMM = pfWHeightInch *
MM_PER_INCH;
103 final double pfIWidthMM = pfIWidthInch *
MM_PER_INCH;
104 final double pfIHeightMM = pfIHeightInch *
MM_PER_INCH;
106 System.err.println(
"PF: Paper whole size "+
107 Math.round(paperWWidthMM)+
" x "+Math.round(paperWHeightMM)+
" mm, "+
108 Math.round(paperWWidthInch)+
" x "+Math.round(paperWHeightInch)+
" inch");
110 System.err.println(
"PF: Paper image size "+paper.getImageableX()+
" / "+paper.getImageableY()+
" "+
111 Math.round(paperIWidthMM)+
" x "+Math.round(paperIHeightMM)+
" mm, "+
112 Math.round(paperIWidthInch)+
" x "+Math.round(paperIHeightInch)+
" inch, "+
113 Math.round(paper.getImageableWidth())+
"x"+Math.round(paper.getImageableHeight())+
" 72dpi dots");
115 System.err.println(
"PF: Page whole size "+
116 Math.round(pfWWidthMM)+
" x "+Math.round(pfWHeightMM)+
" mm, "+
117 Math.round(pfWWidthInch)+
" x "+Math.round(pfWHeightInch)+
" inch");
119 System.err.println(
"PF: Page image size "+pf.getImageableX()+
" / "+pf.getImageableY()+
" "+
120 Math.round(pfIWidthMM)+
" x "+Math.round(pfIHeightMM)+
" mm, "+
121 Math.round(pfIWidthInch)+
" x "+Math.round(pfIHeightInch)+
" inch, "+
122 Math.round(pf.getImageableWidth())+
"x"+Math.round(pf.getImageableHeight())+
" 72dpi dots");
124 System.err.println(
"PF: Page orientation "+pf.getOrientation());
130 final Insets frameInsets =
cont.getInsets();
131 final int frameWidth =
cont.getWidth();
132 final int frameHeight=
cont.getHeight();
133 final double scaleGraphics =
dpi / 72.0;
134 final int frameSWidth = (int) ( frameWidth * scaleGraphics );
135 final int frameSHeight = (int) ( frameHeight * scaleGraphics );
136 final double scaleComp72;
138 final double sx = pf.getImageableWidth() / frameSWidth;
139 final double sy = pf.getImageableHeight() / frameSHeight;
140 scaleComp72 = Math.min(sx, sy);
143 System.err.println(
"PRINT.offscrn thread "+Thread.currentThread().getName());
144 System.err.println(
"PRINT.offscrn DPI: scaleGraphics "+scaleGraphics+
", scaleComp72 "+scaleComp72);
145 System.err.println(
"PRINT.offscrn DPI: frame: border "+frameInsets+
", size "+frameWidth+
"x"+frameHeight+
146 " -> scaled "+frameSWidth+
"x" + frameSHeight);
150 System.err.println(
"PRINT.offscrn image "+image);
151 final Graphics2D g2d = (Graphics2D) image.getGraphics();
152 g2d.setClip(0, 0, frameSWidth, frameSHeight);
153 g2d.scale(scaleGraphics, scaleGraphics);
155 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
156 AWTEDTExecutor.singleton.invoke(
true,
new Runnable() {
165 ImageIO.write(image,
"png", fout);
166 }
catch (
final IOException e) {
171 final Graphics2D g2d = (Graphics2D)g;
172 g2d.translate(pf.getImageableX(), pf.getImageableY());
173 g2d.scale(scaleComp72, scaleComp72);
174 g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(),
null);
DataBuffer specialization using NIO direct buffer of type DataBuffer#TYPE_INT as storage.
static BufferedImageInt createBufferedImage(final int width, final int height, final int imageType, Point location, final Hashtable<?,?> properties)
Creates a BufferedImageInt using a direct color model in sRGB color space.
Printable implementation using NIO DirectDataBufferInt BufferedImage for offscreen rendered printing.
OffscreenPrintable(final PrinterJob job, final Container printContainer, final int printDPI, final int numSamples, final int tileWidth, final int tileHeight, final int imageType, final String pngFilename)
int print(final Graphics g, final PageFormat pf, final int page)
Base Printable implementation class.
final RecursiveLock lockPrinting
static final double MM_PER_INCH