Last time, we talked about how to tell whether text could be displayed using a specific font. And I left you with a teaser dealing with symbol fonts. You see, a symbol font makes it impossible to read the text.
For instance, this would be hard to read if you have Wingdings installed. (Note that this displays in the incorrect font for me on FireFox, but properly in IE -- go figure).
So, how do you display the symbol fonts to the user in something they can read, but still show them the font itself? Simple -- show the font name in Tahoma, then show a simple example of the font, like this:

So the first task we need to accomplish is to tell whether the font is a symbol font or not. This turns out to be reasonably easy (at least, in the simple case). We can use the GetTextCharsetInfo Win32 API to see what character set the font is considered. This will tell us things like whether it's a Big5 Chinese font, or a symbol font. So, in addition to seeing whether the text can be displayed, we also want to check whether the font is a symbol font, like this:[rbcode] dim charset as Integer = GetTextCharsetInfo( g.Handle( Graphics.HandleTypeHDC ), nil, 0 )
Const SYMBOL_CHARSET = 2
if not CanBeDisplayed( g, fontName ) or charset = SYMBOL_CHARSET then
g.TextFont = "Tahoma"
end if[/rbcode]
So if the font is unviewable, or a symbol font (such as Webdings), then it will print the font name in Tahoma.
After displaying the font name in the chosen font, we need to check to see whether we're working with a symbol font. If we are, then we're not done yet -- we still have to display a sample of the symbols. We'll use the same sampling as Word, the string "AbCdEfGhIj" when we display the symbols. The code will look like this:[rbcode] if charset = SYMBOL_CHARSET then
dim x as Integer = g.StringWidth( fontName + " " )
g.TextFont = fontName
dim text as String = "AbCdEfGhIj"
g.DrawString( text, x, y )
end if[/rbcode]
As you can see, we advance to the right a few spaces, then change to the symbol font (from Tahoma) and draw some example symbols. This produces output like this:

As you can see, our symbol fonts are all displayed as we would expect. However, there are some interesting anomalies: notice how there are some fonts that start with WST_ that seem to think they're symbols? I'm not certain why, but they're being reported as symbol fonts, but Word doesn't display them for me. So it's possible that the code we're using to determine whether the font is a symbol font or not isn't fool-proof. But it certainly gets us as close as Word does.
So now your full code should look something like this:[rbcode] dim c as Integer = FontCount
dim y as Integer = Canvas1.Top
dim g as Graphics = Canvas1.Graphics
dim fontName as String
Declare Function GetTextCharsetInfo Lib "Gdi32" ( hdc as Integer, sig as Ptr, flags as Integer ) as Integer
for i as Integer = 0 to c - 1
fontName = Font( i )
g.TextFont = fontName
// This is a hack to select the font into the device context
call g.StringWidth( "Foo" )
dim charset as Integer = GetTextCharsetInfo( g.Handle( Graphics.HandleTypeHDC ), nil, 0 )
Const SYMBOL_CHARSET = 2
if not CanBeDisplayed( g, fontName ) or charset = SYMBOL_CHARSET then
g.TextFont = "Tahoma"
end if
g.DrawString( fontName, 0, y )
if charset = SYMBOL_CHARSET then
dim x as Integer = g.StringWidth( fontName + " " )
g.TextFont = fontName
dim text as String = "AbCdEfGhIj"
g.DrawString( text, x, y )
end if
y = y + g.StringHeight( fontName, Canvas1.Width )
next i[/rbcode]
Enjoy!
Leave a comment