GUI Fixes
[tpg/acess2.git] / Usermode / Applications / axwin2_src / WM / video_text.c
index 897c411..7cf1049 100644 (file)
@@ -23,7 +23,7 @@ typedef struct sGlyph {
        short   TrueHeight;
        
        // Bitmap Data
-       uint8_t Bitmap[];
+       uint8_t Bitmap[];       // 8-bit alpha
        
 }      tGlyph;
 
@@ -46,7 +46,7 @@ struct sFont {
 tGlyph *_GetGlyph(tFont *Font, uint32_t Codepoint);
 void   _RenderGlyph(short X, short Y, tGlyph *Glyph, uint32_t Color);
 tGlyph *_SystemFont_CacheGlyph(tFont *Font, uint32_t Codepoint);
- int   ReadUTF8(char *Input, uint32_t *Output);
+ int   ReadUTF8(const char *Input, uint32_t *Output);
 
 // === GLOBALS ===
 tFont  gSystemFont = {
@@ -62,6 +62,9 @@ int Video_DrawText(short X, short Y, short W, short H, tFont *Font, uint32_t Col
         int    xOfs = 0;
        tGlyph  *glyph;
        uint32_t        ch = 0;
+
+       _SysDebug("Video_DrawText: (X=%i,Y=%i,W=%i,H=%i,Font=%p,", X, Y, W, H, Font);
+       _SysDebug("  Color=%08x,Text='%s')", Color, Text);
        
        // Check the bounds
        if(W < 0 || X < 0 || X >= giScreenWidth)        return 0;
@@ -80,8 +83,7 @@ int Video_DrawText(short X, short Y, short W, short H, tFont *Font, uint32_t Col
                
                // Find (or load) the glyph
                glyph = _GetGlyph(Font, ch);
-               if( glyph )
-                       continue ;      // If not found, just don't render it
+               if( !glyph )    continue ;      // If not found, just don't render it
                
                // End render if it will overflow the perscribed range
                if( xOfs + glyph->TrueWidth > W )
@@ -94,6 +96,27 @@ int Video_DrawText(short X, short Y, short W, short H, tFont *Font, uint32_t Col
        return xOfs;
 }
 
+void Video_GetTextDims(tFont *Font, const char *Text, int *W, int *H)
+{
+        int    w=0, h=0;
+       uint32_t        ch;
+       tGlyph  *glyph;
+       if( !Font )     Font = &gSystemFont;
+       
+       while( *Text )
+       {
+               Text += ReadUTF8(Text, &ch);
+               glyph = _GetGlyph(Font, ch);
+               if( !glyph )    continue;
+               
+               w += glyph->Width;
+               if( h < glyph->Height ) h = glyph->Height;
+       }
+       
+       if(W)   *W = w;
+       if(H)   *H = h;
+}
+
 tGlyph *_GetGlyph(tFont *Font, uint32_t Codepoint)
 {
        tGlyph  *next = NULL, *prev = NULL;
@@ -103,7 +126,7 @@ tGlyph      *_GetGlyph(tFont *Font, uint32_t Codepoint)
        if( Codepoint < 128 )
        {
                if( Font->AsciiGlyphs[Codepoint] == NULL ) {
-                       Font->CacheGlyph(Font, Codepoint);
+                       Font->AsciiGlyphs[Codepoint] = Font->CacheGlyph(Font, Codepoint);
                }
                
                return Font->AsciiGlyphs[Codepoint];
@@ -186,7 +209,37 @@ tGlyph     *_GetGlyph(tFont *Font, uint32_t Codepoint)
  */
 void _RenderGlyph(short X, short Y, tGlyph *Glyph, uint32_t Color)
 {
-       
+        int    xStart = 0, yStart = 0;
+        int    x, y;
+       uint32_t        *outBuf;
+       uint8_t *inBuf;
+       // TODO: Implement
+
+       X += Glyph->OffsetX;
+       if( X < 0 ) {   // If -ve, skip the first -X collums
+               xStart = -X;
+               X = 0;
+       }
+
+       Y += Glyph->OffsetY;
+       if( Y < 0 ) {   // If -ve, skip the first -Y lines
+               yStart = -Y;
+               Y = 0;
+       }
+
+       outBuf = gpScreenBuffer + Y*giScreenWidth + X;
+       inBuf = Glyph->Bitmap + yStart*Glyph->TrueWidth;
+
+       for( y = yStart; y < Glyph->TrueHeight; y ++ )
+       {
+               for( x = xStart; x < Glyph->TrueWidth; x ++ )
+               {
+                       outBuf[x] = Video_AlphaBlend( outBuf[x], Color, inBuf[x] );
+               }
+               outBuf += giScreenWidth;
+               inBuf += Glyph->TrueWidth;
+
+       }
 }
 
 // Load system font (8x16 monospace)
@@ -199,6 +252,9 @@ tGlyph *_SystemFont_CacheGlyph(tFont *Font, uint32_t Codepoint)
         int    i;
        uint8_t index = 0;
        tGlyph  *ret;
+       uint8_t *data;
+
+       _SysDebug("_SystemFont_CacheGlyph: (Font=%p, Codepoint=0x%06x)", Font, Codepoint);
        
        if( Codepoint < 128 ) {
                index = Codepoint;
@@ -207,7 +263,15 @@ tGlyph *_SystemFont_CacheGlyph(tFont *Font, uint32_t Codepoint)
                index = '?';    // Unknowns come out as a question mark
        }
 
+       _SysDebug(" index = %i", index);
+
        ret = malloc( sizeof(tGlyph) + FONT_WIDTH*FONT_HEIGHT );
+       if( !ret ) {
+               _SysDebug("ERROR: malloc(%i) failed", sizeof(tGlyph) + FONT_WIDTH*FONT_HEIGHT);
+               return NULL;
+       }
+
+       ret->Codepoint = Codepoint;
 
        ret->Width = FONT_WIDTH;
        ret->Height = FONT_HEIGHT;
@@ -218,16 +282,18 @@ tGlyph *_SystemFont_CacheGlyph(tFont *Font, uint32_t Codepoint)
        ret->OffsetX = 0;
        ret->OffsetY = 0;
        
+       data = &VTermFont[index * FONT_HEIGHT];
+
        for( i = 0; i < FONT_HEIGHT; i ++ )
        {
-               ret->Bitmap[ i * 8 + 0 ] = VTermFont[index] & (1 << 0) ? 255 : 0;
-               ret->Bitmap[ i * 8 + 1 ] = VTermFont[index] & (1 << 1) ? 255 : 0;
-               ret->Bitmap[ i * 8 + 2 ] = VTermFont[index] & (1 << 2) ? 255 : 0;
-               ret->Bitmap[ i * 8 + 3 ] = VTermFont[index] & (1 << 3) ? 255 : 0;
-               ret->Bitmap[ i * 8 + 4 ] = VTermFont[index] & (1 << 4) ? 255 : 0;
-               ret->Bitmap[ i * 8 + 5 ] = VTermFont[index] & (1 << 5) ? 255 : 0;
-               ret->Bitmap[ i * 8 + 6 ] = VTermFont[index] & (1 << 6) ? 255 : 0;
-               ret->Bitmap[ i * 8 + 7 ] = VTermFont[index] & (1 << 7) ? 255 : 0;
+               ret->Bitmap[ i * 8 + 0 ] = data[i] & (1 << 7) ? 255 : 0;
+               ret->Bitmap[ i * 8 + 1 ] = data[i] & (1 << 6) ? 255 : 0;
+               ret->Bitmap[ i * 8 + 2 ] = data[i] & (1 << 5) ? 255 : 0;
+               ret->Bitmap[ i * 8 + 3 ] = data[i] & (1 << 4) ? 255 : 0;
+               ret->Bitmap[ i * 8 + 4 ] = data[i] & (1 << 3) ? 255 : 0;
+               ret->Bitmap[ i * 8 + 5 ] = data[i] & (1 << 2) ? 255 : 0;
+               ret->Bitmap[ i * 8 + 6 ] = data[i] & (1 << 1) ? 255 : 0;
+               ret->Bitmap[ i * 8 + 7 ] = data[i] & (1 << 0) ? 255 : 0;
        }
 
        return ret;
@@ -238,9 +304,9 @@ tGlyph *_SystemFont_CacheGlyph(tFont *Font, uint32_t Codepoint)
  * \fn int ReadUTF8(char *Input, uint32_t *Val)
  * \brief Read a UTF-8 character from a string
  */
-int ReadUTF8(char *Input, uint32_t *Val)
+int ReadUTF8(const char *Input, uint32_t *Val)
 {
-       uint8_t *str = (uint8_t *)Input;
+       const uint8_t   *str = (const uint8_t *)Input;
        *Val = 0xFFFD;  // Assume invalid character
        
        // ASCII

UCC git Repository :: git.ucc.asn.au