DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
Go Back  Xtreme Visual Basic Talk > > > DrawString Spacing Between Individual Characters


Reply
 
Thread Tools Display Modes
  #1  
Old 07-08-2012, 05:41 PM
Ninth Wave Ninth Wave is offline
Freshman
 
Join Date: Nov 2005
Location: Australia
Posts: 35
Question DrawString Spacing Between Individual Characters


When I use DrawString for say a short string

"hello"

It seems fine, but if I extend the string to something like

"hello how are you what is your name"

the spacing between the characters of all the letters starts to change. Is there any way to make it stay the same? Regardless of the length of the string.

Help would be greatly appreciated.

Last edited by Ninth Wave; 07-08-2012 at 06:24 PM.
Reply With Quote
  #2  
Old 07-08-2012, 08:33 PM
passel's Avatar
passelDrawString Spacing Between Individual Characters passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Choose a fixed width font, like Courier, for example.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 07-09-2012, 12:41 AM
Ninth Wave Ninth Wave is offline
Freshman
 
Join Date: Nov 2005
Location: Australia
Posts: 35
Default

Quote:
Originally Posted by passel View Post
Choose a fixed width font, like Courier, for example.
Thanks, that does work.

Do you know how I could get it to work with other fonts, even Verdana?
Reply With Quote
  #4  
Old 07-09-2012, 08:15 AM
AtmaWeapon's Avatar
AtmaWeaponDrawString Spacing Between Individual Characters AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

There's no way to make Verdana render characters with a fixed width. It is a font designed to calculate the optimum spacing for each character as it renders. This changes in various situations; for example, the "f" in "food" has to be farther from the "o" than the first "f" in "buffalo" must be from the next. If you zoom in close, you'll even see that "ff" is connected in some fonts. The reasoning involves a lot of discussion about readability.

Fixed width fonts don't do that. Every letter is always the same single width.

To me, a more interesting question is, "Why does it matter to you?" Since this is normal behavior for fonts like Verdana, why are you trying to change it? What are you doing?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 07-09-2012, 04:31 PM
hDC_0DrawString Spacing Between Individual Characters hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 559
Default

Just so this thread gets meta-tagged properly...

When we talk about variable spacing between letters/characters
of proportional fonts it's called kerning
(or less commonly called mortising),
with the root word "kern" denoting the part of a type letter that overhangs the edge of the type block,
and the word "kern" is generally thought to be derived from the French word "carne" meaning corner.

You can read more about algorithmic versus table-based kerning here.

To search for fixed width fonts you can also use the term "monospaced" fonts,
which is one of the ways that fonts are sub-divided on this
Wikipedia "List of typefaces" page.

Programmatically determining which fonts are fixed width is not exactly slam-dunk easy using .Net with GDI/GDI+,
but I do have a couple bookmarked offsite threads:
1.) a social.msdn thread concerning:
"How do you determine if a given font family is a fixed-pitch?"
2.) a stackoverflow thread entitled:
"How do I get all installed fixed-width fonts?"

There is also a .Net method (Graphics.MeasureString), that can can measure the specified string when drawn with the specified font.
This thread gives some sample VB.Net DrawString code for using the method.
If you are trying to design some kind of multi-line "smart label" that also takes into account character's height then this thread might be of interest (the link to lebb's offsite final solution is given in the last post).

Besides Courier/Courier New, Lucida Console and Lucida Sans Typewriter are two of the other Windows fixed width fonts that are fairly widely used.
This page lists all fonts included with Windows from Win98 through Vista.
The Windows 7 included fonts are listed here.

Of course, Consolas is the popular fixed width semi-default font for .Net but
there has been a discussion of other fonts for use in .Net here.

Last edited by hDC_0; 07-09-2012 at 05:19 PM.
Reply With Quote
  #6  
Old 07-09-2012, 05:24 PM
Ninth Wave Ninth Wave is offline
Freshman
 
Join Date: Nov 2005
Location: Australia
Posts: 35
Default

Quote:
Originally Posted by AtmaWeapon View Post
To me, a more interesting question is, "Why does it matter to you?" Since this is normal behavior for fonts like Verdana, why are you trying to change it? What are you doing?
Thanks for your info.
I am writing something akin to a console where the user types commands, and using DrawString to do it.
This means that as the user is typing, what they have already typed keeps shifting and makes it weird.

If there is a better way to print text to a form I'd like to hear it.

Thankyou very much hDC_0

Last edited by Ninth Wave; 07-09-2012 at 05:49 PM.
Reply With Quote
  #7  
Old 07-09-2012, 05:46 PM
hDC_0DrawString Spacing Between Individual Characters hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 559
Default Text rendering in .Net

Have you looked at pInvoking TextOut or ExtTextOut?
It's pretty crude, though, in terms of not really being able to control kerning without a lot of extra coding.

According to his article, "Rendering Text Using the .NET Framework",
another alternative is to use the TextRender Class.

It has a DrawText method and you can:
Quote:
..manipulate how the text is drawn by using one of the DrawText overloads
that takes a TextFormatFlags parameter. For example,
the default behavior of the TextRenderer is to add padding to the bounding rectangle
of the drawn text to accommodate overhanging glyphs. If you need to draw a line of text
without these extra spaces you should use the versions of DrawText and MeasureText
that take a Size and TextFormatFlags parameter.
Of course if you are going to use TextRendering you want to make sure the
Application.SetCompatibleTextRenderingDefault is set correctly.

Have you looked at the bobpowell article, "Formatting text on a common baseline"?
It gives an example of using the StringFormat class to measure the string length.

I also found a social.msdn thread discussing of TextRenderer.MeasureText versus MeasureCharacterRanges.

It should also be noted that when using "AntiAlias" member for the TextRenderingHint Enumeration:
Quote:
Stem width differences may be noticeable because hinting is turned off.
..but I don't know if this has an effect on kerning or ligatures.

Last edited by hDC_0; 07-09-2012 at 06:16 PM.
Reply With Quote
  #8  
Old 07-09-2012, 05:48 PM
PlausiblyDamp's Avatar
PlausiblyDampDrawString Spacing Between Individual Characters PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

The easiest solution is don't use a variable width font if you don't want variable width on the text rendering. Fonts like Consolas are designed with this in mind.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #9  
Old 07-10-2012, 08:18 AM
AtmaWeapon's Avatar
AtmaWeaponDrawString Spacing Between Individual Characters AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Yeah, every console replacement I've used tends to either:
  • Only show a small list of well-known fixed-width fonts
  • Warn you that not all fonts look pretty.
I've noticed some oddball variance in the kerning of similar strings like hDC_0 talked about, but never found a solution. You might consider trying one of the overloads of DrawString() that takes a bounding rectangle and a StringFormat; playing with those might get you a more consistent experience when using variable-width fonts. But the fact remains that fixed-width fonts are best for consoles.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #10  
Old 07-10-2012, 09:03 AM
DrPunk's Avatar
DrPunkDrawString Spacing Between Individual Characters DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Quote:
Originally Posted by hDC_0 View Post
Have you looked at pInvoking TextOut or ExtTextOut?
It's pretty crude, though, in terms of not really being able to control kerning without a lot of extra coding.

According to his article, "Rendering Text Using the .NET Framework",
another alternative is to use the TextRender Class.

It has a DrawText method and you can:


Of course if you are going to use TextRendering you want to make sure the
Application.SetCompatibleTextRenderingDefault is set correctly.

Have you looked at the bobpowell article, "Formatting text on a common baseline"?
It gives an example of using the StringFormat class to measure the string length.

I also found a social.msdn thread discussing of TextRenderer.MeasureText versus MeasureCharacterRanges.

It should also be noted that when using "AntiAlias" member for the TextRenderingHint Enumeration:
..but I don't know if this has an effect on kerning or ligatures.
TextRenderer gets you much closer results to how controls draw their text than DrawString does, I've found.

It's worth noting that TextRenderer.DrawText using Points doesn't work on Windows 2000. Always best to use the method that requires Rectangle.
__________________
There are no computers in heaven!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
 
DrawString Spacing Between Individual Characters
DrawString Spacing Between Individual Characters
 
-->