home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 199.lha / GimmeLib / postext.c < prev    next >
C/C++ Source or Header  |  1988-12-27  |  5KB  |  208 lines

  1. /*
  2.  *  FILE: postext.c
  3.  *  Support routines for writing text (or a number) in a rastport,
  4.  *  with flexible text location specification (eg any corner, centre).
  5.  *
  6.  *  Public Domain, but keep my name in it as the original author.
  7.  *  31-Aug-88    Jan Sven Trabandt   first release version
  8.  */
  9.  
  10.  
  11. #define I_AM_POSTEXT
  12. #include "gimmelib/gimmefuncs.h"
  13. #include "gimmelib/postext.h"
  14.  
  15.  
  16. SHORT positionText( rp, myflags, s, num, x, y )
  17.     register struct RastPort    *rp;
  18.     register ULONG        myflags;
  19.     UBYTE   *s;
  20.     LONG    num;
  21.     SHORT   x, y;
  22. {
  23.     register SHORT  i, j;
  24.     SHORT        len, rastlen, maxrastlen;
  25.     SHORT        maxbit, lowi;
  26.     SHORT        dest, temp;
  27.     SHORT        height, baseline;
  28.     struct RastPort myrp;
  29.     UBYTE        buf[12];        /* big enough for max ULONG + '\0' */
  30.  
  31.  
  32. #ifdef GIMME_WIMPY
  33.     if( !rp ) {
  34.     return( -1 );
  35.     }
  36. #endif
  37.     if( s ) {
  38.     len = strlen( s );
  39.     } else {
  40.     len = sprintf( buf, "%1ld", num );
  41.     s = buf;
  42.     }
  43.     maxrastlen = len * (rp->TxWidth + rp->TxSpacing);
  44.     rastlen = TextLength( rp, s, (long) len );
  45.  
  46.     if( myflags & (GPT_YUPWARDS | GPT_YDOWNWARDS) ) {
  47.     myrp = *rp;            /* copy struct */
  48.     myrp.BitMap = gimmeBitMap( rp->BitMap->Depth, rastlen, myrp.TxHeight );
  49.     if( !myrp.BitMap ) {
  50.         return( -1 );
  51.     }
  52.     myrp.Layer = NULL;        /* make sure no layer else trouble */
  53.     myrp.GelsInfo = NULL;
  54.     Move( &myrp, 0L, (long) myrp.TxBaseline );
  55.     Text( &myrp, s, (long) len );
  56.  
  57.     maxbit = rastlen - 1;
  58.     switch( myflags & GPT_XFLAGS ) {
  59.       case GPT_XRIGHT:
  60.         if( myflags & GPT_YDOWNWARDS ) {
  61.         y -= maxbit;
  62.         }
  63.         break;
  64.       case GPT_XCENTRE:
  65.         y -= rastlen >> 1;
  66.         break;
  67.       case GPT_XLEFT:
  68.       default:
  69.         if( myflags & GPT_YUPWARDS ) {
  70.         y -= maxbit;
  71.         }
  72.         break;
  73.     } /* switch */
  74.  
  75.     if( myflags & GPT_XTHICKEN ) {              /* if double thickness */
  76.         height = (myrp.TxHeight << 1) - 1;
  77.         baseline = (myrp.TxBaseline << 1);
  78.     } else {
  79.         height = myrp.TxHeight - 1;
  80.         baseline = myrp.TxBaseline;
  81.     }
  82.  
  83.     switch( myflags & GPT_YFLAGS ) {        /* make x top of char */
  84.       case GPT_YBOTTOM:
  85.         if( myflags & GPT_YUPWARDS ) {
  86.         x -= height;
  87.         }
  88.         break;
  89.       case GPT_YCENTRE:            /* skew to top of char */
  90.         if( myflags & GPT_YDOWNWARDS ) {
  91.         /* x += (height >> 1) - height; */
  92.         x -= (height + 1) >> 1;
  93.         } else {
  94.         x -= height >> 1;
  95.         }
  96.         break;
  97.       case GPT_YCENTREBASE:         /* skew to bottom of char */
  98.         if( myflags & GPT_YDOWNWARDS ) {
  99.         x += ((baseline + 1) >> 1) - height;
  100.         } else {
  101.         x -= (baseline + 1) >> 1;
  102.         }
  103.         break;
  104.       case GPT_YTOP:
  105.         if( myflags & GPT_YDOWNWARDS ) {
  106.         x -= height;
  107.         }
  108.         break;
  109.       case GPT_YBASELINE:
  110.       default:
  111.         if( myflags & GPT_YDOWNWARDS ) {
  112.         x += baseline - height;
  113.         } else {
  114.         x -= baseline;
  115.         }
  116.         break;
  117.     } /* switch */
  118.  
  119.     if( y < 0 ) {
  120.         rastlen += y;
  121.         y = 0;
  122.     }
  123.     lowi = rastlen - rp->BitMap->Rows;
  124.     if( lowi > 0 ) {
  125.         rastlen -= lowi;
  126.     } else {
  127.         lowi = 0;
  128.     }
  129.     maxbit = rastlen - 1;
  130.     if( myflags & GPT_YDOWNWARDS ) {
  131.         if( lowi ) {
  132.         maxbit -= lowi;
  133.         lowi = 0;
  134.         }
  135.         temp = x + height;
  136.         for( j = myrp.TxHeight - 1; j >= 0; --j ) {
  137.         if( GPT_XTHICKEN ) {
  138.             dest = temp - (j << 1) - 1;
  139.         } else {
  140.             dest = temp - j;
  141.         }
  142.         for( i = maxbit; i >= lowi; --i ) {
  143.             /* if no error and not background colour */
  144.             if( ReadPixel(&myrp, (long) i, (long) j) > 0L ) {
  145.             WritePixel( rp, (long) dest, (long) y + i );
  146.             if( myflags & GPT_XTHICKEN ) {
  147.                 WritePixel( rp, (long) dest + 1, (long) y + i );
  148.             }
  149.             }
  150.         } /* for */
  151.         } /* for */
  152.     } else {        /* else upwards */
  153.         for( i = maxbit; i >= lowi; --i ) {
  154.         dest = y + maxbit - i;
  155.         for( j = myrp.TxHeight - 1; j >= 0; --j ) {
  156.             /* if no error and not background colour */
  157.             if( ReadPixel(&myrp, (long) i, (long) j) > 0L ) {
  158.             if( myflags & GPT_XTHICKEN ) {
  159.                 temp = j << 1;
  160.                 WritePixel( rp, (long) x + temp + 1, (long) dest );
  161.                 WritePixel( rp, (long) x + temp, (long) dest );
  162.             } else {
  163.                 WritePixel( rp, (long) x + j, (long) dest );
  164.             }
  165.             }
  166.         } /* for */
  167.         } /* for */
  168.     }
  169.     getRidOfBitMap( myrp.BitMap );
  170.     } else {
  171.     switch( myflags & GPT_XFLAGS ) {
  172.       case GPT_XRIGHT:
  173.         x -= rastlen - 1;
  174.         break;
  175.       case GPT_XCENTRE:
  176.         x -= rastlen >> 1;
  177.         break;
  178.       case GPT_XLEFT:
  179.       default:
  180.         break;
  181.     } /* switch */
  182.  
  183.     switch( myflags & GPT_YFLAGS ) {        /* make y top of char */
  184.       case GPT_YBOTTOM:
  185.         y -= rp->TxHeight - 1;
  186.         break;
  187.       case GPT_YCENTRE:
  188.         y -= (rp->TxHeight - 1) >> 1;       /* skew to top of char */
  189.         break;
  190.       case GPT_YCENTREBASE:
  191.         y -= (rp->TxBaseline + 1) >> 1;     /* skew to bottom of char */
  192.         break;
  193.       case GPT_YTOP:
  194.         break;
  195.       case GPT_YBASELINE:
  196.       default:
  197.         y -= rp->TxBaseline;
  198.         break;
  199.     } /* switch */
  200.     y += rp->TxBaseline;        /* adjust top to baseline for Text() */
  201.     /* note rp's TxBaseline is offset from top pixel of character */
  202.  
  203.     Move( rp, (long) x, (long) y );
  204.     Text( rp, s, (long) len );
  205.     }
  206.     return( maxrastlen );
  207. } /* positionText */
  208.