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

  1. ;/* MeasureText.c - Execute me to compile me with Lattice 5.10a
  2. LC -b0 -cfistq -v -y -j73 MeasureText.c
  3. Blink FROM LIB:c.o, MeasureText.o TO MeasureText LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5. **
  6. ** The following example, measuretext.c, opens a window on the default
  7. ** public screen and renders the contents of an ASCII file into the
  8. ** window.  It uses TextFit() to measure how much of a line of text will
  9. ** fit across the window.  If the entire line doesn't fit, measuretext
  10. ** will wrap the remainder of the line into the rows that follow.  This
  11. ** example makes use of an ASL font requester, letting the user choose
  12. ** the font, style, size, drawing mode, and color.
  13. */
  14. #define INTUITION_IOBSOLETE_H
  15. #include <dos/dos.h>
  16. #include <dos/dosextens.h>
  17. #include <graphics/text.h>
  18. #include <graphics/rastport.h>
  19. #include <intuition/intuition.h>
  20. #include <exec/libraries.h>
  21.  
  22. #include <clib/alib_stdio_protos.h>
  23. #include <clib/graphics_protos.h>
  24. #include <clib/intuition_protos.h>
  25. #include <clib/diskfont_protos.h>
  26. #include <clib/dos_protos.h>
  27. #include <clib/exec_protos.h>
  28. #include <clib/asl_protos.h>
  29.  
  30. #ifdef LATTICE
  31. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  32. int chkabort(void) { return(0); }
  33. #endif
  34.  
  35. #define BUFSIZE 32768
  36.  
  37. UBYTE *vers = "\0$VER: MeasureText 37.1";
  38.  
  39. UBYTE buffer[BUFSIZE];
  40.  
  41. void MainLoop(void);
  42. void EOP(void);
  43.  
  44. struct Library *IntuitionBase, *GfxBase, *DiskfontBase, *AslBase;
  45. BPTR myfile;
  46. UWORD wtbarheight;
  47. struct FontRequester *fr;
  48. struct TextFont *myfont;
  49. struct Window *w;
  50. struct RastPort *myrp;
  51. struct Task *mytask;
  52.  
  53. void main(int argc, char **argv)
  54. {
  55.   struct TextAttr myta;
  56.  
  57.   if (argc == 2)
  58.   {
  59.     if (myfile = Open(argv[1], MODE_OLDFILE))                 /* Open the file to print out. */
  60.     {
  61.       if (DiskfontBase = OpenLibrary("diskfont.library", 37L))        /* Open the libraries. */
  62.       {
  63.         if (IntuitionBase = OpenLibrary("intuition.library", 37L))
  64.         {
  65.           if (GfxBase = OpenLibrary("graphics.library", 37L))
  66.           {
  67.             if (AslBase = OpenLibrary("asl.library", 37L))
  68.             {
  69.               if (fr = (struct FontRequester *)                /* Open an ASL font requester */
  70.                        AllocAslRequestTags(ASL_FontRequest,
  71.                                     /* Supply initial values for requester */
  72.                                     ASL_FontName, (ULONG)"topaz.font",
  73.                                     ASL_FontHeight, 11L,
  74.                                     ASL_FontStyles, FSF_BOLD | FSF_ITALIC,
  75.                                     ASL_FrontPen,  0x01L,
  76.                                     ASL_BackPen,   0x00L,
  77.  
  78.                                      /* Give us all the gadgetry */
  79.                                     ASL_FuncFlags, FONF_FRONTCOLOR | FONF_BACKCOLOR |
  80.                                                    FONF_DRAWMODE | FONF_STYLES,
  81.                                     TAG_DONE))
  82.  
  83.  
  84.  
  85.  
  86.               {
  87.                 /* Pop up the requester */
  88.                 if (AslRequest(fr, 0L))
  89.                 {
  90.                   myta.ta_Name       = fr->fo_Attr.ta_Name;         /* extract the font and */
  91.                   myta.ta_YSize      = fr->fo_Attr.ta_YSize;        /* display attributes   */
  92.                   myta.ta_Style      = fr->fo_Attr.ta_Style;        /* from the FontRequest */
  93.                   myta.ta_Flags      = fr->fo_Attr.ta_Flags;        /* structure.           */
  94.  
  95.                   if (myfont = OpenDiskFont(&myta))
  96.                   {
  97.                     if (w = OpenWindowTags(NULL,WA_SizeGadget,  TRUE,
  98.                                                 WA_MinWidth,    200,
  99.                                                 WA_MinHeight,   200,
  100.                                                 WA_DragBar,     TRUE,
  101.                                                 WA_DepthGadget, TRUE,
  102.                                                 WA_Title,       (ULONG)argv[1],
  103.                                                 TAG_DONE))
  104.                     {
  105.                       myrp = w->RPort;
  106.                       /* figure out where the baseline of the uppermost line should be. */
  107.                       wtbarheight = w->WScreen->BarHeight + myfont->tf_Baseline + 2;
  108.  
  109.                       /* Set the font and add software styling to the text if I asked for it */
  110.                       /* in OpenFont() and didn't get it.  Because most Amiga fonts do not   */
  111.                       /* have styling built into them (with the exception of the CG outline  */
  112.                       /* fonts), if the user selected some kind of styling for the text, it  */
  113.                       /* will to be added algorithmically by calling SetSoftStyle().         */
  114.  
  115.                       SetFont(myrp, myfont);
  116.                       SetSoftStyle(myrp,   myta.ta_Style ^ myfont->tf_Style,
  117.                                     (FSF_BOLD | FSF_UNDERLINED | FSF_ITALIC));
  118.                       SetDrMd(myrp, fr->fo_DrawMode);
  119.                       SetAPen(myrp, fr->fo_FrontPen);
  120.                       SetBPen(myrp, fr->fo_BackPen);
  121.                       Move(myrp, w->WScreen->WBorLeft, wtbarheight);
  122.                       mytask = FindTask(NULL);
  123.  
  124.                       MainLoop();
  125.  
  126.                       Delay(25);                    /* short delay to give user a chance to */
  127.                       CloseWindow(w);               /* see the text before it goes away.    */
  128.                     }
  129.                     CloseFont(myfont);
  130.                   }
  131.                 }
  132.                 else
  133.                   VPrintf("Request Cancelled\n", NULL);
  134.                 FreeAslRequest(fr);
  135.               }
  136.               CloseLibrary(AslBase);
  137.             }
  138.             CloseLibrary(GfxBase);
  139.           }
  140.           CloseLibrary(IntuitionBase);
  141.         }
  142.         CloseLibrary(DiskfontBase);
  143.       }
  144.       Close(myfile);
  145.     }
  146.   }
  147.   else
  148.     VPrintf("template: MeasureText <file name>\n", NULL);
  149. }
  150.  
  151.  
  152. void MainLoop(void)
  153. {
  154.   struct TextExtent resulttextent;
  155.   LONG fit, actual, count, printable, crrts;
  156.   BOOL aok = TRUE;
  157.  
  158.   while (((actual = Read(myfile, buffer, BUFSIZE)) > 0) && aok)  /* while there's something to */
  159.   {                                                              /* read, fill the buffer.     */
  160.     count = 0;
  161.  
  162.  
  163.     while(count < actual)
  164.     {
  165.       crrts = 0;
  166.  
  167.       while ( ((buffer[count] < myfont->tf_LoChar) ||    /* skip non-printable characters, but */
  168.                (buffer[count] > myfont->tf_HiChar)) &&   /* account for newline characters.    */
  169.                (count < actual) )
  170.       {
  171.         if (buffer[count] == '\012') crrts++; /* is this character a newline?  if it is, bump */
  172.         count++;                               /* up the newline count.                        */
  173.       }
  174.  
  175.       if (crrts > 0)                  /* if there where any newlines, be sure to display them. */
  176.       {
  177.         Move(myrp, w->BorderLeft, myrp->cp_y + (crrts * (myfont->tf_YSize + 1)));
  178.         EOP();                                          /* did we go past the end of the page? */
  179.       }
  180.  
  181.       printable = count;
  182.       while ( (buffer[printable] >= myfont->tf_LoChar) &&      /* find the next non-printables */
  183.               (buffer[printable] <= myfont->tf_HiChar) &&
  184.               (printable < actual) )
  185.       {
  186.         printable++;
  187.       }                                  /* print the string of printable characters wrapping  */
  188.       while (count < printable)          /* lines to the beginning of the next line as needed. */
  189.       {
  190.         /* how many characters in the current string of printable characters will fit */
  191.         /* between the rastport's current X position and the edge of the window?      */
  192.         fit = TextFit(  myrp,                &(buffer[count]),
  193.                         (printable - count), &resulttextent,
  194.                         NULL,                1,
  195.                         (w->Width  - (myrp->cp_x + w->BorderLeft + w->BorderRight)),
  196.                         myfont->tf_YSize + 1  );
  197.         if ( fit == 0 )
  198.         {
  199.             /* nothing else fits on this line, need to wrap to the next line.         */
  200.             Move(myrp, w->BorderLeft, myrp->cp_y + myfont->tf_YSize + 1);
  201.         }
  202.         else
  203.         {
  204.            Text(myrp, &(buffer[count]), fit);
  205.            count += fit;
  206.         }
  207.         EOP();
  208.       }
  209.  
  210.       if (mytask->tc_SigRecvd & SIGBREAKF_CTRL_C)        /* did the user hit CTRL-C (the shell */
  211.       {                                                  /* window has to receive the CTRL-C)? */
  212.         aok = FALSE;
  213.         VPrintf("Ctrl-C Break\n", NULL);
  214.         count = BUFSIZE + 1;
  215.       }
  216.     }
  217.   }
  218.   if (actual < 0)
  219.     VPrintf("Error while reading\n", NULL);
  220. }
  221.  
  222.  
  223. void EOP(void)
  224. {
  225.     if (myrp->cp_y > (w->Height - (w->BorderBottom + 2))) /* If we reached page bottom, clear the */
  226.     {                                                     /* rastport and move back to the top.   */
  227.         Delay(25);
  228.  
  229.         SetAPen(myrp, 0);
  230.         RectFill(myrp, (LONG)w->BorderLeft, (LONG)w->BorderTop, w->Width - (w->BorderRight + 1),
  231.                  w->Height - (w->BorderBottom + 1) );
  232.         SetAPen(myrp, 1);
  233.         Move(myrp, w->BorderLeft + 1, wtbarheight);
  234.         SetAPen(myrp, fr->fo_FrontPen);
  235.     }
  236. }
  237.