Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Text Wrapping in the .NET DrawingContext DrawText method

I'm working on a project that has me approximating text rendered as an image and a DHTML editor for the text. The images are rendered using the .NET 4 DrawingContext object's DrawText method.

The DrawText method will take text along with font information as well as dimensions and calculate the wrapping necessary to get the text to fit as much as possible, placing an ellipsis at the end if the text is too long. So, if I have the following code to draw text in a Rectangle it will abbrevaiate it:

string longText = @"A choice of five engines, although the 2-liter turbo diesel, supposedly good for 48 m.p.g. highway, is not coming to America, at least for now. A 300-horsepower supercharged gasoline engine will likely be the first offered in the United States. All models will use start-stop technology, and fuel consumption will decrease by an average of 19 percent across the A6 lineup. A 245-horsepower A6 hybrid was also unveiled, but no decision has yet been made as to its North America sales prospects. Figure later in 2012, if sufficient demand is detected.";

var drawing = new DrawingGroup();
using (var context = drawing.Open())
{
    var text = new FormattedText(longText,
        CultureInfo.CurrentCulture,
        FlowDirection.LeftToRight,
        new Typeface("Calibri"),
        30,
        Brushes.Green);
    text.MaxTextHeight = myRect.Height;
    text.MaxTextWidth = myRect.Width;

    context.DrawText(text, new Point(0, 0));
}

var db = new DrawingBrush(drawing);
db.Stretch = Stretch.None;
myRect.Fill = db;

Is there a way to calculate how the text will be wrapped? In this example, the outputted text is wrapped at "2-liter" and "48 m.p.g" etc as seen in the image below: alt text

like image 732
t3rse Avatar asked Jan 19 '11 23:01

t3rse


2 Answers

You can use the Graphics.MeasureString(String, Font, Int32) function. You pass it the string, font, and maximum width. It returns a SizeF with the rectangle it would form. You can use this to get the overall height, and thus the number of lines:

Graphics g = ...;
Font f = new Font("Calibri", 30.0);
SizeF sz = g.MeasureString(longText, f, myRect.Width);
float height = sz.Height;
int lines = (int)Math.round(height / f.Height); // overall height divided by the line height = number of lines

There are many ways to get a Graphics object, and any will do since you are only using it to measure and not to draw (you may have to correct its DpiX, DpiY, and PageUnit fields since those effect measurements.

Ways to get a Graphics object:

Graphics g = e.Graphics; // in OnPaint, with PaintEventArgs e
Graphics g = x.CreateGrahics(); // where x is any Form or Control
Graphics g = Graphics.CreateFrom(img); // where img is an Image.
like image 80
coderforlife Avatar answered Oct 22 '22 19:10

coderforlife


Not sure if you still need a solution or if this particular solution is appropriate for your application, but if you insert the below snippet just after your using block it will show you the text in each line (and therefore where the text was broken for wrapping).

I arrived at this solution using the very ghetto/guerrilla approach of just browsing properties while debugging, looking for the wrapped text segments - I found 'em and they were in accessible properties...so there you go. There very well may be a more proper/direct way.

// Object heirarchy:
// DrawingGroup (whole thing)
//  - DrawingGroup (lines)
//     - GlyphRunDrawing.GlyphRun.Characters (parts of lines)

// Note, if text is clipped, the ellipsis will be placed in its own 
// separate "line" below.  Give it a try and you'll see what I mean.

List<DrawingGroup> lines = drawing.Children.OfType<DrawingGroup>().ToList();

foreach (DrawingGroup line in lines)
{
    List<char> lineparts = line.Children
        .OfType<GlyphRunDrawing>()
        .SelectMany(grd => grd.GlyphRun.Characters)
        .ToList();

    string lineText = new string(lineparts.ToArray());

    Debug.WriteLine(lineText);
}

Btw, Hi David. :-)

like image 2
shaunmartin Avatar answered Oct 22 '22 20:10

shaunmartin