473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Enumerating fonts

Hello, I'm trying to enumerate the fixed-pitch font families installed on a
system. I am _not_ trying to create a generic monospaced font. I've tried
looking at:

FontFamily
InstalledFontCollection
Font

The closest I could get was to use Font.ToLogFont(...). I should be able to
get a GDI LOGFONT structure and then look at the lfPitchAndFamily member.

Problem is, this requires unmanaged code, and I cannot find any quick
examples of how to use this function anyway (e.g., I cannot find a namespace
containing the LOGFONT structure, so how do I cast the object parameter?). I
clearly have no experience in using unmanaged code with .NET.

Is there an easier way to do this, preferably with managed code only? If
not, can someone provide or point me to an example of using the
ToLogFont(...) function, including namespace references?

Thanks in advance,
Brian P. Bailey
Nov 15 '05 #1
4 8335
Well, I was hoping to give you a nice example but I've discovered that much
of the information that I hoped was available is not.

I did manage to put together something that gets what font info is available
and has some interop for the LOGFONT structures but alas the system doesn't
return the information required such as the font pitch and family.

For what it's worth you can see the code after my signature. Perhaps if I
play with it some more I'll be able to squeeze a bit more juice out of it.

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace logfontexmpl
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(224, 248);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 32);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Go";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(320, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

[DllImport("Gdi32.dll")]
protected static extern int GetObject(System.IntPtr hGDIObj, int chBuffer,
System.IntPtr lpBuffer);
private unsafe void button1_Click(object sender, System.EventArgs e)
{
System.Text.StringBuilder sb=new System.Text.StringBuilder();
sb.Append("Monospaced font families:\r\n");
foreach(FontFamily ff in FontFamily.Families)
{
Font fn=null;
if(ff.IsStyleAvailable(FontStyle.Regular))
fn=new Font(ff,10);
else if(ff.IsStyleAvailable(FontStyle.Italic))
fn=new Font(ff,10,FontStyle.Italic);
else if(ff.IsStyleAvailable(FontStyle.Bold))
fn=new Font(ff,10,FontStyle.Bold);
else
break;
LOGFONT[] lf=new LOGFONT[]{new LOGFONT()};
//fn.ToLogFont((object)lf);

GetObject(fn.ToHfont(),sizeof(LOGFONT),Marshal.Uns afeAddrOfPinnedArrayElemen
t(lf,0));
if((lf[0].lfPitchAndFamily & 0x03)
!=(int)PitchAndFamilyDefs.FIXED_PITCH) //This isn't working becase the info
is not in the LOGFONT!!! note that the operator should be == not !=
{
sb.Append(ff.Name+"\r\n");
}
fn.Dispose();
}
this.textBox1.Text=sb.ToString();
}
}

public enum OutPrecisionDefs
{
OUT_DEFAULT_PRECIS,
OUT_STRING_PRECIS,
OUT_CHARACTER_PRECIS,
OUT_STROKE_PRECIS,
OUT_TT_PRECIS,
OUT_DEVICE_PRECIS,
OUT_RASTER_PRECIS,
OUT_TT_ONLY_PRECIS,
OUT_OUTLINE_PRECIS,
OUT_SCREEN_OUTLINE_PRECIS,
OUT_PS_ONLY_PRECIS
}

public enum ClipPrecisionDefs
{
CLIP_DEFAULT_PRECIS = 0,
CLIP_CHARACTER_PRECIS = 1,
CLIP_STROKE_PRECIS = 2,
CLIP_MASK = 0xf,
CLIP_LH_ANGLES = (1<<4),
CLIP_TT_ALWAYS = (2<<4),
CLIP_EMBEDDED = (8<<4)
}

public enum QualityDefs
{
DEFAULT_QUALITY, //0
DRAFT_QUALITY, //1
PROOF_QUALITY, //2
NONANTIALIASED_QUALITY, //3
ANTIALIASED_QUALITY, //4
CLEARTYPE_QUALITY, //5
CLEARTYPE_NATURAL_QUALITY //6
}

public enum PitchAndFamilyDefs
{
DEFAULT_PITCH =0,
FIXED_PITCH =1,
VARIABLE_PITCH =2,
FF_DONTCARE =(0<<4), /* Don't care or don't know. */
FF_ROMAN =(1<<4), /* Variable stroke width, serifed. */
/* Times Roman, Century Schoolbook, etc. */
FF_SWISS =(2<<4), /* Variable stroke width, sans-serifed. */
/* Helvetica, Swiss, etc. */
FF_MODERN =(3<<4), /* Constant stroke width, serifed or
sans-serifed. */
/* Pica, Elite, Courier, etc. */
FF_SCRIPT =(4<<4), /* Cursive, etc. */
FF_DECORATIVE =(5<<4), /* Old English, etc. */
}

public enum CharsetDefs
{
ANSI_CHARSET =0,
DEFAULT_CHARSET =1,
SYMBOL_CHARSET =2,
SHIFTJIS_CHARSET =128,
HANGEUL_CHARSET =129,
HANGUL_CHARSET =129,
GB2312_CHARSET =134,
CHINESEBIG5_CHARSET =136,
OEM_CHARSET =255,
JOHAB_CHARSET =130,
HEBREW_CHARSET =177,
ARABIC_CHARSET =178,
GREEK_CHARSET =161,
TURKISH_CHARSET =162,
VIETNAMESE_CHARSET =163,
THAI_CHARSET =222,
EASTEUROPE_CHARSET =238,
RUSSIAN_CHARSET =204,
MAC_CHARSET =77,
BALTIC_CHARSET =186
}

public enum FontWeightDefs
{
FW_DONTCARE =0,
FW_THIN =100,
FW_EXTRALIGHT =200,
FW_LIGHT =300,
FW_NORMAL =400,
FW_MEDIUM =500,
FW_SEMIBOLD =600,
FW_BOLD =700,
FW_EXTRABOLD =800,
FW_HEAVY =900,
FW_ULTRALIGHT =200,
FW_REGULAR =400,
FW_DEMIBOLD =600,
FW_ULTRABOLD =800,
FW_BLACK =900
}

[StructLayout(LayoutKind.Sequential)]
public struct LOGFONT
{
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;

}
}
"Brian P. Bailey" <NS************@hotmail.com> wrote in message
news:FNAzb.412372$HS4.3330367@attbi_s01...
Hello, I'm trying to enumerate the fixed-pitch font families installed on a system. I am _not_ trying to create a generic monospaced font. I've tried
looking at:

FontFamily
InstalledFontCollection
Font

The closest I could get was to use Font.ToLogFont(...). I should be able to get a GDI LOGFONT structure and then look at the lfPitchAndFamily member.

Problem is, this requires unmanaged code, and I cannot find any quick
examples of how to use this function anyway (e.g., I cannot find a namespace containing the LOGFONT structure, so how do I cast the object parameter?). I clearly have no experience in using unmanaged code with .NET.

Is there an easier way to do this, preferably with managed code only? If
not, can someone provide or point me to an example of using the
ToLogFont(...) function, including namespace references?

Thanks in advance,
Brian P. Bailey

Nov 15 '05 #2
Thanks for the code. I thought of something else, though it is probably
expensive runtime-wise. Perhaps I can get a graphics object and call
Graphics.MeasureString() for each font. If the width of a space is the same
as for string "W" then the font is likely fixed-width. I'll give it a shot,
but I'm sure this will be extremely expensive.

If it is expensive, then maybe I can offload this enumeration to a singleton
object that does it once during initialization and then handles the
SystemEvents.InstalledFontsChanged event.

Darn, this was so easy outside of .NET... It seems kind of stupid that I can
query a font's style and height but not something so basic as whether it is
fixed-width. It's always the little things that drive you nuts. I wonder how
they do it in the VS.NET options dialog: "bold type indicates fixed-width
fonts..."
Nov 15 '05 #3
I've tried my idea of using Graphics.MeasureString() for each font, and that
method seems to work, and it's not too expensive; however, I need to compare
the width of string "i" to string "W" rather than a space. It seems that
MeasureString returns a smaller size for a space, even on fixed-width fonts
like Courier New.

I also noticed something odd about the member Font.GdiCharSet. It has the
value DEFAULT_CHARSET (x01) for all fonts, even WingDings and Symbol. Using
LOGFONT structure correctly identifies these fonts with charset
SYMBOL_CHARSET (x02).

Overall, I get the impression that the Font class is unfinished and buggy.
Is there an official list of bug reports for .NET? I looked up
Font.GdiCharSet and GdiCharSet on KnowledgeBase, and came up with nothing.
Nov 15 '05 #4
Hi, I had noticed the charset thing too.

I don't think that Font is as much unfinished as only implemented just
enough.

I also wonder how much attention will be paid to GDI+ over the next few
years with attention focussed on Avalon.

--
Bob Powell [MVP]
C#, System.Drawing

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Brian P. Bailey" <NS************@hotmail.com> wrote in message
news:AUVzb.307703$ao4.1054661@attbi_s51...
I've tried my idea of using Graphics.MeasureString() for each font, and that method seems to work, and it's not too expensive; however, I need to compare the width of string "i" to string "W" rather than a space. It seems that
MeasureString returns a smaller size for a space, even on fixed-width fonts like Courier New.

I also noticed something odd about the member Font.GdiCharSet. It has the
value DEFAULT_CHARSET (x01) for all fonts, even WingDings and Symbol. Using LOGFONT structure correctly identifies these fonts with charset
SYMBOL_CHARSET (x02).

Overall, I get the impression that the Font class is unfinished and buggy. Is there an official list of bug reports for .NET? I looked up
Font.GdiCharSet and GdiCharSet on KnowledgeBase, and came up with nothing.

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: J Turner | last post by:
Hi All, I've googled and googled, but I can't seem to track this down anywhere... Point me to a thread if I've missed it. How would one retrieve a list of all available system fonts on a...
4
by: L | last post by:
Hi there, Does C# support OpenType fonts? My c# application is not recognizing OpenType fonts. Thanks, Lalasa.
2
by: Tony | last post by:
I have this problem - I have a hashtable, containing a list of filenames. Every 60 seconds, I have a thread that enumerates thru this hashtable, and based on some simple logic, some of the items...
5
by: Colin McGuire | last post by:
Hi again. I have written the following small piece of code Dim f As FontFamily For Each f In System.Drawing.FontFamily.Families Console.WriteLine(f.Name) Next f and when executed it prints...
4
by: Aaron Gray | last post by:
Is there anyway to get a list of all the availiable fonts in Javascript ? Aaron
2
by: Luc | last post by:
I saw a few posts on this newsgroup about it but nothing to help me resolve this problem: We designed a window in .NET on a platform using small fonts (120 ppp). But this window will run on...
1
by: Atul | last post by:
Hi, I have installed a truetype font (.ttf) on a linux machne (SUSE linux 10, KDE) by copying it to my .fonts folder. I can use the font in all applications like open-office and firefox browser....
12
by: ctclibby | last post by:
Hi all Have a customer that wants a specific font included in her web page design; Comic Sans MS. This is one of the MS core fonts. The problem I have is that some of the *nix browsers do NOT...
2
by: Gary | last post by:
1. I've installed a bunch of fonts in main application, which is in native C++ code using AddFontResourceEx 2. A C# application is launched from this main application, but failed to enumerate...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.