home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d832 / term.lha / Term / term-3.1-Source.lha / termScale.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  9KB  |  458 lines

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