Revision 323
more stuff
font.c | ||
---|---|---|
20 | 20 |
* @param xpm XPM describing the glyph |
21 | 21 |
* @return Pointer to created glyph |
22 | 22 |
*/ |
23 |
static glyph_t* (glyph_ctor)(const char **xpm){ |
|
23 |
static glyph_t* (glyph_ctor)(const char *const *xpm){
|
|
24 | 24 |
if(xpm == NULL) return NULL; |
25 | 25 |
glyph_t *ret = malloc(sizeof(glyph_t)); |
26 | 26 |
if(ret == NULL) return NULL; |
... | ... | |
71 | 71 |
for(int16_t w = 0; w < p->w; ++w){ |
72 | 72 |
uint8_t a = *(p->map + w + h*p->w); |
73 | 73 |
int16_t x_ = x+w, y_ = y-p->h+h; |
74 |
unsigned pos = x_ +y_*W;
|
|
75 |
alp_buf[pos] = a; |
|
74 |
int32_t pos = x_ +y_*W;
|
|
75 |
if(0 <= pos && pos < W*H) alp_buf[pos] = a;
|
|
76 | 76 |
} |
77 | 77 |
} |
78 | 78 |
return SUCCESS; |
... | ... | |
98 | 98 |
for(size_t i = 0; i < ret->nchars; ++i){ |
99 | 99 |
sprintf(filepath, "%s/ascii%03d.xpm2", s, i); |
100 | 100 |
char **xpm = xpm_load_xpm2(filepath); |
101 |
ret->glyphs[i] = glyph_ctor((const char**)xpm);
|
|
101 |
ret->glyphs[i] = glyph_ctor((const char *const *)xpm);
|
|
102 | 102 |
if(ret->glyphs[i] != NULL) good = true; |
103 | 103 |
} |
104 | 104 |
if(good) return ret; |
... | ... | |
137 | 137 |
const font_t *fnt; |
138 | 138 |
char *txt; |
139 | 139 |
int16_t x, y; |
140 |
int size; |
|
141 |
uint32_t color; |
|
140 |
uint16_t size; |
|
142 | 141 |
text_valign valign; |
143 | 142 |
text_halign halign; |
143 |
uint32_t color; |
|
144 | 144 |
}; |
145 | 145 |
text_t* (text_ctor)(const font_t *fnt, const char *txt){ |
146 | 146 |
if(fnt == NULL) return NULL; |
... | ... | |
170 | 170 |
} |
171 | 171 |
const char* (text_get_string)(const text_t *p){return p->txt; } |
172 | 172 |
void (text_set_pos) (text_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; } |
173 |
void (text_set_size) (text_t *p, unsigned size ){ p->size = size ; }
|
|
173 |
void (text_set_size) (text_t *p, uint16_t size ){ p->size = size ; }
|
|
174 | 174 |
void (text_set_color) (text_t *p, uint32_t color ){ p->color = color ; } |
175 | 175 |
void (text_set_valign)(text_t *p, text_valign valign ){ p->valign = valign; } |
176 | 176 |
void (text_set_halign)(text_t *p, text_halign halign ){ p->halign = halign; } |
... | ... | |
186 | 186 |
uint16_t W = 0, H = 0; { |
187 | 187 |
for(size_t i = 0; i < len; ++i){ |
188 | 188 |
const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]]; |
189 |
if(g != NULL){ W += g->w; H = max(H, g->h); }
|
|
189 |
if(g != NULL){ W += g->w; H = umax16(H, g->h); }
|
|
190 | 190 |
} |
191 | 191 |
} |
192 | 192 |
uint8_t *alp_buf = malloc(W*H); |
193 | 193 |
if(alp_buf == NULL) return ALLOC_ERROR;{ |
194 |
int16_t y = H; |
|
194 |
int16_t y = (int16_t)H;
|
|
195 | 195 |
int16_t x = 0; |
196 | 196 |
for(size_t i = 0; i < len; ++i){ |
197 | 197 |
const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]]; |
... | ... | |
204 | 204 |
|
205 | 205 |
double factor = (double)p->size/(double)H; |
206 | 206 |
|
207 |
newH = H*factor;
|
|
208 |
newW = W*factor;
|
|
207 |
newH = (uint16_t)(H*factor);
|
|
208 |
newW = (uint16_t)(W*factor);
|
|
209 | 209 |
alp_new_buf = malloc(newW*newH); |
210 | 210 |
if(alp_new_buf == NULL) return ALLOC_ERROR; |
211 | 211 |
|
212 |
for(size_t newy = 0; newy < newH; ++newy){
|
|
213 |
size_t y = newy/factor;
|
|
214 |
for(size_t newx = 0; newx < newW; ++newx){
|
|
215 |
size_t x = newx/factor;
|
|
212 |
for(uint16_t newy = 0; newy < newH; ++newy){
|
|
213 |
uint16_t y = (uint16_t)(newy/factor);
|
|
214 |
for(uint16_t newx = 0; newx < newW; ++newx){
|
|
215 |
uint16_t x = (uint16_t)(newx/factor);
|
|
216 | 216 |
*(alp_new_buf+newx+newy*newW) = *(alp_buf+x+y*W); |
217 | 217 |
} |
218 | 218 |
} |
... | ... | |
224 | 224 |
case text_halign_left : initx = p->x ; break; |
225 | 225 |
case text_halign_center: initx = p->x - newW/2; break; |
226 | 226 |
case text_halign_right : initx = p->x - newW ; break; |
227 |
default: return LOGIC_ERROR; |
|
227 |
//default: return LOGIC_ERROR;
|
|
228 | 228 |
} |
229 | 229 |
} |
230 | 230 |
// Get initial value of y |
... | ... | |
233 | 233 |
case text_valign_top : inity = p->y ; break; |
234 | 234 |
case text_valign_center: inity = p->y - newH/2; break; |
235 | 235 |
case text_valign_bottom: inity = p->y - newH ; break; |
236 |
default: return LOGIC_ERROR; |
|
236 |
//default: return LOGIC_ERROR;
|
|
237 | 237 |
} |
238 | 238 |
} |
239 | 239 |
// Draw text |
... | ... | |
244 | 244 |
if(!(0 <= x && x < graph_get_XRes() && |
245 | 245 |
0 <= y && y < graph_get_YRes())) continue; |
246 | 246 |
uint8_t a = *(alp_new_buf+newx+newy*newW); |
247 |
if(a < ALPHA_THRESHOLD) graph_set_pixel(x,y,p->color);
|
|
247 |
if(a < ALPHA_THRESHOLD) graph_set_pixel((uint16_t)x,(uint16_t)y,p->color);
|
|
248 | 248 |
} |
249 | 249 |
} |
250 | 250 |
free(alp_new_buf); |
Also available in: Unified diff