1 /**
2  ** loadfont.c ---- load a font from a disk file
3  **
4  ** Copyright (c) 1995 Csaba Biegl, 820 Stirrup Dr, Nashville, TN 37221
5  ** [e-mail: csaba@vuse.vanderbilt.edu]
6  **
7  ** This file is part of the GRX graphics library.
8  **
9  ** The GRX graphics library is free software; you can redistribute it
10  ** and/or modify it under some conditions; see the "copying.grx" file
11  ** for details.
12  **
13  ** This library is distributed in the hope that it will be useful,
14  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  **
17  **/
18 
19 #include <string.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 
23 #include "libgrx.h"
24 #include "grfontdv.h"
25 
doit(char * fname,char * path,int cvt,int w,int h,int lo,int hi)26 static GrFont *doit(char *fname,char *path,int cvt,int w,int h,int lo,int hi)
27 {
28     GrFontDriver **fd;
29     GrFontHeader hdr;
30     GrFont *f, *res;
31     char pathname[200];
32     char tempstring[200];
33     int  plen;
34     GRX_ENTER();
35     res = NULL;
36     strcpy(pathname,path);
37     strcat(pathname,fname);
38     DBGPRINTF(DBG_FONT,("searching font %s\n", pathname));
39     plen = strlen(pathname);
40     hdr.name   = &tempstring[0];
41     hdr.family = &tempstring[100];
42     for(fd = _GrFontDriverTable; (*fd) != NULL; fd++) {
43 	DBGPRINTF(DBG_FONT,("Driver \"%s\", extension \"%s\"\n", (*fd)->name, (*fd)->ext));
44 	pathname[plen] = '\0';
45 	if(!((*fd)->openfile)(pathname)) {
46 	    strcpy(&pathname[plen],(*fd)->ext);
47 	    if(!((*fd)->openfile)(pathname)) continue;
48 	}
49 	if(!((*fd)->header)(&hdr)) {
50 	    DBGPRINTF(DBG_FONT,("fd->header failed for %s\n", pathname));
51 	    (*fd)->cleanup();
52 	    continue;
53 	}
54 	f = _GrBuildFont(
55 	    &hdr,
56 	    cvt,
57 	    w,h,
58 	    lo,hi,
59 	    (*fd)->charwdt,
60 	    (*fd)->bitmap,
61 	    (*fd)->scalable
62 	);
63 	if(!f) {
64 	    DBGPRINTF(DBG_FONT,("_GrBuildFont failed for %s\n", pathname));
65 	    (*fd)->cleanup();
66 	    continue;
67 	}
68         (*fd)->cleanup();
69 	res = f;
70 	break;
71     }
72     GRX_RETURN(res);
73 }
74 
GrLoadConvertedFont(char * name,int cvt,int w,int h,int minc,int maxc)75 GrFont *GrLoadConvertedFont(char *name,int cvt,int w,int h,int minc,int maxc)
76 {
77     GrFont *f;
78     int  chr,len,abspath,dc;
79     char fname[200];
80     GRX_ENTER();
81     len = 0;
82     abspath = FALSE;
83     dc = TRUE;
84     while((chr = *name++) != '\0') {
85 	switch(chr) {
86 #if defined(__MSDOS__) || defined(__WIN32__)
87 	  case ':':
88 	    abspath = TRUE;
89 	    break;
90 	  case '\\':
91 	    chr = '/';
92 #endif
93 	  case '/':
94 	    dc = TRUE;
95 	    if(len == 0) abspath = TRUE;
96 	    break;
97 	  default:
98 	    if(isspace(chr)) {
99 		if(len == 0) continue;
100 		name = "";
101 		chr  = '\0';
102 	    }
103 #ifdef __DJGPP__
104             /* allow syntax /dev/env/DJDIR */
105 	    if ((len == 9) && (strncmp(fname,"/dev/env/",9)==0))
106 		dc = FALSE;
107 	    if (dc)
108 #endif
109 #if defined(__MSDOS__) || defined(__WIN32__)
110 		chr = tolower(chr);
111 #endif
112 	    break;
113 	}
114 	fname[len++] = chr;
115     }
116     fname[len] = '\0';
117     f = doit(fname,"",cvt,w,h,minc,maxc);
118     if((f == NULL) && !abspath) {
119 	if(_GrFontFileInfo.npath < 0) {
120             char *fPath = getenv("GRXFONT");
121 #ifdef GRX_DEFAULT_FONT_PATH
122             if (!fPath) fPath = GRX_DEFAULT_FONT_PATH;
123 #endif
124 	    GrSetFontPath(fPath);
125 	}
126 	for(len = 0; len < _GrFontFileInfo.npath; len++) {
127 	    f = doit(fname,_GrFontFileInfo.path[len],cvt,w,h,minc,maxc);
128 	    if(f != NULL) break;
129 	}
130     }
131     GRX_RETURN(f);
132 }
133 
GrLoadFont(char * name)134 GrFont *GrLoadFont(char *name)
135 {
136     return(GrLoadConvertedFont(name,GR_FONTCVT_NONE,0,0,0,0));
137 }
138 
139