Advertisement
Guest User

modFontInfo.bas

a guest
Feb 7th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Option Explicit
  3.  
  4. Private Declare Function EnumFontFamiliesEx Lib "gdi32" Alias "EnumFontFamiliesExA" (ByVal hDC As Long, _
  5.                                                                                      lpLogFont As LOGFONT, _
  6.                                                                                      ByVal lpEnumFontProc As Long, _
  7.                                                                                      ByVal lParam As Long, _
  8.                                                                                      ByVal dwReserved As Long) As Long
  9. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  10.  
  11. Private Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Long, _
  12.                                                                    ByVal hdc As Long) As Long
  13.  
  14.  
  15. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, _
  16.                                                     ByVal nIndex As Long) As Long
  17.  
  18. Private Const LF_FACESIZE As Long = 32
  19. Private Const LF_FULLFACESIZE As Long = 64
  20.  
  21. Private Const ANSI_CHARSET As Long = 0
  22. Private Const DEFAULT_CHARSET As Long = 1
  23.  
  24. Public Const RASTER_FONTTYPE = &H1        ' raster font
  25. Public Const DEVICE_FONTTYPE = &H2        ' device specific font (depends from hDC)
  26. Public Const TRUETYPE_FONTTYPE = &H4      ' TrueType font
  27.  
  28. Private Const LOGPIXELSY = 90
  29.  
  30. Private m_hDC         As Long
  31. Private m_Charset     As Byte
  32. Private m_PitchFamily As Byte
  33. Private m_FaceName    As String
  34.  
  35. Public Type NEWTEXTMETRIC
  36.   tmHeight            As Long
  37.   tmAscent            As Long
  38.   tmDescent           As Long
  39.   tmInternalLeading   As Long
  40.   tmExternalLeading   As Long
  41.   tmAveCharWidth      As Long
  42.   tmMaxCharWidth      As Long
  43.   tmWeight            As Long
  44.   tmOverhang          As Long
  45.   tmDigitizedAspectX  As Long
  46.   tmDigitizedAspectY  As Long
  47.   tmFirstChar         As Byte
  48.   tmLastChar          As Byte
  49.   tmDefaultChar       As Byte
  50.   tmBreakChar         As Byte
  51.   tmItalic            As Byte
  52.   tmUnderlined        As Byte
  53.   tmStruckOut         As Byte
  54.   tmPitchAndFamily    As Byte
  55.   tmCharSet           As Byte
  56.   ntmFlags            As Long
  57.   ntmSizeEM           As Long
  58.   ntmCellHeight       As Long
  59.   ntmAveWidth         As Long
  60. End Type
  61.  
  62. Public Type FONTSIGNATURE
  63.   fsUsb(4)            As Long
  64.   fsCsb(2)            As Long
  65. End Type
  66.  
  67. Public Type NEWTEXTMETRICEX
  68.   ntmTm               As NEWTEXTMETRIC
  69.   ntmFontSig          As FONTSIGNATURE
  70. End Type
  71.  
  72. Public Type LOGFONT
  73.   lfHeight            As Long
  74.   lfWidth             As Long
  75.   lfEscapement        As Long
  76.   lfOrientation       As Long
  77.   lfWeight            As Long
  78.   lfItalic            As Byte
  79.   lfUnderline         As Byte
  80.   lfStrikeOut         As Byte
  81.   lfCharSet           As Byte
  82.   lfOutPrecision      As Byte
  83.   lfClipPrecision     As Byte
  84.   lfQuality           As Byte
  85.   lfPitchAndFamily    As Byte
  86.   lfFaceName(0 To (LF_FACESIZE - 1)) As Byte
  87. End Type
  88.  
  89. Public Type ENUMLOGFONTEX
  90.   elfLogFont          As LOGFONT
  91.   elfFullName(0 To (LF_FULLFACESIZE - 1)) As Byte
  92.   elfStyle(0 To (LF_FACESIZE - 1))        As Byte
  93.   elfScript(0 To (LF_FACESIZE - 1))       As Byte
  94. End Type
  95.  
  96. Private miSize        As Long     ' font sizes count
  97. Private mlFontInfo    As Long     ' font info count
  98. Private mvaTTsizes    As Variant  ' truetype standard sizes
  99.  
  100. Public Enum enFontType
  101.   ftRaster = RASTER_FONTTYPE
  102.   ftDevice = DEVICE_FONTTYPE
  103.   ftTrueType = TRUETYPE_FONTTYPE
  104. End Enum  
  105.  
  106.  
  107. Public Type tagFontInfo
  108.   FontEx    As ENUMLOGFONTEX
  109.   MetricEx  As NEWTEXTMETRICEX
  110.   nType     As enFontType
  111.   sFaceName As String
  112.   sFullName As String
  113.   sStyle    As String
  114.   sScript   As String
  115.   laSizes() As Long
  116. End Type
  117.  
  118. ' font info data
  119. Public taFontInfo()   As tagFontInfo
  120.  
  121. ' convert from byte array to unicode string and trim ending null
  122. Function TrimZ(ByRef cbStr() As Byte) As String
  123.   Dim sStr As String
  124.  
  125.   sStr = StrConv(cbStr, vbUnicode)
  126.   TrimZ = Left$(sStr, InStr(sStr, vbNullChar) - 1)
  127. End Function
  128.  
  129. ' callback to enumerate sizes
  130. Private Function EnumFontSizesExProc(ByRef lpelfe As ENUMLOGFONTEX, _
  131.                                      ByRef lpntme As NEWTEXTMETRICEX, _
  132.                                      ByVal FontType As Long, _
  133.                                      ByVal lParam As Long) As Long
  134.                                      
  135.   Dim lLogSize As Long, lPointSize As Long
  136.   Dim iSize As Long
  137.                                      
  138.   If FontType <> TRUETYPE_FONTTYPE Then
  139.     ' if not TT, retrieve available sizes
  140.    lLogSize = lpntme.ntmTm.tmHeight - lpntme.ntmTm.tmInternalLeading
  141.     lPointSize = (lLogSize * 72) / GetDeviceCaps(m_hDC, LOGPIXELSY)
  142.     With taFontInfo(lParam)
  143.       For iSize = LBound(.laSizes) To UBound(.laSizes)
  144.         If .laSizes(iSize) = lPointSize Then
  145.           EnumFontSizesExProc = 1
  146.           Exit Function
  147.         End If
  148.       Next iSize
  149.       ReDim Preserve .laSizes(miSize)
  150.       .laSizes(miSize) = lPointSize
  151.       miSize = miSize + 1
  152.       If miSize = 200 Then
  153.         EnumFontSizesExProc = 0
  154.         Exit Function
  155.       End If
  156.     End With
  157.   Else
  158.     ' for TT fonts use fixed table as a reference
  159.    With taFontInfo(lParam)
  160.       ReDim .laSizes(LBound(malTTsizes) To UBound(malTTsizes))
  161.       For iSize = LBound(malTTsizes) To UBound(malTTsizes)
  162.         .laSizes(iSize) = CLng(mvaTTsizes(iSize))
  163.       Next iSize
  164.     End With
  165.     EnumFontSizesExProc = 0
  166.     Exit Function
  167.   End If
  168.                                      
  169.   EnumFontSizesExProc = 1
  170. End Function
  171.  
  172. ' run sizes enumeration for given font
  173. Private Function EnumFontSizes(ByRef lpelfe As ENUMLOGFONTEX, _
  174.                                ByVal lIndex As Long) As Long
  175.   Dim lRet As Long
  176.  
  177.   ' calls the enumeration for the given font to retrieve sizes
  178.  lRet = EnumFontFamiliesEx(m_hDC, lpelfe.elfLogFont, AddressOf EnumFontSizesExProc, lIndex, 0)
  179.   EnumFontSizes = lRet
  180. End Function
  181.  
  182. ' callback to enumerate fonts
  183. Private Function EnumFontFamiliesExProc(ByRef lpelfe As ENUMLOGFONTEX, _
  184.                                         ByRef lpntme As NEWTEXTMETRICEX, _
  185.                                         ByVal FontType As Long, _
  186.                                         ByVal lParam As Long) As Long
  187.   Dim lIndex As Long
  188.   Dim lRet As Long
  189.  
  190.   ' add this font to the array
  191.  lIndex = mlFontInfo
  192.   ReDim Preserve taFontInfo(mlFontInfo)
  193.   With taFontInfo(mlFontInfo)
  194.     LSet .FontEx = lpelfe
  195.     LSet .MetricEx = lpntme
  196.     .nType = FontType
  197.     .sFaceName = TrimZ(lpelfe.elfLogFont.lfFaceName)
  198.     .sFullName = TrimZ(lpelfe.elfFullName)
  199.     .sStyle = TrimZ(lpelfe.elfStyle)
  200.     .sScript = TrimZ(lpelfe.elfScript)
  201.     ReDim .laSizes(0)
  202.   End With
  203.   mlFontInfo = mlFontInfo + 1
  204.  
  205.   ' enumerate available sizes for this font
  206.  miSize = 0
  207.   lRet = EnumFontSizes(lpelfe, lIndex)
  208.  
  209.   ' continue fonts enumeration
  210.  EnumFontFamiliesExProc = 1
  211. End Function
  212.  
  213. ' load fonts informations
  214. Public Function LoadFonts(Optional ByVal hDC As Long = -1) As Long
  215.   Dim lf As LOGFONT
  216.   Dim lResult As Long
  217.  
  218.   If hDC = -1 Then
  219.     m_hDC = GetDC(0)
  220.   Else
  221.     m_hDC = hDC
  222.   End If
  223.  
  224.   ' font enumeration parameters
  225.  m_Charset = DEFAULT_CHARSET
  226.   m_PitchFamily = 0
  227.   m_FaceName = vbNullString
  228.  
  229.   ' sizes table for TT fonts
  230.  mvaTTsizes = Array(8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72)
  231.  
  232.   ' array of font informations
  233.  mlFontInfo = 0
  234.   ReDim taFontInfo(mlFontInfo)
  235.      
  236.   ' setup parameters
  237.  lf.lfCharSet = m_Charset
  238.   lf.lfPitchAndFamily = m_PitchFamily
  239.   lf.lfFaceName(0) = 0
  240.  
  241.   ' enumerate fonts
  242.  lResult = EnumFontFamiliesEx(m_hDC, lf, AddressOf EnumFontFamiliesExProc, 0, 0)
  243.  
  244.   If hDC = -1 Then
  245.     ReleaseDC(0)
  246.   End If
  247.  
  248.   LoadFonts = lResult
  249. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement