Skip to content

Commit ad2df85

Browse files
ShahzaibIbrahimfedejeanne
authored andcommitted
Artifacts due to rounding errors in StyledText
While selecting the text in code editor the styled text have some artifacts/blank lines due to rounding error while translating points to pixel. Now we are passing the x and y in drawInPixels method in points instead of pixels and expecting it to be scaled up in the method.
1 parent 3dcd207 commit ad2df85

File tree

1 file changed

+16
-13
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+16
-13
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ long createGdipBrush(Color color, int alpha) {
705705
*/
706706
public void draw (GC gc, int x, int y) {
707707
checkLayout();
708-
drawInPixels(gc, DPIUtil.scaleUp(getDevice(), x, getZoom(gc)), DPIUtil.scaleUp(getDevice(), y, getZoom(gc)));
708+
drawInPixels(gc, x, y);
709709
}
710710

711711
/**
@@ -729,11 +729,11 @@ public void draw (GC gc, int x, int y) {
729729
*/
730730
public void draw (GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) {
731731
checkLayout();
732-
drawInPixels(gc, DPIUtil.scaleUp(getDevice(), x, getZoom(gc)), DPIUtil.scaleUp(getDevice(), y, getZoom(gc)), selectionStart, selectionEnd, selectionForeground, selectionBackground);
732+
drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground);
733733
}
734734

735-
void drawInPixels (GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) {
736-
drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, 0);
735+
void drawInPixels (GC gc, int xInPoints, int yInPoints, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground) {
736+
drawInPixels(gc, xInPoints, yInPoints, selectionStart, selectionEnd, selectionForeground, selectionBackground, 0);
737737
}
738738

739739
/**
@@ -765,7 +765,7 @@ void drawInPixels (GC gc, int x, int y, int selectionStart, int selectionEnd, Co
765765
*/
766766
public void draw (GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) {
767767
checkLayout();
768-
drawInPixels(gc, DPIUtil.scaleUp(getDevice(), x, getZoom(gc)), DPIUtil.scaleUp(getDevice(), y, getZoom(gc)), selectionStart, selectionEnd, selectionForeground, selectionBackground, flags);
768+
drawInPixels(gc, x, y, selectionStart, selectionEnd, selectionForeground, selectionBackground, flags);
769769
}
770770

771771
private int getNativeZoom(GC gc) {
@@ -783,19 +783,19 @@ private int getZoom() {
783783
return DPIUtil.getZoomForAutoscaleProperty(nativeZoom);
784784
}
785785

786-
void drawInPixels (GC gc, int x, int y) {
787-
drawInPixels(gc, x, y, -1, -1, null, null);
786+
void drawInPixels (GC gc, int xInPoints, int yInPoints) {
787+
drawInPixels(gc, xInPoints, yInPoints, -1, -1, null, null);
788788
}
789789

790-
void drawInPixels (GC gc, int x, int y, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) {
790+
void drawInPixels (GC gc, int xInPoints, int yInPoints, int selectionStart, int selectionEnd, Color selectionForeground, Color selectionBackground, int flags) {
791791
computeRuns(gc);
792792
if (gc == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
793793
if (gc.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
794794
if (selectionForeground != null && selectionForeground.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
795795
if (selectionBackground != null && selectionBackground.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
796796
int length = text.length();
797797
if (length == 0 && flags == 0) return;
798-
y += getScaledVerticalIndent();
798+
yInPoints += verticalIndentInPoints;
799799
long hdc = gc.handle;
800800
Rectangle clip = gc.getClippingInPixels();
801801
GCData data = gc.data;
@@ -835,12 +835,15 @@ void drawInPixels (GC gc, int x, int y, int selectionStart, int selectionEnd, Co
835835
selectionEnd = translateOffset(Math.min(Math.max(0, selectionEnd), length - 1));
836836
}
837837
}
838+
int x = DPIUtil.scaleUp(getDevice(), xInPoints, getZoom(gc));
838839
RECT rect = new RECT();
839840
OS.SetBkMode(hdc, OS.TRANSPARENT);
840841
for (int line=0; line<runs.length; line++) {
841842
int drawX = x + getLineIndentInPixel(line);
842-
int drawY = y + DPIUtil.scaleUp(getDevice(), lineY[line], getZoom(gc));
843+
int drawY = DPIUtil.scaleUp(getDevice(), yInPoints + lineY[line], getZoom(gc));
843844
StyleItem[] lineRuns = runs[line];
845+
int drawYWithLineHeight = DPIUtil.scaleUp(getDevice(), yInPoints + lineY[line+1] - lineSpacingInPoints, getZoom(gc));
846+
int drawYWithLineHeightWithSpacing = DPIUtil.scaleUp(getDevice(), yInPoints + lineY[line+1], getZoom(gc));
844847
int lineHeight = DPIUtil.scaleUp(getDevice(), lineY[line+1] - lineY[line] - lineSpacingInPoints, getZoom(gc));
845848
int lineHeightWithSpacing = DPIUtil.scaleUp(getDevice(), lineY[line+1] - lineY[line], getZoom(gc));
846849
//Draw last line selection
@@ -870,7 +873,7 @@ void drawInPixels (GC gc, int x, int y, int selectionStart, int selectionEnd, Co
870873
Gdip.Graphics_FillRectangle(gdipGraphics, gdipSelBackground, drawX + lineWidthInPixels[line], drawY, width, lineHeightWithSpacing);
871874
} else {
872875
OS.SelectObject(hdc, selBackground);
873-
OS.PatBlt(hdc, drawX + lineWidthInPixels[line], drawY, width, lineHeightWithSpacing, OS.PATCOPY);
876+
OS.PatBlt(hdc, drawX + lineWidthInPixels[line], drawY, width, drawYWithLineHeightWithSpacing, OS.PATCOPY);
874877
}
875878
}
876879
}
@@ -885,9 +888,9 @@ void drawInPixels (GC gc, int x, int y, int selectionStart, int selectionEnd, Co
885888
if (drawX + run.width >= clip.x) {
886889
if (!run.lineBreak || run.softBreak) {
887890
if (extents) {
888-
OS.SetRect(rect, drawX, drawY, drawX + run.width, drawY + lineHeightWithSpacing);
891+
OS.SetRect(rect, drawX, drawY, drawX + run.width, drawYWithLineHeightWithSpacing);
889892
}else {
890-
OS.SetRect(rect, drawX, drawY, drawX + run.width, drawY + lineHeight);
893+
OS.SetRect(rect, drawX, drawY, drawX + run.width, drawYWithLineHeight);
891894
}
892895
if (gdip) {
893896
drawRunBackgroundGDIP(run, gdipGraphics, rect, selectionStart, selectionEnd, alpha, gdipSelBackground, hasSelection);

0 commit comments

Comments
 (0)