Browse Source

FontFamily fallback

repros/custom-fonts-pr
Benedikt Schroeder 8 years ago
parent
commit
01444ccfee
  1. 92
      src/Avalonia.Visuals/Media/FontFamily.cs
  2. 42
      src/Avalonia.Visuals/Media/Fonts/CachedFontFamily.cs
  3. 52
      src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs
  4. 35
      src/Avalonia.Visuals/Media/Fonts/FontFamilyCache.cs
  5. 4
      src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs
  6. 52
      src/Avalonia.Visuals/Media/Fonts/FontFamilyLoader.cs
  7. 12
      src/Avalonia.Visuals/Media/Fonts/FontResource.cs
  8. 59
      src/Avalonia.Visuals/Media/Fonts/FontResourceCollection.cs
  9. 20
      src/Avalonia.Visuals/Media/Fonts/IFontResourceLoader.cs
  10. 30
      src/Skia/Avalonia.Skia/FormattedTextImpl.cs
  11. 76
      src/Skia/Avalonia.Skia/SKTypefaceCollection.cs
  12. 43
      src/Skia/Avalonia.Skia/SKTypefaceCollectionCache.cs
  13. 121
      src/Skia/Avalonia.Skia/TypefaceCache.cs
  14. 2
      src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs
  15. 67
      src/Windows/Avalonia.Direct2D1/Media/DWriteResourceFontFileEnumerator.cs
  16. 85
      src/Windows/Avalonia.Direct2D1/Media/DWriteResourceFontFileStream.cs
  17. 97
      src/Windows/Avalonia.Direct2D1/Media/DWriteResourceFontLoader.cs
  18. 52
      src/Windows/Avalonia.Direct2D1/Media/Direct2D1FontCollectionCache.cs
  19. 282
      src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs

92
src/Avalonia.Visuals/Media/FontFamily.cs

@ -3,57 +3,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media.Fonts;
namespace Avalonia.Media
{
public class FontFamily
{
internal static FontFamily Default = new FontFamily("Courier New");
/// <summary>
/// Initializes a new instance of the <see cref="FontFamily"/> class.
/// Initializes a new instance of the <see cref="T:Avalonia.Media.FontFamily" /> class.
/// </summary>
/// <param name="name">The name.</param>
/// <exception cref="ArgumentNullException">name</exception>
public FontFamily(string name = "Courier New")
/// <param name="name">The name of the <see cref="FontFamily"/>.</param>
/// <exception cref="T:System.ArgumentNullException">name</exception>
public FontFamily(string name)
{
if (name == null) throw new ArgumentNullException(nameof(name));
FamilyNames = new FamilyNameList(name);
if (name == null) throw new ArgumentNullException();
FamilyNames = new FamilyNameCollection(new[] { name });
}
/// <summary>
/// Initializes a new instance of the <see cref="T:Avalonia.Media.FontFamily" /> class.
/// </summary>
/// <param name="names">The names of the <see cref="FontFamily"/>.</param>
/// <exception cref="T:System.ArgumentNullException">name</exception>
public FontFamily(IEnumerable<string> names)
{
if (names == null) throw new ArgumentNullException(nameof(names));
FamilyNames = new FamilyNameList(names);
if (names == null) throw new ArgumentNullException();
FamilyNames = new FamilyNameCollection(names);
}
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:Avalonia.Media.FontFamily" /> class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="source">The source.</param>
/// <param name="name">The name of the <see cref="FontFamily"/>.</param>
/// <param name="source">The source of font resources.</param>
public FontFamily(string name, Uri source) : this(name)
{
Key = new FontFamilyKey(source);
}
/// <summary>
/// Gets the name.
/// Gets the name of the font family.
/// </summary>
/// <value>
/// The name.
/// The name of the font family.
/// </value>
public string Name => FamilyNames.FirstFamilyName;
public string Name => FamilyNames.PrimaryFamilyName;
/// <summary>
/// Gets the key.
/// Gets the family names.
/// </summary>
/// <value>
/// The key.
/// The family familyNames.
/// </value>
internal FontFamilyKey Key { get; }
internal FamilyNameCollection FamilyNames
{
get;
}
internal FamilyNameList FamilyNames { get; }
/// <summary>
/// Gets the key for associated resources.
/// </summary>
/// <value>
/// The family familyNames.
/// </value>
internal FontFamilyKey Key { get; }
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
@ -63,34 +79,13 @@ namespace Avalonia.Media
/// </returns>
public override string ToString()
{
if (Key != null)
{
return Key + "#" + Name;
}
return Name;
}
internal class FamilyNameList : List<string>
{
public FamilyNameList(string familyName)
{
Add(familyName);
FirstFamilyName = familyName;
}
public FamilyNameList(IEnumerable<string> familyNames) : base(familyNames)
{
FirstFamilyName = this[0];
}
public string FirstFamilyName { get; }
}
/// <summary>
/// Parses a <see cref="FontFamily"/> string.
/// Parses a <see cref="T:Avalonia.Media.FontFamily"/> string.
/// </summary>
/// <param name="s">The <see cref="FontFamily"/> string.</param>
/// <param name="s">The <see cref="T:Avalonia.Media.FontFamily"/> string.</param>
/// <returns></returns>
/// <exception cref="ArgumentException">
/// Specified family is not supported.
@ -99,19 +94,20 @@ namespace Avalonia.Media
{
if (string.IsNullOrEmpty(s)) throw new ArgumentException("Specified family is not supported.");
var fontFamilyExpression = s.Split('#');
var segments = s.Split('#');
switch (fontFamilyExpression.Length)
switch (segments.Length)
{
case 1:
{
var familyNames = fontFamilyExpression[0].Split(';');
return new FontFamily(familyNames);
{
var names = segments[0].Split(',')
.Select(x => x.Trim())
.Where(x => !string.IsNullOrWhiteSpace(x));
return new FontFamily(names);
}
case 2:
{
var source = new Uri(fontFamilyExpression[0], UriKind.RelativeOrAbsolute);
return new FontFamily(fontFamilyExpression[1], source);
return new FontFamily(segments[1], new Uri(segments[0], UriKind.RelativeOrAbsolute));
}
default:
{

42
src/Avalonia.Visuals/Media/Fonts/CachedFontFamily.cs

@ -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;
}
}

52
src/Avalonia.Visuals/Media/Fonts/FamilyNameCollection.cs

@ -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();
}
}
}

35
src/Avalonia.Visuals/Media/Fonts/FontFamilyCache.cs

@ -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));
}
}
}

4
src/Avalonia.Visuals/Media/Fonts/FontFamilyKey.cs

@ -7,7 +7,7 @@ using System.Linq;
namespace Avalonia.Media.Fonts
{
/// <summary>
/// Unique idetifier for a quantity of <see cref="FontResource"/> that is stored at a given location.
/// Represents an idetifier for a <see cref="FontFamily"/>
/// </summary>
internal class FontFamilyKey
{
@ -16,7 +16,7 @@ namespace Avalonia.Media.Fonts
/// </summary>
/// <param name="source"></param>
public FontFamilyKey(Uri source)
{
{
if (source.AbsolutePath.Contains(".ttf"))
{
var filePathWithoutExtension = source.AbsolutePath.Replace(".ttf", "");

52
src/Avalonia.Visuals/Media/Fonts/FontResourceLoader.cs → src/Avalonia.Visuals/Media/Fonts/FontFamilyLoader.cs

@ -9,40 +9,19 @@ using Avalonia.Platform;
namespace Avalonia.Media.Fonts
{
/// <inheritdoc />
/// <summary>
/// Implementation of <see cref="T:Avalonia.Media.Fonts.IFontResourceLoader" />
/// </summary>
internal class FontResourceLoader : IFontResourceLoader
internal static class FontFamilyLoader
{
private static readonly Dictionary<string, AssemblyDescriptor> s_assemblyNameCache
= new Dictionary<string, AssemblyDescriptor>();
private readonly AssemblyDescriptor _defaultAssembly;
private static readonly AssemblyDescriptor s_defaultAssembly;
/// <summary>
/// Initializes a new instance of the <see cref="FontResourceLoader"/> class.
/// </summary>
/// <param name="assembly">The default assembly.</param>
public FontResourceLoader(Assembly assembly = null)
static FontFamilyLoader()
{
if (assembly == null)
{
assembly = Assembly.GetEntryAssembly();
}
if (assembly != null)
{
_defaultAssembly = new AssemblyDescriptor(assembly);
}
s_defaultAssembly = new AssemblyDescriptor(Assembly.GetEntryAssembly());
}
/// <inheritdoc />
/// <summary>
/// Returns a quanity of <see cref="T:Avalonia.Media.Fonts.FontResource" /> that belongs to a given <see cref="T:Avalonia.Media.Fonts.FontFamilyKey" />
/// </summary>
/// <param name="fontFamilyKey"></param>
/// <returns></returns>
public IEnumerable<FontResource> GetFontResources(FontFamilyKey fontFamilyKey)
public static IEnumerable<FontResource> GetFontResources(FontFamilyKey fontFamilyKey)
{
return fontFamilyKey.FileName != null
? GetFontResourcesByFileName(fontFamilyKey.Location, fontFamilyKey.FileName)
@ -54,7 +33,7 @@ namespace Avalonia.Media.Fonts
/// </summary>
/// <param name="location"></param>
/// <returns></returns>
private IEnumerable<FontResource> GetFontResourcesByLocation(Uri location)
private static IEnumerable<FontResource> GetFontResourcesByLocation(Uri location)
{
var assembly = GetAssembly(location);
@ -64,7 +43,7 @@ namespace Avalonia.Media.Fonts
var matchingResources = assembly.Resources.Where(x => x.Contains(locationPath));
return matchingResources.Select(x => new FontResource(GetResourceUri(x, assembly.Name)));
return matchingResources.Select(x => CreateResource(GetResourceUri(x, assembly.Name)));
}
/// <summary>
@ -74,7 +53,7 @@ namespace Avalonia.Media.Fonts
/// <param name="location"></param>
/// <param name="fileName"></param>
/// <returns></returns>
private IEnumerable<FontResource> GetFontResourcesByFileName(Uri location, string fileName)
private static IEnumerable<FontResource> GetFontResourcesByFileName(Uri location, string fileName)
{
var assembly = GetAssembly(location);
@ -84,7 +63,12 @@ namespace Avalonia.Media.Fonts
var matchingResources = assembly.Resources.Where(x => x.Contains(compareTo));
return matchingResources.Select(x => new FontResource(GetResourceUri(x, assembly.Name)));
return matchingResources.Select(x => CreateResource(GetResourceUri(x, assembly.Name)));
}
private static FontResource CreateResource(Uri source)
{
return new FontResource(source);
}
/// <summary>
@ -103,13 +87,13 @@ namespace Avalonia.Media.Fonts
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
private AssemblyDescriptor GetAssembly(Uri uri)
private static AssemblyDescriptor GetAssembly(Uri uri)
{
if (uri == null) return null;
var parameters = ParseParameters(uri);
return parameters.TryGetValue("assembly", out var assemblyName) ? GetAssembly(assemblyName) : _defaultAssembly;
return parameters.TryGetValue("assembly", out var assemblyName) ? GetAssembly(assemblyName) : s_defaultAssembly;
}
/// <summary>
@ -120,11 +104,11 @@ namespace Avalonia.Media.Fonts
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private AssemblyDescriptor GetAssembly(string name)
private static AssemblyDescriptor GetAssembly(string name)
{
if (name == null)
{
return _defaultAssembly;
return s_defaultAssembly;
}
if (!s_assemblyNameCache.TryGetValue(name, out var rv))

12
src/Avalonia.Visuals/Media/Fonts/FontResource.cs

@ -5,19 +5,23 @@ using System;
namespace Avalonia.Media.Fonts
{
/// <summary>
/// Represents a font resource
/// </summary>
internal class FontResource
{
/// <summary>
/// Initializes a new instance of the <see cref="FontResource"/> class.
/// </summary>
/// <param name="source">The source.</param>
public FontResource(Uri source)
{
Source = source;
}
/// <summary>
/// Source of the font resource.
/// Gets the source.
/// </summary>
/// <value>
/// The source.
/// </value>
public Uri Source { get; }
}
}

59
src/Avalonia.Visuals/Media/Fonts/FontResourceCollection.cs

@ -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);
}
}
}

20
src/Avalonia.Visuals/Media/Fonts/IFontResourceLoader.cs

@ -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);
}
}

30
src/Skia/Avalonia.Skia/FormattedTextImpl.cs

@ -25,7 +25,7 @@ namespace Avalonia.Skia
// Replace 0 characters with zero-width spaces (200B)
Text = Text.Replace((char)0, (char)0x200B);
SKTypeface skiaTypeface;
SKTypeface skiaTypeface = TypefaceCache.Default;
if (typeface.FontFamily.Key != null)
{
@ -34,11 +34,25 @@ namespace Avalonia.Skia
}
else
{
skiaTypeface = TypefaceCache.GetTypeface(
typeface.FontFamily,
typeface.Style,
typeface.Weight);
}
if (typeface.FontFamily.FamilyNames.HasFallbacks)
{
foreach (var familyName in typeface.FontFamily.FamilyNames)
{
skiaTypeface = TypefaceCache.GetTypeface(
familyName,
typeface.Style,
typeface.Weight);
if (skiaTypeface != TypefaceCache.Default) break;
}
}
else
{
skiaTypeface = TypefaceCache.GetTypeface(
typeface.FontFamily.Name,
typeface.Style,
typeface.Weight);
}
}
_paint = new SKPaint();
@ -46,7 +60,7 @@ namespace Avalonia.Skia
//Paint.TextEncoding = SKTextEncoding.Utf8;
_paint.TextEncoding = SKTextEncoding.Utf16;
_paint.IsStroke = false;
_paint.IsAntialias = true;
_paint.IsAntialias = true;
_paint.LcdRenderText = true;
_paint.SubpixelText = true;
_paint.Typeface = skiaTypeface;
@ -256,7 +270,7 @@ namespace Avalonia.Skia
subStr = Text.Substring(i, len);
ApplyWrapperTo(ref currentPaint, currentWrapper, ref currd, paint, canUseLcdRendering);
canvas.DrawText(subStr, currX, origin.Y + line.Top + _lineOffset, paint);
i += len;

76
src/Skia/Avalonia.Skia/SKTypefaceCollection.cs

@ -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;
}
}
}

43
src/Skia/Avalonia.Skia/SKTypefaceCollectionCache.cs

@ -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;
}
}
}

121
src/Skia/Avalonia.Skia/TypefaceCache.cs

@ -1,15 +1,13 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Media.Fonts;
using Avalonia.Platform;
using SkiaSharp;
namespace Avalonia.Skia
{
static class TypefaceCache
{
public static SKTypeface Default = SKTypeface.FromFamilyName(FontFamily.Default.Name);
static readonly Dictionary<string, Dictionary<FontKey, SKTypeface>> Cache = new Dictionary<string, Dictionary<FontKey, SKTypeface>>();
struct FontKey
@ -46,9 +44,9 @@ namespace Avalonia.Skia
// Equals and GetHashCode ommitted
}
static SKTypeface GetTypeface(FontFamily fontFamily, FontKey key)
private static SKTypeface GetTypeface(string name, FontKey key)
{
var familyKey = fontFamily.Name;
var familyKey = name;
if (!Cache.TryGetValue(familyKey, out var entry))
{
@ -59,9 +57,9 @@ namespace Avalonia.Skia
{
typeface = SKTypeface.FromFamilyName(familyKey, key.Weight, SKFontStyleWidth.Normal, key.Slant);
if (typeface == null)
if (typeface.FamilyName != name)
{
typeface = SKTypeface.FromFamilyName(null);
typeface = Default;
}
entry[key] = typeface;
@ -70,7 +68,7 @@ namespace Avalonia.Skia
return typeface;
}
public static SKTypeface GetTypeface(FontFamily fontFamily, FontStyle style, FontWeight weight)
public static SKTypeface GetTypeface(string name, FontStyle style, FontWeight weight)
{
SKFontStyleSlant skStyle = SKFontStyleSlant.Upright;
@ -85,113 +83,8 @@ namespace Avalonia.Skia
break;
}
return GetTypeface(fontFamily, new FontKey((SKFontStyleWeight)weight, skStyle));
return GetTypeface(name, new FontKey((SKFontStyleWeight)weight, skStyle));
}
}
internal class SKTypefaceCollection
{
private static readonly SKTypeface s_defaultTypeface = SKTypeface.FromFamilyName(null);
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(FontFamily fontFamily, SKTypeface typeface)
{
var key = new FontKey(fontFamily.Name, (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 : s_defaultTypeface;
}
}
internal static class SKTypefaceCollectionCache
{
private static readonly ConcurrentDictionary<FontFamilyKey, SKTypefaceCollection> 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 cachedFontFamily = FontFamilyCache.GetOrAddFontFamily(fontFamily.Key);
var typeFaceCollection = new SKTypefaceCollection();
if (!cachedFontFamily.FontResources.Any()) return typeFaceCollection;
var assetLoader = AvaloniaLocator.Current.GetService<IAssetLoader>();
foreach (var fontResource in cachedFontFamily.FontResources)
{
var stream = assetLoader.Open(fontResource.Source);
var typeface = SKTypeface.FromStream(stream);
typeFaceCollection.AddTypeFace(fontFamily, typeface);
}
return typeFaceCollection;
}
}
}

2
src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs

@ -117,7 +117,7 @@ namespace Avalonia.Direct2D1
}
public IFormattedTextImpl CreateFormattedText(
string text,
string text,
Typeface typeface,
TextAlignment textAlignment,
TextWrapping wrapping,

67
src/Windows/Avalonia.Direct2D1/Media/DWriteResourceFontFileEnumerator.cs

@ -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;
}
}
}
}

85
src/Windows/Avalonia.Direct2D1/Media/DWriteResourceFontFileStream.cs

@ -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;
}
}
}

97
src/Windows/Avalonia.Direct2D1/Media/DWriteResourceFontLoader.cs

@ -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];
}
}
}

52
src/Windows/Avalonia.Direct2D1/Media/Direct2D1FontCollectionCache.cs

@ -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);
}
}
}

282
src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs

@ -1,14 +1,10 @@
// 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.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Media.Fonts;
using Avalonia.Platform;
using SharpDX;
using DWrite = SharpDX.DirectWrite;
namespace Avalonia.Direct2D1.Media
@ -27,31 +23,7 @@ namespace Avalonia.Direct2D1.Media
var factory = AvaloniaLocator.Current.GetService<DWrite.Factory>();
DWrite.TextFormat textFormat;
if (typeface.FontFamily.Key != null)
{
var fontCollection = Direct2D1CustomFontCollectionCache.GetOrAddCustomFontCollection(typeface.FontFamily, factory);
textFormat = new DWrite.TextFormat(
factory,
typeface.FontFamily.Name,
fontCollection,
(DWrite.FontWeight)typeface.Weight,
(DWrite.FontStyle)typeface.Style,
DWrite.FontStretch.Normal,
(float)typeface.FontSize);
}
else
{
textFormat = new DWrite.TextFormat(
factory,
typeface.FontFamily.Name,
(DWrite.FontWeight)typeface.Weight,
(DWrite.FontStyle)typeface.Style,
DWrite.FontStretch.Normal,
(float)typeface.FontSize);
}
var textFormat = Direct2D1FontCollectionCache.GetTextFormat(typeface);
textFormat.WordWrapping =
wrapping == TextWrapping.Wrap ? DWrite.WordWrapping.Wrap : DWrite.WordWrapping.NoWrap;
@ -144,256 +116,4 @@ namespace Avalonia.Direct2D1.Media
return new Size(width, TextLayout.Metrics.Height);
}
}
internal static class Direct2D1CustomFontCollectionCache
{
private static readonly ConcurrentDictionary<FontFamilyKey, DWrite.FontCollection> s_cachedFonts =
new ConcurrentDictionary<FontFamilyKey, DWrite.FontCollection>();
public static DWrite.FontCollection GetOrAddCustomFontCollection(FontFamily fontFamily, DWrite.Factory factory)
{
return s_cachedFonts.GetOrAdd(fontFamily.Key, x => CreateCustomFontCollection(x, factory));
}
private static DWrite.FontCollection CreateCustomFontCollection(FontFamilyKey key, DWrite.Factory factory)
{
var fontFamily = FontFamilyCache.GetOrAddFontFamily(key);
var fontLoader = new ResourceFontLoader(factory, fontFamily.FontResources);
return new DWrite.FontCollection(factory, fontLoader, fontLoader.Key);
}
}
internal class ResourceFontLoader : CallbackBase, DWrite.FontCollectionLoader, DWrite.FontFileLoader
{
private readonly List<ResourceFontFileStream> _fontStreams = new List<ResourceFontFileStream>();
private readonly List<ResourceFontFileEnumerator> _enumerators = new List<ResourceFontFileEnumerator>();
private readonly DataStream _keyStream;
/// <summary>
/// Initializes a new instance of the <see cref="ResourceFontLoader"/> class.
/// </summary>
/// <param name="factory">The factory.</param>
/// <param name="fontResources"></param>
public ResourceFontLoader(DWrite.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 ResourceFontFileStream(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>
DWrite.FontFileEnumerator DWrite.FontCollectionLoader.CreateEnumeratorFromKey(DWrite.Factory factory, DataPointer collectionKey)
{
var enumerator = new ResourceFontFileEnumerator(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>
DWrite.FontFileStream DWrite.FontFileLoader.CreateStreamFromKey(DataPointer fontFileReferenceKey)
{
var index = SharpDX.Utilities.Read<int>(fontFileReferenceKey.Pointer);
return _fontStreams[index];
}
}
/// <summary>
/// This FontFileStream implem is reading data from a <see cref="DataStream"/>.
/// </summary>
public class ResourceFontFileStream : CallbackBase, DWrite.FontFileStream
{
private readonly DataStream _stream;
/// <summary>
/// Initializes a new instance of the <see cref="ResourceFontFileStream"/> class.
/// </summary>
/// <param name="stream">The stream.</param>
public ResourceFontFileStream(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 DWrite.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 DWrite.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 DWrite.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 DWrite.FontFileStream.GetLastWriteTime()
{
return 0;
}
}
/// <summary>
/// Resource FontFileEnumerator.
/// </summary>
public class ResourceFontFileEnumerator : CallbackBase, DWrite.FontFileEnumerator
{
private DWrite.Factory _factory;
private DWrite.FontFileLoader _loader;
private DataStream keyStream;
private DWrite.FontFile _currentFontFile;
/// <summary>
/// Initializes a new instance of the <see cref="ResourceFontFileEnumerator"/> class.
/// </summary>
/// <param name="factory">The factory.</param>
/// <param name="loader">The loader.</param>
/// <param name="key">The key.</param>
public ResourceFontFileEnumerator(DWrite.Factory factory, DWrite.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 DWrite.FontFileEnumerator.MoveNext()
{
bool moveNext = keyStream.RemainingLength != 0;
if (!moveNext) return false;
_currentFontFile?.Dispose();
_currentFontFile = new DWrite.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>
DWrite.FontFile DWrite.FontFileEnumerator.CurrentFontFile
{
get
{
((IUnknown)_currentFontFile).AddReference();
return _currentFontFile;
}
}
}
}

Loading…
Cancel
Save