home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / printer / dvi2pcl.lha / sortfonts.c < prev    next >
C/C++ Source or Header  |  1992-11-25  |  2KB  |  80 lines

  1. /*
  2.  * Sorts order of fonts used in the dvi-file and not stored permanently in
  3.  * the printers memory in respect to font usage. Therefore a rating value
  4.  * is calculated for each font from their character usage in the document.
  5.  * The fonts are ordered in `fontlist' in order of their rating value
  6.  * `usage'. The function returns the number of different fonts used, but
  7.  * not stored  permanently. As a side effect, for each font the first and
  8.  * last character, used in the document ist stored in font->bc and font->ec
  9.  * resp., to avoid unnecessary searching later on.
  10.  */
  11.  
  12. #include "globals.h"
  13.  
  14. int sortfonts(fontlist)
  15. int fontlist[];
  16. {
  17.     int    f;
  18.     int    i;
  19.     int    t;
  20.     int    store_all;
  21.     int    fontnum = 0;
  22.     long    total;
  23.     long    different;
  24.     long    tt;
  25.     long    usage[MAXFONTS];
  26.     charfmt    *u;
  27.     charfmt    *cbase;
  28.  
  29.     /* Determine rating value for each font used but not perm loaded */
  30.     for (f = 0 ; f < MAXFONTS ; f++)
  31.         if ((font = fontptr[f]) && (font->down < 0)) {
  32.             cbase = font->chr;
  33.             total = different = 0;
  34.             font->bc = 256;
  35.             font->ec = -1;
  36.             for(i = 0 ; i < 256 ; i++)
  37.                 if((cbase + i)->use_count)
  38.                 {
  39.                     font->bc = i;
  40.                     break;
  41.                 }                      /* First char used */
  42.             for(i = 255 ; i >= font->bc ; i--)
  43.                 if((cbase + i)->use_count) {
  44.                     font->ec = i;
  45.                     break;
  46.                 }                      /* Last char used  */
  47.             for(i = font->bc ; i <= font->ec ; i++) {
  48.                 u = cbase + i;
  49.                 if(u->use_count) {
  50.                     different++;
  51.                     total += u->use_count;
  52.                 }    /* Different used chars */
  53.             }        /* and total usage      */
  54.  
  55.             fontlist[fontnum] = f;
  56.  
  57.             /* Calculate rating */
  58.             if(different)
  59.                 usage[fontnum++] =
  60.                     (256*(total - different))/different;
  61.             else
  62.                 usage[fontnum++] = 0;
  63.         }
  64.  
  65.     /* Now sort used fonts in order of the usage rating values */
  66.     for(f = fontnum - 1 ; f > 0 ; f--)
  67.         for(i = 0 ; i < f ; i++)
  68.             if(usage[i] < usage[f]) {
  69.                 tt = usage[i];
  70.                 usage[i] = usage[f];
  71.                 usage[f] = tt;
  72.                 t  = fontlist[i];
  73.                 fontlist[i] = fontlist[f];
  74.                 fontlist[f] = t;
  75.             }
  76.  
  77.     /* Return the number of different fonts not permanently loaded */
  78.     return(fontnum);
  79. }
  80.