19 changed files with 575 additions and 648 deletions
@ -1,42 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Collections.Generic; |
|||
|
|||
namespace Avalonia.Media.Fonts |
|||
{ |
|||
/// <summary>
|
|||
/// Holds a quantity of <see cref="FontResource"/> that belongs to a specific <see cref="FontFamilyKey"/>
|
|||
/// </summary>
|
|||
internal class CachedFontFamily |
|||
{ |
|||
private readonly FontResourceCollection _fontResourceCollection; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="CachedFontFamily"/> class.
|
|||
/// </summary>
|
|||
/// <param name="key">The key.</param>
|
|||
/// <param name="fontResourceCollection">The font resource collection.</param>
|
|||
public CachedFontFamily(FontFamilyKey key, FontResourceCollection fontResourceCollection) |
|||
{ |
|||
Key = key; |
|||
_fontResourceCollection = fontResourceCollection; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the key.
|
|||
/// </summary>
|
|||
/// <value>
|
|||
/// The key.
|
|||
/// </value>
|
|||
public FontFamilyKey Key { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the font resources.
|
|||
/// </summary>
|
|||
/// <value>
|
|||
/// The font resources.
|
|||
/// </value>
|
|||
public IEnumerable<FontResource> FontResources => _fontResourceCollection.FontResources; |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Collections.ObjectModel; |
|||
using System.Linq; |
|||
|
|||
namespace Avalonia.Media.Fonts |
|||
{ |
|||
internal class FamilyNameCollection : IEnumerable<string> |
|||
{ |
|||
private readonly ReadOnlyCollection<string> _familyNames; |
|||
|
|||
public FamilyNameCollection(IEnumerable<string> familyNames) |
|||
{ |
|||
if (familyNames == null) throw new ArgumentNullException(nameof(familyNames)); |
|||
var names = new List<string>(familyNames); |
|||
if (names.Count == 0) throw new ArgumentException($"{nameof(familyNames)} must not be empty."); |
|||
_familyNames = new ReadOnlyCollection<string>(names); |
|||
PrimaryFamilyName = _familyNames.First(); |
|||
HasFallbacks = _familyNames.Count > 1; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the primary family name.
|
|||
/// </summary>
|
|||
/// <value>
|
|||
/// The primary family name.
|
|||
/// </value>
|
|||
public string PrimaryFamilyName { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether fallbacks are defined.
|
|||
/// </summary>
|
|||
/// <value>
|
|||
/// <c>true</c> if fallbacks are defined; otherwise, <c>false</c>.
|
|||
/// </value>
|
|||
public bool HasFallbacks { get; } |
|||
|
|||
public IEnumerator<string> GetEnumerator() |
|||
{ |
|||
return _familyNames.GetEnumerator(); |
|||
} |
|||
|
|||
IEnumerator IEnumerable.GetEnumerator() |
|||
{ |
|||
return GetEnumerator(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,35 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Collections.Concurrent; |
|||
|
|||
namespace Avalonia.Media.Fonts |
|||
{ |
|||
/// <summary>
|
|||
/// Caches all <see cref="CachedFontFamily"/> instances to reduce memory usage and speed up loading times of custom font families
|
|||
/// </summary>
|
|||
internal static class FontFamilyCache |
|||
{ |
|||
private static readonly ConcurrentDictionary<FontFamilyKey, CachedFontFamily> s_cachedFontFamilies = new ConcurrentDictionary<FontFamilyKey, CachedFontFamily>(); |
|||
|
|||
/// <summary>
|
|||
/// Gets the or add cached font family.
|
|||
/// </summary>
|
|||
/// <param name="key">The key.</param>
|
|||
/// <returns></returns>
|
|||
public static CachedFontFamily GetOrAddFontFamily(FontFamilyKey key) |
|||
{ |
|||
return s_cachedFontFamilies.GetOrAdd(key, CreateCachedFontFamily); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates the cached font family.
|
|||
/// </summary>
|
|||
/// <param name="fontFamilyKey">The font family key.</param>
|
|||
/// <returns></returns>
|
|||
private static CachedFontFamily CreateCachedFontFamily(FontFamilyKey fontFamilyKey) |
|||
{ |
|||
return new CachedFontFamily(fontFamilyKey, new FontResourceCollection(fontFamilyKey)); |
|||
} |
|||
} |
|||
} |
|||
@ -1,59 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
namespace Avalonia.Media.Fonts |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a collection of <see cref="FontResource"/> that is identified by a unique <see cref="FontFamilyKey"/>
|
|||
/// </summary>
|
|||
internal class FontResourceCollection |
|||
{ |
|||
private Dictionary<Uri, FontResource> _fontResources; |
|||
private readonly IFontResourceLoader _fontResourceLoader = new FontResourceLoader(); |
|||
|
|||
public FontResourceCollection(FontFamilyKey key) |
|||
{ |
|||
Key = key; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the key.
|
|||
/// </summary>
|
|||
/// <value>
|
|||
/// The key.
|
|||
/// </value>
|
|||
public FontFamilyKey Key { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the font resources.
|
|||
/// </summary>
|
|||
/// <value>
|
|||
/// The font resources.
|
|||
/// </value>
|
|||
public IEnumerable<FontResource> FontResources |
|||
{ |
|||
get |
|||
{ |
|||
if (_fontResources == null) |
|||
{ |
|||
_fontResources = CreateFontResources(); |
|||
} |
|||
|
|||
return _fontResources.Values; |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates the font resources.
|
|||
/// </summary>
|
|||
/// <returns></returns>
|
|||
private Dictionary<Uri, FontResource> CreateFontResources() |
|||
{ |
|||
return _fontResourceLoader.GetFontResources(Key).ToDictionary(x => x.Source); |
|||
} |
|||
} |
|||
} |
|||
@ -1,20 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Collections.Generic; |
|||
|
|||
namespace Avalonia.Media.Fonts |
|||
{ |
|||
/// <summary>
|
|||
/// Loads <see cref="FontResource"/> that can be identified by a given <see cref="FontFamilyKey"/>
|
|||
/// </summary>
|
|||
internal interface IFontResourceLoader |
|||
{ |
|||
/// <summary>
|
|||
/// Returns a quanity of <see cref="FontResource"/> that belongs to a given <see cref="FontFamilyKey"/>
|
|||
/// </summary>
|
|||
/// <param name="fontFamilyKey"></param>
|
|||
/// <returns></returns>
|
|||
IEnumerable<FontResource> GetFontResources(FontFamilyKey fontFamilyKey); |
|||
} |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
using System.Collections.Concurrent; |
|||
using Avalonia.Media; |
|||
using SkiaSharp; |
|||
|
|||
namespace Avalonia.Skia |
|||
{ |
|||
internal class SKTypefaceCollection |
|||
{ |
|||
struct FontKey |
|||
{ |
|||
public readonly string Name; |
|||
public readonly SKFontStyleSlant Slant; |
|||
public readonly SKFontStyleWeight Weight; |
|||
|
|||
public FontKey(string name, SKFontStyleWeight weight, SKFontStyleSlant slant) |
|||
{ |
|||
Name = name; |
|||
Slant = slant; |
|||
Weight = weight; |
|||
} |
|||
|
|||
public override int GetHashCode() |
|||
{ |
|||
int hash = 17; |
|||
hash = hash * 31 + Name.GetHashCode(); |
|||
hash = hash * 31 + (int)Slant; |
|||
hash = hash * 31 + (int)Weight; |
|||
|
|||
return hash; |
|||
} |
|||
|
|||
public override bool Equals(object other) |
|||
{ |
|||
return other is FontKey ? Equals((FontKey)other) : false; |
|||
} |
|||
|
|||
public bool Equals(FontKey other) |
|||
{ |
|||
return Name == other.Name && Slant == other.Slant && |
|||
Weight == other.Weight; |
|||
} |
|||
|
|||
// Equals and GetHashCode ommitted
|
|||
} |
|||
|
|||
private readonly ConcurrentDictionary<FontKey, SKTypeface> _cachedTypefaces = |
|||
new ConcurrentDictionary<FontKey, SKTypeface>(); |
|||
|
|||
public void AddTypeFace(SKTypeface typeface) |
|||
{ |
|||
var key = new FontKey(typeface.FamilyName, (SKFontStyleWeight)typeface.FontWeight, typeface.FontSlant); |
|||
|
|||
_cachedTypefaces.TryAdd(key, typeface); |
|||
} |
|||
|
|||
public SKTypeface GetTypeFace(Typeface typeface) |
|||
{ |
|||
SKFontStyleSlant skStyle = SKFontStyleSlant.Upright; |
|||
|
|||
switch (typeface.Style) |
|||
{ |
|||
case FontStyle.Italic: |
|||
skStyle = SKFontStyleSlant.Italic; |
|||
break; |
|||
|
|||
case FontStyle.Oblique: |
|||
skStyle = SKFontStyleSlant.Oblique; |
|||
break; |
|||
} |
|||
|
|||
var key = new FontKey(typeface.FontFamily.Name, (SKFontStyleWeight)typeface.Weight, skStyle); |
|||
|
|||
return _cachedTypefaces.TryGetValue(key, out var skTypeface) ? skTypeface : TypefaceCache.Default; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
using System.Collections.Concurrent; |
|||
using Avalonia.Media; |
|||
using Avalonia.Media.Fonts; |
|||
using Avalonia.Platform; |
|||
using SkiaSharp; |
|||
|
|||
namespace Avalonia.Skia |
|||
{ |
|||
internal static class SKTypefaceCollectionCache |
|||
{ |
|||
private static readonly ConcurrentDictionary<FontFamilyKey, SKTypefaceCollection> s_cachedCollections; |
|||
|
|||
static SKTypefaceCollectionCache() |
|||
{ |
|||
s_cachedCollections = new ConcurrentDictionary<FontFamilyKey, SKTypefaceCollection>(); |
|||
} |
|||
|
|||
public static SKTypefaceCollection GetOrAddTypefaceCollection(FontFamily fontFamily) |
|||
{ |
|||
return s_cachedCollections.GetOrAdd(fontFamily.Key, x => CreateCustomFontCollection(fontFamily)); |
|||
} |
|||
|
|||
private static SKTypefaceCollection CreateCustomFontCollection(FontFamily fontFamily) |
|||
{ |
|||
var resources = FontFamilyLoader.GetFontResources(fontFamily.Key); |
|||
|
|||
var typeFaceCollection = new SKTypefaceCollection(); |
|||
|
|||
var assetLoader = AvaloniaLocator.Current.GetService<IAssetLoader>(); |
|||
|
|||
foreach (var fontResource in resources) |
|||
{ |
|||
var stream = assetLoader.Open(fontResource.Source); |
|||
|
|||
var typeface = SKTypeface.FromStream(stream); |
|||
|
|||
typeFaceCollection.AddTypeFace(typeface); |
|||
} |
|||
|
|||
return typeFaceCollection; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
using SharpDX; |
|||
using SharpDX.DirectWrite; |
|||
|
|||
namespace Avalonia.Direct2D1.Media |
|||
{ |
|||
/// <summary>
|
|||
/// Resource FontFileEnumerator.
|
|||
/// </summary>
|
|||
public class DWriteResourceFontFileEnumerator : CallbackBase, FontFileEnumerator |
|||
{ |
|||
private readonly Factory _factory; |
|||
private readonly FontFileLoader _loader; |
|||
private readonly DataStream _keyStream; |
|||
private FontFile _currentFontFile; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DWriteResourceFontFileEnumerator"/> class.
|
|||
/// </summary>
|
|||
/// <param name="factory">The factory.</param>
|
|||
/// <param name="loader">The loader.</param>
|
|||
/// <param name="key">The key.</param>
|
|||
public DWriteResourceFontFileEnumerator(Factory factory, FontFileLoader loader, DataPointer key) |
|||
{ |
|||
_factory = factory; |
|||
_loader = loader; |
|||
_keyStream = new DataStream(key.Pointer, key.Size, true, false); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Advances to the next font file in the collection. When it is first created, the enumerator is positioned before the first element of the collection and the first call to MoveNext advances to the first file.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// the value TRUE if the enumerator advances to a file; otherwise, FALSE if the enumerator advances past the last file in the collection.
|
|||
/// </returns>
|
|||
/// <unmanaged>HRESULT IDWriteFontFileEnumerator::MoveNext([Out] BOOL* hasCurrentFile)</unmanaged>
|
|||
bool FontFileEnumerator.MoveNext() |
|||
{ |
|||
bool moveNext = _keyStream.RemainingLength != 0; |
|||
|
|||
if (!moveNext) return false; |
|||
|
|||
_currentFontFile?.Dispose(); |
|||
|
|||
_currentFontFile = new FontFile(_factory, _keyStream.PositionPointer, 4, _loader); |
|||
|
|||
_keyStream.Position += 4; |
|||
|
|||
return true; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets a reference to the current font file.
|
|||
/// </summary>
|
|||
/// <value></value>
|
|||
/// <returns>a reference to the newly created <see cref="SharpDX.DirectWrite.FontFile"/> object.</returns>
|
|||
/// <unmanaged>HRESULT IDWriteFontFileEnumerator::GetCurrentFontFile([Out] IDWriteFontFile** fontFile)</unmanaged>
|
|||
FontFile FontFileEnumerator.CurrentFontFile |
|||
{ |
|||
get |
|||
{ |
|||
((IUnknown)_currentFontFile).AddReference(); |
|||
|
|||
return _currentFontFile; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,85 @@ |
|||
using System; |
|||
using SharpDX; |
|||
using SharpDX.DirectWrite; |
|||
|
|||
namespace Avalonia.Direct2D1.Media |
|||
{ |
|||
/// <summary>
|
|||
/// This FontFileStream implem is reading data from a <see cref="DataStream"/>.
|
|||
/// </summary>
|
|||
public class DWriteResourceFontFileStream : CallbackBase, FontFileStream |
|||
{ |
|||
private readonly DataStream _stream; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DWriteResourceFontFileStream"/> class.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream.</param>
|
|||
public DWriteResourceFontFileStream(DataStream stream) |
|||
{ |
|||
_stream = stream; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads a fragment from a font file.
|
|||
/// </summary>
|
|||
/// <param name="fragmentStart">When this method returns, contains an address of a reference to the start of the font file fragment. This parameter is passed uninitialized.</param>
|
|||
/// <param name="fileOffset">The offset of the fragment, in bytes, from the beginning of the font file.</param>
|
|||
/// <param name="fragmentSize">The size of the file fragment, in bytes.</param>
|
|||
/// <param name="fragmentContext">When this method returns, contains the address of</param>
|
|||
/// <remarks>
|
|||
/// Note that ReadFileFragment implementations must check whether the requested font file fragment is within the file bounds. Otherwise, an error should be returned from ReadFileFragment. {{DirectWrite}} may invoke <see cref="SharpDX.DirectWrite.FontFileStream"/> methods on the same object from multiple threads simultaneously. Therefore, ReadFileFragment implementations that rely on internal mutable state must serialize access to such state across multiple threads. For example, an implementation that uses separate Seek and Read operations to read a file fragment must place the code block containing Seek and Read calls under a lock or a critical section.
|
|||
/// </remarks>
|
|||
/// <unmanaged>HRESULT IDWriteFontFileStream::ReadFileFragment([Out, Buffer] const void** fragmentStart,[None] __int64 fileOffset,[None] __int64 fragmentSize,[Out] void** fragmentContext)</unmanaged>
|
|||
void FontFileStream.ReadFileFragment(out IntPtr fragmentStart, long fileOffset, long fragmentSize, out IntPtr fragmentContext) |
|||
{ |
|||
lock (this) |
|||
{ |
|||
fragmentContext = IntPtr.Zero; |
|||
|
|||
_stream.Position = fileOffset; |
|||
|
|||
fragmentStart = _stream.PositionPointer; |
|||
|
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Releases a fragment from a file.
|
|||
/// </summary>
|
|||
/// <param name="fragmentContext">A reference to the client-defined context of a font fragment returned from {{ReadFileFragment}}.</param>
|
|||
/// <unmanaged>void IDWriteFontFileStream::ReleaseFileFragment([None] void* fragmentContext)</unmanaged>
|
|||
void FontFileStream.ReleaseFileFragment(IntPtr fragmentContext) |
|||
{ |
|||
// Nothing to release. No context are used
|
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Obtains the total size of a file.
|
|||
/// </summary>
|
|||
/// <returns>the total size of the file.</returns>
|
|||
/// <remarks>
|
|||
/// Implementing GetFileSize() for asynchronously loaded font files may require downloading the complete file contents. Therefore, this method should be used only for operations that either require a complete font file to be loaded (for example, copying a font file) or that need to make decisions based on the value of the file size (for example, validation against a persisted file size).
|
|||
/// </remarks>
|
|||
/// <unmanaged>HRESULT IDWriteFontFileStream::GetFileSize([Out] __int64* fileSize)</unmanaged>
|
|||
long FontFileStream.GetFileSize() |
|||
{ |
|||
return _stream.Length; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Obtains the last modified time of the file.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// the last modified time of the file in the format that represents the number of 100-nanosecond intervals since January 1, 1601 (UTC).
|
|||
/// </returns>
|
|||
/// <remarks>
|
|||
/// The "last modified time" is used by DirectWrite font selection algorithms to determine whether one font resource is more up to date than another one.
|
|||
/// </remarks>
|
|||
/// <unmanaged>HRESULT IDWriteFontFileStream::GetLastWriteTime([Out] __int64* lastWriteTime)</unmanaged>
|
|||
long FontFileStream.GetLastWriteTime() |
|||
{ |
|||
return 0; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,97 @@ |
|||
using System.Collections.Generic; |
|||
using Avalonia.Media.Fonts; |
|||
using Avalonia.Platform; |
|||
using SharpDX; |
|||
using SharpDX.DirectWrite; |
|||
|
|||
namespace Avalonia.Direct2D1.Media |
|||
{ |
|||
internal class DWriteResourceFontLoader : CallbackBase, FontCollectionLoader, FontFileLoader |
|||
{ |
|||
private readonly List<DWriteResourceFontFileStream> _fontStreams = new List<DWriteResourceFontFileStream>(); |
|||
private readonly List<DWriteResourceFontFileEnumerator> _enumerators = new List<DWriteResourceFontFileEnumerator>(); |
|||
private readonly DataStream _keyStream; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="DWriteResourceFontLoader"/> class.
|
|||
/// </summary>
|
|||
/// <param name="factory">The factory.</param>
|
|||
/// <param name="fontResources"></param>
|
|||
public DWriteResourceFontLoader(Factory factory, IEnumerable<FontResource> fontResources) |
|||
{ |
|||
var factory1 = factory; |
|||
|
|||
var assetLoader = AvaloniaLocator.Current.GetService<IAssetLoader>(); |
|||
|
|||
foreach (var font in fontResources) |
|||
{ |
|||
var resourceStream = assetLoader.Open(font.Source); |
|||
|
|||
var dataStream = new DataStream((int)resourceStream.Length, true, true); |
|||
|
|||
resourceStream.CopyTo(dataStream); |
|||
|
|||
dataStream.Position = 0; |
|||
|
|||
_fontStreams.Add(new DWriteResourceFontFileStream(dataStream)); |
|||
} |
|||
|
|||
// Build a Key storage that stores the index of the font
|
|||
_keyStream = new DataStream(sizeof(int) * _fontStreams.Count, true, true); |
|||
|
|||
for (int i = 0; i < _fontStreams.Count; i++) |
|||
{ |
|||
_keyStream.Write(i); |
|||
} |
|||
|
|||
_keyStream.Position = 0; |
|||
|
|||
// Register the
|
|||
factory1.RegisterFontFileLoader(this); |
|||
factory1.RegisterFontCollectionLoader(this); |
|||
} |
|||
|
|||
|
|||
/// <summary>
|
|||
/// Gets the key used to identify the FontCollection as well as storing index for fonts.
|
|||
/// </summary>
|
|||
/// <value>The key.</value>
|
|||
public DataStream Key => _keyStream; |
|||
|
|||
/// <summary>
|
|||
/// Creates a font file enumerator object that encapsulates a collection of font files. The font system calls back to this interface to create a font collection.
|
|||
/// </summary>
|
|||
/// <param name="factory">Pointer to the <see cref="SharpDX.DirectWrite.Factory"/> object that was used to create the current font collection.</param>
|
|||
/// <param name="collectionKey">A font collection key that uniquely identifies the collection of font files within the scope of the font collection loader being used. The buffer allocated for this key must be at least the size, in bytes, specified by collectionKeySize.</param>
|
|||
/// <returns>
|
|||
/// a reference to the newly created font file enumerator.
|
|||
/// </returns>
|
|||
/// <unmanaged>HRESULT IDWriteFontCollectionLoader::CreateEnumeratorFromKey([None] IDWriteFactory* factory,[In, Buffer] const void* collectionKey,[None] int collectionKeySize,[Out] IDWriteFontFileEnumerator** fontFileEnumerator)</unmanaged>
|
|||
FontFileEnumerator FontCollectionLoader.CreateEnumeratorFromKey(Factory factory, DataPointer collectionKey) |
|||
{ |
|||
var enumerator = new DWriteResourceFontFileEnumerator(factory, this, collectionKey); |
|||
|
|||
_enumerators.Add(enumerator); |
|||
|
|||
return enumerator; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Creates a font file stream object that encapsulates an open file resource.
|
|||
/// </summary>
|
|||
/// <param name="fontFileReferenceKey">A reference to a font file reference key that uniquely identifies the font file resource within the scope of the font loader being used. The buffer allocated for this key must at least be the size, in bytes, specified by fontFileReferenceKeySize.</param>
|
|||
/// <returns>
|
|||
/// a reference to the newly created <see cref="SharpDX.DirectWrite.FontFileStream"/> object.
|
|||
/// </returns>
|
|||
/// <remarks>
|
|||
/// The resource is closed when the last reference to fontFileStream is released.
|
|||
/// </remarks>
|
|||
/// <unmanaged>HRESULT IDWriteFontFileLoader::CreateStreamFromKey([In, Buffer] const void* fontFileReferenceKey,[None] int fontFileReferenceKeySize,[Out] IDWriteFontFileStream** fontFileStream)</unmanaged>
|
|||
FontFileStream FontFileLoader.CreateStreamFromKey(DataPointer fontFileReferenceKey) |
|||
{ |
|||
var index = SharpDX.Utilities.Read<int>(fontFileReferenceKey.Pointer); |
|||
|
|||
return _fontStreams[index]; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
using System.Collections.Concurrent; |
|||
using Avalonia.Media; |
|||
using Avalonia.Media.Fonts; |
|||
|
|||
namespace Avalonia.Direct2D1.Media |
|||
{ |
|||
internal static class Direct2D1FontCollectionCache |
|||
{ |
|||
private static readonly ConcurrentDictionary<FontFamilyKey, SharpDX.DirectWrite.FontCollection> s_cachedCollections; |
|||
private static readonly SharpDX.DirectWrite.Factory s_factory; |
|||
private static readonly SharpDX.DirectWrite.FontCollection s_installedFontCollection; |
|||
|
|||
static Direct2D1FontCollectionCache() |
|||
{ |
|||
s_cachedCollections = new ConcurrentDictionary<FontFamilyKey, SharpDX.DirectWrite.FontCollection>(); |
|||
s_factory = AvaloniaLocator.Current.GetService<SharpDX.DirectWrite.Factory>(); |
|||
s_installedFontCollection = s_factory.GetSystemFontCollection(false); |
|||
} |
|||
|
|||
public static SharpDX.DirectWrite.FontCollection GetOrAddFontCollection(FontFamily fontFamily) |
|||
{ |
|||
return fontFamily.Key == null ? s_installedFontCollection : s_cachedCollections.GetOrAdd(fontFamily.Key, CreateFontCollection); |
|||
} |
|||
|
|||
private static SharpDX.DirectWrite.FontCollection CreateFontCollection(FontFamilyKey key) |
|||
{ |
|||
var resources = FontFamilyLoader.GetFontResources(key); |
|||
|
|||
var fontLoader = new DWriteResourceFontLoader(s_factory, resources); |
|||
|
|||
return new SharpDX.DirectWrite.FontCollection(s_factory, fontLoader, fontLoader.Key); |
|||
} |
|||
|
|||
public static SharpDX.DirectWrite.TextFormat GetTextFormat(Typeface typeface) |
|||
{ |
|||
var fontFamily = typeface.FontFamily; |
|||
var fontCollection = GetOrAddFontCollection(fontFamily); |
|||
var fontFamilyName = FontFamily.Default.Name; |
|||
|
|||
//Should this be cached?
|
|||
foreach (var familyName in fontFamily.FamilyNames) |
|||
{ |
|||
if (!fontCollection.FindFamilyName(familyName, out _)) continue; |
|||
fontFamilyName = familyName; |
|||
break; |
|||
} |
|||
|
|||
return new SharpDX.DirectWrite.TextFormat(s_factory, fontFamilyName, fontCollection, (SharpDX.DirectWrite.FontWeight)typeface.Weight, |
|||
(SharpDX.DirectWrite.FontStyle)typeface.Style, SharpDX.DirectWrite.FontStretch.Normal, (float)typeface.FontSize); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue