home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC Shareware 1999 March
/
PCShareware-3-99.iso
/
IMPLE
/
DJGPP.RAR
/
DJGPP2
/
XLIB-SR0.ZIP
/
SRC
/
XLIBEMU
/
FONT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-14
|
10KB
|
459 lines
/* $Id: font.c 1.2 1994/01/15 02:09:03 ulrich Exp $ */
/*
* font.c
*
* X library font handling functions.
*/
#include "Xlibemu.h"
#include <stdio.h>
#include <ctype.h>
extern GrTextOption _TextOpt;
#ifndef HAVE_STRICMP
extern int strcasecmp (const char *, const char *);
#define stricmp strcasecmp
#endif
#include <grxfile.h>
#include <grxfont.h>
typedef struct {
char file_name[20];
char *logical_name;
} _WFontName;
_WFontName *_FontList = NULL;
int _FontListSize = 0;
void
_WInitFontList ()
{
FILE *fp;
int n, list_size;
_WFontName *font_list;
char dir_name[120];
char buffer[200];
char *p;
if (_FontList) return;
/* scan xfonts.dir produced with \'grep "FONT.*$" *.fnt\' */
GrSetFontPath (getenv ("GRXFONT"));
sprintf (dir_name,"%s/xfonts.dir", _GrFontPath);
fp = fopen (dir_name, "rt");
if (fp == NULL)
return;
font_list = 0;
n = list_size = 0;
while (fgets (buffer, 200, fp)) {
if (n >= list_size) {
list_size += list_size/2 + 1;
font_list = (_WFontName *)
Xrealloc (font_list, list_size * sizeof(_WFontName));
}
p = strchr (buffer, '\n');
if (p) *p = '\0';
p = strchr (buffer, ':');
if (! p) continue;
*p = 0;
strcpy (font_list[n].file_name, buffer);
font_list[n].logical_name = strdup (strlwr (p + strlen (":FONT ")));
#if 0
fprintf (stderr, "%s: %s\n",
font_list[n].file_name,
font_list[n].logical_name);
#endif
n++;
}
fclose (fp);
_FontList = font_list;
_FontListSize = n;
}
static int match_font(char *pattern, char *string)
{
int nlit;
while (*pattern)
{
switch (*pattern)
{
case '*':
pattern++;
if (*pattern == 0)
return 1;
nlit=0;
while ((pattern[nlit] != 0)
&& (pattern[nlit] != '*')
&& (pattern[nlit] != '?') )
nlit++;
while (1)
{
if (strncmp(string, pattern, nlit) == 0)
break;
string++;
if (*string == 0)
return 0;
}
break;
case '?':
if (*string == 0)
return 0;
pattern++;
string++;
break;
default:
if (*pattern != *string)
return 0;
pattern++;
string++;
break;
}
}
if (*string)
return 0;
return 1;
}
char **
_WListFonts (const char *pattern, int max_count, int *count)
{
int i, match_size, match_count;
char **match_list;
char *match_pattern;
if (_FontListSize == 0)
_WInitFontList ();
match_size = 0;
match_count = 0;
match_list = 0;
#if 0
fprintf (stderr, "list fonts %s\n", pattern);
#endif
match_pattern = (char *) alloca (strlen (pattern) + 1);
strcpy (match_pattern, pattern);
strlwr (match_pattern);
for (i = 0; i < _FontListSize; i++) {
if (0 == match_font (match_pattern, _FontList[i].logical_name))
continue;
if (match_count >= max_count)
break;
if (match_count >= match_size) {
match_size += match_size/2 + 1;
match_list = (char **)
Xrealloc (match_list, (match_size+1) * sizeof(char *));
}
match_list[match_count] = strdup (_FontList[i].file_name);
#if 0
fprintf (stderr, "%d: %s\n", match_count, match_list[n]);
#endif
match_count++;
}
if (match_count > 0)
match_list[match_count] = NULL;
*count = match_count;
return match_list;
}
void
_WFreeFontNames (char **name_table)
{
char **p = name_table;
if (! p) return;
while (*p) { free (*p); p++; }
free (name_table);
}
Font
XLoadFont(
Display* display,
_Xconst char* name)
{
GrFont *font;
int width, height;
char pattern[200];
if (isdigit (name[0])
&& sscanf ((char *) name, "%dx%d", &width, &height) == 2) {
sprintf (pattern, "-*-clean-medium-r-normal--%d-*-*-*-c-%d-*-1", height, 10 * width);
name = pattern;
}
else if (! strcmp (name, "fixed")) {
width = 8;
height = 16;
sprintf (pattern, "-*-clean-medium-r-normal--%d-*-*-*-c-%d-*-1", height, 10 * width);
name = pattern;
}
if (strpbrk (name, "-*?")) {
int count;
char **list;
list = _WListFonts (name, 1, &count);
if (count > 0) {
font = GrLoadFont (list[0]);
_WFreeFontNames (list);
if (font)
return font;
}
}
font = GrLoadFont ((char *)name);
if (font == None) {
/* Load the default font */
font = GrLoadFont ("pc8x16");
}
if (font == None) {
_WError (display, None, BadName, 45 /* X_OpenFont */);
}
return font;
}
XFontStruct *
XLoadQueryFont(
Display* display,
_Xconst char* name)
{
Font font;
font = XLoadFont (display, name);
if (font == None)
return NULL;
return XQueryFont (display, (XID) font);
}
XFontStruct *
XQueryFont(
Display* display,
XID font_ID)
{
Font font = (Font) font_ID;
XFontStruct *fs;
int i, numchar, width, lbearing, rbearing, ascent, descent;
if (font == None)
return NULL;
fs = (XFontStruct *) Xmalloc (sizeof(XFontStruct));
if (fs == NULL)
return NULL;
fs->ext_data = NULL;
fs->fid = font;
fs->direction = 0;
fs->min_char_or_byte2 = font->fnt_minchar;
fs->max_char_or_byte2 = font->fnt_maxchar;
fs->min_byte1 = font->fnt_minchar;
fs->max_byte1 = font->fnt_maxchar;
fs->all_chars_exist = True;
fs->default_char = ' ';
fs->n_properties = 0;
fs->properties = NULL;
width = font->fnt_width;
ascent = font->fnt_baseline;
descent = font->fnt_height - ascent;
fs->ascent = ascent;
fs->descent = descent;
if (font->fnt_isfixed) {
fs->min_bounds.lbearing = 0;
fs->min_bounds.rbearing = width;
fs->min_bounds.width = width;
fs->min_bounds.ascent = ascent;
fs->min_bounds.descent = descent;
fs->max_bounds = fs->min_bounds;
fs->per_char = NULL;
}
else {
numchar = font->fnt_maxchar - font->fnt_minchar + 1;
fs->per_char = (XCharStruct *) Xmalloc (numchar * sizeof (XCharStruct));
fs->min_bounds.lbearing = 0;
fs->min_bounds.rbearing = width;
fs->min_bounds.width = width;
fs->min_bounds.ascent = ascent;
fs->min_bounds.descent = descent;
fs->max_bounds = fs->min_bounds;
for (i = 0; i < numchar; i++) {
width = PFP(font)->pf_width[i];
lbearing = 0;
rbearing = width;
fs->per_char[i].lbearing = lbearing;
fs->per_char[i].rbearing = rbearing;
fs->per_char[i].width = width;
fs->per_char[i].ascent = ascent;
fs->per_char[i].descent = descent;
if (lbearing < fs->min_bounds.lbearing)
fs->min_bounds.lbearing = lbearing;
else if (lbearing > fs->max_bounds.lbearing)
fs->max_bounds.lbearing = lbearing;
if (rbearing < fs->min_bounds.rbearing)
fs->min_bounds.rbearing = rbearing;
else if (rbearing > fs->max_bounds.rbearing)
fs->max_bounds.lbearing = rbearing;
if (width < fs->min_bounds.width)
fs->min_bounds.width = width;
else if (width > fs->max_bounds.width)
fs->max_bounds.width = width;
if (ascent < fs->min_bounds.ascent)
fs->min_bounds.ascent = ascent;
else if (ascent > fs->max_bounds.ascent)
fs->max_bounds.ascent = ascent;
if (descent < fs->min_bounds.descent)
fs->min_bounds.ascent = descent;
else if (descent > fs->max_bounds.descent)
fs->max_bounds.ascent = descent;
}
}
return fs;
}
int
XFreeFont(
Display* display,
XFontStruct* font_struct)
{
XFree (font_struct);
return 0;
}
int
XFreeFontInfo(
char** names,
XFontStruct* free_info,
int actual_count)
{
XFree (free_info);
return 0;
}
int
XTextExtents(
XFontStruct* font_struct,
_Xconst char* string,
int nchars,
int* direction_return,
int* font_ascent_return,
int* font_descent_return,
XCharStruct* overall_return)
{
int width, ascent, descent;
GrFont *font = font_struct->fid;
ascent = font->fnt_baseline;
descent = font->fnt_height - ascent;
*direction_return = font_struct->direction;
*font_ascent_return = ascent;
*font_descent_return = descent;
if (nchars > 0 && string != NULL) {
_TextOpt.txo_font = font;
width = GrStringWidth ((char *)string, nchars, &_TextOpt);
overall_return->lbearing = 0;
overall_return->rbearing = width;
overall_return->width = width;
overall_return->ascent = ascent;
overall_return->descent = descent;
}
return Success;
}
int
XTextExtents16(
XFontStruct* font_struct,
_Xconst XChar2b* string,
int nchars,
int* direction_return,
int* font_ascent_return,
int* font_descent_return,
XCharStruct* overall_return)
{
return 0;
}
int
XTextWidth(
XFontStruct* font_struct,
_Xconst char* string,
int count
)
{
_TextOpt.txo_font = font_struct->fid;
return GrStringWidth ((char *)string, count, &_TextOpt);
}
int
XTextWidth16(
XFontStruct* font_struct,
_Xconst XChar2b* string,
int count)
{
return 0;
}
char **XListFonts(
Display* display,
_Xconst char* pattern,
int maxnames,
int* actual_count_return)
{
char **list;
int count;
list = _WListFonts (pattern, maxnames, &count);
*actual_count_return = count;
return list;
}
char **XListFontsWithInfo(
Display* display,
_Xconst char* pattern,
int maxnames,
int* count_return,
XFontStruct** info_return)
{
int i, count;
XFontStruct *font_array, *fs;
char **name_array;
name_array = XListFonts (display, pattern, maxnames, count_return);
count = *count_return;
if (count > 0) {
font_array = (XFontStruct *) Xmalloc (count * sizeof(XFontStruct));
for (i = 0; i < count; i++) {
fs = XLoadQueryFont (display, name_array[i]);
font_array[i] = *fs;
XFreeFont (display, fs);
}
*info_return = font_array;
}
return name_array;
}
int XFreeFontNames(
char** list)
{
_WFreeFontNames (list);
return 0;
}