davidxuang / FluentIcons

A multi-framework wrapper of https://github.com/microsoft/fluentui-system-icons
MIT License
74 stars 6 forks source link

Crash when NativeAOT enabled #8

Closed shatyuka closed 2 months ago

shatyuka commented 5 months ago

Avalonia 11.0.10 FluentIcons.Avalonia 1.1.234 Crash on both macOS and Windows with UseSegoeMetrics() and NativeAOT on.

Unhandled Exception: System.InvalidOperationException: Could not create glyphTypeface.
   at Avalonia.Media.Typeface.get_GlyphTypeface() + 0x78
   at Avalonia.Media.TextFormatting.TextRunProperties.get_CachedGlyphTypeface() + 0x3c
   at Avalonia.Media.TextFormatting.TextCharacters.CreateShapeableRun(ReadOnlyMemory`1, TextRunProperties, SByte, FontManager, TextRunProperties&) + 0x80
   at Avalonia.Media.TextFormatting.TextCharacters.GetShapeableCharacters(ReadOnlyMemory`1, SByte, FontManager, TextRunProperties&, FormattingObjectPool.RentedList`1) + 0x54
   at Avalonia.Media.TextFormatting.TextFormatterImpl.CoalesceLevels(IReadOnlyList`1, ReadOnlySpan`1, FontManager, FormattingObjectPool.RentedList`1) + 0x474
   at Avalonia.Media.TextFormatting.TextFormatterImpl.ShapeTextRuns(IReadOnlyList`1, TextParagraphProperties, FormattingObjectPool, FontManager, FlowDirection&) + 0x318
   at Avalonia.Media.TextFormatting.TextFormatterImpl.FormatLine(ITextSource, Int32, Double, TextParagraphProperties, TextLineBreak) + 0x114
   at Avalonia.Media.TextFormatting.TextLayout.CreateTextLines() + 0x180
   at Avalonia.Media.TextFormatting.TextLayout..ctor(String, Typeface, Double, IBrush, TextAlignment, TextWrapping, TextTrimming, TextDecorationCollection, FlowDirection, Double, Double, Double, Double, Int32, IReadOnlyList`1) + 0x1bc
   at FluentIcons.Avalonia.SymbolIcon.Core.InvalidateText(String, Typeface, Double, IBrush) + 0x94
   at FluentIcons.Avalonia.SymbolIcon.InvalidateText() + 0x154
   at FluentIcons.Avalonia.SymbolIcon.OnLoaded(RoutedEventArgs) + 0x1c
   at Avalonia.Controls.Control.<>c.<.cctor>b__69_0() + 0xc8
   at Avalonia.Threading.DispatcherOperation.InvokeCore() + 0x1ac
   at Avalonia.Threading.DispatcherOperation.Execute() + 0x6c
   at Avalonia.Threading.Dispatcher.ExecuteJob(DispatcherOperation) + 0x64
   at Avalonia.Threading.Dispatcher.ExecuteJobsCore(Boolean) + 0xb4
   at Avalonia.Threading.Dispatcher.Signaled() + 0x40
   at Avalonia.Native.Interop.Impl.__MicroComIAvnPlatformThreadingInterfaceEventsVTable.Signaled(Void*) + 0x7c
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x24
   at Avalonia.Native.DispatcherImpl.RunLoop(CancellationToken) + 0x268
   at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl) + 0x110
   at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame) + 0xb0
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken) + 0x10c
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[]) + 0x54
davidxuang commented 5 months ago

我的最小化 demo 在 AOT 下起不来,也看不到 stack trace

是资源文件被错误剪裁掉了吗?建议可以用 HxD 之类的工具看看生成的 exe 有没有正确包括两个 ttf 文件

shatyuka commented 5 months ago

确认了字体文件是有打包进exe的。 而且在xaml里指定Segoe字体是可以运行的:

<StackPanel>
    <ic:SymbolIcon Symbol="Home" />
    <ic:SymbolIcon Symbol="Home" UseSegoeMetrics="True"/>
</StackPanel>

BuildAvaloniaApp 里调用 UseSegoeMetrics() 就会崩溃

davidxuang commented 5 months ago

AOT 要加些啥东西,怎么查 trace?我这边 demo 起不来。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <TrimMode>copyused</TrimMode>
    <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
    <PublishAot>true</PublishAot>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Avalonia" Version="11.0.*" />
    <PackageReference Include="Avalonia.Desktop" Version="11.0.*" />
    <PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.*" />
    <PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.*" />
    <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
    <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.*" />
    <PackageReference Include="FluentIcons.Avalonia" Version="1.1.*" />
    <!-- <PackageReference Include="SkiaSharp" Version="3.0.0-preview.0.132" /> -->
  </ItemGroup>

  <ItemGroup>
    <TrimmerRootDescriptor Include=".\TrimmerRoots.xml" />
  </ItemGroup>
</Project>
<linker>
    <!-- App -->
    <assembly fullname="AvaloniaDemo" preserve="All" />
    <!-- Avalonia Themes -->
    <assembly fullname="Avalonia.Themes.Fluent" preserve="All" />
</linker>
shatyuka commented 5 months ago

csproj里加<PublishAot>true</PublishAot> 我用Rider直接运行就可以看到崩溃堆栈

davidxuang commented 5 months ago

上游 bug。参见链接 issue。 Upstream bug. See linked isssue.

davidxuang commented 2 months ago

Fixed as of https://github.com/AvaloniaUI/Avalonia/releases/tag/11.1.0