Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions Source/HtmlRenderer/Adapters/RAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ public abstract class RAdapter
/// <summary>
/// cache of brush color to brush instance
/// </summary>
/// <remarks>The adapter is shared by every container, the caches are locked on themselves.</remarks>
private readonly Dictionary<RColor, RBrush> _brushesCache = new Dictionary<RColor, RBrush>();

/// <summary>
/// cache of pen color to pen instance
/// cache of pens, a cached pen is never changed, since another thread may be drawing with it
/// </summary>
private readonly Dictionary<RColor, RPen> _penCache = new Dictionary<RColor, RPen>();
private readonly Dictionary<(RColor Color, double Width, RDashStyle DashStyle), RPen> _penCache = new Dictionary<(RColor, double, RDashStyle), RPen>();

/// <summary>
/// cache of all the font used not to create same font again and again
Expand Down Expand Up @@ -256,18 +257,36 @@ public RColor GetColor(string colorName)
}

/// <summary>
/// Get cached pen instance for the given color.
/// Get cached solid pen instance for the given color, 1 wide.
/// </summary>
/// <param name="color">the color to get pen for</param>
/// <returns>pen instance</returns>
/// <returns>pen instance, don't change it</returns>
public RPen GetPen(RColor color)
{
RPen pen;
if (!_penCache.TryGetValue(color, out pen))
return GetPen(color, 1, RDashStyle.Solid);
}

/// <summary>
/// Get cached pen instance for the given color, width and dash style.
/// </summary>
/// <param name="color">the color to get pen for</param>
/// <param name="width">the width of the pen</param>
/// <param name="dashStyle">the dash style of the pen</param>
/// <returns>pen instance, don't change it</returns>
public RPen GetPen(RColor color, double width, RDashStyle dashStyle)
{
lock (_penCache)
{
_penCache[color] = pen = CreatePen(color);
RPen pen;
if (!_penCache.TryGetValue((color, width, dashStyle), out pen))
{
pen = CreatePen(color);
pen.Width = width;
pen.DashStyle = dashStyle;
_penCache[(color, width, dashStyle)] = pen;
}
return pen;
}
return pen;
}

/// <summary>
Expand All @@ -277,12 +296,15 @@ public RPen GetPen(RColor color)
/// <returns>brush instance</returns>
public RBrush GetSolidBrush(RColor color)
{
RBrush brush;
if (!_brushesCache.TryGetValue(color, out brush))
lock (_brushesCache)
{
_brushesCache[color] = brush = CreateSolidBrush(color);
RBrush brush;
if (!_brushesCache.TryGetValue(color, out brush))
{
_brushesCache[color] = brush = CreateSolidBrush(color);
}
return brush;
}
return brush;
}

/// <summary>
Expand Down Expand Up @@ -372,7 +394,13 @@ public void AddFontFamilyMapping(string fromFamily, string toFamily)
public virtual async Task<bool> AddFontFace(string familyName, RUri uri, int weight, bool isItalic, int stretch, IReadOnlyList<CodepointRange> ranges)
{
RFontFamily fontFamily;
if (!_fontFaceCache.TryGetValue(uri.AbsoluteUri, out fontFamily))
bool cached;
lock (_fontFaceCache)
{
cached = _fontFaceCache.TryGetValue(uri.AbsoluteUri, out fontFamily);
}

if (!cached)
{
var networkResponse = await GetResourceStream(uri).ConfigureAwait(false);
if (networkResponse == null || networkResponse.ResourceStream == null)
Expand Down Expand Up @@ -404,7 +432,14 @@ public virtual async Task<bool> AddFontFace(string familyName, RUri uri, int wei
return false;
}

_fontFaceCache[uri.AbsoluteUri] = fontFamily;
lock (_fontFaceCache)
{
// Another container may have loaded the same face in the mean time, keep the first
if (_fontFaceCache.TryGetValue(uri.AbsoluteUri, out var existing))
fontFamily = existing;
else
_fontFaceCache[uri.AbsoluteUri] = fontFamily;
}
}

_fontsHandler.AddFontFace(familyName, fontFamily, weight, isItalic, stretch, ranges);
Expand Down
16 changes: 14 additions & 2 deletions Source/HtmlRenderer/Adapters/RGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,27 @@ protected RGraphics(RAdapter adapter, RRect initialClip)
}

/// <summary>
/// Get color pen.
/// Get solid color pen, 1 wide.
/// </summary>
/// <param name="color">the color to get the pen for</param>
/// <returns>pen instance</returns>
/// <returns>pen instance, shared, don't change it</returns>
public RPen GetPen(RColor color)
{
return _adapter.GetPen(color);
}

/// <summary>
/// Get color pen with the given width and dash style.
/// </summary>
/// <param name="color">the color to get the pen for</param>
/// <param name="width">the width of the pen</param>
/// <param name="dashStyle">the dash style of the pen</param>
/// <returns>pen instance, shared, don't change it</returns>
public RPen GetPen(RColor color, double width, RDashStyle dashStyle)
{
return _adapter.GetPen(color, width, dashStyle);
}

/// <summary>
/// Get solid color brush.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions Source/HtmlRenderer/Core/Dom/CssBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1958,8 +1958,6 @@ internal void PaintDecoration(RGraphics g, RRect rectangle, bool isFirst, bool i

var bottomInset = ActualPaddingBottom - ActualBorderBottomWidth;
var pen = g.GetPen(ActualColor);
pen.Width = 1;
pen.DashStyle = RDashStyle.Solid;

// CSS 2.1 §16.3.1, the value can list several lines.
foreach (var line in TextDecoration.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
Expand Down
14 changes: 4 additions & 10 deletions Source/HtmlRenderer/Core/Handlers/BordersDrawHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,15 @@ private static RGraphicsPath GetRoundedBorderPath(RGraphics g, Border border, Cs
/// </summary>
private static RPen GetPen(RGraphics g, string style, RColor color, double width)
{
var p = g.GetPen(color);
p.Width = width;
switch (style)
{
case "solid":
p.DashStyle = RDashStyle.Solid;
break;
case "dotted":
p.DashStyle = RDashStyle.Dot;
break;
return g.GetPen(color, width, RDashStyle.Dot);
case "dashed":
p.DashStyle = RDashStyle.Dash;
break;
return g.GetPen(color, width, RDashStyle.Dash);
default:
return g.GetPen(color, width, RDashStyle.Solid);
}
return p;
}

/// <summary>
Expand Down
Loading
Loading