Kamujin's picture

TextPrinter.Print does not properly handle text with newline characters.

Project:The Open Toolkit library
Category:bug report

textPrinter.Print("Line1\nLine2\nLine3", font, Color.Black);

...prints all 3 lines to the same line.


textPrinter.Measure("Line1\nLine2\nLine3", font).BoundingBox;

...returns a rectangle as if the text were 2 lines of text.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture


Which OS?

Does this occur when you use System.Environment.NewLine? '\n' is not a valid newline character on Windows.

Kamujin's picture


Ubuntu 9.04.

"\n" is a valid newline in .NET as well as for many most Win32 controls. Console.WriteLine("Line1\nLine2") yields output on 2 lines on both mono and .NET runtimes.

If you are thinking of the "\r\n" pairs often used in windows files, it is still worth noting that the '\r" mean carriage return and the "\n" means newline.

Regardless of how you choose to implement this, I think it would be worth the time to look at for the following reasons.

1) The current implementation treats the "\n" character as carriage return (without a newline) and overwrites the previous text.

2) textPrinter.Measure("Line1\nLine2\nLine3", font).BoundingBox; returns a HEIGHT property equal to 2 lines of height.

Hope this helps and thanks.

the Fiddler's picture


This issue is slightly more complicated than it looks. Ideally, newlines would be processed by GDI+ (which we use for text measurements / output). Unfortunately, the GDI+ implementation on Mono cannot handle newlines at this point and .Net is awfully slow when measuring strings with newlines.

The current workaround is to split text into an array of strings (something equivalent to string[] lines = text.Replace('\r', '').Split('\n')) and processes each line separately. The real solution would be to bypass GDI+ completely and use GDI on Windows and Mono.Pango on Linux / Mac OS X. Unfortunately, I cannot dedicate the time for this right now.

I don't doubt that you have encountered a bug. Text printing with newlines seems to work on .Net and Mono 2.2+ (subject to some limitations on Mono), but I haven't tested with Mono 2.0. I'll try to reproduce on Ubuntu 9.04 and fix this.

Edit: The fact that several WinForms controls can cope with '\n' does not make it any more valid. Many other controls cannot (like the TextBox or the MessageBox) - better play safe and treat this as an implementation detail (which actually changed during the 1.1 -> 2.0 transition). AFAIK, the console is a completely different beast.

the Fiddler's picture


Status:open» confirmed

Ok, I can reproduce the problem. Investigating.

the Fiddler's picture


Version:0.9.7» 0.9.x-dev
Status:confirmed» duplicate

This is a duplicate of issue #847: TextPrinter layout when width, height are zero..