home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / progs / present / presfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-22  |  5.2 KB  |  219 lines

  1. /*
  2. **        PresFont.c    (NC)Not Copyrighted by The Frobozz Magic Software Company
  3. **
  4. **        Font management functions
  5. */
  6.  
  7. #define INCL_GPI
  8.  
  9. #include <os2.h>
  10. #include <presfont.h>
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. static SHORT sFontSize[FONTSIZES]   = {80, 100, 120, 140, 180, 240};
  17.  
  18. static CHAR  *szFacename[FONTFACES] = {
  19.     "System Proportional",
  20.    "Courier",
  21.     "Helv",
  22.     "Tms Rmn",
  23.     "System Monospaced"
  24. };
  25.  
  26. static LONG  alMatch[FONTFACES][FONTSIZES];
  27. static LONG     lcidFont = 1L;                        // font id dispenser
  28.  
  29.  
  30. typedef struct _fl {                /* fl */
  31.     struct _fl *pflNext;
  32.     USHORT        idFace;
  33.     USHORT        usSize;
  34.     USHORT        fsSelection;
  35.     BOOL            fOutline;
  36.     LONG            lcid;
  37. } FONTLIST;
  38.  
  39. typedef FONTLIST *PFONTLIST;    /* pfl */
  40.  
  41. PFONTLIST pflFirst = NULL;
  42.  
  43. BOOL PresInitFonts(HPS hps)
  44. {
  45.     FONTMETRICS *pfm;
  46.     HDC         hdc;
  47.     LONG        lHorzRes, lVertRes, lRequestFonts, lNumberFonts;
  48.     SHORT       sIndex, sFace, sSize;
  49.  
  50.     hdc = GpiQueryDevice(hps);
  51.     DevQueryCaps(hdc, CAPS_HORIZONTAL_FONT_RES, 1L, &lHorzRes);
  52.     DevQueryCaps(hdc, CAPS_VERTICAL_FONT_RES,   1L, &lVertRes);
  53.  
  54.     for (sFace = 0; sFace < FONTFACES; sFace++) {
  55.         lRequestFonts = 0;
  56.         lNumberFonts = GpiQueryFonts(hps, QF_PUBLIC, szFacename[sFace],
  57.                                       &lRequestFonts, 0L, NULL);
  58.         if (lNumberFonts == 0)
  59.            continue; 
  60.  
  61.         if (lNumberFonts * sizeof(FONTMETRICS) >= 65536L)
  62.            return FALSE;
  63.  
  64.         pfm = malloc((SHORT)lNumberFonts * sizeof(FONTMETRICS));
  65.  
  66.         if (pfm == NULL)
  67.            return FALSE;
  68.  
  69.         GpiQueryFonts(hps, QF_PUBLIC, szFacename[sFace],
  70.                 &lNumberFonts, (LONG)sizeof(FONTMETRICS), pfm);
  71.  
  72.         for (sIndex = 0; sIndex < (SHORT)lNumberFonts; sIndex++) {
  73.             if (pfm[sIndex].sXDeviceRes == (SHORT)lHorzRes &&
  74.                  pfm[sIndex].sYDeviceRes == (SHORT)lVertRes &&
  75.              (pfm[sIndex].fsDefn & 1) == 0) {
  76.                 for (sSize = 0; sSize < FONTSIZES; sSize++) {
  77.                     if (pfm[sIndex].sNominalPointSize == sFontSize[sSize])
  78.                         break;
  79.                 }
  80.             if (sSize != FONTSIZES) {
  81.                     alMatch[sFace][sSize] = pfm[sIndex].lMatch;
  82.                 }
  83.             }
  84.         }
  85.       free(pfm);
  86.     }
  87.     return TRUE;
  88. }
  89.  
  90. LONG EzfCreateLogFont(HPS hps, LONG lcid, USHORT idFace, USHORT idSize,
  91.                                            USHORT fsSelection)
  92. {
  93.     static FATTRS fat = {sizeof(fat)};
  94.  
  95.     if (idFace >= FONTFACES || idSize >= FONTSIZES || alMatch[idFace][idSize] == 0) {
  96.         return FALSE;
  97.     }
  98.     fat.fsSelection    = fsSelection;
  99.     fat.lMatch         = alMatch[idFace][idSize];
  100.  
  101.     strcpy(fat.szFacename, szFacename[idFace]);
  102.  
  103.     return GpiCreateLogFont(hps, NULL, lcid, &fat);
  104. }
  105.  
  106. LONG EzfCreateOutlineFont(HPS hps, LONG lcid, PSZ pszFace)
  107. {
  108.     FATTRS fat;
  109.     fat.usRecordLength    = sizeof(fat);
  110.     fat.fsSelection       = 0;
  111.     fat.lMatch                 = 0;
  112.     fat.idRegistry            = 0;
  113.     fat.usCodePage            = GpiQueryCp(hps);
  114.     fat.lMaxBaselineExt    = 0;
  115.     fat.lAveCharWidth        = 0;
  116.     fat.fsType                = 0;
  117.     fat.fsFontUse            = FATTR_FONTUSE_OUTLINE |
  118.                                   FATTR_FONTUSE_TRANSFORMABLE;
  119.     strcpy(fat.szFacename, pszFace);
  120.     return GpiCreateLogFont(hps, NULL, lcid, &fat);
  121. }
  122.  
  123. PFONTLIST FindFont(USHORT idFace, USHORT usSize, USHORT fsSelection)
  124. {
  125.     PFONTLIST pfl;
  126.     for (pfl = pflFirst; pfl != NULL; pfl = pfl->pflNext) {
  127.         if (idFace == pfl->idFace &&
  128.              usSize == pfl->usSize &&
  129.              fsSelection == pfl->fsSelection) {
  130.             return pfl;
  131.         }
  132.     }
  133.     return NULL;
  134. }
  135.  
  136. PFONTLIST AddFont(USHORT idFace, USHORT usSize, USHORT fsSelection, LONG lcid, BOOL fOutline)
  137. {
  138.     PFONTLIST pfl = malloc(sizeof(FONTLIST));
  139.     pfl->pflNext = pflFirst;
  140.     pflFirst = pfl;
  141.     pfl->idFace = idFace;
  142.     pfl->usSize = usSize;
  143.     pfl->fsSelection = fsSelection;
  144.     pfl->lcid = lcid;
  145.     pfl->fOutline = fOutline;
  146.     return pfl;
  147. }
  148.  
  149. LONG FindOutlineFont(USHORT idFace)
  150. {
  151.     PFONTLIST pfl;
  152.     for (pfl = pflFirst; pfl != NULL; pfl = pfl->pflNext) {
  153.         if (pfl->fOutline && pfl->idFace == idFace) {
  154.             return pfl->lcid;
  155.         }
  156.     }
  157.     return 0L;
  158. }
  159.  
  160. HFONT PresGetFont(HPS hps, PSZ pszFace, USHORT usSize, USHORT fsSelection)
  161. {
  162.     register int i;
  163.     USHORT idFace;
  164.     PFONTLIST pfl;
  165.     LONG lcid;
  166.     BOOL fOutline = FALSE;
  167.     for (i = 0; i < FONTFACES; i++) {
  168.         if (stricmp(pszFace, szFacename[i]) == 0) {
  169.             break;
  170.         }
  171.     }
  172.     if (i == FONTFACES) {
  173.         return NULL;
  174.     }
  175.     idFace = i;
  176.     pfl = FindFont(idFace, usSize, fsSelection);
  177.     if (pfl == NULL) {
  178.         for (i = 0; i < FONTSIZES; i++) {
  179.             if (sFontSize[i] == 10 * usSize) {
  180.                 break;
  181.             }
  182.         }
  183.         lcid = lcidFont;
  184.         if (i == FONTSIZES ||
  185.               EzfCreateLogFont(hps, lcidFont, idFace, i, fsSelection) == GPI_ERROR) {
  186.             if ((lcid = FindOutlineFont(idFace)) == 0L) {
  187.                 EzfCreateOutlineFont(hps, lcidFont, pszFace);
  188.                 lcid = lcidFont++;
  189.             }
  190.             fOutline = TRUE;
  191.         } else {
  192.             lcidFont++;
  193.         }
  194.         pfl = AddFont(idFace, usSize, fsSelection, lcid, fOutline);
  195.     }
  196.     return (HFONT)pfl;
  197. }
  198.  
  199. BOOL PresSetFont(HPS hps, PVOID hf)
  200. {
  201.     PFONTLIST pfl = hf;
  202.     if (GpiSetCharSet(hps, pfl->lcid) == GPI_ERROR) {
  203.         return FALSE;
  204.     }
  205.     if (pfl->fOutline) {            // outline ?
  206.         SIZEF sizf;
  207.         POINTL ptl;
  208.         sizf.cx = sizf.cy = MAKEFIXED(pfl->usSize, 0);
  209.         GpiSetCharBox(hps, &sizf);
  210.         if (pfl->fsSelection & FATTR_SEL_ITALIC) {
  211.             ptl.x = 2; ptl.y = 4;
  212.         } else {
  213.             ptl.x = 0; ptl.y = 1;
  214.         }
  215.         GpiSetCharShear(hps, &ptl);
  216.     }
  217.     return TRUE;
  218. }
  219.