home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 192.lha / Snip_v1.3 / gfx_txt.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  5KB  |  195 lines

  1.  
  2. #include "snip:defs.h"
  3.  
  4. typedef union bits32
  5. {
  6.     UBYTE bytes[8];
  7.     ULONG longword[2];
  8. }       long_byte;
  9.  
  10. extern char *charcode;
  11. extern short scratchheight, scratchwidth, scratchsize;
  12. extern short x_offset, y_offset;
  13. extern char *scratch;
  14. extern long_byte char_bitmap[];
  15.  
  16. /*
  17.  *  Given the (x,y) coordinate of the upper left of an 8x8 character position
  18.  *  within a window, this fills the 8-byte buffer with the bitmap of the
  19.  *  character at that position. The function after this one converts that
  20.  *  bitmap to the corresponding ASCII character.
  21.  *
  22.  */
  23.  
  24. get_byte(rp, xstart, ystart, buf)
  25. register struct RastPort *rp;
  26. register short xstart, ystart;
  27. register UBYTE buf[8];
  28. {
  29.     register short x,y;
  30.     register UBYTE tx_byte;
  31.  
  32.     for (y=0; y<8; y++)
  33.     {
  34.         tx_byte = 0;
  35.  
  36.         for (x=0; x<8; x++)
  37.         {
  38.             /* NB: I match _any_ colour here */
  39.  
  40.             if (ReadPixel(rp, xstart+x, ystart+y))
  41.             {
  42.                 tx_byte |= (1 << (7-x));
  43.             }
  44.         }
  45.  
  46.         buf[y] = tx_byte;
  47.     }
  48. }
  49.  
  50. /*
  51.  *  Given an 8x8 matrix of bits which has been read from a window, this
  52.  *  function compares it to the bit patterns of all known characters.
  53.  *
  54.  *  If it matches, the ASCII code of the character is returned, otherwise
  55.  *  the ASCII '?' is returned. All the usual ASCII characters are tested for,
  56.  *  so the function will only fail to find a match under these conditions:
  57.  *      - different font in use (you can use the -f option to get around this)
  58.  *      - cursor over letter (since all colours are tested for in a letter,
  59.  *        a cursor'ed letter is seen as a solid square)
  60.  *      - non-standard character like e with grave accent, (c) copyright
  61.  *        symbol, etc. I'm not sure if the bitmaps are different for different
  62.  *        keymaps, but I didn't consider these a vital issue anyway.
  63.  *      - characters are non-plain text, like italics or underline
  64.  *      - characters in inverse video, or any 2 non-background pens
  65.  *
  66.  */
  67.  
  68. UBYTE test_byte(bitmap)
  69. register ULONG bitmap[2];
  70. {
  71.     register short count;
  72.  
  73.     register long_byte *pattern = (long_byte *)char_bitmap;
  74.  
  75.     for (count = 0; count < 95; count++)    /* must remember to fix magic numbers like 95 */
  76.     {
  77.  
  78.         if ( bitmap[0] == pattern->longword[0] &&
  79.              bitmap[1] == pattern->longword[1]
  80.            )
  81.         {
  82.             return(charcode[count]);
  83.         }
  84.         else
  85.             pattern++;
  86.     }
  87.  
  88. /*     for (count=0; count<8; count++) */
  89. /*     printf("0x%x, ",bitmap[bytes[count]]); */
  90. /*     printf("\n"); */
  91.  
  92.     return('?');    /* unknown character (eg accented letter) */
  93. }
  94.  
  95. /*
  96.  *  This function will take a rectangular region of a window, and call the
  97.  *  preceding functions on each 8x8 square within it to create an array of
  98.  *  strings which can be supplied somehow to other programs.
  99.  *
  100.  *  I expect to use this mostly with terminal programs, when there is info
  101.  *  on the screen I want to record without having to write it down (UGH!)
  102.  *  or trying to call it up again and ASCII-capture it, then edit it, etc.
  103.  *
  104.  *  Also for some editors it may be handier than their own cut-and-paste
  105.  *  functions, especially those which don't support column operations
  106.  *  (ie almost all).
  107.  *
  108.  */
  109.  
  110. interpret(rp, x1, y1, x2, y2)
  111. register struct RastPort *rp;
  112. register short x1, y1, x2, y2;
  113. {
  114.     long_byte buf;
  115.     register short xstart, ystart = y1 + 1;
  116.     register short counter = 0;
  117.     UBYTE letter;
  118.  
  119.     scratchwidth = ((x2 - x1)+1) >> 3;    /* figure # chars horizontally */
  120.     scratchheight = ((y2 - y1)+1) >> 3;   /* figure number of characters vertically */
  121.  
  122.     scratchsize = (scratchwidth + 1) * scratchheight;
  123.  
  124.     scratch = AllocMem( scratchsize , NULL);
  125.  
  126.     if (NOT scratch)
  127.     {
  128.         Wr(Output(),"Out of memory!\n");
  129.         _abort();
  130.     }
  131.  
  132.     while (ystart < y2)
  133.     {
  134.         xstart = x1 + 1;
  135.  
  136.         while (xstart < x2)
  137.         {
  138.             get_byte(rp, xstart, ystart, &buf);
  139.             letter = test_byte(&buf);
  140.             scratch[counter++] = letter;
  141.             xstart += 8;
  142.         }
  143.  
  144.         scratch[counter++] = '\0';      /* create null-terminated lines */
  145.         ystart += 8;
  146.     }
  147.  
  148. }
  149.  
  150. /*
  151.  *  Given an (x,y) point in a window, calculate the point to the upper left
  152.  *  of the 8x8 character position it represents.
  153.  *
  154.  */
  155.  
  156. compute_xy1(window, x1, y1)
  157. register struct Window *window;
  158. register short *x1, *y1;
  159. {
  160.     *x1 = ((window->MouseX - ((window->MouseX - window->BorderLeft) % 8)) - 1) + x_offset;
  161.     *y1 = ((window->MouseY - ((window->MouseY - window->BorderTop) % 8)) - 1) + y_offset;
  162. }
  163.  
  164. /*
  165.  *  This works the same way, but calculates the point to the lower-right of
  166.  *  the 8x8 character square.
  167.  *
  168.  */
  169.  
  170. compute_xy2(window, x2, y2)
  171. register struct Window *window;
  172. register short *x2, *y2;
  173. {
  174.     compute_xy1(window, x2, y2);
  175.     *x2 += 9;
  176.     *y2 += 9;
  177. }
  178.  
  179. /*
  180.  *  Draw a box in complement mode in the target window.
  181.  *
  182.  */
  183.  
  184. do_box(rp, x1, y1, x2, y2)
  185. register struct RastPort *rp;
  186. register short x1, y1, x2, y2;
  187. {
  188.     Move(rp, x1, y1);
  189.     Draw(rp, x2, y1);
  190.     Draw(rp, x2, y2);
  191.     Draw(rp, x1, y2);
  192.     Draw(rp, x1, y1+1);
  193. }
  194.  
  195.