aboutsummaryrefslogtreecommitdiffstats
path: root/code/editor/editor_font.h
blob: 9018f874abbc89165af57be769ae8dda69e6fd4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#if !defined(EDITOR_FONT_H)
#define EDITOR_FONT_H

#include <stdarg.h>
#include <stdio.h>

// TODO(luca): UTF8 handling 

    typedef struct font font;
    struct font
    {
        stbtt_fontinfo Info;
        s32 Ascent;
        s32 Descent;
        s32 LineGap;
        v2 BoundingBox[2];
        b32 Initialized;
    };
    
//~ API

internal f32
GetBaseline(font *Font, f32 Scale)
{
    f32 Result = Scale*(f32)(Font->Ascent - Font->Descent + Font->LineGap);
    return Result;
}

internal void
InitFont(font *Font, char *FilePath)
{
    str8 File = OS_ReadEntireFileIntoMemory(FilePath);
    
    if(File.Size)
    {
        if(stbtt_InitFont(&Font->Info, File.Data, stbtt_GetFontOffsetForIndex(File.Data, 0)))
        {
            Font->Info.data = (u8 *)File.Data;
            
            s32 X0, Y0, X1, Y1;
            stbtt_GetFontBoundingBox(&Font->Info, &X0, &Y0, &X1, &Y1);
            Font->BoundingBox[0] = V2S32(X0, Y0);
            Font->BoundingBox[1] = V2S32(X1, Y1);
            stbtt_GetFontVMetrics(&Font->Info, &Font->Ascent, &Font->Descent, &Font->LineGap);
            Font->Initialized = true;
        }
        else
        {
            // TODO(luca): Logging
        }
    }
    else
    {
        // TODO(luca): Logging
    }
}

//- Rendering 
internal void
DrawCharacter(app_offscreen_buffer *Buffer,  u8 *FontBitmap,
              int FontWidth, int FontHeight, 
              int XOffset, int YOffset,
              u32 Color)
{
    s32 MinX = 0;
    s32 MinY = 0;
    s32 MaxX = FontWidth;
    s32 MaxY = FontHeight;
    
    if(XOffset < 0)
    {
        MinX = -XOffset;
        XOffset = 0;
    }
    if(YOffset < 0)
    {
        MinY = -YOffset;
        YOffset = 0;
    }
    if(XOffset + FontWidth > Buffer->Width)
    {
        MaxX -= ((XOffset + FontWidth) - Buffer->Width);
    }
    if(YOffset + FontHeight > Buffer->Height)
    {
        MaxY -= ((YOffset + FontHeight) - Buffer->Height);
    }
    
    u8 *Row = (u8 *)(Buffer->Pixels) + 
    (YOffset*Buffer->Pitch) +
    (XOffset*Buffer->BytesPerPixel);
    
    for(int  Y = MinY;
        Y < MaxY;
        Y++)
    {
        u32 *Pixel = (u32 *)Row;
        for(int X = MinX;
            X < MaxX;
            X++)
        {
            u8 Brightness = FontBitmap[Y*FontWidth+X];
            
            f32 Alpha = ((f32)Brightness/255.0f);
            
#if 0
            f32 R = Lerp(Color.R*255.0f, DR, Alpha);
            f32 G = Lerp(Color.G*255.0f, DG, Alpha);
            f32 B = Lerp(Color.B*255.0f, DB, Alpha);
            f32 A = 255.0f;
#else
            f32 SR = (f32)((Color >> 16) & 0xFF);
            f32 SG = (f32)((Color >> 8) & 0xFF);
            f32 SB = (f32)((Color >> 0) & 0xFF);
            
            f32 R = SR*Alpha;
            f32 G = SG*Alpha;
            f32 B = SB*Alpha;
            f32 A = Brightness;
#endif
            
            
            u32 Value = (((u32)(A) << 24) |
                         ((u32)(R) << 16) |
                         ((u32)(G) << 8) |
                         ((u32)(B) << 0));
            *Pixel = Value;
            
            Pixel += 1;
        }
        
        Row += Buffer->Pitch;
    }
}


#endif //EDITOR_FONT_H