Skip to content
Prev Previous commit
Next Next commit
#2123 Add virtual method GetTypeface to allow overriding typeface ret…
…rieval
  • Loading branch information
mennowo committed Feb 15, 2025
commit 5821f8f92797e61180a4abeaf8f32570b0c91021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.

- Example to demonstrate how to create vertical BarSeries
- Add a public method `AddTypeface` to `SkiaRenderContext` to allow manually adding typefaces to the cache

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is obsolete now, otherwise this looks good to me now!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, I can remove it later this week.

- Add a virtual method `GetTypeface` to `SkiaRenderContext` to allow overriding retrieving typefaces

## [2.2.0] - 2024-09-03

Expand Down
72 changes: 43 additions & 29 deletions Source/OxyPlot.SkiaSharp/SkiaRenderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace OxyPlot.SkiaSharp
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed?

@mennowo mennowo Feb 19, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I just commited these changes. Could it be merged like this? It would be nice if this makes it into a new release so I can switch back to using the nuget package instead of custom built dll. However, there is no rush. Also, I'm happy to add additional feedback if more changes are needed.

using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -498,6 +499,45 @@ public bool AddTypeface(string fontFamily, double fontWeight, SKTypeface typefac
return false;
}

/// <summary>
///
/// </summary>
/// <param name="fontDescriptor">Descriptor for the font to create a typeface for</param>
/// <returns>The typeface for the font descriptor</returns>
public virtual SKTypeface GetTypeface(FontDescriptor fontDescriptor)
{
if (!this.typefaceCache.TryGetValue(fontDescriptor, out var typeface))
{
typeface = SKTypeface.FromFamilyName(fontDescriptor.FontFamily, new SKFontStyle((int)fontDescriptor.FontWeight, (int)SKFontStyleWidth.Normal, SKFontStyleSlant.Upright));

if (typeface.FamilyName != fontDescriptor.FontFamily) // requested font not found or is WASM
{
try
{
var assembly = Assembly.GetEntryAssembly(); // the executing program (the GUI Project (WPF, WASM, ...))
var weight = this._fontWeights.ContainsKey((int)fontDescriptor.FontWeight) ? this._fontWeights[(int)fontDescriptor.FontWeight] : "Regular";
var filename = $"{fontDescriptor.FontFamily}-{weight}.ttf".ToLower();
Debug.WriteLine($"Load Font {filename}");

var matches = assembly!.GetManifestResourceNames().Where(item => item.ToLower().EndsWith(filename));
if (!matches.Any()) matches = assembly!.GetManifestResourceNames().Where(item => item.ToLower().EndsWith(fontDescriptor.FontFamily + ".ttf"));
foreach (var item in matches)
{
var s = assembly.GetManifestResourceStream(item);
typeface = SKTypeface.FromStream(s);
}
}
catch
{
Debug.WriteLine($"Requested Font {fontDescriptor.FontFamily} could not be found, falling back to {typeface.FamilyName}");
}
}
this.typefaceCache.Add(fontDescriptor, typeface);
}

return typeface;
}

/// <summary>
/// Disposes managed resources.
/// </summary>
Expand Down Expand Up @@ -887,34 +927,8 @@ private SKPaint GetStrokePaint(OxyColor strokeColor, double strokeThickness, Edg
private SKPaint GetTextPaint(string fontFamily, double fontSize, double fontWeight, out SKShaper shaper)
{
var fontDescriptor = new FontDescriptor(fontFamily, fontWeight);
if (!this.typefaceCache.TryGetValue(fontDescriptor, out var typeface))
{
typeface = SKTypeface.FromFamilyName(fontFamily, new SKFontStyle((int)fontWeight, (int)SKFontStyleWidth.Normal, SKFontStyleSlant.Upright));

if (typeface.FamilyName != fontFamily) // requested font not found or is WASM
{
try
{
var assembly = Assembly.GetEntryAssembly(); // the executing program (the GUI Project (WPF, WASM, ...))
var weight = (_fontWeights.ContainsKey((int)fontWeight) ? _fontWeights[(int)fontWeight] : "Regular");
var filename = $"{fontFamily}-{weight}.ttf".ToLower();
Debug.WriteLine($"Load Font {filename}");

var matches = assembly!.GetManifestResourceNames().Where(item => item.ToLower().EndsWith(filename));
if (!matches.Any()) matches = assembly!.GetManifestResourceNames().Where(item => item.ToLower().EndsWith(fontFamily + ".ttf"));
foreach (var item in matches)
{
var s = assembly.GetManifestResourceStream(item);
typeface = SKTypeface.FromStream(s);
}
}
catch
{
Debug.WriteLine($"Requested Font {fontFamily} could not be found, falling back to {typeface.FamilyName}");
}
}
this.typefaceCache.Add(fontDescriptor, typeface);
}

var typeface = this.GetTypeface(fontDescriptor);

if (this.UseTextShaping)
{
Expand Down Expand Up @@ -988,7 +1002,7 @@ private bool ShouldUseAntiAliasing(EdgeRenderingMode edgeRenderingMode)
/// <summary>
/// Represents a font description.
/// </summary>
private struct FontDescriptor
public struct FontDescriptor
{
/// <summary>
/// Initializes a new instance of the <see cref="FontDescriptor"/> struct.
Expand Down