Skip to content

Commit d183022

Browse files
committed
Revert "Correct GC offset calculation #788"
- This reverts commit 657fffb. - This created a regression on Mac - See #1067
1 parent 596a08c commit d183022

File tree

4 files changed

+52
-106
lines changed

4 files changed

+52
-106
lines changed

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

+17-22
Original file line numberDiff line numberDiff line change
@@ -469,28 +469,23 @@ NSAutoreleasePool checkGC (int mask) {
469469
path.setLineCapStyle(capStyle);
470470
}
471471
if ((state & DRAW_OFFSET) != 0) {
472-
boolean isAntiAliasingActive = getAntialias() != SWT.OFF;
473-
int effectiveLineWidth = data.lineWidth < 1 ? 1 : Math.round(data.lineWidth);
474-
if (isAntiAliasingActive || effectiveLineWidth % 2 == 1) {
475-
NSSize offset = new NSSize();
476-
// In case the effective line width is odd or anti-aliasing is used, add (0.5, 0.5) to coordinates in
477-
// the coordinate system in which drawings are performed.
478-
// I.e., a line starting at (0,0) will effectively start in pixel right below
479-
// that coordinate with its center at (0.5, 0.5).
480-
offset.width = offset.height = 0.5f;
481-
if (!isAntiAliasingActive) {
482-
offset.width /= effectiveLineWidth;
483-
offset.height /= effectiveLineWidth;
484-
}
485-
// The offset will be applied to the coordinate system of the GC; so transform
486-
// it from the drawing coordinate system to the coordinate system of the GC by
487-
// applying the inverse transformation as the one applied to the GC and correct
488-
// it by the line width.
489-
if (data.inverseTransform != null) {
490-
data.inverseTransform.transformSize(offset);
491-
}
492-
data.drawXOffset = Math.abs(offset.width);
493-
data.drawYOffset = Math.abs(offset.height);
472+
data.drawXOffset = data.drawYOffset = 0;
473+
NSSize size = new NSSize();
474+
size.width = size.height = 1;
475+
if (data.transform != null) {
476+
size = data.transform.transformSize(size);
477+
}
478+
double scaling = size.width;
479+
if (scaling < 0) scaling = -scaling;
480+
double strokeWidth = data.lineWidth * scaling;
481+
if (strokeWidth == 0 || ((int)strokeWidth % 2) == 1) {
482+
data.drawXOffset = 0.5f / scaling;
483+
}
484+
scaling = size.height;
485+
if (scaling < 0) scaling = -scaling;
486+
strokeWidth = data.lineWidth * scaling;
487+
if (strokeWidth == 0 || ((int)strokeWidth % 2) == 1) {
488+
data.drawYOffset = 0.5f / scaling;
494489
}
495490
}
496491
return pool;

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

+17-27
Original file line numberDiff line numberDiff line change
@@ -379,33 +379,23 @@ void checkGC (int mask) {
379379
Cairo.cairo_set_miter_limit(cairo, data.lineMiterLimit);
380380
}
381381
if ((state & DRAW_OFFSET) != 0) {
382-
boolean isAntiAliasingActive = getAntialias() != SWT.OFF;
383-
int effectiveLineWidth = data.lineWidth < 1 ? 1 : Math.round(data.lineWidth);
384-
if (isAntiAliasingActive || effectiveLineWidth % 2 == 1) {
385-
double[] offsetX = new double[]{1};
386-
double[] offsetY = new double[]{1};
387-
// In case the effective line width is odd or anti-aliasing is used, add (0.5, 0.5) to coordinates in
388-
// the coordinate system in which drawings are performed.
389-
// I.e., a line starting at (0,0) will effectively start in pixel right below
390-
// that coordinate with its center at (0.5, 0.5).
391-
offsetX[0] = offsetY[0] = 0.5f;
392-
if (!isAntiAliasingActive) {
393-
offsetX[0] /= effectiveLineWidth;
394-
offsetY[0] /= effectiveLineWidth;
395-
}
396-
// The offset will be applied to the coordinate system of the GC; so transform
397-
// it from the drawing coordinate system to the coordinate system of the GC by
398-
// applying the inverse transformation as the one applied to the GC and correct
399-
// it by the line width.
400-
double[] matrix = new double[6];
401-
Cairo.cairo_get_matrix(cairo, matrix);
402-
double[] inverseMatrix = Arrays.copyOf(matrix, 6);
403-
Cairo.cairo_matrix_invert(inverseMatrix);
404-
Cairo.cairo_set_matrix(cairo, inverseMatrix);
405-
Cairo.cairo_user_to_device_distance(cairo, offsetX, offsetY);
406-
Cairo.cairo_set_matrix(cairo, matrix);
407-
data.cairoXoffset = Math.abs(offsetX[0]);
408-
data.cairoYoffset = Math.abs(offsetY[0]);
382+
data.cairoXoffset = data.cairoYoffset = 0;
383+
double[] matrix = new double[6];
384+
Cairo.cairo_get_matrix(cairo, matrix);
385+
double[] dx = new double[]{1};
386+
double[] dy = new double[]{1};
387+
Cairo.cairo_user_to_device_distance(cairo, dx, dy);
388+
double scaling = dx[0];
389+
if (scaling < 0) scaling = -scaling;
390+
double strokeWidth = data.lineWidth * scaling;
391+
if (strokeWidth == 0 || ((int)strokeWidth % 2) == 1) {
392+
data.cairoXoffset = 0.5 / scaling;
393+
}
394+
scaling = dy[0];
395+
if (scaling < 0) scaling = -scaling;
396+
strokeWidth = data.lineWidth * scaling;
397+
if (strokeWidth == 0 || ((int)strokeWidth % 2) == 1) {
398+
data.cairoYoffset = 0.5 / scaling;
409399
}
410400
}
411401
}

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

+18-24
Original file line numberDiff line numberDiff line change
@@ -323,30 +323,24 @@ void checkGC(int mask) {
323323
data.gdipFont = gdipFont;
324324
}
325325
if ((state & DRAW_OFFSET) != 0) {
326-
boolean isAntiAliasingActive = getAntialias() != SWT.OFF;
327-
int effectiveLineWidth = data.lineWidth < 1 ? 1 : Math.round(data.lineWidth);
328-
if (isAntiAliasingActive || effectiveLineWidth % 2 == 1) {
329-
PointF offset = new PointF();
330-
// In case the effective line width is odd or anti-aliasing is used, add (0.5, 0.5) to coordinates in
331-
// the coordinate system in which drawings are performed.
332-
// I.e., a line starting at (0,0) will effectively start in pixel right below
333-
// that coordinate with its center at (0.5, 0.5).
334-
offset.X = offset.Y = 0.5f;
335-
if (!isAntiAliasingActive) {
336-
offset.X /= effectiveLineWidth;
337-
offset.Y /= effectiveLineWidth;
338-
}
339-
// The offset will be applied to the coordinate system of the GC; so transform
340-
// it from the drawing coordinate system to the coordinate system of the GC by
341-
// applying the inverse transformation as the one applied to the GC and correct
342-
// it by the line width.
343-
long newMatrix = Gdip.Matrix_new(1, 0, 0, 1, 0, 0);
344-
Gdip.Graphics_GetTransform(gdipGraphics, newMatrix);
345-
Gdip.Matrix_Invert(newMatrix);
346-
Gdip.Matrix_TransformVectors(newMatrix, offset, 1);
347-
Gdip.Matrix_delete(newMatrix);
348-
data.gdipXOffset = Math.abs(offset.X);
349-
data.gdipYOffset = Math.abs(offset.Y);
326+
data.gdipXOffset = data.gdipYOffset = 0;
327+
long matrix = Gdip.Matrix_new(1, 0, 0, 1, 0, 0);
328+
PointF point = new PointF();
329+
point.X = point.Y = 1;
330+
Gdip.Graphics_GetTransform(gdipGraphics, matrix);
331+
Gdip.Matrix_TransformVectors(matrix, point, 1);
332+
Gdip.Matrix_delete(matrix);
333+
float scaling = point.X;
334+
if (scaling < 0) scaling = -scaling;
335+
float penWidth = data.lineWidth * scaling;
336+
if (penWidth == 0 || (((int)penWidth) & 1) == 1) {
337+
data.gdipXOffset = 0.5f / scaling;
338+
}
339+
scaling = point.Y;
340+
if (scaling < 0) scaling = -scaling;
341+
penWidth = data.lineWidth * scaling;
342+
if (penWidth == 0 || (((int)penWidth) & 1) == 1) {
343+
data.gdipYOffset = 0.5f / scaling;
350344
}
351345
}
352346
return;

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

-33
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
package org.eclipse.swt.tests.junit;
1616

1717
import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem;
18-
import static org.hamcrest.MatcherAssert.assertThat;
19-
import static org.hamcrest.Matchers.is;
20-
import static org.hamcrest.Matchers.not;
2118
import static org.junit.Assert.assertArrayEquals;
2219
import static org.junit.Assert.assertEquals;
2320
import static org.junit.Assert.assertFalse;
@@ -38,7 +35,6 @@
3835
import org.eclipse.swt.graphics.Point;
3936
import org.eclipse.swt.graphics.RGB;
4037
import org.eclipse.swt.graphics.Rectangle;
41-
import org.eclipse.swt.graphics.Transform;
4238
import org.eclipse.swt.internal.DPIUtil;
4339
import org.eclipse.swt.widgets.Canvas;
4440
import org.eclipse.swt.widgets.Display;
@@ -737,35 +733,6 @@ public void test_bug493455_drawImageAlpha_srcPos() {
737733

738734
}
739735

740-
/**
741-
* @see <a href="https://github.com/eclipse-platform/eclipse.platform.swt/issues/788">Issue 788</a>
742-
*/
743-
@Test
744-
public void test_drawLine_noSingularitiesIn45DregreeRotation() {
745-
int imageSize = 3;
746-
int centerPixel = imageSize / 2;
747-
Image image = new Image(Display.getDefault(), imageSize, imageSize);
748-
GC gc = new GC(image);
749-
Transform rotation = new Transform(gc.getDevice());
750-
gc.getTransform(rotation);
751-
752-
try {
753-
// Rotate 45° around image center
754-
rotation.translate(centerPixel, centerPixel);
755-
rotation.rotate(45);
756-
rotation.translate(- centerPixel, - centerPixel);
757-
gc.setTransform(rotation);
758-
gc.drawLine(centerPixel, centerPixel, centerPixel + 1, centerPixel);
759-
760-
assertThat("line is not drawn with 45 degree rotation",
761-
image.getImageData().getPixel(centerPixel, centerPixel), not(is(-1)));
762-
} finally {
763-
rotation.dispose();
764-
gc.dispose();
765-
image.dispose();
766-
}
767-
}
768-
769736
/* custom */
770737
Display display;
771738
Shell shell;

0 commit comments

Comments
 (0)