From e67053b281ffb99a95fe923b1beee87d136d9257 Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Tue, 20 Sep 2016 14:46:54 +0300 Subject: [PATCH] IDEA-98074 Line spacing makes cursor oversized --- .../editor/impl/EditorComponentImpl.java | 4 ++-- .../editor/impl/view/EditorPainter.java | 18 ++++++++++-------- .../openapi/editor/impl/view/EditorView.java | 11 +++++++++-- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorComponentImpl.java b/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorComponentImpl.java index 5b5ed8ec0eda..92b861604f9d 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorComponentImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorComponentImpl.java @@ -251,8 +251,8 @@ public class EditorComponentImpl extends JTextComponent implements Scrollable, D } public void repaintEditorComponent(int x, int y, int width, int height) { - int topOverhang = myEditor.myView.getTopOverhang(); - int bottomOverhang = myEditor.myView.getBottomOverhang(); + int topOverhang = Math.max(0, myEditor.myView.getTopOverhang()); + int bottomOverhang = Math.max(0, myEditor.myView.getBottomOverhang()); repaint(x, y - topOverhang, width, height + topOverhang + bottomOverhang); } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/impl/view/EditorPainter.java b/platform/platform-impl/src/com/intellij/openapi/editor/impl/view/EditorPainter.java index 9eac8685e262..61ba33408d26 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/impl/view/EditorPainter.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/impl/view/EditorPainter.java @@ -795,7 +795,8 @@ class EditorPainter implements TextDrawingCallback { if (locations == null) return; Graphics2D g = IdeBackgroundUtil.getOriginalGraphics(g_); - int lineHeight = myView.getLineHeight(); + int nominalLineHeight = myView.getNominalLineHeight(); + int topOverhang = myView.getTopOverhang(); EditorSettings settings = myEditor.getSettings(); Color caretColor = myEditor.getColorsScheme().getColor(EditorColors.CARET_COLOR); if (caretColor == null) caretColor = new JBColor(CARET_DARK, CARET_LIGHT); @@ -803,7 +804,7 @@ class EditorPainter implements TextDrawingCallback { for (EditorImpl.CaretRectangle location : locations) { g.setColor(caretColor); int x = location.myPoint.x; - int y = location.myPoint.y; + int y = location.myPoint.y - topOverhang; Caret caret = location.myCaret; boolean isRtl = location.myIsRtl; if (myEditor.isInsertMode() != settings.isBlockCursor()) { @@ -812,7 +813,7 @@ class EditorPainter implements TextDrawingCallback { if (!ImmediatePainter.isZeroLatencyTypingEnabled()) { if (x > minX && lineWidth > 1) x--; // fully cover extra character's pixel which can appear due to antialiasing } - g.fillRect(x, y, lineWidth, lineHeight); + g.fillRect(x, y, lineWidth, nominalLineHeight); if (myDocument.getTextLength() > 0 && caret != null && !myView.getTextLayoutCache().getLineLayout(caret.getLogicalPosition().line).isLtr()) { g.fillPolygon(new int[]{ @@ -826,7 +827,7 @@ class EditorPainter implements TextDrawingCallback { else { int width = location.myWidth; int startX = Math.max(minX, isRtl ? x - width : x); - g.fillRect(startX, y, width, lineHeight - 1); + g.fillRect(startX, y, width, nominalLineHeight - 1); if (myDocument.getTextLength() > 0 && caret != null) { int targetVisualColumn = caret.getVisualPosition().column; for (VisualLineFragmentsIterator.Fragment fragment : VisualLineFragmentsIterator.create(myView, @@ -839,7 +840,7 @@ class EditorPainter implements TextDrawingCallback { startVisualColumn == targetVisualColumn && !isRtl || endVisualColumn == targetVisualColumn && isRtl) { g.setColor(ColorUtil.isDark(caretColor) ? CARET_LIGHT : CARET_DARK); - fragment.draw(g, startX, y + myView.getAscent(), + fragment.draw(g, startX, y + topOverhang + myView.getAscent(), targetVisualColumn - startVisualColumn - (isRtl ? 1 : 0), targetVisualColumn - startVisualColumn + (isRtl ? 0 : 1)); break; @@ -854,12 +855,13 @@ class EditorPainter implements TextDrawingCallback { void repaintCarets() { EditorImpl.CaretRectangle[] locations = myEditor.getCaretLocations(false); if (locations == null) return; - int lineHeight = myView.getLineHeight(); + int nominalLineHeight = myView.getNominalLineHeight(); + int topOverhang = myView.getTopOverhang(); for (EditorImpl.CaretRectangle location : locations) { int x = location.myPoint.x; - int y = location.myPoint.y; + int y = location.myPoint.y - topOverhang; int width = Math.max(location.myWidth, CARET_DIRECTION_MARK_SIZE); - myEditor.getContentComponent().repaintEditorComponent(x - width, y, width * 2, lineHeight); + myEditor.getContentComponent().repaintEditorComponent(x - width, y, width * 2, nominalLineHeight); } } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/impl/view/EditorView.java b/platform/platform-impl/src/com/intellij/openapi/editor/impl/view/EditorView.java index b74ae87d75a5..ba3044d15b58 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/impl/view/EditorView.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/impl/view/EditorView.java @@ -383,6 +383,13 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable { } } + public int getNominalLineHeight() { + synchronized (myLock) { + initMetricsIfNeeded(); + return myLineHeight + myTopOverhang + myBottomOverhang; + } + } + public int getLineHeight() { synchronized (myLock) { initMetricsIfNeeded(); @@ -455,8 +462,8 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable { int ascent = FontLayoutService.getInstance().getAscent(fm); myAscent = (int)Math.ceil(ascent * verticalScalingFactor); - myTopOverhang = Math.max(ascent - myAscent, 0); - myBottomOverhang = Math.max(fontMetricsHeight - ascent - myLineHeight + myAscent, 0); + myTopOverhang = ascent - myAscent; + myBottomOverhang = fontMetricsHeight - ascent - myLineHeight + myAscent; // assuming that bold italic 'W' gives a good approximation of font's widest character FontMetrics fmBI = myEditor.getContentComponent().getFontMetrics(myEditor.getColorsScheme().getFont(EditorFontType.BOLD_ITALIC));