|
46 | 46 | import org.eclipse.swt.graphics.Rectangle; |
47 | 47 | import org.eclipse.swt.graphics.Transform; |
48 | 48 | import org.eclipse.swt.internal.DPIUtil; |
| 49 | +import org.eclipse.swt.layout.FormData; |
| 50 | +import org.eclipse.swt.layout.FormLayout; |
49 | 51 | import org.eclipse.swt.widgets.Canvas; |
50 | 52 | import org.eclipse.swt.widgets.Display; |
51 | 53 | import org.eclipse.swt.widgets.Shell; |
@@ -246,6 +248,60 @@ public void test_dispose() { |
246 | 248 | gc.dispose(); |
247 | 249 | } |
248 | 250 |
|
| 251 | + |
| 252 | +@Test |
| 253 | +public void test_drawImage_nonAutoScalableGC_bug_2504() { |
| 254 | + Shell shell = new Shell(display); |
| 255 | + shell.setLayout(new FormLayout()); |
| 256 | + |
| 257 | + float targetScale = 2f; |
| 258 | + int srcSize = 50; |
| 259 | + |
| 260 | + Image image = new Image(display, srcSize, srcSize); |
| 261 | + GC gcSrc = new GC(image); |
| 262 | + gcSrc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); |
| 263 | + gcSrc.fillRectangle(0, 0, srcSize, srcSize); |
| 264 | + gcSrc.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); |
| 265 | + gcSrc.fillRectangle(2, 2, srcSize - 4, srcSize - 4); |
| 266 | + gcSrc.dispose(); |
| 267 | + |
| 268 | + Rectangle bounds = image.getBounds(); |
| 269 | + |
| 270 | + Canvas canvas = new Canvas(shell, SWT.NONE) { |
| 271 | + @Override |
| 272 | + public boolean isAutoScalable() { |
| 273 | + return false; |
| 274 | + } |
| 275 | + }; |
| 276 | + FormData formData = new FormData(); |
| 277 | + formData.width = Math.round(bounds.width * targetScale); |
| 278 | + formData.height = Math.round(bounds.height * targetScale); |
| 279 | + canvas.setLayoutData(formData); |
| 280 | + canvas.addPaintListener(e -> { |
| 281 | + e.gc.drawImage(image, 0, 0, bounds.width, bounds.height, |
| 282 | + 0, 0, formData.width, formData.height); |
| 283 | + }); |
| 284 | + |
| 285 | + shell.open(); |
| 286 | + while (display.readAndDispatch()) { |
| 287 | + } |
| 288 | + Image target = new Image(display, formData.width, formData.height); |
| 289 | + GC gcCopy = new GC(canvas); |
| 290 | + gcCopy.copyArea(target, 0, 0); |
| 291 | + gcCopy.dispose(); |
| 292 | + |
| 293 | + ImageData data = target.getImageData(); |
| 294 | + |
| 295 | + int bottomRightX = formData.width - 1; |
| 296 | + int bottomRightY = formData.height - 1; |
| 297 | + RGB bottomRight = data.palette.getRGB(data.getPixel(bottomRightX, bottomRightY)); |
| 298 | + RGB black = new RGB(0, 0, 0); |
| 299 | + assertEquals( black, bottomRight, "Bottom-right pixel is not black! when source GC is not autoScalable"); |
| 300 | + shell.dispose(); |
| 301 | + target.dispose(); |
| 302 | + image.dispose(); |
| 303 | +} |
| 304 | + |
249 | 305 | @Test |
250 | 306 | public void test_drawArcIIIIII() { |
251 | 307 | gc.drawArc(10, 20, 50, 25, 90, 90); |
|
0 commit comments