Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,6 @@ public void fillGradientRectangle(int x, int y, int width, int height, boolean v
toRGB = foregroundRGB;
}
long cairo = data.cairo;
long pattern;

/*
* Here the co-ordinates passed are in points for GTK3.
Expand All @@ -1613,15 +1612,26 @@ public void fillGradientRectangle(int x, int y, int width, int height, boolean v
* to set the device scale to current scale factor
*/
long surface = Cairo.cairo_get_target(cairo);
double[] oldScaleX = new double[1], oldScaleY = new double[1];
if (surface != 0) {
Cairo.cairo_surface_get_device_scale(surface, oldScaleX, oldScaleY);
float scaleFactor = DPIUtil.getDeviceZoom() / 100f;
Cairo.cairo_surface_set_device_scale(surface, scaleFactor, scaleFactor);
}
try {
fillGradientRectangleInCairo(cairo, x, y, width, height, vertical, fromRGB, toRGB);
} finally {
// the surface outlives this call, later drawing must not inherit the scale
if (surface != 0) Cairo.cairo_surface_set_device_scale(surface, oldScaleX[0], oldScaleY[0]);
}
}

private void fillGradientRectangleInCairo(long cairo, int x, int y, int width, int height, boolean vertical, RGB fromRGB, RGB toRGB) {
if (fromRGB.equals(toRGB)) {
fillRectangle(x, y, width, height);
return;
}
long pattern;

if (vertical) {
pattern = Cairo.cairo_pattern_create_linear (0.0, 0.0, 0.0, 1.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,44 @@ public void test_fillGradientRectangleIIIIZ() {
gc.fillGradientRectangle(0, 0, 0, 0, false);
}

@Test
public void test_fillGradientRectangle_doesNotChangeLaterDrawing() {
int deviceZoom = DPIUtil.getDeviceZoom();
// the gradient scales the surface to the device zoom, which a 1:1 image does not have
DPIUtil.setDeviceZoom(200);
try {
ImageData gradientFirst = drawGradientAndRectangle(true);
ImageData rectangleFirst = drawGradientAndRectangle(false);
assertArrayEquals(rectangleFirst.data, gradientFirst.data,
"drawing after fillGradientRectangle must land where it lands before it");
} finally {
DPIUtil.setDeviceZoom(deviceZoom);
}
}

private ImageData drawGradientAndRectangle(boolean gradientFirst) {
Image image = new Image(display, 40, 40);
try {
if (gradientFirst) drawGradient(image);
GC rectangleGC = new GC(image);
rectangleGC.setBackground(display.getSystemColor(SWT.COLOR_RED));
rectangleGC.fillRectangle(10, 10, 5, 5);
rectangleGC.dispose();
Comment thread
vogella marked this conversation as resolved.
if (!gradientFirst) drawGradient(image);
return image.getImageData(100);
} finally {
image.dispose();
}
}

private void drawGradient(Image image) {
GC gradientGC = new GC(image);
gradientGC.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
gradientGC.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
gradientGC.fillGradientRectangle(0, 0, 2, 2, false);
gradientGC.dispose();
}

@Test
public void test_fillOvalIIII() {
gc.fillOval(10, 0, 20, 30);
Expand Down
Loading