Skip to content

Commit cfc8dde

Browse files
author
ArthurHub
committed
* refactor
1 parent ad643dd commit cfc8dde

16 files changed

Lines changed: 159 additions & 126 deletions

File tree

Source/HtmlRenderer.Core/CssData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal IDictionary<string, Dictionary<string, List<CssBlock>>> MediaBlocks
7676
/// Check if there are css blocks for the given class selector.
7777
/// </summary>
7878
/// <param name="className">the class selector to check for css blocks by</param>
79-
/// <param name="media">optinal: the css media type (default - all)</param>
79+
/// <param name="media">optional: the css media type (default - all)</param>
8080
/// <returns>true - has css blocks for the class, false - otherwise</returns>
8181
public bool ContainsCssBlock(string className, string media = "all")
8282
{
@@ -92,7 +92,7 @@ public bool ContainsCssBlock(string className, string media = "all")
9292
/// selector or hierarchy selector.
9393
/// </summary>
9494
/// <param name="className">the class selector to get css blocks by</param>
95-
/// <param name="media">optinal: the css media type (default - all)</param>
95+
/// <param name="media">optional: the css media type (default - all)</param>
9696
/// <returns>collection of css blocks, empty collection if no blocks exists (never null)</returns>
9797
public IEnumerable<CssBlock> GetCssBlock(string className, string media = "all")
9898
{
@@ -113,7 +113,7 @@ public IEnumerable<CssBlock> GetCssBlock(string className, string media = "all")
113113
/// If there is already css blocks for the same class it will check for each existing block
114114
/// if the hierarchical selectors match (or not exists). if do the two css blocks will be merged into
115115
/// one where the new block properties overwrite existing if needed. if the new block doesn't mach any
116-
/// existing it will be added either to the beggining of the list if it has no hierarchical selectors or at the end.<br/>
116+
/// existing it will be added either to the beginning of the list if it has no hierarchical selectors or at the end.<br/>
117117
/// Css block without hierarchical selectors must be added to the beginning of the list so more specific block
118118
/// can overwrite it when the style is applied.
119119
/// </remarks>

Source/HtmlRenderer.Core/Dom/CssBox.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15-
using System.Drawing.Drawing2D;
1615
using System.Globalization;
1716
using HtmlRenderer.Core.Dom.Entities;
1817
using HtmlRenderer.Core.Entities;
@@ -128,7 +127,7 @@ public CssBox ParentBox
128127
if (value != null && !value.Boxes.Contains(this))
129128
{
130129
_parentBox.Boxes.Add(this);
131-
_htmlContainer = value.HtmlContainer;
130+
HtmlContainer = value.HtmlContainer;
132131
}
133132
}
134133
}
@@ -730,7 +729,7 @@ private void CreateListItemBox(IGraphics g)
730729
_listItemBox = new CssBox(null, null);
731730
_listItemBox.InheritStyle(this);
732731
_listItemBox.Display = CssConstants.Inline;
733-
_listItemBox._htmlContainer = HtmlContainer;
732+
_listItemBox.HtmlContainer = HtmlContainer;
734733

735734
if (ListStyleType.Equals(CssConstants.Disc, StringComparison.InvariantCultureIgnoreCase))
736735
{

Source/HtmlRenderer.Core/Dom/CssBoxFrame.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ private void DrawImage(IGraphics g, PointInt offset, RectangleInt rect)
444444
}
445445
else if (_isVideo && !_imageLoadingComplete)
446446
{
447-
RenderUtils.DrawImageLoadingIcon(g, rect);
447+
RenderUtils.DrawImageLoadingIcon(g, HtmlContainer, rect);
448448
if (rect.Width > 19 && rect.Height > 19)
449449
{
450450
g.DrawRectangle(g.GetPen(ColorInt.LightGray), rect.X, rect.Y, rect.Width, rect.Height);

Source/HtmlRenderer.Core/Dom/CssBoxImage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ protected override void PaintImp(IGraphics g)
108108
{
109109
if (_imageLoadingComplete && r.Width > 19 && r.Height > 19)
110110
{
111-
RenderUtils.DrawImageErrorIcon(g, r);
111+
RenderUtils.DrawImageErrorIcon(g, HtmlContainer, r);
112112
}
113113
}
114114
else
115115
{
116-
RenderUtils.DrawImageLoadingIcon(g, r);
116+
RenderUtils.DrawImageLoadingIcon(g, HtmlContainer, r);
117117
if (r.Width > 19 && r.Height > 19)
118118
{
119119
g.DrawRectangle(g.GetPen(ColorInt.LightGray), r.X, r.Y, r.Width, r.Height);

Source/HtmlRenderer.Core/Handlers/ContextMenuHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public void ShowContextMenu(IControl parent, CssRect rect, CssBox link)
284284
_parentControl = parent;
285285
_currentRect = rect;
286286
_currentLink = link;
287-
_contextMenu = _htmlContainer.Global2.CreateContextMenu();
287+
_contextMenu = _htmlContainer.Global.CreateContextMenu();
288288

289289
if(rect != null)
290290
{
@@ -395,7 +395,7 @@ private void OnCopyLinkClick(object sender, EventArgs eventArgs)
395395
{
396396
try
397397
{
398-
_htmlContainer.Global2.SetToClipboard(_currentLink.HrefLink);
398+
_htmlContainer.Global.SetToClipboard(_currentLink.HrefLink);
399399
}
400400
catch (Exception ex)
401401
{
@@ -415,7 +415,7 @@ private void OnSaveImageClick(object sender, EventArgs eventArgs)
415415
try
416416
{
417417
var imageSrc = _currentRect.OwnerBox.GetAttribute("src");
418-
_htmlContainer.Global2.SaveToFile(_currentRect.Image, Path.GetFileName(imageSrc) ?? "image", Path.GetExtension(imageSrc) ?? "png");
418+
_htmlContainer.Global.SaveToFile(_currentRect.Image, Path.GetFileName(imageSrc) ?? "image", Path.GetExtension(imageSrc) ?? "png");
419419
}
420420
catch (Exception ex)
421421
{
@@ -434,7 +434,7 @@ private void OnCopyImageLinkClick(object sender, EventArgs eventArgs)
434434
{
435435
try
436436
{
437-
_htmlContainer.Global2.SetToClipboard(_currentRect.OwnerBox.GetAttribute("src"));
437+
_htmlContainer.Global.SetToClipboard(_currentRect.OwnerBox.GetAttribute("src"));
438438
}
439439
catch (Exception ex)
440440
{
@@ -453,7 +453,7 @@ private void OnCopyImageClick(object sender, EventArgs eventArgs)
453453
{
454454
try
455455
{
456-
_htmlContainer.Global2.SetToClipboard(_currentRect.Image);
456+
_htmlContainer.Global.SetToClipboard(_currentRect.Image);
457457
}
458458
catch (Exception ex)
459459
{

Source/HtmlRenderer.Core/Handlers/ImageLoadHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private void SetFromInlineData(string src)
228228
/// </summary>
229229
/// <param name="src">the source that has the base64 encoded image</param>
230230
/// <returns>image from base64 data string or null if failed</returns>
231-
private static IImage GetImageFromData(string src)
231+
private IImage GetImageFromData(string src)
232232
{
233233
var s = src.Substring(src.IndexOf(':') + 1).Split(new[] { ',' }, 2);
234234
if (s.Length == 2)
@@ -246,7 +246,7 @@ private static IImage GetImageFromData(string src)
246246
if (imagePartsCount > 0)
247247
{
248248
byte[] imageData = base64PartsCount > 0 ? Convert.FromBase64String(s[1].Trim()) : new UTF8Encoding().GetBytes(Uri.UnescapeDataString(s[1].Trim()));
249-
return HtmlContainerInt.Global.FromStream(new MemoryStream(imageData));
249+
return _htmlContainer.Global.FromStream(new MemoryStream(imageData));
250250
}
251251
}
252252
return null;
@@ -309,7 +309,7 @@ private void LoadImageFromFile(FileInfo source)
309309
if (source.Exists)
310310
{
311311
_imageFileStream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
312-
_image = HtmlContainerInt.Global.FromStream(_imageFileStream);
312+
_image = _htmlContainer.Global.FromStream(_imageFileStream);
313313
_releaseImageObject = true;
314314
}
315315
ImageLoadComplete();

Source/HtmlRenderer.Core/Handlers/SelectionHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public void CopySelectedHtml()
303303
var html = DomUtils.GenerateHtml(_root, HtmlGenerationStyle.Inline, true);
304304
var plainText = DomUtils.GetSelectedPlainText(_root);
305305
if (!string.IsNullOrEmpty(plainText))
306-
_root.HtmlContainer.Global2.SetToClipboard(html, plainText);
306+
_root.HtmlContainer.Global.SetToClipboard(html, plainText);
307307
}
308308
}
309309

Source/HtmlRenderer.Core/HtmlContainerInt.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public sealed class HtmlContainerInt : IDisposable
8383
/// <summary>
8484
///
8585
/// </summary>
86-
private static IGlobal _global;
86+
private readonly IGlobal _global;
8787

8888
/// <summary>
8989
/// the root css box of the parsed html
@@ -106,7 +106,7 @@ public sealed class HtmlContainerInt : IDisposable
106106
private ColorInt _selectionForeColor;
107107

108108
/// <summary>
109-
/// the backcolor to use for selected text
109+
/// the back-color to use for selected text
110110
/// </summary>
111111
private ColorInt _selectionBackColor;
112112

@@ -127,7 +127,7 @@ public sealed class HtmlContainerInt : IDisposable
127127
private bool _isContextMenuEnabled = true;
128128

129129
/// <summary>
130-
/// Gets or sets a value indicating if antialiasing should be avoided
130+
/// Gets or sets a value indicating if anti-aliasing should be avoided
131131
/// for geometry like backgrounds and borders
132132
/// </summary>
133133
private bool _avoidGeometryAntialias;
@@ -167,18 +167,19 @@ public sealed class HtmlContainerInt : IDisposable
167167

168168

169169
/// <summary>
170-
///
170+
/// Init.
171171
/// </summary>
172-
public static IGlobal Global
172+
public HtmlContainerInt(IGlobal global)
173173
{
174-
internal get { return _global; }
175-
set { _global = value; }
174+
ArgChecker.AssertArgNotNull(global, "global");
175+
176+
_global = global;
176177
}
177-
178+
178179
/// <summary>
179180
///
180181
/// </summary>
181-
internal IGlobal Global2
182+
internal IGlobal Global
182183
{
183184
get { return _global; }
184185
}

Source/HtmlRenderer.Core/HtmlRendererUtils.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,34 @@ namespace HtmlRenderer.Core
2020
/// </summary>
2121
public static class HtmlRendererUtils
2222
{
23+
/// <summary>
24+
/// Resolve color value from given color name.
25+
/// </summary>
26+
/// <param name="colorName">the color name</param>
27+
/// <returns>color value</returns>
28+
public delegate ColorInt ResolveColorFromNameDelegate(String colorName);
29+
30+
/// <summary>
31+
/// The manifest resource name for embedded image used for image loading.
32+
/// </summary>
33+
public static string ManifestResourceNameForImageLoad
34+
{
35+
get { return "HtmlRenderer.Core.Utils.ImageLoad.png"; }
36+
}
37+
38+
/// <summary>
39+
/// The manifest resource name for embedded image used for image loading failed.
40+
/// </summary>
41+
public static string ManifestResourceNameForImageError
42+
{
43+
get { return "HtmlRenderer.Core.Utils.ImageError.png"; }
44+
}
45+
46+
/// <summary>
47+
/// Resolve color value from given color name.
48+
/// </summary>
49+
public static ResolveColorFromNameDelegate ResolveColorFromName { get; set; }
50+
2351
/// <summary>
2452
/// Measure the size of the html by performing layout under the given restrictions.
2553
/// </summary>

Source/HtmlRenderer.Core/IGlobal.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// "The Art of War"
1212

1313
using System.IO;
14-
using HtmlRenderer.Core.Entities;
1514

1615
namespace HtmlRenderer.Core
1716
{
@@ -21,12 +20,15 @@ namespace HtmlRenderer.Core
2120
public interface IGlobal
2221
{
2322
/// <summary>
24-
/// Get color instance from given color name.
23+
/// Get image to be used while HTML image is loading.
2524
/// </summary>
26-
/// <param name="colorName">the color name</param>
27-
/// <returns>color instance</returns>
28-
ColorInt ColorFromName(string colorName);
29-
25+
IImage GetLoadImage();
26+
27+
/// <summary>
28+
/// Get image to be used if HTML image load failed.
29+
/// </summary>
30+
IImage GetErrorImage();
31+
3032
/// <summary>
3133
/// Create an <see cref="IImage"/> object from the given stream.
3234
/// </summary>

0 commit comments

Comments
 (0)