buffer.h - abc2ps - A powerful sheet setting tool using the simple abc notation
 (HTM) git clone git://vernunftzentrum.de/abc2ps.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       buffer.h (4232B)
       ---
            1 /*  
            2  *  This file is part of abc2ps, 
            3  *  Copyright (C) 1996,1997,1998  Michael Methfessel
            4  *  See file abc2ps.c for details.
            5  */
            6 
            7 /*  subroutines to handle output buffer  */
            8 
            9 /* PUTn: add to buffer with n arguments */
           10 
           11 #define PUT0(f) {sprintf(mbf,f); a2b(mbf); }
           12 #define PUT1(f,a) {sprintf(mbf,f,a); a2b(mbf); }
           13 #define PUT2(f,a,b) {sprintf(mbf,f,a,b); a2b(mbf); }
           14 #define PUT3(f,a,b,c) {sprintf(mbf,f,a,b,c); a2b(mbf); }
           15 #define PUT4(f,a,b,c,d) {sprintf(mbf,f,a,b,c,d); a2b(mbf); }
           16 #define PUT5(f,a,b,c,d,e) {sprintf(mbf,f,a,b,c,d,e); a2b(mbf); }
           17 
           18 
           19 /* ----- a2b: appends string to output buffer ----- */
           20 void a2b (t)
           21 char *t;
           22 {
           23   size_t l,i;
           24 
           25   l=strlen(t);
           26   /*  printf ("Append %d <%s>\n", l, t); */
           27 
           28   if (nbuf+l>BUFFSZ) {
           29     printf("+++ a2b: buffer full, BUFFSZ=%d\n", BUFFSZ);
           30     exit (1);
           31   }
           32 
           33   for (i=0;i<l;i++) buf[nbuf+i]=t[i];
           34   nbuf += l;
           35 
           36 }
           37 
           38 
           39 /* ----- bskip(h): translate down by h points in output buffer ---- */
           40 void bskip(h)
           41 float h;
           42 {
           43   if (h*h>0.0001) {
           44     PUT1("0 %.2f T\n", -h)
           45     bposy=bposy-h;
           46   }
           47 }
           48 
           49 /* ----- init_pdims: initialize page dimensions ----- */
           50 void init_pdims ()
           51 {
           52   if (in_page) return;
           53   posx=cfmt.leftmargin;
           54   posy=cfmt.pageheight-cfmt.topmargin; 
           55 
           56 }
           57 
           58 /* ----- clear_buffer ------- */
           59 void clear_buffer ()
           60 {
           61   nbuf   = 0;
           62   bposy  = 0.0;
           63   ln_num = 0;
           64 }
           65 
           66 /* ----- write_index_entry ------- */
           67 void write_index_entry ()
           68 {
           69   char s[801];
           70   float w,dx1,dx2;
           71 
           72   if (!index_initialized) init_index_file ();
           73   
           74   if (vb>=8) printf("Write index entry: %5d  <%s>\n",
           75                     pagenum, info.title);
           76   
           77   /* spacing determined here */
           78   index_posy = index_posy-1.2*cfmt.indexfont.size;
           79 
           80   if (index_posy-cfmt.indexfont.size < cfmt.botmargin) {
           81     close_index_page (findex);
           82     init_index_page (findex);
           83   }
           84 
           85   dx1 = 1.8*cfmt.indexfont.size;
           86   dx2 = dx1+cfmt.indexfont.size;
           87   
           88   tex_str (info.title,s,&w);
           89   if (strlen(s)==0) strcpy (s, "No title");
           90   fprintf (findex, "%.2f %.2f M (%d) lshow\n", 
           91            index_posx+dx1, index_posy, pagenum);
           92 
           93   fprintf (findex, "%.2f %.2f M (%s) S\n", 
           94            index_posx+dx2, index_posy, s);
           95 
           96   if (strlen(info.rhyth) || strlen(info.orig)) {
           97     fprintf (findex, "(  (");
           98     if (strlen(info.rhyth)) fprintf (findex, "%s", info.rhyth);
           99     if (strlen(info.rhyth) && strlen(info.orig))
          100       fprintf (findex, ", ");
          101     if (strlen(info.orig)) fprintf (findex, "%s", info.orig);
          102     fprintf (findex, ")) S\n");
          103   }
          104 
          105   
          106   if (strlen(info.comp[0])) fprintf (findex, "(   - %s) S\n", info.comp);
          107   
          108   if (cfmt.withxrefs) fprintf (findex, "(   [%s]) S\n", info.xref);
          109 
          110 }
          111 
          112 
          113 
          114 
          115 /* ----- write_buffer: write buffer contents, break at full pages ---- */
          116 void write_buffer (fp)
          117 FILE *fp;
          118 {
          119   int i,l,b1,b2,nb;
          120   float p1,dp;
          121   
          122   if (nbuf==0) return;
          123 
          124   writenum++;
          125   
          126   if ((writenum==1) && make_index) write_index_entry();
          127 
          128   nb=0;
          129   for (l=0;l<ln_num;l++) {
          130     b1=0;
          131     p1=0;
          132     if (l>0) {
          133       b1=ln_buf[l-1];
          134       p1=ln_pos[l-1];
          135     }
          136     b2=ln_buf[l];
          137     dp=ln_pos[l]-p1;
          138     if ((posy+dp<cfmt.botmargin) && (!epsf)) {
          139       write_pagebreak (fp);
          140     }
          141     for (i=b1;i<b2;i++) putc(buf[i],fp);
          142     posy=posy+dp;
          143     nb=ln_buf[l];
          144   }
          145 
          146   if (nb<nbuf) {
          147     for (i=nb;i<nbuf;i++) putc(buf[i],fp);
          148   }
          149   
          150   clear_buffer();
          151   return;
          152 }
          153 
          154 /* ----- buffer_eob: handle completed block in buffer ------- */
          155 /* if the added stuff does not fit on current page, write it out
          156    after page break and change buffer handling mode to pass though */
          157 void buffer_eob (fp)
          158 FILE *fp;
          159 {
          160   int do_break;
          161 
          162   if (ln_num>=BUFFLN) 
          163     rx("max number off buffer lines exceeded"," -- check BUFFLN");
          164   
          165   ln_buf[ln_num]=nbuf;
          166   ln_pos[ln_num]=bposy;
          167   ln_num++;
          168   
          169   if (!use_buffer) {
          170     write_buffer (fp);
          171     return;
          172   }
          173 
          174   do_break=0;
          175   if (posy+bposy<cfmt.botmargin) do_break=1;
          176   if (cfmt.oneperpage) do_break=1;
          177   
          178   if (do_break && (!epsf)) {
          179     if (tunenum != 1 ) write_pagebreak (fp);
          180     write_buffer (fp);
          181     use_buffer=0;
          182   }
          183 
          184   return;
          185 }
          186 
          187 /* ----- check_buffer: dump buffer if less than nb bytes avilable --- */
          188 void check_buffer (fp, nb)
          189 FILE *fp;
          190 int nb;
          191 {
          192   char mm[81];
          193 
          194   if (nbuf+nb>BUFFSZ) {
          195     sprintf (mm, "BUFFSZ exceeded at line %d", ln_num);
          196     wng("possibly bad page breaks, ", mm);
          197     write_buffer (fp);
          198     use_buffer=0;
          199   }
          200 }
          201 
          202