home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / AvailFonts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  13.3 KB  |  330 lines

  1. ;/* AvailFonts.c - Execute me to compile me with Lattice 5.10a
  2. LC -cfistq -v -y -j73 AvailFonts.c
  3. Blink FROM LIB:c.o,AvailFonts.o TO AvailFonts LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;*/
  5.  
  6. #include <exec/types.h>
  7. #include <dos/rdargs.h>
  8. #include <dos/dosextens.h>
  9. #include <intuition/intuition.h>
  10. #include <intuition/screens.h>
  11. #include <graphics/text.h>
  12. #include <graphics/displayinfo.h>
  13. #include <graphics/regions.h>
  14. #include <graphics/gfx.h>
  15. #include <libraries/diskfont.h>
  16. #include <utility/tagitem.h>
  17. #include <clib/exec_protos.h>
  18. #include <clib/dos_protos.h>
  19. #include <clib/layers_protos.h>
  20. #include <clib/alib_stdio_protos.h>
  21. #include <clib/intuition_protos.h>
  22. #include <clib/graphics_protos.h>
  23. #include <clib/diskfont_protos.h>
  24. #include <clib/utility_protos.h>
  25.  
  26. #ifdef LATTICE
  27. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  28. int chkabort(void) { return(0); }
  29. #endif
  30.  
  31. UBYTE *vers = "\0$VER: AvailFonts 36.3";
  32.  
  33. void MainLoop(void);
  34. ULONG StrLen(UBYTE *);
  35.  
  36. struct stringstruct {
  37.         UBYTE *string;
  38.         LONG charcount;
  39.         WORD stringwidth;
  40. };
  41.  
  42. UBYTE *alphabetstring = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  43. struct stringstruct fname, fheight, XDPI, YDPI, entrynum;
  44. struct Library *DiskfontBase, *IntuitionBase, *LayersBase, *GfxBase, *UtilityBase;
  45. struct Window *mywin;
  46. struct RastPort *mycliprp, myrp;
  47. struct Rectangle myrect;
  48. struct Region *new_region, *old_region;
  49. struct DrawInfo *mydrawinfo;
  50. struct AvailFontsHeader *afh;
  51. LONG fontheight, alphabetcharcount;
  52. WORD stringwidth;
  53.  
  54. void main(int argc, char **argv)
  55. {
  56.   struct TextFont *defaultfont = NULL;
  57.   struct TextAttr defaultfontattr = { "topaz.font", 9, 0, 0 };
  58.  
  59.   LONG afsize, afshortage, cliprectside;
  60.  
  61.   fname.string = "Font Name:  ";
  62.   fheight.string = "Font Height:  ";
  63.   XDPI.string = "X DPI:  ";
  64.   YDPI.string = "Y DPI:  ";
  65.   entrynum.string = "Entry #:  ";
  66.  
  67.   if (DiskfontBase = OpenLibrary("diskfont.library", 37L))              /* Open the libraries. */
  68.   {
  69.     if (IntuitionBase = OpenLibrary("intuition.library", 37L))
  70.     {
  71.       if (GfxBase = OpenLibrary("graphics.library", 37L))
  72.       {
  73.         if (LayersBase = OpenLibrary("layers.library", 37L))
  74.         {
  75.           if (UtilityBase = OpenLibrary("utility.library", 37L))
  76.           {
  77.             if (mywin = OpenWindowTags(NULL,                              /* Open that window. */
  78.                                     WA_SmartRefresh,TRUE,
  79.                                     WA_SizeGadget,  FALSE,
  80.                                     WA_CloseGadget, TRUE,
  81.                                     WA_IDCMP,       IDCMP_CLOSEWINDOW,
  82.                                     WA_DragBar,     TRUE,
  83.                                     WA_DepthGadget, TRUE,
  84.                                     WA_Title,       (ULONG)"AvailFonts() example",
  85.                                     TAG_END))
  86.             {
  87.               myrp = *(mywin->RPort);       /* A structure assign: clone my window's Rastport. */
  88.                                             /* RastPort.  This RastPort will be used to render */
  89.                                             /* the font specs, not the actual font sample.     */
  90.               if (mydrawinfo = GetScreenDrawInfo(mywin->WScreen))
  91.               {
  92.                 SetFont(&myrp, mydrawinfo->dri_Font);
  93.  
  94.                 myrect.MinX = mywin->BorderLeft;                          /* LAYOUT THE WINDOW */
  95.                 myrect.MinY = mywin->BorderTop;
  96.                 myrect.MaxX = mywin->Width - (mywin->BorderRight + 1);
  97.                 myrect.MaxY = mywin->Height - (mywin->BorderBottom + 1);
  98.  
  99.                 cliprectside = (myrect.MaxX - myrect.MinX) / 20;
  100.  
  101.                 fname.charcount    = StrLen(fname.string);
  102.                 fheight.charcount  = StrLen(fheight.string);
  103.                 XDPI.charcount     = StrLen(XDPI.string);
  104.                 YDPI.charcount     = StrLen(YDPI.string);
  105.                 entrynum.charcount = StrLen(entrynum.string);
  106.                 alphabetcharcount  = StrLen(alphabetstring);
  107.  
  108.                 fontheight = (myrp.Font->tf_YSize) + 2;
  109.  
  110.                 if (fontheight > ((myrect.MaxY - myrect.MinY) / 6))  /* If the default screen  */
  111.                 {                                                    /* font is more than one- */
  112.                   defaultfont = OpenFont(&defaultfontattr);          /* sixth the size of the  */
  113.                   SetFont(&myrp, defaultfont);                       /* window, use topaz-9.   */
  114.                   fontheight = (myrp.Font->tf_YSize) + 2;
  115.                 }
  116.  
  117.                 fname.stringwidth   = TextLength(&myrp, (STRPTR)fname.string, fname.charcount);
  118.                 fheight.stringwidth = TextLength(&myrp, (STRPTR)fheight.string, fheight.charcount);
  119.                 XDPI.stringwidth    = TextLength(&myrp, (STRPTR)XDPI.string, XDPI.charcount);
  120.                 YDPI.stringwidth    = TextLength(&myrp, (STRPTR)YDPI.string, YDPI.charcount);
  121.                 entrynum.stringwidth =
  122.                     TextLength(&myrp, (STRPTR)entrynum.string, entrynum.charcount);
  123.  
  124.                 stringwidth = fname.stringwidth;         /* What is the largest string length? */
  125.                 stringwidth =
  126.                     (fheight.stringwidth > stringwidth) ? fheight.stringwidth : stringwidth;
  127.                 stringwidth = (XDPI.stringwidth > stringwidth) ? XDPI.stringwidth : stringwidth;
  128.                 stringwidth = (YDPI.stringwidth > stringwidth) ? YDPI.stringwidth : stringwidth;
  129.                 stringwidth =
  130.                     (entrynum.stringwidth > stringwidth) ? entrynum.stringwidth : stringwidth;
  131.                 stringwidth += mywin->BorderLeft;
  132.  
  133.                 if (stringwidth < ((myrect.MaxX - myrect.MinX) >> 1)) /* If the stringwidth is */
  134.                 {                                                 /* more than half the viewing*/
  135.                   SetAPen(&myrp, mydrawinfo->dri_Pens[TEXTPEN]);  /* area, quit because the    */
  136.                   SetDrMd(&myrp, JAM2);                           /* font is just too big.     */
  137.  
  138.                   Move(&myrp, myrect.MinX + 8 + stringwidth - fname.stringwidth,
  139.                                myrect.MinY + 4 + (myrp.Font->tf_Baseline));
  140.                   Text(&myrp, fname.string, fname.charcount);
  141.  
  142.                   Move(&myrp, myrect.MinX + 8 + stringwidth - fheight.stringwidth,
  143.                               myrp.cp_y + fontheight);
  144.                   Text(&myrp, fheight.string, fheight.charcount);
  145.  
  146.                   Move(&myrp, myrect.MinX + 8 + stringwidth - XDPI.stringwidth,
  147.                               myrp.cp_y + fontheight);
  148.                   Text(&myrp, XDPI.string, XDPI.charcount);
  149.  
  150.                   Move(&myrp, myrect.MinX + 8 + stringwidth - YDPI.stringwidth,
  151.                               myrp.cp_y + fontheight);
  152.                   Text(&myrp, YDPI.string, YDPI.charcount);
  153.  
  154.                   Move(&myrp, myrect.MinX + 8 + stringwidth - entrynum.stringwidth,
  155.                               myrp.cp_y + fontheight);
  156.                   Text(&myrp, entrynum.string, entrynum.charcount);
  157.  
  158.                   myrect.MinX = myrect.MinX + cliprectside;
  159.                   myrect.MaxX = myrect.MaxX - cliprectside;
  160.                   myrect.MinY = myrect.MinY + (5 * fontheight) + 8;
  161.                   myrect.MaxY = myrect.MaxY - 8;
  162.  
  163.                   SetAPen(&myrp, mydrawinfo->dri_Pens[SHINEPEN]);         /* Draw a box around */
  164.                   Move(&myrp, myrect.MinX - 1, myrect.MaxY + 1);          /* the cliprect.     */
  165.                   Draw(&myrp, myrect.MaxX + 1, myrect.MaxY + 1);
  166.                   Draw(&myrp, myrect.MaxX + 1, myrect.MinY - 1);
  167.  
  168.                   SetAPen(&myrp, mydrawinfo->dri_Pens[SHADOWPEN]);
  169.                   Draw(&myrp, myrect.MinX - 1, myrect.MinY - 1);
  170.                   Draw(&myrp, myrect.MinX - 1, myrect.MaxY);
  171.  
  172.                   SetAPen(&myrp, mydrawinfo->dri_Pens[TEXTPEN]);
  173.                                           /* Fill up a buffer with a list of the available fonts */
  174.                   afsize = AvailFonts((STRPTR)afh, 0L, AFF_MEMORY|AFF_DISK|AFF_SCALED|AFF_TAGGED);
  175.                   do
  176.                   {
  177.                     afh = (struct AvailFontsHeader *) AllocMem(afsize, 0);
  178.                     if (afh)
  179.                     {
  180.                       afshortage = AvailFonts((STRPTR)afh, afsize,
  181.                                               AFF_MEMORY|AFF_DISK|AFF_SCALED|AFF_TAGGED);
  182.                       if (afshortage)
  183.                       {
  184.                         FreeMem(afh, afsize);
  185.                         afsize += afshortage;
  186.                         afh = (struct AvailFontsHeader *)(-1L);
  187.                       }
  188.                     }
  189.                   } while (afshortage && afh);
  190.  
  191.                   if (afh)
  192.                   {
  193.                     /* This is for the layers.library clipping region that gets attached to */
  194.                     /* the window.  This prevents the application from unnecessarily        */
  195.                     /* rendering beyond the bounds of the inner part of the window. For     */
  196.                     /* more information on clipping, see the Layers chapter of this manual. */
  197.  
  198.                     if (new_region = NewRegion())                 /* More layers stuff */
  199.                     {
  200.                       if (OrRectRegion(new_region, &myrect));/* Even more layers stuff */
  201.                       {
  202.                          /* Obtain a pointer to the window's rastport and set up some of    */
  203.                          /* the rastport attributes.  This example obtains the text pen     */
  204.                          /* for the window's screen using the GetScreenDrawInfo() function. */
  205.                          mycliprp = mywin->RPort;
  206.                          SetAPen(mycliprp, mydrawinfo->dri_Pens[TEXTPEN]);
  207.  
  208.                          MainLoop();
  209.                       }
  210.                       DisposeRegion(new_region);
  211.                     }
  212.                     FreeMem(afh, afsize);
  213.                   }
  214.                 }
  215.                 FreeScreenDrawInfo(mywin->WScreen, mydrawinfo);
  216.               }
  217.               CloseWindow(mywin);
  218.             }
  219.             CloseLibrary(UtilityBase);
  220.           }
  221.           CloseLibrary(LayersBase);
  222.         }
  223.         CloseLibrary(GfxBase);
  224.       }
  225.       CloseLibrary(IntuitionBase);
  226.     }
  227.     CloseLibrary(DiskfontBase);
  228.   }
  229. }
  230.  
  231.  
  232.  
  233. void MainLoop(void)
  234. {
  235.   UWORD x;
  236.   struct Task *mytask;
  237.   struct IntuiMessage *mymsg;
  238.   BOOL aok = TRUE;
  239.   struct TAvailFonts *afont;
  240.   struct TextFont *myfont;
  241.   UBYTE buf[8];
  242.   ULONG dpi;
  243.  
  244.   mytask = FindTask(NULL);
  245.   afont = (struct TAvailFonts *)&(afh[1]);
  246.  
  247.   for (x = 0; (x < afh->afh_NumEntries); x++)
  248.   {
  249.     if (aok)
  250.     {
  251.       if (myfont = OpenDiskFont(&(afont->taf_Attr)))
  252.       {
  253.         SetAPen(&myrp, mydrawinfo->dri_Pens[BACKGROUNDPEN]);   /* Print the TextFont attributes. */
  254.         RectFill( &myrp, stringwidth, mywin->BorderTop + 4,
  255.                   mywin->Width - (mywin->BorderRight + 1), myrect.MinY - 2 );
  256.  
  257.         SetAPen(&myrp, mydrawinfo->dri_Pens[TEXTPEN]);
  258.         Move( &myrp, stringwidth + mywin->BorderLeft,
  259.               mywin->BorderTop + 4 + (myrp.Font->tf_Baseline) );
  260.         Text( &myrp, (UBYTE *)myfont->tf_Message.mn_Node.ln_Name,
  261.               StrLen((UBYTE *)myfont->tf_Message.mn_Node.ln_Name) );
  262.  
  263.         Move(&myrp, stringwidth + mywin->BorderLeft, myrp.cp_y + fontheight); /* Print the      */
  264.         sprintf(buf, "%d\0", myfont->tf_YSize);                               /* font's Y Size. */
  265.         Text(&myrp, buf, StrLen(buf));
  266.  
  267.         Move(&myrp, stringwidth + mywin->BorderLeft, myrp.cp_y + fontheight); /* Print the X DPI */
  268.         dpi = GetTagData( TA_DeviceDPI, 0L,
  269.                           ((struct TextFontExtension *)(myfont->tf_Extension))->tfe_Tags);
  270.         if (dpi)
  271.         {
  272.             sprintf(buf, "%d\0", ((dpi & 0xFFFF0000)>>16));
  273.             Text(&myrp, buf, StrLen(buf));
  274.         }
  275.         else Text(&myrp, "nil", 3L);
  276.  
  277.         Move(&myrp, stringwidth + mywin->BorderLeft, myrp.cp_y + fontheight); /* Print the Y DPI */
  278.         if (dpi)
  279.         {
  280.             sprintf(buf, "%d\0", (dpi & 0x0000FFFF));
  281.             Text(&myrp, buf, StrLen(buf));
  282.         }
  283.         else Text(&myrp, "nil", 3L);
  284.  
  285.         Move(&myrp, stringwidth + mywin->BorderLeft, myrp.cp_y + fontheight);     /* Print the */
  286.         sprintf(buf, "%d\0", x);                                                   /* entrynum. */
  287.         Text(&myrp, buf, StrLen(buf));
  288.  
  289.         SetFont(mycliprp, myfont);
  290.         old_region = InstallClipRegion(mywin->WLayer, new_region); /* Install clipping rectangle */
  291.  
  292.         SetRast(mycliprp, mydrawinfo->dri_Pens[BACKGROUNDPEN]);
  293.         Move( mycliprp, myrect.MinX, myrect.MaxY - (myfont->tf_YSize - myfont->tf_Baseline) );
  294.         Text(mycliprp, alphabetstring, alphabetcharcount);
  295.  
  296.         Delay(100);
  297.  
  298.         new_region = InstallClipRegion(mywin->WLayer, old_region);  /* Remove clipping rectangle */
  299.  
  300.         while (mymsg = (struct IntuiMessage *)GetMsg(mywin->UserPort))
  301.         {
  302.             aok = FALSE;
  303.             x = afh->afh_NumEntries;
  304.             ReplyMsg((struct Message *)mymsg);
  305.         }
  306.  
  307.  
  308.  
  309.         if (mytask->tc_SigRecvd & SIGBREAKF_CTRL_C)        /* Did the user hit CTRL-C (the shell */
  310.         {                                                  /* window has to receive the CTRL-C)? */
  311.             aok = FALSE;
  312.             x = afh->afh_NumEntries;
  313.             VPrintf("Ctrl-C Break\n", NULL);
  314.         }
  315.         CloseFont(myfont);
  316.       }
  317.     }
  318.     afont++;
  319.   }
  320. }
  321.  
  322.  
  323. ULONG StrLen(UBYTE *string)
  324. {
  325.     ULONG x = 0L;
  326.  
  327.     while (string[x++]);
  328.     return(--x);
  329. }
  330.