diff --git a/managed/NativeSkia.Tests/DataTests.cs b/managed/NativeSkia.Tests/DataTests.cs index c2bedf7..2bc6211 100644 --- a/managed/NativeSkia.Tests/DataTests.cs +++ b/managed/NativeSkia.Tests/DataTests.cs @@ -13,7 +13,7 @@ public void CreateFromFile() // read file via C# API var expectedContent = File.ReadAllBytes(path); - expectedContent.Length.Should().Be(30_167); + expectedContent.Length.Should().Be(28_357); // // read file via NativeSkia API using var data = SkData.FromFile(path); @@ -30,7 +30,7 @@ public void CreateFromBinary() // read file via C# API var expectedContent = File.ReadAllBytes(path); - expectedContent.Length.Should().Be(30_167); + expectedContent.Length.Should().Be(28_357); // read file via NativeSkia API using var data = SkData.FromBinary(expectedContent); diff --git a/managed/NativeSkia.Tests/ParagraphTests.cs b/managed/NativeSkia.Tests/ParagraphTests.cs index 3c1b2d4..11821bb 100644 --- a/managed/NativeSkia.Tests/ParagraphTests.cs +++ b/managed/NativeSkia.Tests/ParagraphTests.cs @@ -19,7 +19,7 @@ public void DrawParagraphOnPdfCanvas() typefaceProvider.AddTypefaceFromData(latoTypefaceData); // build simple paragraph; - var fontCollection = SkFontCollection.Create(typefaceProvider, true, true); + var fontCollection = SkFontCollection.Create(typefaceProvider, SkFontManager.Global); var paragraphStyleConfiguration = new ParagraphStyleConfiguration { @@ -35,7 +35,7 @@ public void DrawParagraphOnPdfCanvas() FontSize = 18, FontWeight = TextStyleConfiguration.FontWeights.Normal, - FontFamily = new SkText("Times New Roman"), + FontFamilies = GetFontFamilyPointers("Times New Roman"), BackgroundColor = 0x00000000, ForegroundColor = 0xFF000000, @@ -53,8 +53,8 @@ public void DrawParagraphOnPdfCanvas() using var textStyle = new SkTextStyle(textStyleConfiguration); using var textStyleUnderline = new SkTextStyle(textStyleConfiguration with { DecorationType = TextStyleConfiguration.TextDecoration.Underline }); - using var monoTypefaceStyle = new SkTextStyle(textStyleConfiguration with { FontFamily = new SkText("JetBrains Mono"), BackgroundColor = 0x33000000 }); - using var latoTypefaceStyle = new SkTextStyle(textStyleConfiguration with { FontFamily = new SkText("Lato"), BackgroundColor = 0x22000000 }); + using var monoTypefaceStyle = new SkTextStyle(textStyleConfiguration with { FontFamilies = GetFontFamilyPointers("JetBrains Mono"), BackgroundColor = 0x33000000 }); + using var latoTypefaceStyle = new SkTextStyle(textStyleConfiguration with { FontFamilies = GetFontFamilyPointers("Lato"), BackgroundColor = 0x22000000 }); paragraphBuilder.AddText("Lorem Ipsum is simply \ndummy text of the printing and typesetting industry. ", textStyle); paragraphBuilder.AddText("🥰😅🥰🥰🥰 ", textStyle); @@ -100,7 +100,8 @@ public void DrawParagraphOnPdfCanvas() [Test] public void GetLineHeights() { - using var fontCollection = SkFontCollection.Create(null, true, true); + using var typefaceProvider = new SkTypefaceProvider(); + using var fontCollection = SkFontCollection.Create(typefaceProvider, SkFontManager.Global); var paragraphStyleConfiguration = new ParagraphStyleConfiguration { @@ -114,7 +115,7 @@ public void GetLineHeights() { FontSize = 18, FontWeight = TextStyleConfiguration.FontWeights.Normal, - FontFamily = new SkText("Times New Roman") + FontFamilies = GetFontFamilyPointers("Times New Roman") }); paragraphBuilder.AddPlaceholder(new SkPlaceholderStyle { Width = 20, Height = 20, Alignment = SkPlaceholderStyle.PlaceholderAlignment.Bottom }); @@ -141,7 +142,7 @@ public void GetUnresolvedCodepoints() using var latoTypefaceData = SkData.FromFile("input/Lato/Lato-Light.ttf"); typefaceProvider.AddTypefaceFromData(latoTypefaceData); - using var fontCollection = SkFontCollection.Create(typefaceProvider, false, true); + using var fontCollection = SkFontCollection.Create(typefaceProvider, SkFontManager.Empty); var paragraphStyleConfiguration = new ParagraphStyleConfiguration(); using var paragraphBuilder = SkParagraphBuilder.Create(paragraphStyleConfiguration, fontCollection); @@ -151,7 +152,7 @@ public void GetUnresolvedCodepoints() FontSize = 20, FontWeight = TextStyleConfiguration.FontWeights.Normal, ForegroundColor = 0xFF000000, - FontFamily = new SkText("Lato") + FontFamilies = GetFontFamilyPointers("Lato") }); const string text = "Fire as emoji 🔥 and in Japanese 火."; @@ -168,8 +169,9 @@ public void GetUnresolvedCodepoints() [Test] public void Subscript() { - // build simple paragraph; - var fontCollection = SkFontCollection.Create(null, true, true); + // build simple paragraph + using var typefaceProvider = new SkTypefaceProvider(); + var fontCollection = SkFontCollection.Create(typefaceProvider, SkFontManager.Global); var paragraphStyleConfiguration = new ParagraphStyleConfiguration { @@ -185,7 +187,7 @@ public void Subscript() FontSize = 18, FontWeight = TextStyleConfiguration.FontWeights.Normal, - FontFamily = new SkText("Times New Roman"), + FontFamilies = GetFontFamilyPointers("Times New Roman"), BackgroundColor = 0x00000000, ForegroundColor = 0xFF000000, @@ -232,7 +234,8 @@ public void Subscript() [Test] public void DrawParagraphWithHyperlink() { - using var fontCollection = SkFontCollection.Create(null, true, true); + using var typefaceProvider = new SkTypefaceProvider(); + using var fontCollection = SkFontCollection.Create(typefaceProvider, SkFontManager.Global); using var stream = new SkWriteStream(); using var pdf = SkPdfDocument.Create(stream, new SkPdfDocumentMetadata()); @@ -271,7 +274,7 @@ SkParagraph BuildParagraph() FontSize = 18, ForegroundColor = 0xFF000000, FontWeight = TextStyleConfiguration.FontWeights.Medium, - FontFamily = new SkText("Times New Roman") + FontFamilies = GetFontFamilyPointers("Times New Roman") }; var linkStyleConfiguration = baseTextStyleConfiguration with @@ -293,4 +296,14 @@ SkParagraph BuildParagraph() return paragraphBuilder.CreateParagraph(); } } + + IntPtr[] GetFontFamilyPointers(params string[] texts) + { + var result = new IntPtr[TextStyleConfiguration.FONT_FAMILIES_LENGTH]; + + for (var i = 0; i < Math.Min(result.Length, texts.Length); i++) + result[i] = new SkText(texts[i]).Instance; + + return result; + } } \ No newline at end of file diff --git a/managed/NativeSkia/Text/SkFontCollection.cs b/managed/NativeSkia/Text/SkFontCollection.cs index c524523..3a92f14 100644 --- a/managed/NativeSkia/Text/SkFontCollection.cs +++ b/managed/NativeSkia/Text/SkFontCollection.cs @@ -16,22 +16,14 @@ private struct CreateCommand { public IntPtr FontManager; public IntPtr TypefaceProvider; - [MarshalAs(UnmanagedType.I1)] public bool EnableFontFallback; } - public static SkFontCollection Create(SkTypefaceProvider? typefaceProvider = null, bool useGlobalFonts = false, bool enableFontFallback = false) + public static SkFontCollection Create(SkTypefaceProvider typefaceProvider, SkFontManager fontManager) { - typefaceProvider ??= new SkTypefaceProvider(); - - var fontManager = useGlobalFonts - ? SkFontManager.Global - : SkFontManager.Empty; - var command = new CreateCommand { FontManager = fontManager.Instance, - TypefaceProvider = typefaceProvider.Instance, - EnableFontFallback = enableFontFallback + TypefaceProvider = typefaceProvider.Instance }; var instance = API.font_collection_create(command); @@ -42,7 +34,7 @@ public static SkFontCollection Create(SkTypefaceProvider? typefaceProvider = nul { Dispose(); } - + public void Dispose() { if (Instance == IntPtr.Zero) @@ -60,4 +52,4 @@ private static class API [DllImport(SkiaAPI.LibraryName)] public static extern void font_collection_unref(IntPtr fontCollection); } -} +} \ No newline at end of file diff --git a/managed/NativeSkia/Text/SkTextStyle.cs b/managed/NativeSkia/Text/SkTextStyle.cs index 711afbe..a3be374 100644 --- a/managed/NativeSkia/Text/SkTextStyle.cs +++ b/managed/NativeSkia/Text/SkTextStyle.cs @@ -9,9 +9,9 @@ internal struct TextStyleConfiguration public FontWeights FontWeight; public bool IsItalic; - public IntPtr FontFamily; - public IntPtr FontFamilyFallback; - + public const int FONT_FAMILIES_LENGTH = 16; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = FONT_FAMILIES_LENGTH)] public IntPtr[] FontFamilies; + public uint ForegroundColor; public uint BackgroundColor; diff --git a/native/src/text/fontCollection.cpp b/native/src/text/fontCollection.cpp index 32bc517..a8b9e0b 100644 --- a/native/src/text/fontCollection.cpp +++ b/native/src/text/fontCollection.cpp @@ -8,7 +8,6 @@ extern "C" { struct CreateFontCollectionCommand { SkFontMgr *fontManager; skia::textlayout::TypefaceFontProvider *typefaceFontProvider; - bool enableFontFallback; }; QUEST_API skia::textlayout::FontCollection *font_collection_create(CreateFontCollectionCommand command) { @@ -16,12 +15,7 @@ QUEST_API skia::textlayout::FontCollection *font_collection_create(CreateFontCol fontCollection->setAssetFontManager(sk_ref_sp(command.typefaceFontProvider)); fontCollection->setDefaultFontManager(sk_ref_sp(command.fontManager)); - - if (command.enableFontFallback) { - fontCollection->enableFontFallback(); - } else { - fontCollection->disableFontFallback(); - } + fontCollection->enableFontFallback(); return fontCollection; } diff --git a/native/src/text/textStyle.cpp b/native/src/text/textStyle.cpp index f0a6fe4..df04ddd 100644 --- a/native/src/text/textStyle.cpp +++ b/native/src/text/textStyle.cpp @@ -5,12 +5,13 @@ extern "C" { struct TextStyleConfiguration { + static const int FONT_FAMILIES_LENGTH = 16; + SkScalar fontSize; SkFontStyle::Weight fontWeight; bool isItalic; - char *fontFamily; - char *fontFamilyFallback; + char* fontFamilies[FONT_FAMILIES_LENGTH]; SkColor foregroundColor; SkColor backgroundColor; @@ -37,11 +38,13 @@ QUEST_API skia::textlayout::TextStyle *text_style_create(TextStyleConfiguration // set font families std::vector fontFamilies; - fontFamilies.emplace_back(configuration.fontFamily); - if (configuration.fontFamilyFallback) - fontFamilies.emplace_back(configuration.fontFamilyFallback); + for (auto & fontFamily : configuration.fontFamilies) { + if (fontFamily == nullptr) + continue; + fontFamilies.emplace_back(fontFamily); + } textStyle->setFontFamilies(fontFamilies); // end @@ -76,4 +79,5 @@ QUEST_API skia::textlayout::TextStyle *text_style_create(TextStyleConfiguration QUEST_API void text_style_delete(skia::textlayout::TextStyle *textStyle) { delete textStyle; } + } \ No newline at end of file