* Improve SkiaEncoder's font handling
Our previous approach didn’t work with some complex library names, even when the required fonts were present, because the font handling logic was too simplistic. Modern Unicode and the fonts have become quite complex, making it challenging to implement it correctly. This improved implementation still isn’t the most correct way, but it’s better than it used to be. It now falls back to multiple fonts to find the best one and also handles extended grapheme clusters that were incorrectly processed before.
* Fix space
* Remove redundant comment
* Make _typefaces an array
* Make Measure and Draw text function name clear
* Fix rename
@ -24,6 +25,7 @@ public class SkiaEncoder : IImageEncoder
privatereadonlyILogger<SkiaEncoder>_logger;
privatereadonlyIApplicationPaths_appPaths;
privatestaticreadonlySKImageFilter_imageFilter;
privatestaticreadonlySKTypeface[]_typefaces;
#pragma warning disable CA1810
staticSkiaEncoder()
@ -46,6 +48,21 @@ public class SkiaEncoder : IImageEncoder
kernelOffset,
SKShaderTileMode.Clamp,
true);
// Initialize the list of typefaces
// We have to statically build a list of typefaces because MatchCharacter only accepts a single character or code point
// But in reality a human-readable character (grapheme cluster) could be multiple code points. For example, 🚵🏻♀️ is a single emoji but 5 code points (U+1F6B5 + U+1F3FB + U+200D + U+2640 + U+FE0F)
_typefaces=
[
SKFontManager.Default.MatchCharacter(null,SKFontStyleWeight.Bold,SKFontStyleWidth.Normal,SKFontStyleSlant.Upright,null,'鸡'),// CJK Simplified Chinese
SKFontManager.Default.MatchCharacter(null,SKFontStyleWeight.Bold,SKFontStyleWidth.Normal,SKFontStyleSlant.Upright,null,'雞'),// CJK Traditional Chinese
SKFontManager.Default.MatchCharacter(null,SKFontStyleWeight.Bold,SKFontStyleWidth.Normal,SKFontStyleSlant.Upright,null,'ノ'),// CJK Japanese
SKFontManager.Default.MatchCharacter(null,SKFontStyleWeight.Bold,SKFontStyleWidth.Normal,SKFontStyleSlant.Upright,null,'각'),// CJK Korean
SKFontManager.Default.MatchCharacter(null,SKFontStyleWeight.Bold,SKFontStyleWidth.Normal,SKFontStyleSlant.Upright,null,128169),// Emojis, 128169 is the 💩emoji