GDI+ Problem
-
Hello! GDI+ text output makes me sick :doh: Sometimes GDI+ writes text with wide s p a c e s between letters, sometimes it writes letters very close together....The same with words...:eek: How can I configure the text drawing style or how can I solve this problems? My actual code for text output:
StringFormat Format=new StringFormat(); Format.FormatFlags= StringFormatFlags.NoWrap | StringFormatFlags.NoFontFallback | StringFormatFlags.LineLimit | StringFormatFlags.MeasureTrailingSpaces; G.DrawString("Some text",Font,Brush,0,0,Format); //or, after measuring the string: G.DrawString("Some text",Font,Brush,TextRect,Format);
-
Hello! GDI+ text output makes me sick :doh: Sometimes GDI+ writes text with wide s p a c e s between letters, sometimes it writes letters very close together....The same with words...:eek: How can I configure the text drawing style or how can I solve this problems? My actual code for text output:
StringFormat Format=new StringFormat(); Format.FormatFlags= StringFormatFlags.NoWrap | StringFormatFlags.NoFontFallback | StringFormatFlags.LineLimit | StringFormatFlags.MeasureTrailingSpaces; G.DrawString("Some text",Font,Brush,0,0,Format); //or, after measuring the string: G.DrawString("Some text",Font,Brush,TextRect,Format);
Does this all happen with the same font?
-
Does this all happen with the same font?
-
Try a different font to determine if the font is causing the problem.
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Hello! GDI+ text output makes me sick :doh: Sometimes GDI+ writes text with wide s p a c e s between letters, sometimes it writes letters very close together....The same with words...:eek: How can I configure the text drawing style or how can I solve this problems? My actual code for text output:
StringFormat Format=new StringFormat(); Format.FormatFlags= StringFormatFlags.NoWrap | StringFormatFlags.NoFontFallback | StringFormatFlags.LineLimit | StringFormatFlags.MeasureTrailingSpaces; G.DrawString("Some text",Font,Brush,0,0,Format); //or, after measuring the string: G.DrawString("Some text",Font,Brush,TextRect,Format);
There was a discussion not long ago about GDI+ squishing characters together at the end of a sentance when
NoWrap
was used (or a couple other conditions were true). This lead to a topic on MSDN about why it does that and the properStringFormat
to use to eliminate that (StringFormat.GenericTypographic
). Try to use the same flags as that if possible. As far as spaces appearing between letters, this is fairly odd. I've never seen this when drawing my own strings and have never seen anything documented to even control this. Either the text you're passing has spaces (inproper Unicode string?) or the font you're using has some issues. Try a different one and see.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Hello! GDI+ text output makes me sick :doh: Sometimes GDI+ writes text with wide s p a c e s between letters, sometimes it writes letters very close together....The same with words...:eek: How can I configure the text drawing style or how can I solve this problems? My actual code for text output:
StringFormat Format=new StringFormat(); Format.FormatFlags= StringFormatFlags.NoWrap | StringFormatFlags.NoFontFallback | StringFormatFlags.LineLimit | StringFormatFlags.MeasureTrailingSpaces; G.DrawString("Some text",Font,Brush,0,0,Format); //or, after measuring the string: G.DrawString("Some text",Font,Brush,TextRect,Format);
This is a known problem in GDI+. The resolution is to use non-gridfit font hinting or a monospaced font. You should also use FitBlackBox to prevent the lines from painted at slighty different heights. Here's how you can check for it (note: i have a fonthint field in my class): Also AA fonts look very poor in small sizes.
void CheckFontBug(Graphics g)
{
//is font monospace?
float w = g.MeasureString("w", font, int.MaxValue, sf).Width * 10f;
float l = g.MeasureString("llllllllll", font, int.MaxValue, sf).Width;
fonthint = (System.Math.Abs(w-l) > 0.005f) ? //allow float error
(fontheight > 14f ? TextRenderingHint.AntiAlias : TextRenderingHint.SingleBitPerPixel) :
TextRenderingHint.SystemDefault;
}leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.