Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.69 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 314 up20180642
/// GLYPH
12 317 up20180642
typedef struct {
13 182 up20180642
    uint16_t w, h;
14
    uint8_t *map;
15 317 up20180642
} glyph_t;
16 183 up20180642
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 314 up20180642
/// FONT
59 183 up20180642
struct font{
60
    size_t nchars;
61
    glyph_t **glyphs;
62
};
63
font_t* (font_ctor)(const char *s){
64
    font_t *ret = malloc(sizeof(font_t));
65
    if(ret == NULL) return NULL;
66
    ret->nchars = 128;
67
    ret->glyphs = malloc(ret->nchars*sizeof(glyph_t*));
68
    if(ret->glyphs == NULL){
69
        free(ret->glyphs);
70
        free(ret);
71
        return NULL;
72
    }
73 192 up20180642
    int good = false;
74 183 up20180642
    char filepath[1024];
75
    for(size_t i = 0; i < ret->nchars; ++i){
76
        sprintf(filepath, "%s/ascii%03d.xpm2", s, i);
77
        char **xpm = xpm_load_xpm2(filepath);
78
        ret->glyphs[i] = glyph_ctor((const char**)xpm);
79 192 up20180642
        if(ret->glyphs[i] != NULL) good = true;
80 183 up20180642
    }
81 192 up20180642
    if(good) return ret;
82
    else{
83
        //font_dtor(ret);
84
        return NULL;
85
    }
86 183 up20180642
}
87 313 up20180642
int (font_dtor)(font_t *p){
88
    if(p == NULL) return SUCCESS;
89 183 up20180642
    for(size_t i = 0; i < p->nchars; ++i)
90
        glyph_dtor(p->glyphs[i]);
91
    free(p->glyphs);
92
    free(p);
93 313 up20180642
    return SUCCESS;
94 183 up20180642
}
95
96 314 up20180642
static font_t *consolas     = NULL;
97
static font_t *default_font = NULL;
98 313 up20180642
99
int (font_init)(void){
100
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
101
    if(consolas == NULL) return NULL_PTR;
102
    default_font = consolas;
103
    return SUCCESS;
104
}
105 314 up20180642
const font_t* font_get_default(void){ return default_font; }
106
const font_t* font_get_consolas(void){ return consolas; }
107 313 up20180642
int (font_free)(void){
108
    int r;
109
    if((r = font_dtor(consolas))) return r;
110
    consolas = NULL;
111
    default_font = NULL;
112
    return SUCCESS;
113
}
114
115 314 up20180642
/// TEXT
116 183 up20180642
struct text{
117
    const font_t *fnt;
118
    char *txt;
119
    int16_t x, y;
120
    int size;
121
    uint32_t color;
122 314 up20180642
    text_valign valign;
123
    text_halign halign;
124 183 up20180642
};
125
text_t* (text_ctor)(const font_t *fnt, const char *txt){
126
    if(fnt == NULL) return NULL;
127
    text_t *ret = malloc(sizeof(text_t));
128
    if(ret == NULL) return NULL;
129
    ret->fnt = fnt;
130
    ret->txt = NULL;
131 314 up20180642
    text_set_string(ret, txt);
132 183 up20180642
    ret->x = 0;
133
    ret->y = 0;
134 188 up20180642
    ret->size = 25;
135 253 up20180642
    ret->color = GRAPH_BLACK;
136 188 up20180642
    ret->valign = text_valign_top;
137
    ret->halign = text_halign_left;
138 183 up20180642
    return ret;
139
}
140
void (text_dtor)(text_t *p){
141
    if(p == NULL) return;
142
    free(p->txt);
143
    free(p);
144
}
145 314 up20180642
void (text_set_string) (text_t *p, const char *txt){
146 183 up20180642
    size_t sz = strlen(txt);
147
    p->txt = realloc(p->txt, (sz+1)*sizeof(char));
148
    if(p->txt == NULL) return;
149
    strcpy(p->txt, txt);
150
}
151 250 up20180655
char* (text_get_string)(const text_t *p){return p->txt; }
152 314 up20180642
void (text_set_pos)   (text_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
153
void (text_set_size)  (text_t *p, unsigned size       ){ p->size = size    ; }
154
void (text_set_color) (text_t *p, uint32_t color      ){ p->color = color  ; }
155
void (text_set_valign)(text_t *p, text_valign valign  ){ p->valign = valign; }
156
void (text_set_halign)(text_t *p, text_halign halign  ){ p->halign = halign; }
157 297 up20180642
int16_t (text_get_x)  (const text_t *p){ return p->x; }
158
int16_t (text_get_y)  (const text_t *p){ return p->y; }
159 183 up20180642
160 188 up20180642
int (text_draw)(const text_t *p){
161
    if(p == NULL) return NULL_PTR;
162
    int ret = SUCCESS;
163
    // Get buffer with rescaled text
164
    uint8_t *alp_new_buf = NULL;
165
    uint16_t newH, newW;{
166
        const size_t len = strlen(p->txt);
167
        uint16_t W = 0, H = 0; {
168
            for(size_t i = 0; i < len; ++i){
169
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
170
                if(g != NULL){ W += g->w; H = max(H, g->h); }
171
            }
172
        }
173
        uint8_t *alp_buf = malloc(W*H);
174
        if(alp_buf == NULL) return ALLOC_ERROR;{
175
            int16_t y = H;
176
            int16_t x = 0;
177
            for(size_t i = 0; i < len; ++i){
178
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
179
                if(g != NULL){
180
                    if((ret = glyph_draw_to_alpha_buffer(g, x, y, alp_buf, W, H))) return ret;
181
                    x += g->w;
182
                }
183
            }
184
        }
185 183 up20180642
186 188 up20180642
        double factor = (double)p->size/(double)H;
187
188
        newH = H*factor;
189
        newW = W*factor;
190
        alp_new_buf = malloc(newW*newH);
191
        if(alp_new_buf == NULL) return ALLOC_ERROR;
192
193
        for(size_t newy = 0; newy < newH; ++newy){
194 298 up20180642
            size_t y = newy/factor;
195 188 up20180642
            for(size_t newx = 0; newx < newW; ++newx){
196 298 up20180642
                size_t x = newx/factor;
197 188 up20180642
                *(alp_new_buf+newx+newy*newW) = *(alp_buf+x+y*W);
198 183 up20180642
            }
199
        }
200 188 up20180642
        free(alp_buf);
201
    }
202
    // Get initial value of x
203
    int16_t initx;{
204
        switch(p->halign){
205
            case text_halign_left  : initx = p->x         ; break;
206
            case text_halign_center: initx = p->x - newW/2; break;
207
            case text_halign_right : initx = p->x - newW  ; break;
208
            default: return LOGIC_ERROR;
209 183 up20180642
        }
210
    }
211 188 up20180642
    // Get initial value of y
212
    int16_t inity;{
213
        switch(p->valign){
214
            case text_valign_top   : inity = p->y         ; break;
215
            case text_valign_center: inity = p->y - newH/2; break;
216
            case text_valign_bottom: inity = p->y - newH  ; break;
217
            default: return LOGIC_ERROR;
218 183 up20180642
        }
219
    }
220 188 up20180642
    // Draw text
221 307 up20180642
    for(int16_t x, y, newy = 0; newy < newH; ++newy){
222
        y = inity+newy;
223 188 up20180642
        for(int16_t newx = 0; newx < newW; ++newx){
224 307 up20180642
            x = initx+newx;
225
            if(!(0 <= x && x < graph_get_XRes() &&
226
                 0 <= y && y < graph_get_YRes())) continue;
227 188 up20180642
            uint8_t a = *(alp_new_buf+newx+newy*newW);
228 307 up20180642
            if(a < ALPHA_THRESHOLD) graph_set_pixel(x,y,p->color);
229 188 up20180642
        }
230
    }
231
    free(alp_new_buf);
232
    return SUCCESS;
233 183 up20180642
}