Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<VersionPrefix>3.4.0</VersionPrefix>
<VersionPrefix>3.4.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<AvaloniaVersionPrefix>11.3.9.1</AvaloniaVersionPrefix>
<AvaloniaVersionPrefix>11.3.9.2</AvaloniaVersionPrefix>
<AvaloniaVersionSuffix>$(VersionSuffix)</AvaloniaVersionSuffix>
<Authors>Wiesław Šoltés</Authors>
<Company>Wiesław Šoltés</Company>
Expand Down
135 changes: 135 additions & 0 deletions src/Svg.Skia/SkiaModel.Caching.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright (c) Wiesław Šoltés. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using ShimSkiaSharp;
using Svg.Skia.TypefaceProviders;

namespace Svg.Skia;

public partial class SkiaModel
{
private const int TypefaceCacheLimit = 512;
private const int ResolvedTypefaceCacheLimit = 512;
private const int PositionedTextCacheRefTrimThreshold = 1024;

private readonly struct TypefaceKey : IEquatable<TypefaceKey>
{
public TypefaceKey(
string? familyName,
SkiaSharp.SKFontStyleWeight weight,
SkiaSharp.SKFontStyleWidth width,
SkiaSharp.SKFontStyleSlant slant)
{
FamilyName = familyName;
Weight = weight;
Width = width;
Slant = slant;
}

public string? FamilyName { get; }
public SkiaSharp.SKFontStyleWeight Weight { get; }
public SkiaSharp.SKFontStyleWidth Width { get; }
public SkiaSharp.SKFontStyleSlant Slant { get; }

public bool Equals(TypefaceKey other)
{
return string.Equals(FamilyName, other.FamilyName, StringComparison.Ordinal)
&& Weight == other.Weight
&& Width == other.Width
&& Slant == other.Slant;
}

public override bool Equals(object? obj)
{
return obj is TypefaceKey other && Equals(other);
}

public override int GetHashCode()
{
unchecked
{
var hash = FamilyName?.GetHashCode() ?? 0;
hash = (hash * 397) ^ (int)Weight;
hash = (hash * 397) ^ (int)Width;
hash = (hash * 397) ^ (int)Slant;
return hash;
}
}
}

private readonly struct FontSignature : IEquatable<FontSignature>
{
public FontSignature(SkiaSharp.SKPaint paint)
{
TypefaceHandle = paint.Typeface?.Handle ?? IntPtr.Zero;
TextSize = paint.TextSize;
TextScaleX = paint.TextScaleX;
TextSkewX = paint.TextSkewX;
LcdRenderText = paint.LcdRenderText;
SubpixelText = paint.SubpixelText;
FakeBoldText = paint.FakeBoldText;
}

public IntPtr TypefaceHandle { get; }
public float TextSize { get; }
public float TextScaleX { get; }
public float TextSkewX { get; }
public bool LcdRenderText { get; }
public bool SubpixelText { get; }
public bool FakeBoldText { get; }

public bool Equals(FontSignature other)
{
return TypefaceHandle == other.TypefaceHandle
&& TextSize.Equals(other.TextSize)
&& TextScaleX.Equals(other.TextScaleX)
&& TextSkewX.Equals(other.TextSkewX)
&& LcdRenderText == other.LcdRenderText
&& SubpixelText == other.SubpixelText
&& FakeBoldText == other.FakeBoldText;
}

public override bool Equals(object? obj)
{
return obj is FontSignature other && Equals(other);
}

public override int GetHashCode()
{
unchecked
{
var hash = TypefaceHandle.GetHashCode();
hash = (hash * 397) ^ TextSize.GetHashCode();
hash = (hash * 397) ^ TextScaleX.GetHashCode();
hash = (hash * 397) ^ TextSkewX.GetHashCode();
hash = (hash * 397) ^ LcdRenderText.GetHashCode();
hash = (hash * 397) ^ SubpixelText.GetHashCode();
hash = (hash * 397) ^ FakeBoldText.GetHashCode();
return hash;
}
}
}

private sealed class PositionedTextCache
{
public PositionedTextCache(FontSignature signature, SkiaSharp.SKTextBlob textBlob)
{
Signature = signature;
TextBlob = textBlob;
}

public FontSignature Signature { get; }
public SkiaSharp.SKTextBlob TextBlob { get; }
}

private readonly ConcurrentDictionary<TypefaceKey, SkiaSharp.SKTypeface?> _typefaceCache = new();
private readonly ConcurrentDictionary<TypefaceKey, SkiaSharp.SKTypeface?> _resolvedTypefaceCache = new();
private readonly object _positionedTextCacheLock = new();
private ConditionalWeakTable<DrawTextBlobCanvasCommand, PositionedTextCache> _positionedTextCache = new();
private readonly List<WeakReference<SkiaSharp.SKTextBlob>> _positionedTextCacheRefs = new();
private IList<ITypefaceProvider>? _providerStateList;
private int _providerStateHash;
}
Loading
Loading