home *** CD-ROM | disk | FTP | other *** search
/ The Elite Hackers Toolkit / TheEliteHackersToolkitVolume1_1998.rar / HACKERS.BIN / appcraks / WD95_V21.ZIP / EZFONT.CPP next >
C/C++ Source or Header  |  1996-01-01  |  2KB  |  82 lines

  1. /*---------------------------------------
  2.    EZFONT.C -- Easy Font Creation
  3.                (c) Charles Petzold, 1996
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <string.h>
  8. #include <math.h>
  9. #include "ezfont.h"
  10.  
  11. HFONT EzCreateFont (HDC hdc, char * szFaceName, int iDeciPtHeight,
  12.                     int iDeciPtWidth, int iAttributes, BOOL fLogRes)
  13.      {
  14.      FLOAT      cxDpi, cyDpi ;
  15.      HFONT      hFont ;
  16.      LOGFONT    lf ;
  17.      POINT      pt ;
  18.      TEXTMETRIC tm ;
  19.  
  20.      SaveDC (hdc) ;
  21.  
  22.      SetGraphicsMode (hdc, GM_ADVANCED) ;
  23.      ModifyWorldTransform (hdc, NULL, MWT_IDENTITY) ;
  24.      SetViewportOrgEx (hdc, 0, 0, NULL) ;
  25.      SetWindowOrgEx   (hdc, 0, 0, NULL) ;
  26.  
  27.      if (fLogRes)
  28.           {
  29.           cxDpi = (FLOAT) GetDeviceCaps (hdc, LOGPIXELSX) ;
  30.           cyDpi = (FLOAT) GetDeviceCaps (hdc, LOGPIXELSY) ;
  31.           }
  32.      else
  33.           {
  34.           cxDpi = (FLOAT) (25.4 * GetDeviceCaps (hdc, HORZRES) /
  35.                                   GetDeviceCaps (hdc, HORZSIZE)) ;
  36.  
  37.           cyDpi = (FLOAT) (25.4 * GetDeviceCaps (hdc, VERTRES) /
  38.                                   GetDeviceCaps (hdc, VERTSIZE)) ;
  39.           }
  40.  
  41.      pt.x = (int) (iDeciPtWidth  * cxDpi / 72) ;
  42.      pt.y = (int) (iDeciPtHeight * cyDpi / 72) ;
  43.  
  44.      DPtoLP (hdc, &pt, 1) ;
  45.  
  46.      lf.lfHeight         = - (int) (fabs (pt.y) / 10.0 + 0.5) ;
  47.      lf.lfWidth          = 0 ;
  48.      lf.lfEscapement     = 0 ;
  49.      lf.lfOrientation    = 0 ;
  50.      lf.lfWeight         = iAttributes & EZ_ATTR_BOLD      ? 700 : 0 ;
  51.      lf.lfItalic         = iAttributes & EZ_ATTR_ITALIC    ?   1 : 0 ;
  52.      lf.lfUnderline      = iAttributes & EZ_ATTR_UNDERLINE ?   1 : 0 ;
  53.      lf.lfStrikeOut      = iAttributes & EZ_ATTR_STRIKEOUT ?   1 : 0 ;
  54.      lf.lfCharSet        = 0 ;
  55.      lf.lfOutPrecision   = 0 ;
  56.      lf.lfClipPrecision  = 0 ;
  57.      lf.lfQuality        = 0 ;
  58.      lf.lfPitchAndFamily = 0 ;
  59.  
  60.      strcpy (lf.lfFaceName, szFaceName) ;
  61.  
  62.      hFont = CreateFontIndirect (&lf) ;
  63.  
  64.      if (iDeciPtWidth != 0)
  65.           {
  66.           hFont = (HFONT) SelectObject (hdc, hFont) ;
  67.  
  68.           GetTextMetrics (hdc, &tm) ;
  69.  
  70.           DeleteObject (SelectObject (hdc, hFont)) ;
  71.  
  72.           lf.lfWidth = (int) (tm.tmAveCharWidth *
  73.                               fabs (pt.x) / fabs (pt.y) + 0.5) ;
  74.  
  75.           hFont = CreateFontIndirect (&lf) ;
  76.           }
  77.  
  78.      RestoreDC (hdc, -1) ;
  79.  
  80.      return hFont ;
  81.      }
  82.