home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / v / v12n06.zip / OUTFON.ZIP / OUTFONTS.C next >
C/C++ Source or Header  |  1992-12-22  |  10KB  |  303 lines

  1. /*-------------------------------------------
  2.    OUTFONTS.C -- Displays OS/2 Outline Fonts
  3.                  (c) Charles Petzold, 1993
  4.   -------------------------------------------*/
  5.  
  6. #define INCL_WIN
  7. #define INCL_GPI
  8. #include <os2.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "outfonts.h"
  13.  
  14. #define LCID_FONT    1
  15.  
  16. MRESULT EXPENTRY ClientWndProc (HWND, ULONG, MPARAM, MPARAM) ;
  17.  
  18. int main (void)
  19.      {
  20.      static CHAR  szClientClass [] = "OutFonts" ;
  21.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU  |
  22.                                  FCF_SIZEBORDER    | FCF_MINMAX   |
  23.                                  FCF_SHELLPOSITION | FCF_TASKLIST |
  24.                                  FCF_MENU ;
  25.      HAB          hab ;
  26.      HMQ          hmq ;
  27.      HWND         hwndFrame, hwndClient ;
  28.      QMSG         qmsg ;
  29.  
  30.      hab = WinInitialize (0) ;
  31.      hmq = WinCreateMsgQueue (hab, 0) ;
  32.  
  33.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  34.  
  35.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  36.                                      &flFrameFlags, szClientClass,
  37.                                      "Outline Fonts", 0L,
  38.                                      NULLHANDLE, ID_RESOURCE, &hwndClient) ;
  39.  
  40.      while (WinGetMsg (hab, &qmsg, NULLHANDLE, 0, 0))
  41.           WinDispatchMsg (hab, &qmsg) ;
  42.  
  43.      WinDestroyWindow (hwndFrame) ;
  44.      WinDestroyMsgQueue (hmq) ;
  45.      WinTerminate (hab) ;
  46.      return 0 ;
  47.      }
  48.  
  49. LONG GetAllOutlineFonts (HPS hps, PFONTMETRICS * ppfm)
  50.      {
  51.      LONG         l, lAllFnt, lOutFnt ;
  52.      PFONTMETRICS pfmAll, pfmOut ;
  53.  
  54.                // Find number of fonts
  55.  
  56.      lAllFnt = 0 ;
  57.      lAllFnt = GpiQueryFonts (hps, QF_PUBLIC, NULL, &lAllFnt, 0, NULL) ;
  58.  
  59.      if (lAllFnt ==  0)
  60.           return 0 ;
  61.  
  62.                // Allocate memory for FONTMETRICS structures
  63.  
  64.      pfmAll = (PFONTMETRICS) calloc (lAllFnt, sizeof (FONTMETRICS)) ;
  65.  
  66.      if (pfmAll == NULL)
  67.           return 0 ;
  68.  
  69.      pfmOut = (PFONTMETRICS) calloc (lAllFnt, sizeof (FONTMETRICS)) ;
  70.  
  71.      if (pfmOut == NULL)
  72.           {
  73.           free (pfmAll) ;
  74.           return 0 ;
  75.           }
  76.                // Get all fonts
  77.  
  78.      GpiQueryFonts (hps, QF_PUBLIC, NULL, &lAllFnt,
  79.                          sizeof (FONTMETRICS), pfmAll) ;
  80.  
  81.                // Get all the outline fonts
  82.  
  83.      lOutFnt = 0 ;
  84.  
  85.      for (l = 0 ; l < lAllFnt ; l++)
  86.           if (pfmAll[l].fsDefn & FM_DEFN_OUTLINE)
  87.                pfmOut [lOutFnt ++] = pfmAll [l] ;
  88.  
  89.                // Clean up
  90.  
  91.      free (pfmAll) ;
  92.      pfmOut = (PFONTMETRICS) realloc (pfmOut, lOutFnt * sizeof (FONTMETRICS)) ;
  93.  
  94.      * ppfm = pfmOut ;
  95.  
  96.      return lOutFnt ;
  97.      }
  98.  
  99. MRESULT EXPENTRY ClientWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  100.      {
  101.      static BOOL         fItalic, fUnder, fOutline, fStrike, fBold,
  102.                          fBaseline, fTextBox ;
  103.      static FATTRS       fat ;
  104.      static HWND         hwndMenu ;
  105.      static int          aiPtSizes [] = { 4, 8, 10, 12, 14, 18, 24, 36, 48 } ;
  106.      static LONG         xRes, yRes, lFonts ;
  107.      static PFONTMETRICS pfm ;
  108.      static SHORT        cyClient, sPtSize = IDM_PTS12 ;
  109.      CHAR                szBuffer [FACESIZE + 32] ;
  110.      FONTMETRICS         fm ;
  111.      HDC                 hdc ;
  112.      HPS                 hps ;
  113.      int                 i, iPtSize ;
  114.      LONG                l ;
  115.      POINTL              ptl, aptl [TXTBOX_COUNT] ;
  116.      SIZEF               sizef ;
  117.  
  118.      switch (msg)
  119.       {
  120.           case WM_CREATE:
  121.                          // Get the array of FONTMETRICS structures
  122.  
  123.                hps = WinGetPS (hwnd) ;
  124.                GpiQueryFontAction (hps, QFA_PUBLIC) ;
  125.                lFonts = GetAllOutlineFonts (hps, & pfm) ;
  126.  
  127.                          // Get the font resolution of the device
  128.  
  129.                hdc = GpiQueryDevice (hps) ;
  130.                DevQueryCaps (hdc, CAPS_HORIZONTAL_FONT_RES, 1, &xRes) ;
  131.                DevQueryCaps (hdc, CAPS_VERTICAL_FONT_RES,   1, &yRes) ;
  132.                WinReleasePS (hps) ;
  133.  
  134.                          // Get menu window handle
  135.  
  136.                hwndMenu = WinWindowFromID (WinQueryWindow (hwnd, QW_PARENT),
  137.                                            FID_MENU) ;
  138.                return 0 ;
  139.  
  140.           case WM_SIZE:
  141.                cyClient = HIUSHORT (mp2) ;
  142.                return 0 ;
  143.  
  144.           case WM_COMMAND:
  145.                switch (COMMANDMSG(&msg)->cmd)
  146.                     {
  147.                     case IDM_PTS04:
  148.                     case IDM_PTS08:
  149.                     case IDM_PTS10:
  150.                     case IDM_PTS12:
  151.                     case IDM_PTS14:
  152.                     case IDM_PTS18:
  153.                     case IDM_PTS24:
  154.                     case IDM_PTS36:
  155.                     case IDM_PTS48:
  156.                          WinCheckMenuItem (hwndMenu, sPtSize, FALSE) ;
  157.                          sPtSize = COMMANDMSG(&msg)->cmd ;
  158.                          break ;
  159.  
  160.                     case IDM_ITALIC:      fItalic   = ! fItalic   ;  break ;
  161.                     case IDM_UNDERSCORE:  fUnder    = ! fUnder    ;  break ;
  162.                     case IDM_OUTLINE:     fOutline  = ! fOutline  ;  break ;
  163.                     case IDM_STRIKEOUT:   fStrike   = ! fStrike   ;  break ;
  164.                     case IDM_BOLD:        fBold     = ! fBold     ;  break ;
  165.                     case IDM_BASELINE:    fBaseline = ! fBaseline ;  break ;
  166.                     case IDM_TEXTBOX:     fTextBox  = ! fTextBox  ;  break ;
  167.  
  168.                     default:  return 0 ;
  169.                     }
  170.  
  171.                WinCheckMenuItem (hwndMenu, COMMANDMSG(&msg)->cmd,
  172.                     ! WinIsMenuItemChecked (hwndMenu, COMMANDMSG(&msg)->cmd)) ;
  173.  
  174.                WinInvalidateRect (hwnd, NULL, TRUE) ;
  175.                return 0 ;
  176.  
  177.           case WM_PAINT:
  178.                hps = WinBeginPaint (hwnd, NULLHANDLE, NULL) ;
  179.  
  180.                GpiErase (hps) ;
  181.  
  182.                     // Get new fonts if they've changed
  183.  
  184.                if (QFA_PUBLIC & GpiQueryFontAction (hps, QFA_PUBLIC))
  185.                     {
  186.                     free (pfm) ;
  187.                     lFonts = GetAllOutlineFonts (hps, & pfm) ;
  188.                     }
  189.  
  190.                     // Set POINTL structure to upper left corner of client
  191.  
  192.                ptl.x = 0 ;
  193.                ptl.y = cyClient ;
  194.  
  195.                     // Set the character box for the point size
  196.  
  197.                iPtSize = aiPtSizes [sPtSize - IDM_PTS04] ;
  198.  
  199.                sizef.cx = 65536 * xRes * iPtSize / 72 ;
  200.                sizef.cy = 65536 * yRes * iPtSize / 72 ;
  201.  
  202.                GpiSetCharBox (hps, &sizef) ;
  203.  
  204.                     // Loop through all the bitmap fonts
  205.  
  206.                for (l = 0 ; l < lFonts ; l++)
  207.                     {
  208.                          // Define the FATTRS structure
  209.  
  210.                     fat.usRecordLength  = sizeof (FATTRS) ;
  211.  
  212.                     fat.fsSelection = (fItalic  ? FATTR_SEL_ITALIC     : 0) |
  213.                                       (fUnder   ? FATTR_SEL_UNDERSCORE : 0) |
  214.                                       (fOutline ? FATTR_SEL_OUTLINE    : 0) |
  215.                                       (fStrike  ? FATTR_SEL_STRIKEOUT  : 0) |
  216.                                       (fBold    ? FATTR_SEL_BOLD       : 0) ;
  217.  
  218.                     fat.lMatch          = 0 ;
  219.  
  220.                     strcpy (fat.szFacename, pfm[l].szFacename) ;
  221.  
  222.                     fat.idRegistry      = pfm[l].idRegistry ;
  223.                     fat.usCodePage      = pfm[l].usCodePage ;
  224.                     fat.lMaxBaselineExt = 0 ;
  225.                     fat.lAveCharWidth   = 0 ;
  226.                     fat.fsType          = FATTR_FONTUSE_OUTLINE |
  227.                                           FATTR_FONTUSE_TRANSFORMABLE ;
  228.                     fat.fsFontUse       = 0 ;
  229.  
  230.                          // Create the logical font and select it
  231.  
  232.                     GpiCreateLogFont (hps, NULL, LCID_FONT, &fat) ;
  233.                     GpiSetCharSet (hps, LCID_FONT) ;
  234.  
  235.                          // Query the font metrics of the current font
  236.  
  237.                     GpiQueryFontMetrics (hps, sizeof (FONTMETRICS), &fm) ;
  238.  
  239.                          // Set up a text string to display
  240.  
  241.                     sprintf (szBuffer, " \xDB %s - %d points \xDB ",
  242.                              fm.szFacename, iPtSize) ;
  243.  
  244.                          // Drop POINTL structure to baseline of font
  245.  
  246.                     ptl.y -= fm.lMaxAscender ;
  247.  
  248.                          // Display the character string
  249.  
  250.                     GpiCharStringAt (hps, &ptl, strlen (szBuffer), szBuffer) ;
  251.  
  252.                          // Adjust text box points for left baseline
  253.  
  254.                     GpiQueryTextBox (hps, strlen (szBuffer), szBuffer,
  255.                                      TXTBOX_COUNT, aptl) ;
  256.  
  257.                     for (i = 0 ; i < TXTBOX_COUNT ; i++)
  258.                          {
  259.                          aptl[i].x += ptl.x ;
  260.                          aptl[i].y += ptl.y ;
  261.                          }
  262.  
  263.                          // Possibly display the baseline
  264.  
  265.                     if (fBaseline)
  266.                          {
  267.                          GpiMove (hps, &ptl) ;
  268.                          GpiLine (hps, aptl + TXTBOX_CONCAT) ;
  269.                          }
  270.  
  271.                          // Possibly display the character box
  272.  
  273.                     if (fTextBox)
  274.                          {
  275.                          GpiMove (hps, aptl + TXTBOX_TOPLEFT) ;
  276.                          GpiLine (hps, aptl + TXTBOX_TOPRIGHT) ;
  277.                          GpiLine (hps, aptl + TXTBOX_BOTTOMRIGHT) ;
  278.                          GpiLine (hps, aptl + TXTBOX_BOTTOMLEFT) ;
  279.                          GpiLine (hps, aptl + TXTBOX_TOPLEFT) ;
  280.                          }
  281.  
  282.                          // Drop POINTL structure down to bottom of text
  283.  
  284.                     ptl.y -= fm.lMaxDescender ;
  285.  
  286.                          // Select the default font; delete the logical font
  287.  
  288.                     GpiSetCharSet (hps, LCID_DEFAULT) ;
  289.                     GpiDeleteSetId (hps, LCID_FONT) ;
  290.                     }
  291.  
  292.                WinEndPaint (hps) ;
  293.                return 0 ;
  294.  
  295.           case WM_DESTROY:
  296.                if (lFonts > 0)
  297.                     free (pfm) ;
  298.  
  299.                return 0 ;
  300.           }
  301.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  302.      }
  303.