home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_FontNames.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  15KB  |  484 lines

  1. /* $XConsortium: FontNames.c /main/12 1996/10/22 14:18:43 kaleb $ */
  2. /*
  3.  
  4. Copyright (c) 1986  X Consortium
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  19. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  
  23. Except as contained in this notice, the name of the X Consortium shall not be
  24. used in advertising or otherwise to promote the sale, use or other dealings
  25. in this Software without prior written authorization from the X Consortium.
  26.  
  27. */
  28.  
  29. /* $XFree86: xc/lib/X11/FontNames.c,v 1.1.1.2.2.2 1998/05/18 14:08:38 dawes Exp $ */
  30.  
  31. #define NEED_REPLIES
  32. #include "Xlib_private.h"
  33. #include <X11/keysym.h>
  34. #include <alloca.h>
  35.  
  36. extern struct {
  37.     char *alias, *fontname;
  38. } FontAlias[];
  39.  
  40. int RenderFontName(FONTMETRICS *pfm, char *name, Xlib_FontHints *hint, Xlib_Font *xfont)
  41. {
  42.     DBUG_ENTER("RenderFontName")
  43.     char tempstr[256], *family = NULL, *ch;
  44.     char *charset = "iso8859", *weight = "medium", *foundry = "adobe";
  45.     char *width = "normal", slant = 'r', spacing = 'p';
  46.     int pixels = 0, points = 0, hdpi = 0, vdpi = 0, avgwidth = 0;
  47.     int codepage = 1;
  48.  
  49.     if (pfm->fsType & FM_TYPE_FIXED) spacing = 'm';
  50.  
  51.     if (pfm->fsType & FM_TYPE_ATOMS)
  52.         family = XGetAtomName(NULL, (Atom)pfm->FamilyNameAtom);
  53.     if (!family) {
  54.         family = malloc(strlen(pfm->szFamilyname)+16);
  55.         if (!family) return 0;
  56.         strcpy(family, pfm->szFamilyname);
  57.     }
  58.  
  59.     for (ch = family; *ch; ch++) 
  60.     if (*ch == '-') *ch = ' '; else *ch = tolower(*ch);
  61.  
  62.     if (!strcmp(family, "warpsans") || !strncmp(family, "lotus", 5) ||
  63.         strstr(family, "klinzhai") || strstr(family, "symbol") || 
  64.         strstr(family, "bar code") || !strncmp(family, "wing", 4))
  65.       foundry = "misc"; else
  66.     if (strstr(family, " mt") || strstr(family, " ms") ||
  67.         !strncmp(family, "monotype", 8)) foundry = "monotype";
  68.     
  69.     if (!strcmp(family, "tms rmn") || !strcmp(family, "times new roman")) strcpy(family, "times"); else
  70.     if (!strcmp(family, "helv")) strcpy(family, "helvetica"); else
  71.     if (!strncmp(family, "system ", 7)) {strcpy(family, family+7); foundry="misc";}
  72.     if (!strcmp(family, "vio")) {strcpy(family, "fixed"); foundry="misc"; spacing = 'c';}
  73.  
  74.     if (pfm->fsDefn & FM_DEFN_OUTLINE && hint && 
  75.        (hint->avgwidth || hint->pixels || hint->points)) { /* outline font */
  76.         HPS hps = WinGetScreenPS(hwndDesktop);
  77.         FATTRS fat = { sizeof(FATTRS), 0, pfm->lMatch, "",
  78.             pfm->idRegistry, 1004, 0, 0, 0, 0 };
  79.         strncpy(fat.szFacename,pfm->szFacename,FACESIZE);
  80.  
  81.         if (GpiCreateLogFont(hps, NULL, 254L, &fat) != GPI_ERROR) {
  82.             SIZEL sizl = {0,0};
  83.             SIZEF sizf = {0,0};
  84.             int psmode = 0;
  85.             long my_pts = 0, my_pel = 0;
  86.  
  87.             if (!hint->pixels && hint->points && hint->vres) {
  88.                 my_pts = hint->points;
  89.                 my_pel = (my_pts * hint->vres /* + 359 */) / 720L;
  90.             }
  91.             if (!my_pel && hint->pixels) {
  92.                 my_pel = hint->pixels;
  93.             }
  94.  
  95.             if (hint->avgwidth || my_pel) {
  96.                 if (hint->avgwidth)
  97.                     sizf.cx = MAKEFIXED(hint->avgwidth,0) / 10;
  98.                 if (my_pel)
  99.                     sizf.cy = MAKEFIXED(my_pel,0);
  100.                 if (!sizf.cy) sizf.cy = sizf.cx;
  101.                 if (!sizf.cx) sizf.cx = sizf.cy;
  102.                 GpiSetCharSet(hps, 254L);
  103.                 if (!GpiSetCharBox(hps, &sizf)) printf("GpiSetCharBox Error\n");
  104.                 GpiQueryFontMetrics(hps, sizeof(FONTMETRICS), pfm);
  105.                 if (hint->avgwidth && pfm->lAveCharWidth) 
  106.                     sizf.cx = (sizf.cx * hint->avgwidth)/(pfm->lAveCharWidth*10);
  107.                 if (my_pel && pfm->lMaxBaselineExt)
  108.                     sizf.cy = (sizf.cy * my_pel)/(pfm->lMaxBaselineExt);
  109.                 else    sizf.cy = sizf.cx;
  110.                 if (!hint->avgwidth) {
  111.                     if (hint->vres && hint->hres && hint->hres != hint->vres)
  112.                         sizf.cx = (sizf.cy/hint->vres) * hint->hres;
  113.                     else
  114.                         sizf.cx = sizf.cy;
  115.                 }
  116.                 if (!GpiSetCharBox(hps, &sizf)) printf("GpiSetCharBox Error\n");
  117.             }
  118.  
  119.             if (xfont) {
  120.                 HDC hdc = GpiQueryDevice(hps);
  121.                 LONG width, height;
  122.                 DevQueryCaps(hdc, CAPS_GRAPHICS_CHAR_WIDTH, 1, &width);
  123.                 DevQueryCaps(hdc, CAPS_GRAPHICS_CHAR_HEIGHT,1, &height);
  124.                 sizf.cx = MAKEFIXED(width, 0);
  125.                 sizf.cy = MAKEFIXED(height, 0);
  126.                 memcpy(&xfont->fattrs, &fat, sizeof(FATTRS));
  127.                 memcpy(&xfont->sizef, &sizf, sizeof(SIZEF));
  128.                 xfont->psmode = 1;
  129.                 xfont->descender = pfm->lMaxDescender;
  130.             }
  131.  
  132.             GpiQueryFontMetrics(hps, sizeof(FONTMETRICS), pfm);
  133.             /*pfm->fsDefn &= ~FM_DEFN_OUTLINE;*/
  134.  
  135.             if (hint->hres) pfm->sXDeviceRes = (hint->hres * 24 + 600) / 25;
  136.             if (hint->vres) pfm->sYDeviceRes = (hint->vres * 24 + 600) / 25;
  137.  
  138.             if (my_pts) points = my_pts; else
  139.             {
  140.                 my_pts = (720 * pfm->lMaxBaselineExt + (hint->vres-1)) / hint->vres;
  141.                 points = my_pts;
  142.             }
  143.  
  144.             GpiSetCharSet(hps, 0L);
  145.             GpiDeleteSetId(hps, 254L);
  146.         } else printf("Failed!");
  147.         WinReleasePS(hps);
  148.     } else 
  149.     if (xfont) {
  150.         FATTRS fat = { sizeof(FATTRS), 0, pfm->lMatch, "",
  151.             pfm->idRegistry, 1004, pfm->lMaxBaselineExt, pfm->lAveCharWidth, 0, 0 };
  152.         strncpy(fat.szFacename,pfm->szFacename,FACESIZE);
  153.         memcpy(&xfont->fattrs, &fat, sizeof(FATTRS));
  154.         xfont->psmode = 0;
  155.     }
  156.  
  157.     if (!(pfm->fsDefn & FM_DEFN_OUTLINE) || hint) { /* bitmapped or hinted font */
  158.         pixels = pfm->lMaxBaselineExt;
  159.         if (!points) points = pfm->lEmHeight * 10;
  160.         hdpi = (25*pfm->sXDeviceRes - 600) / 24;
  161.         vdpi = (25*pfm->sYDeviceRes - 600) / 24;
  162.         avgwidth = pfm->lAveCharWidth * 10;
  163.     }
  164.  
  165.     if (pfm->usCodePage == 437)  { charset = "ibm"; codepage = 437; } else
  166.     if (pfm->usCodePage == 850)  { charset = "ibm"; codepage = 850; } else
  167.     if (pfm->usCodePage == 1208) { charset = "utf"; codepage = 8; } else
  168.     if (pfm->usCodePage == 1200) { charset = "ucs"; codepage = 2; }
  169.  
  170.     switch (pfm->usWeightClass) {
  171.     case 1:    weight = "ultralight"; break;
  172.     case 2: weight = "extralight"; break;
  173.     case 3: weight = "light"; break;
  174.     case 4: weight = "semilight"; break;
  175.     case 6: weight = "semibold"; break;
  176.     case 7: weight = "bold"; break;
  177.     case 8: weight = "extrabold"; break;
  178.     case 9: weight = "ultrabold";
  179.     }
  180.  
  181.     switch (pfm->usWidthClass) {
  182.     case 1: width = "ultracondensed"; break;
  183.     case 2: width = "extracondensed"; break;
  184.     case 3: width = "condensed"; break;
  185.     case 4: width = "semicondensed"; break;
  186.     case 6: width = "semiexpanded"; break;
  187.     case 7: width = "expanded"; break;
  188.     case 8: width = "extraexpanded"; break;
  189.     case 9: width = "ultraexpanded";
  190.     }    
  191.  
  192.     if (pfm->fsSelection & FM_SEL_ITALIC) slant = 'i'; else
  193.     if (pfm->sCharSlope) slant = 'o';
  194.  
  195.     sprintf(tempstr,"-%s-%s-%s-%c-%s--%d-%d-%d-%d-%c-%d-%s-%d",
  196.         foundry, family, weight, slant, width, pixels, 
  197.         points, hdpi, vdpi, spacing, avgwidth, charset, codepage);
  198.  
  199.     free(family);
  200.     if (name) strcpy(name, tempstr);
  201.     DBUG_RETURN(strlen(tempstr) + 1);
  202. }
  203.  
  204. Xlib_FontHints *Xlib_ParseFontHints(HPS hps, char* pattern, int plen)
  205. {
  206.     DBUG_ENTER("Xlib_ParseFontHints")
  207.     if (FontFileCountDashes(pattern,plen) == 14) {
  208.         Xlib_FontHints *hints;
  209.         /* prepare hints.... */
  210.         char *st = (char *)pattern;
  211.         char spixels[8], spoints[8], swidth[8];
  212.         char shres[8], svres[8];
  213.         LONG res[2];
  214.         int i = 0;
  215.         while (i<7) if (*(st++) == '-') i++;
  216.         strncpy(spixels,st,7);
  217.         while (i<8) if (*(st++) == '-') i++;
  218.         strncpy(spoints,st,7);
  219.         while (i<9) if (*(st++) == '-') i++;
  220.         strncpy(shres,st,7);
  221.         while (i<10) if (*(st++) == '-') i++;
  222.         strncpy(svres,st,7);
  223.         while (i<12) if (*(st++) == '-') i++;
  224.         strncpy(swidth,st,7);
  225.         hints = malloc(sizeof(Xlib_FontHints));
  226.         hints->pixels = atoi(spixels);
  227.         hints->points = atoi(spoints);
  228.         hints->avgwidth = atoi(swidth);
  229.         hints->hres = atoi(shres);
  230.         hints->vres = atoi(svres); 
  231.         if (!hints->vres && hints->hres) hints->vres = hints->hres;
  232.         if (!hints->hres && hints->vres) hints->hres = hints->vres;
  233.         if (!hints->hres && !hints->vres) {
  234.             HDC hdc = GpiQueryDevice(hps);
  235.             DevQueryCaps(hdc, CAPS_HORIZONTAL_FONT_RES, 2, res);
  236.             hints->hres = (25*res[0] - 600) / 24;
  237.             hints->vres = (25*res[1] - 600) / 24;
  238.         }
  239. #if 0
  240.         if (hints->points && !hints->pixels) {
  241.             hints->pixels = (hints->points * hints->vres + 360) / 720;
  242.             printf("pixel height = %d\n",hints->pixels);
  243.         }
  244. #endif
  245.         DBUG_RETURN(hints);
  246.     }
  247.     DBUG_RETURN(NULL);
  248. }
  249.  
  250. #if NeedFunctionPrototypes
  251. char **XListFonts(
  252. register Display *dpy,
  253. _Xconst char *pattern,  /* null-terminated */
  254. int maxNames,
  255. int *actualCount)    /* RETURN */
  256. #else
  257. char **XListFonts(dpy, pattern, maxNames, actualCount)
  258. register Display *dpy;
  259. char *pattern;  /* null-terminated */
  260. int maxNames;
  261. int *actualCount;    /* RETURN */
  262. #endif
  263. {       
  264.     DBUG_ENTER("XListFonts")
  265.     char **flist, **fpos;
  266.     char *ch;
  267.  
  268.     if (maxNames <= 0) {
  269.     if (actualCount) *actualCount = 0;
  270.     DBUG_RETURN(NULL);
  271.     }
  272.  
  273.     if (pattern == NULL) {    /* List all fonts */
  274.     HPS hps;
  275.     LONG cFonts, lTemp = 0L, lSize;
  276.     FONTMETRICS *pfm, *cpfm;
  277.     int i, j, aliascount = 0, physcount = 0;
  278.     char *tmp, *tmpbuf;
  279.  
  280.     hps = WinGetScreenPS(hwndDesktop);
  281.     cFonts = GpiQueryFonts(hps, QF_PUBLIC | QF_PRIVATE, NULL, 
  282.             &lTemp, sizeof(FONTMETRICS), NULL);
  283.     if (cFonts > maxNames) cFonts = maxNames;
  284.     pfm = malloc( cFonts * sizeof(FONTMETRICS) );
  285.     GpiQueryFonts(hps, QF_PUBLIC | QF_PRIVATE, NULL,
  286.             &cFonts, sizeof(FONTMETRICS), pfm);
  287.     WinReleasePS(hps);
  288.  
  289.     for (i=0,lSize = 0; FontAlias[i].alias; i++) {
  290.         lSize += strlen(FontAlias[i].alias)+1;
  291.         aliascount++;
  292.     }
  293.     if (aliascount > maxNames) aliascount = maxNames;
  294.     if (cFonts + aliascount > maxNames) cFonts = maxNames - aliascount;
  295.     if (actualCount) *actualCount = cFonts + aliascount;
  296.  
  297.     /* Now we translate the font list into XLFD */
  298.     tmp = tmpbuf = malloc(256 * cFonts);
  299.  
  300.     for (cpfm = pfm, lTemp = 0; lTemp < cFonts; lTemp++, cpfm++) {
  301.         i = RenderFontName(cpfm, tmp, NULL, NULL);
  302.         lSize += i; tmp += i; physcount++;
  303.     }
  304.     free(pfm);
  305.  
  306.     flist = fpos = malloc((cFonts + aliascount + 1) * sizeof(char *));
  307.     ch = malloc (lSize + 1); *(ch++) = '*';    
  308.  
  309.     for (i=0; i<aliascount; i++, fpos++) {
  310.         strcpy((*fpos = ch), FontAlias[i].alias);
  311.         j = strlen(ch) + 1;
  312.         ch += j;
  313.     }
  314.     for (tmp=tmpbuf, i=0; i<physcount; i++, fpos++) {
  315.         strcpy((*fpos = ch), tmp);
  316.         j = strlen(ch) + 1;
  317.         ch += j; tmp += j;
  318.     }
  319.     *fpos = NULL;
  320.     free(tmpbuf);
  321.  
  322.     } else {
  323.  
  324.     HPS hps;
  325.     LONG cFonts, lTemp = 0L, lSize;
  326.     FONTMETRICS *pfm, *cpfm;
  327.     int plen, i, j, aliascount = 0, physcount = 0;
  328.     Xlib_FontHints *hints = NULL;
  329.     char my_pattern[256], *tmp, *tmpbuf;
  330.  
  331.     strcpy(my_pattern, (char *)pattern);
  332.     strlwr(my_pattern); plen = strlen(my_pattern);
  333.  
  334.     hps = WinGetScreenPS(hwndDesktop);
  335.     hints = Xlib_ParseFontHints(hps, my_pattern, plen);
  336.     cFonts = GpiQueryFonts(hps, QF_PUBLIC | QF_PRIVATE, NULL, 
  337.             &lTemp, sizeof(FONTMETRICS), NULL);
  338.     pfm = malloc( cFonts * sizeof(FONTMETRICS) );
  339.     GpiQueryFonts(hps, QF_PUBLIC | QF_PRIVATE, NULL,
  340.             &cFonts, sizeof(FONTMETRICS), pfm);
  341.     WinReleasePS(hps);
  342.  
  343.     for (i=0,lSize = 0; FontAlias[i].alias; i++) {
  344.         j = strlen(FontAlias[i].alias);
  345.         if (!PatternMatch(my_pattern, plen, FontAlias[i].alias, j)) continue;
  346.         lSize += j + 1; aliascount++;
  347.     }
  348.     if (aliascount > maxNames) {
  349.         aliascount = maxNames;
  350.         tmpbuf = NULL;
  351.     } else {
  352.         /* Now we translate the font list into XLFD */
  353.         tmp = tmpbuf = malloc(256 * cFonts);
  354.  
  355.         for (cpfm = pfm, lTemp = 0; lTemp < cFonts; lTemp++, cpfm++) {
  356.             i = RenderFontName(cpfm, tmp, hints, NULL);
  357.             if (!PatternMatch(my_pattern, plen, tmp, i-1)) continue;
  358.             lSize += i; tmp += i; physcount++;
  359.         }
  360.         free(pfm); if (hints) free(hints);
  361.  
  362.         if (physcount + aliascount > maxNames) physcount = maxNames - aliascount;
  363.     }
  364.     if (actualCount) *actualCount = physcount + aliascount;
  365.  
  366.     flist = fpos = malloc( (physcount + aliascount + 1) * sizeof(char *));
  367.     ch = malloc (lSize + 1); *(ch++) = '*';    
  368.     flist[0] = ch;
  369.  
  370.     for (i=0; i<aliascount; i++, fpos++) {
  371.         j = strlen(FontAlias[i].alias);
  372.         if (!PatternMatch(my_pattern, plen, FontAlias[i].alias, j)) continue;
  373.         strcpy((*fpos = ch), FontAlias[i].alias);
  374.         ch += j + 1;
  375.     }
  376.     if (tmpbuf)
  377.     for (tmp=tmpbuf, i=0; i<physcount; i++, fpos++) {
  378.         strcpy((*fpos = ch), tmp);
  379.         j = strlen(ch) + 1;
  380.         ch += j; tmp += j;
  381.     }
  382.     *fpos = NULL;
  383.     if (tmpbuf) free(tmpbuf);
  384.     }
  385.     DBUG_RETURN(flist);
  386. }
  387.  
  388. int XFreeFontNames(list)
  389. char **list;
  390. {       
  391.     DBUG_ENTER("XFreeFontNames")
  392.     if (list) {
  393.         DBUG_POINT("*(list[0]-1)");
  394.         if (!*(list[0]-1)) { /* from ListFontsWithInfo */
  395.             register char **names;
  396.             for (names = list+1; *names; names++) {
  397.                 DBUG_POINT("Xfree(*names)");
  398.                 Xfree (*names);
  399.             }
  400.         }
  401.         DBUG_POINT("Xfree(list[0]-1)");
  402.         Xfree (list[0]-1);
  403.         DBUG_POINT("Xfree(list)");
  404.         Xfree ((char *)list);
  405.     }
  406.     DBUG_RETURN(1);
  407. }
  408.  
  409. int PatternMatch(pat, patdashes, string, stringdashes)
  410.     char       *pat;
  411.     char       *string;
  412. {
  413.     DBUG_ENTER("PatternMatch")
  414.     char        c,
  415.                 t;
  416.  
  417.     if (stringdashes < patdashes)
  418.     DBUG_RETURN(0);
  419.     for (;;) {
  420.     switch (c = *pat++) {
  421.     case '*':
  422.         if (!(c = *pat++))
  423.         DBUG_RETURN(1);
  424.         if (c == XK_minus) {
  425.         patdashes--;
  426.         for (;;) {
  427.             while ((t = *string++) != XK_minus)
  428.             if (!t)
  429.                 DBUG_RETURN(0);
  430.             stringdashes--;
  431.             if (PatternMatch(pat, patdashes, string, stringdashes))
  432.             DBUG_RETURN(1);
  433.             if (stringdashes == patdashes)
  434.             DBUG_RETURN(0);
  435.         }
  436.         } else {
  437.         for (;;) {
  438.             while ((t = *string++) != c) {
  439.             if (!t)
  440.                 DBUG_RETURN(0);
  441.             if (t == XK_minus) {
  442.                 if (stringdashes-- < patdashes)
  443.                 DBUG_RETURN(0);
  444.             }
  445.             }
  446.             if (PatternMatch(pat, patdashes, string, stringdashes))
  447.             DBUG_RETURN(1);
  448.         }
  449.         }
  450.     case '?':
  451.         if (*string++ == XK_minus)
  452.         stringdashes--;
  453.         break;
  454.     case '\0':
  455.         return (*string == '\0');
  456.     case XK_minus:
  457.         if (*string++ == XK_minus) {
  458.         patdashes--;
  459.         stringdashes--;
  460.         break;
  461.         }
  462.         DBUG_RETURN(0);
  463.     default:
  464.         if (c == *string++)
  465.         break;
  466.         DBUG_RETURN(0);
  467.     }
  468.     }
  469. }
  470.  
  471. int
  472. FontFileCountDashes (name, namelen)
  473.     char    *name;
  474.     int        namelen;
  475. {
  476.     DBUG_ENTER("FontFileCountDashes")
  477.     int    ndashes = 0;
  478.  
  479.     while (namelen--)
  480.     if (*name++ == '-')
  481.         ++ndashes;
  482.     DBUG_RETURN(ndashes);
  483. }
  484.