home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / term23_2.lha / Source_Code / termSource / termScale.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  9KB  |  460 lines

  1. /*
  2. **    $Id: termScale.c,v 1.2 92/08/15 20:15:08 olsen Sta Locker: olsen $
  3. **    $Revision: 1.2 $
  4. **    $Date: 92/08/15 20:15:08 $
  5. **
  6. **    Single scaled character output routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* Some static data required by the bitmap scaling routine. */
  15.  
  16. STATIC struct RastPort        *ScaleRPort;
  17. STATIC struct BitMap        *ScaleSrcBitMap,
  18.                 *ScaleDstBitMap;
  19. STATIC struct BitScaleArgs    *ScaleArgs;
  20.  
  21. STATIC WORD             ScaleCache = -1,
  22.                  PlaneWidth,
  23.                  PlaneHeight;
  24.  
  25.     /* DeleteScale():
  26.      *
  27.      *    Frees all the data associated with font scaling.
  28.      */
  29.  
  30. VOID
  31. DeleteScale()
  32. {
  33.     WORD i;
  34.  
  35.     if(ScaleArgs)
  36.     {
  37.         FreeVec(ScaleArgs);
  38.  
  39.         ScaleArgs = NULL;
  40.     }
  41.  
  42.     if(ScaleDstBitMap)
  43.     {
  44.         for(i = 0 ; i < ScaleDstBitMap -> Depth ; i++)
  45.         {
  46.             if(ScaleDstBitMap -> Planes[i])
  47.                 FreeRaster(ScaleDstBitMap -> Planes[i],PlaneWidth * 2,PlaneHeight * 2);
  48.         }
  49.  
  50.         FreeVec(ScaleDstBitMap);
  51.  
  52.         ScaleDstBitMap = NULL;
  53.     }
  54.  
  55.     if(ScaleSrcBitMap)
  56.     {
  57.         for(i = 0 ; i < ScaleSrcBitMap -> Depth ; i++)
  58.         {
  59.             if(ScaleSrcBitMap -> Planes[i])
  60.                 FreeRaster(ScaleSrcBitMap -> Planes[i],PlaneWidth,PlaneHeight);
  61.         }
  62.  
  63.         FreeVec(ScaleSrcBitMap);
  64.  
  65.         ScaleSrcBitMap = NULL;
  66.     }
  67.  
  68.     if(ScaleRPort)
  69.     {
  70.         FreeVec(ScaleRPort);
  71.  
  72.         ScaleRPort = NULL;
  73.     }
  74. }
  75.  
  76.     /* CreateScale():
  77.      *
  78.      *    Sets up the data required for real-time font scaling
  79.      *    (bitmaps, rastports, etc.).
  80.      */
  81.  
  82. BYTE
  83. CreateScale()
  84. {
  85.         /* Create a RastPort to render into. */
  86.  
  87.     if(ScaleRPort = (struct RastPort *)AllocVec(sizeof(struct RastPort),MEMF_ANY|MEMF_CLEAR))
  88.     {
  89.         WORD MaxWidth,i;
  90.  
  91.         if(GFX)
  92.             MaxWidth = GFX -> tf_XSize;
  93.         else
  94.             MaxWidth = 0;
  95.  
  96.         if(TextFontWidth > MaxWidth)
  97.             MaxWidth = TextFontWidth;
  98.  
  99.             /* Remember dimensions. */
  100.  
  101.         PlaneWidth    = MaxWidth;
  102.         PlaneHeight    = TextFontHeight;
  103.  
  104.             /* Initialize it. */
  105.  
  106.         InitRastPort(ScaleRPort);
  107.  
  108.             /* Create the bitmap to render into. */
  109.  
  110.         if(ScaleSrcBitMap = (struct BitMap *)AllocVec(sizeof(struct BitMap),MEMF_ANY|MEMF_CLEAR))
  111.         {
  112.                 /* Create the bitmap to place the scaled font data into. */
  113.  
  114.             if(ScaleDstBitMap = (struct BitMap *)AllocVec(sizeof(struct BitMap),MEMF_ANY|MEMF_CLEAR))
  115.             {
  116.                 BYTE AllFine = TRUE;
  117.  
  118.                     /* Initialize the bitmap. */
  119.  
  120.                 InitBitMap(ScaleSrcBitMap,Screen -> RastPort . BitMap -> Depth,PlaneWidth,PlaneHeight);
  121.  
  122.                     /* Allocate the necessary memory space. */
  123.  
  124.                 for(i = 0 ; i < ScaleSrcBitMap -> Depth ; i++)
  125.                 {
  126.                     if(!(ScaleSrcBitMap -> Planes[i] = AllocRaster(PlaneWidth,PlaneHeight)))
  127.                     {
  128.                         AllFine = FALSE;
  129.  
  130.                         break;
  131.                     }
  132.                 }
  133.  
  134.                 if(AllFine)
  135.                 {
  136.                         /* Initialize destination bitmap, it must be
  137.                          * able to hold four times the size of the
  138.                          * source data.
  139.                          */
  140.  
  141.                     InitBitMap(ScaleDstBitMap,Screen -> RastPort . BitMap -> Depth,PlaneWidth * 2,PlaneHeight * 2);
  142.  
  143.                         /* Allocate space for the destination area. */
  144.  
  145.                     for(i = 0 ; i < ScaleDstBitMap -> Depth ; i++)
  146.                     {
  147.                         if(!(ScaleDstBitMap -> Planes[i] = AllocRaster(PlaneWidth * 2,PlaneHeight * 2)))
  148.                         {
  149.                             AllFine = FALSE;
  150.  
  151.                             break;
  152.                         }
  153.                     }
  154.  
  155.                     if(AllFine)
  156.                     {
  157.                             /* Put the source bitmap into the source RastPort. */
  158.  
  159.                         ScaleRPort -> BitMap = ScaleSrcBitMap;
  160.  
  161.                             /* Install the fonts. */
  162.  
  163.                         SetFont(ScaleRPort,CurrentFont);
  164.  
  165.                             /* Set the default rendering pens. */
  166.  
  167.                         SetAPen(ScaleRPort,1);
  168.                         SetBPen(ScaleRPort,0);
  169.  
  170.                             /* By default, overwrite data. */
  171.  
  172.                         SetDrMd(ScaleRPort,JAM2);
  173.  
  174.                             /* Allocate space for the bitmap scaling arguments. */
  175.  
  176.                         if(ScaleArgs = (struct BitScaleArgs *)AllocVec(sizeof(struct BitScaleArgs),MEMF_ANY|MEMF_CLEAR))
  177.                         {
  178.                                 /* Initialize the structure. */
  179.  
  180.                             ScaleArgs -> bsa_SrcWidth    = TextFontWidth;
  181.                             ScaleArgs -> bsa_SrcHeight    = TextFontHeight;
  182.  
  183.                             ScaleArgs -> bsa_YSrcFactor    = 1;
  184.  
  185.                             ScaleArgs -> bsa_SrcBitMap    = ScaleSrcBitMap;
  186.                             ScaleArgs -> bsa_DestBitMap    = ScaleDstBitMap;
  187.  
  188.                             return(TRUE);
  189.                         }
  190.                     }
  191.                 }
  192.             }
  193.         }
  194.     }
  195.  
  196.     return(FALSE);
  197. }
  198.  
  199.     /* PrintScaled(UBYTE Char,UBYTE Scale):
  200.      *
  201.      *    This is the big one: since VT100 supports a number of
  202.      *    font sizes (double height, double width, 132 columns),
  203.      *    the approriate characters are scaled in real-time before
  204.      *    they are displayed.
  205.      */
  206.  
  207. VOID __regargs
  208. PrintScaled(UBYTE *Buffer,LONG Size,UBYTE Scale)
  209. {
  210.     UWORD SrcY,DestX,DestY,SizeX,Baseline;
  211.  
  212.         /* Determine the scale of the destination character. */
  213.  
  214.     if(Config . FontScale == SCALE_HALF)
  215.     {
  216.             /* Determine scale to be used. */
  217.  
  218.         switch(Scale)
  219.         {
  220.                 /* Half width. */
  221.  
  222.             case SCALE_ATTR_NORMAL:
  223.  
  224.                 ScaleArgs -> bsa_XDestFactor    = 1;
  225.                 ScaleArgs -> bsa_YDestFactor    = 1;
  226.                 ScaleArgs -> bsa_XSrcFactor    = 2;
  227.  
  228.                 SrcY    = 0;
  229.                 DestX    = CursorX * TextFontWidth / 2;
  230.                 SizeX    = TextFontWidth / 2;
  231.  
  232.                 ScaleCache = -1;
  233.  
  234.                 break;
  235.  
  236.                 /* Half width, double height (top bits). */
  237.  
  238.             case SCALE_ATTR_TOP2X:
  239.  
  240.                 ScaleArgs -> bsa_XDestFactor    = 1;
  241.                 ScaleArgs -> bsa_YDestFactor    = 2;
  242.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  243.  
  244.                 SrcY    = 0;
  245.                 DestX    = CursorX * TextFontWidth;
  246.                 SizeX    = TextFontWidth;
  247.  
  248.                 ScaleCache = -1;
  249.  
  250.                 break;
  251.  
  252.                 /* Half width, double height (bottom bits). */
  253.  
  254.             case SCALE_ATTR_BOT2X:
  255.  
  256.                 ScaleArgs -> bsa_XDestFactor    = 1;
  257.                 ScaleArgs -> bsa_YDestFactor    = 2;
  258.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  259.  
  260.                 SrcY    = TextFontHeight;
  261.                 DestX    = CursorX * TextFontWidth;
  262.                 SizeX    = TextFontWidth;
  263.  
  264.                 ScaleCache = -1;
  265.  
  266.                 break;
  267.         }
  268.     }
  269.     else
  270.     {
  271.             /* Determine scale to be used. */
  272.  
  273.         switch(Scale)
  274.         {
  275.                 /* Double height (top bits). */
  276.  
  277.             case SCALE_ATTR_TOP2X:
  278.  
  279.                 ScaleArgs -> bsa_XDestFactor    = 2;
  280.                 ScaleArgs -> bsa_YDestFactor    = 2;
  281.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  282.  
  283.                 SrcY    = 0;
  284.                 DestX    = CursorX * TextFontWidth * 2;
  285.                 SizeX    = TextFontWidth * 2;
  286.  
  287.                 ScaleCache = -1;
  288.  
  289.                 break;
  290.  
  291.                 /* Double height (bottom bits). */
  292.  
  293.             case SCALE_ATTR_BOT2X:
  294.  
  295.                 ScaleArgs -> bsa_XDestFactor    = 2;
  296.                 ScaleArgs -> bsa_YDestFactor    = 2;
  297.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  298.  
  299.                 SrcY    = TextFontHeight;
  300.                 DestX    = CursorX * TextFontWidth * 2;
  301.                 SizeX    = TextFontWidth * 2;
  302.  
  303.                 ScaleCache = -1;
  304.  
  305.                 break;
  306.  
  307.                 /* Double width. */
  308.  
  309.             case SCALE_ATTR_2X:
  310.  
  311.                 ScaleArgs -> bsa_XDestFactor    = 2;
  312.                 ScaleArgs -> bsa_YDestFactor    = 1;
  313.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  314.  
  315.                 SrcY    = 0;
  316.                 DestX    = CursorX * TextFontWidth * 2;
  317.                 SizeX    = TextFontWidth * 2;
  318.  
  319.                 ScaleCache = -1;
  320.  
  321.                 break;
  322.         }
  323.     }
  324.  
  325.         /* Look for the font type to scale. */
  326.  
  327.     if(ScaleRPort -> Font != CurrentFont)
  328.     {
  329.         SetFont(ScaleRPort,CurrentFont);
  330.  
  331.         ScaleArgs -> bsa_SrcWidth = TextFontWidth;
  332.  
  333.         ScaleCache = -1;
  334.     }
  335.  
  336.         /* Set the approriate colours. */
  337.  
  338.     if(ScaleRPort -> FgPen != RPort -> FgPen)
  339.     {
  340.         SetAPen(ScaleRPort,RPort -> FgPen);
  341.  
  342.         ScaleCache = -1;
  343.     }
  344.  
  345.     if(ScaleRPort -> BgPen != RPort -> BgPen)
  346.     {
  347.         SetBPen(ScaleRPort,RPort -> BgPen);
  348.  
  349.         ScaleCache = -1;
  350.     }
  351.  
  352.         /* Calculate topmost line to write to. */
  353.  
  354.     DestY = CursorY * TextFontHeight;
  355.  
  356.         /* Remember the font baseline. */
  357.  
  358.     Baseline = CurrentFont -> tf_Baseline;
  359.  
  360.     if(CurrentFont == GFX)
  361.     {
  362.         BYTE Mode = 1;
  363.  
  364.             /* Run down the buffer... */
  365.  
  366.         while(Size--)
  367.         {
  368.             if(GfxTable[*Buffer] == Mode)
  369.             {
  370.                 if(*Buffer != ScaleCache)
  371.                 {
  372.                     ScaleCache = *Buffer;
  373.  
  374.                         /* Print the character to be scaled into the
  375.                          * invisible drawing area.
  376.                          */
  377.  
  378.                     Move(ScaleRPort,0,Baseline);
  379.  
  380.                     Text(ScaleRPort,Buffer++,1);
  381.  
  382.                         /* Scale the font. */
  383.  
  384.                     BitMapScale(ScaleArgs);
  385.                 }
  386.                 else
  387.                     Buffer++;
  388.  
  389.                     /* Render the character. */
  390.  
  391.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,DestX,DestY,SizeX,TextFontHeight,0xC0);
  392.             }
  393.             else
  394.             {
  395.                 ScaleCache = *Buffer;
  396.  
  397.                 if(Mode)
  398.                     SetFont(ScaleRPort,TextFont);
  399.                 else
  400.                     SetFont(ScaleRPort,GFX);
  401.  
  402.                 Mode ^= 1;
  403.  
  404.                     /* Print the character to be scaled into the
  405.                      * invisible drawing area.
  406.                      */
  407.  
  408.                 Move(ScaleRPort,0,Baseline);
  409.  
  410.                 Text(ScaleRPort,Buffer++,1);
  411.  
  412.                     /* Scale the font. */
  413.  
  414.                 BitMapScale(ScaleArgs);
  415.  
  416.                     /* Render the character. */
  417.  
  418.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,DestX,DestY,SizeX,TextFontHeight,0xC0);
  419.             }
  420.  
  421.             DestX += SizeX;
  422.         }
  423.  
  424.         if(!Mode)
  425.             SetFont(ScaleRPort,GFX);
  426.     }
  427.     else
  428.     {
  429.             /* Run down the buffer... */
  430.  
  431.         while(Size--)
  432.         {
  433.             if(*Buffer != ScaleCache)
  434.             {
  435.                 ScaleCache = *Buffer;
  436.  
  437.                     /* Print the character to be scaled into the
  438.                      * invisible drawing area.
  439.                      */
  440.  
  441.                 Move(ScaleRPort,0,Baseline);
  442.  
  443.                 Text(ScaleRPort,Buffer++,1);
  444.  
  445.                     /* Scale the font. */
  446.  
  447.                 BitMapScale(ScaleArgs);
  448.             }
  449.             else
  450.                 Buffer++;
  451.  
  452.                 /* Render the character. */
  453.  
  454.             BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,DestX,DestY,SizeX,TextFontHeight,0xC0);
  455.  
  456.             DestX += SizeX;
  457.         }
  458.     }
  459. }
  460.