Assembly code snippets

Back to the snippets overview

Details

TitlePacking and Unpacking a TTF font into your program for GDI use.
AuthorNaN
Submitted by:NaN
Date added:2002-03-30 09:11:26
Date modified:2002-03-30 09:11:26

Comments

How to go about packing and unpacking a true type font from an .EXE and using it discretely in the application. This fragment will install the TTF file into your windows font table, then use the font as needed, and when the program is to exit the font table entry is removed so the system will *forget* it was once there (effectively *trying* to hide your font being used).

Snippet

ADD THIS TO YOUR RSRC.RC RESOURCE FILE (Joycir2.ttf is the true type font!):
============================================================================

          700 FONT "Joycir2.ttf"


HERE IS THE DATA AND .DATA ENTRIES USED:
========================================
   .data
        szFontName    db "\Joycir.ttf",0
        FontName      db "Joy Circuit",0
        TestString    db "NaN Did It Again!",0

   .data?
        hRes           dd ?
        hData          dd ?
        ResSize        dd ?
        pData          dd ?
        hFile          dd ?
        OutSize        dd ?
        hFont          dd ?

        pInstPath      db (MAX_PATH + 50) dup (?)
        PathSize       dd ?


HERE IS THE SETUP CODE TO INSTALL THIS FONT AS YOUR PROGRAM IS RUNNING
FOR LIMITED USE: =====================================================
================

          invoke FindResource, hInstance, 700, RT_FONT
          mov hRes, eax

          invoke SizeofResource, hInstance, hRes
          mov ResSize, eax

          invoke LoadResource, hInstance, hRes
          mov hData, eax

          invoke LockResource, hData
          mov pData, eax

          ;Note: Windows *says* you need to use SYSTEM dir,
          ;      but works fine in local dir on Win98SE

          ;invoke GetSystemDirectory, addr pInstPath, MAX_PATH
          ;mov PathSize, eax

          invoke GetCurrentDirectory, MAX_PATH, addr pInstPath
          mov PathSize, eax

          invoke szCatStr, addr pInstPath, addr szFontName

          invoke CreateFile, ADDR pInstPath,
                       GENERIC_WRITE, \
                       NULL, \
                       NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,\
                       NULL
          mov hFile, eax

          invoke WriteFile, hFile,pData,ResSize, ADDR OutSize, NULL

          invoke CloseHandle, hFile

          ; -----------------------------------------------------
          ; TTF now extracted from the EXE and saved to disk..
          ;   The entire need for this is since Win9x doesnt have
          ;   API suport to AddFontResource with an HANDLE, instead
          ;   it insists on having the File Path to the TTF font.
          ;
          ; This is DUMB, but its the way Microsoft made it!
          ;   There is a fix for this on Windows NT/2000/XP systems tho!
          ; -----------------------------------------------------

          invoke AddFontResource, addr pInstPath

          ; -----------------------------------------------------
          ; Took a while to get this tweeked!!  The font *was* loaded
          ; onto the system, just right values were needed in the create Font..
          ;
          ; ANSI_CHARSET == Seems to be the defining change needed (from OEM)
          ; OUT_TT_ONLY_PRECIS == Was also needed as much as ANSI_CHARSET!
          ;
          invoke CreateFont,48,24,0,0,500,0,0,0,ANSI_CHARSET,\
                              OUT_TT_ONLY_PRECIS, CLIP_DEFAULT_PRECIS,\
                              DEFAULT_QUALITY,DEFAULT_PITCH or FF_SCRIPT,\
                              ADDR FontName
          mov hFont, eax


          ; -----------------------------------------------------
          ; NOW Use the "hFont" as you would like, where you needed it!
          ;    My example was:
          ; -----------------------------------------------------
          .elseif uMsg == WM_PAINT
             invoke BeginPaint,hWin,ADDR Ps
             mov hDC, eax

               invoke SelectObject, hDC, hFont
               mov hFont,eax

               invoke SetTextColor,hDC,00FF2020h
               invoke SetBkColor,hDC,00C0C0C0h

               invoke TextOut,hDC,10,40,ADDR TestString,SIZEOF TestString - 1

               invoke SelectObject,hDC, hFont
               mov hFont, eax

             invoke Paint_Proc,hWin,hDC
             invoke EndPaint,hWin,ADDR Ps
             return 0


          ; -----------------------------------------------------
          ; LASTLY, clean up considerations:
          ;
          ; -----------------------------------------------------

          .elseif uMsg == WM_DESTROY
             invoke RemoveFontResource, addr pInstPath
             invoke DeleteFile, addr pInstPath
             invoke DeleteObject, hFont
             invoke PostQuitMessage,NULL
             return 0



Well there it is.. Hope you can use it!  If you figure out a better 9x way, please let me know! Enjoy...

NaN