How to calculate "Size" FormattedText or GlyphRun in wpf?

In WPF4, how can I calculate the 'Size' for FormattedText or GlyphRun for a drawingvisual .

I use drawvisual in canvas. When I resize text or text, changes occur, but the actual width and height are the same or not updated.

 Using dc As DrawingContext = drawingvisual.RenderOpen Dim ft As New FormattedText(...) dc.DrawText(ft, New Point(0, 0)) dc.Close() End Using 
+6
source share
2 answers

Once you have initialized FormattedText, it has the Width and Height elements that are equal to its actual rendering size, given its parameters. In fact, changing the parameters immediately updates them, e, g,:

 FormattedText ft = new FormattedText(cellString, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, fontFace, fontSize, fontBrush); ft.Trimming = TextTrimming.CharacterEllipsis; double originalHeight = ft.Height; double width = ft.Width; ft.MaxTextWidth = bCellRect.Width; // Set the width to get a new height double height = ft.Height; 

Edit for GlyphRun:. When you created your GlyphRun, you already gave it the width of the advance for each character, so you add them for the width. To get the height, use GlyphTypeface.Baseline * FontSize

+6
source

As a note: I noticed a 10x performance increase when using DrawingContext.DrawGlyphRun vs DrawingContext.DrawFormattedText.

GlyphRun is not documented, but once you read this article , you can understand how it works.

+3
source

Source: https://habr.com/ru/post/892892/


All Articles