simplify HTML markup (IDEA-67767)

This commit is contained in:
Dmitry Batrak
2014-04-24 19:00:28 +04:00
parent bcc4a00bbd
commit 622b745fe0
@@ -19,7 +19,6 @@ import com.intellij.openapi.editor.richcopy.model.ColorRegistry;
import com.intellij.openapi.editor.richcopy.model.FontNameRegistry;
import com.intellij.openapi.editor.richcopy.model.MarkupHandler;
import com.intellij.openapi.editor.richcopy.model.SyntaxInfo;
import com.intellij.util.StringBuilderSpinAllocator;
import com.intellij.util.ui.UIUtil;
import gnu.trove.TIntObjectHashMap;
import org.jetbrains.annotations.NotNull;
@@ -40,6 +39,9 @@ public class HtmlTransferableData extends AbstractSyntaxAwareReaderTransferableD
private FontNameRegistry myFontNameRegistry;
private int myMaxLength;
private int myDefaultForeground;
private int myDefaultBackground;
private int myDefaultFontFamily;
private int myForeground;
private int myBackground;
private int myFontFamily;
@@ -57,20 +59,25 @@ public class HtmlTransferableData extends AbstractSyntaxAwareReaderTransferableD
myResultBuffer = holder;
myColorRegistry = mySyntaxInfo.getColorRegistry();
myFontNameRegistry = mySyntaxInfo.getFontNameRegistry();
myDefaultForeground = myForeground = mySyntaxInfo.getDefaultForeground();
myDefaultBackground = myBackground = mySyntaxInfo.getDefaultBackground();
myMaxLength = maxLength;
try {
buildColorMap();
myResultBuffer.append("<pre style=\"background-color:");
appendColor(myResultBuffer, mySyntaxInfo.getDefaultBackground());
appendColor(myResultBuffer, myDefaultBackground);
myResultBuffer.append(";color:");
appendColor(myResultBuffer, myDefaultForeground);
myResultBuffer.append(';');
if (myFontNameRegistry.size() == 1) {
appendFontFamilyRule(myResultBuffer, myFontNameRegistry.getAllIds()[0]);
myFontNameRegistry = null;
int[] fontIds = myFontNameRegistry.getAllIds();
if (fontIds.length > 0) {
myFontFamily = myDefaultFontFamily = fontIds[0];
appendFontFamilyRule(myResultBuffer, myDefaultFontFamily);
}
appendFontSizeRule(myResultBuffer, mySyntaxInfo.getSingleFontSize());
myResultBuffer.append("\" bgcolor=\"");
appendColor(myResultBuffer, mySyntaxInfo.getDefaultBackground());
myResultBuffer.append("\">");
else {
myFontFamily = myDefaultFontFamily = -1;
}
myResultBuffer.append("font-size:").append(mySyntaxInfo.getSingleFontSize()).append("pt;\">");
mySyntaxInfo.processOutputInfo(this);
@@ -84,38 +91,30 @@ public class HtmlTransferableData extends AbstractSyntaxAwareReaderTransferableD
}
}
private void defineForeground(int id, @NotNull StringBuilder styleBuffer, @NotNull StringBuilder closeTagBuffer) {
myResultBuffer.append("<font color=\"");
appendColor(myResultBuffer, id);
myResultBuffer.append("\">");
private void appendFontFamilyRule(@NotNull StringBuilder styleBuffer, int fontFamilyId) {
styleBuffer.append("font-family:'").append(myFontNameRegistry.dataById(fontFamilyId)).append("';");
}
private static void defineBold(@NotNull StringBuilder styleBuffer) {
styleBuffer.append("font-weight:bold;");
}
private static void defineItalic(@NotNull StringBuilder styleBuffer) {
styleBuffer.append("font-style:italic;");
}
private void defineForeground(int id, @NotNull StringBuilder styleBuffer) {
styleBuffer.append("color:");
appendColor(styleBuffer, id);
styleBuffer.append(";");
closeTagBuffer.insert(0, "</font>");
}
private void defineBackground(int id, @NotNull StringBuilder styleBuffer, @NotNull StringBuilder closeTagBuffer) {
myResultBuffer.append("<font bgcolor=\"");
appendColor(myResultBuffer, id);
myResultBuffer.append("\">");
private void defineBackground(int id, @NotNull StringBuilder styleBuffer) {
styleBuffer.append("background-color:");
appendColor(styleBuffer, id);
styleBuffer.append(";");
closeTagBuffer.insert(0, "</font>");
}
private void defineBold(@NotNull StringBuilder styleBuffer, @NotNull StringBuilder closeTagBuffer) {
myResultBuffer.append("<b>");
styleBuffer.append("font-weight:bold;");
closeTagBuffer.insert(0, "</b>");
}
private void defineItalic(@NotNull StringBuilder styleBuffer, @NotNull StringBuilder closeTagBuffer) {
myResultBuffer.append("<i>");
styleBuffer.append("font-style:italic;");
closeTagBuffer.insert(0, "</i>");
}
private void appendColor(StringBuilder builder, int id) {
builder.append(myColors.get(id));
}
@@ -128,51 +127,33 @@ public class HtmlTransferableData extends AbstractSyntaxAwareReaderTransferableD
}
}
private void appendFontFamilyRule(@NotNull StringBuilder styleBuffer, int fontFamilyId) {
styleBuffer.append("font-family:'").append(myFontNameRegistry.dataById(fontFamilyId)).append("';");
}
private static void appendFontSizeRule(@NotNull StringBuilder styleBuffer, int fontSize) {
styleBuffer.append("font-size:").append(fontSize).append("pt;");
}
@Override
public void handleText(int startOffset, int endOffset) {
boolean formattedText = myForeground > 0 || myBackground > 0 || myFontFamily > 0 || myBold || myItalic;
boolean formattedText = myForeground != myDefaultForeground || myBackground != myDefaultBackground || myFontFamily != myDefaultFontFamily || myBold || myItalic;
if (!formattedText) {
escapeAndAdd(startOffset, endOffset);
return;
}
StringBuilder styleBuffer = StringBuilderSpinAllocator.alloc();
StringBuilder closeTagBuffer = StringBuilderSpinAllocator.alloc();
try {
if (myForeground > 0) {
defineForeground(myForeground, styleBuffer, closeTagBuffer);
}
if (myBackground > 0) {
defineBackground(myBackground, styleBuffer, closeTagBuffer);
}
if (myBold) {
defineBold(styleBuffer, closeTagBuffer);
}
if (myItalic) {
defineItalic(styleBuffer, closeTagBuffer);
}
if (myFontFamily > 0) {
appendFontFamilyRule(styleBuffer, myFontFamily);
}
myResultBuffer.append("<span style=\"");
myResultBuffer.append(styleBuffer);
myResultBuffer.append("\">");
escapeAndAdd(startOffset, endOffset);
myResultBuffer.append("</span>");
myResultBuffer.append(closeTagBuffer);
myResultBuffer.append("<span style=\"");
if (myForeground != myDefaultForeground) {
defineForeground(myForeground, myResultBuffer);
}
finally {
StringBuilderSpinAllocator.dispose(styleBuffer);
StringBuilderSpinAllocator.dispose(closeTagBuffer);
if (myBackground != myDefaultBackground) {
defineBackground(myBackground, myResultBuffer);
}
if (myBold) {
defineBold(myResultBuffer);
}
if (myItalic) {
defineItalic(myResultBuffer);
}
if (myFontFamily != myDefaultFontFamily) {
appendFontFamilyRule(myResultBuffer, myFontFamily);
}
myResultBuffer.append("\">");
escapeAndAdd(startOffset, endOffset);
myResultBuffer.append("</span>");
}
private void escapeAndAdd(int start, int end) {
@@ -199,9 +180,7 @@ public class HtmlTransferableData extends AbstractSyntaxAwareReaderTransferableD
@Override
public void handleFont(int fontNameId) throws Exception {
if (myFontNameRegistry != null) {
myFontFamily = fontNameId;
}
myFontFamily = fontNameId;
}
@Override