tags:

views:

903

answers:

2

Can I read and preview fonts (mainly ttf and otf) in C#? What other languages can/should I use?

Read:

  • Info like font family, font name

Preview:

  • Use the font to display some text
  • Any way to display all supported font characters?
A: 

To preview a font you can render it out to a form like this:

public partial class MyForm: Form
{
   .
   .
   .
   public void ShowMyFont()
   {
      Graphics graphics = this.CreateGraphics();
      graphics.DrawString("Hello world!", new Font("Arial", 12), Brushes.Black, 0, 0);
   }
}
Charlie
I believe original poster meant previewing not installed fonts - ie. from file.
leafnode
+3  A: 

Caution: don't use System.Drawing / System.Windows.Forms if you want to preview OTF fonts. Unless they're TTF's in disguise, you won't get them to show. System.Drawing, based on GDI+, only supports TTF fonts!

However, if you can use .NET 3.0, you could use

Fonts.GetFontFamilies(location)

from System.Windows.Media namespace (just reference PresentationCore.dll).

From a FontFamily, you can get the individual Typefaces (.ttc files contain more than one 'font', but a FontFamily also combines the various weights and variants). And from a Typeface, you can call TryGetGlyphTypeface to get the GlyphTypeface, which has a CharacterToGlyphMap property, which should tell you which unicode codepoints are physically supported.

It also seems possible to use GlyphTypeface directly, but I see no way that you can handle .ttc files. However, if that's not relevant, just create a GlyphTypeface per file.

I'd advice against trying all Unicode codepoints sequentially though.

Ruben
thanks. i'll look into it. its more of a personal project, so i can use whatever framework there is. i prefer the latest
iceangel89
did u mean ttf when u say ttc?
iceangel89
Nope, .ttc files are so-called TrueType Collection files. They contain several typefaces in one file. For instance Cambria.ttc, which contains Cambria Regular and Cambria Regular Math.
Ruben
oh, fonts shld use that... having separate ttf files seem to just increase the number of fonts i see. i think having even 20 typefaces in 1 file shldnt increase the file size or speed too much ... just my 2 cents
iceangel89