font.c 7.41 KB
#include "ultra64.h"
#include "viewer.h"
#include "page.h"
#include "font.h"
#include "texture/font_e.c"
#include "texture/font_e.tbl"
#include "texrect.h"
#include "texture.h"

#define HZ_NUM         (87*94)
#define FONT12_SIZE    18
#define GB_CODE_START  0xA1
#define GB_CODE_SIZE   94

int     gBlink = 40;
u16     gTexSnum[256*11] __attribute__ ((aligned (16)));
u16     gTexWnum[256*11] __attribute__ ((aligned (16)));

#if 0
u8      gFontMap[147204];
u16     gStringMap[256*MAX_STRING_LEN] __attribute__ ((aligned (16)));
u16    *gStringMapPtr;

#ifdef _16x16_GLYPH
void gen_gb_glyph(u8* font_map, u16* out_glyph, u8 b1, u8 b2, u16 color)
{
    u8  gb_offset = 0xa1;
    u8  unit_len  = 94;
    u8  byte;
    u8 *ptr;
    int i, j;

    int idx = (b1 - gb_offset) * unit_len + (b2 - gb_offset);

    ptr = font_map + idx*32; 
    for (i=0; i<32; i++) {
    byte = ptr[i]; 
        for (j=0; j<8; j++)
        out_glyph[i*8+j] = (byte & (0x80>>j)) == 0 ? 0 : color;
    }
}
#endif

void output_12x12_line(u16 *bit, int v, u16 color)
{
    int i;
    u16 *p=bit;

    for (i=11; i>=0; i--) {
        *p++ = ((v >> i) & 1) ? color:0x0;
    }

    for (i=0; i<4; i++)
       *p++ = 0x0;

    return ;
}

int gen_gb_glyph(int gb1, int gb2, u16 *bit, u8 *font, u16 color)
{
    int offset, i, j;
    u8 *p;
    u16 *line;

    if (gb1 < 0xA1 || gb2 < 0xA1 || bit==NULL || font==NULL)
        return -1;

    offset = (gb1 - GB_CODE_START) * GB_CODE_SIZE + (gb2 - GB_CODE_START);
    if (offset >= HZ_NUM) return -2;
    offset *= FONT12_SIZE;
    p = font + offset;

    for (i=0, line=bit; i<6; i++, p+=3) {
        output_12x12_line(line, ((int) p[0]) << 4 | ((p[1] >> 4) & 0xf), color);
        line += 16;
        output_12x12_line(line, ((int) p[2]) << 4 | (p[1]  & 0xf), color);
        line += 16;
    }
    for (i=0; i<4; i++, line += 16/2) /* pading extra 4 lines */
        output_12x12_line(line, 0, color);

    {
      static int count = 0;

      if( count < 0 ) {
    count++;
    PRINTF( "glyph:\n" );
    for( i = 0; i < 16; i++ ) {
      PRINTF( "   " );
      for( j = 0; j < 16; j++ ) {
        if( bit[16*i + j] )
          PRINTF( "+" );
        else
          PRINTF( " " );
      }
      PRINTF( "\n" );
    }
      }
    }

    return 0; 
}

void drawGBChar(u16* texFont, u8 gb_b0, u8 gb_b1, f32 posx, f32 posy, u16 color)
{
    gen_gb_glyph(gb_b0, gb_b1, gStringMapPtr, gFontMap, color);
    drawTexRect(gStringMapPtr, //texTmp
                G_IM_FMT_RGBA, G_IM_SIZ_16b,
                16, 16,
                16, 16,
                1<<10, 1<<10,
                16, 16,
                posx, posy);
    gStringMapPtr += 1024;
}
#endif

void drawZhChar(u8 gb_b0, u8 gb_b1, f32 posx, f32 posy, u16 color)
{
    drawTexRect(texZhglyph+gb_b1*TEX_ZH_GLYPH_W*TEX_ZH_GLYPH_H*2, 
                G_IM_FMT_RGBA, G_IM_SIZ_16b,
                24, 24,
                24, 24,
                1<<10, 1<<10,
                24, 24,
                posx, posy);
}

void drawAsciiChar(u8 ascii, f32 posx, f32 posy, u16 color) 
{
    drawTexRectScale(texAscii + (font_e_tbl[(ascii & 0x7f)<<1]-1) * 256,
             G_IM_FMT_I, G_IM_SIZ_8b,
             16, 16,
             16, 16,
             1<<10, 1<<10,
             16, 16,
             posx, posy);
}

void drawSnum(u8 num, f32 posx, f32 posy, u16 color) 
{
    u16* sptr = (u16*)(texSnum + (num-0x30)*512);
    u16* dptr;

    if (color==0xFFFF) 
        dptr = gTexWnum + (num-0x30)*256;
    else
        dptr = gTexSnum + (num-0x30)*256;

    colorTex(sptr, dptr, 16, 16, color);
    drawTexRectScale(dptr,
             G_IM_FMT_RGBA, G_IM_SIZ_16b,
             16, 16,
             16, 16,
             1<<10, 1<<10,
             16, 16,
             posx, posy);
}

void drawBnum(u8 num, f32 posx, f32 posy, u16 color) 
{
    u8* sptr = texBnum + (num-0x30)*(TEX_BNUM_W*TEX_BNUM_H*2);

    drawTexRect(sptr,
                G_IM_FMT_RGBA, G_IM_SIZ_16b,
                TEX_BNUM_W, TEX_BNUM_H,
                16, 16,
                1<<10, 1<<10,
                TEX_BNUM_W, TEX_BNUM_H,
                posx, posy);
}

void drawBnumString(u8* str, f32 posx, f32 posy, u16 color)
{
    f32 offx = posx, offy = posy;

    while (*str!='\0') {
        if (*str>=0x30 && *str<=0x39) {
            drawBnum(*str, offx, offy, color);
            str++;
            offx += 16; 
        }
    }
}

void drawString(u8* str, u16* texFont, f32 posx, f32 posy)
{
    f32 offx = posx, offy = posy;
    
    while (*str!='\0') {
        if (*str>=0xA1) {
                /*
            drawGBChar(texFont, *(str), *(str+1), offx, offy, 0xFFFF); 
            offx += 14;
            */
            str += 2;
        } else if (*str=='\n') {
            str++;
            offx = posx; 
            offy += 16; 
        } else if (*str==' ') {
            str++;
            offx += 9;
        } else {
            drawAsciiChar(*str, offx, offy, 0xFFFF);
            str++;
            offx += 16; //(f32)(font_e_tbl[(((*str) & 0x7f)<<1) + 1]*1.8); 
        }
    }
}

void drawColorString(u8* str, f32 posx, f32 posy, u16 color)
{
    f32 offx = posx, offy = posy;
    
    while (*str!='\0') {
        if (*str>=0xA1) {  
            drawZhChar(*(str), *(str+1), offx, offy, color); 
            str += 2;
            offx += TEX_ZH_GLYPH_W+1;
        } else if (*str=='\n') {
            str++;
            offx = posx; 
            offy += TEX_ZH_GLYPH_H+2; 
        } else if (*str==' ') {
            str++;
            offx += 9;
        } else if (*str>=0x30 && *str<=0x3A) {
            drawSnum(*str, offx, offy, color);
            str++;
            offx += 12; 
        } else {
            drawAsciiChar(*str, offx, offy, color);
            str++;
            offx += 16; //(f32)(font_e_tbl[(((*str) & 0x7f)<<1) + 1]*1.8); 
        }
    }
}

void drawBlinkString(u8* str, f32 posx, f32 posy, u16 color)
{
    if (gBlink == 60) 
        gBlink = 0;
    else {
        gBlink ++;
        if (gBlink > 10) 
            drawColorString(str, posx, posy, color);
    }
}

void drawIsbnString(u8* str, u16* texFont, f32 posx, f32 posy)
{
    f32 offx = posx, offy = posy;
    
    while (*str!='\0') {
        if (*str>=0x30 && *str<=0x39) {
            drawTexRect(texIsbnGlyph + (*str - 0x30) * 512,
                        G_IM_FMT_IA, G_IM_SIZ_16b,
                        16, 16, 16, 16, 1<<10, 1<<10, 16, 16,
                        offx, offy);
            offx += 9;
        } else if (*str=='-') {
            drawTexRect(texIsbnGlyph + 36 * 512,
                        G_IM_FMT_IA, G_IM_SIZ_16b,
                        16, 16, 16, 16, 1<<10, 1<<10, 16, 16,
                        offx, offy);
            offx += 9;
        } else if (*str=='[') {
            drawTexRect(texIsbnGlyph + 37 * 512,
                        G_IM_FMT_IA, G_IM_SIZ_16b,
                        16, 16, 16, 16, 1<<10, 1<<10, 16, 16,
                        offx, offy);
            offx += 10;
        } else if (*str==']') {
            drawTexRect(texIsbnGlyph + 38 * 512,
                        G_IM_FMT_IA, G_IM_SIZ_16b,
                        16, 16, 16, 16, 1<<10, 1<<10, 16, 16,
                        offx, offy);
            offx += 10;
        } else if (*str==' ') {
            offx += 10;
        } else if (*str>=0x41 && *str<=0x5A) {
            drawTexRect(texIsbnGlyph + (*str - 55) * 512,
                        G_IM_FMT_IA, G_IM_SIZ_16b,
                        16, 16, 16, 16, 1<<10, 1<<10, 16, 16,
                        offx, offy);
            offx += 11;  
        }
        str ++;
    }
}