From ac44e446fb43a2a376339175823a3d5a61d65478 Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Fri, 20 Jan 2017 12:50:19 +0300 Subject: [PATCH] IDEA-166581 Incorrect font rendering in vcs change preview (new hidpi) --- .../openapi/vcs/ex/LineStatusMarkerPopup.java | 5 ++- .../hint/EditorFragmentComponent.java | 39 +++++++++++++++---- .../openapi/editor/impl/view/EditorView.java | 17 ++++---- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/platform/diff-impl/src/com/intellij/openapi/vcs/ex/LineStatusMarkerPopup.java b/platform/diff-impl/src/com/intellij/openapi/vcs/ex/LineStatusMarkerPopup.java index a72dfccff381..7397c8d8127b 100644 --- a/platform/diff-impl/src/com/intellij/openapi/vcs/ex/LineStatusMarkerPopup.java +++ b/platform/diff-impl/src/com/intellij/openapi/vcs/ex/LineStatusMarkerPopup.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -214,7 +214,8 @@ public abstract class LineStatusMarkerPopup { } EditorFragmentComponent fragmentComponent = - EditorFragmentComponent.createEditorFragmentComponent(uEditor, myRange.getVcsLine1(), myRange.getVcsLine2(), false, false); + EditorFragmentComponent.createEditorFragmentComponent(myEditor.getContentComponent(), uEditor, + myRange.getVcsLine1(), myRange.getVcsLine2(), false, false); EditorFactory.getInstance().releaseEditor(uEditor); diff --git a/platform/platform-impl/src/com/intellij/codeInsight/hint/EditorFragmentComponent.java b/platform/platform-impl/src/com/intellij/codeInsight/hint/EditorFragmentComponent.java index aafa36435588..e092c6651319 100644 --- a/platform/platform-impl/src/com/intellij/codeInsight/hint/EditorFragmentComponent.java +++ b/platform/platform-impl/src/com/intellij/codeInsight/hint/EditorFragmentComponent.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,17 +47,17 @@ public class EditorFragmentComponent extends JPanel { private static final int LINE_BORDER_THICKNESS = 1; private static final int EMPTY_BORDER_THICKNESS = 2; - private EditorFragmentComponent(EditorEx editor, int startLine, int endLine, boolean showFolding, boolean showGutter) { + private EditorFragmentComponent(Component component, EditorEx editor, int startLine, int endLine, boolean showFolding, boolean showGutter) { editor.setPurePaintingMode(true); try { - doInit(editor, startLine, endLine, showFolding, showGutter); + doInit(component, editor, startLine, endLine, showFolding, showGutter); } finally { editor.setPurePaintingMode(false); } } - private void doInit(EditorEx editor, int startLine, int endLine, boolean showFolding, boolean showGutter) { + private void doInit(Component anchorComponent, EditorEx editor, int startLine, int endLine, boolean showFolding, boolean showGutter) { Document doc = editor.getDocument(); final int endOffset = endLine < doc.getLineCount() ? doc.getLineEndOffset(endLine) : doc.getTextLength(); boolean newRendering = editor instanceof EditorImpl; @@ -85,7 +85,8 @@ public class EditorFragmentComponent extends JPanel { editor.getScrollingModel().scrollHorizontally(0); } - final BufferedImage textImage = UIUtil.createImage(editor.getComponent(), textImageWidth, textImageHeight, BufferedImage.TYPE_INT_RGB); + final BufferedImage textImage = UIUtil.createImage(anchorComponent == null ? editor.getContentComponent() : anchorComponent, + textImageWidth, textImageHeight, BufferedImage.TYPE_INT_RGB); Graphics textGraphics = textImage.getGraphics(); EditorUIUtil.setupAntialiasing(textGraphics); @@ -224,23 +225,47 @@ public class EditorFragmentComponent extends JPanel { return hint; } + /** + * @param component Should be provided if editor is not currently displayable. + * Makes for correct rendering on multi-monitor configurations. + */ + public static EditorFragmentComponent createEditorFragmentComponent(Component component, Editor editor, + int startLine, + int endLine, + boolean showFolding, boolean showGutter) { + return createEditorFragmentComponent(component, editor, startLine, endLine, showFolding, showGutter, true); + } + public static EditorFragmentComponent createEditorFragmentComponent(Editor editor, int startLine, int endLine, boolean showFolding, boolean showGutter) { return createEditorFragmentComponent(editor, startLine, endLine, showFolding, showGutter, true); } - + public static EditorFragmentComponent createEditorFragmentComponent(Editor editor, int startLine, int endLine, boolean showFolding, boolean showGutter, boolean useCaretRowBackground) { + return createEditorFragmentComponent(null, editor, startLine, endLine, showFolding, showGutter, useCaretRowBackground); + } + + /** + * @param component Should be provided if editor is not currently displayable. + * Makes for correct rendering on multi-monitor configurations. + */ + public static EditorFragmentComponent createEditorFragmentComponent(Component component, + Editor editor, + int startLine, + int endLine, + boolean showFolding, boolean showGutter, + boolean useCaretRowBackground) { final EditorEx editorEx = (EditorEx)editor; final Color old = editorEx.getBackgroundColor(); Color backColor = getBackgroundColor(editor, useCaretRowBackground); editorEx.setBackgroundColor(backColor); - EditorFragmentComponent fragmentComponent = new EditorFragmentComponent(editorEx, startLine, endLine, + EditorFragmentComponent fragmentComponent = new EditorFragmentComponent(component, editorEx, startLine, endLine, showFolding, showGutter); fragmentComponent.setBackground(backColor); 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 ccb3743ff628..796ff0ef2112 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 @@ -38,7 +38,6 @@ import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.TestOnly; -import javax.swing.*; import java.awt.*; import java.awt.event.HierarchyEvent; import java.awt.event.HierarchyListener; @@ -137,13 +136,13 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable, Hi @Override public void hierarchyChanged(HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && e.getComponent().isShowing()) { - checkFontRenderContext(); + checkFontRenderContext(null); } } @Override public void visibleAreaChanged(VisibleAreaEvent e) { - checkFontRenderContext(); + checkFontRenderContext(null); } public int yToVisualLine(int y) { return myMapper.yToVisualLine(y); @@ -263,6 +262,7 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable, Hi public void paint(Graphics2D g) { assertIsDispatchThread(); myEditor.getSoftWrapModel().prepareToMapping(); + checkFontRenderContext(g.getFontRenderContext()); myPainter.paint(g); } @@ -338,7 +338,7 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable, Hi default: myBidiFlags = Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT; } - setFontRenderContext(); + setFontRenderContext(null); myLogicalPositionCache.reset(false); myTextLayoutCache.resetToDocumentSize(false); invalidateFoldRegionLayouts(); @@ -517,14 +517,13 @@ public class EditorView implements TextDrawingCallback, Disposable, Dumpable, Hi } } - private void setFontRenderContext() { - JComponent component = myEditor.getContentComponent(); - myFontRenderContext = FontInfo.getFontRenderContext(component); + private void setFontRenderContext(FontRenderContext context) { + myFontRenderContext = context == null ? FontInfo.getFontRenderContext(myEditor.getContentComponent()) : context; } - private void checkFontRenderContext() { + private void checkFontRenderContext(FontRenderContext context) { FontRenderContext oldContext = myFontRenderContext; - setFontRenderContext(); + setFontRenderContext(context); if (!myFontRenderContext.equals(oldContext)) { myTextLayoutCache.resetToDocumentSize(false); }