Project

General

Profile

Statistics
| Revision:

root / proj / src / font.c @ 218

History | View | Annotate | Download (5.88 KB)

1 182 up20180642
#include <lcom/lcf.h>
2
3
#include "font.h"
4
5 183 up20180642
#include "xpm_utils.h"
6
#include "graph.h"
7
#include "utils.h"
8
#include "errors.h"
9 188 up20180642
#include <assert.h>
10 183 up20180642
11 182 up20180642
struct glyph{
12
    uint16_t w, h;
13
    uint8_t *map;
14
};
15 183 up20180642
typedef struct glyph glyph_t;
16
static glyph_t* (glyph_ctor)(const char **xpm){
17
    if(xpm == NULL) return NULL;
18 182 up20180642
    glyph_t *ret = malloc(sizeof(glyph_t));
19
    if(ret == NULL) return NULL;
20
    enum xpm_image_type type = XPM_8_8_8_8;
21
    xpm_image_t img;
22 213 up20180642
    ret->map = NULL;
23
    uint8_t *map = xpm_load((xpm_map_t)xpm, type, &img);
24
    if(map == NULL){
25 182 up20180642
        free(ret);
26
        return NULL;
27
    }
28
    ret->w = img.width;
29
    ret->h = img.height;
30 213 up20180642
    ret->map = malloc(ret->w*ret->h*sizeof(uint8_t));
31
    if(ret->map == NULL){
32
        free(ret);
33
        return NULL;
34
    }
35
    for(unsigned i = 0; i < ret->w*ret->h; ++i){
36
        ret->map[i] = map[4*i+3];
37
    }
38 182 up20180642
    return ret;
39
}
40 183 up20180642
static void (glyph_dtor)(glyph_t *p){
41 182 up20180642
    if(p == NULL) return;
42
    free(p->map);
43
    free(p);
44
}
45 183 up20180642
static int (glyph_draw_to_alpha_buffer)(const glyph_t *p, int16_t x, int16_t y, uint8_t *alp_buf, uint16_t W, uint16_t H){
46
    if(p == NULL) return NULL_PTR;
47
    for(int16_t h = 0; h < p->h; ++h){
48
        for(int16_t w = 0; w < p->w; ++w){
49 213 up20180642
            uint8_t a = *(p->map + w + h*p->w);
50 208 up20180642
            int16_t x_ = x+w, y_ = y-p->h+h;
51
            unsigned pos = x_ +y_*W;
52
            alp_buf[pos] = a;
53 183 up20180642
        }
54
    }
55
    return SUCCESS;
56
}
57
58
struct font{
59
    size_t nchars;
60
    glyph_t **glyphs;
61
};
62
#include "sprite.h"
63
#include "rectangle.h"
64
font_t* (font_ctor)(const char *s){
65
    font_t *ret = malloc(sizeof(font_t));
66
    if(ret == NULL) return NULL;
67
    ret->nchars = 128;
68
    ret->glyphs = malloc(ret->nchars*sizeof(glyph_t*));
69
    if(ret->glyphs == NULL){
70
        free(ret->glyphs);
71
        free(ret);
72
        return NULL;
73
    }
74 192 up20180642
    int good = false;
75 183 up20180642
    char filepath[1024];
76
    for(size_t i = 0; i < ret->nchars; ++i){
77
        sprintf(filepath, "%s/ascii%03d.xpm2", s, i);
78
        char **xpm = xpm_load_xpm2(filepath);
79
        ret->glyphs[i] = glyph_ctor((const char**)xpm);
80 192 up20180642
        if(ret->glyphs[i] != NULL) good = true;
81 183 up20180642
    }
82 192 up20180642
    if(good) return ret;
83
    else{
84
        //font_dtor(ret);
85
        return NULL;
86
    }
87 183 up20180642
}
88
void (font_dtor)(font_t *p){
89
    if(p == NULL) return;
90
    for(size_t i = 0; i < p->nchars; ++i)
91
        glyph_dtor(p->glyphs[i]);
92
    free(p->glyphs);
93
    free(p);
94
}
95
96
struct text{
97
    const font_t *fnt;
98
    char *txt;
99
    int16_t x, y;
100
    int size;
101
    uint32_t color;
102 188 up20180642
    enum text_valign valign;
103
    enum text_halign halign;
104 183 up20180642
};
105
text_t* (text_ctor)(const font_t *fnt, const char *txt){
106
    if(fnt == NULL) return NULL;
107
    text_t *ret = malloc(sizeof(text_t));
108
    if(ret == NULL) return NULL;
109
    ret->fnt = fnt;
110
    ret->txt = NULL;
111
    text_set_text(ret, txt);
112
    ret->x = 0;
113
    ret->y = 0;
114 188 up20180642
    ret->size = 25;
115 183 up20180642
    ret->color = BLACK;
116 188 up20180642
    ret->valign = text_valign_top;
117
    ret->halign = text_halign_left;
118 183 up20180642
    return ret;
119
}
120
void (text_dtor)(text_t *p){
121
    if(p == NULL) return;
122
    free(p->txt);
123
    free(p);
124
}
125
void (text_set_text) (text_t *p, const char *txt){
126
    size_t sz = strlen(txt);
127
    p->txt = realloc(p->txt, (sz+1)*sizeof(char));
128
    if(p->txt == NULL) return;
129
    strcpy(p->txt, txt);
130
}
131 188 up20180642
void (text_set_pos)   (text_t *p, int16_t x, int16_t y   ){ p->x = x; p->y = y; }
132
void (text_set_size)  (text_t *p, unsigned size          ){ p->size = size    ; }
133
void (text_set_color) (text_t *p, uint32_t color         ){ p->color = color  ; }
134
void (text_set_valign)(text_t *p, enum text_valign valign){ p->valign = valign; }
135
void (text_set_halign)(text_t *p, enum text_halign halign){ p->halign = halign; }
136 183 up20180642
137 188 up20180642
int (text_draw)(const text_t *p){
138
    if(p == NULL) return NULL_PTR;
139
    int ret = SUCCESS;
140
    // Get buffer with rescaled text
141
    uint8_t *alp_new_buf = NULL;
142
    uint16_t newH, newW;{
143
        const size_t len = strlen(p->txt);
144
        uint16_t W = 0, H = 0; {
145
            for(size_t i = 0; i < len; ++i){
146
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
147
                if(g != NULL){ W += g->w; H = max(H, g->h); }
148
            }
149
        }
150
        uint8_t *alp_buf = malloc(W*H);
151
        if(alp_buf == NULL) return ALLOC_ERROR;{
152
            int16_t y = H;
153
            int16_t x = 0;
154
            for(size_t i = 0; i < len; ++i){
155
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
156
                if(g != NULL){
157
                    if((ret = glyph_draw_to_alpha_buffer(g, x, y, alp_buf, W, H))) return ret;
158
                    x += g->w;
159
                }
160
            }
161
        }
162 183 up20180642
163 188 up20180642
        double factor = (double)p->size/(double)H;
164
165
        newH = H*factor;
166
        newW = W*factor;
167
        alp_new_buf = malloc(newW*newH);
168
        if(alp_new_buf == NULL) return ALLOC_ERROR;
169
170
        for(size_t newy = 0; newy < newH; ++newy){
171
            size_t y = newy/factor;
172
            for(size_t newx = 0; newx < newW; ++newx){
173
                size_t x = newx/factor;
174
                *(alp_new_buf+newx+newy*newW) = *(alp_buf+x+y*W);
175 183 up20180642
            }
176
        }
177 188 up20180642
        free(alp_buf);
178 183 up20180642
179
180 188 up20180642
    }
181
    // Get initial value of x
182
    int16_t initx;{
183
        switch(p->halign){
184
            case text_halign_left  : initx = p->x         ; break;
185
            case text_halign_center: initx = p->x - newW/2; break;
186
            case text_halign_right : initx = p->x - newW  ; break;
187
            default: return LOGIC_ERROR;
188 183 up20180642
        }
189
    }
190 188 up20180642
    // Get initial value of y
191
    int16_t inity;{
192
        switch(p->valign){
193
            case text_valign_top   : inity = p->y         ; break;
194
            case text_valign_center: inity = p->y - newH/2; break;
195
            case text_valign_bottom: inity = p->y - newH  ; break;
196
            default: return LOGIC_ERROR;
197 183 up20180642
        }
198
    }
199 188 up20180642
    // Draw text
200
    for(int16_t newy = 0; newy < newH; ++newy){
201
        for(int16_t newx = 0; newx < newW; ++newx){
202
            uint8_t a = *(alp_new_buf+newx+newy*newW);
203 207 up20180642
            if(a < 0x7F) graph_set_pixel(initx+newx,inity+newy,p->color);
204 188 up20180642
        }
205
    }
206
    free(alp_new_buf);
207
    return SUCCESS;
208 183 up20180642
}