Skip to content

Commit 7ba60dd

Browse files
author
ArthurHub
committed
* refactor
1 parent fa8e750 commit 7ba60dd

11 files changed

Lines changed: 138 additions & 103 deletions

File tree

Source/HtmlRenderer.Core/Handlers/ImageLoadHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private IImage GetImageFromData(string src)
247247
if (imagePartsCount > 0)
248248
{
249249
byte[] imageData = base64PartsCount > 0 ? Convert.FromBase64String(s[1].Trim()) : new UTF8Encoding().GetBytes(Uri.UnescapeDataString(s[1].Trim()));
250-
return _htmlContainer.Global.FromStream(new MemoryStream(imageData));
250+
return _htmlContainer.Global.ImageFromStream(new MemoryStream(imageData));
251251
}
252252
}
253253
return null;
@@ -310,7 +310,7 @@ private void LoadImageFromFile(FileInfo source)
310310
if (source.Exists)
311311
{
312312
_imageFileStream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
313-
_image = _htmlContainer.Global.FromStream(_imageFileStream);
313+
_image = _htmlContainer.Global.ImageFromStream(_imageFileStream);
314314
_releaseImageObject = true;
315315
}
316316
ImageLoadComplete();

Source/HtmlRenderer.Core/HtmlRenderer.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="Handlers\SelectionHandler.cs" />
9090
<Compile Include="Handlers\StylesheetLoadHandler.cs" />
9191
<Compile Include="HtmlRendererUtils.cs" />
92+
<Compile Include="Interfaces\GlobalBase.cs" />
9293
<Compile Include="Interfaces\IFontFamily.cs" />
9394
<Compile Include="Interfaces\IBrush.cs" />
9495
<Compile Include="Interfaces\IContextMenu.cs" />

Source/HtmlRenderer.Core/HtmlRendererUtils.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@ namespace HtmlRenderer.Core
2222
/// </summary>
2323
public static class HtmlRendererUtils
2424
{
25-
/// <summary>
26-
/// The manifest resource name for embedded image used for image loading.
27-
/// </summary>
28-
public static string ManifestResourceNameForImageLoad
29-
{
30-
get { return "HtmlRenderer.Core.Utils.ImageLoad.png"; }
31-
}
32-
33-
/// <summary>
34-
/// The manifest resource name for embedded image used for image loading failed.
35-
/// </summary>
36-
public static string ManifestResourceNameForImageError
37-
{
38-
get { return "HtmlRenderer.Core.Utils.ImageError.png"; }
39-
}
40-
4125
/// <summary>
4226
/// The default stylesheet.
4327
/// </summary>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// "Therefore those skilled at the unorthodox
2+
// are infinite as heaven and earth,
3+
// inexhaustible as the great rivers.
4+
// When they come to an end,
5+
// they begin again,
6+
// like the days and months;
7+
// they die and are reborn,
8+
// like the four seasons."
9+
//
10+
// - Sun Tsu,
11+
// "The Art of War"
12+
13+
14+
using System.IO;
15+
16+
namespace HtmlRenderer.Core.Interfaces
17+
{
18+
/// <summary>
19+
/// Optional base class for <see cref="IGlobal"/> implementers to provide base helper functionality.
20+
/// </summary>
21+
public abstract class GlobalBase
22+
{
23+
#region Fields and Consts
24+
25+
/// <summary>
26+
/// default CSS parsed data singleton
27+
/// </summary>
28+
private CssData _defaultCssData;
29+
30+
/// <summary>
31+
/// image used to draw loading image icon
32+
/// </summary>
33+
private static IImage _loadImage;
34+
35+
/// <summary>
36+
/// image used to draw error image icon
37+
/// </summary>
38+
private static IImage _errorImage;
39+
40+
#endregion
41+
42+
public CssData GetDefaultCssData()
43+
{
44+
return _defaultCssData ?? (_defaultCssData = CreateDefaultCssData());
45+
}
46+
47+
/// <summary>
48+
/// Get image to be used while HTML image is loading.
49+
/// </summary>
50+
public IImage GetLoadImage()
51+
{
52+
if( _loadImage == null )
53+
{
54+
var stream = typeof(HtmlRendererUtils).Assembly.GetManifestResourceStream("HtmlRenderer.Core.Utils.ImageLoad.png");
55+
if( stream != null )
56+
_loadImage = ImageFromStream(stream);
57+
}
58+
return _loadImage;
59+
}
60+
61+
/// <summary>
62+
/// Get image to be used if HTML image load failed.
63+
/// </summary>
64+
public IImage GetErrorImage()
65+
{
66+
if (_errorImage == null)
67+
{
68+
var stream = typeof(HtmlRendererUtils).Assembly.GetManifestResourceStream("HtmlRenderer.Core.Utils.ImageError.png");
69+
if (stream != null)
70+
_errorImage = ImageFromStream(stream);
71+
}
72+
return _errorImage;
73+
}
74+
75+
/// <summary>
76+
/// Create an <see cref="IImage"/> object from the given stream.
77+
/// </summary>
78+
/// <param name="memoryStream">the stream to create image from</param>
79+
/// <returns>new image instance</returns>
80+
public abstract IImage ImageFromStream(Stream memoryStream);
81+
82+
/// <summary>
83+
/// Create a default CSS data object that will be cached.
84+
/// </summary>
85+
/// <returns></returns>
86+
protected abstract CssData CreateDefaultCssData();
87+
}
88+
}

Source/HtmlRenderer.Core/Interfaces/IGlobal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public interface IGlobal
5151
/// </summary>
5252
/// <param name="memoryStream">the stream to create image from</param>
5353
/// <returns>new image instance</returns>
54-
IImage FromStream(Stream memoryStream);
54+
IImage ImageFromStream(Stream memoryStream);
5555

5656
/// <summary>
5757
///

Source/HtmlRenderer.WinForms/Adapters/GlobalAdapter.cs

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace HtmlRenderer.WinForms.Adapters
2424
/// <summary>
2525
/// Adapter for general stuff for core.
2626
/// </summary>
27-
internal sealed class GlobalAdapter : IGlobal
27+
internal sealed class GlobalAdapter : GlobalBase, IGlobal
2828
{
2929
#region Fields and Consts
3030

@@ -33,21 +33,6 @@ internal sealed class GlobalAdapter : IGlobal
3333
/// </summary>
3434
private static readonly GlobalAdapter _instance = new GlobalAdapter();
3535

36-
/// <summary>
37-
/// default CSS parsed data singleton
38-
/// </summary>
39-
private CssData _defaultCssData;
40-
41-
/// <summary>
42-
/// image used to draw loading image icon
43-
/// </summary>
44-
private static IImage _loadImage;
45-
46-
/// <summary>
47-
/// image used to draw error image icon
48-
/// </summary>
49-
private static IImage _errorImage;
50-
5136
#endregion
5237

5338

@@ -73,11 +58,6 @@ public static GlobalAdapter Instance
7358
get { return _instance; }
7459
}
7560

76-
public CssData GetDefaultCssData()
77-
{
78-
return _defaultCssData ?? ( _defaultCssData = CssData.Parse(this, HtmlRendererUtils.DefaultStyleSheet, false) );
79-
}
80-
8161
/// <summary>
8262
/// Resolve color value from given color name.
8363
/// </summary>
@@ -89,34 +69,6 @@ public ColorInt ResolveColorFromName(string colorName)
8969
return Utils.Convert(color);
9070
}
9171

92-
/// <summary>
93-
/// Get image to be used while HTML image is loading.
94-
/// </summary>
95-
public IImage GetLoadImage()
96-
{
97-
if (_loadImage == null)
98-
{
99-
var stream = typeof(HtmlRendererUtils).Assembly.GetManifestResourceStream(HtmlRendererUtils.ManifestResourceNameForImageLoad);
100-
if (stream != null)
101-
_loadImage = new ImageAdapter(Image.FromStream(stream));
102-
}
103-
return _loadImage;
104-
}
105-
106-
/// <summary>
107-
/// Get image to be used if HTML image load failed.
108-
/// </summary>
109-
public IImage GetErrorImage()
110-
{
111-
if (_errorImage == null)
112-
{
113-
var stream = typeof(HtmlRendererUtils).Assembly.GetManifestResourceStream(HtmlRendererUtils.ManifestResourceNameForImageError);
114-
if (stream != null)
115-
_errorImage = new ImageAdapter(Image.FromStream(stream));
116-
}
117-
return _errorImage;
118-
}
119-
12072
/// <summary>
12173
/// Convert image object returned from <see cref="HtmlImageLoadEventArgs"/> to <see cref="IImage"/>.
12274
/// </summary>
@@ -132,7 +84,7 @@ public IImage ConvertImage(object image)
13284
/// </summary>
13385
/// <param name="memoryStream">the stream to create image from</param>
13486
/// <returns>new image instance</returns>
135-
public IImage FromStream(Stream memoryStream)
87+
public override IImage ImageFromStream(Stream memoryStream)
13688
{
13789
return new ImageAdapter(Image.FromStream(memoryStream));
13890
}
@@ -222,5 +174,14 @@ public void SaveToFile(IImage image, string name, string extension, IControl con
222174
}
223175
}
224176
}
177+
178+
/// <summary>
179+
/// Create a default CSS data object that will be cached.
180+
/// </summary>
181+
/// <returns></returns>
182+
protected override CssData CreateDefaultCssData()
183+
{
184+
return CssData.Parse(this, HtmlRendererUtils.DefaultStyleSheet, false);
185+
}
225186
}
226187
}

Source/HtmlRenderer.WinForms/HtmlContainer.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using HtmlRenderer.Core.Parse;
2020
using HtmlRenderer.Core.Utils;
2121
using HtmlRenderer.WinForms.Adapters;
22+
using HtmlRenderer.WinForms.Utilities;
2223

2324
namespace HtmlRenderer.WinForms
2425
{
@@ -232,8 +233,8 @@ public bool IsContextMenuEnabled
232233
/// </example>
233234
public PointF ScrollOffset
234235
{
235-
get { return new PointF(_htmlContainerInt.ScrollOffset.X, _htmlContainerInt.ScrollOffset.Y); }
236-
set { _htmlContainerInt.ScrollOffset = new PointInt(value.X, value.Y); }
236+
get { return Utils.Convert(_htmlContainerInt.ScrollOffset); }
237+
set { _htmlContainerInt.ScrollOffset = Utils.Convert(value); }
237238
}
238239

239240
/// <summary>
@@ -242,30 +243,30 @@ public PointF ScrollOffset
242243
/// </summary>
243244
public PointF Location
244245
{
245-
get { return new PointF(_htmlContainerInt.Location.X, _htmlContainerInt.Location.Y); }
246-
set { _htmlContainerInt.Location = new PointInt(value.X, value.Y); }
246+
get { return Utils.Convert(_htmlContainerInt.Location); }
247+
set { _htmlContainerInt.Location = Utils.Convert(value); }
247248
}
248249

249250
/// <summary>
250251
/// The max width and height of the rendered html.<br/>
251252
/// The max width will effect the html layout wrapping lines, resize images and tables where possible.<br/>
252253
/// The max height does NOT effect layout, but will not render outside it (clip).<br/>
253-
/// <see cref="HtmlContainerInt.ActualSize"/> can be exceed the max size by layout restrictions (unwrappable line, set image size, etc.).<br/>
254+
/// <see cref="ActualSize"/> can be exceed the max size by layout restrictions (unwrappable line, set image size, etc.).<br/>
254255
/// Set zero for unlimited (width\height separately).<br/>
255256
/// </summary>
256257
public SizeF MaxSize
257258
{
258-
get { return new SizeF(_htmlContainerInt.MaxSize.Width, _htmlContainerInt.MaxSize.Height); }
259-
set { _htmlContainerInt.MaxSize = new SizeInt(value.Width,value.Height); }
259+
get { return Utils.Convert(_htmlContainerInt.MaxSize); }
260+
set { _htmlContainerInt.MaxSize = Utils.Convert(value); }
260261
}
261262

262263
/// <summary>
263264
/// The actual size of the rendered html (after layout)
264265
/// </summary>
265266
public SizeF ActualSize
266267
{
267-
get { return new SizeF(_htmlContainerInt.ActualSize.Width, _htmlContainerInt.ActualSize.Height); }
268-
internal set { _htmlContainerInt.ActualSize = new SizeInt(value.Width, value.Height); }
268+
get { return Utils.Convert(_htmlContainerInt.ActualSize); }
269+
internal set { _htmlContainerInt.ActualSize = Utils.Convert(value); }
269270
}
270271

271272
/// <summary>
@@ -313,7 +314,7 @@ public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
313314
/// <returns>found attribute value or null if not found</returns>
314315
public string GetAttributeAt(Point location, string attribute)
315316
{
316-
return _htmlContainerInt.GetAttributeAt(new PointInt(location.X, location.Y), attribute);
317+
return _htmlContainerInt.GetAttributeAt(Utils.Convert(location), attribute);
317318
}
318319

319320
/// <summary>
@@ -323,7 +324,7 @@ public string GetAttributeAt(Point location, string attribute)
323324
/// <returns>css link href if exists or null</returns>
324325
public string GetLinkAt(Point location)
325326
{
326-
return _htmlContainerInt.GetLinkAt(new PointInt(location.X, location.Y));
327+
return _htmlContainerInt.GetLinkAt(Utils.Convert(location));
327328
}
328329

329330
/// <summary>
@@ -336,7 +337,7 @@ public string GetLinkAt(Point location)
336337
public RectangleF? GetElementRectangle(string elementId)
337338
{
338339
var r = _htmlContainerInt.GetElementRectangle(elementId);
339-
return r.HasValue ? new RectangleF(r.Value.X, r.Value.Y, r.Value.Width, r.Value.Height) : (RectangleF?)null;
340+
return r.HasValue ? Utils.Convert(r.Value) : (RectangleF?)null;
340341
}
341342

342343
/// <summary>
@@ -377,7 +378,7 @@ public void HandleMouseDown(Control parent, MouseEventArgs e)
377378
ArgChecker.AssertArgNotNull(parent, "parent");
378379
ArgChecker.AssertArgNotNull(e, "e");
379380

380-
_htmlContainerInt.HandleMouseDown(new ControlAdapter(parent), new PointInt(e.Location.X, e.Location.Y));
381+
_htmlContainerInt.HandleMouseDown(new ControlAdapter(parent), Utils.Convert(e.Location));
381382
}
382383

383384
/// <summary>
@@ -390,7 +391,7 @@ public void HandleMouseUp(Control parent, MouseEventArgs e)
390391
ArgChecker.AssertArgNotNull(parent, "parent");
391392
ArgChecker.AssertArgNotNull(e, "e");
392393

393-
_htmlContainerInt.HandleMouseUp(new ControlAdapter(parent), new PointInt(e.Location.X, e.Location.Y), CreateMouseEvent(e));
394+
_htmlContainerInt.HandleMouseUp(new ControlAdapter(parent), Utils.Convert(e.Location), CreateMouseEvent(e));
394395
}
395396

396397
/// <summary>
@@ -403,7 +404,7 @@ public void HandleMouseDoubleClick(Control parent, MouseEventArgs e)
403404
ArgChecker.AssertArgNotNull(parent, "parent");
404405
ArgChecker.AssertArgNotNull(e, "e");
405406

406-
_htmlContainerInt.HandleMouseDoubleClick(new ControlAdapter(parent), new PointInt(e.Location.X, e.Location.Y));
407+
_htmlContainerInt.HandleMouseDoubleClick(new ControlAdapter(parent), Utils.Convert(e.Location));
407408
}
408409

409410
/// <summary>
@@ -416,7 +417,7 @@ public void HandleMouseMove(Control parent, MouseEventArgs e)
416417
ArgChecker.AssertArgNotNull(parent, "parent");
417418
ArgChecker.AssertArgNotNull(e, "e");
418419

419-
_htmlContainerInt.HandleMouseMove(new ControlAdapter(parent), new PointInt(e.Location.X, e.Location.Y));
420+
_htmlContainerInt.HandleMouseMove(new ControlAdapter(parent), Utils.Convert(e.Location));
420421
}
421422

422423
/// <summary>
@@ -465,7 +466,7 @@ private static MouseEventInt CreateMouseEvent(MouseEventArgs e)
465466
/// <summary>
466467
/// Create HtmlRenderer key event from win forms key event.
467468
/// </summary>
468-
private KeyEventInt CreateKeyEevent(KeyEventArgs e)
469+
private static KeyEventInt CreateKeyEevent(KeyEventArgs e)
469470
{
470471
return new KeyEventInt(e.Control, e.KeyCode == Keys.A, e.KeyCode == Keys.C);
471472
}

0 commit comments

Comments
 (0)