|
| 1 | +using LVGLSharp.Interop; |
| 2 | + |
| 3 | +namespace LVGLSharp.Runtime.Linux; |
| 4 | + |
| 5 | +internal static unsafe class LinuxRuntimeFontHelper |
| 6 | +{ |
| 7 | + internal readonly struct LinuxRuntimeFontInitialization |
| 8 | + { |
| 9 | + internal LinuxRuntimeFontInitialization( |
| 10 | + lv_font_t* fallbackFont, |
| 11 | + SixLaborsFontManager? fontManager, |
| 12 | + string? resolvedSystemFontPath, |
| 13 | + string fontDiagnosticSummary, |
| 14 | + string glyphDiagnosticSummary, |
| 15 | + lv_style_t* defaultFontStyle) |
| 16 | + { |
| 17 | + FallbackFont = fallbackFont; |
| 18 | + FontManager = fontManager; |
| 19 | + ResolvedSystemFontPath = resolvedSystemFontPath; |
| 20 | + FontDiagnosticSummary = fontDiagnosticSummary; |
| 21 | + GlyphDiagnosticSummary = glyphDiagnosticSummary; |
| 22 | + DefaultFontStyle = defaultFontStyle; |
| 23 | + } |
| 24 | + |
| 25 | + internal lv_font_t* FallbackFont { get; } |
| 26 | + |
| 27 | + internal SixLaborsFontManager? FontManager { get; } |
| 28 | + |
| 29 | + internal string? ResolvedSystemFontPath { get; } |
| 30 | + |
| 31 | + internal string FontDiagnosticSummary { get; } |
| 32 | + |
| 33 | + internal string GlyphDiagnosticSummary { get; } |
| 34 | + |
| 35 | + internal lv_style_t* DefaultFontStyle { get; } |
| 36 | + |
| 37 | + internal void ApplyTo( |
| 38 | + ref lv_font_t* fallbackFont, |
| 39 | + ref SixLaborsFontManager? fontManager, |
| 40 | + ref lv_style_t* defaultFontStyle) |
| 41 | + { |
| 42 | + fallbackFont = FallbackFont; |
| 43 | + fontManager = FontManager; |
| 44 | + defaultFontStyle = DefaultFontStyle; |
| 45 | + } |
| 46 | + |
| 47 | + internal void ApplyDiagnosticsTo( |
| 48 | + ref lv_font_t* fallbackFont, |
| 49 | + ref SixLaborsFontManager? fontManager, |
| 50 | + ref string? fontDiagnosticSummary, |
| 51 | + ref string? glyphDiagnosticSummary, |
| 52 | + ref lv_style_t* defaultFontStyle) |
| 53 | + { |
| 54 | + ApplyTo(ref fallbackFont, ref fontManager, ref defaultFontStyle); |
| 55 | + fontDiagnosticSummary = FontDiagnosticSummary; |
| 56 | + glyphDiagnosticSummary = GlyphDiagnosticSummary; |
| 57 | + } |
| 58 | + |
| 59 | + internal void ApplyPathAndDiagnosticTo( |
| 60 | + ref lv_font_t* fallbackFont, |
| 61 | + ref SixLaborsFontManager? fontManager, |
| 62 | + ref string? resolvedSystemFontPath, |
| 63 | + ref string? fontDiagnosticSummary, |
| 64 | + ref lv_style_t* defaultFontStyle) |
| 65 | + { |
| 66 | + ApplyTo(ref fallbackFont, ref fontManager, ref defaultFontStyle); |
| 67 | + resolvedSystemFontPath = ResolvedSystemFontPath; |
| 68 | + fontDiagnosticSummary = FontDiagnosticSummary; |
| 69 | + } |
| 70 | + |
| 71 | + internal void ApplyFullTo( |
| 72 | + ref lv_font_t* fallbackFont, |
| 73 | + ref SixLaborsFontManager? fontManager, |
| 74 | + ref string? resolvedSystemFontPath, |
| 75 | + ref string? fontDiagnosticSummary, |
| 76 | + ref string? glyphDiagnosticSummary, |
| 77 | + ref lv_style_t* defaultFontStyle) |
| 78 | + { |
| 79 | + ApplyTo(ref fallbackFont, ref fontManager, ref defaultFontStyle); |
| 80 | + resolvedSystemFontPath = ResolvedSystemFontPath; |
| 81 | + fontDiagnosticSummary = FontDiagnosticSummary; |
| 82 | + glyphDiagnosticSummary = GlyphDiagnosticSummary; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + internal static LinuxRuntimeFontInitialization InitializeRuntimeFont( |
| 87 | + lv_obj_t* root, |
| 88 | + float dpi, |
| 89 | + bool disableManagedFont = false, |
| 90 | + float size = 12) |
| 91 | + { |
| 92 | + var fallbackFont = LvglFontHelper.GetEffectiveTextFont(root, lv_part_t.LV_PART_MAIN); |
| 93 | + var fontManager = TryApplySystemFont( |
| 94 | + root, |
| 95 | + dpi, |
| 96 | + fallbackFont, |
| 97 | + out var resolvedSystemFontPath, |
| 98 | + out var fontDiagnosticSummary, |
| 99 | + out var glyphDiagnosticSummary, |
| 100 | + out var defaultFontStyle, |
| 101 | + disableManagedFont, |
| 102 | + size); |
| 103 | + |
| 104 | + return new LinuxRuntimeFontInitialization( |
| 105 | + fallbackFont, |
| 106 | + fontManager, |
| 107 | + resolvedSystemFontPath, |
| 108 | + fontDiagnosticSummary, |
| 109 | + glyphDiagnosticSummary, |
| 110 | + defaultFontStyle); |
| 111 | + } |
| 112 | + |
| 113 | + internal static SixLaborsFontManager? TryApplySystemFont( |
| 114 | + lv_obj_t* root, |
| 115 | + float dpi, |
| 116 | + lv_font_t* fallback, |
| 117 | + out string? resolvedSystemFontPath, |
| 118 | + out string fontDiagnosticSummary, |
| 119 | + out string glyphDiagnosticSummary, |
| 120 | + out lv_style_t* defaultFontStyle, |
| 121 | + bool disableManagedFont = false, |
| 122 | + float size = 12) |
| 123 | + { |
| 124 | + fontDiagnosticSummary = LinuxSystemFontResolver.GetFontPathDiagnosticSummary(); |
| 125 | + glyphDiagnosticSummary = LinuxSystemFontResolver.GetGlyphDiagnosticSummary(); |
| 126 | + resolvedSystemFontPath = disableManagedFont ? null : LinuxSystemFontResolver.TryResolveFontPath(); |
| 127 | + defaultFontStyle = null; |
| 128 | + |
| 129 | + if (string.IsNullOrWhiteSpace(resolvedSystemFontPath)) |
| 130 | + { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + return LvglFontHelper.ApplyManagedFont( |
| 135 | + root, |
| 136 | + resolvedSystemFontPath, |
| 137 | + size, |
| 138 | + dpi, |
| 139 | + fallback, |
| 140 | + out _, |
| 141 | + out defaultFontStyle); |
| 142 | + } |
| 143 | +} |
0 commit comments