Back to index

wims  3.65+svn20090927
gf.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 /* dvi 2 gif driver, gf font loader and processor */
00019 
00020 int g;
00021 FONTHEADER *fh;
00022 int brush, x,y,xmax,ymax,thischar,xsize,charsize,charcnt,charpos;
00023 int chswitch;
00024 long int datapos;
00025 char *onechar;
00026 unsigned char *gfbuf;
00027 int gfbuflen;
00028 FILE *fontf;
00029 
00030 void GF_eoc(void)
00031 {
00032     if(onechar==NULL) return;
00033     fwrite(onechar,1,charsize,fontf); datapos+=charsize;
00034     free(onechar); onechar=NULL;
00035 }
00036 
00037 void GF_boc(int p)
00038 {
00039     unsigned char cc;
00040     int min_m, min_n, max_m, max_n, del_m, del_n;
00041     if(!chswitch) return;
00042     if(p==0) {
00043        cc=texint(gfbuf+(++g),4); g+=8;
00044        min_m=texint(gfbuf+g,4); g+=4;
00045        max_m=texint(gfbuf+g,4); g+=4;
00046        min_n=texint(gfbuf+g,4); g+=4;
00047        max_n=texint(gfbuf+g,4); g+=4;
00048        del_m=max_m-min_m; del_n=max_n-min_n;
00049        g--;
00050     }
00051     else {
00052        cc=gfbuf[++g];
00053        del_m=gfbuf[++g]; max_m=gfbuf[++g];
00054        del_n=gfbuf[++g]; max_n=gfbuf[++g];
00055        min_m=max_m-del_m; min_n=max_n-del_n;
00056     }
00057     if(cc>tfm.ec || cc<tfm.bc) return;
00058     charpos=cc-tfm.bc;
00059     if(del_m<0) del_m=0; if(del_n<0) del_n=0;
00060     if(del_m>=MAX_FONT_X-1) del_m=MAX_FONT_X-2;
00061     if(del_n>=MAX_FONT_Y-1) del_n=MAX_FONT_Y-2;
00062     x=y=0; xmax=del_m+1; ymax=del_n+1;
00063     fh[charpos].xstart=min_m; fh[charpos].ystart=-max_n;
00064     fh[charpos].xmax=xmax; fh[charpos].ymax=ymax;
00065     brush=0; thischar=cc;
00066     xsize=(xmax+7)/8; charsize=xsize*ymax;
00067     onechar=xmalloc(charsize+1024);
00068     memset(onechar,0,charsize+1024);
00069     fh[charpos].start=datapos;
00070     fh[charpos].w=tfm.f[cc].w;
00071 }
00072 
00073 void GF_paint(int p)
00074 {
00075     int i;
00076     if(onechar==NULL) return;
00077     if(brush) for(i=x;i<p+x && i<xmax;i++) {
00078        onechar[y*xsize+(i>>3)]|=(1<<(i&7));
00079     }
00080     x+=p; if(x>xmax) x=xmax;
00081     brush^=1;
00082 }
00083 
00084 void GF_new_row(int p)
00085 {
00086     y++; if(y>ymax) y=ymax;
00087     x=p; if(x>xmax) x=xmax;
00088     brush=1;
00089 }
00090 
00091 void GF_skip(int p)
00092 {
00093     if(p>0) p=texint(gfbuf+(++g),p);
00094     p++; y+=p; if(y>ymax) y=ymax;
00095     x=0; brush=0;
00096 }
00097 
00098 void GF_xxx(int p)
00099 {
00100     unsigned int t;
00101     t=texint(gfbuf+(++g),p);
00102     g+=p+t-1;
00103 }
00104 
00105 void GF_yyy(void)
00106 {
00107     g+=4;
00108 }
00109 
00110 void GF_char_loc(int p)
00111 {
00112     if(p==0) g+=1+1+4+4;
00113     else g+=1+4+4+4+4;
00114 }
00115 
00116 void GF_post(void)
00117 {
00118     chswitch=0;
00119 }
00120 
00121 void GF_post_post(void)
00122 {
00123     g=gfbuflen; chswitch=0;
00124 }
00125 
00126 #include "gfcmd.c"
00127 
00128 void loadgf(char *fname, int density)
00129 {
00130     int len;
00131     char namebuf[128];
00132     
00133     snprintf(namebuf,sizeof(namebuf),"%s/texgf.%dgf",tmpdir,density);
00134     len=getfile(namebuf,&gfbuf);
00135     if(len<=0) {
00136        error("Metafont failed.");
00137     }
00138     gfbuflen=len;
00139 }
00140 
00141 void makegf(char *fontname, int density)
00142 {
00143     call_sh("mkdir -p %s\n\
00144 cd %s\n\
00145 rm -f texgf.* 2>/dev/null\n\
00146 cat >texgf.mf <<@\n\
00147 batchmode;\n\
00148 mode_def texgf =\n\
00149        mode_param (pixels_per_inch, %d);\n\
00150        mode_param (blacker, %f);\n\
00151        mode_param (fillin, 0);\n\
00152        mode_param (o_correction, 0);\n\
00153        mode_common_setup_;\n\
00154 enddef;\n\
00155 mode = texgf;\n\
00156 input %s;\n\
00157 @\n\
00158 mf texgf </dev/null || mf-nowin texgf </dev/null\n\
00159 ", fontdir, tmpdir, density, pow((double)density/100,0.75)*blacker, fontname);
00160 }
00161 
00162 void gf2font(char *fontname,int density)
00163 {
00164     unsigned char cc;
00165     char namebuf[1024];
00166     char tmpname[1024];
00167     
00168     if((gfbuf[0]&255)!=gf_pre || (gfbuf[1]&255)!=131) return;
00169     g=3+(gfbuf[2]&255);
00170     if(gfbuflen<=g) return;
00171     if(tfm.ec==0 && tfm.bc==0) return;
00172     charcnt=tfm.ec-tfm.bc+1;
00173     fh=xmalloc(charcnt*sizeof(FONTHEADER)+256);
00174     memset(fh,0,charcnt*sizeof(FONTHEADER));
00175     snprintf(tmpname,sizeof(tmpname),"%s/wims.fonttmp",tmpdir);
00176     fontf=fopen(tmpname,"w"); if(fontf==NULL) return;
00177     brush=0; x=y=xmax=ymax=datapos=0; onechar=NULL; chswitch=1;
00178     for(;g<gfbuflen;g++) {
00179        cc=gfbuf[g];
00180        if(cc<=63) {GF_paint(cc); continue;}
00181        if(cc>=74 && cc<=238) {GF_new_row(cc-74); continue;}
00182        switch(cc) {
00183            case gf_boc:     GF_boc(0); break;
00184            case gf_boc1:    GF_boc(1); break;
00185            case gf_eoc:     GF_eoc(); break;
00186            case gf_skip0:   GF_skip(0); break;
00187            case gf_skip1:   GF_skip(1); break;
00188            case gf_skip2:   GF_skip(2); break;
00189            case gf_skip3:   GF_skip(3); break;
00190            case gf_char_loc:       GF_char_loc(1); break;
00191            case gf_char_loc0:      GF_char_loc(0); break;
00192            case gf_xxx1:    GF_xxx(1); break;
00193            case gf_xxx2:    GF_xxx(2); break;
00194            case gf_xxx3:    GF_xxx(3); break;
00195            case gf_xxx4:    GF_xxx(4); break;
00196            case gf_yyy:     GF_yyy(); break;
00197            
00198            default: break;
00199        }
00200     }
00201     fclose(fontf);
00202     snprintf(namebuf,sizeof(namebuf),"%s/%s.font",
00203             tmpdir,fontname);
00204     fontf=fopen(namebuf,"w"); if(fontf!=NULL) {
00205        fwrite(&(tfm.checksum),sizeof(int),1,fontf);
00206        fwrite(&(tfm.designsize),sizeof(int),1,fontf);
00207        fwrite(&(tfm.bc),sizeof(int),1,fontf);
00208        fwrite(&(tfm.ec),sizeof(int),1,fontf);
00209        fwrite(fh,sizeof(FONTHEADER),charcnt,fontf);
00210        fclose(fontf);
00211        call_sh("cat %s >>%s; mkdir -p %s/%d; mv -f %s %s/%d",
00212               tmpname,namebuf,fontdir,density,namebuf,fontdir,density);
00213        unlink(tmpname);
00214     }
00215     free(fh);
00216 }
00217