1
/* $Xorg: osPexFont.c,v 1.4 2001/02/09 02:04:19 xorgcvs Exp $ */
5
Copyright 1989, 1990, 1991, 1998 The Open Group
7
Permission to use, copy, modify, distribute, and sell this software and its
8
documentation for any purpose is hereby granted without fee, provided that
9
the above copyright notice appear in all copies and that both that
10
copyright notice and this permission notice appear in supporting
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
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
OPEN GROUP 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.
23
Except as contained in this notice, the name of The Open Group 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 Open Group.
28
Copyright 1989, 1990, 1991 by Sun Microsystems, Inc.
32
Permission to use, copy, modify, and distribute this software and its
33
documentation for any purpose and without fee is hereby granted,
34
provided that the above copyright notice appear in all copies and that
35
both that copyright notice and this permission notice appear in
36
supporting documentation, and that the name of Sun Microsystems,
37
not be used in advertising or publicity pertaining to distribution of
38
the software without specific, written prior permission.
40
SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
41
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
42
SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
43
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
44
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
45
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49
/* $XFree86: xc/programs/Xserver/PEX5/ospex/osPexFont.c,v 3.19 2002/05/31 18:45:53 dawes Exp $ */
59
#include "keysymdef.h"
61
#define NEED_OS_LIMITS
64
#ifndef PEX_DEFAULT_FONTPATH
65
#define PEX_DEFAULT_FONTPATH "/usr/lib/X11/fonts/PEX"
72
#if !defined(X_NOT_POSIX) || defined(SYSV) || defined(__CYGWIN__) || defined(USG)
80
typedef struct dirent ENTRY;
84
#define FILENAME_MAX MAXNAMLEN
86
#define FILENAME_MAX 255
90
#else /* XFree86LOADER */
92
/* XXX This should be taken care of elsewhere */
93
typedef struct _xf86dirent ENTRY;
95
#endif /* XFree86LOADER */
97
#define FileName(file) file->d_name
106
#define FileName(file) file.cFileName
110
extern void CopyISOLatin1Lowered();
111
extern int get_lowered_truncated_entry();
113
void ClosePEXFontFile();
114
void SetPEXFontFilePtr();
116
extern diFontHandle defaultPEXFont;
119
* Unless an environment variable named PEX_FONTPATH is set before the
120
* server is started up, PEX will look in the path defined in the
121
* PEX_DEFAULT_FONTPATH compiler constant defined in miFont.h for PEX fonts.
122
* If environment variable PEX_FONTPATH is defined, then this will
123
* be used as the path to the fonts .
127
pex_get_font_directory_path()
129
static int already_determined = 0;
130
static char *font_dir_path = NULL;
132
if (!already_determined) {
133
if (getenv("PEX_FONTPATH")) {
135
(char *)xalloc((unsigned long)(1+strlen(getenv("PEX_FONTPATH"))));
136
strcpy(font_dir_path, getenv("PEX_FONTPATH"));
140
(char *)xalloc((unsigned long)(1+strlen(PEX_DEFAULT_FONTPATH)));
141
strcpy(font_dir_path, PEX_DEFAULT_FONTPATH);
143
char *p = (char*)__XOS2RedirRoot(PEX_DEFAULT_FONTPATH);
145
(char *)xalloc((unsigned long)(1+strlen(p)));
146
strcpy(font_dir_path, p);
149
already_determined = 1;
152
return (font_dir_path);
157
* The next two functions (pex_setup_wild_match() and pex_is_matching()) are
158
* stolen (and slightly modified) from MIT X11R4 fonts/mkfontdir/fontdir.c.
159
* pex_setup_wild_match() sets up some state about the pattern to match, which
160
* pex_is_matching() then uses.
164
/* results of this function are used by pex_is_matching() */
167
pex_setup_wild_match(pat, phead, ptail, plen)
169
int *phead, *ptail, *plen; /* out */
171
register int head, tail;
172
register char c, *firstWild;
174
*plen = tail = strlen(pat);
175
for ( firstWild = pat;
176
((c = *firstWild) && !((c == XK_asterisk) || (c == XK_question)));
180
head = firstWild - pat;
182
while ((c = pat[head]) && (c != XK_asterisk))
186
while (pat[tail-1] != XK_asterisk)
193
/* returns value greater than 0 if successful. head, tail, and plen
194
* come from a previous call to pex_setup_wild_match
197
pex_is_matching(string, pat, head, tail, plen)
198
register char *string; /* in */
199
register char *pat; /* in */
200
int head, tail, plen; /* in */
204
register char cp, cs;
207
for (i = 0; i < head; i++)
210
if (cp == XK_question)
216
else if (cp != string[i])
220
return (string[head] ? res : 1);
224
/* we just skipped an asterisk */
227
while ((cp = pat[i]) != XK_asterisk)
229
if (!(cs = string[l]))
231
if ((cp != cs) && (cp != XK_question))
235
if (cp == XK_asterisk)
242
while ((cs = string[m]) != cp)
256
m = strlen(&string[l]);
263
if ((cp != string[l]) && (cp != XK_question))
272
* Caller is responsible for freeing contents of buffer and buffer when
275
#define ABSOLUTE_MAX_NAMES 200
278
pex_get_matching_names(patLen, pPattern, maxNames, numNames, names)
279
ddUSHORT patLen; /* in */
280
ddUCHAR *pPattern; /* in */
281
ddUSHORT maxNames; /* in */
282
ddULONG *numNames; /* out - number of names found */
283
char ***names; /* out - pointer to list of strings */
287
WIN32_FIND_DATA dir_entry;
294
char entry[PATH_MAX+1];
295
int i, head, tail, len, total = 0;
297
if (!(pattern = (char *)xalloc((unsigned long)(1 + patLen))))
300
CopyISOLatin1Lowered((unsigned char*)pattern, pPattern, patLen);
302
if (!(*names = (char **)xalloc((unsigned long)(ABSOLUTE_MAX_NAMES * sizeof(char *)))))
306
sprintf(path, "%s/*.*", pex_get_font_directory_path());
307
if ((fontdirh = FindFirstFile(path, &dir_entry)) == INVALID_HANDLE_VALUE) {
313
if (!(fontdir = opendir(pex_get_font_directory_path()))) {
320
pex_setup_wild_match(pattern, &head, &tail, &len);
325
while (total < maxNames && (dir_entry = readdir(fontdir)))
329
if (!get_lowered_truncated_entry(FileName(dir_entry), entry))
332
if (pex_is_matching(entry, pattern, head, tail, len) > 0) {
334
if (!( (*names)[total] = (char *)xalloc((unsigned long)(1 + strlen(entry))))) {
335
for (i = 0; i < total; i++)
342
strcpy((*names)[total], entry);
347
while (total < maxNames && FindNextFile(fontdirh, &dir_entry));
362
* get_stroke(stroke, fp) extracts the definition of characters
363
* from the font file. It return -1 if anything goes wrong, 0 if
368
get_stroke(stroke, fp)
369
Ch_stroke_data *stroke;
372
listofddPoint *spath;
374
unsigned long closed; /* placeholder, really unused */
375
register ddULONG npath;
376
register miListHeader *hdr = &(stroke->strokes);
378
stroke->n_vertices = 0;
379
npath = hdr->maxLists = hdr->numLists;
380
hdr->type = DD_2D_POINT;
381
hdr->ddList = spath = (listofddPoint *)
382
xalloc((unsigned long)(sizeof(listofddPoint) * npath));
387
for (i = 0; i < npath; i++, spath++)
388
spath->pts.p2Dpt = NULL;
390
for (i = 0, spath = hdr->ddList; i < npath; i++, spath++) {
392
/* for each subpath of the character definition ... */
394
if (fread((char *) &spath->numPoints,
395
sizeof(spath->numPoints), 1, fp) != 1 ||
396
fread((char *) &closed, sizeof(closed), 1, fp) != 1)
399
if (spath->numPoints <= 0)
402
spath->maxData = sizeof(ddCoord2D) * spath->numPoints;
404
if (!(spath->pts.p2Dpt = (ddCoord2D *) xalloc((unsigned long)(spath->maxData))))
407
if (fread((char *)spath->pts.p2Dpt, sizeof(ddCoord2D),
408
spath->numPoints, fp) != spath->numPoints)
411
stroke->n_vertices += spath->numPoints;
422
LoadPEXFontFile(length, fontname, pFont)
427
char fname[FILENAME_MAX+1];
429
Font_file_header header;
430
Property *properties = 0;
432
miFontHeader *font = (miFontHeader *)(pFont->deviceData);
433
int found_first, found_it = 0, err = Success, numChars, np;
435
char lowered_entry[PATH_MAX+1];
438
WIN32_FIND_DATA dir_entry;
445
register Ch_stroke_data **ch_font, *ch_stroke = 0;
446
register Dispatch *tblptr = 0;
447
register Property *propptr = 0;
448
register pexFontProp *fpptr = 0;
450
if (!(name_to_match = (char *)xalloc((unsigned int)(1 + length))))
451
return (PEXERR(PEXFontError));
453
CopyISOLatin1Lowered((unsigned char *)name_to_match,
454
(unsigned char *)fontname, length);
456
/* open up the font directory and look for matching file names */
458
sprintf(path, "%s/*.*", pex_get_font_directory_path());
459
if ((fontdirh = FindFirstFile(path, &dir_entry)) == INVALID_HANDLE_VALUE)
460
return (PEXERR(PEXFontError));
462
if (!(fontdir = opendir(pex_get_font_directory_path())))
463
return (PEXERR(PEXFontError));
469
while(!found_it && (dir_entry = readdir(fontdir)))
472
/* strip off .phont and make all lower case */
473
if (!get_lowered_truncated_entry(FileName(dir_entry), lowered_entry))
476
/* does this match what got passed in? */
477
if (strcmp(lowered_entry, name_to_match) == 0)
480
xfree(name_to_match);
482
while (!found_it && FindNextFile(fontdirh, &dir_entry) && !found_it);
486
return (PEXERR(PEXFontError));
488
(void) strcpy(fname, pex_get_font_directory_path());
489
(void) strcat(fname, "/");
490
(void) strcat(fname, FileName(dir_entry));
498
if ((fp = fopen(fname, "r")) == NULL)
499
return (PEXERR(PEXFontError));
502
* read in the file header. The file header has fields containing the
503
* num of characters in the font, the extreme values, and number of font
504
* properties defined, if any.
508
if (fread((char *) &header, sizeof(header), 1, fp) != 1) {
509
(void) ClosePEXFontFile(fp);
510
return (PEXERR(PEXFontError)); }
512
/* Initialize font structure */
513
(void) strcpy(font->name, header.name);
514
font->font_type = FONT_POLYLINES;
515
font->top = header.top;
516
font->bottom = header.bottom;
517
font->num_ch = header.num_ch;
518
font->font_info.numProps = (CARD32)header.num_props;
519
font->max_width = header.max_width;
521
/* read in the font properties, if any, into font data area */
522
if (header.num_props > 0) {
524
(void) SetPEXFontFilePtr(fp, START_PROPS); /* Get to props position */
525
properties = (Property *) xalloc(header.num_props * sizeof(Property));
526
if (properties == NULL) {
527
(void) ClosePEXFontFile(fp);
530
if (fread((char *) properties, sizeof(Property),
531
header.num_props, fp) != header.num_props) {
532
xfree((char *) properties);
533
(void) ClosePEXFontFile(fp);
534
return (PEXERR(PEXFontError)); }
536
/* Create space for font properties in the font data area */
539
(pexFontProp *) xalloc( (unsigned long)(header.num_props
540
* sizeof(pexFontProp)));
541
if (font->properties == NULL) {
542
xfree((char *) properties);
543
(void) ClosePEXFontFile(fp);
546
np = header.num_props;
547
for ( i=0, propptr = properties, fpptr = font->properties;
549
i++, propptr++, fpptr++) {
551
if (propptr->propname == NULL) {
552
(header.num_props)--;
555
fpptr->name = MakeAtom( (char *)propptr->propname,
556
strlen(propptr->propname), 1);
558
if (propptr->propvalue != NULL)
559
fpptr->value = MakeAtom((char *)propptr->propvalue,
560
strlen(propptr->propvalue), 1);
561
else fpptr->value = 0;
564
/* free up local storage allocated for properties */
565
xfree((char *) properties);
568
/* position file pointer to dispatch data */
569
(void) SetPEXFontFilePtr(fp, (long) START_DISPATCH(header.num_props));
572
* read in the distable font, use the offset to see if the
573
* character is defined or not. The strokes are defined in Phigs style.
574
* The "center" of the character is not the physical center. It is the
575
* center defined by the font designer. The actual center is half the
579
table = (Dispatch *)xalloc((unsigned long)(sizeof(Dispatch) *font->num_ch));
582
(void) ClosePEXFontFile(fp);
585
if (fread((char *) table, sizeof(Dispatch), font->num_ch, fp)
587
xfree((char *) table);
588
(void) ClosePEXFontFile(fp);
589
return (PEXERR(PEXFontError)); }
592
(Ch_stroke_data **) xalloc((unsigned long)(sizeof(Ch_stroke_data *) *
594
if (font->ch_data == NULL) {
595
xfree((char *) table);
596
(void) ClosePEXFontFile(fp);
599
/* The next loop initializes all ch_data pointers to null; essential
600
for non-crashing during font clean-up in case of failed font file
601
read. Also count the number of non-blank chars.
603
for ( i = 0, ch_font = font->ch_data, tblptr = table, numChars = 0;
605
i++, ch_font++, tblptr++) {
607
if (tblptr->offset != 0) numChars++; }
609
ch_stroke = (Ch_stroke_data *)xalloc((unsigned long)(numChars *
610
sizeof(Ch_stroke_data)));
616
/* read in the char data (the font file format should be changed
617
so that the allocation can be done outside this loop--this
618
method is inefficient)
620
for ( i = 0, ch_font = font->ch_data, tblptr = table, found_first = 0;
622
i++, ch_font++, tblptr++) {
623
if (tblptr->offset != 0) {
624
(*ch_font) = ch_stroke++;
625
(*ch_font)->strokes.ddList = NULL;
626
(*ch_font)->center = table[i].center;
627
(*ch_font)->right = table[i].right;
629
(void) SetPEXFontFilePtr(fp, tblptr->offset);
631
/* read in the type, number of subpaths, and n_vertices fields */
632
if ( (fread(&((*ch_font)->type),
633
sizeof(Font_path_type), 1, fp) != 1)
634
|| (fread(&((*ch_font)->strokes.numLists),
635
sizeof(ddULONG),1,fp) != 1)
636
|| (fread(&((*ch_font)->n_vertices),
637
sizeof(ddULONG), 1, fp) != 1) )
639
err = PEXERR(PEXFontError);
643
(*ch_font)->strokes.maxLists = (*ch_font)->strokes.numLists;
644
if ((*ch_font)->strokes.numLists > 0) {
646
if (get_stroke(*ch_font, fp)) {
651
font->font_info.firstGlyph = i;
654
font->font_info.lastGlyph = i; }
658
xfree((char *)table);
660
(void) ClosePEXFontFile(fp);
665
(void) ClosePEXFontFile(fp);
666
if (table) xfree(table);
668
if (pFont == defaultPEXFont) defaultPEXFont = 0; /* force free */
670
FreePEXFont((diFontHandle) pFont, pFont->id);
682
SetPEXFontFilePtr(fp,where)
686
(void) fseek(fp, where, SEEK_SET); /* set pointer at "where" bytes
687
from the beginning of the file */