Project

General

Profile

Statistics
| Revision:

root / proj / libs / graph / src / font.c @ 362

History | View | Annotate | Download (7.5 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 319 up20180642
/**
12
 * @brief Text glyph. Represents a letter that can be drawn.
13
 */
14 317 up20180642
typedef struct {
15 182 up20180642
    uint16_t w, h;
16
    uint8_t *map;
17 317 up20180642
} glyph_t;
18 319 up20180642
/**
19
 * @brief Construct glyph.
20
 * @param   xpm XPM describing the glyph
21
 * @return      Pointer to created glyph
22
 */
23 323 up20180642
static glyph_t* (glyph_ctor)(const char *const *xpm){
24 183 up20180642
    if(xpm == NULL) return NULL;
25 182 up20180642
    glyph_t *ret = malloc(sizeof(glyph_t));
26
    if(ret == NULL) return NULL;
27
    enum xpm_image_type type = XPM_8_8_8_8;
28
    xpm_image_t img;
29 213 up20180642
    ret->map = NULL;
30
    uint8_t *map = xpm_load((xpm_map_t)xpm, type, &img);
31
    if(map == NULL){
32 182 up20180642
        free(ret);
33
        return NULL;
34
    }
35
    ret->w = img.width;
36
    ret->h = img.height;
37 213 up20180642
    ret->map = malloc(ret->w*ret->h*sizeof(uint8_t));
38
    if(ret->map == NULL){
39
        free(ret);
40
        return NULL;
41
    }
42
    for(unsigned i = 0; i < ret->w*ret->h; ++i){
43
        ret->map[i] = map[4*i+3];
44
    }
45 182 up20180642
    return ret;
46
}
47 319 up20180642
/**
48
 * @brief Destruct glyph.
49
 * @param   p   Pointer to glyph to be destroyed
50
 */
51 183 up20180642
static void (glyph_dtor)(glyph_t *p){
52 182 up20180642
    if(p == NULL) return;
53
    free(p->map);
54
    free(p);
55
}
56 319 up20180642
/**
57
 * @brief Draw glyph to an alpha-buffer.
58
 *
59
 * An alpha-buffer is an array of uint8_t that represents transparencies.
60
 * It proves fit as a tool for drawing text.
61
 * @param   p       Pointer to glyph to be drawn
62
 * @param   x       X-position to draw the glyph
63
 * @param   y       Y-position to draw the glyph
64
 * @param   alp_buf Alpha-buffer to which the glyph will be drawn
65
 * @param   W       Width of the alpha-buffer
66
 * @param   H       Height of the alpha-buffer
67
 */
68 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){
69
    if(p == NULL) return NULL_PTR;
70
    for(int16_t h = 0; h < p->h; ++h){
71
        for(int16_t w = 0; w < p->w; ++w){
72 213 up20180642
            uint8_t a = *(p->map + w + h*p->w);
73 208 up20180642
            int16_t x_ = x+w, y_ = y-p->h+h;
74 323 up20180642
            int32_t pos = x_ +y_*W;
75
            if(0 <= pos && pos < W*H) alp_buf[pos] = a;
76 183 up20180642
        }
77
    }
78
    return SUCCESS;
79
}
80
81 314 up20180642
/// FONT
82 183 up20180642
struct font{
83
    size_t nchars;
84
    glyph_t **glyphs;
85
};
86
font_t* (font_ctor)(const char *s){
87
    font_t *ret = malloc(sizeof(font_t));
88
    if(ret == NULL) return NULL;
89
    ret->nchars = 128;
90
    ret->glyphs = malloc(ret->nchars*sizeof(glyph_t*));
91
    if(ret->glyphs == NULL){
92
        free(ret->glyphs);
93
        free(ret);
94
        return NULL;
95
    }
96 192 up20180642
    int good = false;
97 183 up20180642
    char filepath[1024];
98
    for(size_t i = 0; i < ret->nchars; ++i){
99
        sprintf(filepath, "%s/ascii%03d.xpm2", s, i);
100
        char **xpm = xpm_load_xpm2(filepath);
101 323 up20180642
        ret->glyphs[i] = glyph_ctor((const char *const *)xpm);
102 192 up20180642
        if(ret->glyphs[i] != NULL) good = true;
103 183 up20180642
    }
104 192 up20180642
    if(good) return ret;
105
    else{
106
        //font_dtor(ret);
107
        return NULL;
108
    }
109 183 up20180642
}
110 319 up20180642
void (font_dtor)(font_t *p){
111
    if(p == NULL) return;
112 183 up20180642
    for(size_t i = 0; i < p->nchars; ++i)
113
        glyph_dtor(p->glyphs[i]);
114
    free(p->glyphs);
115
    free(p);
116
}
117
118 314 up20180642
static font_t *consolas     = NULL;
119
static font_t *default_font = NULL;
120 313 up20180642
121
int (font_init)(void){
122
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
123
    if(consolas == NULL) return NULL_PTR;
124
    default_font = consolas;
125
    return SUCCESS;
126
}
127 314 up20180642
const font_t* font_get_default(void){ return default_font; }
128
const font_t* font_get_consolas(void){ return consolas; }
129 319 up20180642
void (font_free)(void){
130
    font_dtor(consolas);
131 313 up20180642
    consolas = NULL;
132
    default_font = NULL;
133
}
134
135 314 up20180642
/// TEXT
136 333 up20180642
#include "text.h"
137
138 183 up20180642
struct text{
139
    const font_t *fnt;
140
    char *txt;
141
    int16_t x, y;
142 323 up20180642
    uint16_t size;
143 314 up20180642
    text_valign valign;
144
    text_halign halign;
145 323 up20180642
    uint32_t color;
146 183 up20180642
};
147
text_t* (text_ctor)(const font_t *fnt, const char *txt){
148
    if(fnt == NULL) return NULL;
149
    text_t *ret = malloc(sizeof(text_t));
150
    if(ret == NULL) return NULL;
151
    ret->fnt = fnt;
152
    ret->txt = NULL;
153 314 up20180642
    text_set_string(ret, txt);
154 183 up20180642
    ret->x = 0;
155
    ret->y = 0;
156 188 up20180642
    ret->size = 25;
157 253 up20180642
    ret->color = GRAPH_BLACK;
158 188 up20180642
    ret->valign = text_valign_top;
159
    ret->halign = text_halign_left;
160 183 up20180642
    return ret;
161
}
162
void (text_dtor)(text_t *p){
163
    if(p == NULL) return;
164
    free(p->txt);
165
    free(p);
166
}
167 314 up20180642
void (text_set_string) (text_t *p, const char *txt){
168 183 up20180642
    size_t sz = strlen(txt);
169
    p->txt = realloc(p->txt, (sz+1)*sizeof(char));
170
    if(p->txt == NULL) return;
171
    strcpy(p->txt, txt);
172
}
173 319 up20180642
const char* (text_get_string)(const text_t *p){return p->txt; }
174 314 up20180642
void (text_set_pos)   (text_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
175 323 up20180642
void (text_set_size)  (text_t *p, uint16_t size       ){ p->size = size    ; }
176 314 up20180642
void (text_set_color) (text_t *p, uint32_t color      ){ p->color = color  ; }
177
void (text_set_valign)(text_t *p, text_valign valign  ){ p->valign = valign; }
178
void (text_set_halign)(text_t *p, text_halign halign  ){ p->halign = halign; }
179 297 up20180642
int16_t (text_get_x)  (const text_t *p){ return p->x; }
180
int16_t (text_get_y)  (const text_t *p){ return p->y; }
181 188 up20180642
int (text_draw)(const text_t *p){
182
    if(p == NULL) return NULL_PTR;
183
    int ret = SUCCESS;
184
    // Get buffer with rescaled text
185
    uint8_t *alp_new_buf = NULL;
186
    uint16_t newH, newW;{
187
        const size_t len = strlen(p->txt);
188
        uint16_t W = 0, H = 0; {
189
            for(size_t i = 0; i < len; ++i){
190
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
191 335 up20180642
                if(g != NULL){ W += g->w; H = max_u16(H, g->h); }
192 188 up20180642
            }
193
        }
194
        uint8_t *alp_buf = malloc(W*H);
195
        if(alp_buf == NULL) return ALLOC_ERROR;{
196 323 up20180642
            int16_t y = (int16_t)H;
197 188 up20180642
            int16_t x = 0;
198
            for(size_t i = 0; i < len; ++i){
199
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
200
                if(g != NULL){
201
                    if((ret = glyph_draw_to_alpha_buffer(g, x, y, alp_buf, W, H))) return ret;
202
                    x += g->w;
203
                }
204
            }
205
        }
206 183 up20180642
207 188 up20180642
        double factor = (double)p->size/(double)H;
208
209 323 up20180642
        newH = (uint16_t)(H*factor);
210
        newW = (uint16_t)(W*factor);
211 188 up20180642
        alp_new_buf = malloc(newW*newH);
212
        if(alp_new_buf == NULL) return ALLOC_ERROR;
213
214 323 up20180642
        for(uint16_t newy = 0; newy < newH; ++newy){
215
            uint16_t y = (uint16_t)(newy/factor);
216
            for(uint16_t newx = 0; newx < newW; ++newx){
217
                uint16_t x = (uint16_t)(newx/factor);
218 188 up20180642
                *(alp_new_buf+newx+newy*newW) = *(alp_buf+x+y*W);
219 183 up20180642
            }
220
        }
221 188 up20180642
        free(alp_buf);
222
    }
223
    // Get initial value of x
224
    int16_t initx;{
225
        switch(p->halign){
226
            case text_halign_left  : initx = p->x         ; break;
227
            case text_halign_center: initx = p->x - newW/2; break;
228
            case text_halign_right : initx = p->x - newW  ; break;
229 323 up20180642
            //default: return LOGIC_ERROR;
230 183 up20180642
        }
231
    }
232 188 up20180642
    // Get initial value of y
233
    int16_t inity;{
234
        switch(p->valign){
235
            case text_valign_top   : inity = p->y         ; break;
236
            case text_valign_center: inity = p->y - newH/2; break;
237
            case text_valign_bottom: inity = p->y - newH  ; break;
238 323 up20180642
            //default: return LOGIC_ERROR;
239 183 up20180642
        }
240
    }
241 188 up20180642
    // Draw text
242 307 up20180642
    for(int16_t x, y, newy = 0; newy < newH; ++newy){
243
        y = inity+newy;
244 188 up20180642
        for(int16_t newx = 0; newx < newW; ++newx){
245 307 up20180642
            x = initx+newx;
246
            if(!(0 <= x && x < graph_get_XRes() &&
247
                 0 <= y && y < graph_get_YRes())) continue;
248 188 up20180642
            uint8_t a = *(alp_new_buf+newx+newy*newW);
249 323 up20180642
            if(a < ALPHA_THRESHOLD) graph_set_pixel((uint16_t)x,(uint16_t)y,p->color);
250 188 up20180642
        }
251
    }
252
    free(alp_new_buf);
253
    return SUCCESS;
254 183 up20180642
}