rich copy: make text pasted in target application have same size as original text in IDEA editor

This commit is contained in:
Dmitry Batrak
2015-01-14 15:20:02 +03:00
parent 3ad5a6101e
commit 0015f09cc1
5 changed files with 23 additions and 11 deletions
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
import com.intellij.codeInsight.editorActions.CopyPastePostProcessor;
import com.intellij.codeInsight.editorActions.CopyPastePreProcessor;
import com.intellij.ide.highlighter.HighlighterFactory;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
@@ -45,6 +46,7 @@ import com.intellij.openapi.editor.richcopy.view.RtfTransferableData;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.psi.PsiFile;
import com.intellij.psi.TokenType;
@@ -271,7 +273,17 @@ public class TextWithMarkupProcessor extends CopyPastePostProcessor<RawTextWithM
myText = charSequence;
myDefaultForeground = scheme.getDefaultForeground();
myDefaultBackground = scheme.getDefaultBackground();
builder = new SyntaxInfo.Builder(myDefaultForeground, myDefaultBackground, scheme.getEditorFontSize());
// Java assumes screen resolution of 72dpi when calculating font size in pixels. External applications are supposedly using correct
// resolution, so we need to adjust font size for copied text to look the same in them.
// (See https://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-Desktop/html/java2d.html#gdlwn)
// Java on Mac is not affected by this issue.
int javaFontSize = scheme.getEditorFontSize();
float fontSize = SystemInfo.isMac || ApplicationManager.getApplication().isHeadlessEnvironment() ?
javaFontSize :
javaFontSize * 72f / Toolkit.getDefaultToolkit().getScreenResolution();
builder = new SyntaxInfo.Builder(myDefaultForeground, myDefaultBackground, fontSize);
myIndentSymbolsToStrip = indentSymbolsToStrip;
}
@@ -34,13 +34,13 @@ public class SyntaxInfo {
private final int myDefaultForeground;
private final int myDefaultBackground;
private final int myFontSize;
private final float myFontSize;
private SyntaxInfo(int outputInfoCount,
byte[] outputInfosSerialized,
int defaultForeground,
int defaultBackground,
int fontSize,
float fontSize,
@NotNull FontNameRegistry fontNameRegistry,
@NotNull ColorRegistry colorRegistry)
{
@@ -71,7 +71,7 @@ public class SyntaxInfo {
return myDefaultBackground;
}
public int getFontSize() {
public float getFontSize() {
return myFontSize;
}
@@ -146,12 +146,12 @@ public class SyntaxInfo {
private final FontNameRegistry myFontNameRegistry = new FontNameRegistry();
private final int myDefaultForeground;
private final int myDefaultBackground;
private final int myFontSize;
private final float myFontSize;
private final ByteArrayOutputStream myStream = new ByteArrayOutputStream();
private final OutputInfoSerializer.OutputStream myOutputInfoStream;
private int myOutputInfoCount;
public Builder(Color defaultForeground, Color defaultBackground, int fontSize) {
public Builder(Color defaultForeground, Color defaultBackground, float fontSize) {
myDefaultForeground = myColorRegistry.getId(defaultForeground);
myDefaultBackground = myColorRegistry.getId(defaultBackground);
myFontSize = fontSize;
@@ -83,7 +83,7 @@ public class HtmlTransferableData extends AbstractSyntaxAwareReaderTransferableD
else {
myFontFamily = myDefaultFontFamily = -1;
}
myResultBuffer.append("font-size:").append(mySyntaxInfo.getFontSize()).append("pt;\">");
myResultBuffer.append(String.format("font-size:%.1fpt;\">", mySyntaxInfo.getFontSize()));
mySyntaxInfo.processOutputInfo(this);
@@ -77,8 +77,8 @@ public class RtfTransferableData extends AbstractSyntaxAwareInputStreamTransfera
return "US-ASCII";
}
private static void addFontSize(StringBuilder buffer, int fontSize) {
buffer.append("\\fs").append(fontSize * 2);
private static void addFontSize(StringBuilder buffer, float fontSize) {
buffer.append("\\fs").append(Math.round(fontSize * 2));
}
private static class MyVisitor implements MarkupHandler {
@@ -88,7 +88,7 @@ public class RtfTransferableData extends AbstractSyntaxAwareInputStreamTransfera
private final int myMaxLength;
private final int myDefaultBackgroundId;
private final int myFontSize;
private final float myFontSize;
private int myForegroundId = -1;
private int myFontNameId = -1;
private int myFontStyle = -1;
@@ -305,7 +305,7 @@ public class SyntaxInfoConstructionTest extends LightPlatformCodeInsightFixtureT
final ColorRegistry colorRegistry = syntaxInfo.getColorRegistry();
assertEquals(JBColor.BLACK, colorRegistry.dataById(syntaxInfo.getDefaultForeground()));
assertEquals(JBColor.WHITE, colorRegistry.dataById(syntaxInfo.getDefaultBackground()));
assertEquals(getFontSize(), syntaxInfo.getFontSize());
assertEquals((float)getFontSize(), syntaxInfo.getFontSize(), 0.01f);
syntaxInfo.processOutputInfo(new MarkupHandler() {
@Override
public void handleText(int startOffset, int endOffset) throws Exception {