2

I am searching for a way to go about finding the generic face name (Sans-Serif, Serif, Monospace, Cursive, Fantasy) for a given font using the .NET framework. My language of preference is C#, but I've been writing source code for nearly 15 years, so if you can give me an example in any modern language, I can go about translating it, myself.

[Edit - Clarification]

I need a way to determine which of the generic font families (Sans-Serif, Serif, Monospace, Cursive, or Fantasy... and possibly even Symbol) any given System.Drawing.Font object belongs to.

As an example, if I create a font from the following line of code:

System.Drawing.Font font = new System.Drawing.Font( "Serif", 12f, System.Drawing.GraphicsUnit.Point );

The resulting font would likely be the 12-point "Times New Roman" font. But I don't want "Times" or "New Roman" or even "Times New Roman", I need to know that it's in the "Serif" family.

3 Answers3

2

You can go with linq

Here the new list of font of Microsoft you looking for.

Example:

  • Sans-Serif or Serif = Microsoft Sans Serif
  • Monospace = Batang


var font = FontFamily.Families
                     .Where(c => c.Name == "Microsoft Sans Serif")
                     .FirstOrDefault();

UPDATE:

Heres you're looking for the Generics font

http://msdn.microsoft.com/en-us/library/system.drawing.fontfamily.aspx

var font = FontFamily.GenericMonospace;
spajce
  • 7,044
  • 5
  • 29
  • 44
  • This doesn't get what I'm looking for. I don't have a `"TheFontYouLookingFor"` in mind. I have an arbitrary `System.Drawing.Font` object, and I need to determine which of the generic families it is from. What I need to get is one of "Sans-Serif", "Serif", "Monospace", "Cursive", or "Fantasy" (and possibly even "Symbol"). – Jason Satterfield Feb 22 '13 at 02:33
  • oh.. i meant the word "TheFontYouLookingFor" is the actual __font name__ `:)` – spajce Feb 22 '13 at 02:34
  • I realize that. But I'm trying to get the generic name from an existing font. – Jason Satterfield Feb 22 '13 at 02:41
  • And I (or rather, my users) have fonts installed. When they make use of a font, I need to get the generic name of that font. Your example implies I need a font that matches a given generic name. I actually need to go the other way. I have the font. I need the generic name. – Jason Satterfield Feb 22 '13 at 02:58
  • did you check the [link](http://msdn.microsoft.com/en-us/library/system.drawing.fontfamily.aspx)? you can see the `GenericSerif` – spajce Feb 22 '13 at 03:01
1

This is a typographic font classification that is not directly available from the Windows font api functions. A possible backdoor is GetFontData(), it lets you access TrueType tables for the font directly. Lots of info there, I'm however not aware of this exact kind of info being available. You can have a look for yourself in the specification of the tables.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for the link. You're right: there was a ton of interesting, and potentially useful information. Unfortunately, I couldn't find anything in the font tables that links a font to a generic family name. Unless I get a better answer, I'm marking this as the answer, since this post was the one that pointed me to the conclusion that there is no guaranteed way to find this information for every font available. Thanks, again, Hans. – Jason Satterfield Feb 22 '13 at 15:45
0

A way to go about finding the generic face name for a given font.

Assuming you have a Font object, you can get the font family name like this:

Font font;
string familyName = font.FontFamily.Name;

To respond to your edit and comment: as far as I could find, no information on the font type is stored in a font. For example, I could design a font and classify it as Fantasy, while you classify the font as Sans-serif. Because there is no clear definition for what each class entails, it is not a property of the font.

There is a system-wide default monospace, sans-serif and serif font (through FontFamily) but that won't tell you whether any font has serifs. Using font.Style you can find out whether a font is cursive, underlined, etc. Through this answer you can find out whether a font is monospaced.

Community
  • 1
  • 1
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • How convenient! I'm not really sure how I overlooked this. Thank you. – Jason Satterfield Feb 22 '13 at 02:14
  • Alright, this didn't quite get what I'm looking for. When I load a font using `Font f = new Font( "MonoSans", 12.0f, GraphicsUnit.Point );` then examine `f.FontFamily.Name`, I get "DejaVu Sans". What I would expect to see is "Sans-Serif" (or some other variation). – Jason Satterfield Feb 22 '13 at 02:22
  • @JasonSatterfield That's weird, when I tried your code I got `Microsoft Sans Serif` – Abdusalam Ben Haj Feb 22 '13 at 02:33
  • You would if you don't have MonoSans.ttf installed. But this still supports my statement. I'm not looking for the font's internal name ("Microsoft Sans Serif" in your case). I'm looking for the generic family name (simply "Sans Serif"). – Jason Satterfield Feb 22 '13 at 02:43