IDEA-168218 'Copy as Rich Text' should use some fallback font

GitOrigin-RevId: 81f06315e0f3decdd6a868aa8e89b09203c22212
This commit is contained in:
Dmitry Batrak
2020-05-06 13:09:56 +03:00
committed by intellij-monorepo-bot
parent 75a47ba64c
commit ce64696fce
9 changed files with 34 additions and 8 deletions

View File

@@ -16,10 +16,13 @@
package com.intellij.openapi.editor.richcopy;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.colors.FontPreferences;
import com.intellij.openapi.editor.impl.FontInfo;
import com.intellij.util.ReflectionUtil;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Locale;
@@ -34,6 +37,7 @@ public class FontMapper {
private static final String[] logicalFontsToMap = {Font.DIALOG, Font.DIALOG_INPUT, Font.MONOSPACED, Font.SERIF, Font.SANS_SERIF};
private static final Map<String, String> logicalToPhysicalMapping = new HashMap<>();
private static final Map<String, Boolean> monospacedMapping = new HashMap<>();
static {
try {
@@ -69,4 +73,15 @@ public class FontMapper {
String mapped = logicalToPhysicalMapping.get(logicalFontName);
return mapped == null ? logicalFontName : mapped;
}
public static boolean isMonospaced(@NotNull String fontName) {
Boolean result = monospacedMapping.get(fontName);
if (result == null) {
FontMetrics metrics = FontInfo.getFontMetrics(new Font(fontName, Font.PLAIN, FontPreferences.DEFAULT_FONT_SIZE),
new FontRenderContext(null, false, false));
result = metrics.charWidth('l') == metrics.charWidth('W');
monospacedMapping.put(fontName, result);
}
return result;
}
}

View File

@@ -1,6 +1,7 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.editor.richcopy.view;
import com.intellij.openapi.editor.richcopy.FontMapper;
import com.intellij.openapi.editor.richcopy.model.ColorRegistry;
import com.intellij.openapi.editor.richcopy.model.FontNameRegistry;
import com.intellij.openapi.editor.richcopy.model.MarkupHandler;
@@ -93,7 +94,12 @@ public class HtmlSyntaxInfoReader extends AbstractSyntaxAwareReader implements M
}
protected void appendFontFamilyRule(@NotNull StringBuilder styleBuffer, int fontFamilyId) {
styleBuffer.append("font-family:'").append(myFontNameRegistry.dataById(fontFamilyId)).append("';");
String fontName = myFontNameRegistry.dataById(fontFamilyId);
styleBuffer.append("font-family:'").append(fontName).append('\'');
if (FontMapper.isMonospaced(fontName)) {
styleBuffer.append(",monospace");
}
styleBuffer.append(';');
}
private static void defineBold(@NotNull StringBuilder styleBuffer) {

View File

@@ -1,6 +1,7 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.editor.richcopy.view;
import com.intellij.openapi.editor.richcopy.FontMapper;
import com.intellij.openapi.editor.richcopy.model.ColorRegistry;
import com.intellij.openapi.editor.richcopy.model.FontNameRegistry;
import com.intellij.openapi.editor.richcopy.model.MarkupHandler;
@@ -46,7 +47,11 @@ public class RtfTransferableData extends AbstractSyntaxAwareInputStreamTransfera
FontNameRegistry fontNameRegistry = mySyntaxInfo.getFontNameRegistry();
for (int id : fontNameRegistry.getAllIds()) {
String fontName = fontNameRegistry.dataById(id);
holder.append(String.format("{\\f%d %s;}", id, fontName));
holder.append("{\\f").append(id);
if (FontMapper.isMonospaced(fontName)) {
holder.append("\\fmodern");
}
holder.append(' ').append(fontName).append(";}");
}
holder.append("}\n");

View File

@@ -1 +1 @@
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><pre style="background-color:#ffffff;color:#000000;font-family:'___PLATFORM_SPECIFIC___';font-size:___PLATFORM_SPECIFIC___pt;">&#32;<span style="color:#000080;font-weight:bold;">int&#32;</span>field;<br><br>&#32;<span style="color:#000080;font-weight:bold;">public&#32;static&#32;void&#32;</span>main(String[]&#32;args)&#32;{</pre></body></html>
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><pre style="background-color:#ffffff;color:#000000;font-family:'___PLATFORM_SPECIFIC___',monospace;font-size:___PLATFORM_SPECIFIC___pt;">&#32;<span style="color:#000080;font-weight:bold;">int&#32;</span>field;<br><br>&#32;<span style="color:#000080;font-weight:bold;">public&#32;static&#32;void&#32;</span>main(String[]&#32;args)&#32;{</pre></body></html>

View File

@@ -1,5 +1,5 @@
{\rtf1\ansi\deff0{\colortbl;\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue128;}
{\fonttbl{\f1 ___PLATFORM_SPECIFIC___;}}
{\fonttbl{\f1\fmodern ___PLATFORM_SPECIFIC___;}}
\s0\box\cbpat2\cb2\cf1\fs___PLATFORM_SPECIFIC___

View File

@@ -1,5 +1,5 @@
{\rtf1\ansi\deff0{\colortbl;\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue109;}
{\fonttbl{\f1 ___PLATFORM_SPECIFIC___;}}
{\fonttbl{\f1\fmodern ___PLATFORM_SPECIFIC___;}}
\s0\box\cbpat2\cb2\cf1\fs___PLATFORM_SPECIFIC___

View File

@@ -1 +1 @@
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><pre style="background-color:#ffffff;color:#000000;font-family:'___PLATFORM_SPECIFIC___';font-size:___PLATFORM_SPECIFIC___pt;"><span style="color:#000080;font-weight:bold;">public&#32;class&#32;</span>Basic&#32;{<br>&#32;&#32;<span style="color:#000080;font-weight:bold;">int&#32;</span>field;<br><br>&#32;&#32;<span style="color:#000080;font-weight:bold;">public&#32;static&#32;void&#32;</span>main(String[]&#32;args)&#32;{<br>&#32;&#32;&#32;&#32;System.out.println(<span style="color:#008000;font-weight:bold;">"Hello</span><span style="color:#000080;font-weight:bold;">\t</span><span style="color:#008000;font-weight:bold;">world!"</span>);<br>&#32;&#32;}<br>}<br></pre></body></html>
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><pre style="background-color:#ffffff;color:#000000;font-family:'___PLATFORM_SPECIFIC___',monospace;font-size:___PLATFORM_SPECIFIC___pt;"><span style="color:#000080;font-weight:bold;">public&#32;class&#32;</span>Basic&#32;{<br>&#32;&#32;<span style="color:#000080;font-weight:bold;">int&#32;</span>field;<br><br>&#32;&#32;<span style="color:#000080;font-weight:bold;">public&#32;static&#32;void&#32;</span>main(String[]&#32;args)&#32;{<br>&#32;&#32;&#32;&#32;System.out.println(<span style="color:#008000;font-weight:bold;">"Hello</span><span style="color:#000080;font-weight:bold;">\t</span><span style="color:#008000;font-weight:bold;">world!"</span>);<br>&#32;&#32;}<br>}<br></pre></body></html>

View File

@@ -1,5 +1,5 @@
{\rtf1\ansi\deff0{\colortbl;\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue0;}
{\fonttbl{\f1 ___PLATFORM_SPECIFIC___;}}
{\fonttbl{\f1\fmodern ___PLATFORM_SPECIFIC___;}}
\s0\box\cbpat2\cb2\cf1\fs___PLATFORM_SPECIFIC___
\cf3

View File

@@ -1,5 +1,5 @@
{\rtf1\ansi\deff0{\colortbl;\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue109;\red15\green112\blue3;}
{\fonttbl{\f1 ___PLATFORM_SPECIFIC___;}}
{\fonttbl{\f1\fmodern ___PLATFORM_SPECIFIC___;}}
\s0\box\cbpat2\cb2\cf1\fs___PLATFORM_SPECIFIC___
\cf3