author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8352 | 956b231fed62 |
child 17674 | 46246fbf98d4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8352
diff
changeset
|
2 |
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.awt.windows; |
|
27 |
||
28 |
import java.awt.BasicStroke; |
|
29 |
import java.awt.Color; |
|
30 |
import java.awt.Font; |
|
31 |
import java.awt.Graphics; |
|
32 |
import java.awt.Graphics2D; |
|
33 |
import java.awt.Image; |
|
34 |
import java.awt.Shape; |
|
35 |
import java.awt.Stroke; |
|
36 |
import java.awt.Transparency; |
|
37 |
||
38 |
import java.awt.font.FontRenderContext; |
|
39 |
import java.awt.font.GlyphVector; |
|
40 |
import java.awt.font.TextLayout; |
|
41 |
||
42 |
import java.awt.geom.AffineTransform; |
|
43 |
import java.awt.geom.NoninvertibleTransformException; |
|
44 |
import java.awt.geom.PathIterator; |
|
45 |
import java.awt.geom.Point2D; |
|
46 |
import java.awt.geom.Rectangle2D; |
|
47 |
import java.awt.geom.Line2D; |
|
48 |
||
49 |
import java.awt.image.BufferedImage; |
|
50 |
import java.awt.image.ColorModel; |
|
51 |
import java.awt.image.DataBuffer; |
|
52 |
import java.awt.image.IndexColorModel; |
|
53 |
import java.awt.image.WritableRaster; |
|
8352
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
54 |
import java.awt.image.ComponentSampleModel; |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
55 |
import java.awt.image.MultiPixelPackedSampleModel; |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
56 |
import java.awt.image.SampleModel; |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
57 |
|
2 | 58 |
import sun.awt.image.ByteComponentRaster; |
59 |
import sun.awt.image.BytePackedRaster; |
|
60 |
import java.awt.print.PageFormat; |
|
61 |
import java.awt.print.Printable; |
|
62 |
import java.awt.print.PrinterException; |
|
63 |
import java.awt.print.PrinterJob; |
|
64 |
||
65 |
import java.util.Arrays; |
|
66 |
||
67 |
import sun.font.CharToGlyphMapper; |
|
68 |
import sun.font.CompositeFont; |
|
69 |
import sun.font.Font2D; |
|
3928 | 70 |
import sun.font.FontUtilities; |
2 | 71 |
import sun.font.PhysicalFont; |
72 |
import sun.font.TrueTypeFont; |
|
73 |
||
74 |
import sun.print.PathGraphics; |
|
75 |
import sun.print.ProxyGraphics2D; |
|
76 |
||
77 |
class WPathGraphics extends PathGraphics { |
|
78 |
||
79 |
/** |
|
80 |
* For a drawing application the initial user space |
|
81 |
* resolution is 72dpi. |
|
82 |
*/ |
|
83 |
private static final int DEFAULT_USER_RES = 72; |
|
84 |
||
85 |
private static final float MIN_DEVICE_LINEWIDTH = 1.2f; |
|
86 |
private static final float MAX_THINLINE_INCHES = 0.014f; |
|
87 |
||
88 |
/* Note that preferGDITextLayout implies useGDITextLayout. |
|
89 |
* "prefer" is used to override cases where would otherwise |
|
90 |
* choose not to use it. Note that non-layout factors may |
|
91 |
* still mean that GDI cannot be used. |
|
92 |
*/ |
|
93 |
private static boolean useGDITextLayout = true; |
|
94 |
private static boolean preferGDITextLayout = false; |
|
95 |
static { |
|
96 |
String textLayoutStr = |
|
97 |
(String)java.security.AccessController.doPrivileged( |
|
98 |
new sun.security.action.GetPropertyAction( |
|
99 |
"sun.java2d.print.enableGDITextLayout")); |
|
100 |
||
101 |
if (textLayoutStr != null) { |
|
102 |
useGDITextLayout = Boolean.getBoolean(textLayoutStr); |
|
103 |
if (!useGDITextLayout) { |
|
104 |
if (textLayoutStr.equalsIgnoreCase("prefer")) { |
|
105 |
useGDITextLayout = true; |
|
106 |
preferGDITextLayout = true; |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
WPathGraphics(Graphics2D graphics, PrinterJob printerJob, |
|
113 |
Printable painter, PageFormat pageFormat, int pageIndex, |
|
114 |
boolean canRedraw) { |
|
115 |
super(graphics, printerJob, painter, pageFormat, pageIndex, canRedraw); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Creates a new <code>Graphics</code> object that is |
|
120 |
* a copy of this <code>Graphics</code> object. |
|
121 |
* @return a new graphics context that is a copy of |
|
122 |
* this graphics context. |
|
123 |
* @since JDK1.0 |
|
124 |
*/ |
|
3928 | 125 |
@Override |
2 | 126 |
public Graphics create() { |
127 |
||
128 |
return new WPathGraphics((Graphics2D) getDelegate().create(), |
|
129 |
getPrinterJob(), |
|
130 |
getPrintable(), |
|
131 |
getPageFormat(), |
|
132 |
getPageIndex(), |
|
133 |
canDoRedraws()); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Strokes the outline of a Shape using the settings of the current |
|
138 |
* graphics state. The rendering attributes applied include the |
|
139 |
* clip, transform, paint or color, composite and stroke attributes. |
|
140 |
* @param s The shape to be drawn. |
|
141 |
* @see #setStroke |
|
142 |
* @see #setPaint |
|
143 |
* @see java.awt.Graphics#setColor |
|
144 |
* @see #transform |
|
145 |
* @see #setTransform |
|
146 |
* @see #clip |
|
147 |
* @see #setClip |
|
148 |
* @see #setComposite |
|
149 |
*/ |
|
3928 | 150 |
@Override |
2 | 151 |
public void draw(Shape s) { |
152 |
||
153 |
Stroke stroke = getStroke(); |
|
154 |
||
155 |
/* If the line being drawn is thinner than can be |
|
156 |
* rendered, then change the line width, stroke |
|
157 |
* the shape, and then set the line width back. |
|
158 |
* We can only do this for BasicStroke's. |
|
159 |
*/ |
|
160 |
if (stroke instanceof BasicStroke) { |
|
161 |
BasicStroke lineStroke; |
|
162 |
BasicStroke minLineStroke = null; |
|
163 |
float deviceLineWidth; |
|
164 |
float lineWidth; |
|
165 |
AffineTransform deviceTransform; |
|
166 |
Point2D.Float penSize; |
|
167 |
||
168 |
/* Get the requested line width in user space. |
|
169 |
*/ |
|
170 |
lineStroke = (BasicStroke) stroke; |
|
171 |
lineWidth = lineStroke.getLineWidth(); |
|
172 |
penSize = new Point2D.Float(lineWidth, lineWidth); |
|
173 |
||
174 |
/* Compute the line width in device coordinates. |
|
175 |
* Work on a point in case there is asymetric scaling |
|
176 |
* between user and device space. |
|
177 |
* Take the absolute value in case there is negative |
|
178 |
* scaling in effect. |
|
179 |
*/ |
|
180 |
deviceTransform = getTransform(); |
|
181 |
deviceTransform.deltaTransform(penSize, penSize); |
|
182 |
deviceLineWidth = Math.min(Math.abs(penSize.x), |
|
183 |
Math.abs(penSize.y)); |
|
184 |
||
185 |
/* If the requested line is too thin then map our |
|
186 |
* minimum line width back to user space and set |
|
187 |
* a new BasicStroke. |
|
188 |
*/ |
|
189 |
if (deviceLineWidth < MIN_DEVICE_LINEWIDTH) { |
|
190 |
||
191 |
Point2D.Float minPenSize = new Point2D.Float( |
|
192 |
MIN_DEVICE_LINEWIDTH, |
|
193 |
MIN_DEVICE_LINEWIDTH); |
|
194 |
||
195 |
try { |
|
196 |
AffineTransform inverse; |
|
197 |
float minLineWidth; |
|
198 |
||
199 |
/* Convert the minimum line width from device |
|
200 |
* space to user space. |
|
201 |
*/ |
|
202 |
inverse = deviceTransform.createInverse(); |
|
203 |
inverse.deltaTransform(minPenSize, minPenSize); |
|
204 |
||
205 |
minLineWidth = Math.max(Math.abs(minPenSize.x), |
|
206 |
Math.abs(minPenSize.y)); |
|
207 |
||
208 |
/* Use all of the parameters from the current |
|
209 |
* stroke but change the line width to our |
|
210 |
* calculated minimum. |
|
211 |
*/ |
|
212 |
minLineStroke = new BasicStroke(minLineWidth, |
|
213 |
lineStroke.getEndCap(), |
|
214 |
lineStroke.getLineJoin(), |
|
215 |
lineStroke.getMiterLimit(), |
|
216 |
lineStroke.getDashArray(), |
|
217 |
lineStroke.getDashPhase()); |
|
218 |
setStroke(minLineStroke); |
|
219 |
||
220 |
} catch (NoninvertibleTransformException e) { |
|
221 |
/* If we can't invert the matrix there is something |
|
222 |
* very wrong so don't worry about the minor matter |
|
223 |
* of a minimum line width. |
|
224 |
*/ |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
super.draw(s); |
|
229 |
||
230 |
/* If we changed the stroke, put back the old |
|
231 |
* stroke in order to maintain a minimum line |
|
232 |
* width. |
|
233 |
*/ |
|
234 |
if (minLineStroke != null) { |
|
235 |
setStroke(lineStroke); |
|
236 |
} |
|
237 |
||
238 |
/* The stroke in effect was not a BasicStroke so we |
|
239 |
* will not try to enforce a minimum line width. |
|
240 |
*/ |
|
241 |
} else { |
|
242 |
super.draw(s); |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Draws the text given by the specified string, using this |
|
248 |
* graphics context's current font and color. The baseline of the |
|
249 |
* first character is at position (<i>x</i>, <i>y</i>) in this |
|
250 |
* graphics context's coordinate system. |
|
251 |
* @param str the string to be drawn. |
|
252 |
* @param x the <i>x</i> coordinate. |
|
253 |
* @param y the <i>y</i> coordinate. |
|
254 |
* @see java.awt.Graphics#drawBytes |
|
255 |
* @see java.awt.Graphics#drawChars |
|
256 |
* @since JDK1.0 |
|
257 |
*/ |
|
3928 | 258 |
@Override |
2 | 259 |
public void drawString(String str, int x, int y) { |
260 |
drawString(str, (float) x, (float) y); |
|
261 |
} |
|
262 |
||
3928 | 263 |
@Override |
2 | 264 |
public void drawString(String str, float x, float y) { |
265 |
drawString(str, x, y, getFont(), getFontRenderContext(), 0f); |
|
266 |
} |
|
267 |
||
268 |
/* A return value of 0 would mean font not available to GDI, or the |
|
269 |
* it can't be used for this string. |
|
270 |
* A return of 1 means it is suitable, including for composites. |
|
271 |
* We check that the transform in effect is doable with GDI, and that |
|
272 |
* this is a composite font AWT can handle, or a physical font GDI |
|
273 |
* can handle directly. Its possible that some strings may ultimately |
|
274 |
* fail the more stringent tests in drawString but this is rare and |
|
275 |
* also that method will always succeed, as if the font isn't available |
|
276 |
* it will use outlines via a superclass call. Also it is only called for |
|
277 |
* the default render context (as canDrawStringToWidth() will return |
|
278 |
* false. That is why it ignores the frc and width arguments. |
|
279 |
*/ |
|
3928 | 280 |
@Override |
2 | 281 |
protected int platformFontCount(Font font, String str) { |
282 |
||
283 |
AffineTransform deviceTransform = getTransform(); |
|
284 |
AffineTransform fontTransform = new AffineTransform(deviceTransform); |
|
285 |
fontTransform.concatenate(getFont().getTransform()); |
|
286 |
int transformType = fontTransform.getType(); |
|
287 |
||
288 |
/* Test if GDI can handle the transform */ |
|
289 |
boolean directToGDI = ((transformType != |
|
290 |
AffineTransform.TYPE_GENERAL_TRANSFORM) |
|
291 |
&& ((transformType & AffineTransform.TYPE_FLIP) |
|
292 |
== 0)); |
|
293 |
||
294 |
if (!directToGDI) { |
|
295 |
return 0; |
|
296 |
} |
|
297 |
||
298 |
/* Since all windows fonts are available, and the JRE fonts |
|
299 |
* are also registered. Only the Font.createFont() case is presently |
|
300 |
* unknown to GDI. Those can be registered too, although that |
|
301 |
* code does not exist yet, it can be added too, so we should not |
|
302 |
* fail that case. Just do a quick check whether its a TrueTypeFont |
|
303 |
* - ie not a Type1 font etc, and let drawString() resolve the rest. |
|
304 |
*/ |
|
3928 | 305 |
Font2D font2D = FontUtilities.getFont2D(font); |
2 | 306 |
if (font2D instanceof CompositeFont || |
307 |
font2D instanceof TrueTypeFont) { |
|
308 |
return 1; |
|
309 |
} else { |
|
310 |
return 0; |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
private static boolean isXP() { |
|
315 |
String osVersion = System.getProperty("os.version"); |
|
316 |
if (osVersion != null) { |
|
317 |
Float version = Float.valueOf(osVersion); |
|
318 |
return (version.floatValue() >= 5.1f); |
|
319 |
} else { |
|
320 |
return false; |
|
321 |
} |
|
322 |
} |
|
323 |
||
324 |
/* In case GDI doesn't handle shaping or BIDI consistently with |
|
325 |
* 2D's TextLayout, we can detect these cases and redelegate up to |
|
326 |
* be drawn via TextLayout, which in is rendered as runs of |
|
327 |
* GlyphVectors, to which we can assign positions for each glyph. |
|
328 |
*/ |
|
329 |
private boolean strNeedsTextLayout(String str, Font font) { |
|
330 |
char[] chars = str.toCharArray(); |
|
3928 | 331 |
boolean isComplex = FontUtilities.isComplexText(chars, 0, chars.length); |
2 | 332 |
if (!isComplex) { |
333 |
return false; |
|
334 |
} else if (!useGDITextLayout) { |
|
335 |
return true; |
|
336 |
} else { |
|
337 |
if (preferGDITextLayout || |
|
3928 | 338 |
(isXP() && FontUtilities.textLayoutIsCompatible(font))) { |
2 | 339 |
return false; |
340 |
} else { |
|
341 |
return true; |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
private int getAngle(Point2D.Double pt) { |
|
347 |
/* Get the rotation in 1/10'ths degree (as needed by Windows) |
|
348 |
* so that GDI can draw the text rotated. |
|
349 |
* This calculation is only valid for a uniform scale, no shearing. |
|
350 |
*/ |
|
351 |
double angle = Math.toDegrees(Math.atan2(pt.y, pt.x)); |
|
352 |
if (angle < 0.0) { |
|
353 |
angle+= 360.0; |
|
354 |
} |
|
355 |
/* Windows specifies the rotation anti-clockwise from the x-axis |
|
356 |
* of the device, 2D specifies +ve rotation towards the y-axis |
|
357 |
* Since the 2D y-axis runs from top-to-bottom, windows angle of |
|
358 |
* rotation here is opposite than 2D's, so the rotation needed |
|
359 |
* needs to be recalculated in the opposite direction. |
|
360 |
*/ |
|
361 |
if (angle != 0.0) { |
|
362 |
angle = 360.0 - angle; |
|
363 |
} |
|
364 |
return (int)Math.round(angle * 10.0); |
|
365 |
} |
|
366 |
||
367 |
private float getAwScale(double scaleFactorX, double scaleFactorY) { |
|
368 |
||
369 |
float awScale = (float)(scaleFactorX/scaleFactorY); |
|
370 |
/* don't let rounding errors be interpreted as non-uniform scale */ |
|
371 |
if (awScale > 0.999f && awScale < 1.001f) { |
|
372 |
awScale = 1.0f; |
|
373 |
} |
|
374 |
return awScale; |
|
375 |
} |
|
376 |
||
377 |
/** |
|
378 |
* Renders the text specified by the specified <code>String</code>, |
|
379 |
* using the current <code>Font</code> and <code>Paint</code> attributes |
|
380 |
* in the <code>Graphics2D</code> context. |
|
381 |
* The baseline of the first character is at position |
|
382 |
* (<i>x</i>, <i>y</i>) in the User Space. |
|
383 |
* The rendering attributes applied include the <code>Clip</code>, |
|
384 |
* <code>Transform</code>, <code>Paint</code>, <code>Font</code> and |
|
385 |
* <code>Composite</code> attributes. For characters in script systems |
|
386 |
* such as Hebrew and Arabic, the glyphs can be rendered from right to |
|
387 |
* left, in which case the coordinate supplied is the location of the |
|
388 |
* leftmost character on the baseline. |
|
389 |
* @param s the <code>String</code> to be rendered |
|
390 |
* @param x, y the coordinates where the <code>String</code> |
|
391 |
* should be rendered |
|
392 |
* @see #setPaint |
|
393 |
* @see java.awt.Graphics#setColor |
|
394 |
* @see java.awt.Graphics#setFont |
|
395 |
* @see #setTransform |
|
396 |
* @see #setComposite |
|
397 |
* @see #setClip |
|
398 |
*/ |
|
3928 | 399 |
@Override |
2 | 400 |
public void drawString(String str, float x, float y, |
401 |
Font font, FontRenderContext frc, float targetW) { |
|
402 |
if (str.length() == 0) { |
|
403 |
return; |
|
404 |
} |
|
405 |
||
406 |
if (WPrinterJob.shapeTextProp) { |
|
407 |
super.drawString(str, x, y, font, frc, targetW); |
|
408 |
return; |
|
409 |
} |
|
410 |
||
411 |
/* If the Font has layout attributes we need to delegate to TextLayout. |
|
412 |
* TextLayout renders text as GlyphVectors. We try to print those |
|
413 |
* using printer fonts - ie using Postscript text operators so |
|
414 |
* we may be reinvoked. In that case the "!printingGlyphVector" test |
|
415 |
* prevents us recursing and instead sends us into the body of the |
|
416 |
* method where we can safely ignore layout attributes as those |
|
417 |
* are already handled by TextLayout. |
|
418 |
* Similarly if layout is needed based on the text, then we |
|
419 |
* delegate to TextLayout if possible, or failing that we delegate |
|
420 |
* upwards to filled shapes. |
|
421 |
*/ |
|
422 |
boolean layoutNeeded = strNeedsTextLayout(str, font); |
|
423 |
if ((font.hasLayoutAttributes() || layoutNeeded) |
|
424 |
&& !printingGlyphVector) { |
|
425 |
TextLayout layout = new TextLayout(str, font, frc); |
|
426 |
layout.draw(this, x, y); |
|
427 |
return; |
|
428 |
} else if (layoutNeeded) { |
|
429 |
super.drawString(str, x, y, font, frc, targetW); |
|
430 |
return; |
|
431 |
} |
|
432 |
||
433 |
AffineTransform deviceTransform = getTransform(); |
|
434 |
AffineTransform fontTransform = new AffineTransform(deviceTransform); |
|
435 |
fontTransform.concatenate(font.getTransform()); |
|
436 |
int transformType = fontTransform.getType(); |
|
437 |
||
438 |
/* Use GDI for the text if the graphics transform is something |
|
439 |
* for which we can obtain a suitable GDI font. |
|
440 |
* A flip or shearing transform on the graphics or a transform |
|
441 |
* on the font force us to decompose the text into a shape. |
|
442 |
*/ |
|
443 |
boolean directToGDI = ((transformType != |
|
444 |
AffineTransform.TYPE_GENERAL_TRANSFORM) |
|
445 |
&& ((transformType & AffineTransform.TYPE_FLIP) |
|
446 |
== 0)); |
|
447 |
||
448 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
449 |
try { |
|
450 |
wPrinterJob.setTextColor((Color)getPaint()); |
|
451 |
} catch (ClassCastException e) { // peek should detect such paints. |
|
452 |
directToGDI = false; |
|
453 |
} |
|
454 |
||
455 |
if (!directToGDI) { |
|
456 |
super.drawString(str, x, y, font, frc, targetW); |
|
457 |
return; |
|
458 |
} |
|
459 |
||
460 |
/* Now we have checked everything is OK to go through GDI as text |
|
461 |
* with the exception of testing GDI can find and use the font. That |
|
462 |
* is handled in the textOut() call. |
|
463 |
*/ |
|
464 |
||
465 |
/* Compute the starting position of the string in |
|
466 |
* device space. |
|
467 |
*/ |
|
468 |
Point2D.Float userpos = new Point2D.Float(x, y); |
|
469 |
Point2D.Float devpos = new Point2D.Float(); |
|
470 |
||
471 |
/* Already have the translate from the deviceTransform, |
|
472 |
* but the font may have a translation component too. |
|
473 |
*/ |
|
474 |
if (font.isTransformed()) { |
|
475 |
AffineTransform fontTx = font.getTransform(); |
|
476 |
float translateX = (float)(fontTx.getTranslateX()); |
|
477 |
float translateY = (float)(fontTx.getTranslateY()); |
|
478 |
if (Math.abs(translateX) < 0.00001) translateX = 0f; |
|
479 |
if (Math.abs(translateY) < 0.00001) translateY = 0f; |
|
480 |
userpos.x += translateX; userpos.y += translateY; |
|
481 |
} |
|
482 |
deviceTransform.transform(userpos, devpos); |
|
483 |
||
484 |
if (getClip() != null) { |
|
485 |
deviceClip(getClip().getPathIterator(deviceTransform)); |
|
486 |
} |
|
487 |
||
488 |
/* Get the font size in device coordinates. |
|
489 |
* The size needed is the font height scaled to device space. |
|
490 |
* Although we have already tested that there is no shear, |
|
491 |
* there may be a non-uniform scale, so the width of the font |
|
492 |
* does not scale equally with the height. That is handled |
|
493 |
* by specifying an 'average width' scale to GDI. |
|
494 |
*/ |
|
495 |
float fontSize = font.getSize2D(); |
|
496 |
||
497 |
Point2D.Double pty = new Point2D.Double(0.0, 1.0); |
|
498 |
fontTransform.deltaTransform(pty, pty); |
|
499 |
double scaleFactorY = Math.sqrt(pty.x*pty.x+pty.y*pty.y); |
|
500 |
float scaledFontSizeY = (float)(fontSize * scaleFactorY); |
|
501 |
||
502 |
Point2D.Double ptx = new Point2D.Double(1.0, 0.0); |
|
503 |
fontTransform.deltaTransform(ptx, ptx); |
|
504 |
double scaleFactorX = Math.sqrt(ptx.x*ptx.x+ptx.y*ptx.y); |
|
505 |
float scaledFontSizeX = (float)(fontSize * scaleFactorX); |
|
506 |
||
507 |
float awScale = getAwScale(scaleFactorX, scaleFactorY); |
|
508 |
int iangle = getAngle(ptx); |
|
509 |
||
3928 | 510 |
Font2D font2D = FontUtilities.getFont2D(font); |
2 | 511 |
if (font2D instanceof TrueTypeFont) { |
512 |
textOut(str, font, (TrueTypeFont)font2D, frc, |
|
513 |
scaledFontSizeY, iangle, awScale, |
|
514 |
deviceTransform, scaleFactorX, |
|
515 |
x, y, devpos.x, devpos.y, targetW); |
|
516 |
} else if (font2D instanceof CompositeFont) { |
|
517 |
/* Composite fonts are made up of multiple fonts and each |
|
518 |
* substring that uses a particular component font needs to |
|
519 |
* be separately sent to GDI. |
|
520 |
* This works for standard composite fonts, alternate ones, |
|
521 |
* Fonts that are a physical font backed by a standard composite, |
|
522 |
* and with fallback fonts. |
|
523 |
*/ |
|
524 |
CompositeFont compFont = (CompositeFont)font2D; |
|
525 |
float userx = x, usery = y; |
|
526 |
float devx = devpos.x, devy = devpos.y; |
|
527 |
char[] chars = str.toCharArray(); |
|
528 |
int len = chars.length; |
|
529 |
int[] glyphs = new int[len]; |
|
530 |
compFont.getMapper().charsToGlyphs(len, chars, glyphs); |
|
531 |
||
532 |
int startChar = 0, endChar = 0, slot = 0; |
|
533 |
while (endChar < len) { |
|
534 |
||
535 |
startChar = endChar; |
|
536 |
slot = glyphs[startChar] >>> 24; |
|
537 |
||
538 |
while (endChar < len && ((glyphs[endChar] >>> 24) == slot)) { |
|
539 |
endChar++; |
|
540 |
} |
|
541 |
String substr = new String(chars, startChar,endChar-startChar); |
|
542 |
PhysicalFont slotFont = compFont.getSlotFont(slot); |
|
543 |
textOut(substr, font, slotFont, frc, |
|
544 |
scaledFontSizeY, iangle, awScale, |
|
545 |
deviceTransform, scaleFactorX, |
|
546 |
userx, usery, devx, devy, 0f); |
|
547 |
Rectangle2D bds = font.getStringBounds(substr, frc); |
|
548 |
float xAdvance = (float)bds.getWidth(); |
|
549 |
userx += xAdvance; |
|
550 |
userpos.x += xAdvance; |
|
551 |
deviceTransform.transform(userpos, devpos); |
|
552 |
} |
|
553 |
} else { |
|
554 |
super.drawString(str, x, y, font, frc, targetW); |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
/** return true if the Graphics instance can directly print |
|
559 |
* this glyphvector |
|
560 |
*/ |
|
3928 | 561 |
@Override |
2 | 562 |
protected boolean printGlyphVector(GlyphVector gv, float x, float y) { |
563 |
/* We don't want to try to handle per-glyph transforms. GDI can't |
|
564 |
* handle per-glyph rotations, etc. There's no way to express it |
|
565 |
* in a single call, so just bail for this uncommon case. |
|
566 |
*/ |
|
567 |
if ((gv.getLayoutFlags() & GlyphVector.FLAG_HAS_TRANSFORMS) != 0) { |
|
568 |
return false; |
|
569 |
} |
|
570 |
||
571 |
AffineTransform deviceTransform = getTransform(); |
|
572 |
AffineTransform fontTransform = new AffineTransform(deviceTransform); |
|
573 |
Font font = gv.getFont(); |
|
574 |
fontTransform.concatenate(font.getTransform()); |
|
575 |
int transformType = fontTransform.getType(); |
|
576 |
||
577 |
/* Use GDI for the text if the graphics transform is something |
|
578 |
* for which we can obtain a suitable GDI font. |
|
579 |
* A flip or shearing transform on the graphics or a transform |
|
580 |
* on the font force us to decompose the text into a shape. |
|
581 |
*/ |
|
582 |
boolean directToGDI = |
|
583 |
((transformType != AffineTransform.TYPE_GENERAL_TRANSFORM) && |
|
584 |
((transformType & AffineTransform.TYPE_FLIP) == 0)); |
|
585 |
||
586 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
587 |
try { |
|
588 |
wPrinterJob.setTextColor((Color)getPaint()); |
|
589 |
} catch (ClassCastException e) { // peek should detect such paints. |
|
590 |
directToGDI = false; |
|
591 |
} |
|
592 |
||
593 |
if (WPrinterJob.shapeTextProp || !directToGDI) { |
|
594 |
return false; |
|
595 |
} |
|
596 |
/* Compute the starting position of the string in |
|
597 |
* device space. |
|
598 |
*/ |
|
599 |
Point2D.Float userpos = new Point2D.Float(x, y); |
|
600 |
Point2D.Float devpos = new Point2D.Float(); |
|
601 |
||
602 |
/* Already have the translate from the deviceTransform, |
|
603 |
* but the font may have a translation component too. |
|
604 |
*/ |
|
605 |
if (font.isTransformed()) { |
|
606 |
AffineTransform fontTx = font.getTransform(); |
|
607 |
float translateX = (float)(fontTx.getTranslateX()); |
|
608 |
float translateY = (float)(fontTx.getTranslateY()); |
|
609 |
if (Math.abs(translateX) < 0.00001) translateX = 0f; |
|
610 |
if (Math.abs(translateY) < 0.00001) translateY = 0f; |
|
611 |
userpos.x += translateX; userpos.y += translateY; |
|
612 |
} |
|
613 |
deviceTransform.transform(userpos, devpos); |
|
614 |
||
615 |
if (getClip() != null) { |
|
616 |
deviceClip(getClip().getPathIterator(deviceTransform)); |
|
617 |
} |
|
618 |
||
619 |
/* Get the font size in device coordinates. |
|
620 |
* The size needed is the font height scaled to device space. |
|
621 |
* Although we have already tested that there is no shear, |
|
622 |
* there may be a non-uniform scale, so the width of the font |
|
623 |
* does not scale equally with the height. That is handled |
|
624 |
* by specifying an 'average width' scale to GDI. |
|
625 |
*/ |
|
626 |
float fontSize = font.getSize2D(); |
|
627 |
||
628 |
Point2D.Double pty = new Point2D.Double(0.0, 1.0); |
|
629 |
fontTransform.deltaTransform(pty, pty); |
|
630 |
double scaleFactorY = Math.sqrt(pty.x*pty.x+pty.y*pty.y); |
|
631 |
float scaledFontSizeY = (float)(fontSize * scaleFactorY); |
|
632 |
||
633 |
Point2D.Double pt = new Point2D.Double(1.0, 0.0); |
|
634 |
fontTransform.deltaTransform(pt, pt); |
|
635 |
double scaleFactorX = Math.sqrt(pt.x*pt.x+pt.y*pt.y); |
|
636 |
float scaledFontSizeX = (float)(fontSize * scaleFactorX); |
|
637 |
||
638 |
float awScale = getAwScale(scaleFactorX, scaleFactorY); |
|
639 |
int iangle = getAngle(pt); |
|
640 |
||
641 |
int numGlyphs = gv.getNumGlyphs(); |
|
642 |
int[] glyphCodes = gv.getGlyphCodes(0, numGlyphs, null); |
|
643 |
float[] glyphPos = gv.getGlyphPositions(0, numGlyphs, null); |
|
644 |
||
645 |
/* layout replaces glyphs which have been combined away |
|
646 |
* with 0xfffe or 0xffff. These are supposed to be invisible |
|
647 |
* and we need to handle this here as GDI will interpret it |
|
648 |
* as a missing glyph. We'll do it here by compacting the |
|
649 |
* glyph codes array, but we have to do it in conjunction with |
|
650 |
* compacting the positions/advances arrays too AND updating |
|
651 |
* the number of glyphs .. |
|
652 |
* Note that since the slot number for composites is in the |
|
653 |
* significant byte we need to mask out that for comparison of |
|
654 |
* the invisible glyph. |
|
655 |
*/ |
|
656 |
int invisibleGlyphCnt = 0; |
|
657 |
for (int gc=0; gc<numGlyphs; gc++) { |
|
658 |
if ((glyphCodes[gc] & 0xffff) >= |
|
659 |
CharToGlyphMapper.INVISIBLE_GLYPHS) { |
|
660 |
invisibleGlyphCnt++; |
|
661 |
} |
|
662 |
} |
|
663 |
if (invisibleGlyphCnt > 0) { |
|
664 |
int visibleGlyphCnt = numGlyphs - invisibleGlyphCnt; |
|
665 |
int[] visibleGlyphCodes = new int[visibleGlyphCnt]; |
|
666 |
float[] visiblePositions = new float[visibleGlyphCnt*2]; |
|
667 |
int index = 0; |
|
668 |
for (int i=0; i<numGlyphs; i++) { |
|
669 |
if ((glyphCodes[i] & 0xffff) |
|
670 |
< CharToGlyphMapper.INVISIBLE_GLYPHS) { |
|
671 |
visibleGlyphCodes[index] = glyphCodes[i]; |
|
672 |
visiblePositions[index*2] = glyphPos[i*2]; |
|
673 |
visiblePositions[index*2+1] = glyphPos[i*2+1]; |
|
674 |
index++; |
|
675 |
} |
|
676 |
} |
|
677 |
numGlyphs = visibleGlyphCnt; |
|
678 |
glyphCodes = visibleGlyphCodes; |
|
679 |
glyphPos = visiblePositions; |
|
680 |
} |
|
681 |
||
682 |
/* To get GDI to rotate glyphs we need to specify the angle |
|
683 |
* of rotation to GDI when creating the HFONT. This implicitly |
|
684 |
* also rotates the baseline, and this adjusts the X & Y advances |
|
685 |
* of the glyphs accordingly. |
|
686 |
* When we specify the advances, they are in device space, so |
|
687 |
* we don't want any further interpretation applied by GDI, but |
|
688 |
* since as noted the advances are interpreted in the HFONT's |
|
689 |
* coordinate space, our advances would be rotated again. |
|
690 |
* We don't have any way to tell GDI to rotate only the glyphs and |
|
691 |
* not the advances, so we need to account for this in the advances |
|
692 |
* we supply, by supplying unrotated advances. |
|
693 |
* Note that "iangle" is in the opposite direction to 2D's normal |
|
694 |
* direction of rotation, so this rotation inverts the |
|
695 |
* rotation element of the deviceTransform. |
|
696 |
*/ |
|
697 |
AffineTransform advanceTransform = |
|
698 |
new AffineTransform(deviceTransform); |
|
699 |
advanceTransform.rotate(iangle*Math.PI/1800.0); |
|
700 |
float[] glyphAdvPos = new float[glyphPos.length]; |
|
701 |
||
702 |
advanceTransform.transform(glyphPos, 0, //source |
|
703 |
glyphAdvPos, 0, //destination |
|
704 |
glyphPos.length/2); //num points |
|
705 |
||
3928 | 706 |
Font2D font2D = FontUtilities.getFont2D(font); |
2 | 707 |
if (font2D instanceof TrueTypeFont) { |
708 |
String family = font2D.getFamilyName(null); |
|
709 |
int style = font.getStyle() | font2D.getStyle(); |
|
710 |
if (!wPrinterJob.setFont(family, scaledFontSizeY, style, |
|
711 |
iangle, awScale)) { |
|
712 |
return false; |
|
713 |
} |
|
714 |
wPrinterJob.glyphsOut(glyphCodes, devpos.x, devpos.y, glyphAdvPos); |
|
715 |
||
716 |
} else if (font2D instanceof CompositeFont) { |
|
717 |
/* Composite fonts are made up of multiple fonts and each |
|
718 |
* substring that uses a particular component font needs to |
|
719 |
* be separately sent to GDI. |
|
720 |
* This works for standard composite fonts, alternate ones, |
|
721 |
* Fonts that are a physical font backed by a standard composite, |
|
722 |
* and with fallback fonts. |
|
723 |
*/ |
|
724 |
CompositeFont compFont = (CompositeFont)font2D; |
|
725 |
float userx = x, usery = y; |
|
726 |
float devx = devpos.x, devy = devpos.y; |
|
727 |
||
728 |
int start = 0, end = 0, slot = 0; |
|
729 |
while (end < numGlyphs) { |
|
730 |
||
731 |
start = end; |
|
732 |
slot = glyphCodes[start] >>> 24; |
|
733 |
||
734 |
while (end < numGlyphs && ((glyphCodes[end] >>> 24) == slot)) { |
|
735 |
end++; |
|
736 |
} |
|
737 |
/* If we can't get the font, bail to outlines. |
|
738 |
* But we should always be able to get all fonts for |
|
739 |
* Composites, so this is unlikely, so any overstriking |
|
740 |
* if only one slot is unavailable is not worth worrying |
|
741 |
* about. |
|
742 |
*/ |
|
743 |
PhysicalFont slotFont = compFont.getSlotFont(slot); |
|
744 |
if (!(slotFont instanceof TrueTypeFont)) { |
|
745 |
return false; |
|
746 |
} |
|
747 |
String family = slotFont.getFamilyName(null); |
|
748 |
int style = font.getStyle() | slotFont.getStyle(); |
|
749 |
if (!wPrinterJob.setFont(family, scaledFontSizeY, style, |
|
750 |
iangle, awScale)) { |
|
751 |
return false; |
|
752 |
} |
|
753 |
||
754 |
int[] glyphs = Arrays.copyOfRange(glyphCodes, start, end); |
|
755 |
float[] posns = Arrays.copyOfRange(glyphAdvPos, |
|
756 |
start*2, end*2); |
|
757 |
if (start != 0) { |
|
758 |
Point2D.Float p = |
|
759 |
new Point2D.Float(x+glyphPos[start*2], |
|
760 |
y+glyphPos[start*2+1]); |
|
761 |
deviceTransform.transform(p, p); |
|
762 |
devx = p.x; |
|
763 |
devy = p.y; |
|
764 |
} |
|
765 |
wPrinterJob.glyphsOut(glyphs, devx, devy, posns); |
|
766 |
} |
|
767 |
} else { |
|
768 |
return false; |
|
769 |
} |
|
770 |
return true; |
|
771 |
} |
|
772 |
||
773 |
private void textOut(String str, |
|
774 |
Font font, PhysicalFont font2D, |
|
775 |
FontRenderContext frc, |
|
776 |
float deviceSize, int rotation, float awScale, |
|
777 |
AffineTransform deviceTransform, |
|
778 |
double scaleFactorX, |
|
779 |
float userx, float usery, |
|
780 |
float devx, float devy, float targetW) { |
|
781 |
||
782 |
String family = font2D.getFamilyName(null); |
|
783 |
int style = font.getStyle() | font2D.getStyle(); |
|
784 |
WPrinterJob wPrinterJob = (WPrinterJob)getPrinterJob(); |
|
785 |
boolean setFont = wPrinterJob.setFont(family, deviceSize, style, |
|
786 |
rotation, awScale); |
|
787 |
if (!setFont) { |
|
788 |
super.drawString(str, userx, usery, font, frc, targetW); |
|
789 |
return; |
|
790 |
} |
|
791 |
||
792 |
float[] glyphPos = null; |
|
793 |
if (!okGDIMetrics(str, font, frc, scaleFactorX)) { |
|
794 |
/* If there is a 1:1 char->glyph mapping then char positions |
|
795 |
* are the same as glyph positions and we can tell GDI |
|
796 |
* where to place the glyphs. |
|
797 |
* On drawing we remove control chars so these need to be |
|
798 |
* removed now so the string and positions are the same length. |
|
799 |
* For other cases we need to pass glyph codes to GDI. |
|
800 |
*/ |
|
801 |
str = wPrinterJob.removeControlChars(str); |
|
802 |
char[] chars = str.toCharArray(); |
|
803 |
int len = chars.length; |
|
804 |
GlyphVector gv = null; |
|
3928 | 805 |
if (!FontUtilities.isComplexText(chars, 0, len)) { |
2 | 806 |
gv = font.createGlyphVector(frc, str); |
807 |
} |
|
808 |
if (gv == null) { |
|
809 |
super.drawString(str, userx, usery, font, frc, targetW); |
|
810 |
return; |
|
811 |
} |
|
812 |
glyphPos = gv.getGlyphPositions(0, len, null); |
|
813 |
Point2D gvAdvPt = gv.getGlyphPosition(gv.getNumGlyphs()); |
|
814 |
||
815 |
/* GDI advances must not include device space rotation. |
|
816 |
* See earlier comment in printGlyphVector() for details. |
|
817 |
*/ |
|
818 |
AffineTransform advanceTransform = |
|
819 |
new AffineTransform(deviceTransform); |
|
820 |
advanceTransform.rotate(rotation*Math.PI/1800.0); |
|
821 |
float[] glyphAdvPos = new float[glyphPos.length]; |
|
822 |
||
823 |
advanceTransform.transform(glyphPos, 0, //source |
|
824 |
glyphAdvPos, 0, //destination |
|
825 |
glyphPos.length/2); //num points |
|
826 |
glyphPos = glyphAdvPos; |
|
827 |
} |
|
828 |
wPrinterJob.textOut(str, devx, devy, glyphPos); |
|
829 |
} |
|
830 |
||
831 |
/* If 2D and GDI agree on the advance of the string we do not |
|
832 |
* need to explicitly assign glyph positions. |
|
833 |
* If we are to use the GDI advance, require it to agree with |
|
834 |
* JDK to a precision of <= 0.2% - ie 1 pixel in 500 |
|
835 |
* discrepancy after rounding the 2D advance to the |
|
836 |
* nearest pixel and is greater than one pixel in total. |
|
837 |
* ie strings < 500 pixels in length will be OK so long |
|
838 |
* as they differ by only 1 pixel even though that is > 0.02% |
|
839 |
* The bounds from 2D are in user space so need to |
|
840 |
* be scaled to device space for comparison with GDI. |
|
841 |
* scaleX is the scale from user space to device space needed for this. |
|
842 |
*/ |
|
843 |
private boolean okGDIMetrics(String str, Font font, |
|
844 |
FontRenderContext frc, double scaleX) { |
|
845 |
||
846 |
Rectangle2D bds = font.getStringBounds(str, frc); |
|
847 |
double jdkAdvance = bds.getWidth(); |
|
848 |
jdkAdvance = Math.round(jdkAdvance*scaleX); |
|
849 |
int gdiAdvance = ((WPrinterJob)getPrinterJob()).getGDIAdvance(str); |
|
850 |
if (jdkAdvance > 0 && gdiAdvance > 0) { |
|
851 |
double diff = Math.abs(gdiAdvance-jdkAdvance); |
|
852 |
double ratio = gdiAdvance/jdkAdvance; |
|
853 |
if (ratio < 1) { |
|
854 |
ratio = 1/ratio; |
|
855 |
} |
|
856 |
return diff <= 1 || ratio < 1.002; |
|
857 |
} |
|
858 |
return true; |
|
859 |
} |
|
860 |
||
861 |
/** |
|
862 |
* The various <code>drawImage()</code> methods for |
|
863 |
* <code>WPathGraphics</code> are all decomposed |
|
864 |
* into an invocation of <code>drawImageToPlatform</code>. |
|
865 |
* The portion of the passed in image defined by |
|
866 |
* <code>srcX, srcY, srcWidth, and srcHeight</code> |
|
867 |
* is transformed by the supplied AffineTransform and |
|
868 |
* drawn using GDI to the printer context. |
|
869 |
* |
|
870 |
* @param img The image to be drawn. |
|
871 |
* @param xform Used to tranform the image before drawing. |
|
872 |
* This can be null. |
|
873 |
* @param bgcolor This color is drawn where the image has transparent |
|
874 |
* pixels. If this parameter is null then the |
|
875 |
* pixels already in the destination should show |
|
876 |
* through. |
|
877 |
* @param srcX With srcY this defines the upper-left corner |
|
878 |
* of the portion of the image to be drawn. |
|
879 |
* |
|
880 |
* @param srcY With srcX this defines the upper-left corner |
|
881 |
* of the portion of the image to be drawn. |
|
882 |
* @param srcWidth The width of the portion of the image to |
|
883 |
* be drawn. |
|
884 |
* @param srcHeight The height of the portion of the image to |
|
885 |
* be drawn. |
|
886 |
* @param handlingTransparency if being recursively called to |
|
887 |
* print opaque region of transparent image |
|
888 |
*/ |
|
889 |
protected boolean drawImageToPlatform(Image image, AffineTransform xform, |
|
890 |
Color bgcolor, |
|
891 |
int srcX, int srcY, |
|
892 |
int srcWidth, int srcHeight, |
|
893 |
boolean handlingTransparency) { |
|
894 |
||
895 |
BufferedImage img = getBufferedImage(image); |
|
896 |
if (img == null) { |
|
897 |
return true; |
|
898 |
} |
|
899 |
||
900 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
901 |
||
902 |
/* The full transform to be applied to the image is the |
|
903 |
* caller's transform concatenated on to the transform |
|
904 |
* from user space to device space. If the caller didn't |
|
905 |
* supply a transform then we just act as if they passed |
|
906 |
* in the identify transform. |
|
907 |
*/ |
|
908 |
AffineTransform fullTransform = getTransform(); |
|
909 |
if (xform == null) { |
|
910 |
xform = new AffineTransform(); |
|
911 |
} |
|
912 |
fullTransform.concatenate(xform); |
|
913 |
||
914 |
/* Split the full transform into a pair of |
|
915 |
* transforms. The first transform holds effects |
|
916 |
* that GDI (under Win95) can not perform such |
|
917 |
* as rotation and shearing. The second transform |
|
918 |
* is setup to hold only the scaling effects. |
|
919 |
* These transforms are created such that a point, |
|
920 |
* p, in user space, when transformed by 'fullTransform' |
|
921 |
* lands in the same place as when it is transformed |
|
922 |
* by 'rotTransform' and then 'scaleTransform'. |
|
923 |
* |
|
924 |
* The entire image transformation is not in Java in order |
|
925 |
* to minimize the amount of memory needed in the VM. By |
|
926 |
* dividing the transform in two, we rotate and shear |
|
927 |
* the source image in its own space and only go to |
|
928 |
* the, usually, larger, device space when we ask |
|
929 |
* GDI to perform the final scaling. |
|
930 |
* Clamp this to the device scale for better quality printing. |
|
931 |
*/ |
|
932 |
double[] fullMatrix = new double[6]; |
|
933 |
fullTransform.getMatrix(fullMatrix); |
|
934 |
||
935 |
/* Calculate the amount of scaling in the x |
|
936 |
* and y directions. This scaling is computed by |
|
937 |
* transforming a unit vector along each axis |
|
938 |
* and computing the resulting magnitude. |
|
939 |
* The computed values 'scaleX' and 'scaleY' |
|
940 |
* represent the amount of scaling GDI will be asked |
|
941 |
* to perform. |
|
942 |
*/ |
|
943 |
Point2D.Float unitVectorX = new Point2D.Float(1, 0); |
|
944 |
Point2D.Float unitVectorY = new Point2D.Float(0, 1); |
|
945 |
fullTransform.deltaTransform(unitVectorX, unitVectorX); |
|
946 |
fullTransform.deltaTransform(unitVectorY, unitVectorY); |
|
947 |
||
948 |
Point2D.Float origin = new Point2D.Float(0, 0); |
|
949 |
double scaleX = unitVectorX.distance(origin); |
|
950 |
double scaleY = unitVectorY.distance(origin); |
|
951 |
||
952 |
double devResX = wPrinterJob.getXRes(); |
|
953 |
double devResY = wPrinterJob.getYRes(); |
|
954 |
double devScaleX = devResX / DEFAULT_USER_RES; |
|
955 |
double devScaleY = devResY / DEFAULT_USER_RES; |
|
553 | 956 |
|
957 |
/* check if rotated or sheared */ |
|
958 |
int transformType = fullTransform.getType(); |
|
959 |
boolean clampScale = ((transformType & |
|
960 |
(AffineTransform.TYPE_GENERAL_ROTATION | |
|
961 |
AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0); |
|
962 |
if (clampScale) { |
|
963 |
if (scaleX > devScaleX) scaleX = devScaleX; |
|
964 |
if (scaleY > devScaleY) scaleY = devScaleY; |
|
965 |
} |
|
2 | 966 |
|
967 |
/* We do not need to draw anything if either scaling |
|
968 |
* factor is zero. |
|
969 |
*/ |
|
970 |
if (scaleX != 0 && scaleY != 0) { |
|
971 |
||
972 |
/* Here's the transformation we will do with Java2D, |
|
973 |
*/ |
|
974 |
AffineTransform rotTransform = new AffineTransform( |
|
975 |
fullMatrix[0] / scaleX, //m00 |
|
976 |
fullMatrix[1] / scaleY, //m10 |
|
977 |
fullMatrix[2] / scaleX, //m01 |
|
978 |
fullMatrix[3] / scaleY, //m11 |
|
979 |
fullMatrix[4] / scaleX, //m02 |
|
980 |
fullMatrix[5] / scaleY); //m12 |
|
981 |
||
982 |
/* The scale transform is not used directly: we instead |
|
983 |
* directly multiply by scaleX and scaleY. |
|
984 |
* |
|
985 |
* Conceptually here is what the scaleTransform is: |
|
986 |
* |
|
987 |
* AffineTransform scaleTransform = new AffineTransform( |
|
988 |
* scaleX, //m00 |
|
989 |
* 0, //m10 |
|
990 |
* 0, //m01 |
|
991 |
* scaleY, //m11 |
|
992 |
* 0, //m02 |
|
993 |
* 0); //m12 |
|
994 |
*/ |
|
995 |
||
996 |
/* Convert the image source's rectangle into the rotated |
|
997 |
* and sheared space. Once there, we calculate a rectangle |
|
998 |
* that encloses the resulting shape. It is this rectangle |
|
999 |
* which defines the size of the BufferedImage we need to |
|
1000 |
* create to hold the transformed image. |
|
1001 |
*/ |
|
1002 |
Rectangle2D.Float srcRect = new Rectangle2D.Float(srcX, srcY, |
|
1003 |
srcWidth, |
|
1004 |
srcHeight); |
|
1005 |
||
1006 |
Shape rotShape = rotTransform.createTransformedShape(srcRect); |
|
1007 |
Rectangle2D rotBounds = rotShape.getBounds2D(); |
|
1008 |
||
1009 |
/* add a fudge factor as some fp precision problems have |
|
1010 |
* been observed which caused pixels to be rounded down and |
|
1011 |
* out of the image. |
|
1012 |
*/ |
|
1013 |
rotBounds.setRect(rotBounds.getX(), rotBounds.getY(), |
|
1014 |
rotBounds.getWidth()+0.001, |
|
1015 |
rotBounds.getHeight()+0.001); |
|
1016 |
||
1017 |
int boundsWidth = (int) rotBounds.getWidth(); |
|
1018 |
int boundsHeight = (int) rotBounds.getHeight(); |
|
1019 |
||
1020 |
if (boundsWidth > 0 && boundsHeight > 0) { |
|
1021 |
||
1022 |
/* If the image has transparent or semi-transparent |
|
1023 |
* pixels then we'll have the application re-render |
|
1024 |
* the portion of the page covered by the image. |
|
1025 |
* The BufferedImage will be at the image's resolution |
|
1026 |
* to avoid wasting memory. By re-rendering this portion |
|
1027 |
* of a page all compositing is done by Java2D into |
|
1028 |
* the BufferedImage and then that image is copied to |
|
1029 |
* GDI. |
|
1030 |
* However several special cases can be handled otherwise: |
|
1031 |
* - bitmask transparency with a solid background colour |
|
1032 |
* - images which have transparency color models but no |
|
1033 |
* transparent pixels |
|
1034 |
* - images with bitmask transparency and an IndexColorModel |
|
1035 |
* (the common transparent GIF case) can be handled by |
|
1036 |
* rendering just the opaque pixels. |
|
1037 |
*/ |
|
1038 |
boolean drawOpaque = true; |
|
1039 |
if (!handlingTransparency && hasTransparentPixels(img)) { |
|
1040 |
drawOpaque = false; |
|
1041 |
if (isBitmaskTransparency(img)) { |
|
1042 |
if (bgcolor == null) { |
|
1043 |
if (drawBitmaskImage(img, xform, bgcolor, |
|
1044 |
srcX, srcY, |
|
1045 |
srcWidth, srcHeight)) { |
|
1046 |
// image drawn, just return. |
|
1047 |
return true; |
|
1048 |
} |
|
1049 |
} else if (bgcolor.getTransparency() |
|
1050 |
== Transparency.OPAQUE) { |
|
1051 |
drawOpaque = true; |
|
1052 |
} |
|
1053 |
} |
|
1054 |
if (!canDoRedraws()) { |
|
1055 |
drawOpaque = true; |
|
1056 |
} |
|
1057 |
} else { |
|
1058 |
// if there's no transparent pixels there's no need |
|
1059 |
// for a background colour. This can avoid edge artifacts |
|
1060 |
// in rotation cases. |
|
1061 |
bgcolor = null; |
|
1062 |
} |
|
1063 |
// if src region extends beyond the image, the "opaque" path |
|
1064 |
// may blit b/g colour (including white) where it shoudn't. |
|
1065 |
if ((srcX+srcWidth > img.getWidth(null) || |
|
1066 |
srcY+srcHeight > img.getHeight(null)) |
|
1067 |
&& canDoRedraws()) { |
|
1068 |
drawOpaque = false; |
|
1069 |
} |
|
1070 |
if (drawOpaque == false) { |
|
1071 |
||
1072 |
fullTransform.getMatrix(fullMatrix); |
|
1073 |
AffineTransform tx = |
|
1074 |
new AffineTransform( |
|
1075 |
fullMatrix[0] / devScaleX, //m00 |
|
1076 |
fullMatrix[1] / devScaleY, //m10 |
|
1077 |
fullMatrix[2] / devScaleX, //m01 |
|
1078 |
fullMatrix[3] / devScaleY, //m11 |
|
1079 |
fullMatrix[4] / devScaleX, //m02 |
|
1080 |
fullMatrix[5] / devScaleY); //m12 |
|
1081 |
||
1082 |
Rectangle2D.Float rect = |
|
1083 |
new Rectangle2D.Float(srcX, srcY, srcWidth, srcHeight); |
|
1084 |
||
1085 |
Shape shape = fullTransform.createTransformedShape(rect); |
|
1086 |
// Region isn't user space because its potentially |
|
1087 |
// been rotated for landscape. |
|
1088 |
Rectangle2D region = shape.getBounds2D(); |
|
1089 |
||
1090 |
region.setRect(region.getX(), region.getY(), |
|
1091 |
region.getWidth()+0.001, |
|
1092 |
region.getHeight()+0.001); |
|
1093 |
||
1094 |
// Try to limit the amount of memory used to 8Mb, so |
|
1095 |
// if at device resolution this exceeds a certain |
|
1096 |
// image size then scale down the region to fit in |
|
1097 |
// that memory, but never to less than 72 dpi. |
|
1098 |
||
1099 |
int w = (int)region.getWidth(); |
|
1100 |
int h = (int)region.getHeight(); |
|
1101 |
int nbytes = w * h * 3; |
|
1102 |
int maxBytes = 8 * 1024 * 1024; |
|
1103 |
double origDpi = (devResX < devResY) ? devResX : devResY; |
|
1104 |
int dpi = (int)origDpi; |
|
1105 |
double scaleFactor = 1; |
|
1106 |
||
1107 |
double maxSFX = w/(double)boundsWidth; |
|
1108 |
double maxSFY = h/(double)boundsHeight; |
|
1109 |
double maxSF = (maxSFX > maxSFY) ? maxSFY : maxSFX; |
|
1110 |
int minDpi = (int)(dpi/maxSF); |
|
1111 |
if (minDpi < DEFAULT_USER_RES) minDpi = DEFAULT_USER_RES; |
|
1112 |
||
1113 |
while (nbytes > maxBytes && dpi > minDpi) { |
|
1114 |
scaleFactor *= 2; |
|
1115 |
dpi /= 2; |
|
1116 |
nbytes /= 4; |
|
1117 |
} |
|
1118 |
if (dpi < minDpi) { |
|
1119 |
scaleFactor = (origDpi / minDpi); |
|
1120 |
} |
|
1121 |
||
1122 |
region.setRect(region.getX()/scaleFactor, |
|
1123 |
region.getY()/scaleFactor, |
|
1124 |
region.getWidth()/scaleFactor, |
|
1125 |
region.getHeight()/scaleFactor); |
|
1126 |
||
1127 |
/* |
|
1128 |
* We need to have the clip as part of the saved state, |
|
1129 |
* either directly, or all the components that are |
|
1130 |
* needed to reconstitute it (image source area, |
|
1131 |
* image transform and current graphics transform). |
|
1132 |
* The clip is described in user space, so we need to |
|
1133 |
* save the current graphics transform anyway so just |
|
1134 |
* save these two. |
|
1135 |
*/ |
|
1136 |
wPrinterJob.saveState(getTransform(), getClip(), |
|
1137 |
region, scaleFactor, scaleFactor); |
|
1138 |
return true; |
|
1139 |
/* The image can be rendered directly by GDI so we |
|
1140 |
* copy it into a BufferedImage (this takes care of |
|
1141 |
* ColorSpace and BufferedImageOp issues) and then |
|
1142 |
* send that to GDI. |
|
1143 |
*/ |
|
1144 |
} else { |
|
1145 |
/* Create a buffered image big enough to hold the portion |
|
1146 |
* of the source image being printed. |
|
1147 |
* The image format will be 3BYTE_BGR for most cases |
|
1148 |
* except where we can represent the image as a 1, 4 or 8 |
|
1149 |
* bits-per-pixel DIB. |
|
1150 |
*/ |
|
1151 |
int dibType = BufferedImage.TYPE_3BYTE_BGR; |
|
1152 |
IndexColorModel icm = null; |
|
1153 |
||
1154 |
ColorModel cm = img.getColorModel(); |
|
1155 |
int imgType = img.getType(); |
|
1156 |
if (cm instanceof IndexColorModel && |
|
1157 |
cm.getPixelSize() <= 8 && |
|
1158 |
(imgType == BufferedImage.TYPE_BYTE_BINARY || |
|
1159 |
imgType == BufferedImage.TYPE_BYTE_INDEXED)) { |
|
1160 |
icm = (IndexColorModel)cm; |
|
1161 |
dibType = imgType; |
|
1162 |
/* BYTE_BINARY may be 2 bpp which DIB can't handle. |
|
1163 |
* Convert this to 4bpp. |
|
1164 |
*/ |
|
1165 |
if (imgType == BufferedImage.TYPE_BYTE_BINARY && |
|
1166 |
cm.getPixelSize() == 2) { |
|
1167 |
||
1168 |
int[] rgbs = new int[16]; |
|
1169 |
icm.getRGBs(rgbs); |
|
1170 |
boolean transparent = |
|
1171 |
icm.getTransparency() != Transparency.OPAQUE; |
|
1172 |
int transpixel = icm.getTransparentPixel(); |
|
1173 |
||
1174 |
icm = new IndexColorModel(4, 16, |
|
1175 |
rgbs, 0, |
|
1176 |
transparent, transpixel, |
|
1177 |
DataBuffer.TYPE_BYTE); |
|
1178 |
} |
|
1179 |
} |
|
1180 |
||
1181 |
int iw = (int)rotBounds.getWidth(); |
|
1182 |
int ih = (int)rotBounds.getHeight(); |
|
1183 |
BufferedImage deepImage = null; |
|
1184 |
/* If there is no special transform needed (this is a |
|
1185 |
* simple BLIT) and dibType == img.getType() and we |
|
1186 |
* didn't create a new IndexColorModel AND the whole of |
|
1187 |
* the source image is being drawn (GDI can't handle a |
|
1188 |
* portion of the original source image) then we |
|
1189 |
* don't need to create this intermediate image - GDI |
|
1190 |
* can access the data from the original image. |
|
1191 |
* Since a subimage can be created by calling |
|
1192 |
* BufferedImage.getSubImage() that condition needs to |
|
1193 |
* be accounted for too. This implies inspecting the |
|
1194 |
* data buffer. In the end too many cases are not able |
|
1195 |
* to take advantage of this option until we can teach |
|
1196 |
* the native code to properly navigate the data buffer. |
|
1197 |
* There was a concern that since in native code since we |
|
1198 |
* need to DWORD align and flip to a bottom up DIB that |
|
1199 |
* the "original" image may get perturbed by this. |
|
1200 |
* But in fact we always malloc new memory for the aligned |
|
1201 |
* copy so this isn't a problem. |
|
1202 |
* This points out that we allocate two temporaries copies |
|
1203 |
* of the image : one in Java and one in native. If |
|
1204 |
* we can be smarter about not allocating this one when |
|
1205 |
* not needed, that would seem like a good thing to do, |
|
1206 |
* even if in many cases the ColorModels don't match and |
|
1207 |
* its needed. |
|
1208 |
* Until all of this is resolved newImage is always true. |
|
1209 |
*/ |
|
1210 |
boolean newImage = true; |
|
1211 |
if (newImage) { |
|
1212 |
if (icm == null) { |
|
1213 |
deepImage = new BufferedImage(iw, ih, dibType); |
|
1214 |
} else { |
|
1215 |
deepImage = new BufferedImage(iw, ih, dibType,icm); |
|
1216 |
} |
|
1217 |
||
1218 |
/* Setup a Graphics2D on to the BufferedImage so that |
|
1219 |
* the source image when copied, lands within the |
|
1220 |
* image buffer. |
|
1221 |
*/ |
|
1222 |
Graphics2D imageGraphics = deepImage.createGraphics(); |
|
1223 |
imageGraphics.clipRect(0, 0, |
|
1224 |
deepImage.getWidth(), |
|
1225 |
deepImage.getHeight()); |
|
1226 |
||
1227 |
imageGraphics.translate(-rotBounds.getX(), |
|
1228 |
-rotBounds.getY()); |
|
1229 |
imageGraphics.transform(rotTransform); |
|
1230 |
||
1231 |
/* Fill the BufferedImage either with the caller |
|
1232 |
* supplied color, 'bgColor' or, if null, with white. |
|
1233 |
*/ |
|
1234 |
if (bgcolor == null) { |
|
1235 |
bgcolor = Color.white; |
|
1236 |
} |
|
1237 |
||
1238 |
imageGraphics.drawImage(img, |
|
1239 |
srcX, srcY, |
|
1240 |
srcX + srcWidth, |
|
1241 |
srcY + srcHeight, |
|
1242 |
srcX, srcY, |
|
1243 |
srcX + srcWidth, |
|
1244 |
srcY + srcHeight, |
|
1245 |
bgcolor, null); |
|
1246 |
imageGraphics.dispose(); |
|
1247 |
} else { |
|
1248 |
deepImage = img; |
|
1249 |
} |
|
1250 |
||
1251 |
/* Scale the bounding rectangle by the scale transform. |
|
1252 |
* Because the scaling transform has only x and y |
|
1253 |
* scaling components it is equivalent to multiply |
|
1254 |
* the x components of the bounding rectangle by |
|
1255 |
* the x scaling factor and to multiply the y components |
|
1256 |
* by the y scaling factor. |
|
1257 |
*/ |
|
1258 |
Rectangle2D.Float scaledBounds |
|
1259 |
= new Rectangle2D.Float( |
|
1260 |
(float) (rotBounds.getX() * scaleX), |
|
1261 |
(float) (rotBounds.getY() * scaleY), |
|
1262 |
(float) (rotBounds.getWidth() * scaleX), |
|
1263 |
(float) (rotBounds.getHeight() * scaleY)); |
|
1264 |
||
1265 |
/* Pull the raster data from the buffered image |
|
1266 |
* and pass it along to GDI. |
|
1267 |
*/ |
|
1268 |
WritableRaster raster = deepImage.getRaster(); |
|
1269 |
byte[] data; |
|
1270 |
if (raster instanceof ByteComponentRaster) { |
|
1271 |
data = ((ByteComponentRaster)raster).getDataStorage(); |
|
1272 |
} else if (raster instanceof BytePackedRaster) { |
|
1273 |
data = ((BytePackedRaster)raster).getDataStorage(); |
|
1274 |
} else { |
|
1275 |
return false; |
|
1276 |
} |
|
1277 |
||
8352
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1278 |
int bitsPerPixel = 24; |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1279 |
SampleModel sm = deepImage.getSampleModel(); |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1280 |
if (sm instanceof ComponentSampleModel) { |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1281 |
ComponentSampleModel csm = (ComponentSampleModel)sm; |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1282 |
bitsPerPixel = csm.getPixelStride() * 8; |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1283 |
} else if (sm instanceof MultiPixelPackedSampleModel) { |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1284 |
MultiPixelPackedSampleModel mppsm = |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1285 |
(MultiPixelPackedSampleModel)sm; |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1286 |
bitsPerPixel = mppsm.getPixelBitStride(); |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1287 |
} else { |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1288 |
if (icm != null) { |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1289 |
int diw = deepImage.getWidth(); |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1290 |
int dih = deepImage.getHeight(); |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1291 |
if (diw > 0 && dih > 0) { |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1292 |
bitsPerPixel = data.length*8/diw/dih; |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1293 |
} |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1294 |
} |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1295 |
} |
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1296 |
|
2 | 1297 |
/* Because the caller's image has been rotated |
1298 |
* and sheared into our BufferedImage and because |
|
1299 |
* we will be handing that BufferedImage directly to |
|
1300 |
* GDI, we need to set an additional clip. This clip |
|
1301 |
* makes sure that only parts of the BufferedImage |
|
1302 |
* that are also part of the caller's image are drawn. |
|
1303 |
*/ |
|
1304 |
Shape holdClip = getClip(); |
|
1305 |
clip(xform.createTransformedShape(srcRect)); |
|
1306 |
deviceClip(getClip().getPathIterator(getTransform())); |
|
1307 |
||
1308 |
wPrinterJob.drawDIBImage |
|
1309 |
(data, scaledBounds.x, scaledBounds.y, |
|
1310 |
(float)Math.rint(scaledBounds.width+0.5), |
|
1311 |
(float)Math.rint(scaledBounds.height+0.5), |
|
1312 |
0f, 0f, |
|
1313 |
deepImage.getWidth(), deepImage.getHeight(), |
|
8352
956b231fed62
7006865: Regression: Corrupted output when printing images with bit depth of 4
prr
parents:
5506
diff
changeset
|
1314 |
bitsPerPixel, icm); |
2 | 1315 |
|
1316 |
setClip(holdClip); |
|
1317 |
} |
|
1318 |
} |
|
1319 |
} |
|
1320 |
||
1321 |
return true; |
|
1322 |
} |
|
1323 |
||
1324 |
/** |
|
1325 |
* Have the printing application redraw everything that falls |
|
1326 |
* within the page bounds defined by <code>region</code>. |
|
1327 |
*/ |
|
1328 |
public void redrawRegion(Rectangle2D region, double scaleX, double scaleY, |
|
1329 |
Shape savedClip, AffineTransform savedTransform) |
|
1330 |
throws PrinterException { |
|
1331 |
||
1332 |
WPrinterJob wPrinterJob = (WPrinterJob)getPrinterJob(); |
|
1333 |
Printable painter = getPrintable(); |
|
1334 |
PageFormat pageFormat = getPageFormat(); |
|
1335 |
int pageIndex = getPageIndex(); |
|
1336 |
||
1337 |
/* Create a buffered image big enough to hold the portion |
|
1338 |
* of the source image being printed. |
|
1339 |
*/ |
|
1340 |
BufferedImage deepImage = new BufferedImage( |
|
1341 |
(int) region.getWidth(), |
|
1342 |
(int) region.getHeight(), |
|
1343 |
BufferedImage.TYPE_3BYTE_BGR); |
|
1344 |
||
1345 |
/* Get a graphics for the application to render into. |
|
1346 |
* We initialize the buffer to white in order to |
|
1347 |
* match the paper and then we shift the BufferedImage |
|
1348 |
* so that it covers the area on the page where the |
|
1349 |
* caller's Image will be drawn. |
|
1350 |
*/ |
|
1351 |
Graphics2D g = deepImage.createGraphics(); |
|
1352 |
ProxyGraphics2D proxy = new ProxyGraphics2D(g, wPrinterJob); |
|
1353 |
proxy.setColor(Color.white); |
|
1354 |
proxy.fillRect(0, 0, deepImage.getWidth(), deepImage.getHeight()); |
|
1355 |
proxy.clipRect(0, 0, deepImage.getWidth(), deepImage.getHeight()); |
|
1356 |
||
1357 |
proxy.translate(-region.getX(), -region.getY()); |
|
1358 |
||
1359 |
/* Calculate the resolution of the source image. |
|
1360 |
*/ |
|
1361 |
float sourceResX = (float)(wPrinterJob.getXRes() / scaleX); |
|
1362 |
float sourceResY = (float)(wPrinterJob.getYRes() / scaleY); |
|
1363 |
||
1364 |
/* The application expects to see user space at 72 dpi. |
|
1365 |
* so change user space from image source resolution to |
|
1366 |
* 72 dpi. |
|
1367 |
*/ |
|
1368 |
proxy.scale(sourceResX / DEFAULT_USER_RES, |
|
1369 |
sourceResY / DEFAULT_USER_RES); |
|
1370 |
||
1371 |
proxy.translate( |
|
1372 |
-wPrinterJob.getPhysicalPrintableX(pageFormat.getPaper()) |
|
1373 |
/ wPrinterJob.getXRes() * DEFAULT_USER_RES, |
|
1374 |
-wPrinterJob.getPhysicalPrintableY(pageFormat.getPaper()) |
|
1375 |
/ wPrinterJob.getYRes() * DEFAULT_USER_RES); |
|
1376 |
/* NB User space now has to be at 72 dpi for this calc to be correct */ |
|
1377 |
proxy.transform(new AffineTransform(getPageFormat().getMatrix())); |
|
1378 |
proxy.setPaint(Color.black); |
|
1379 |
||
1380 |
painter.print(proxy, pageFormat, pageIndex); |
|
1381 |
||
1382 |
g.dispose(); |
|
1383 |
||
1384 |
/* We need to set the device clip using saved information. |
|
1385 |
* savedClip intersects the user clip with a clip that restricts |
|
1386 |
* the GDI rendered area of our BufferedImage to that which |
|
1387 |
* may correspond to a rotate or shear. |
|
1388 |
* The saved device transform is needed as the current transform |
|
1389 |
* is not likely to be the same. |
|
1390 |
*/ |
|
1391 |
deviceClip(savedClip.getPathIterator(savedTransform)); |
|
1392 |
||
1393 |
/* Scale the bounding rectangle by the scale transform. |
|
1394 |
* Because the scaling transform has only x and y |
|
1395 |
* scaling components it is equivalent to multiplying |
|
1396 |
* the x components of the bounding rectangle by |
|
1397 |
* the x scaling factor and to multiplying the y components |
|
1398 |
* by the y scaling factor. |
|
1399 |
*/ |
|
1400 |
Rectangle2D.Float scaledBounds |
|
1401 |
= new Rectangle2D.Float( |
|
1402 |
(float) (region.getX() * scaleX), |
|
1403 |
(float) (region.getY() * scaleY), |
|
1404 |
(float) (region.getWidth() * scaleX), |
|
1405 |
(float) (region.getHeight() * scaleY)); |
|
1406 |
||
1407 |
/* Pull the raster data from the buffered image |
|
1408 |
* and pass it along to GDI. |
|
1409 |
*/ |
|
1410 |
ByteComponentRaster tile |
|
1411 |
= (ByteComponentRaster)deepImage.getRaster(); |
|
1412 |
||
1413 |
wPrinterJob.drawImage3ByteBGR(tile.getDataStorage(), |
|
1414 |
scaledBounds.x, scaledBounds.y, |
|
1415 |
scaledBounds.width, |
|
1416 |
scaledBounds.height, |
|
1417 |
0f, 0f, |
|
1418 |
deepImage.getWidth(), deepImage.getHeight()); |
|
1419 |
||
1420 |
} |
|
1421 |
||
1422 |
/* |
|
1423 |
* Fill the path defined by <code>pathIter</code> |
|
1424 |
* with the specified color. |
|
1425 |
* The path is provided in device coordinates. |
|
1426 |
*/ |
|
1427 |
protected void deviceFill(PathIterator pathIter, Color color) { |
|
1428 |
||
1429 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
1430 |
||
1431 |
convertToWPath(pathIter); |
|
1432 |
wPrinterJob.selectSolidBrush(color); |
|
1433 |
wPrinterJob.fillPath(); |
|
1434 |
} |
|
1435 |
||
1436 |
/* |
|
1437 |
* Set the printer device's clip to be the |
|
1438 |
* path defined by <code>pathIter</code> |
|
1439 |
* The path is provided in device coordinates. |
|
1440 |
*/ |
|
1441 |
protected void deviceClip(PathIterator pathIter) { |
|
1442 |
||
1443 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
1444 |
||
1445 |
convertToWPath(pathIter); |
|
1446 |
wPrinterJob.selectClipPath(); |
|
1447 |
} |
|
1448 |
||
1449 |
/** |
|
1450 |
* Draw the bounding rectangle using transformed coordinates. |
|
1451 |
*/ |
|
1452 |
protected void deviceFrameRect(int x, int y, int width, int height, |
|
1453 |
Color color) { |
|
1454 |
||
1455 |
AffineTransform deviceTransform = getTransform(); |
|
1456 |
||
1457 |
/* check if rotated or sheared */ |
|
1458 |
int transformType = deviceTransform.getType(); |
|
1459 |
boolean usePath = ((transformType & |
|
1460 |
(AffineTransform.TYPE_GENERAL_ROTATION | |
|
1461 |
AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0); |
|
1462 |
||
1463 |
if (usePath) { |
|
1464 |
draw(new Rectangle2D.Float(x, y, width, height)); |
|
1465 |
return; |
|
1466 |
} |
|
1467 |
||
1468 |
Stroke stroke = getStroke(); |
|
1469 |
||
1470 |
if (stroke instanceof BasicStroke) { |
|
1471 |
BasicStroke lineStroke = (BasicStroke) stroke; |
|
1472 |
||
1473 |
int endCap = lineStroke.getEndCap(); |
|
1474 |
int lineJoin = lineStroke.getLineJoin(); |
|
1475 |
||
1476 |
||
1477 |
/* check for default style and try to optimize it by |
|
1478 |
* calling the frameRect native function instead of using paths. |
|
1479 |
*/ |
|
1480 |
if ((endCap == BasicStroke.CAP_SQUARE) && |
|
1481 |
(lineJoin == BasicStroke.JOIN_MITER) && |
|
1482 |
(lineStroke.getMiterLimit() ==10.0f)) { |
|
1483 |
||
1484 |
float lineWidth = lineStroke.getLineWidth(); |
|
1485 |
Point2D.Float penSize = new Point2D.Float(lineWidth, |
|
1486 |
lineWidth); |
|
1487 |
||
1488 |
deviceTransform.deltaTransform(penSize, penSize); |
|
1489 |
float deviceLineWidth = Math.min(Math.abs(penSize.x), |
|
1490 |
Math.abs(penSize.y)); |
|
1491 |
||
1492 |
/* transform upper left coordinate */ |
|
1493 |
Point2D.Float ul_pos = new Point2D.Float(x, y); |
|
1494 |
deviceTransform.transform(ul_pos, ul_pos); |
|
1495 |
||
1496 |
/* transform lower right coordinate */ |
|
1497 |
Point2D.Float lr_pos = new Point2D.Float(x + width, |
|
1498 |
y + height); |
|
1499 |
deviceTransform.transform(lr_pos, lr_pos); |
|
1500 |
||
1501 |
float w = (float) (lr_pos.getX() - ul_pos.getX()); |
|
1502 |
float h = (float)(lr_pos.getY() - ul_pos.getY()); |
|
1503 |
||
1504 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
1505 |
||
1506 |
/* use selectStylePen, if supported */ |
|
1507 |
if (wPrinterJob.selectStylePen(endCap, lineJoin, |
|
1508 |
deviceLineWidth, color) == true) { |
|
1509 |
wPrinterJob.frameRect((float)ul_pos.getX(), |
|
1510 |
(float)ul_pos.getY(), w, h); |
|
1511 |
} |
|
1512 |
/* not supported, must be a Win 9x */ |
|
1513 |
else { |
|
1514 |
||
1515 |
double lowerRes = Math.min(wPrinterJob.getXRes(), |
|
1516 |
wPrinterJob.getYRes()); |
|
1517 |
||
1518 |
if ((deviceLineWidth/lowerRes) < MAX_THINLINE_INCHES) { |
|
1519 |
/* use the default pen styles for thin pens. */ |
|
1520 |
wPrinterJob.selectPen(deviceLineWidth, color); |
|
1521 |
wPrinterJob.frameRect((float)ul_pos.getX(), |
|
1522 |
(float)ul_pos.getY(), w, h); |
|
1523 |
} |
|
1524 |
else { |
|
1525 |
draw(new Rectangle2D.Float(x, y, width, height)); |
|
1526 |
} |
|
1527 |
} |
|
1528 |
} |
|
1529 |
else { |
|
1530 |
draw(new Rectangle2D.Float(x, y, width, height)); |
|
1531 |
} |
|
1532 |
} |
|
1533 |
} |
|
1534 |
||
1535 |
||
1536 |
/* |
|
1537 |
* Fill the rectangle with specified color and using Windows' |
|
1538 |
* GDI fillRect function. |
|
1539 |
* Boundaries are determined by the given coordinates. |
|
1540 |
*/ |
|
1541 |
protected void deviceFillRect(int x, int y, int width, int height, |
|
1542 |
Color color) { |
|
1543 |
/* |
|
1544 |
* Transform to device coordinates |
|
1545 |
*/ |
|
1546 |
AffineTransform deviceTransform = getTransform(); |
|
1547 |
||
1548 |
/* check if rotated or sheared */ |
|
1549 |
int transformType = deviceTransform.getType(); |
|
1550 |
boolean usePath = ((transformType & |
|
1551 |
(AffineTransform.TYPE_GENERAL_ROTATION | |
|
1552 |
AffineTransform.TYPE_GENERAL_TRANSFORM)) != 0); |
|
1553 |
if (usePath) { |
|
1554 |
fill(new Rectangle2D.Float(x, y, width, height)); |
|
1555 |
return; |
|
1556 |
} |
|
1557 |
||
1558 |
Point2D.Float tlc_pos = new Point2D.Float(x, y); |
|
1559 |
deviceTransform.transform(tlc_pos, tlc_pos); |
|
1560 |
||
1561 |
Point2D.Float brc_pos = new Point2D.Float(x+width, y+height); |
|
1562 |
deviceTransform.transform(brc_pos, brc_pos); |
|
1563 |
||
1564 |
float deviceWidth = (float) (brc_pos.getX() - tlc_pos.getX()); |
|
1565 |
float deviceHeight = (float)(brc_pos.getY() - tlc_pos.getY()); |
|
1566 |
||
1567 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
1568 |
wPrinterJob.fillRect((float)tlc_pos.getX(), (float)tlc_pos.getY(), |
|
1569 |
deviceWidth, deviceHeight, color); |
|
1570 |
} |
|
1571 |
||
1572 |
||
1573 |
/** |
|
1574 |
* Draw a line using a pen created using the specified color |
|
1575 |
* and current stroke properties. |
|
1576 |
*/ |
|
1577 |
protected void deviceDrawLine(int xBegin, int yBegin, int xEnd, int yEnd, |
|
1578 |
Color color) { |
|
1579 |
Stroke stroke = getStroke(); |
|
1580 |
||
1581 |
if (stroke instanceof BasicStroke) { |
|
1582 |
BasicStroke lineStroke = (BasicStroke) stroke; |
|
1583 |
||
1584 |
if (lineStroke.getDashArray() != null) { |
|
1585 |
draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd)); |
|
1586 |
return; |
|
1587 |
} |
|
1588 |
||
1589 |
float lineWidth = lineStroke.getLineWidth(); |
|
1590 |
Point2D.Float penSize = new Point2D.Float(lineWidth, lineWidth); |
|
1591 |
||
1592 |
AffineTransform deviceTransform = getTransform(); |
|
1593 |
deviceTransform.deltaTransform(penSize, penSize); |
|
1594 |
||
1595 |
float deviceLineWidth = Math.min(Math.abs(penSize.x), |
|
1596 |
Math.abs(penSize.y)); |
|
1597 |
||
1598 |
Point2D.Float begin_pos = new Point2D.Float(xBegin, yBegin); |
|
1599 |
deviceTransform.transform(begin_pos, begin_pos); |
|
1600 |
||
1601 |
Point2D.Float end_pos = new Point2D.Float(xEnd, yEnd); |
|
1602 |
deviceTransform.transform(end_pos, end_pos); |
|
1603 |
||
1604 |
int endCap = lineStroke.getEndCap(); |
|
1605 |
int lineJoin = lineStroke.getLineJoin(); |
|
1606 |
||
1607 |
/* check if it's a one-pixel line */ |
|
1608 |
if ((end_pos.getX() == begin_pos.getX()) |
|
1609 |
&& (end_pos.getY() == begin_pos.getY())) { |
|
1610 |
||
1611 |
/* endCap other than Round will not print! |
|
1612 |
* due to Windows GDI limitation, force it to CAP_ROUND |
|
1613 |
*/ |
|
1614 |
endCap = BasicStroke.CAP_ROUND; |
|
1615 |
} |
|
1616 |
||
1617 |
||
1618 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
1619 |
||
1620 |
/* call native function that creates pen with style */ |
|
1621 |
if (wPrinterJob.selectStylePen(endCap, lineJoin, |
|
1622 |
deviceLineWidth, color)) { |
|
1623 |
wPrinterJob.moveTo((float)begin_pos.getX(), |
|
1624 |
(float)begin_pos.getY()); |
|
1625 |
wPrinterJob.lineTo((float)end_pos.getX(), |
|
1626 |
(float)end_pos.getY()); |
|
1627 |
} |
|
1628 |
/* selectStylePen is not supported, must be Win 9X */ |
|
1629 |
else { |
|
1630 |
||
1631 |
/* let's see if we can use a a default pen |
|
1632 |
* if it's round end (Windows' default style) |
|
1633 |
* or it's vertical/horizontal |
|
1634 |
* or stroke is too thin. |
|
1635 |
*/ |
|
1636 |
double lowerRes = Math.min(wPrinterJob.getXRes(), |
|
1637 |
wPrinterJob.getYRes()); |
|
1638 |
||
1639 |
if ((endCap == BasicStroke.CAP_ROUND) || |
|
1640 |
(((xBegin == xEnd) || (yBegin == yEnd)) && |
|
1641 |
(deviceLineWidth/lowerRes < MAX_THINLINE_INCHES))) { |
|
1642 |
||
1643 |
wPrinterJob.selectPen(deviceLineWidth, color); |
|
1644 |
wPrinterJob.moveTo((float)begin_pos.getX(), |
|
1645 |
(float)begin_pos.getY()); |
|
1646 |
wPrinterJob.lineTo((float)end_pos.getX(), |
|
1647 |
(float)end_pos.getY()); |
|
1648 |
} |
|
1649 |
else { |
|
1650 |
draw(new Line2D.Float(xBegin, yBegin, xEnd, yEnd)); |
|
1651 |
} |
|
1652 |
} |
|
1653 |
} |
|
1654 |
} |
|
1655 |
||
1656 |
||
1657 |
/** |
|
1658 |
* Given a Java2D <code>PathIterator</code> instance, |
|
1659 |
* this method translates that into a Window's path |
|
1660 |
* in the printer device context. |
|
1661 |
*/ |
|
1662 |
private void convertToWPath(PathIterator pathIter) { |
|
1663 |
||
1664 |
float[] segment = new float[6]; |
|
1665 |
int segmentType; |
|
1666 |
||
1667 |
WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob(); |
|
1668 |
||
1669 |
/* Map the PathIterator's fill rule into the Window's |
|
1670 |
* polygon fill rule. |
|
1671 |
*/ |
|
1672 |
int polyFillRule; |
|
1673 |
if (pathIter.getWindingRule() == PathIterator.WIND_EVEN_ODD) { |
|
1674 |
polyFillRule = WPrinterJob.POLYFILL_ALTERNATE; |
|
1675 |
} else { |
|
1676 |
polyFillRule = WPrinterJob.POLYFILL_WINDING; |
|
1677 |
} |
|
1678 |
wPrinterJob.setPolyFillMode(polyFillRule); |
|
1679 |
||
1680 |
wPrinterJob.beginPath(); |
|
1681 |
||
1682 |
while (pathIter.isDone() == false) { |
|
1683 |
segmentType = pathIter.currentSegment(segment); |
|
1684 |
||
1685 |
switch (segmentType) { |
|
1686 |
case PathIterator.SEG_MOVETO: |
|
1687 |
wPrinterJob.moveTo(segment[0], segment[1]); |
|
1688 |
break; |
|
1689 |
||
1690 |
case PathIterator.SEG_LINETO: |
|
1691 |
wPrinterJob.lineTo(segment[0], segment[1]); |
|
1692 |
break; |
|
1693 |
||
1694 |
/* Convert the quad path to a bezier. |
|
1695 |
*/ |
|
1696 |
case PathIterator.SEG_QUADTO: |
|
1697 |
int lastX = wPrinterJob.getPenX(); |
|
1698 |
int lastY = wPrinterJob.getPenY(); |
|
1699 |
float c1x = lastX + (segment[0] - lastX) * 2 / 3; |
|
1700 |
float c1y = lastY + (segment[1] - lastY) * 2 / 3; |
|
1701 |
float c2x = segment[2] - (segment[2] - segment[0]) * 2/ 3; |
|
1702 |
float c2y = segment[3] - (segment[3] - segment[1]) * 2/ 3; |
|
1703 |
wPrinterJob.polyBezierTo(c1x, c1y, |
|
1704 |
c2x, c2y, |
|
1705 |
segment[2], segment[3]); |
|
1706 |
break; |
|
1707 |
||
1708 |
case PathIterator.SEG_CUBICTO: |
|
1709 |
wPrinterJob.polyBezierTo(segment[0], segment[1], |
|
1710 |
segment[2], segment[3], |
|
1711 |
segment[4], segment[5]); |
|
1712 |
break; |
|
1713 |
||
1714 |
case PathIterator.SEG_CLOSE: |
|
1715 |
wPrinterJob.closeFigure(); |
|
1716 |
break; |
|
1717 |
} |
|
1718 |
||
1719 |
||
1720 |
pathIter.next(); |
|
1721 |
} |
|
1722 |
||
1723 |
wPrinterJob.endPath(); |
|
1724 |
||
1725 |
} |
|
1726 |
||
1727 |
} |