473,809 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
InstalledFontCo llection
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 lfPitchAndFamil y 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 8364
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.Collecti ons;
using System.Componen tModel;
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.Componen tModel.Containe r components = null;

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

//
// TODO: Add any constructor code after InitializeCompo nent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(8, 8);
this.textBox1.M ultiline = true;
this.textBox1.N ame = "textBox1";
this.textBox1.S crollBars = System.Windows. Forms.ScrollBar s.Vertical;
this.textBox1.S ize = new System.Drawing. Size(224, 248);
this.textBox1.T abIndex = 0;
this.textBox1.T ext = "";
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(240, 32);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 1;
this.button1.Te xt = "Go";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(320, 266);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.textBox 1);
this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}
#endregion

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

[DllImport("Gdi3 2.dll")]
protected static extern int GetObject(Syste m.IntPtr hGDIObj, int chBuffer,
System.IntPtr lpBuffer);
private unsafe void button1_Click(o bject sender, System.EventArg s e)
{
System.Text.Str ingBuilder sb=new System.Text.Str ingBuilder();
sb.Append("Mono spaced font families:\r\n") ;
foreach(FontFam ily ff in FontFamily.Fami lies)
{
Font fn=null;
if(ff.IsStyleAv ailable(FontSty le.Regular))
fn=new Font(ff,10);
else if(ff.IsStyleAv ailable(FontSty le.Italic))
fn=new Font(ff,10,Font Style.Italic);
else if(ff.IsStyleAv ailable(FontSty le.Bold))
fn=new Font(ff,10,Font Style.Bold);
else
break;
LOGFONT[] lf=new LOGFONT[]{new LOGFONT()};
//fn.ToLogFont((o bject)lf);

GetObject(fn.To Hfont(),sizeof( LOGFONT),Marsha l.UnsafeAddrOfP innedArrayEleme n
t(lf,0));
if((lf[0].lfPitchAndFami ly & 0x03)
!=(int)PitchAnd FamilyDefs.FIXE D_PITCH) //This isn't working becase the info
is not in the LOGFONT!!! note that the operator should be == not !=
{
sb.Append(ff.Na me+"\r\n");
}
fn.Dispose();
}
this.textBox1.T ext=sb.ToString ();
}
}

public enum OutPrecisionDef s
{
OUT_DEFAULT_PRE CIS,
OUT_STRING_PREC IS,
OUT_CHARACTER_P RECIS,
OUT_STROKE_PREC IS,
OUT_TT_PRECIS,
OUT_DEVICE_PREC IS,
OUT_RASTER_PREC IS,
OUT_TT_ONLY_PRE CIS,
OUT_OUTLINE_PRE CIS,
OUT_SCREEN_OUTL INE_PRECIS,
OUT_PS_ONLY_PRE CIS
}

public enum ClipPrecisionDe fs
{
CLIP_DEFAULT_PR ECIS = 0,
CLIP_CHARACTER_ PRECIS = 1,
CLIP_STROKE_PRE CIS = 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_QUA LITY, //4
CLEARTYPE_QUALI TY, //5
CLEARTYPE_NATUR AL_QUALITY //6
}

public enum PitchAndFamilyD efs
{
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_CHARSE T =128,
HANGEUL_CHARSET =129,
HANGUL_CHARSET =129,
GB2312_CHARSET =134,
CHINESEBIG5_CHA RSET =136,
OEM_CHARSET =255,
JOHAB_CHARSET =130,
HEBREW_CHARSET =177,
ARABIC_CHARSET =178,
GREEK_CHARSET =161,
TURKISH_CHARSET =162,
VIETNAMESE_CHAR SET =163,
THAI_CHARSET =222,
EASTEUROPE_CHAR SET =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(La youtKind.Sequen tial)]
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 lfPitchAndFamil y;

}
}
"Brian P. Bailey" <NS************ @hotmail.com> wrote in message
news:FNAzb.4123 72$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
InstalledFontCo llection
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 lfPitchAndFamil y 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.Measur eString() 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.In stalledFontsCha nged 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.Measur eString() 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.3077 03$ao4.1054661@ attbi_s51...
I've tried my idea of using Graphics.Measur eString() 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
1243
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 win32 box using Mark Hammond's win32all extensions? Thanks,
4
11972
by: L | last post by:
Hi there, Does C# support OpenType fonts? My c# application is not recognizing OpenType fonts. Thanks, Lalasa.
2
4578
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 in the hashtable has to be removed. But when I remove a pair from the hashtable, inside the enumeration loop, I get an exception. Why cant I remove from a hashtable, while enumerating ?
5
2925
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 out
4
3680
by: Aaron Gray | last post by:
Is there anyway to get a list of all the availiable fonts in Javascript ? Aaron
2
2937
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 servers configured with large fonts. For a reason I cant explain, not only the font changes when settings the window on large font but all the widgets seem to be resized. This is kind of problematic because I have to redraw (resize) all the widgets...
1
3263
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. However, I cannot use the font in a python app that I am writing. The list returned by Tkfont.families does not contain this particular font. Any suggestions ?
12
2354
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 include these fonts by default. http://www.diamondvet.com/test renders those fonts correctly using an XP box. After I installed those fonts on my machine, firefox and mozilla both rendered them correctly. So here is the question:
2
4681
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 those installed fonts. My issue is that I don't want those fonts to be used outside of the main application so that I've set the font characteristics as FR_PRIVATE. However, I want the C# application be able to use those fonts. Although the C# app...
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.