Project

General

Profile

Revision 188

many changes

View differences:

font.c
6 6
#include "graph.h"
7 7
#include "utils.h"
8 8
#include "errors.h"
9
#include <assert.h>
9 10

  
10 11
struct glyph{
11 12
    uint16_t w, h;
......
93 94
    int16_t x, y;
94 95
    int size;
95 96
    uint32_t color;
97
    enum text_valign valign;
98
    enum text_halign halign;
96 99
};
97 100
text_t* (text_ctor)(const font_t *fnt, const char *txt){
98 101
    if(fnt == NULL) return NULL;
......
103 106
    text_set_text(ret, txt);
104 107
    ret->x = 0;
105 108
    ret->y = 0;
106
    ret->size = 70;
109
    ret->size = 25;
107 110
    ret->color = BLACK;
111
    ret->valign = text_valign_top;
112
    ret->halign = text_halign_left;
108 113
    return ret;
109 114
}
110 115
void (text_dtor)(text_t *p){
......
118 123
    if(p->txt == NULL) return;
119 124
    strcpy(p->txt, txt);
120 125
}
121
void (text_set_pos)  (text_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
122
void (text_set_size) (text_t *p, unsigned size       ){ p->size = size;     }
123
void (text_set_color)(text_t *p, uint32_t color      ){ p->color = color;   }
124
void (text_draw)(const text_t *p){
125
    printf("-%s-\n", p->txt);
126
    const size_t len = strlen(p->txt);
127
    uint16_t W = 0, H = 0;
128
    for(size_t i = 0; i < len; ++i){
129
        const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
130
        if(g != NULL){ W += g->w; H = max(H, g->h); }
131
    }
132
    printf("%d %d\n", W, H);
126
void (text_set_pos)   (text_t *p, int16_t x, int16_t y   ){ p->x = x; p->y = y; }
127
void (text_set_size)  (text_t *p, unsigned size          ){ p->size = size    ; }
128
void (text_set_color) (text_t *p, uint32_t color         ){ p->color = color  ; }
129
void (text_set_valign)(text_t *p, enum text_valign valign){ p->valign = valign; }
130
void (text_set_halign)(text_t *p, enum text_halign halign){ p->halign = halign; }
133 131

  
134
    uint8_t *alp_buf = malloc(W*H);
132
int (text_draw)(const text_t *p){
133
    if(p == NULL) return NULL_PTR;
134
    int ret = SUCCESS;
135
    // Get buffer with rescaled text
136
    uint8_t *alp_new_buf = NULL;
137
    uint16_t newH, newW;{
138
        const size_t len = strlen(p->txt);
139
        uint16_t W = 0, H = 0; {
140
            for(size_t i = 0; i < len; ++i){
141
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
142
                if(g != NULL){ W += g->w; H = max(H, g->h); }
143
            }
144
        }
145
        uint8_t *alp_buf = malloc(W*H);
146
        if(alp_buf == NULL) return ALLOC_ERROR;{
147
            int16_t y = H;
148
            int16_t x = 0;
149
            for(size_t i = 0; i < len; ++i){
150
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
151
                if(g != NULL){
152
                    if((ret = glyph_draw_to_alpha_buffer(g, x, y, alp_buf, W, H))) return ret;
153
                    x += g->w;
154
                }
155
            }
156
        }
135 157

  
136
    {
137
        int16_t y = H;
138
        int16_t x = 0;
139
        for(size_t i = 0; i < len; ++i){
140
            const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
141
            if(g != NULL){
142
                if(glyph_draw_to_alpha_buffer(g, x, y, alp_buf, W, H)) return;
143
                x += g->w;
158
        double factor = (double)p->size/(double)H;
159

  
160
        newH = H*factor;
161
        newW = W*factor;
162
        alp_new_buf = malloc(newW*newH);
163
        if(alp_new_buf == NULL) return ALLOC_ERROR;
164

  
165
        for(size_t newy = 0; newy < newH; ++newy){
166
            size_t y = newy/factor;
167
            for(size_t newx = 0; newx < newW; ++newx){
168
                size_t x = newx/factor;
169
                *(alp_new_buf+newx+newy*newW) = *(alp_buf+x+y*W);
144 170
            }
145 171
        }
146
    }
147
    double factor = (double)p->size/(double)H;
172
        free(alp_buf);
148 173

  
149
    uint16_t newH = H*factor;
150
    uint16_t newW = W*factor;
151 174

  
152
    for(size_t newy = 0; newy < newH; ++newy){
153
        size_t y = newy/factor;
154
        for(size_t newx = 0; newx < newW; ++newx){
155
            size_t x = newx/factor;
156
            *(alp_buf+newx+newy*W) = *(alp_buf+x+y*W);
157
            (void)x;
158
            (void)y;
175
    }
176
    // Get initial value of x
177
    int16_t initx;{
178
        switch(p->halign){
179
            case text_halign_left  : initx = p->x         ; break;
180
            case text_halign_center: initx = p->x - newW/2; break;
181
            case text_halign_right : initx = p->x - newW  ; break;
182
            default: return LOGIC_ERROR;
159 183
        }
160 184
    }
161

  
162
    printf("L163\n");
163
    for(int16_t y = 0; y < newH; ++y){ //printf("L163,%d\n", y);
164
        for(int16_t x = 0; x < newW; ++x){ //if(y > 147) printf("L164, %d %d\n", x, y);
165
            uint8_t a = *(alp_buf+x+y*W); //if(y > 147) printf("L165\n");
166
            graph_set_pixel_alpha(x+p->x,y+p->y,p->color, a); //if(y > 147) printf("L166\n");
185
    // Get initial value of y
186
    int16_t inity;{
187
        switch(p->valign){
188
            case text_valign_top   : inity = p->y         ; break;
189
            case text_valign_center: inity = p->y - newH/2; break;
190
            case text_valign_bottom: inity = p->y - newH  ; break;
191
            default: return LOGIC_ERROR;
167 192
        }
168 193
    }
169
    printf("L170\n");
170
    free(alp_buf);
171
    printf("L172\n");
194
    // Draw text
195
    for(int16_t newy = 0; newy < newH; ++newy){
196
        for(int16_t newx = 0; newx < newW; ++newx){
197
            uint8_t a = *(alp_new_buf+newx+newy*newW);
198
            graph_set_pixel_alpha(initx+newx,inity+newy,p->color, a);
199
        }
200
    }
201
    free(alp_new_buf);
202
    return SUCCESS;
172 203
}

Also available in: Unified diff