📄 selfont.c
字号:
for ( t=0; t < 20; t++ ) {
/* Only follow 20 aliases deep, assume failure if more ... */
/* Find foundry for the current font */
foundry = NULL;
if ( (tmp = index(fontname,',')) != NULL ) {
/*
* We have a font name like T1,c0934345 or
* Adobe,Times (e.g. it includes foundry or
* rendering method). Separate them here.
*/
comma = tmp - fontname;
tmp++;
strncpy(fndry,fontname,comma);
fndry[comma] = '\0';
foundry = fndry;
fontname = tmp;
}
fontclass = decode_font_class(foundry);
if ( plogfont == NULL && fontclass == 0 )
fontclass = MWLF_CLASS_BUILTIN;
if ( fontclass ) {
/* The font is a "physical" font, use it directly */
strcpy(physname,fontname);
return fontclass;
}
if ( found_font ) {
/* Oops, should not get here, unless a font definition
* resulted in a non-existent font, e.g. the fontclass
* is unknown.
*/
goto default_font;
}
font = find_suitable_font(foundry,fontname,plogfont);
if ( font == NULL ) {
goto default_font;
}
if ( !font->alias )
found_font = 1;
fontname = font->lf.lfFaceName;
}
default_font:
strcpy(physname, MWFONT_SYSTEM_VAR);
return MWLF_CLASS_BUILTIN;
}
/* This function can be used to clear the existing, possibly default list
* of fonts on the system. This is typically done before reading a
* configuration file that defines the available fonts.
*/
void
GdClearFontList(void)
{
struct available_font *font, *next_font;
font = all_fonts;
while ( font != 0 ) {
next_font = font->next;
if ( font->foundry != 0 )
free(font->foundry);
if ( font->family != 0 )
free(font->family);
free(font->fontname);
free(font);
font = next_font;
}
all_fonts = 0;
}
/* This function will add a font to the list of available fonts.
* The physical name is the name as used by the underlying font
* rendering engine.
*/
int
GdAddFont(char *fndry, char *fmly, char *fontname, PMWLOGFONT lf,
unsigned int flags)
{
struct available_font *font, *walk;
int fontclass = 0;
char *physname = lf->lfFaceName;
if ( !strncmp(physname,"T1,",3) ) {
#ifdef HAVE_T1LIB_SUPPORT
/* Can handle Type 1 fonts */
physname += 3;
fontclass = MWLF_CLASS_T1LIB;
goto do_font;
#else
/* Can't handle Type 1 fonts */
return -1;
#endif
}
if ( !strncmp(physname,"FT,",3) ) {
#if HAVE_FREETYPE_SUPPORT
/* Can handle FreeType fonts */
physname += 3;
fontclass = MWLF_CLASS_FREETYPE;
goto do_font;
#else
/* Can't handle Type 1 fonts */
return -1;
#endif
}
if ( !strncmp(physname,"MWF,",4) ) {
/* This is a Microwindows built in font */
physname += 4;
fontclass = MWLF_CLASS_BUILTIN;
goto do_font;
}
/* Only aliases does not need to use T1, FT or MWF description */
if ( !(flags & MWLF_FLAGS_ALIAS) )
return -1;
do_font:
font = malloc(sizeof(*font));
if ( font == 0 )
return -1;
font->foundry = 0;
if ( strcmp("-",fndry) ) {
font->foundry = strdup(fndry);
if ( font->foundry == 0 ) {
free(font);
return -1;
}
}
font->family = 0;
if ( strcmp("-",fmly) ) {
font->family = strdup(fmly);
if ( font->family == 0 ) {
free(font->foundry);
free(font);
return -1;
}
}
font->fontname = strdup(fontname);
if ( font->fontname == 0 ) {
free(font->foundry);
free(font->family);
free(font);
return -1;
}
memcpy(&font->lf,lf,sizeof(*lf));
printf("Adding font: '%s' '%s' '%s' '%s'\n",font->foundry,
font->family,font->fontname,font->lf.lfFaceName);
font->next = 0;
font->alias = (flags & MWLF_FLAGS_ALIAS) ? 1 : 0;
font->fontclass = fontclass;
/* Stupid append at end of list code */
if ( all_fonts == 0 ) {
all_fonts = font;
} else {
for ( walk = all_fonts; walk->next != 0; walk = walk->next )
;
walk->next = font;
}
return 0;
}
/*
* These functions are used to set attributes in a logical font
* structure, called through a table of function pointers.
*/
static void font_set_light(PMWLOGFONT lf)
{ lf->lfWeight = MWLF_WEIGHT_LIGHT; }
static void font_set_regular(PMWLOGFONT lf)
{ lf->lfWeight = MWLF_WEIGHT_REGULAR; }
static void font_set_medium(PMWLOGFONT lf)
{ lf->lfWeight = MWLF_WEIGHT_MEDIUM; }
static void font_set_demibold(PMWLOGFONT lf)
{ lf->lfWeight = MWLF_WEIGHT_DEMIBOLD; }
static void font_set_bold(PMWLOGFONT lf)
{ lf->lfWeight = MWLF_WEIGHT_BOLD; }
static void font_set_black(PMWLOGFONT lf)
{ lf->lfWeight = MWLF_WEIGHT_BLACK; }
static void font_set_italic(PMWLOGFONT lf) { lf->lfItalic = 1; }
static void font_set_roman(PMWLOGFONT lf) { lf->lfRoman = 1; }
static void font_set_oblique(PMWLOGFONT lf) { lf->lfOblique = 1; }
static void font_set_normal(PMWLOGFONT lf)
{ lf->lfPitch = MWLF_PITCH_NORMAL; }
static void font_set_semicondensed(PMWLOGFONT lf)
{ lf->lfPitch = MWLF_PITCH_SEMICONDENSED; }
static void font_set_condensed(PMWLOGFONT lf)
{ lf->lfPitch = MWLF_PITCH_CONDENSED; }
static void font_set_serif(PMWLOGFONT lf) { lf->lfSerif = 1; }
static void font_set_sansserif(PMWLOGFONT lf) { lf->lfSansSerif = 1; }
static void font_set_monospace(PMWLOGFONT lf) { lf->lfMonospace = 1; }
static void font_set_proportional(PMWLOGFONT lf) { lf->lfProportional = 1; }
int config_font(char *file, int line, int argc, char *argv[])
{
unsigned int flags = 0;
MWLOGFONT lf;
char tmp[512];
char *p, *q, *fndry, *family, *fontname;
int size, t;
static struct {
char *name;
void (*function)(PMWLOGFONT lf);
} attrs[] = {
/* Weight */
{ "Light", font_set_light },
{ "Regular", font_set_regular },
{ "Medium", font_set_medium },
{ "DemiBold", font_set_demibold },
{ "Demibold", font_set_demibold },
{ "Bold", font_set_bold },
{ "Black", font_set_black },
/* Slant */
{ "Italic", font_set_italic },
{ "Italics", font_set_italic },
{ "Roman", font_set_roman },
{ "Oblique", font_set_oblique },
/* Width */
{ "Normal", font_set_normal },
{ "Semicondensed", font_set_semicondensed },
{ "Condensed", font_set_condensed },
/* Class */
{ "Serif", font_set_serif },
{ "Sans-serif", font_set_sansserif },
{ "Monospace", font_set_monospace },
{ "Proportional", font_set_proportional },
{ 0, 0 }
};
MWLF_Clear(&lf);
if ( argc != 6 ) {
fprintf(stderr,"Bad font description %s:%d\n",file,line);
return 1;
}
if ( !strcmp("alias",argv[1]) ) {
flags |= MWLF_FLAGS_ALIAS;
fndry = "-";
} else {
fndry = argv[1];
}
family = argv[2];
fontname = argv[3];
strcpy(lf.lfFaceName,argv[5]);
p = argv[4];
while ( *p != '\0' ) {
/* Parse attributes */
q = strchr(p,',');
if ( q != 0 ) {
size = q - p;
strncpy(tmp,p,size);
tmp[size] = '\0';
p = q + 1;
} else {
strcpy(tmp,p);
p += strlen(tmp);
}
for ( t = 0; attrs[t].name != 0; t++ ) {
if ( !strcmp(attrs[t].name,tmp) ) {
attrs[t].function(&lf);
goto next;
}
}
fprintf(stderr,"No such font attribute '%s' in %s:%d\n",
tmp,file,line);
return 1;
next: ;
}
GdAddFont(fndry,family,fontname,&lf,flags);
return 0;
}
/*
* Handle a single configuration line entery. Arguments as for
* function 'main(int argc, char **argv)' -- argv[0] is name of
* original configuration file. Return negative value for error,
* zero for OK.
*/
int config_line(char *file, int line, int argc, char *argv[])
{
if ( !argc )
return 0; /* Empty line */
if ( argv[0][0] == '#' )
return 0; /* Comment line */
if ( !strcmp("font", argv[0]) )
return config_font(file,line,argc,argv);
if ( !strcmp("clear-fonts", argv[0]) ) {
GdClearFontList();
return 0;
}
return -1;
}
/*
* Read (one of) the configuration files.
*/
#define MAXCONFIGLINESIZE 1024
#define MAXCONFIGELEMENTS 64
int read_configfile(char *file)
{
FILE *cf;
char buffer[MAXCONFIGLINESIZE+1];
char *args[MAXCONFIGELEMENTS+1];
unsigned char *p;
int argc, s, rc, t, line;
if ( (cf = fopen(file,"r")) == 0 ) {
fprintf(stderr,"Unable to read config file '%s'\n",file);
return -1;
}
line = 0;
while ( !feof(cf) ) {
if ( fgets(buffer,1000,cf) == 0 )
break;
line++;
s = strlen(buffer) - 1;
while ( s >= 0 && buffer[s] == '\n' )
buffer[s--] = '\0';
p = (unsigned char *)buffer;
argc = 0;
for ( t=0; t < MAXCONFIGELEMENTS; t++ ) {
while ( *p != '\0' && isspace(*p) )
p++;
if ( *p == '\"' ) {
/* Quoted string */
p++;
args[t] = p;
argc++;
while ( *p != '\0' && *p != '\"' )
p++;
if ( *p == '\0' ) {
fprintf(stderr,"Unbalanced quotes in %s:%d\n",
file,line);
break;
}
*p++ = '\0';
} else {
if ( *p == '\0' )
break;
args[t] = p;
argc++;
while ( *p != '\0' && !isspace(*p) )
p++;
*p++ = '\0';
}
}
#if 0
{
int t;
for ( t=0; t < argc; t++ )
printf("#%d: '%s'\n",t,args[t]);
}
#endif
rc = config_line(file, line, argc, args);
if ( rc < 0 )
return rc;
}
fclose(cf);
return 0;
}
#endif /* FONTMAPPER*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -