This repository was archived by the owner on Nov 13, 2018. It is now read-only.
forked from ArthurHub/HTML-Renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoForm.cs
More file actions
executable file
·620 lines (531 loc) · 21.2 KB
/
Copy pathDemoForm.cs
File metadata and controls
executable file
·620 lines (531 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using LayoutFarm.ContentManagers;
using LayoutFarm.WebDom;
using Timer = System.Threading.Timer;
namespace LayoutFarm.Demo
{
public partial class DemoForm : Form
{
/// <summary>
/// Cache for resource images
/// </summary>
private readonly Dictionary<string, Image> _imageCache = new Dictionary<string, Image>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// the private font used for the demo
/// </summary>
private readonly PrivateFontCollection _privateFont = new PrivateFontCollection();
/// <summary>
/// the html samples used for performance testing
/// </summary>
private readonly List<string> _perfTestSamples = new List<string>();
/// <summary>
/// the html samples to show in the demo
/// </summary>
private readonly Dictionary<string, string> _samples = new Dictionary<string, string>();
/// <summary>
/// timer to update the rendered html when html in editor changes with delay
/// </summary>
private readonly Timer _updateHtmlTimer;
/// <summary>
/// used ignore html editor updates when updating seperatly
/// </summary>
private bool _updateLock;
PixelFarm.Drawing.GraphicsPlatform graphicsPlatform;
LayoutFarm.HtmlBoxes.HtmlHost htmlHost;
string htmlRootFolder;
/// <summary>
/// Init.
/// </summary>
public DemoForm(PixelFarm.Drawing.GraphicsPlatform p)
{
this.graphicsPlatform = p;
this._htmlPanel = new LayoutFarm.Demo.HtmlPanel(this.graphicsPlatform, 800, 600);
this.htmlHost = new LayoutFarm.HtmlBoxes.HtmlHost(p);
htmlHost.AttachEssentailHandlers(
this.HandleImageRequest,
this.HandleStylesheetRequest);
_htmlPanel.SetHtmlHost(htmlHost);
InitializeComponent();
//_htmlToolTip.ImageLoad += OnImageLoad;
//_htmlToolTip.SetToolTip(_htmlPanel, Resources.Tooltip);
_htmlEditor.Font = new Font(FontFamily.GenericMonospace, 10);
StartPosition = FormStartPosition.CenterScreen;
var size = Screen.GetWorkingArea(Point.Empty);
Size = new Size((int)(size.Width * 0.7), (int)(size.Height * 0.8));
_updateHtmlTimer = new Timer(OnUpdateHtmlTimerTick);
this.Text += " : " + Path.GetDirectoryName(Application.ExecutablePath);
this._samplesTreeView.NodeMouseClick += new TreeNodeMouseClickEventHandler(_samplesTreeView_NodeMouseClick);
}
void _samplesTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
var filename = e.Node.Tag as string;
if (filename == null ||
!File.Exists(filename))
{
return;
}
//------------------------------------
//load file
_updateLock = true;
Application.UseWaitCursor = true;
//set root folder of current html panel
this.htmlRootFolder = Path.GetDirectoryName(filename);
_htmlPanel.Text = File.ReadAllText(filename);
Application.UseWaitCursor = false;
_updateLock = false;
UpdateWebBrowserHtml();
}
public void PrepareSamples()
{
LoadCustomFonts();
LoadSamples();
}
public void LoadDemo(DemoBase demoBase)
{
demoBase.StartDemo(this._htmlPanel);
}
#region Private methods
/// <summary>
/// Loads the tree of document samples
/// </summary>
private void LoadSamples()
{
//find sample folder
string execFromFolder = Path.GetDirectoryName(Application.ExecutablePath);
//only from debug ?
if (!execFromFolder.EndsWith("\\Source\\HtmlRenderer.Demo\\bin\\Debug"))
{
return;
}
int index = execFromFolder.LastIndexOf("\\Source\\HtmlRenderer.Demo\\bin\\Debug");
string rootSampleFolder = execFromFolder.Substring(0, index) + "\\Source\\HtmlRenderer.Demo\\Samples";
var root = new TreeNode("HTML Renderer");
_samplesTreeView.Nodes.Add(root);
string[] sampleDirs = Directory.GetDirectories(rootSampleFolder);
//only 1 file level (not recursive)
foreach (string dirName in sampleDirs)
{
var dirNode = new TreeNode(Path.GetFileName(dirName));
root.Nodes.Add(dirNode);
string[] fileNames = Directory.GetFiles(dirName, "*.htm");
foreach (string fname in fileNames)
{
var onlyFileName = Path.GetFileName(fname);
if (!onlyFileName.StartsWith("x"))
{
//for our convention:
//file start with x will not show here
//(it used for comment out/backup file)
var fileNameNode = new TreeNode(Path.GetFileName(fname));
dirNode.Nodes.Add(fileNameNode);
fileNameNode.Tag = fname;
}
}
}
root.ExpandAll();
//-------------------------
}
public int StartAtSampleIndex
{
get;
set;
}
/// <summary>
/// Load custom fonts to be used by renderer htmls
/// </summary>
private void LoadCustomFonts()
{
//// load custom font font into private fonts collection
//var file = Path.GetTempFileName();
//File.WriteAllBytes(file, HtmlRenderer.Demo.Resource.CustomFont);
//_privateFont.AddFontFile(file);
//// add the fonts to renderer
//foreach (var fontFamily in _privateFont.Families)
//{
// HtmlRender.AddFontFamily(fontFamily);
//}
}
///// <summary>
///// On tree view node click load the html to the html panel and html editor.
///// </summary>
//private void OnSamplesTreeViewAfterSelect(object sender, TreeViewEventArgs e)
//{
// //if (!string.IsNullOrEmpty(name))
// //{
// // _updateLock = true;
// // string html = _samples[name];
// // if (!name.Contains("PerfSamples"))
// // SyntaxHilight.AddColoredText(html, _htmlEditor);
// // else
// // _htmlEditor.Text = html;
// // Application.UseWaitCursor = true;
// // _htmlPanel.AvoidImagesLateLoading = !name.Contains("Many images");
// // _htmlPanel.Text = html;
// // //try
// // //{
// // // _htmlPanel.AvoidImagesLateLoading = !name.Contains("Many images");
// // // _htmlPanel.Text = html;
// // //}
// // //catch (Exception ex)
// // //{
// // // MessageBox.Show(ex.ToString(), "Failed to render HTML");
// // //}
// // Application.UseWaitCursor = false;
// // _updateLock = false;
// // UpdateWebBrowserHtml();
// //}
//}
/// <summary>
/// On text change in the html editor update
/// </summary>
private void OnHtmlEditorTextChanged(object sender, EventArgs e)
{
if (!_updateLock)
{
_updateHtmlTimer.Change(1000, int.MaxValue);
}
}
/// <summary>
/// Update the html renderer with text from html editor.
/// </summary>
private void OnUpdateHtmlTimerTick(object state)
{
BeginInvoke(new MethodInvoker(() =>
{
_updateLock = true;
try
{
_htmlPanel.Text = _htmlEditor.Text;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Failed to render HTML");
}
//SyntaxHilight.AddColoredText(_htmlEditor.Text, _htmlEditor);
UpdateWebBrowserHtml();
_updateLock = false;
}));
}
/// <summary>
/// Open the current html is external process - the default user browser.
/// </summary>
private void OnOpenExternalViewButtonClick(object sender, EventArgs e)
{
var html = _showGeneratedHtmlCB.Checked ? _htmlPanel.GetHtml() : _htmlEditor.Text;
var tmpFile = Path.ChangeExtension(Path.GetTempFileName(), ".htm");
File.WriteAllText(tmpFile, html);
Process.Start(tmpFile);
}
/// <summary>
/// Show\Hide the web browser viwer.
/// </summary>
private void OnToggleWebBrowserButton_Click(object sender, EventArgs e)
{
//_webBrowser.Visible = !_webBrowser.Visible;
//_splitter.Visible = _webBrowser.Visible;
//_toggleWebBrowserButton.Text = _webBrowser.Visible ? "Hide IE View" : "Show IE View";
//if (_webBrowser.Visible)
//{
// _webBrowser.Width = _splitContainer2.Panel2.Width / 2;
// UpdateWebBrowserHtml();
//}
}
/// <summary>
/// Update the html shown in the web browser
/// </summary>
private void UpdateWebBrowserHtml()
{
//if (_webBrowser.Visible)
//{
// _webBrowser.DocumentText = _showGeneratedHtmlCB.Checked ? _htmlPanel.GetHtml() : GetFixedHtml();
//}
}
///// <summary>
///// Fix the raw html by replacing bridge object properties calls with path to file with the data returned from the property.
///// </summary>
///// <returns>fixed html</returns>
//private string GetFixedHtml()
//{
// var html = _htmlEditor.Text;
// html = Regex.Replace(html, @"src=\""(\w.*?)\""", match =>
// {
// var img = TryLoadResourceImage(match.Groups[1].Value);
// if (img != null)
// {
// var tmpFile = Path.GetTempFileName();
// img.Save(tmpFile, ImageFormat.Jpeg);
// return string.Format("src=\"{0}\"", tmpFile);
// }
// return match.Value;
// }, RegexOptions.IgnoreCase);
// html = Regex.Replace(html, @"href=\""(\w.*?)\""", match =>
// {
// var stylesheet = GetBuiltInStyleSheet(match.Groups[1].Value);
// if (stylesheet != null)
// {
// var tmpFile = Path.GetTempFileName();
// File.WriteAllText(tmpFile, stylesheet);
// return string.Format("href=\"{0}\"", tmpFile);
// }
// return match.Value;
// }, RegexOptions.IgnoreCase);
// return html;
//}
/// <summary>
/// On change if to show generated html or regular update the web browser to show the new choice.
/// </summary>
private void OnShowGeneratedHtmlCheckedChanged(object sender, EventArgs e)
{
UpdateWebBrowserHtml();
}
/// <summary>
/// Reload the html shown in the html editor by running coloring again.
/// </summary>
private void OnReloadColorsLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//SyntaxHilight.AddColoredText(_htmlEditor.Text, _htmlEditor);
}
/// <summary>
/// Get stylesheet by given key.
/// </summary>
private static string GetBuiltInStyleSheet(string src)
{
if (src == "StyleSheet")
{
return @"h1, h2, h3 { color: navy; font-weight:normal; }
h1 { margin-bottom: .47em }
h2 { margin-bottom: .3em }
h3 { margin-bottom: .4em }
ul { margin-top: .5em }
ul li {margin: .25em}
body { font:10pt Tahoma }
pre { border:solid 1px gray; background-color:#eee; padding:1em }
a:link { text-decoration: none; }
a:hover { text-decoration: underline; }
.gray { color:gray; }
.example { background-color:#efefef; corner-radius:5px; padding:0.5em; }
.whitehole { background-color:white; corner-radius:10px; padding:15px; }
.caption { font-size: 1.1em }
.comment { color: green; margin-bottom: 5px; margin-left: 3px; }
.comment2 { color: green; }";
}
return null;
}
/// <summary>
/// Handle stylesheet resolve.
/// </summary>
void HandleStylesheetRequest(object sender, TextRequestEventArgs e)
{
var stylesheet = GetBuiltInStyleSheet(e.Src);
if (stylesheet != null)
{
e.TextContent = stylesheet;
}
else
{
//load external style sheet
//beware request destination and request origin
//check style sheet ...
string fullStyleSheetFilename = this.htmlRootFolder + "\\" + e.Src;
if (File.Exists(fullStyleSheetFilename))
{
e.TextContent = File.ReadAllText(fullStyleSheetFilename);
}
}
}
/// <summary>
/// On image load in renderer set the image by event async.
/// </summary>
void HandleImageRequest(object sender, ImageRequestEventArgs e)
{
var img = TryLoadResourceImage(e.ImagSource);
if (img != null)
{
e.SetResultImage(new PixelFarm.Drawing.Bitmap(img.Width, img.Height, img));
}
else
{
//no image found
e.ImageBinder.State = ImageBinderState.Error;
}
}
/// <summary>
/// Get image by resource key.
/// </summary>
Image TryLoadResourceImage(string src)
{
Image image;
if (!_imageCache.TryGetValue(src, out image))
{
switch (src.ToLower())
{
case "htmlicon":
image = LayoutFarm.Demo.Resource.html32;
break;
case "staricon":
image = LayoutFarm.Demo.Resource.favorites32;
break;
case "fonticon":
image = LayoutFarm.Demo.Resource.font32;
break;
case "commenticon":
image = LayoutFarm.Demo.Resource.comment16;
break;
case "imageicon":
image = LayoutFarm.Demo.Resource.image32;
break;
case "methodicon":
image = LayoutFarm.Demo.Resource.method16;
break;
case "propertyicon":
image = LayoutFarm.Demo.Resource.property16;
break;
case "eventicon":
image = LayoutFarm.Demo.Resource.Event16;
break;
}
//----------------------------------
if (image != null)
{
//cache
_imageCache[src] = image;
}
else
{
//load local image ?
string fullImageFileName = this.htmlRootFolder + "\\" + src;
if (File.Exists(fullImageFileName))
{
image = new Bitmap(fullImageFileName);
//cache
_imageCache[src] = image;
}
}
//----------------------------------
}
return image;
}
///// <summary>
///// Show error raised from html renderer.
///// </summary>
//private static void OnRenderError(object sender, HtmlRenderErrorEventArgs e)
//{
// MessageBox.Show(e.Message + (e.Exception != null ? "\r\n" + e.Exception : null), "Error in Html Renderer", MessageBoxButtons.OK);
//}
///// <summary>
///// On specific link click handle it here.
///// </summary>
//private static void OnLinkClicked(object sender, HtmlLinkClickedEventArgs e)
//{
// if (e.Link == "SayHello")
// {
// MessageBox.Show("Hello you!");
// e.Handled = true;
// }
// else if (e.Link == "ShowSampleForm")
// {
// //using (var f = new SampleForm())
// //{
// // f.ShowDialog();
// // e.Handled = true;
// //}
// }
//}
/// <summary>
/// Execute performance test by setting all sample htmls in a loop.
/// </summary>
private void OnRunTestButtonClick(object sender, EventArgs e)
{
_updateLock = true;
_runTestButton.Text = "Running..";
_runTestButton.Enabled = false;
Application.DoEvents();
GC.Collect();
#if NET_40
AppDomain.MonitoringIsEnabled = true;
var startMemory = AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize;
#endif
const int iterations = 40;
//for (int i = 0; i < iterations; i++)
//{
// foreach (var html in _perfTestSamples)
// {
// _htmlPanel.Text = html;
// Application.DoEvents(); // so paint will be called
// }
//}
List<int> selectedSamples = new List<int>();
var allTestCount = _perfTestSamples.Count;
for (int i = 0; i < allTestCount; ++i)
{
selectedSamples.Add(2);
}
//HtmlRenderer.dbugCounter.dbugStartRecord = true;
//HtmlRenderer.dbugCounter.dbugDrawStringCount = 0;
long ms_total = 0;
System.Diagnostics.Stopwatch sw = new Stopwatch();
#if DEBUG
for (int i = 0; i < iterations; i++)
{
foreach (var sampleNum in selectedSamples)
{
ms_total += dbugCounter.Snap(sw, () =>
{
//HtmlRenderer.dbugCounter.dbugStartRecord = true;
//HtmlRenderer.dbugCounter.dbugDrawStringCount = 0;
_htmlPanel.Text = _perfTestSamples[sampleNum];
Application.DoEvents(); // so paint will be called
});
}
}
#endif
long endMemory = 0;
float totalMem = 0;
#if NET_40
endMemory = AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize;
totalMem = (endMemory - startMemory)/1024f;
#endif
float htmlSize = 0;
foreach (var sample in _perfTestSamples)
{
htmlSize += sample.Length * 2;
}
htmlSize = htmlSize / 1024f;
var msg = string.Format("{0} HTMLs ({1:N0} KB)\r\n{2} Iterations", _perfTestSamples.Count, htmlSize, iterations);
msg += "\r\n\r\n";
msg += string.Format("CPU:\r\nTotal: {0} msec\r\nIterationAvg: {1:N2} msec\r\nSingleAvg: {2:N2} msec",
ms_total, ms_total / iterations, ms_total / (double)iterations / _perfTestSamples.Count);
msg += "\r\n\r\n";
msg += string.Format("Memory:\r\nTotal: {0:N0} KB\r\nIterationAvg: {1:N0} KB\r\nSingleAvg: {2:N0} KB\r\nOverhead: {3:N0}%",
totalMem, totalMem / iterations, totalMem / iterations / _perfTestSamples.Count, 100 * (totalMem / iterations) / htmlSize);
Clipboard.SetDataObject(msg);
MessageBox.Show(msg, "Test run results");
_updateLock = false;
_runTestButton.Text = "Run Performance Test";
_runTestButton.Enabled = true;
}
#endregion
}
}