Author Topic: Making the Registered Fonts/Font Dialog Box Wiki Example Better  (Read 1205 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Here is my addition to the Wiki example for getting the list of registered fonts and displaying a Font Dialog box. Once you pick the font, my special function searches an array containing the registered fonts grabbed from the registry keys and then uses another function of mine to search the array several times until it finds the best (from my perspective) match. I'm sure it is sloppy. I used that source merger program and tried to remove as much extra code as possible and keep from having too many $INCLUDEs being left out so people wouldn't be confused. Anyways, here is the code that I made using the Wiki plus some of my own functions. Feel free to tell me if I have something royally screwed and/or use it however you like!
Code: QB64: [Select]
  1. $IF FONTDBI = UNDEFINED THEN
  2.     $LET FONTDBI = TRUE
  3.     CONST HKEY_CLASSES_ROOT = &H80000000~&
  4.     CONST HKEY_CURRENT_USER = &H80000001~&
  5.     CONST HKEY_LOCAL_MACHINE = &H80000002~&
  6.     CONST HKEY_USERS = &H80000003~&
  7.     CONST HKEY_PERFORMANCE_DATA = &H80000004~&
  8.     CONST HKEY_CURRENT_CONFIG = &H80000005~&
  9.     CONST HKEY_DYN_DATA = &H80000006~&
  10.     CONST REG_OPTION_VOLATILE = 1
  11.     CONST REG_OPTION_NON_VOLATILE = 0
  12.     CONST REG_CREATED_NEW_KEY = 1
  13.     CONST REG_OPENED_EXISTING_KEY = 2
  14.     CONST REG_NONE = 0
  15.     CONST REG_SZ = 1
  16.     CONST REG_EXPAND_SZ = 2
  17.     CONST REG_BINARY = 3
  18.     CONST REG_DWORD_LITTLE_ENDIAN = 4 '   value is defined REG_DWORD in Windows header files
  19.     CONST REG_DWORD = 4 '                  32-bit number
  20.     CONST REG_DWORD_BIG_ENDIAN = 5 '       some UNIX systems support big-endian architectures
  21.     CONST REG_LINK = 6
  22.     CONST REG_MULTI_SZ = 7
  23.     CONST REG_RESOURCE_LIST = 8
  24.     CONST REG_FULL_RESOURCE_DESCRIPTOR = 9
  25.     CONST REG_RESOURCE_REQUIREMENTS_LIST = 10
  26.     CONST REG_QWORD_LITTLE_ENDIAN = 11 '  64-bit number in little-endian format
  27.     CONST REG_QWORD = 11 '                 64-bit number
  28.     CONST REG_NOTIFY_CHANGE_NAME = 1
  29.     CONST REG_NOTIFY_CHANGE_ATTRIBUTES = 2
  30.     CONST REG_NOTIFY_CHANGE_LAST_SET = 4
  31.     CONST REG_NOTIFY_CHANGE_SECURITY = 8
  32.     CONST KEY_ALL_ACCESS = &HF003F&
  33.     CONST KEY_CREATE_LINK = &H0020&
  34.     CONST KEY_CREATE_SUB_KEY = &H0004&
  35.     CONST KEY_ENUMERATE_SUB_KEYS = &H0008&
  36.     CONST KEY_EXECUTE = &H20019&
  37.     CONST KEY_NOTIFY = &H0010&
  38.     CONST KEY_QUERY_VALUE = &H0001&
  39.     CONST KEY_READ = &H20019&
  40.     CONST KEY_SET_VALUE = &H0002&
  41.     CONST KEY_WOW64_32KEY = &H0200&
  42.     CONST KEY_WOW64_64KEY = &H0100&
  43.     CONST KEY_WRITE = &H20006&
  44.     CONST ERROR_SUCCESS = 0
  45.     CONST ERROR_FILE_NOT_FOUND = &H2&
  46.     CONST ERROR_INVALID_HANDLE = &H6&
  47.     CONST ERROR_MORE_DATA = &HEA&
  48.     CONST ERROR_NO_MORE_ITEMS = &H103&
  49.     DECLARE DYNAMIC LIBRARY "advapi32"
  50.         FUNCTION RegOpenKeyExA& (BYVAL hKey AS _OFFSET, BYVAL lpSubKey AS _OFFSET, BYVAL ulOptions AS _UNSIGNED LONG, BYVAL samDesired AS _UNSIGNED LONG, BYVAL phkResult AS _OFFSET)
  51.         FUNCTION RegCloseKey& (BYVAL hKey AS _OFFSET)
  52.         FUNCTION RegEnumValueA& (BYVAL hKey AS _OFFSET, BYVAL dwIndex AS _UNSIGNED LONG, BYVAL lpValueName AS _OFFSET, BYVAL lpcchValueName AS _OFFSET, BYVAL lpReserved AS _OFFSET, BYVAL lpType AS _OFFSET, BYVAL lpData AS _OFFSET, BYVAL lpcbData AS _OFFSET)
  53.     END DECLARE
  54.     CONST CF_APPLY = &H200& '             Displays Apply button
  55.     CONST CF_ANSIONLY = &H400& '          list ANSI fonts only
  56.     CONST CF_BOTH = &H3& '                list both Screen and Printer fonts
  57.     CONST CF_EFFECTS = &H100& '          Display Underline and Strike Through boxes
  58.     CONST CF_ENABLEHOOK = &H8& '          set hook to custom template
  59.     CONST CF_ENABLETEMPLATE = &H10& '     enable custom template
  60.     CONST CF_ENABLETEMPLATEHANDLE = &H20&
  61.     CONST CF_FIXEDPITCHONLY = &H4000& '  list only fixed-pitch fonts
  62.     CONST CF_FORCEFONTEXIST = &H10000& '  indicate error when font not listed is chosen
  63.     CONST CF_INACTIVEFONTS = &H2000000& ' display hidden fonts in Win 7 only
  64.     CONST CF_INITTOLOGFONTSTRUCT = &H40& 'use the structure pointed to by the lpLogFont member
  65.     CONST CF_LIMITSIZE = &H2000& '        select font sizes only within nSizeMin and nSizeMax members
  66.     CONST CF_NOOEMFONTS = &H800& '        should not allow vector font selections
  67.     CONST CF_NOFACESEL = &H80000& '       prevent displaying initial selection in font name combo box.
  68.     CONST CF_NOSCRIPTSEL = &H800000& '    Disables the Script combo box
  69.     CONST CF_NOSIMULATIONS = &H1000& '    Disables selection of font simulations
  70.     CONST CF_NOSIZESEL = &H200000& '     Disables Point Size selection
  71.     CONST CF_NOSTYLESEL = &H100000& '     Disables Style selection
  72.     CONST CF_NOVECTORFONTS = &H800&
  73.     CONST CF_NOVERTFONTS = &H1000000&
  74.     CONST CF_OEMTEXT = &H7&
  75.     CONST CF_PRINTERFONTS = &H2& '        list fonts only supported by printer associated with the device
  76.     CONST CF_SCALABLEONLY = &H20000& '    select only vector fonts, scalable printer fonts, and TrueType fonts
  77.     CONST CF_SCREENFONTS = &H1& '        lists only the screen fonts supported by system
  78.     CONST CF_SCRIPTSONLY = &H400& '       lists all non-OEM, Symbol and ANSI sets only
  79.     CONST CF_SELECTSCRIPT = &H400000& '  can only use set specified in the Scripts combo box
  80.     CONST CF_SHOWHELP = &H4& '           displays Help button reference
  81.     CONST CF_TTONLY = &H40000& '         True Type only
  82.     CONST CF_USESTYLE = &H80& '           copies style data for the user's selection to lpszStyle buffer
  83.     CONST CF_WYSIWYG = &H8000& '          only list fonts available on both the printer and display
  84.     CONST BOLD_FONTTYPE = &H100&
  85.     CONST ITALIC_FONTTYPE = &H200&
  86.     CONST PRINTER_FONTTYPE = &H4000&
  87.     CONST REGULAR_FONTTYPE = &H400&
  88.     CONST SCREEN_FONTTYPE = &H2000&
  89.     CONST SIMULATED_FONTTYPE = &H8000&
  90.     CONST FW_DONTCARE = 0
  91.     CONST FW_THIN = 100
  92.     CONST FW_ULTRALIGHT = 200
  93.     CONST FW_LIGHT = 300
  94.     CONST FW_REGULAR = 400
  95.     CONST FW_MEDIUM = 500
  96.     CONST FW_SEMIBOLD = 600
  97.     CONST FW_BOLD = 700
  98.     CONST FW_ULTRABOLD = 800
  99.     CONST FW_HEAVY = 900
  100.     CONST DEFAULT_CHARSET = 1
  101.     CONST LF_DEFAULT = 0
  102.     CONST FF_ROMAN = 16
  103.     CONST LF_FACESIZE = 32
  104.     CONST GMEM_MOVEABLE = &H2
  105.     CONST GMEM_ZEROINIT = &H40
  106.     DECLARE DYNAMIC LIBRARY "comdlg32"
  107.         FUNCTION ChooseFontA& (BYVAL lpcf AS _OFFSET)
  108.         FUNCTION CommDlgExtendedError& () '                'dialog box error checking procedure
  109.     END DECLARE
  110.     TYPE CHOOSEFONT
  111.         lStructSize AS _UNSIGNED LONG
  112.         hwndOwner AS _OFFSET
  113.         HDC AS _OFFSET
  114.         lpLogFont AS _OFFSET
  115.         iPointSize AS LONG
  116.         Flags AS LONG
  117.         rgbColors AS _UNSIGNED LONG
  118.         lCustData AS _OFFSET
  119.         lpfnHook AS _OFFSET
  120.         lpTemplateName AS _OFFSET
  121.         hInstance AS _OFFSET
  122.         lpszStyle AS _OFFSET
  123.         nFontType AS LONG '  if used as Unsigned Integer add Integer padder below
  124.         nSizeMin AS LONG
  125.         nSizeMax AS LONG
  126.     END TYPE
  127.     TYPE LOGFONT
  128.         lfHeight AS LONG
  129.         lfWidth AS LONG
  130.         lfEscapement AS LONG
  131.         lfOrientation AS LONG
  132.         lfWeight AS LONG
  133.         lfItalic AS _BYTE '    not 0 when user selected
  134.         lfUnderline AS _BYTE ' not 0 when user selected
  135.         lfStrikeOut AS _BYTE ' not 0 when user selected
  136.         lfCharSet AS _BYTE
  137.         lfOutPrecision AS _BYTE
  138.         lfClipPrecision AS _BYTE
  139.         lfQuality AS _BYTE
  140.         lfPitchAndFamily AS _BYTE
  141.         lfFaceName AS STRING * 32 'contains name listed in dialog
  142.     END TYPE
  143.     DIM SHARED FontType AS STRING
  144.     DIM SHARED PointSize&
  145.     DIM SHARED FontColor&
  146.     DIM SHARED fontpath AS STRING
  147.     DIM SHARED selectedfont AS STRING
  148.     DIM SHARED FontName AS STRING
  149.     DIM SHARED FontEff AS STRING
  150.  
  151. 'MAIN MODULE
  152. _TITLE "LoadFont Test with Accurate Search"
  153. SCREEN _NEWIMAGE(1280, 720, 32)
  154. AGAIN:
  155. _FONT16
  156. y = CSRLIN
  157. x = POS(0)
  158. f& = LoadFont
  159. IF f& > 0 THEN
  160.     _FONT f&
  161.     LOCATE 20, x
  162.     PRINT "This is "; FontName; ", loaded from "; selectedfont; " in"; PointSize&; "points"
  163.     PRINT "Go again? (Y/N)"
  164.     SYSTEM
  165.     k$ = INKEY$
  166.     IF UCASE$(k$) = "Y" THEN
  167.         GOTO AGAIN
  168.     ELSEIF UCASE$(k$) = "N" THEN
  169.         SYSTEM
  170.     END IF
  171. LOOP UNTIL UCASE$(k$) = "Y" OR UCASE$(k$) = "N"
  172. 'END MAIN MODULE
  173.  
  174. $IF FONTDBM = UNDEFINED THEN
  175.     $LET FONTDBM = TRUE
  176.     SUB GetFonts
  177.         DIM hKey AS _OFFSET
  178.         DIM Ky AS _OFFSET
  179.         DIM SubKey AS STRING
  180.         DIM Value AS STRING
  181.         DIM bData AS STRING
  182.         DIM t AS STRING
  183.         DIM dwType AS _UNSIGNED LONG
  184.         DIM numBytes AS _UNSIGNED LONG
  185.         DIM numTchars AS _UNSIGNED LONG
  186.         DIM l AS LONG
  187.         DIM dwIndex AS _UNSIGNED LONG
  188.         FONTFILE = FREEFILE
  189.         OPEN "F0NTList.INF" FOR OUTPUT AS #FONTFILE 'create a new file for font data
  190.         Ky = HKEY_LOCAL_MACHINE
  191.         SubKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" + CHR$(0)
  192.         Value = SPACE$(261) 'ANSI Value name limit 260 chars + 1 null
  193.         bData = SPACE$(&H7FFF) 'arbitrary
  194.         l = RegOpenKeyExA(Ky, _OFFSET(SubKey), 0, KEY_READ, _OFFSET(hKey))
  195.         IF l THEN
  196.         ELSE
  197.             dwIndex = 0
  198.             DO
  199.                 numBytes = LEN(bData)
  200.                 numTchars = LEN(Value)
  201.                 l = RegEnumValueA(hKey, dwIndex, _OFFSET(Value), _OFFSET(numTchars), 0, _OFFSET(dwType), _OFFSET(bData), _OFFSET(numBytes))
  202.                 IF l THEN
  203.                     EXIT DO
  204.                 ELSE
  205.                     PRINT #FONTFILE, LEFT$(Value, numTchars) + "=" + formatData(dwType, numBytes, bData)
  206.                 END IF
  207.                 dwIndex = dwIndex + 1
  208.             LOOP
  209.             CLOSE #FONTFILE
  210.             l = RegCloseKey(hKey)
  211.         END IF
  212.     END SUB
  213.     FUNCTION whatType$ (dwType AS _UNSIGNED LONG)
  214.         SELECT CASE dwType
  215.             CASE REG_SZ: whatType = "REG_SZ"
  216.             CASE REG_EXPAND_SZ: whatType = "REG_EXPAND_SZ"
  217.             CASE REG_BINARY: whatType = "REG_BINARY"
  218.             CASE REG_DWORD: whatType = "REG_DWORD"
  219.             CASE REG_DWORD_BIG_ENDIAN: whatType = "REG_DWORD_BIG_ENDIAN"
  220.             CASE REG_LINK: whatType = "REG_LINK"
  221.             CASE REG_MULTI_SZ: whatType = "REG_MULTI_SZ"
  222.             CASE REG_RESOURCE_LIST: whatType = "REG_RESOURCE_LIST"
  223.             CASE REG_FULL_RESOURCE_DESCRIPTOR: whatType = "REG_FULL_RESOURCE_DESCRIPTOR"
  224.             CASE REG_RESOURCE_REQUIREMENTS_LIST: whatType = "REG_RESOURCE_REQUIREMENTS_LIST"
  225.             CASE REG_QWORD: whatType = "REG_QWORD"
  226.             CASE ELSE: whatType = "unknown"
  227.         END SELECT
  228.     FUNCTION whatKey$ (hKey AS _OFFSET)
  229.         SELECT CASE hKey
  230.             CASE HKEY_CLASSES_ROOT: whatKey = "HKEY_CLASSES_ROOT"
  231.             CASE HKEY_CURRENT_USER: whatKey = "HKEY_CURRENT_USER"
  232.             CASE HKEY_LOCAL_MACHINE: whatKey = "HKEY_LOCAL_MACHINE"
  233.             CASE HKEY_USERS: whatKey = "HKEY_USERS"
  234.             CASE HKEY_PERFORMANCE_DATA: whatKey = "HKEY_PERFORMANCE_DATA"
  235.             CASE HKEY_CURRENT_CONFIG: whatKey = "HKEY_CURRENT_CONFIG"
  236.             CASE HKEY_DYN_DATA: whatKey = "HKEY_DYN_DATA"
  237.         END SELECT
  238.     FUNCTION formatData$ (dwType AS _UNSIGNED LONG, numBytes AS _UNSIGNED LONG, bData AS STRING)
  239.         DIM t AS STRING
  240.         DIM ul AS _UNSIGNED LONG
  241.         DIM b AS _UNSIGNED _BYTE
  242.         SELECT CASE dwType
  243.             CASE REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ
  244.                 formatData = LEFT$(bData, numBytes - 1)
  245.             CASE REG_DWORD
  246.                 t = LCASE$(HEX$(CVL(LEFT$(bData, 4))))
  247.                 formatData = "0x" + STRING$(8 - LEN(t), &H30) + t
  248.             CASE ELSE
  249.                 IF numBytes THEN
  250.                     b = ASC(LEFT$(bData, 1))
  251.                     IF b < &H10 THEN
  252.                         t = t + "0" + LCASE$(HEX$(b))
  253.                     ELSE
  254.                         t = t + LCASE$(HEX$(b))
  255.                     END IF
  256.                 END IF
  257.                 FOR ul = 2 TO numBytes
  258.                     b = ASC(MID$(bData, ul, 1))
  259.                     IF b < &H10 THEN
  260.                         t = t + " 0" + LCASE$(HEX$(b))
  261.                     ELSE
  262.                         t = t + " " + LCASE$(HEX$(b))
  263.                     END IF
  264.                 NEXT
  265.                 formatData = t
  266.         END SELECT
  267.     FUNCTION ShowFont$ (hWnd AS _OFFSET)
  268.         DIM cf AS CHOOSEFONT
  269.         DIM lfont AS LOGFONT
  270.         SHARED FontColor&, FontType$, FontEff$, PointSize AS LONG 'shared with main program
  271.         lfont.lfHeight = LF_DEFAULT ' determine default height '       set dailog box defaults
  272.         lfont.lfWidth = LF_DEFAULT ' determine default width
  273.         lfont.lfEscapement = LF_DEFAULT ' angle between baseline and escapement vector
  274.         lfont.lfOrientation = LF_DEFAULT ' angle between baseline and orientation vector
  275.         lfont.lfWeight = FW_REGULAR ' normal weight i.e. not bold
  276.         lfont.lfCharSet = DEFAULT_CHARSET ' use default character set
  277.         lfont.lfOutPrecision = LF_DEFAULT ' default precision mapping
  278.         lfont.lfClipPrecision = LF_DEFAULT ' default clipping precision
  279.         lfont.lfQuality = LF_DEFAULT ' default quality setting
  280.         lfont.lfPitchAndFamily = LF_DEFAULT OR FF_ROMAN ' default pitch, proportional with serifs
  281.         lfont.lfFaceName = "Calibri" + CHR$(0) ' string must be null-terminated
  282.         cf.lStructSize = LEN(cf) ' size of structure
  283.         cf.hwndOwner = hWnd ' window opening the dialog box
  284.         cf.lpLogFont = _OFFSET(lfont)
  285.         cf.iPointSize = 120 ' 12 point font (in units of 1/10 point)
  286.         cf.Flags = CF_BOTH OR CF_EFFECTS OR CF_FORCEFONTEXIST OR CF_INITTOLOGFONTSTRUCT OR CF_LIMITSIZE
  287.         cf.rgbColors = _RGB(238, 161, 50)
  288.         cf.nFontType = REGULAR_FONTTYPE ' regular font type i.e. not bold or anything
  289.         cf.nSizeMin = 10 ' minimum point size
  290.         cf.nSizeMax = 72 ' maximum point size
  291.         IF ChooseFontA&(_OFFSET(cf)) <> 0 THEN '    'Initiate Dialog and Read user selections
  292.             ShowFont = LEFT$(lfont.lfFaceName, INSTR(lfont.lfFaceName, CHR$(0)) - 1)
  293.             FontColor& = _RGB(_BLUE32(cf.rgbColors), _GREEN32(cf.rgbColors), _RED32(cf.rgbColors))
  294.             IF cf.nFontType AND BOLD_FONTTYPE THEN FontType$ = "Bold"
  295.             IF cf.nFontType AND ITALIC_FONTTYPE THEN FontType$ = FontType$ + " Italic"
  296.             IF cf.nFontType AND REGULAR_FONTTYPE THEN FontType$ = "Regular"
  297.             IF lfont.lfUnderline THEN FontEff$ = "Underline"
  298.             IF lfont.lfStrikeOut THEN FontEff$ = FontEff$ + "Strikeout"
  299.             PointSize = cf.iPointSize \ 10
  300.         ELSE
  301.             ShowFont = ""
  302.         END IF
  303.     FUNCTION LoadFont&
  304.         Font$ = ShowFont(_WINDOWHANDLE) '           call Dialog Box and get the font selection
  305.         IF Font$ <> "" THEN
  306.             F = FREEFILE
  307.             DIM fontlist(n) AS STRING
  308.             OPEN "F0NTList.INF" FOR BINARY AS #F
  309.             IF LOF(F) = 0 THEN GetFonts
  310.             DO
  311.                 IF NOT EOF(F) THEN
  312.                     LINE INPUT #F, regfont$
  313.                     i = i + 1
  314.                     REDIM _PRESERVE fontlist(i) AS STRING
  315.                     fontlist(i) = regfont$
  316.                 END IF
  317.             LOOP UNTIL EOF(F)
  318.             CLOSE #F
  319.             REDIM Results(0) AS Results
  320.                         FFont$ = Font$
  321.             a = Array_String.FindAny(fontlist(), String.Trim(Font$ + " " + FontType$), Results(), 1) 'test1
  322.             PRINT "="; Font$; " "; FontType$
  323.             IF a > 1 OR a = 0 THEN
  324.                 PRINT "Couldn't find it with first search, broadening"
  325.                 a = Array_String.FindAny(fontlist(), String.Trim(Font$ + " " + FontType$), Results(), 0) 'test2
  326.                 PRINT "%"; Font$; " "; FontType$
  327.                 IF a = 0 THEN
  328.                     PRINT "Couldn't find it with second search, broadening"
  329.                     IF LEFT$(Font$, INSTR(Font$, " ")) = "" THEN
  330.                         a = Array_String.FindAny(fontlist(), String.Trim(Font$ + " " + FontType$), Results(), 1) 'test3
  331.                         PRINT "="; String.Trim(Font$ + " " + FontType$)
  332.                     ELSE
  333.                         a = Array_String.FindAny(fontlist(), LEFT$(Font$, INSTR(Font$, " ")) + FontType$, Results(), 1) 'test3
  334.                         PRINT "="; LEFT$(Font$, INSTR(Font$, " ")) + FontType$
  335.                     END IF
  336.                     IF a = 0 THEN
  337.                         PRINT "Couldn't find it with third search, broadening"
  338.                         IF LEFT$(Font$, INSTR(Font$, " ")) = "" THEN
  339.                             a = Array_String.FindAny(fontlist(), String.Trim(Font$ + " " + FontType$), Results(), 0) 'test4
  340.                             PRINT "%"; String.Trim(Font$ + " " + FontType$)
  341.                         ELSE
  342.                             a = Array_String.FindAny(fontlist(), LEFT$(Font$, INSTR(Font$, " ")) + FontType$, Results(), 0) 'test4
  343.                             PRINT "%"; LEFT$(Font$, INSTR(Font$, " ")) + FontType$
  344.                         END IF
  345.                         IF a = 0 THEN
  346.                             PRINT "Couldn't find it with fourth search, broadening"
  347.                             a = Array_String.FindAny(fontlist(), Font$, Results(), 1) 'test5
  348.                             PRINT "="; Font$
  349.                             IF a = 0 THEN
  350.                                 PRINT "Couldn't find it with fifth search, broadening"
  351.                                 a = Array_String.FindAny(fontlist(), Font$, Results(), 0) 'test6
  352.                                 PRINT "%"; Font$
  353.                                 IF a = 0 THEN
  354.                                     PRINT "Couldn't find it with sixth search, broadening"
  355.                                     IF LEFT$(Font$, INSTR(Font$, " ")) = "" THEN
  356.                                         a = Array_String.FindAny(fontlist(), String.Trim(Font$), Results(), 1) 'test7
  357.                                         PRINT "="; String.Trim(Font$)
  358.                                     ELSE
  359.                                         a = Array_String.FindAny(fontlist(), LEFT$(Font$, INSTR(Font$, " ")), Results(), 1) 'test7
  360.                                         PRINT "="; LEFT$(Font$, INSTR(Font$, " "))
  361.                                     END IF
  362.                                     IF a = 0 THEN
  363.                                         PRINT "Couldn't find it with seventh search, broadening"
  364.                                         IF LEFT$(Font$, INSTR(Font$, " ")) = "" THEN
  365.                                             a = Array_String.FindAny(fontlist(), String.Trim(Font$), Results(), 0) 'test8
  366.                                             PRINT "%"; String.Trim(Font$)
  367.                                         ELSE
  368.                                             a = Array_String.FindAny(fontlist(), LEFT$(Font$, INSTR(Font$, " ")), Results(), 0) 'test8
  369.                                             PRINT "%"; LEFT$(Font$, INSTR(Font$, " "))
  370.                                         END IF
  371.                                         IF a = 0 THEN
  372.                                             PRINT "Couldn't find a suitable font matching that search term in all 8 searches"
  373.                                             PRINT "Last ditch effort to find font"
  374.                                             IF LEFT$(Font$, INSTR(Font$, " ")) = "" THEN
  375.                                                 LastDitchEffort$ = LastDitch(fontlist(), String.Trim(Font$)) 'test9
  376.                                             ELSE
  377.                                                 LastDitchEffort$ = LastDitch(fontlist(), LEFT$(Font$, INSTR(Font$, " "))) 'test9
  378.                                             END IF
  379.                                             IF LastDitchEffort$ = "" THEN
  380.                                                 PRINT "Cannot find the font."
  381.                                                 PRINT "Ending"
  382.                                                 END
  383.                                             ELSE
  384.                                                 PRINT LastDitchEffort$
  385.                                             END IF
  386.                                         END IF
  387.                                     END IF
  388.                                 END IF
  389.                             END IF
  390.                         END IF
  391.                     END IF
  392.                 END IF
  393.             END IF
  394.             IF LastDitchEffort$ = "" THEN
  395.                 FOR y = LBOUND(Results) TO UBOUND(Results)
  396.                     IF INSTR(Results(y).Result, LEFT$(Font$, _INSTRREV(Font$, " "))) AND INSTR(UCASE$(FontType$), "ITALIC") = 0 AND INSTR(UCASE$(FontType$), "BOLD") = 0 THEN
  397.                         PRINT Results(y).Result
  398.                         'PRINT "TRUE1"
  399.                         EXIT FOR
  400.                     ELSEIF INSTR(Results(y).Result, Font$ + " " + FontType$) THEN
  401.                         PRINT Results(y).Result
  402.                         'PRINT "TRUE2"
  403.                         EXIT FOR
  404.                     ELSEIF INSTR(UCASE$(FontType$), "ITALIC") AND INSTR(UCASE$(FontType$), "BOLD") = 0 AND INSTR(UCASE$(Results(y).Result), "ITALIC") AND INSTR(UCASE$(Results(y).Result), "BOLD") = 0 THEN
  405.                         PRINT Results(y).Result
  406.                         'PRINT "TRUE3"
  407.                         EXIT FOR
  408.                     ELSEIF INSTR(UCASE$(FontType$), "ITALIC") AND INSTR(UCASE$(FontType$), "BOLD") AND INSTR(UCASE$(Results(y).Result), "ITALIC") AND INSTR(UCASE$(Results(y).Result), "BOLD") THEN
  409.                         PRINT Results(y).Result
  410.                         'PRINT "TRUE4"
  411.                         EXIT FOR
  412.                     ELSEIF INSTR(Results(y).Result, LEFT$(Font$, _INSTRREV(Font$, " "))) THEN
  413.                         PRINT Results(y).Result
  414.                         'PRINT "TRUE5"
  415.                         EXIT FOR
  416.                     ELSEIF INSTR(Results(y).Result, LEFT$(Font$, _INSTRREV(Font$, " "))) AND INSTR(Results(y).Result, FontType$) THEN
  417.                         PRINT Results(y).Result
  418.                         'PRINT "TRUE5"
  419.                         EXIT FOR
  420.                     END IF
  421.                 NEXT
  422.             END IF
  423.             IF LastDitchEffort$ <> "" THEN
  424.                 selectedfont$ = fontlist(Array_String.FirstOrDefault(fontlist(), LastDitchEffort$, 1))
  425.             ELSE
  426.                 selectedfont$ = fontlist(Array_String.FirstOrDefault(fontlist(), Results(y).Result, 1))
  427.             END IF
  428.             FontName$ = LEFT$(selectedfont$, INSTR(selectedfont$, "=") - 1)
  429.             selectedfont$ = RIGHT$(selectedfont$, LEN(selectedfont$) - INSTR(selectedfont$, "="))
  430.             IF selectedfont$ <> "" THEN
  431.                 fontpath$ = ENVIRON$("SYSTEMROOT") + "\Fonts\"
  432.                 font& = _LOADFONT(fontpath$ + selectedfont$, PointSize&)
  433.                 IF INSTR(UCASE$(FontType$), "BOLD") AND INSTR(UCASE$(FontType$), "ITALIC") AND INSTR(UCASE$(FontType$), "EXTRA") = 0 AND INSTR(UCASE$(FontType$), "ULTRA") = 0 THEN ' AND INSTR(UCASE$(FontType$), "DEMI") = 0
  434.                     font& = _LOADFONT(fontpath$ + selectedfont$, PointSize&, "BOLD,ITALIC")
  435.                 ELSEIF INSTR(UCASE$(FontType$), "BOLD") AND INSTR(UCASE$(FontType$), "ITALIC") = 0 AND INSTR(UCASE$(FontType$), "EXTRA") = 0 AND INSTR(UCASE$(FontType$), "ULTRA") = 0 THEN ' AND INSTR(UCASE$(FontType$), "DEMI") = 0 THEN
  436.                     font& = _LOADFONT(fontpath$ + selectedfont$, PointSize&, "BOLD")
  437.                 ELSEIF INSTR(UCASE$(FontType$), "ITALIC") AND INSTR(UCASE$(FontType$), "BOLD") = 0 THEN
  438.                     font& = _LOADFONT(fontpath$ + selectedfont$, PointSize&, "ITALIC")
  439.                 ELSE
  440.                     font& = _LOADFONT(fontpath$ + selectedfont$, PointSize&)
  441.                 END IF
  442.                 IF font& > 0 THEN
  443.                     COLOR FontColor&
  444.                     LoadFont = font&
  445.                 ELSE
  446.                     LoadFont = 0
  447.                 END IF
  448.             END IF
  449.         END IF
  450.     FUNCTION LastDitch$ (fontlist() AS STRING, Font AS STRING)
  451.         DIM searchFont AS STRING
  452.         FOR i = 1 TO LEN(Font$)
  453.             searchFont = Font
  454.             searchFont = String.Insert(searchFont, " ", i)
  455.             a = Array_String.FirstOrDefault(fontlist(), searchFont, 1)
  456.             IF a > 0 THEN
  457.                 EXIT FOR
  458.             END IF
  459.         NEXT
  460.         LastDitch$ = fontlist(a)
  461.     $IF STRINGMETH = UNDEFINED THEN
  462.         $LET STRINGMETH = TRUE
  463.         TYPE Results
  464.             SearchArrayPosition AS LONG
  465.             Result AS STRING
  466.         END TYPE
  467.         FUNCTION String.Trim$ (trimString AS STRING)
  468.             trimString = LTRIM$(RTRIM$(trimString))
  469.             String.Trim = trimString
  470.         END FUNCTION
  471.         FUNCTION String.Insert$ (toChange AS STRING, insert AS STRING, position AS INTEGER)
  472.             newchange$ = toChange
  473.             newchange$ = MID$(newchange$, 1, position - 1) + insert + MID$(newchange$, position, LEN(newchange$) - position + 1)
  474.             String.Insert = newchange$
  475.         END FUNCTION
  476.         FUNCTION Array_String.FindAny (SearchArray() AS STRING, SearchString AS STRING, Results() AS Results, explicit AS INTEGER)
  477.             FOR i = LBOUND(SearchArray) TO UBOUND(SearchArray)
  478.                 IF explicit = 0 THEN
  479.                     IF INSTR(SearchArray(i), SearchString) THEN
  480.                         REDIM _PRESERVE Results(x) AS Results
  481.                         Results(x).Result = SearchArray(i)
  482.                         Results(x).SearchArrayPosition = i
  483.                         x = x + 1
  484.                     END IF
  485.                 ELSEIF explicit = 1 THEN
  486.                     IF SearchArray(i) = SearchString THEN
  487.                         REDIM _PRESERVE Results(x) AS Results
  488.                         Results(x).Result = SearchArray(i)
  489.                         Results(x).SearchArrayPosition = i
  490.                         x = x + 1
  491.                     END IF
  492.                 END IF
  493.             NEXT
  494.             Array_String.FindAny = x
  495.         END FUNCTION
  496.         FUNCTION Array_String.FirstOrDefault (SearchArray() AS STRING, SearchString AS STRING, contains AS INTEGER)
  497.             FOR i = LBOUND(SearchArray) TO UBOUND(SearchArray)
  498.                 IF contains = 1 THEN
  499.                     IF INSTR(SearchArray(i), SearchString) THEN
  500.                         Array_String.FirstOrDefault = i
  501.                         EXIT FUNCTION
  502.                     END IF
  503.                 ELSEIF contains = 0 THEN
  504.                     IF SearchArray(i) = SearchString THEN
  505.                         Array_String.FirstOrDefault = i
  506.                         EXIT FUNCTION
  507.                     END IF
  508.                 END IF
  509.             NEXT
  510.         END FUNCTION
  511.     $END IF
fontdialogtest.png
 
fontdisplay.png
« Last Edit: June 18, 2020, 03:49:31 pm by SpriggsySpriggs »
Shuwatch!