Skip to content

Commit 551025c

Browse files
committed
Added code rtf output to workaround missing WebBrowser on Mono.
1 parent a337762 commit 551025c

File tree

8 files changed

+578
-17
lines changed

8 files changed

+578
-17
lines changed

Forms/CodeForm.Designer.cs

Lines changed: 13 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Forms/CodeForm.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.Contracts;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
47
using System.Windows.Forms;
58
using ColorCode;
9+
using ColorCode.Parsing;
610
using ReClassNET.CodeGenerator;
711
using ReClassNET.Logger;
812
using ReClassNET.Nodes;
913
using ReClassNET.UI;
14+
using ReClassNET.Util.Rtf;
1015

1116
namespace ReClassNET.Forms
1217
{
@@ -20,7 +25,20 @@ public CodeForm(ICodeGenerator generator, IEnumerable<ClassNode> classes, ILogge
2025
InitializeComponent();
2126

2227
var code = generator.GenerateCode(classes, logger);
23-
codeWebBrowser.DocumentText = new CodeColorizer().Colorize(code, generator.Language == Language.Cpp ? Languages.Cpp : Languages.CSharp);
28+
29+
var buffer = new StringBuilder(code.Length * 2);
30+
using (var writer = new StringWriter(buffer))
31+
{
32+
new CodeColorizer().Colorize(
33+
code,
34+
generator.Language == Language.Cpp ? Languages.Cpp : Languages.CSharp,
35+
new RtfFormatter(),
36+
StyleSheets.Default,
37+
writer
38+
);
39+
}
40+
41+
codeRichTextBox.Rtf = buffer.ToString();
2442
}
2543

2644
protected override void OnLoad(EventArgs e)
@@ -37,4 +55,31 @@ protected override void OnFormClosed(FormClosedEventArgs e)
3755
GlobalWindowManager.RemoveWindow(this);
3856
}
3957
}
58+
59+
class RtfFormatter : IFormatter
60+
{
61+
private readonly RtfBuilder builder = new RtfBuilder(RtfFont.Consolas, 20);
62+
63+
public void Write(string parsedSourceCode, IList<Scope> scopes, IStyleSheet styleSheet, TextWriter textWriter)
64+
{
65+
if (scopes.Any())
66+
{
67+
builder.SetForeColor(styleSheet.Styles[scopes.First().Name].Foreground).Append(parsedSourceCode);
68+
}
69+
else
70+
{
71+
builder.Append(parsedSourceCode);
72+
}
73+
}
74+
75+
public void WriteHeader(IStyleSheet styleSheet, ILanguage language, TextWriter textWriter)
76+
{
77+
78+
}
79+
80+
public void WriteFooter(IStyleSheet styleSheet, ILanguage language, TextWriter textWriter)
81+
{
82+
textWriter.Write(builder.ToString());
83+
}
84+
}
4085
}

ReClass.NET.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,11 @@
370370
<SubType>Component</SubType>
371371
</Compile>
372372
<Compile Include="UI\ViewInfo.cs" />
373+
<Compile Include="Util\Extension.Linq.cs" />
374+
<Compile Include="Util\Rtf\RtfBuilder.cs" />
375+
<Compile Include="Util\Rtf\RtfBuilder.RtfFormatLock.cs" />
376+
<Compile Include="Util\Rtf\RtfBuilder.RtfFormatWrapper.cs" />
377+
<Compile Include="Util\Rtf\RtfFont.cs" />
373378
<Compile Include="Util\Util.cs" />
374379
<Compile Include="Util\WinUtil.cs" />
375380
<EmbeddedResource Include="Forms\AboutForm.resx">

Util/Extension.Linq.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace ReClassNET.Util
4+
{
5+
static class LinqExtension
6+
{
7+
public static string Join(this IEnumerable<string> source)
8+
{
9+
return Join(source, string.Empty);
10+
}
11+
12+
public static string Join(this IEnumerable<string> source, string separator)
13+
{
14+
return string.Join(separator, source);
15+
}
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Diagnostics.Contracts;
3+
4+
namespace ReClassNET.Util.Rtf
5+
{
6+
partial class RtfBuilder
7+
{
8+
private class RtfFormatLock : IDisposable
9+
{
10+
private readonly RtfBuilder builder;
11+
private readonly RtfFormatWrapper wrapped;
12+
13+
public RtfFormatLock(RtfBuilder builder)
14+
{
15+
Contract.Requires(builder != null);
16+
17+
this.builder = builder;
18+
19+
wrapped = new RtfFormatWrapper(builder);
20+
21+
builder.isLocked = true;
22+
}
23+
24+
public void Dispose()
25+
{
26+
wrapped.Dispose();
27+
28+
builder.isLocked = false;
29+
}
30+
}
31+
}
32+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System;
2+
using System.Diagnostics.Contracts;
3+
using System.Drawing;
4+
5+
namespace ReClassNET.Util.Rtf
6+
{
7+
partial class RtfBuilder
8+
{
9+
private class RtfFormatWrapper : IDisposable
10+
{
11+
private readonly RtfBuilder builder;
12+
13+
public RtfFormatWrapper(RtfBuilder builder)
14+
{
15+
Contract.Requires(builder != null);
16+
17+
this.builder = builder;
18+
19+
if (builder.isLocked)
20+
{
21+
return;
22+
}
23+
24+
var buffer = builder.buffer;
25+
26+
int oldLength = buffer.Length;
27+
28+
if ((builder.fontStyle & FontStyle.Bold) == FontStyle.Bold)
29+
{
30+
buffer.Append(@"\b");
31+
}
32+
if ((builder.fontStyle & FontStyle.Italic) == FontStyle.Italic)
33+
{
34+
buffer.Append(@"\i");
35+
}
36+
if ((builder.fontStyle & FontStyle.Underline) == FontStyle.Underline)
37+
{
38+
buffer.Append(@"\ul");
39+
}
40+
if ((builder.fontStyle & FontStyle.Strikeout) == FontStyle.Strikeout)
41+
{
42+
buffer.Append(@"\strike");
43+
}
44+
45+
if (builder.fontSize != builder.DefaultFontSize)
46+
{
47+
buffer.AppendFormat(@"\fs{0}", builder.fontSize);
48+
}
49+
if (builder.fontIndex != 0)
50+
{
51+
buffer.AppendFormat(@"\f{0}", builder.fontIndex);
52+
}
53+
if (builder.foreColor != builder.DefaultForeColor)
54+
{
55+
buffer.AppendFormat(@"\cf{0}", builder.IndexOfColor(builder.foreColor));
56+
}
57+
if (builder.backColor != builder.DefaultBackColor)
58+
{
59+
buffer.AppendFormat(@"\highlight{0}", builder.IndexOfColor(builder.backColor));
60+
}
61+
62+
if (buffer.Length > oldLength)
63+
{
64+
buffer.Append(" ");
65+
}
66+
}
67+
68+
public void Dispose()
69+
{
70+
if (builder.isLocked)
71+
{
72+
return;
73+
}
74+
75+
var buffer = builder.buffer;
76+
77+
var oldLength = buffer.Length;
78+
79+
if ((builder.fontStyle & FontStyle.Bold) == FontStyle.Bold)
80+
{
81+
buffer.Append(@"\b0");
82+
}
83+
if ((builder.fontStyle & FontStyle.Italic) == FontStyle.Italic)
84+
{
85+
buffer.Append(@"\i0");
86+
}
87+
if ((builder.fontStyle & FontStyle.Underline) == FontStyle.Underline)
88+
{
89+
buffer.Append(@"\ulnone");
90+
}
91+
if ((builder.fontStyle & FontStyle.Strikeout) == FontStyle.Strikeout)
92+
{
93+
buffer.Append(@"\strike0");
94+
}
95+
96+
builder.fontStyle = FontStyle.Regular;
97+
98+
if (builder.fontSize != builder.DefaultFontSize)
99+
{
100+
builder.fontSize = builder.DefaultFontSize;
101+
102+
buffer.AppendFormat(@"\fs{0} ", builder.DefaultFontSize);
103+
}
104+
if (builder.fontIndex != 0)
105+
{
106+
buffer.Append(@"\f0");
107+
108+
builder.fontIndex = 0;
109+
}
110+
111+
if (builder.foreColor != builder.DefaultForeColor)
112+
{
113+
builder.foreColor = builder.DefaultForeColor;
114+
115+
buffer.Append(@"\cf0");
116+
}
117+
if (builder.backColor != builder.DefaultBackColor)
118+
{
119+
builder.backColor = builder.DefaultBackColor;
120+
121+
buffer.Append(@"\highlight0");
122+
}
123+
124+
if (buffer.Length > oldLength)
125+
{
126+
buffer.Append(" ");
127+
}
128+
}
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)