libdrawtext

changeset 38:49bc37890c38

fixed the glyphmap buffer size bug
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 10:43:03 +0300
parents 49a190d62271
children 096968d8a0bc
files src/font.c
diffstat 1 files changed, 18 insertions(+), 13 deletions(-) [+]
line diff
     1.1 --- a/src/font.c	Wed Apr 16 11:29:13 2014 +0300
     1.2 +++ b/src/font.c	Thu Apr 17 10:43:03 2014 +0300
     1.3 @@ -42,7 +42,8 @@
     1.4  static int init_freetype(void);
     1.5  static void cleanup(void);
     1.6  
     1.7 -static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh);
     1.8 +static void calc_best_size(int total_width, int max_gwidth, int max_gheight,
     1.9 +		int padding, int pow2, int *imgw, int *imgh);
    1.10  static int next_pow2(int x);
    1.11  
    1.12  static FT_Library ft;
    1.13 @@ -207,8 +208,7 @@
    1.14  	int i, j;
    1.15  	int gx, gy;
    1.16  	int padding = 4;
    1.17 -	int total_width = padding;
    1.18 -	int max_height = 0;
    1.19 +	int total_width, max_width, max_height;
    1.20  
    1.21  	FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
    1.22  
    1.23 @@ -227,19 +227,23 @@
    1.24  		return 0;
    1.25  	}
    1.26  
    1.27 +	total_width = padding;
    1.28 +	max_width = max_height = 0;
    1.29 +
    1.30  	for(i=0; i<gmap->crange; i++) {
    1.31 -		int h;
    1.32 +		int w, h;
    1.33  
    1.34  		FT_Load_Char(face, i + cstart, 0);
    1.35 +		w = FTSZ_TO_PIXELS(face->glyph->metrics.width);
    1.36  		h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
    1.37  
    1.38 -		if(h > max_height) {
    1.39 -			max_height = h;
    1.40 -		}
    1.41 -		total_width += FTSZ_TO_PIXELS(face->glyph->metrics.width) + padding;
    1.42 +		if(w > max_width) max_width = w;
    1.43 +		if(h > max_height) max_height = h;
    1.44 +
    1.45 +		total_width += w + padding;
    1.46  	}
    1.47  
    1.48 -	calc_best_size(total_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
    1.49 +	calc_best_size(total_width, max_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
    1.50  
    1.51  	if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
    1.52  		free(gmap->glyphs);
    1.53 @@ -720,7 +724,7 @@
    1.54  }
    1.55  
    1.56  #ifdef USE_FREETYPE
    1.57 -static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
    1.58 +static void calc_best_size(int total_width, int max_gwidth, int max_gheight, int padding, int pow2, int *imgw, int *imgh)
    1.59  {
    1.60  	int xsz, ysz, num_rows;
    1.61  	float aspect;
    1.62 @@ -728,10 +732,11 @@
    1.63  	for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
    1.64  		num_rows = total_width / xsz + 1;
    1.65  
    1.66 -		/* take into account the one extra padding for each row after the first */
    1.67 -		num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
    1.68 +		/* assume worst case, all last glyphs will float to the next line
    1.69 +		 * so let's add extra rows for that. */
    1.70 +		num_rows += (padding + (max_gwidth + padding) * num_rows + xsz - 1) / xsz;
    1.71  
    1.72 -		ysz = num_rows * (max_glyph_height + padding) + padding;
    1.73 +		ysz = num_rows * (max_gheight + padding) + padding;
    1.74  		if(pow2) {
    1.75  			ysz = next_pow2(ysz);
    1.76  		}