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