home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Anti-Aliased Text / Anti-Aliased Text.c next >
Encoding:
C/C++ Source or Header  |  1994-01-08  |  4.9 KB  |  189 lines  |  [TEXT/KAHL]

  1. #include <QDOffscreen.h>
  2. #include "Anti-Aliased Text.h"
  3.  
  4.  
  5.  
  6.  
  7.  
  8. void FourtoOneDitherTo4BitGrey(PixMapHandle bwMap,PixMapHandle colorMap);
  9.  
  10.  
  11.  
  12.  
  13.  
  14. short AADrawString(StringPtr text)
  15. {
  16.     GWorldPtr        bwGWorld,colorGWorld;
  17.     CTabHandle        cTable;
  18.     GrafPtr            curPort;
  19.     FontInfo        fInfo;
  20.     CGrafPtr        portSave;
  21.     GDHandle        gdSave;
  22.     Style            textFace;
  23.     Rect            bwRect,colorRect,destRect;
  24.     short            textSize,textFont,width,height,smallAscent,err;
  25.     
  26.     
  27.     GetPort(&curPort);
  28.     GetFontInfo(&fInfo);
  29.     
  30.     smallAscent = fInfo.ascent;
  31.     textFace = curPort->txFace;
  32.     textFont = curPort->txFont;
  33.     textSize = curPort->txSize;
  34.     TextSize(textSize*4);
  35.     
  36.     width = StringWidth(text);
  37.     width += 32-(width%32);
  38.     
  39.     GetFontInfo(&fInfo);
  40.     height = fInfo.ascent+fInfo.descent;
  41.     height += 4-(height%4);
  42.     
  43.     TextSize(textSize);
  44.     
  45.     SetRect(&bwRect,0,0,width,height);
  46.     SetRect(&colorRect,0,0,width/4,height/4);
  47.     destRect = colorRect;
  48.     OffsetRect(&destRect,curPort->pnLoc.h,curPort->pnLoc.v-smallAscent);
  49.     
  50.     err = NewGWorld(&bwGWorld,1,&bwRect,nil,nil,0);
  51.     if (err != noErr)
  52.         return err;
  53.     
  54.     cTable = GetCTable(36);
  55.     err = NewGWorld(&colorGWorld,4,&colorRect,cTable,nil,0);
  56.     if (err != noErr)
  57.     {
  58.         DisposeGWorld(bwGWorld);
  59.         DisposCTable(cTable);
  60.         return err;
  61.     }
  62.     
  63.     LockPixels(GetGWorldPixMap(bwGWorld));
  64.     LockPixels(GetGWorldPixMap(colorGWorld));
  65.     
  66.     GetGWorld(&portSave,&gdSave);
  67.     SetGWorld(bwGWorld,nil);
  68.     
  69.     
  70.     EraseRect(&bwRect);
  71.     
  72.     TextSize(textSize*4);
  73.     TextFont(textFont);
  74.     TextFace(textFace);
  75.     
  76.     MoveTo(0,fInfo.ascent);
  77.     DrawString(text);
  78.     
  79.     FourtoOneDitherTo4BitGrey(GetGWorldPixMap(bwGWorld),GetGWorldPixMap(colorGWorld));
  80.     
  81.     
  82.     SetGWorld(portSave,gdSave);
  83.     
  84.     CopyBits((BitMap*)*GetGWorldPixMap(colorGWorld),&curPort->portBits,&colorRect,&destRect,curPort->txMode,nil);
  85.     
  86.     UnlockPixels(GetGWorldPixMap(bwGWorld));
  87.     UnlockPixels(GetGWorldPixMap(colorGWorld));
  88.     
  89.     DisposeGWorld(bwGWorld);
  90.     DisposeGWorld(colorGWorld);
  91.     DisposCTable(cTable);
  92.     
  93.     return noErr;
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. void FourtoOneDitherTo4BitGrey(PixMapHandle bwMap,PixMapHandle colorMap)
  101. {
  102.     Ptr        bwPtr,colorPtr;
  103.     short    height,width,bwRows,colorRows;
  104.     long    colorRowDiff,bwChunkMod;
  105.     Byte    *bwDataPtr,*colorDataPtr;
  106.     char    mode;
  107.     
  108.     
  109.     height = (*colorMap)->bounds.bottom-(*colorMap)->bounds.top;
  110.     width = ((*bwMap)->bounds.right-(*bwMap)->bounds.left)/8;
  111.     bwRows = (*bwMap)->rowBytes&0x3FFF;
  112.     colorRows = (*colorMap)->rowBytes&0x3FFF;
  113.     colorRowDiff = colorRows-width;
  114.     bwChunkMod = 4*bwRows-width;
  115.     
  116.     bwPtr = GetPixBaseAddr(bwMap);
  117.     colorPtr = GetPixBaseAddr(colorMap);
  118.     
  119.     mode = true32b;
  120.     SwapMMUMode(&mode);
  121.     
  122.     asm
  123.     {
  124.         movem.l    A0-A4/D0-D7,-(SP)    ;    save registers
  125.         
  126.         move.l    colorPtr,A2            ;    color data ptr
  127.         move.l    bwPtr,A0            ;    bw data ptr
  128.         moveq    #0,D6                ;    clear D6 for long value
  129.         move.w    bwRows,D6            ;    width of bw pixmap row data
  130.         lea        @data,A3            ;    store lookup table ptr
  131.         moveq    #0,D0                ;    clear D0 for long value
  132.         move.w    bwRows,D0            ;    offset from the right to the left of a chunk
  133.         lsl.l    #2,D0                ;    offset from the top to the bottom of a chunk
  134.         neg.l    D0                    ;    offset from the bottom to the top of a chunk
  135.         addq.w    #1,D0                ;    add offset to next chunk
  136.         
  137. @row    move.w    width,D7            ;    loop this many bytes across
  138.         
  139. @adv    moveq    #0,D3                ;    initial color result low nibble
  140.         moveq    #0,D4                ;    initial color result high nibble
  141.         moveq    #4,D5                ;    set up inner loop counter
  142.         
  143. @sum    move.b    (A0),D1                ;    get next byte    (D1 gets high nibble, D2 gets low nibble)
  144.         move.b    D1,D2                ;    copy for low nibble
  145.         andi.w    #0x000F,D2            ;    get only low nibble
  146.         andi.w    #0x00F0,D1            ;    get only high nibble
  147.         lsr.w    #4,D1                ;    put high nibble in low nibble for lookup
  148.         
  149.         movea.w    D2,A1                ;    get table offset
  150.         adda.l    A3,A1                ;    compute offset from start of table
  151.         add.b    (A1),D3                ;    add #bits in low nibble to color result
  152.         movea.w    D1,A1                ;    get table offset
  153.         adda.l    A3,A1                ;    compute offset from start of table
  154.         add.b    (A1),D4                ;    add #bits in high nibble to color result
  155.         
  156.         adda.l    D6,A0                ;    go to the next line
  157.         subq.w    #1,D5                ;    subract one line of chunk
  158.         bne.s    @sum                ;    branch until we've summed both 4x4 blocks
  159.         
  160.         btst    #4,D3                ;    is high nibble off ramp scale?
  161.         beq.s    @1                    ;    nope, continue
  162.         moveq    #0x0F,D3            ;    clip it to the top of the ramp
  163. @1        lsl.b    #4,D4                ;    shift low resultant nibble to high nibble
  164.         bcc.s    @2
  165.         move.b    #0xF0,D4            ;    clip it to the top of the ramp
  166. @2        or.w    D4,D3                ;    or both nibbles into a byte
  167.         move.b    D3,(A2)+            ;    write the byte into the color pixmap
  168.         
  169.         adda.l    D0,A0                ;    move bw data ptr to the next chunk
  170.         subq.w    #1,D7                ;    subtract another column
  171.         bne.s    @adv                ;    branch until we've advanced across a row
  172.         
  173.         add.l    bwChunkMod,A0        ;    move bw data ptr to the beginning of the next chunk
  174.         add.l    colorRowDiff,A2        ;    move color data ptr to the beginning the next line
  175.         subi.w    #1,height            ;    subtract another row
  176.         bne.s    @row                ;    branch until we've covered every row
  177.         
  178.         movem.l    (SP)+,A0-A4/D0-D7    ;    restore registers
  179.         bra.s    @dataEnd
  180.         
  181. @data    dc.l    0x00010102
  182.         dc.l    0x01020203
  183.         dc.l    0x01020203
  184.         dc.l    0x02030304
  185. @dataEnd
  186.     }
  187.     
  188.     SwapMMUMode(&mode);
  189. }