Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members  

xfont.c

Go to the documentation of this file.
00001 /*  $Id: xfont.c,v 1.2 2000/12/06 18:42:30 dbryson Exp $
00002 **
00003 **  Copyright (c) 2000 Derry Bryson <derry@techass.com>
00004 **
00005 **  Description:
00006 **  
00007 **    Postscript Library
00008 **
00009 **
00010 **  License:
00011 **
00012 **    This library is free software; you can redistribute it and/or
00013 **    modify it under the terms of the GNU Lesser General Public
00014 **    License as published by the Free Software Foundation; either
00015 **    version 2.1 of the License, or (at your option) any later version.
00016 **
00017 **    This library is distributed in the hope that it will be useful,
00018 **    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 **    Lesser General Public License for more details.
00021 **
00022 **    You should have received a copy of the GNU Lesser General Public
00023 **    License along with this library; if not, write to the Free Software
00024 **    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 **
00026 **
00027 **  Contact:
00028 **
00029 **      Mail:
00030 **
00031 **        Technology Associates, Inc.
00032 **        LIBPS Project
00033 **        1455 Deming Way #11
00034 **        Sparks, NV  89431
00035 **        USA
00036 **
00037 **      Email:
00038 **
00039 **        libps@techass.com
00040 **
00041 **      See our website at:
00042 **
00043 **        libps.sourceforge.net
00044 **
00045 */
00046 
00051 #ifdef HAVE_CONFIG_H
00052 # include "psconfig.h"
00053 #endif
00054 
00055 #include <stdio.h>
00056 #include <stdlib.h>
00057 #include <string.h>
00058 #include <unistd.h>
00059 #include <sys/stat.h>
00060 #include <ctype.h>
00061 #include "ps/ps.h"
00062 
00063 #if HAVE_DIRENT_H
00064 # include <dirent.h>
00065 # define NAMLEN(dirent) strlen((dirent)->d_name)
00066 #else
00067 # define dirent direct
00068 # define NAMLEN(dirent) (dirent)->d_namlen
00069 # if HAVE_SYS_NDIR_H
00070 #  include <sys/ndir.h>
00071 # endif
00072 # if HAVE_SYS_DIR_H
00073 #  include <sys/dir.h>
00074 # endif
00075 # if HAVE_NDIR_H
00076 #  include <ndir.h>
00077 # endif
00078 #endif
00079 
00080 struct XAttribute
00081 {
00082   char
00083     *token;
00084   
00085   int 
00086     value;
00087 };
00088 
00089 static struct XAttribute
00090   weights[] =
00091   {
00092     { "*", PS_XFONT_WILDCARD },
00093     { "medium", PS_XFONT_WEIGHT_MEDIUM },
00094     { "bold", PS_XFONT_WEIGHT_BOLD },
00095     { "black", PS_XFONT_WEIGHT_BLACK },
00096     { "extrabold", PS_XFONT_WEIGHT_EXTRABOLD },
00097     { "demibold", PS_XFONT_WEIGHT_DEMIBOLD },
00098     { "ultrabold", PS_XFONT_WEIGHT_ULTRABOLD },
00099     { "light", PS_XFONT_WEIGHT_LIGHT },
00100     { "thin", PS_XFONT_WEIGHT_THIN },
00101     { NULL, PS_XFONT_WEIGHT_UNKNOWN }
00102   };
00103 
00104 static struct XAttribute  
00105   slants[] =
00106   {
00107     { "*", PS_XFONT_WILDCARD },
00108     { "r", PS_XFONT_SLANT_ROMAN },
00109     { "i", PS_XFONT_SLANT_ITALIC },
00110     { "o", PS_XFONT_SLANT_OBLIQUE },
00111     { NULL, PS_XFONT_SLANT_UNKNOWN }
00112   };
00113   
00114 static struct XAttribute  
00115   widths[] =
00116   {
00117     { "*", PS_XFONT_WILDCARD },
00118     { "normal", PS_XFONT_WIDTH_NORMAL },
00119     { "condensed", PS_XFONT_WIDTH_CONDENSED },
00120     { "narrow", PS_XFONT_WIDTH_NARROW },
00121     { "double", PS_XFONT_WIDTH_DOUBLE },
00122     { NULL, PS_XFONT_WIDTH_UNKNOWN }
00123   };
00124   
00125 static struct XAttribute  
00126   spacings[] =
00127   {
00128     { "*", PS_XFONT_WILDCARD },
00129     { "m", PS_XFONT_SPACING_MONOSPACE },
00130     { "p", PS_XFONT_SPACING_PROPORTIONAL },
00131     { NULL, PS_XFONT_SPACING_UNKNOWN }
00132   };
00133   
00134 static int
00135 LookupXFontAttr(char *s, struct XAttribute* attr)
00136 {
00137   int
00138     i;
00139     
00140   for(i = 0; attr[i].token; i++)
00141     if(!strcasecmp(attr[i].token, s))
00142       break;
00143       
00144   return attr[i].value;
00145 }
00146 
00147 static char*
00148 LookupXFontAttrToken(int value, struct XAttribute* attr)
00149 {
00150   int
00151     i;
00152     
00153   for(i = 0; attr[i].token; i++)
00154     if(attr[i].value == value)
00155       return attr[i].token;
00156       
00157   return "Unknown";
00158 }
00159 
00166 char*
00167 psXFontGetWeightName(int weight)
00168 {
00169   return LookupXFontAttrToken(weight, weights);
00170 }
00171 
00178 char*
00179 psXFontGetSlantName(int slant)
00180 {
00181   return LookupXFontAttrToken(slant, slants);
00182 }
00183 
00190 char*
00191 psXFontGetWidthName(int width)
00192 {
00193   return LookupXFontAttrToken(width, widths);
00194 }
00195 
00202 char*
00203 psXFontGetSpacingName(int spacing)
00204 {
00205   return LookupXFontAttrToken(spacing, spacings);
00206 }
00207 
00215 PSXFont* 
00216 psXFontAlloc(void)
00217 {
00218   return calloc(1, sizeof(PSXFont));
00219 }
00220 
00227 void 
00228 psXFontFree(PSXFont* xfont)
00229 {
00230   if(xfont)
00231   {
00232     if(xfont->foundry)
00233       free(xfont->foundry);
00234     if(xfont->family)
00235       free(xfont->family);
00236     if(xfont->encoding)
00237       free(xfont->encoding);
00238     free(xfont);
00239   }
00240 }
00241 
00251 void 
00252 psXFontSet(PSXFont *xfont, char *xlfd)
00253 {
00254   int
00255     field = 0;
00256     
00257   char
00258     *p,
00259     *q,
00260     *r;
00261     
00262   p = strdup(xlfd);
00263   if(p)
00264   {
00265     for(q = r = p; *r && *r != '-'; r++)
00266       ;
00267     if(*r)
00268     {
00269       *r = 0;
00270       r++;
00271     }
00272     while(q != r)
00273     {
00274       switch(field)
00275       {
00276         case 1 : /* foundry */
00277           if(xfont->foundry)
00278             free(xfont->foundry);
00279           xfont->foundry = strdup(q);
00280         break;
00281         
00282         case 2 : /* font family */
00283           if(xfont->family)
00284             free(xfont->family);
00285           xfont->family = strdup(q);
00286         break;
00287         
00288         case 3 : /* weight */
00289           xfont->weight = LookupXFontAttr(q, weights);
00290         break;
00291         
00292         case 4 : /* slant */
00293           xfont->slant = LookupXFontAttr(q, slants);
00294         break;
00295         
00296         case 5 : /* width */
00297           xfont->width = LookupXFontAttr(q, widths);
00298         break;
00299         
00300         case 7 : /* pixels */
00301           xfont->pixels = atoi(q);
00302         break;
00303         
00304         case 8 : /* points */
00305           xfont->points = atoi(q);
00306         break;
00307         
00308         case 9 : /* hdpi */
00309           xfont->hdpi = atoi(q);
00310         break;
00311         
00312         case 10 : /* vdpi */
00313           xfont->vdpi = atoi(q);
00314         break;
00315         
00316         case 11 : /* spacing */
00317           xfont->spacing = LookupXFontAttr(q, spacings);
00318         break;
00319         
00320         case 12 : /* average width */
00321           xfont->averageWidth = atoi(q);
00322         break;
00323         
00324         case 13 : /* encoding */
00325           if(xfont->encoding)
00326             free(xfont->encoding);
00327           xfont->encoding = strdup(q);
00328         break;
00329         
00330         case 14 : /* more encoding */
00331           xfont->encoding = realloc(xfont->encoding, strlen(xfont->encoding) + strlen(q) + 2);
00332           strcat(xfont->encoding, "-");
00333           strcat(xfont->encoding, q);
00334         break;
00335         
00336         default :  
00337           /* do nothing */
00338         break;
00339       }
00340       field++;
00341       for(q = r; *r && *r != '-'; r++)
00342         ;
00343       if(*r)
00344       {
00345         *r = 0;
00346         r++;
00347       }
00348     }
00349     free(p);
00350   }
00351 }
00352 
00362 char* 
00363 psXFontGetXLFD(PSXFont* xfont)
00364 {
00365   char
00366     tStr[2048],
00367     pixels[10],
00368     points[10],
00369     hdpi[10],
00370     vdpi[10],
00371     averageWidth[10];
00372     
00373   if(xfont->pixels == PS_XFONT_WILDCARD)
00374     strcpy(pixels, "*");
00375   else
00376     sprintf(pixels, "%d", xfont->pixels);
00377   if(xfont->points == PS_XFONT_WILDCARD)
00378     strcpy(points, "*");
00379   else
00380     sprintf(points, "%d", xfont->points);
00381   if(xfont->hdpi == PS_XFONT_WILDCARD)
00382     strcpy(hdpi, "*");
00383   else
00384     sprintf(hdpi, "%d", xfont->hdpi);
00385   if(xfont->vdpi == PS_XFONT_WILDCARD)
00386     strcpy(vdpi, "*");
00387   else
00388     sprintf(vdpi, "%d", xfont->vdpi);
00389   if(xfont->averageWidth == PS_XFONT_WILDCARD)
00390     strcpy(averageWidth, "*");
00391   else
00392     sprintf(averageWidth, "%d", xfont->averageWidth);
00393     
00394   sprintf(tStr, "-%s-%s-%s-%s-%s--%s-%s-%s-%s-%s-%s-%s",
00395           xfont->foundry,
00396           xfont->family,
00397           LookupXFontAttrToken(xfont->weight, weights),
00398           LookupXFontAttrToken(xfont->slant, slants),
00399           LookupXFontAttrToken(xfont->width, widths),
00400           pixels,
00401           points,
00402           hdpi,
00403           vdpi,
00404           LookupXFontAttrToken(xfont->spacing, spacings),
00405           averageWidth,
00406           xfont->encoding);
00407           
00408   return strdup(tStr);
00409 }
00410 
00423 int
00424 psXFontMatch(PSXFont* f1, PSXFont* f2)
00425 {
00426   return(!strcasecmp(f1->foundry, f2->foundry) &&
00427          !strcasecmp(f1->family, f2->family) &&
00428          f1->weight == f2->weight &&
00429          f1->slant == f2->slant &&
00430          f1->width == f2->width &&
00431          f1->spacing == f2->spacing &&
00432          !strcasecmp(f1->encoding, f2->encoding));
00433 }
00434 

Generated at Mon Dec 11 22:46:28 2000 for Postscript Utility Library by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000