Skip to content
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

[Bug] TextBlock Inlines (Bold/Italic) not working with Chinese characters #18199

Open
LITTOMA opened this issue Feb 14, 2025 · 4 comments
Open

Comments

@LITTOMA
Copy link

LITTOMA commented Feb 14, 2025

Describe the bug

When using Avalonia TextBlock with Inlines (Bold, Italic, or Span with FontWeight/FontStyle), the special text formatting (bold/italic) doesn't work for Chinese characters. The Chinese text is displayed as normal text without any style changes.

To Reproduce

  1. Create a TextBlock control
  2. Add Inlines with Bold or Italic formatting for Chinese text
  3. Example code & output:
public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();
        InitInlineTextBlock();
        CreateFromCode();
    }

    private void CreateFromCode()
    {
        var _contentPanel = new StackPanel();
        var textBlock = new TextBlock
        {
            TextWrapping = TextWrapping.Wrap,
            FontSize = 16,
            HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
            VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center
        };
        textBlock.Inlines.Add(new Run("这是"));
        textBlock.Inlines.Add(new Bold { Inlines = { new Run("一个") } });
        textBlock.Inlines.Add(new Run("文本,由"));
        textBlock.Inlines.Add(new Italic { Inlines = { new Run("代码") } });
        textBlock.Inlines.Add(new Run("创建"));

        _contentPanel.Children.Add(textBlock);
        container.Children.Add(_contentPanel);
    }

    private void InitInlineTextBlock()
    {
        var bold = new Bold
        {
            Inlines =
            {
                new Run("一个")
            }
        };
        var italic = new Italic
        {
            Inlines =
            {
                new Run("代码")
            }
        };

        codeBehindTextBlock.Inlines.Add(new Run("这是"));
        codeBehindTextBlock.Inlines.Add(bold);
        codeBehindTextBlock.Inlines.Add(new Run("文本,由"));
        codeBehindTextBlock.Inlines.Add(italic);
        codeBehindTextBlock.Inlines.Add(new Run("更新"));
    }
}
<UserControl
    x:Class="AvaloniaTextBlockInlines.Views.MainView"
    xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:AvaloniaTextBlockInlines.ViewModels"
    d:DesignHeight="450"
    d:DesignWidth="800"
    x:DataType="vm:MainViewModel"
    mc:Ignorable="d">

    <StackPanel Name="container" Orientation="Vertical">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
            这是一个<Bold>文本</Bold>
            ,由<Italic>叉啊姆鲁</Italic>
            创建</TextBlock>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
            This <Bold>English</Bold>
            text is created by<Italic>XAML</Italic>
        </TextBlock>
        <TextBlock
            Name="codeBehindTextBlock"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />
    </StackPanel>
</UserControl>

Image

Expected behavior

Chinese characters should be displayed with bold or italic formatting, just like Latin characters do.

Avalonia version

11.2.0

OS

Windows

Additional context

No response

@NLick47
Copy link

NLick47 commented Feb 18, 2025

@Gillibald
Copy link
Contributor

Yes. The fallback algorithm does not make sure the weight and style are matching. The system font manager yields the first font that holds the requested character. It might be possible this issue can't be solved. This is an issue with the system font manager.

@LITTOMA
Copy link
Author

LITTOMA commented Feb 18, 2025

When setting FontWeight and FontStyle on a TextBlock, Chinese text can be properly bolded and italicized. Why does this happen? Does TextBlock use software simulation to render these two special styles?

@Gillibald
Copy link
Contributor

When setting FontWeight and FontStyle on a TextBlock, Chinese text can be properly bolded and italicized. Why does this happen? Does TextBlock use software simulation to render these two special styles?

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants