@@ -45,12 +45,13 @@ public abstract class RAdapter
4545 /// <summary>
4646 /// cache of brush color to brush instance
4747 /// </summary>
48+ /// <remarks>The adapter is shared by every container, the caches are locked on themselves.</remarks>
4849 private readonly Dictionary < RColor , RBrush > _brushesCache = new Dictionary < RColor , RBrush > ( ) ;
4950
5051 /// <summary>
51- /// cache of pen color to pen instance
52+ /// cache of pens, a cached pen is never changed, since another thread may be drawing with it
5253 /// </summary>
53- private readonly Dictionary < RColor , RPen > _penCache = new Dictionary < RColor , RPen > ( ) ;
54+ private readonly Dictionary < ( RColor Color , double Width , RDashStyle DashStyle ) , RPen > _penCache = new Dictionary < ( RColor , double , RDashStyle ) , RPen > ( ) ;
5455
5556 /// <summary>
5657 /// cache of all the font used not to create same font again and again
@@ -256,18 +257,36 @@ public RColor GetColor(string colorName)
256257 }
257258
258259 /// <summary>
259- /// Get cached pen instance for the given color.
260+ /// Get cached solid pen instance for the given color, 1 wide .
260261 /// </summary>
261262 /// <param name="color">the color to get pen for</param>
262- /// <returns>pen instance</returns>
263+ /// <returns>pen instance, don't change it </returns>
263264 public RPen GetPen ( RColor color )
264265 {
265- RPen pen ;
266- if ( ! _penCache . TryGetValue ( color , out pen ) )
266+ return GetPen ( color , 1 , RDashStyle . Solid ) ;
267+ }
268+
269+ /// <summary>
270+ /// Get cached pen instance for the given color, width and dash style.
271+ /// </summary>
272+ /// <param name="color">the color to get pen for</param>
273+ /// <param name="width">the width of the pen</param>
274+ /// <param name="dashStyle">the dash style of the pen</param>
275+ /// <returns>pen instance, don't change it</returns>
276+ public RPen GetPen ( RColor color , double width , RDashStyle dashStyle )
277+ {
278+ lock ( _penCache )
267279 {
268- _penCache [ color ] = pen = CreatePen ( color ) ;
280+ RPen pen ;
281+ if ( ! _penCache . TryGetValue ( ( color , width , dashStyle ) , out pen ) )
282+ {
283+ pen = CreatePen ( color ) ;
284+ pen . Width = width ;
285+ pen . DashStyle = dashStyle ;
286+ _penCache [ ( color , width , dashStyle ) ] = pen ;
287+ }
288+ return pen ;
269289 }
270- return pen ;
271290 }
272291
273292 /// <summary>
@@ -277,12 +296,15 @@ public RPen GetPen(RColor color)
277296 /// <returns>brush instance</returns>
278297 public RBrush GetSolidBrush ( RColor color )
279298 {
280- RBrush brush ;
281- if ( ! _brushesCache . TryGetValue ( color , out brush ) )
299+ lock ( _brushesCache )
282300 {
283- _brushesCache [ color ] = brush = CreateSolidBrush ( color ) ;
301+ RBrush brush ;
302+ if ( ! _brushesCache . TryGetValue ( color , out brush ) )
303+ {
304+ _brushesCache [ color ] = brush = CreateSolidBrush ( color ) ;
305+ }
306+ return brush ;
284307 }
285- return brush ;
286308 }
287309
288310 /// <summary>
@@ -372,7 +394,13 @@ public void AddFontFamilyMapping(string fromFamily, string toFamily)
372394 public virtual async Task < bool > AddFontFace ( string familyName , RUri uri , int weight , bool isItalic , int stretch , IReadOnlyList < CodepointRange > ranges )
373395 {
374396 RFontFamily fontFamily ;
375- if ( ! _fontFaceCache . TryGetValue ( uri . AbsoluteUri , out fontFamily ) )
397+ bool cached ;
398+ lock ( _fontFaceCache )
399+ {
400+ cached = _fontFaceCache . TryGetValue ( uri . AbsoluteUri , out fontFamily ) ;
401+ }
402+
403+ if ( ! cached )
376404 {
377405 var networkResponse = await GetResourceStream ( uri ) . ConfigureAwait ( false ) ;
378406 if ( networkResponse == null || networkResponse . ResourceStream == null )
@@ -404,7 +432,14 @@ public virtual async Task<bool> AddFontFace(string familyName, RUri uri, int wei
404432 return false ;
405433 }
406434
407- _fontFaceCache [ uri . AbsoluteUri ] = fontFamily ;
435+ lock ( _fontFaceCache )
436+ {
437+ // Another container may have loaded the same face in the mean time, keep the first
438+ if ( _fontFaceCache . TryGetValue ( uri . AbsoluteUri , out var existing ) )
439+ fontFamily = existing ;
440+ else
441+ _fontFaceCache [ uri . AbsoluteUri ] = fontFamily ;
442+ }
408443 }
409444
410445 _fontsHandler . AddFontFace ( familyName , fontFamily , weight , isItalic , stretch , ranges ) ;
0 commit comments