font processing in windows

Status
Not open for further replies.

dereferenced

Ars Scholae Palatinae
706
Have you tried using the EnumFonts() or EnumFontFamiliesEx() functions and testing the tmPitchAndFamily member of the TEXTMETRIC or NEWTEXTMETRIC structure of each font for the TMPF_FIXED_PITCH bit?

Keep this in mind:
If this bit is set the font is a variable pitch font. If this bit is clear the font is a fixed pitch font. Note very carefully that those meanings are the opposite of what the constant name implies.
 

russ-iha

Ars Praefectus
4,407
Subscriptor
Thanks... after consulting your suggestion, google, MSDN, and my API book I managed to come up with something that works.


The first step is calling the EnumerateFontFamily API; essentially, if you pass it a null string as the family name, it returns the basic name of each font installed on your system. (Arial is a basic family. Arial bold, Arial outline, etc are in the Arial family.) So you can enumerate all fonts, as well as fonts within a specific family.

Anyway, you have to give the API call a function pointer for the callback, which I do by giving it AddressOf EnumFontsProc. This procedure will get called for every result returned from the API. Note that the lParam you passed to EnumFontsProc is passed to this EnumFontsProc; this is totally under your control, and lets you have the same EnumFontsProc process a variety of items.

Your EnumFontsProc must return a non-zero value to continue processing. If you return 0, the API assumes you found what you wanted and stops the whole deal.

I did create a function to dereference a pointer in VB. This is key because the API returns a pointer to the structures, rather than the structures themselves.


This stuff could all be modified to look for any kind of information about fonts, etc that you need. Feel free to use it if it would be of any benefit.


Code:
Public Declare Function EnumFontFamilies Lib "gdi32" _
Alias "EnumFontFamiliesA" (ByVal hdc As Long, ByVal lpszFamily As _
String, ByVal lpEnumFontFamProc As Long, ByVal lParam As Long) As Long
Public Declare Sub CopyToPtr Lib "kernel32" Alias "RtlMoveMemory" _
(ByVal ptrDest As Long, Source As Any, ByVal Length As Long)
Public Declare Sub DeRefPtr Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, ByVal PtrSource As Long, ByVal Length As Long)

'font constants
Public Const LF_FULLFACESIZE = 64
Public Const LF_FACESIZE = 32
Public Const TMPF_FIXED_PITCH = &H1
'font types
Public Type LOGFONT
        lfHeight As Long
        lfWidth As Long
        lfEscapement As Long
        lfOrientation As Long
        lfWeight As Long
        lfItalic As Byte
        lfUnderline As Byte
        lfStrikeOut As Byte
        lfCharSet As Byte
        lfOutPrecision As Byte
        lfClipPrecision As Byte
        lfQuality As Byte
        lfPitchAndFamily As Byte
        lfFaceName(1 To LF_FACESIZE) As Byte
End Type
Public Type NEWTEXTMETRIC
        tmHeight As Long
        tmAscent As Long
        tmDescent As Long
        tmInternalLeading As Long
        tmExternalLeading As Long
        tmAveCharWidth As Long
        tmMaxCharWidth As Long
        tmWeight As Long
        tmOverhang As Long
        tmDigitizedAspectX As Long
        tmDigitizedAspectY As Long
        tmFirstChar As Byte
        tmLastChar As Byte
        tmDefaultChar As Byte
        tmBreakChar As Byte
        tmItalic As Byte
        tmUnderlined As Byte
        tmStruckOut As Byte
        tmPitchAndFamily As Byte
        tmCharSet As Byte
        ntmFlags As Long
        ntmSizeEM As Long
        ntmCellHeight As Long
        ntmAveWidth As Long
End Type
Public Type ENUMLOGFONT
        elfLogFont As LOGFONT
        elfFullName(LF_FULLFACESIZE) As Byte
        elfStyle(LF_FACESIZE) As Byte
End Type

'private array that will hold our list of fixed-width fonts
Public FixedFonts() As String

'callback procedure that windows will call for each font installed on the system
Private Function EnumFontsProc(ByVal ptrLogicalData As Long, _
 ByVal ptrPhysicalData As Long, ByVal dwFontType As Long, ByVal _
ptrLpData As Long) As Long
    Dim nlf As ENUMLOGFONT
    Dim ntm As NEWTEXTMETRIC
   
    'dereference the pointers (essentially)
    DeRefPtr nlf, ptrLogicalData, Len(nlf)
    DeRefPtr ntm, ptrPhysicalData, Len(ntm)
        
    'if its a fixed-pitch font, add it to the array.
    If Not (ntm.tmPitchAndFamily And TMPF_FIXED_PITCH) = TMPF_FIXED_PITCH Then
        FixedFonts(UBound(FixedFonts)) = GetNameFromByteArray(nlf.elfLogFont.lfFaceName)
        ReDim Preserve FixedFonts(UBound(FixedFonts) + 1)
    End If
   
    'if we had set false here, the api would have terminated without enumerating
    'any further
    EnumFontsProc = True
   
End Function

'converts a byte array string to a vb unicode string, and strips
'ending null character and/or garbage.
Private Function GetNameFromByteArray(src() As Byte) As String
    Dim t As String
    Dim zeropos As Long
    
    t = StrConv(CStr(src), vbUnicode)
    zeropos = InStr(t, Chr(0))
    If zeropos > 1 Then t = Left(t, zeropos - 1)
    GetNameFromByteArray = t
End Function

'public function called by the user.
Public Sub GetFixedFonts()
    Dim lRet As Long
    
    'set our array to 0 elements
    ReDim FixedFonts(0)
    lRet = EnumFontFamilies(frmMain.hdc, vbNullString, AddressOf EnumFontsProc, 1)
    'the last element will always be blank, so knock it off the end
    ReDim Preserve FixedFonts(UBound(FixedFonts) - 1)
        
End Sub
 
Status
Not open for further replies.