home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / v-23 / view.c < prev   
C/C++ Source or Header  |  1996-01-30  |  10KB  |  184 lines

  1. ;/* View.c - Execute me to compile me with SAS/C 6.56
  2. sc NMINC STRMERGE STREQ NOSTKCHK SAVEDS IGNORE=73 View.c
  3. slink FROM LIB:c.o,BulletMainFile.o,engine.o,View.o TO View LIBRARY LIB:sc.lib,LIB:amiga.lib
  4. quit ;*/
  5.  
  6. /* (c)  Copyright 1992 Commodore-Amiga, Inc.   All rights reserved.       */
  7. /* The information contained herein is subject to change without notice,  */
  8. /* and is provided "as is" without warranty of any kind, either expressed */
  9. /* or implied.  The entire risk as to the use of this information is      */
  10. /* assumed by the user.                                                   */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <diskfont/diskfonttag.h>
  15. #include <diskfont/diskfont.h>
  16. #include <diskfont/glyph.h>
  17. #include <diskfont/oterrors.h>
  18. #include <graphics/gfx.h>
  19. #include <utility/tagitem.h>
  20. #include <intuition/intuition.h>
  21. #include <devices/timer.h>
  22. #include <dos/dos.h>
  23.  
  24. #include <clib/alib_stdio_protos.h>
  25. #include <clib/alib_protos.h>
  26. #include <clib/bullet_protos.h>
  27. #include <clib/exec_protos.h>
  28. #include <clib/dos_protos.h>
  29. #include <clib/graphics_protos.h>
  30. #include <clib/intuition_protos.h>
  31.  
  32. UBYTE          *vers = "\0$VER: View 38.7";
  33.  
  34. extern struct Library *BulletBase, *UtilityBase, *GfxBase, *IntuitionBase;
  35. void            BulletExample(struct GlyphEngine *,
  36.                               struct Window *,
  37.                               struct RastPort *,
  38.                               ULONG, ULONG, ULONG, STRPTR);
  39. struct GlyphMap *gm;
  40. PLANEPTR        tempbitmap;
  41. struct IntuiMessage *mymsg;
  42. UBYTE          *viewfilebuf, *currchar;
  43. ULONG           currposition, emheight, emwidth, x, y;
  44.  
  45. BPTR            viewfile;
  46. LONG            actual;
  47. struct Task    *mytask;
  48. struct FileInfoBlock *fib;
  49.  
  50. void
  51. BulletExample(struct GlyphEngine * ge,
  52.               struct Window * w, struct RastPort * rp,
  53.               ULONG pointheight, ULONG xdpi, ULONG ydpi, STRPTR viewfilename)
  54. {
  55.   UWORD           wlimitx, wlimity, newwidth;
  56.   FIXED           kern;
  57.  
  58.   wlimitx = w->Width  - w->BorderRight  - 2; /* The X and Y extent of the window */
  59.   wlimity = w->Height - w->BorderBottom - 2; /* that we can draw into.           */
  60.  
  61.   if (SetInfo(ge,                                      /* Set up the X and Y DPI of */
  62.               OT_DeviceDPI, ((ULONG)xdpi) << 16 | ydpi,/* target raster.  Neither   */
  63.               OT_PointHeight, (ULONG)pointheight << 16,/* of these can be zero!     */
  64.               TAG_END) != OTERR_Success)               /* BulletMainFile.c checks   */
  65.     return;                                            /* for zero.                 */
  66.  
  67.   if (viewfile = Open(viewfilename, MODE_OLDFILE))/* Open the ASCII file to display.*/
  68.   {
  69.     if (fib = AllocDosObject(DOS_FIB, NULL))      /* Find out how big the display   */
  70.     {                                             /* file is by looking at its      */
  71.       if (ExamineFH(viewfile, fib))               /* FileInfoBlock.  Allocate that  */
  72.       {                                           /* Much memory.                   */
  73.         if (viewfilebuf = (UBYTE *) AllocVec(fib->fib_Size, MEMF_CLEAR))
  74.         {
  75.           if (Read(viewfile, (UBYTE *) viewfilebuf, fib->fib_Size))    /* Read the  */
  76.           {                                          /* whole file into its buffer. */
  77.             SetDrMd(w->RPort, JAM1);
  78.             if (tempbitmap = AllocRaster(640, 200)) /* Allocate some Chip RAM space */
  79.             {      /* where we can temporarily store the glyph so we can blit it.   */
  80.               if (ModifyIDCMP(w, IDCMP_CLOSEWINDOW))  /* Turn on the Close gadget.  */
  81.               {
  82.                 emheight = (pointheight * ydpi) / 72; /* Calculate the dimensions   */
  83.                 emwidth = (pointheight * xdpi) / 72;  /* of the EM square in screen */
  84.                      /* pixels. This is necessary because bullet.library measures   */
  85.                      /* character widths and kerning values as fractions of an EM.  */
  86.                      /* An EM (pronounced like the letter 'M') is a measure of      */
  87.                      /* distance that is equal to the point size of a typeface      */
  88.                      /* (which means one EM is not constant across different type   */
  89.                      /* sizes).  For a 72 point typeface, one EM = 72 points which  */
  90.                      /* approximately equals one inch.                              */
  91.  
  92.                 x = w->BorderLeft + 2;           /* Calculate the starting point    */
  93.                 y = w->BorderTop + 2 + emheight; /* for glyph rendering.            */
  94.  
  95.                                       /* Step through each character in the buffer. */
  96.                 for (currposition = 0; currposition < fib->fib_Size; currposition++)
  97.                 {
  98.                                 /* Set the current glyph, which is the one we'll be */
  99.                                 /* rendering in this interation of the loop, and    */
  100.                                 /* the secondary glyph, which, besides being the    */
  101.                                 /* next glyph we will render, is necessary to find  */
  102.                                 /* the proper kerning value between the glyphs.     */
  103.                                 /* Notice that this example does not account for    */
  104.                                 /* the presence of non-printables (carriage return, */
  105.                                 /* DEL, etc.) which effects the kerning.  A real    */
  106.                   if (SetInfo(ge,  /* application should consider these.            */
  107.                               OT_GlyphCode,  (ULONG) viewfilebuf[currposition],
  108.                               OT_GlyphCode2, (ULONG) viewfilebuf[currposition + 1],
  109.                               TAG_END) == OTERR_Success)
  110.                   {
  111.                     kern = 0;         /* Find the kerning adjustment between glyph1 */
  112.                                       /* and glyph2.  This example doesn't account  */
  113.                                       /* for the validity of the glyphs.            */
  114.                     ObtainInfo(ge, OT_TextKernPair, &kern, TAG_END);
  115.  
  116.                                                   /* Ask the scaling engine for the */
  117.                                                   /* bitmap for the current glyph.  */
  118.                     if ((ObtainInfo(ge, OT_GlyphMap, &gm, TAG_END)) == OTERR_Success)
  119.                     {
  120.                        /* Calculate the width of the current character including    */
  121.                        /* any kerning adjustment.  Because the width is represented */
  122.                        /* as a fixed point binary fraction of an EM, this needs to  */
  123.                        /* be converted to a width in screen pixels.                 */
  124.                       newwidth = ((gm->glm_Width - kern) * emwidth) / 65536;
  125.  
  126.  
  127.                       if ((x + newwidth) > wlimitx)  /* Make sure the glyph gets    */
  128.                       {                              /* rendered inside the window  */
  129.                                                      /* bounds.                     */
  130.                         x = w->BorderLeft + 2;
  131.                         y += emheight;
  132.                         if (y > wlimity)   /* If the text goes beyond the bottom of */
  133.                         {                  /* the window, clear the window and move */
  134.                                            /* the current rendering position to the */
  135.                                            /* upper left.                           */
  136.                           y = w->BorderTop + 2 + emheight;
  137.                           RectFill(rp, w->BorderLeft, w->BorderTop, wlimitx, wlimity);
  138.                         }
  139.                       }
  140.  
  141.                       CopyMem(gm->glm_BitMap,/* Copy the raw bitmap to chip memory. */
  142.                               tempbitmap,
  143.                               gm->glm_BMModulo * gm->glm_BMRows);
  144.  
  145.                       BltTemplate(           /* Render the glyph using the blitter  */
  146.                                              /* and the RastPort settings.          */
  147.                                    (PLANEPTR) (((ULONG) tempbitmap)
  148.                                      + (gm->glm_BMModulo * gm->glm_BlackTop)
  149.                                          + ((gm->glm_BlackLeft >> 4) << 1)),
  150.                                    gm->glm_BlackLeft & 0xF,
  151.                                    gm->glm_BMModulo,
  152.                                    w->RPort,
  153.                                    x - gm->glm_X0 + gm->glm_BlackLeft,
  154.                                    y - gm->glm_Y0 + gm->glm_BlackTop,
  155.                                    gm->glm_BlackWidth,     /* glm_X0 & Y0 are used  */
  156.                                    gm->glm_BlackHeight);   /* to make the example a */
  157.                                     /* little simpler.  They are not as accurate as */
  158.                                     /* using glm_XOrigin and glm_YOrigin in con-    */
  159.                                     /* juntion with fractional width and kerning    */
  160.                                     /* values.                                      */
  161.  
  162.                       x += newwidth;
  163.                       ReleaseInfo(ge, OT_GlyphMap, gm, TAG_END);
  164.                     }
  165.                     while (mymsg = (struct IntuiMessage *) GetMsg(w->UserPort))
  166.                     {                                       /* Did the user hit the */
  167.                       ReplyMsg((struct Message *) mymsg);   /* Close Gadget?        */
  168.                       currposition = fib->fib_Size + 1;
  169.                     }
  170.                   }
  171.                 }
  172.               }
  173.               FreeRaster(tempbitmap, 640, 200);
  174.             }
  175.           }
  176.           FreeVec(viewfilebuf);
  177.         }
  178.       }
  179.       FreeDosObject(DOS_FIB, fib);
  180.     }
  181.     Close(viewfile);
  182.   }
  183. }
  184.