home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / printer / prfont12.lha / prfont.c < prev    next >
C/C++ Source or Header  |  1990-02-21  |  8KB  |  303 lines

  1.  
  2. /**********************************************************************/
  3. /*                                                                    */
  4. /* PrFont - Version 1.3                                               */
  5. /*                                                                    */
  6. /*  by Joel Swank 1/12/89                                             */
  7. /*                                                                    */
  8. /**********************************************************************/
  9.  
  10. /**********************************************************************/
  11. /*                                                                    */
  12. /* PrFont  - Prints a sample of all fonts in the fonts: directory     */
  13. /*                                                                    */
  14. /**********************************************************************/
  15. /*                                                                    */
  16. /*    Modification log:                                               */
  17. /*                                                                    */
  18. /*  2/5/89    Original Idea implemented                               */
  19. /*  2/12/89   Switched to Availfonts from directory scan              */
  20. /*  5/21/89   Adjust screen/window parameters                         */
  21. /*  5/30/89   fix open before getargs bug                             */
  22. /*                                                                    */
  23. /**********************************************************************/
  24.  
  25. #include <stdio.h>
  26. #include <functions.h>
  27. #include "myscreen.h"
  28. #include "defines.h"
  29.  
  30.  
  31. /*
  32.  *  Some Font stuff 
  33.  */
  34. struct TextFont *myfont = NULL;  /* pointer to open font  */
  35. struct TextAttr *myattr;         /* pointer to Attribute struct */
  36. struct AvailFontsHeader *avlbuf = NULL; /* pointer to availfonts buffer */
  37. struct AvailFonts *af;           /* pointer to availfonts entries */
  38.  
  39. /* Intuition pointers */
  40. struct Window    *Wind = NULL ;
  41. struct Screen    *MyScreen = NULL ;
  42. struct RastPort *rp;
  43.  
  44. /* Library pointers */
  45. struct Library *DiskfontBase = NULL;
  46. struct IntuitionBase    *IntuitionBase ;
  47. struct DosBase *DosBase = NULL;
  48. struct GfxBase *GfxBase = NULL;
  49.  
  50. char *rindex();
  51.  
  52. long screenpos;      /* current screen Y location */
  53. long all = FALSE;    /* flag - show all chars - set in doargs */
  54. int bufsize = 0;     /* size of availfonts buffer */
  55.  
  56. main(argc, argv)
  57. int argc;
  58. char *argv[];
  59. {
  60.     char tb;
  61.     register i, j;
  62.     int err;
  63.  
  64.     screenpos = SCREENPOSINIT;
  65.  
  66.     if (argc == 0) getWBargs();
  67.     else getCLIargs(argc,argv);
  68.  
  69.     open_all();  /* Open everything */
  70.  
  71.     /* find size of buffer needed */
  72.     bufsize = AvailFonts(&tb,1L,AFF_MEMORY | AFF_DISK)+2;
  73.  
  74.     /* get a buffer of that size */
  75.     avlbuf = (struct AvailFontsHeader *) AllocMem(bufsize,MEMF_PUBLIC);
  76.     if (avlbuf == NULL)
  77.         {
  78.         AutoRequest(Wind,&memmsg,0L,&oktxt,0L,0L,300L,75L);
  79.         done(26);
  80.         }
  81.  
  82.     /* get list of all the system fonts */
  83.     err = AvailFonts(avlbuf,bufsize,AFF_MEMORY | AFF_DISK);
  84.     if (err != NULL)
  85.         {
  86.         AutoRequest(Wind,&availmsg,0L,&oktxt,0L,0L,300L,75L);
  87.         done(28);
  88.         }
  89.  
  90.     af = (struct Availfonts *) &(avlbuf[1]);
  91.     for (j=0; j<avlbuf->afh_NumEntries; j++,af++)
  92.         {          /* weed out duplicates */
  93.         if ((af->af_Attr.ta_Flags & FPF_REMOVED) ||  /* fi removed ...  */
  94.             (af->af_Attr.ta_Flags & FPF_REVPATH) ||  /* or reverse ...  */
  95.             ((af->af_Type & AFF_MEMORY) &&   /* or NOT memory AND disk  */
  96.             (af->af_Attr.ta_Flags & FPF_DISKFONT))); /* then skip */
  97.         else {
  98.             myattr = &(af->af_Attr);
  99.             dofont();
  100.             }
  101.         }
  102.  
  103.  
  104.     if (screenpos > SCREENPOSINIT) do_print();
  105.     done(0);
  106. }
  107.  
  108. /*
  109.  * do font described by myattr
  110.  */
  111.  
  112. dofont()
  113. {
  114.  
  115.     char stringbuf[500];
  116.     char fontsave[50];
  117.     int istart;
  118.     register long fheight;
  119.     register long  i;
  120.     register char *j;
  121.  
  122.     /* get a copy of name */
  123.     strcpy(fontsave,myattr->ta_Name);
  124.     j = rindex(fontsave,'.');
  125.     if (j != NULL) *j = '\0';
  126.  
  127.     if ((myfont = (struct TextFont *) OpenDiskFont(myattr)) == NULL) 
  128.         {
  129.         sprintf(msgfilename,"%s %ld\n",fontsave,
  130.             myattr->ta_YSize);
  131.         AutoRequest(Wind,&openfomsg,0L,&oktxt,0L,0L,300L,75L);
  132.         }
  133.     else
  134.         {
  135.         fheight = myfont->tf_YSize; /* get actual size */
  136.         /* not the requested  font, skippit */
  137.         if (fheight != myattr->ta_YSize) return;
  138.         SetFont(rp,myfont);
  139.         /* build string to print */
  140.         sprintf(stringbuf,"%s %ld ",fontsave,fheight);
  141.         j = stringbuf+strlen(stringbuf);
  142.         istart=myfont->tf_LoChar;
  143.         if (istart <=0) istart = 1;
  144.         for (i=istart; i<= myfont->tf_HiChar; i++)
  145.             *j++ = (char) i;
  146.         stringbuf[j++] = '\0';
  147.         if (all)
  148.             {
  149.             display_font(stringbuf);
  150.             }
  151.         else
  152.             {
  153.             /* move down enough for  */
  154.             screenpos += fheight+2;
  155.             if (screenpos > MyScreen->Height) 
  156.                 {      /* when screen gets full, print it */
  157.                 do_print();
  158.                 screenpos = SCREENPOSINIT+fheight;
  159.                 clr_scr();
  160.                 }
  161.             /* move to baseline of charcter */
  162.             Move(rp,2,screenpos-fheight+myfont->tf_Baseline);
  163.             Text(rp,stringbuf,strlen(stringbuf));
  164.             }
  165.         CloseFont(myfont);
  166.         myfont = NULL;
  167.         }
  168. }
  169.  
  170. /*
  171.  *  display all characters of a font
  172.  */
  173.  
  174. display_font(buf)
  175. register char *buf;
  176. {
  177.     register int start = 0;
  178.     register int i;
  179.     int indent = 2;
  180.     int fheight;
  181.     while (1)
  182.         {
  183.         i=0;
  184.         while(buf[i+start] != '\0')
  185.             {
  186.             if (TextLength(rp,&buf[start],i) >= Wind->Width-indent)
  187.                 {
  188.                 i--;
  189.                 break;
  190.                 }
  191.             i++;
  192.             }
  193.         if (i==0) break;
  194.         fheight = myfont->tf_YSize; /* get actual size */
  195.         /* move down enough for  */
  196.         screenpos += fheight+2;
  197.         if (screenpos > MyScreen->Height) 
  198.             {      /* when screen gets full, print it */
  199.             do_print();
  200.             screenpos = SCREENPOSINIT+fheight;
  201.             clr_scr();
  202.         }
  203.         /* move to baseline of charcter */
  204.         Move(rp,indent,screenpos-fheight+myfont->tf_Baseline);
  205.         Text(rp,&buf[start],i);
  206.         indent = 30;
  207.         start += i;
  208.         }
  209. }
  210.  
  211.  
  212. /*
  213.  *   Open all the stuff I need
  214.  */
  215. open_all()
  216. {
  217.  
  218.     /*    Open the screen and window   */
  219.  
  220.     if ((IntuitionBase = (struct IntuitionBase *)
  221.         OpenLibrary("intuition.library", NULL)) == NULL)
  222.         done(21); /* if no intuition I can't even autorequest */
  223.  
  224.     if ((MyScreen = (struct Screen *)
  225.         OpenScreen(&NewScreenStructure)) == NULL)
  226.         {
  227.         AutoRequest(0L,&screenfail,0L,&oktxt,0L,0L,300L,75L);
  228.         done(23);
  229.         }
  230.  
  231.     New_Window.Screen = MyScreen;
  232.  
  233.     if ((Wind = (struct Window *) OpenWindow(&New_Window)) == NULL)
  234.         {
  235.         AutoRequest(0L,&windfail,0L,&oktxt,0L,0L,300L,75L);
  236.         done(25);
  237.         }
  238.  
  239.     rp = Wind->RPort;
  240.  
  241.     /**** Open the Libraries   ****/
  242.  
  243.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
  244.     if (GfxBase == NULL)
  245.         {
  246.         strcpy(msgfilename,"Graphics");
  247.         AutoRequest(Wind,&libfailmsg,0L,&oktxt,0L,0L,300L,75L);
  248.         done(20);
  249.         }
  250.  
  251.     DiskfontBase = (struct Library *)OpenLibrary("diskfont.library", NULL);
  252.     if (DiskfontBase == NULL) {
  253.         strcpy(msgfilename,"DiskFont");
  254.         AutoRequest(Wind,&libfailmsg,0L,&oktxt,0L,0L,300L,75L);
  255.         done(22);
  256.         }
  257.  
  258.  
  259.     if ((DosBase = (struct DosBase *)
  260.         OpenLibrary("dos.library", 0)) == NULL)
  261.         {
  262.         strcpy(msgfilename,"Dos");
  263.         AutoRequest(Wind,&libfailmsg,0L,&oktxt,0L,0L,300L,75L);
  264.         done(27);
  265.         }
  266.  
  267. }
  268. /*
  269.  * done - just clean up that which is open, and then leave.
  270.  */
  271. done(how)
  272. int how;
  273.     {
  274.     if (avlbuf) FreeMem(avlbuf, (long) (bufsize));
  275.     if (myfont != NULL) CloseFont(myfont);
  276.     if (Wind) CloseWindow(Wind) ;
  277.     if (MyScreen) CloseScreen(MyScreen) ;
  278.     if (IntuitionBase) CloseLibrary(IntuitionBase) ;
  279.     if (GfxBase) CloseLibrary(GfxBase) ;
  280.     if (DosBase) CloseLibrary(DosBase) ;
  281.     if (DiskfontBase) CloseLibrary(DiskfontBase) ;
  282.     exit(how) ;
  283.     }
  284.  
  285.  
  286.  
  287. /*
  288.  * clr_scr : clear the window area of the screen
  289.  *
  290.  */
  291.  
  292. clr_scr()
  293. {
  294.     SetAPen(rp,0L);
  295.     RectFill(rp,0L,SCREENPOSINIT,Wind->Width,Wind->Height);
  296.     SetAPen(rp,1L);
  297. }
  298.  
  299. _abort()
  300. {
  301.     done(24);
  302. }
  303.