1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
|
/* $Xorg: osPexFont.c,v 1.4 2001/02/09 02:04:19 xorgcvs Exp $ */
/*
Copyright 1989, 1990, 1991, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
Copyright 1989, 1990, 1991 by Sun Microsystems, Inc.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Sun Microsystems,
not be used in advertising or publicity pertaining to distribution of
the software without specific, written prior permission.
SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
*/
/* $XFree86: xc/programs/Xserver/PEX5/ospex/osPexFont.c,v 3.19 2002/05/31 18:45:53 dawes Exp $ */
#ifdef WIN32
#define _WILLWINSOCK_
#endif
#include "mipex.h"
#include "miFont.h"
#include "PEXErr.h"
#define XK_LATIN1
#include "keysymdef.h"
#define NEED_GETENV
#define NEED_OS_LIMITS
#include "pexos.h"
#ifndef PEX_DEFAULT_FONTPATH
#define PEX_DEFAULT_FONTPATH "/usr/lib/X11/fonts/PEX"
#endif
#ifndef WIN32
#ifndef XFree86LOADER
#if !defined(X_NOT_POSIX) || defined(SYSV) || defined(__CYGWIN__) || defined(USG)
#include <dirent.h>
#else
#include <sys/dir.h>
#ifndef dirent
#define dirent direct
#endif
#endif
typedef struct dirent ENTRY;
#ifndef FILENAME_MAX
#ifdef MAXNAMLEN
#define FILENAME_MAX MAXNAMLEN
#else
#define FILENAME_MAX 255
#endif
#endif
#else /* XFree86LOADER */
/* XXX This should be taken care of elsewhere */
typedef struct _xf86dirent ENTRY;
#endif /* XFree86LOADER */
#define FileName(file) file->d_name
#else /* WIN32 */
#define BOOL wBOOL
#define ATOM wATOM
#include <windows.h>
#undef BOOL
#undef ATOM
#define FileName(file) file.cFileName
#endif /* WIN32 */
extern void CopyISOLatin1Lowered();
extern int get_lowered_truncated_entry();
void ClosePEXFontFile();
void SetPEXFontFilePtr();
extern diFontHandle defaultPEXFont;
/*
* Unless an environment variable named PEX_FONTPATH is set before the
* server is started up, PEX will look in the path defined in the
* PEX_DEFAULT_FONTPATH compiler constant defined in miFont.h for PEX fonts.
* If environment variable PEX_FONTPATH is defined, then this will
* be used as the path to the fonts .
*/
static char *
pex_get_font_directory_path()
{
static int already_determined = 0;
static char *font_dir_path = NULL;
if (!already_determined) {
if (getenv("PEX_FONTPATH")) {
font_dir_path =
(char *)xalloc((unsigned long)(1+strlen(getenv("PEX_FONTPATH"))));
strcpy(font_dir_path, getenv("PEX_FONTPATH"));
} else {
#ifndef __UNIXOS2__
font_dir_path =
(char *)xalloc((unsigned long)(1+strlen(PEX_DEFAULT_FONTPATH)));
strcpy(font_dir_path, PEX_DEFAULT_FONTPATH);
#else
char *p = (char*)__XOS2RedirRoot(PEX_DEFAULT_FONTPATH);
font_dir_path =
(char *)xalloc((unsigned long)(1+strlen(p)));
strcpy(font_dir_path, p);
#endif
}
already_determined = 1;
}
return (font_dir_path);
}
/*
* The next two functions (pex_setup_wild_match() and pex_is_matching()) are
* stolen (and slightly modified) from MIT X11R4 fonts/mkfontdir/fontdir.c.
* pex_setup_wild_match() sets up some state about the pattern to match, which
* pex_is_matching() then uses.
*/
/* results of this function are used by pex_is_matching() */
static void
pex_setup_wild_match(pat, phead, ptail, plen)
char *pat; /* in */
int *phead, *ptail, *plen; /* out */
{
register int head, tail;
register char c, *firstWild;
*plen = tail = strlen(pat);
for ( firstWild = pat;
((c = *firstWild) && !((c == XK_asterisk) || (c == XK_question)));
firstWild++)
;
head = firstWild - pat;
while ((c = pat[head]) && (c != XK_asterisk))
head++;
if (head < tail)
{
while (pat[tail-1] != XK_asterisk)
tail--;
}
*phead = head;
*ptail = tail;
}
/* returns value greater than 0 if successful. head, tail, and plen
* come from a previous call to pex_setup_wild_match
*/
static int
pex_is_matching(string, pat, head, tail, plen)
register char *string; /* in */
register char *pat; /* in */
int head, tail, plen; /* in */
{
register int i, l;
int j, m, res;
register char cp, cs;
res = -1;
for (i = 0; i < head; i++)
{
cp = pat[i];
if (cp == XK_question)
{
if (!string[i])
return res;
res = 0;
}
else if (cp != string[i])
return res;
}
if (head == plen)
return (string[head] ? res : 1);
l = head;
while (++i < tail)
{
/* we just skipped an asterisk */
j = i;
m = l;
while ((cp = pat[i]) != XK_asterisk)
{
if (!(cs = string[l]))
return 0;
if ((cp != cs) && (cp != XK_question))
{
m++;
cp = pat[j];
if (cp == XK_asterisk)
{
if (!string[m])
return 0;
}
else
{
while ((cs = string[m]) != cp)
{
if (!cs)
return 0;
m++;
}
}
l = m;
i = j;
}
l++;
i++;
}
}
m = strlen(&string[l]);
j = plen - tail;
if (m < j)
return 0;
l = (l + m) - j;
while (cp = pat[i])
{
if ((cp != string[l]) && (cp != XK_question))
return 0;
l++;
i++;
}
return 1;
}
/*
* Caller is responsible for freeing contents of buffer and buffer when
* done with it.
*/
#define ABSOLUTE_MAX_NAMES 200
int
pex_get_matching_names(patLen, pPattern, maxNames, numNames, names)
ddUSHORT patLen; /* in */
ddUCHAR *pPattern; /* in */
ddUSHORT maxNames; /* in */
ddULONG *numNames; /* out - number of names found */
char ***names; /* out - pointer to list of strings */
{
#ifdef WIN32
HANDLE fontdirh;
WIN32_FIND_DATA dir_entry;
char path[MAX_PATH];
#else
DIR *fontdir;
ENTRY *dir_entry;
#endif
char *pattern;
char entry[PATH_MAX+1];
int i, head, tail, len, total = 0;
if (!(pattern = (char *)xalloc((unsigned long)(1 + patLen))))
return 0;
CopyISOLatin1Lowered((unsigned char*)pattern, pPattern, patLen);
if (!(*names = (char **)xalloc((unsigned long)(ABSOLUTE_MAX_NAMES * sizeof(char *)))))
return 0;
#ifdef WIN32
sprintf(path, "%s/*.*", pex_get_font_directory_path());
if ((fontdirh = FindFirstFile(path, &dir_entry)) == INVALID_HANDLE_VALUE) {
xfree(*names);
xfree(pattern);
return 0;
}
#else
if (!(fontdir = opendir(pex_get_font_directory_path()))) {
xfree(*names);
xfree(pattern);
return 0;
}
#endif
pex_setup_wild_match(pattern, &head, &tail, &len);
#ifdef WIN32
do
#else
while (total < maxNames && (dir_entry = readdir(fontdir)))
#endif
{
if (!get_lowered_truncated_entry(FileName(dir_entry), entry))
continue;
if (pex_is_matching(entry, pattern, head, tail, len) > 0) {
if (!( (*names)[total] = (char *)xalloc((unsigned long)(1 + strlen(entry))))) {
for (i = 0; i < total; i++)
xfree((*names)[i]);
xfree(*names);
xfree(pattern);
return 0;
}
strcpy((*names)[total], entry);
total++;
}
}
#ifdef WIN32
while (total < maxNames && FindNextFile(fontdirh, &dir_entry));
#endif
#ifdef WIN32
FindClose(fontdirh);
#else
closedir(fontdir);
#endif
*numNames = total;
return 1;
}
/*
* get_stroke(stroke, fp) extracts the definition of characters
* from the font file. It return -1 if anything goes wrong, 0 if
* everything is OK.
*/
static int
get_stroke(stroke, fp)
Ch_stroke_data *stroke;
FILE *fp;
{
listofddPoint *spath;
register int i;
unsigned long closed; /* placeholder, really unused */
register ddULONG npath;
register miListHeader *hdr = &(stroke->strokes);
stroke->n_vertices = 0;
npath = hdr->maxLists = hdr->numLists;
hdr->type = DD_2D_POINT;
hdr->ddList = spath = (listofddPoint *)
xalloc((unsigned long)(sizeof(listofddPoint) * npath));
if (spath == NULL)
return -1;
for (i = 0; i < npath; i++, spath++)
spath->pts.p2Dpt = NULL;
for (i = 0, spath = hdr->ddList; i < npath; i++, spath++) {
/* for each subpath of the character definition ... */
if (fread((char *) &spath->numPoints,
sizeof(spath->numPoints), 1, fp) != 1 ||
fread((char *) &closed, sizeof(closed), 1, fp) != 1)
return -1;
if (spath->numPoints <= 0)
continue;
spath->maxData = sizeof(ddCoord2D) * spath->numPoints;
if (!(spath->pts.p2Dpt = (ddCoord2D *) xalloc((unsigned long)(spath->maxData))))
return -1;
if (fread((char *)spath->pts.p2Dpt, sizeof(ddCoord2D),
spath->numPoints, fp) != spath->numPoints)
return -1;
stroke->n_vertices += spath->numPoints;
}
return 0;
}
/*
read in the pex font
*/
ErrorCode
LoadPEXFontFile(length, fontname, pFont)
unsigned length;
char * fontname;
diFontHandle pFont;
{
char fname[FILENAME_MAX+1];
FILE *fp;
Font_file_header header;
Property *properties = 0;
Dispatch *table = 0;
miFontHeader *font = (miFontHeader *)(pFont->deviceData);
int found_first, found_it = 0, err = Success, numChars, np;
char *name_to_match;
char lowered_entry[PATH_MAX+1];
#ifdef WIN32
HANDLE fontdirh;
WIN32_FIND_DATA dir_entry;
char path[MAX_PATH];
#else
DIR *fontdir;
ENTRY *dir_entry;
#endif
register int i;
register Ch_stroke_data **ch_font, *ch_stroke = 0;
register Dispatch *tblptr = 0;
register Property *propptr = 0;
register pexFontProp *fpptr = 0;
if (!(name_to_match = (char *)xalloc((unsigned int)(1 + length))))
return (PEXERR(PEXFontError));
CopyISOLatin1Lowered((unsigned char *)name_to_match,
(unsigned char *)fontname, length);
/* open up the font directory and look for matching file names */
#ifdef WIN32
sprintf(path, "%s/*.*", pex_get_font_directory_path());
if ((fontdirh = FindFirstFile(path, &dir_entry)) == INVALID_HANDLE_VALUE)
return (PEXERR(PEXFontError));
#else
if (!(fontdir = opendir(pex_get_font_directory_path())))
return (PEXERR(PEXFontError));
#endif
#ifdef WIN32
do
#else
while(!found_it && (dir_entry = readdir(fontdir)))
#endif
{
/* strip off .phont and make all lower case */
if (!get_lowered_truncated_entry(FileName(dir_entry), lowered_entry))
continue;
/* does this match what got passed in? */
if (strcmp(lowered_entry, name_to_match) == 0)
found_it = 1;
}
xfree(name_to_match);
#ifdef WIN32
while (!found_it && FindNextFile(fontdirh, &dir_entry) && !found_it);
#endif
if (!found_it)
return (PEXERR(PEXFontError));
(void) strcpy(fname, pex_get_font_directory_path());
(void) strcat(fname, "/");
(void) strcat(fname, FileName(dir_entry));
#ifdef WIN32
FindClose(fontdirh);
#else
closedir(fontdir);
#endif
if ((fp = fopen(fname, "r")) == NULL)
return (PEXERR(PEXFontError));
/*
* read in the file header. The file header has fields containing the
* num of characters in the font, the extreme values, and number of font
* properties defined, if any.
*/
tblptr = 0;
if (fread((char *) &header, sizeof(header), 1, fp) != 1) {
(void) ClosePEXFontFile(fp);
return (PEXERR(PEXFontError)); }
/* Initialize font structure */
(void) strcpy(font->name, header.name);
font->font_type = FONT_POLYLINES;
font->top = header.top;
font->bottom = header.bottom;
font->num_ch = header.num_ch;
font->font_info.numProps = (CARD32)header.num_props;
font->max_width = header.max_width;
/* read in the font properties, if any, into font data area */
if (header.num_props > 0) {
(void) SetPEXFontFilePtr(fp, START_PROPS); /* Get to props position */
properties = (Property *) xalloc(header.num_props * sizeof(Property));
if (properties == NULL) {
(void) ClosePEXFontFile(fp);
return (BadAlloc); }
if (fread((char *) properties, sizeof(Property),
header.num_props, fp) != header.num_props) {
xfree((char *) properties);
(void) ClosePEXFontFile(fp);
return (PEXERR(PEXFontError)); }
/* Create space for font properties in the font data area */
font->properties =
(pexFontProp *) xalloc( (unsigned long)(header.num_props
* sizeof(pexFontProp)));
if (font->properties == NULL) {
xfree((char *) properties);
(void) ClosePEXFontFile(fp);
return (BadAlloc); }
np = header.num_props;
for ( i=0, propptr = properties, fpptr = font->properties;
i < np;
i++, propptr++, fpptr++) {
if (propptr->propname == NULL) {
(header.num_props)--;
continue; }
fpptr->name = MakeAtom( (char *)propptr->propname,
strlen(propptr->propname), 1);
if (propptr->propvalue != NULL)
fpptr->value = MakeAtom((char *)propptr->propvalue,
strlen(propptr->propvalue), 1);
else fpptr->value = 0;
}
/* free up local storage allocated for properties */
xfree((char *) properties);
}
/* position file pointer to dispatch data */
(void) SetPEXFontFilePtr(fp, (long) START_DISPATCH(header.num_props));
/*
* read in the distable font, use the offset to see if the
* character is defined or not. The strokes are defined in Phigs style.
* The "center" of the character is not the physical center. It is the
* center defined by the font designer. The actual center is half the
* "right" value.
*/
table = (Dispatch *)xalloc((unsigned long)(sizeof(Dispatch) *font->num_ch));
if (table == NULL) {
(void) ClosePEXFontFile(fp);
return (BadAlloc); }
if (fread((char *) table, sizeof(Dispatch), font->num_ch, fp)
!= font->num_ch) {
xfree((char *) table);
(void) ClosePEXFontFile(fp);
return (PEXERR(PEXFontError)); }
font->ch_data =
(Ch_stroke_data **) xalloc((unsigned long)(sizeof(Ch_stroke_data *) *
font->num_ch));
if (font->ch_data == NULL) {
xfree((char *) table);
(void) ClosePEXFontFile(fp);
return (BadAlloc); }
/* The next loop initializes all ch_data pointers to null; essential
for non-crashing during font clean-up in case of failed font file
read. Also count the number of non-blank chars.
*/
for ( i = 0, ch_font = font->ch_data, tblptr = table, numChars = 0;
i < font->num_ch;
i++, ch_font++, tblptr++) {
*ch_font = NULL;
if (tblptr->offset != 0) numChars++; }
ch_stroke = (Ch_stroke_data *)xalloc((unsigned long)(numChars *
sizeof(Ch_stroke_data)));
if (!ch_stroke) {
err = BadAlloc;
goto disaster;
}
/* read in the char data (the font file format should be changed
so that the allocation can be done outside this loop--this
method is inefficient)
*/
for ( i = 0, ch_font = font->ch_data, tblptr = table, found_first = 0;
i < font->num_ch;
i++, ch_font++, tblptr++) {
if (tblptr->offset != 0) {
(*ch_font) = ch_stroke++;
(*ch_font)->strokes.ddList = NULL;
(*ch_font)->center = table[i].center;
(*ch_font)->right = table[i].right;
(void) SetPEXFontFilePtr(fp, tblptr->offset);
/* read in the type, number of subpaths, and n_vertices fields */
if ( (fread(&((*ch_font)->type),
sizeof(Font_path_type), 1, fp) != 1)
|| (fread(&((*ch_font)->strokes.numLists),
sizeof(ddULONG),1,fp) != 1)
|| (fread(&((*ch_font)->n_vertices),
sizeof(ddULONG), 1, fp) != 1) )
{
err = PEXERR(PEXFontError);
goto disaster;
}
(*ch_font)->strokes.maxLists = (*ch_font)->strokes.numLists;
if ((*ch_font)->strokes.numLists > 0) {
if (get_stroke(*ch_font, fp)) {
err = BadAlloc;
goto disaster; }
if (!found_first) {
font->font_info.firstGlyph = i;
found_first = 1; }
font->font_info.lastGlyph = i; }
}
}
xfree((char *)table);
(void) ClosePEXFontFile(fp);
return (Success);
disaster:
(void) ClosePEXFontFile(fp);
if (table) xfree(table);
#if 0
if (pFont == defaultPEXFont) defaultPEXFont = 0; /* force free */
#endif
FreePEXFont((diFontHandle) pFont, pFont->id);
return (err);
}
void
ClosePEXFontFile(fp)
FILE *fp;
{
fclose (fp);
}
void
SetPEXFontFilePtr(fp,where)
FILE *fp;
long where;
{
(void) fseek(fp, where, SEEK_SET); /* set pointer at "where" bytes
from the beginning of the file */
}
|