home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 355.lha / printfonts_v1.10 / PrintFonts.c < prev    next >
C/C++ Source or Header  |  1990-03-12  |  10KB  |  315 lines

  1. /*PrintFonts V1.10 (c)1989,1990 by Dave Schreiber.  All rights reserved*/
  2. /*Must be freely distributed, except for shipping, copying, media, and*/
  3. /*related costs.*/
  4. /*V1.00 finished August 30, 1989*/
  5. /*V1.10 finished January 21, 1990*/
  6.  
  7. /*    New features:  can print in more than one column
  8.                      can determine the maximum number of columns to use
  9.                      can print an alternate description of a font
  10.                         (good for use with unreadable fonts like
  11.                         Cairo or Music).
  12. */
  13.  
  14. /*Compiled using Lattice C V5.04*/
  15.  
  16. #include "setup.h"
  17.  
  18.  
  19. int InterpretArgs();
  20. WORD PrintPage();
  21. void DumpWindowToPrinter();
  22.  
  23. main(argc,argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.    struct AvailFontsHeader *afh;
  28.    ULONG afhsize; /*Size of AvailFontsHeader buffer*/
  29.    ULONG numfonts,curfont;
  30.    BYTE max,readable,both;
  31.    WORD columns;
  32.    WORD maxlen;
  33.    struct AvailFonts *af;
  34.    
  35.    if(argc==2 && argv[1][0]=='?')   /*User wants help*/
  36.    {
  37.       puts("PrintFonts V1.10");
  38.       puts("(c)1989, 1990 by Dave Schreiber.  All rights reserved.");
  39.       puts("Format for use:");
  40.       puts("  1>  PrintFonts [options]");
  41.       puts("Where options are any of the following:");
  42.       puts("  -r:  Print each font's name in a readable format");
  43.       puts(" -c#:  Use # number of columns");
  44.       puts("  -m:  Determine the max number of columns that can be");
  45.       puts("       used and do not print.");
  46.       puts("  -b:  Determines the maximum number of columns and");
  47.       puts("       prints using that number of columns");
  48.       exit(0);
  49.    }
  50.    
  51.       /*Parse the command line arguments and set the appropriate flags*/
  52.    if(!InterpretArgs(argc,argv,&max,&readable,&columns,&both))
  53.       exit(1000);
  54.  
  55.    if(columns<1)
  56.    {
  57.       puts("You must have at least one column!");
  58.       exit(2000);
  59.    }
  60.    
  61.    startup();  /*Open Libraries, etc.*/
  62.    afhsize=AvailFonts(NULL,NULL,AFF_MEMORY|AFF_DISK);
  63.    
  64.    afh=(struct AvailFontsHeader *)AllocMem(afhsize,MEMF_CLEAR); 
  65.       /*Gets a buffer large enough to hold all the font information*/
  66.       /*(AvailFonts(0,0,[type]) returns the amount of memory needed*/
  67.       /*to hold the information for all the fonts of type [type])  */
  68.  
  69.    if(afh==NULL)  /*Could't allocate the memory*/
  70.    {
  71.       closeup();
  72.       ExitProg("Couldn't allocate enough memory!");
  73.    }
  74.    
  75.    AvailFonts(afh,afhsize,0xff); /*Get the info for fonts*/
  76.  
  77.    TopazAttr.ta_Name="topaz.font"; /*Topaz is used if the user wants*/
  78.    TopazAttr.ta_YSize=8;           /*'readable' font names*/
  79.    TopazAttr.ta_Style=0;
  80.    TopazAttr.ta_Flags=FPF_ROMFONT|FPF_DISKFONT;
  81.    
  82.    if((Topaz=(struct TextFont *)OpenFont(&TopazAttr))==NULL)
  83.    {
  84.       FreeMem(afh,afhsize);
  85.       closeup();
  86.       ExitProg("Can't open Topaz 8 point font!!!!!!!!!!!!");
  87.    }
  88.     
  89.    numfonts=afh->afh_NumEntries;  /*Get the number of fonts*/
  90.    curfont=1;                     /*First font*/
  91.    af=(struct AvailFonts *)&afh[curfont];
  92.    while(curfont <= numfonts)
  93.       maxlen=PrintPage(&curfont,numfonts,af,columns,max,readable);
  94.          
  95.    maxlen=MAX_X/maxlen;
  96.    if(maxlen < 1)
  97.       maxlen=1;
  98.       
  99.    if(max)  /*If just finding the max length*/
  100.    {
  101.       printf("These fonts can be printed in %d column",maxlen);
  102.       if(maxlen==1)
  103.          printf(".\n");
  104.       else
  105.          printf("s.\n");
  106.    }
  107.    
  108.    if(both) /*If -b switch, do it again, but print this time*/
  109.    {
  110.       curfont=1;
  111.       af=(struct AvailFonts *)&afh[curfont];
  112.       while(curfont <= numfonts)
  113.          PrintPage(&curfont,numfonts,af,maxlen,FALSE,readable);
  114.    }
  115.  
  116.    FreeMem(afh,afhsize);    /*Free up the allocated memory*/
  117.  
  118.    CloseFont(Topaz);
  119.    closeup();
  120. }
  121.  
  122. int InterpretArgs(argc,argv,max,readable,cols,both)
  123. int argc;
  124. char *argv[];
  125. BYTE *max,*readable,*both;
  126. WORD *cols;
  127. {
  128.    BYTE c;
  129.    int columns;
  130.    
  131.    *cols=1;    /*Load in some defaults*/
  132.    *max=FALSE;
  133.    *readable=FALSE;
  134.    *both=FALSE;
  135.    
  136.    for (c=1;c<argc;c++)    /*For each argument*/
  137.       switch(argv[c][1])   /*Letter after the slash*/
  138.       {
  139.          case 'b':      /*For -b switch, set both*/
  140.          case 'B':
  141.             *both=TRUE; /*AND max*/
  142.          case 'm':   /*Max columns mode*/
  143.          case 'M':   /*Just in case the user typed -T instead of -t*/
  144.             *max=TRUE;
  145.             break;
  146.          case 'r':   /*Readable*/
  147.          case 'R':
  148.             *readable=TRUE;
  149.             break;
  150.          case 'c':   /*Number of columns*/
  151.          case 'C':
  152.             if(argv[c][2]==NULL)
  153.                break;
  154.             stcd_i(&argv[c][2],&columns); /*Convert text to #*/
  155.             *cols=columns;
  156.             break;
  157.          default:
  158.             puts("I don't understand option:");
  159.             puts(argv[c]);
  160.             return(FALSE);
  161.       }
  162.    return(TRUE);
  163. }
  164.  
  165.    /*Setup and print a page of fonts*/
  166. WORD PrintPage(curfont,numfonts,af,cols,findmax,readable)
  167. ULONG *curfont;
  168. ULONG numfonts;
  169. struct AvailFonts *af;
  170. BYTE findmax,readable;
  171. WORD cols;
  172. {
  173.    
  174. #define FLAGS af->af_Attr.ta_Flags
  175. #define Rp Wdw->RPort
  176.  
  177.    BYTE Full=FALSE;
  178.    USHORT TextY=STARTINGROW;        /*Current Y coordinate*/
  179.    USHORT LastY;                    /*Last Y coordinate*/
  180.    static WORD max=0;
  181.    WORD length;
  182.    WORD column,StartX;
  183.    WORD ColWidth;
  184.    
  185.    char Master[256],fontstr[256],SizeStr[6],*string;
  186.    struct TextFont *tf;
  187.    struct TextAttr TFont;
  188.  
  189.    ColWidth=MAX_X/cols; /*Get the # of pixels in each column*/
  190.    
  191.    string = (char *)&Master[1]; /*Initialize a pointer and*/
  192.    Master[0]='(';       /*a string*/
  193.    
  194.    SetAPen(Rp,0);
  195.    RectFill(Rp,0,0,MAX_X-1,MAX_Y-1);
  196.    SetAPen(Rp,1);
  197.    ClearScreen(Rp);     /*Clear the bitmap*/
  198.    af+=(*curfont-1);
  199.    
  200.    
  201.    for(column=StartX=0;column<cols && *curfont <= numfonts;
  202.          StartX=(++column*ColWidth)) /*Loop for each column*/
  203.    {
  204.       TextY=STARTINGROW;
  205.       Full=FALSE;
  206.       
  207.       while(!Full && (*curfont <= numfonts))
  208.       {      
  209.          /*If the font is:  not removed, goes from left to right*/
  210.          /*and is EITHER a disk or memory font, do nothing.*/
  211.          /*(This is partly from the RKM)*/
  212.          if((af->af_Attr.ta_Style==0) &&
  213.          !( ((FLAGS & FPF_REMOVED)||(FLAGS & FPF_REVPATH) ||
  214.                ((FLAGS & FPF_DISKFONT) && (af->af_Type & AFF_MEMORY))) ) )
  215.             if((af->af_Attr.ta_YSize) < (MAX_Y-(4+TextY))) /*If it'll fit...*/
  216.             {
  217.             
  218.                TFont.ta_Name=(char *)af->af_Attr.ta_Name;  /*Get the name*/
  219.                TFont.ta_YSize=af->af_Attr.ta_YSize; /*Get the size*/
  220.                TFont.ta_Style=af->af_Attr.ta_Style; /*Plain style*/
  221.                TFont.ta_Flags=FPF_ROMFONT|FPF_DISKFONT|
  222.                      FPF_PROPORTIONAL|FPF_DESIGNED;  /*Straight from RKM*/
  223.             
  224.                if((tf=(struct TextFont *)OpenDiskFont(&TFont))!=NULL)
  225.                {
  226.                      /*Position 'cursor' at baseline*/
  227.                   TextY+=(LastY=(tf->tf_Baseline));
  228.                   strcpy(fontstr,af->af_Attr.ta_Name);
  229.                   strcpy(string,fontstr);  /*Get font name*/
  230.                   string[strlen(string)-5]=NULL;
  231.                   strcat(string," ");      /*Tack on a space*/
  232.                   stcu_d(SizeStr,af->af_Attr.ta_YSize); /*Get the size in chars*/
  233.                   strcat(string,SizeStr);
  234.                   strcat(string," ");
  235.                   
  236.                   Move(Rp,StartX,TextY);               
  237.                   SetFont(Rp,tf);      /*Set the font*/
  238.                   Text(Rp,string,strlen(string));  /*Print the font info*/
  239.                
  240.                         /*Get the length of the text string in pixels*/
  241.                   length=TextLength(Rp,string,strlen(string));
  242.                   
  243.                   if(readable) /*If -r switch...*/
  244.                   {
  245.                      Master[strlen(Master)-1]=')';
  246.                      SetFont(Rp,Topaz);
  247.                      Text(Rp,Master,strlen(Master));
  248.                      length+=TextLength(Rp,Master,strlen(Master));
  249.                   }
  250.                   
  251.                   if(length > max)
  252.                      max=length; /*And store in max if the biggest*/
  253.    
  254.                      /*Add decender area to height*/
  255.                   TextY+=(LastY=(tf->tf_YSize-tf->tf_Baseline+2));
  256.                   CloseFont(tf);   /*Close the font*/
  257.                }
  258.                (*curfont)++;
  259.                af++;
  260.             }
  261.             else
  262.                Full = TRUE;
  263.          else     
  264.          {
  265.             (*curfont)++;
  266.             af++;
  267.          }  
  268.       }
  269.    }
  270.    
  271.    if(!findmax)   /*Print fonts if -m switch isn't set*/
  272.       DumpWindowToPrinter(Wdw,printerMsg,
  273.             (TextY+LastY>MAX_Y ? MAX_Y : TextY+LastY));
  274.    /*Add a little for serifs, but not more than MAX_Y*/
  275.    return(max); /*And return the max text length*/
  276. }
  277.  
  278. void DumpWindowToPrinter(Wdw,request,LastY)
  279. struct Window *Wdw;
  280. union printerIO *request;
  281. USHORT LastY;
  282. {
  283.    struct ViewPort *Vp;
  284.    char FormFeed[2];
  285.    FormFeed[0]=0x0c;       /*Form feed character*/
  286.    FormFeed[1]=NULL;       /*NULL terminator*/
  287.    Vp=&Wdw->WScreen->ViewPort;
  288.    /*Set up for dumping the rastport*/
  289.    request->iodrp.io_Command=PRD_DUMPRPORT;
  290.    request->iodrp.io_RastPort=Rp;
  291.    request->iodrp.io_ColorMap=Vp->ColorMap;
  292.    request->iodrp.io_Modes=Vp->Modes;
  293.    request->iodrp.io_SrcX=0;
  294.    request->iodrp.io_SrcY=0;
  295.    request->iodrp.io_SrcWidth=MAX_X;
  296.    request->iodrp.io_SrcHeight=MAX_Y;
  297.    request->iodrp.io_DestCols=0;
  298.    request->iodrp.io_DestRows=0;
  299.    request->iodrp.io_Special=SPECIAL_FULLCOLS|
  300.             SPECIAL_FULLROWS;
  301.    DoIO(request); /*Do the actual dump*/
  302.    
  303.    /*Do a form feed*/
  304.    /*Perhaps this extra step is unnecessary, but I find it works better*/
  305.    /*with my setup (DeskJet+ and Epson LX-800) than leaving out this and*/
  306.    /*the SPECIAL_NOFORMFEED switch above*/
  307. #ifdef FFEW
  308.    request->ios.io_Command=CMD_WRITE;
  309.    request->ios.io_Data=(APTR)FormFeed;
  310.    request->ios.io_Length=-1;  /*-1 == NULL terminated string*/
  311.    DoIO(request); /*Do the form feed*/
  312. #endif
  313. }
  314.  
  315.