AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.22k stars 2.18k forks source link

Text measurement improvements #132

Closed kekekeks closed 1 year ago

kekekeks commented 9 years ago

We need something to be able to implement methods like public override void MeasureString(string str, Font font, double maxWidth, out int charFit, out double charFitWidth) (HtmlRenderer requires this).

For example, Graphics.MeasureCharacterRanges returns info about each character's position.

grokys commented 9 years ago

Is there an equivalent API in WPF that we can copy?

tshcherban commented 8 years ago

There is no equivalent, but WPF contains FormattedText class. Usage example: http://stackoverflow.com/a/840642

kekekeks commented 8 years ago

@tshcherban https://github.com/Perspex/Perspex/blob/master/src/Perspex.SceneGraph/Media/FormattedText.cs

h0730303779 commented 1 year ago

How did you solve it later? I am now rendering some strings and also need to know the width of each rendered string.

kekekeks commented 1 year ago

See TextFormatter and friends.

In HtmlRenderer MeasureString is now implemented like this: https://github.com/AvaloniaUI/Avalonia.HtmlRenderer/blob/f4e8c20a53a4cb4cc09b4f2ad8460e75d9f1fb36/Avalonia.HtmlRenderer/Adapters/GraphicsAdapter.cs#L115-L124