I have a couple of methods that draw outlined text. The details of this are unimportant, but it serves to illustrate the problem:
(source code from Graphics DrawPath produces unexpected results when rendering text)
Private Sub FillTextSolid(g As Graphics, bounds As RectangleF, text As String, font As Font, fillColor As Color, sf As StringFormat)
Using gp As GraphicsPath = New GraphicsPath(),
brush As New SolidBrush(fillColor)
gp.AddString(text, font.FontFamily, font.Style, font.Size, bounds, sf)
g.FillPath(brush, gp)
End Using
End Sub
correctly converts a long string into one with an ellipsis inside the bounds. E.g.
Manic Miner is a platform video game originally written for the ZX Spectrum by Matthew Smith and released by Bug-Byte in 1983. It is the first game in the Miner Willy series and among the early titles in the platform game genre.
becomes:
Manic Miner is a platform video game originally written for the ZX Spectrum by Matthew Smith and released by Bug-Byte in 1983. It is the first game in the Miner...
All well and good. What I need is a way in code to see exactly what part of the full text has been displayed. This will then be used to cycle through the text in the same bounds (almost paging if you will) to display all the text.
I looked at MeasureString but this didn't seem to achieve this. Is there any way I can discern this? In pseudo code, something like:
Dim textShown as string = gp.AddString(text, font.FontFamily, font.Style, font.Size, bounds, sf).TextShown
Thanks
Given the
FillTextSolid()method shown before in:Graphics DrawPath produces unexpected results when rendering text
you can use the current GraphicsPath, Rectangle bounds, Font size and style used for drawing the the text in a graphics context, to calculate the position of the last character drawn and, as a consequence, the last word, if necessary.
I've added in
FillTextSolid()a call to theGetPathLastCharPosition()method, which is responsible for the calculation. Pass to the method the objects described, as they're currently configured (these settings can of course change at any time: see the animation at the bottom).To determine the current last word printed using a GraphicsPath object, you cannot split the string in parts separated by, e.g., a white space, since each char is part of the rendering.
Also to note: for the measure to work as intended, you cannot set the drawing Font size in Points, the Font size must be expressed in pixels.
You could also use Point units, but the GraphicsPath class, when Points are specified, generates (correctly) a Font measure in
EMs - considering the Font Cell Ascent and Descent - which is not the same as theFont.Height.You can of course convert the measure from
Ems to Pixels, but it just adds complexity for no good reason (in the context of this question, at least).See a description of these details and how to calculate the GraphicsPath EMs in:
Properly draw text using GraphicsPath
GetPathLastCharPosition()uses Graphics.MeasureCharacterRanges to measure the bounding rectangle of each char in the Text string, in chunks of 32 chars per iteration. This is because StringFormat.SetMeasurableCharacterRanges only takes a maximum of 32 CharacterRange elements.So, we take the Text in chunks of 32 chars, get the bounding Region of each and verify whether the Region contains the last Point in the GraphicsPath.
The last Point generated by a GraphicsPath is returned by the GraphicsPath.GetLastPoint().
It can be adapted to handle right to left languages.
Of course, you could also ignore the last point and just consider whether a Region bounds fall outside the bounding rectangle of the canvas.
Anyway, when the a Region that contains the last point is found, the method stops and returns the position of the last character in the string that is part of the drawing.
This is how it works:
C# version of the method: