Back to index

wims  3.65+svn20090927
font.c
Go to the documentation of this file.
00001 /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 /* tex 2 gif translator, font loader */
00019 
00020 void mf2font(char *fontname, int density)
00021 {
00022     loadtfm(fontname);
00023     makegf(fontname,density);
00024     loadgf(fontname,density);
00025     gf2font(fontname,density);
00026 }
00027 
00028 FONT *loadfont(char *fname, int checksum, int density, FONT *ft)
00029 {
00030     char namebuf[2048];
00031     char *fcache, *cc;
00032     long int l,l2;
00033     FILE *f;
00034     int newfont;
00035     
00036     newfont=0;
00037     for(cc=fname; isalnum(*cc); cc++); 
00038     if(*cc!=0) error("Bad font name.");
00039     snprintf(namebuf,sizeof(namebuf),"%s/%d/%s.font",fontdir,density,fname);
00040     f=fopen(namebuf,"r"); if(f==NULL) {
00041        renewfont: mf2font(fname,density);
00042        f=fopen(namebuf,"r"); newfont=1;
00043     }
00044     if(f==NULL) return NULL;
00045     fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
00046     if(l<=0 || l>FILE_LENGTH_LIMIT) return NULL;
00047     fcache=xmalloc(l+16);
00048     l2=fread(fcache,1,l,f); if(l2!=l) {
00049        error("Error reading font file.");
00050     }
00051     memmove(&ft->checksum,fcache,sizeof(int));
00052     memmove(&ft->designsize,fcache+sizeof(int),sizeof(int));
00053     memmove(&ft->bc,fcache+2*sizeof(int),sizeof(int));
00054     memmove(&ft->ec,fcache+3*sizeof(int),sizeof(int));
00055     ft->cnt=ft->ec-ft->bc+1;
00056     cc=fcache+4*sizeof(int); ft->fh=(FONTHEADER *) cc;
00057     ft->data=fcache+4*sizeof(int)+ft->cnt*sizeof(FONTHEADER);
00058     ft->cache=fcache;
00059     fclose(f);
00060     if(checksum!=ft->checksum && checksum!=0 && ft->checksum!=0) {
00061        if(newfont) {
00062            fprintf(stderr,"%08X != %08X\n",checksum, ft->checksum);
00063            error("Font checksum discord.");
00064        }
00065        free(fcache); unlink(namebuf); goto renewfont;
00066     }
00067     return ft;
00068 }
00069