Skip to content

Commit 7d0ec2a

Browse files
HeikoKlarefedejeanne
authored andcommitted
[Win32] Fix wrong line width in GC upon recreation for different zoom
The Win32 implementation of the GC class stores executed operations to allow the recreation of the underlying drawable's handle for a different zoom if requested. The setLineAttributes() operation stores the passed line attributes including the line width. Once this operation is applied, the line width is transformed from point into pixel coordinates. Instead of just using the pixel value during operation execution, the actual attribute value (in points) of the operation is overwritten, such that subsequent applications of that operation will use a wrong value. This change adapts the implementation of the SetLineAttributes operation to not overwrite the original line attributes. In addition, it extracts the point-to-pixel conversion for dashes that was erroneously placed inside the called setLineAttributesInPixels method so that a proper line attributes object transformed into pixel values is passed to that method.
1 parent 716e80e commit 7d0ec2a

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/GC.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5361,8 +5361,15 @@ private class SetLineAttributesOperation extends ReplaceableOperation {
53615361

53625362
@Override
53635363
void apply() {
5364-
attributes.width = Win32DPIUtils.pointToPixel(drawable, attributes.width, getZoom());
5365-
setLineAttributesInPixels(attributes);
5364+
setLineAttributesInPixels(convertToPixels(attributes));
5365+
}
5366+
5367+
private LineAttributes convertToPixels(LineAttributes attributes) {
5368+
int zoom = getZoom();
5369+
float[] dashInPixels = Win32DPIUtils.pointToPixel(drawable, attributes.dash, zoom);
5370+
float widthInPixels = Win32DPIUtils.pointToPixel(drawable, attributes.width, zoom);
5371+
LineAttributes lineAttributesInPixels = new LineAttributes(widthInPixels, attributes.cap, attributes.join, attributes.style, dashInPixels, attributes.dashOffset, attributes.miterLimit);
5372+
return lineAttributesInPixels;
53665373
}
53675374
}
53685375

@@ -5424,12 +5431,6 @@ private void setLineAttributesInPixels (LineAttributes attributes) {
54245431
if (!changed && lineDashes[i] != dash) changed = true;
54255432
}
54265433
if (changed) {
5427-
float[] newDashes = new float[dashes.length];
5428-
int deviceZoom = getZoom();
5429-
for (int i = 0; i < newDashes.length; i++) {
5430-
newDashes[i] = Win32DPIUtils.pointToPixel(drawable, dashes[i], deviceZoom);
5431-
}
5432-
dashes = newDashes;
54335434
mask |= LINE_STYLE;
54345435
} else {
54355436
dashes = lineDashes;

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,21 @@ public static int[] pointToPixel(int[] pointArray, int zoom) {
240240
return returnArray;
241241
}
242242

243+
public static float[] pointToPixel(Drawable drawable, float values[], int zoom) {
244+
if (drawable != null && !drawable.isAutoScalable()) {
245+
return values;
246+
}
247+
if (zoom == 100 || values == null) {
248+
return values;
249+
}
250+
float scaleFactor = DPIUtil.getScalingFactor (zoom);
251+
float scaledValues[] = new float[values.length];
252+
for (int i = 0; i < scaledValues.length; i++) {
253+
scaledValues[i] = values[i] * scaleFactor;
254+
}
255+
return scaledValues;
256+
}
257+
243258
public static int[] pointToPixel(Drawable drawable, int[] pointArray, int zoom) {
244259
if (drawable != null && !drawable.isAutoScalable()) return pointArray;
245260
return pointToPixel (pointArray, zoom);

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_GC.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,17 @@ public void test_setForegroundLorg_eclipse_swt_graphics_Color() {
855855
assertEquals(new LineAttributes(width, cap, join, style, dashes, dashOffset, miterLimit), gc.getLineAttributes(), "actual line attributes differ from the ones that have been set");
856856
assertEquals(width, passedLineAttributes.width, 0.0f, "setter call changed line width");
857857

858+
// Ensure that after recreating the image handle from the GC on Windows does not overwrite any values
859+
int currentZoom = DPIUtil.getDeviceZoom();
860+
int alternateZoom = currentZoom == 100 ? 200 : 100;
861+
image.getImageData(alternateZoom);
862+
assertEquals(width, gc.getLineWidth(), "unexpected line width");
863+
assertEquals(cap, gc.getLineCap(), "unexpected line cap");
864+
assertEquals(join, gc.getLineJoin(), "unexpected line join");
865+
assertEquals(style, gc.getLineStyle(), "unexpected line style");
866+
assertEquals(new LineAttributes(width, cap, join, style, dashes, dashOffset, miterLimit), gc.getLineAttributes(), "actual line attributes differ from the ones that have been set");
867+
assertEquals(width, passedLineAttributes.width, 0.0f, "setter call changed line width");
868+
858869
gc.setLineAttributes(new LineAttributes(1));
859870
assertEquals(new LineAttributes(1), gc.getLineAttributes());
860871
}

0 commit comments

Comments
 (0)