Repository navigation
Allow loading typefaces into SkiaRenderContext manually #2123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
15ed822
03a70c6
9fac088
562fd51
31ca1ca
53ac016
5821f8f
13eeee4
7f75430
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…rieval
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ namespace OxyPlot.SkiaSharp | |
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Drawing; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is needed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
||
|
|
@@ -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> | ||
|
|
@@ -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) | ||
| { | ||
|
|
@@ -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. | ||
|
|
||
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.