2 libdrawtext - a simple library for fast text rendering in OpenGL
3 Copyright (C) 2011-2018 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include FT_FREETYPE_H
37 #include "drawtext_impl.h"
43 int (*readchar)(struct io*);
44 void *(*readline)(void *buf, int bsz, struct io*);
47 #define FTSZ_TO_PIXELS(x) ((x) / 64)
48 #define MAX_IMG_SIZE 8192
50 static int opt_padding = 8;
51 static int opt_save_ppm;
53 static struct dtx_glyphmap *load_glyphmap(struct io *io);
56 static int init_freetype(void);
57 static void cleanup(void);
59 static int calc_best_size(int total_width, int max_gwidth, int max_gheight,
60 int padding, int pow2, int *imgw, int *imgh);
61 static int next_pow2(int x);
68 static int init_freetype(void)
71 if(FT_Init_FreeType(&ft) != 0) {
80 static void cleanup(void)
86 #endif /* USE_FREETYPE */
88 static int find_pow2(int x);
90 struct dtx_font *dtx_open_font(const char *fname, int sz)
92 struct dtx_font *fnt = 0;
97 if(!(fnt = calloc(1, sizeof *fnt))) {
98 fperror("failed to allocate font structure");
102 if(FT_New_Face(ft, fname, 0, (FT_Face*)&fnt->face) != 0) {
103 fprintf(stderr, "failed to open font file: %s\n", fname);
108 /* pre-create the extended ASCII range glyphmap */
110 dtx_prepare_range(fnt, sz, 0, 256);
113 dtx_use_font(fnt, sz);
117 fprintf(stderr, "ignoring call to dtx_open_font: not compiled with freetype support!\n");
123 struct dtx_font *dtx_open_font_mem(void *ptr, int memsz, int fontsz)
125 struct dtx_font *fnt = 0;
132 if(!(fnt = calloc(1, sizeof *fnt))) {
133 fperror("failed to allocate font structure");
137 memset(&args, 0, sizeof args);
138 args.flags = FT_OPEN_MEMORY;
139 args.memory_base = ptr;
140 args.memory_size = memsz;
142 if(FT_Open_Face(ft, &args, 0, (FT_Face*)&fnt->face) != 0) {
143 fprintf(stderr, "failed to open font from memory\n");
148 /* pre-create the extended ASCII range glyphmap */
150 dtx_prepare_range(fnt, fontsz, 0, 256);
153 dtx_use_font(fnt, fontsz);
157 fprintf(stderr, "ignoring call to dtx_open_font_mem: not compiled with freetype support!\n");
163 struct dtx_font *dtx_open_font_glyphmap(const char *fname)
165 struct dtx_font *fnt;
166 struct dtx_glyphmap *gmap;
168 if(!(fnt = calloc(1, sizeof *fnt))) {
169 fperror("failed to allocate font structure");
174 if(!(gmap = dtx_load_glyphmap(fname))) {
179 dtx_add_glyphmap(fnt, gmap);
182 dtx_use_font(fnt, gmap->ptsize);
188 struct dtx_font *dtx_open_font_glyphmap_mem(void *ptr, int memsz)
190 struct dtx_font *fnt;
191 struct dtx_glyphmap *gmap;
193 if(!(fnt = calloc(1, sizeof *fnt))) {
194 fperror("failed to allocate font structure");
198 if(!(gmap = dtx_load_glyphmap_mem(ptr, memsz))) {
203 dtx_add_glyphmap(fnt, gmap);
206 dtx_use_font(fnt, gmap->ptsize);
211 void dtx_close_font(struct dtx_font *fnt)
216 FT_Done_Face(fnt->face);
219 /* destroy the glyphmaps */
221 void *tmp = fnt->gmaps;
222 fnt->gmaps = fnt->gmaps->next;
223 dtx_free_glyphmap(tmp);
229 void dtx_prepare(struct dtx_font *fnt, int sz)
231 if(!dtx_get_font_glyphmap_range(fnt, sz, 0, 256)) {
232 fprintf(stderr, "%s: failed (sz: %d, range: 0-255 [ascii])\n", __func__, sz);
236 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
238 if(!dtx_get_font_glyphmap_range(fnt, sz, cstart, cend)) {
239 fprintf(stderr, "%s: failed (sz: %d, range: %d-%d)\n", __func__, sz, cstart, cend);
243 int dtx_calc_font_distfield(struct dtx_font *fnt, int scale_numer, int scale_denom)
245 struct dtx_glyphmap *gm = fnt->gmaps;
247 if(dtx_calc_glyphmap_distfield(gm) == -1) {
248 fprintf(stderr, "%s failed to create distfield glyphmap\n", __func__);
252 if(dtx_resize_glyphmap(gm, scale_numer, scale_denom, DTX_LINEAR) == -1) {
253 fprintf(stderr, "%s: failed to resize glyhphmap during distfield conversion\n", __func__);
261 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
263 struct dtx_glyphmap *gm;
265 /* check to see if the last we've given out fits the bill */
266 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
267 return fnt->last_gmap;
270 /* otherwise search for the appropriate glyphmap */
273 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
282 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
284 struct dtx_glyphmap *gm;
286 /* search the available glyphmaps to see if we've got one that includes
287 * the requested range
291 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
297 /* not found, create one and add it to the list */
298 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
304 int dtx_get_num_glyphmaps(struct dtx_font *fnt)
307 struct dtx_glyphmap *gm = fnt->gmaps;
315 struct dtx_glyphmap *dtx_get_glyphmap(struct dtx_font *fnt, int idx)
317 struct dtx_glyphmap *gm = fnt->gmaps;
324 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
326 struct dtx_glyphmap *gmap = 0;
329 FT_Face face = fnt->face;
332 int total_width, max_width, max_height;
333 int half_pad = opt_padding / 2;
335 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
337 if(!(gmap = calloc(1, sizeof *gmap))) {
342 gmap->cstart = cstart;
344 gmap->crange = cend - cstart;
345 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
346 gmap->baseline = -FTSZ_TO_PIXELS((float)face->size->metrics.descender);
348 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
353 total_width = opt_padding;
354 max_width = max_height = 0;
356 for(i=0; i<gmap->crange; i++) {
359 FT_Load_Char(face, i + cstart, 0);
360 w = FTSZ_TO_PIXELS(face->glyph->metrics.width);
361 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
363 if(w > max_width) max_width = w;
364 if(h > max_height) max_height = h;
366 total_width += w + opt_padding;
369 if(calc_best_size(total_width, max_width, max_height, opt_padding, 1, &gmap->xsz, &gmap->ysz) == -1) {
374 gmap->xsz_shift = find_pow2(gmap->xsz);
376 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
381 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
386 for(i=0; i<gmap->crange; i++) {
387 float gwidth, gheight;
388 unsigned char *src, *dst;
391 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
393 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
394 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
396 if(gx > gmap->xsz - gwidth - opt_padding) {
398 gy += max_height + opt_padding;
401 src = glyph->bitmap.buffer;
402 dst = gmap->pixels + (gy << gmap->xsz_shift) + gx;
404 for(j=0; j<(int)glyph->bitmap.rows; j++) {
405 memcpy(dst, src, glyph->bitmap.width);
407 src += glyph->bitmap.pitch;
410 gmap->glyphs[i].code = i;
411 gmap->glyphs[i].x = gx - half_pad;
412 gmap->glyphs[i].y = gy - half_pad;
413 gmap->glyphs[i].width = gwidth + half_pad * 2;
414 gmap->glyphs[i].height = gheight + half_pad * 2;
415 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
416 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
417 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
418 /* also precalc normalized */
419 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
420 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
421 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
422 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
424 gx += gwidth + opt_padding;
427 /* add it to the glyphmaps list of the font */
428 dtx_add_glyphmap(fnt, gmap);
429 #endif /* USE_FREETYPE */
434 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
443 #define CHECK_BOUNDS(gm, x, y) ((x) >= 0 && (x) < (gm)->xsz && (y) >= 0 && (y) < (gm)->ysz)
444 #define GET_PIXEL(gm, x, y) ((gm)->pixels[((y) << (gm)->xsz_shift) + (x)])
446 static int calc_distance(struct dtx_glyphmap *gmap, int x, int y, int max_dist)
448 int i, j, startx, starty, endx, endy, px, py;
450 int min_distsq = INT_MAX;
451 unsigned char cpix = GET_PIXEL(gmap, x, y);
454 if(max_dist > 128) max_dist = 128;
456 startx = x >= max_dist ? x - max_dist : 0;
457 starty = y >= max_dist ? y - max_dist : 0;
458 endx = x + max_dist < gmap->xsz ? x + max_dist : gmap->xsz - 1;
459 endy = y + max_dist < gmap->ysz ? y + max_dist : gmap->ysz - 1;
461 /* try the cardinal directions first to find the search bounding box */
463 int max_dist = x - startx;
464 for(j=0; j<max_dist; j++) {
465 if(GET_PIXEL(gmap, x - j, y) != cpix) {
470 max_dist = endx + 1 - x;
471 for(j=0; j<max_dist; j++) {
472 if(GET_PIXEL(gmap, x + j, y) != cpix) {
477 max_dist = y - starty;
478 for(j=0; j<max_dist; j++) {
479 if(GET_PIXEL(gmap, x, y - j) != cpix) {
484 max_dist = endy + 1 - y;
485 for(j=0; j<max_dist; j++) {
486 if(GET_PIXEL(gmap, x, y + j) != cpix) {
493 /* find the minimum squared distance inside the bounding box */
494 bwidth = endx + 1 - startx;
495 bheight = endy + 1 - starty;
498 for(i=0; i<bheight; i++) {
500 for(j=0; j<bwidth; j++) {
501 if(GET_PIXEL(gmap, px, py) != cpix) {
504 int distsq = dx * dx + dy * dy;
506 if(distsq < min_distsq) {
515 dist = (int)sqrt(min_distsq);
516 if(dist > 127) dist = 127;
518 return cpix ? dist + 128 : 127 - dist;
521 struct distcalc_data {
522 struct dtx_glyphmap *gmap;
524 unsigned char *pixels;
527 static void distcalc_func(void *cls)
530 struct distcalc_data *data = cls;
531 struct dtx_glyphmap *gmap = data->gmap;
533 printf("scanline %d of %d\n", data->scanline + 1, gmap->ysz);
534 for(i=0; i<gmap->xsz; i++) {
535 *data->pixels++ = calc_distance(gmap, i, data->scanline, 64);
539 int dtx_calc_glyphmap_distfield(struct dtx_glyphmap *gmap)
541 int i, num_pixels = gmap->xsz * gmap->ysz;
542 unsigned char *new_pixels;
545 struct dtx_thread_pool *tpool = 0;
546 struct distcalc_data *data = 0;
549 /* first quantize the glyphmap to 1bit */
551 for(i=0; i<num_pixels; i++) {
552 unsigned char c = *dptr;
553 *dptr++ = c < 128 ? 0 : 255;
556 if(!(new_pixels = malloc(num_pixels))) {
557 fprintf(stderr, "%s: failed to allocate %dx%d pixel buffer\n", __func__, gmap->xsz, gmap->ysz);
563 tpool = dtx_tpool_create(0);
564 data = malloc(sizeof *data * gmap->ysz);
567 for(i=0; i<gmap->ysz; i++) {
569 data[i].scanline = i;
570 data[i].pixels = new_pixels + (i << gmap->xsz_shift);
571 dtx_tpool_enqueue(tpool, data + i, distcalc_func, 0);
573 dtx_tpool_wait(tpool);
574 dtx_tpool_destroy(tpool);
577 #endif /* USE_THREADS */
579 for(i=0; i<gmap->ysz; i++) {
580 struct distcalc_data d;
583 d.pixels = new_pixels + (i << gmap->xsz_shift);
589 gmap->pixels = new_pixels;
593 static unsigned char sample_area(struct dtx_glyphmap *gm, float x, float y, float area)
596 int ksz = (int)(area + 0.5);
597 int half_ksz = ksz / 2;
599 int sum = 0, nsamples = 0;
601 for(i=0; i<ksz; i++) {
602 for(j=0; j<ksz; j++) {
603 int sx = x + j - half_ksz;
604 int sy = y + i - half_ksz;
606 if(sx < 0 || sx >= gm->xsz || sy < 0 || sy >= gm->ysz) {
610 sum += gm->pixels[(sy << gm->xsz_shift) + sx];
618 return sum > 255 ? 255 : sum;
621 static unsigned char sample_pixel(struct dtx_glyphmap *gm, int x, int y)
623 if(CHECK_BOUNDS(gm, x, y)) {
624 return gm->pixels[(y << gm->xsz_shift) + x];
629 static int count_bits(int x)
632 for(i=0; i<sizeof x * CHAR_BIT; i++) {
639 int dtx_resize_glyphmap(struct dtx_glyphmap *gmap, int snum, int sdenom, int filter)
641 int i, j, nxsz, nysz;
642 unsigned char *dptr, *new_pixels;
643 float scale, inv_scale, area;
645 if(snum == sdenom) return 0;
647 if((count_bits(snum) | count_bits(sdenom)) != 1) {
648 fprintf(stderr, "%s: invalid scale fraction %d/%d (not power of 2)\n", __func__, snum, sdenom);
652 /* normalize the fraction */
661 if(snum != 1 && sdenom != 1) {
662 fprintf(stderr, "%s: invalid scale fraction %d/%d (neither is 1)\n", __func__, snum, sdenom);
666 nxsz = snum * gmap->xsz / sdenom;
667 nysz = snum * gmap->ysz / sdenom;
669 if(nxsz < 1 || nysz < 1) {
673 new_pixels = malloc(nxsz * nysz);
675 fprintf(stderr, "%s: failed to allocate %dx%d pixel buffer\n", __func__, nxsz, nysz);
681 scale = (float)snum / (float)sdenom;
682 inv_scale = 1.0 / scale;
683 area = scale <= 1.0 ? inv_scale : 2.0;
685 if(filter == DTX_NEAREST) {
686 /* no filtering, nearest neighbor */
687 for(i=0; i<nysz; i++) {
688 for(j=0; j<nxsz; j++) {
689 *dptr++ = sample_pixel(gmap, j * inv_scale, i * inv_scale);
693 /* bilinear filtering */
694 for(i=0; i<nysz; i++) {
695 for(j=0; j<nxsz; j++) {
696 *dptr++ = sample_area(gmap, j * inv_scale, i * inv_scale, area);
702 gmap->pixels = new_pixels;
705 gmap->xsz_shift = find_pow2(nxsz);
707 /* also scale all the metrics accordingly */
708 for(i=0; i<gmap->crange; i++) {
709 struct glyph *g = gmap->glyphs + i;
718 gmap->ptsize = snum * gmap->ptsize / sdenom;
719 gmap->line_advance *= scale;
723 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
728 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
733 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
738 int dtx_get_glyphmap_ptsize(struct dtx_glyphmap *gmap)
743 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
746 struct dtx_glyphmap *gmap;
748 if(!(fp = fopen(fname, "rb"))) {
751 gmap = dtx_load_glyphmap_stream(fp);
757 static int file_readchar(struct io *io)
759 return fgetc(io->data);
762 static void *file_readline(void *buf, int bsz, struct io *io)
764 return fgets(buf, bsz, io->data);
767 static int mem_readchar(struct io *io)
771 if(io->size-- <= 0) {
778 static void *mem_readline(void *buf, int bsz, struct io *io)
783 while(--bsz > 0 && (c = mem_readchar(io)) != -1) {
792 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
796 io.readchar = file_readchar;
797 io.readline = file_readline;
798 return load_glyphmap(&io);
801 struct dtx_glyphmap *dtx_load_glyphmap_mem(void *ptr, int memsz)
806 io.readchar = mem_readchar;
807 io.readline = mem_readline;
808 return load_glyphmap(&io);
811 static struct dtx_glyphmap *load_glyphmap(struct io *io)
815 struct dtx_glyphmap *gmap;
816 struct glyph *glyphs = 0;
818 int min_code = INT_MAX;
819 int max_code = INT_MIN;
820 int i, max_pixval = 255, num_pixels;
823 if(!(gmap = calloc(1, sizeof *gmap))) {
824 fperror("failed to allocate glyphmap");
828 gmap->line_advance = FLT_MIN;
830 while(hdr_lines < 3) {
832 if(!io->readline(buf, sizeof buf, io)) {
833 fperror("unexpected end of file");
837 while(isspace(*line)) {
843 float x, y, xsz, ysz, orig_x, orig_y, adv, line_adv;
846 if((res = sscanf(line + 1, " size: %d\n", &ptsize)) == 1) {
847 gmap->ptsize = ptsize;
849 } else if((res = sscanf(line + 1, " advance: %f\n", &line_adv)) == 1) {
850 gmap->line_advance = line_adv;
852 } else if((res = sscanf(line + 1, " %d: %fx%f+%f+%f o:%f,%f adv:%f\n",
853 &c, &xsz, &ysz, &x, &y, &orig_x, &orig_y, &adv)) == 8) {
854 if(!(g = malloc(sizeof *g))) {
855 fperror("failed to allocate glyph");
866 /* normalized coordinates will be precalculated after everything is loaded */
879 fprintf(stderr, "%s: invalid glyph info line\n", __func__);
886 if(line[0] != 'P' || !(line[1] == '6' || line[1] == '5')) {
887 fprintf(stderr, "%s: invalid file format (magic)\n", __func__);
890 greyscale = line[1] == '5';
894 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
895 fprintf(stderr, "%s: invalid file format (dim)\n", __func__);
903 max_pixval = strtol(line, &endp, 10);
905 fprintf(stderr, "%s: invalid file format (maxval)\n", __func__);
912 break; /* can't happen */
918 if(gmap->ptsize == -1 || gmap->line_advance == FLT_MIN) {
919 fprintf(stderr, "%s: invalid glyphmap, insufficient information in ppm comments\n", __func__);
923 /* precalculate normalized glyph coordinates */
926 g->nx = g->x / gmap->xsz;
927 g->ny = g->y / gmap->ysz;
928 g->nwidth = g->width / gmap->xsz;
929 g->nheight = g->height / gmap->ysz;
933 num_pixels = gmap->xsz * gmap->ysz;
934 if(!(gmap->pixels = malloc(num_pixels))) {
935 fperror("failed to allocate pixels");
939 for(i=0; i<num_pixels; i++) {
940 long c = io->readchar(io);
942 fprintf(stderr, "unexpected end of file while reading pixels\n");
945 gmap->pixels[i] = 255 * c / max_pixval;
952 gmap->xsz_shift = find_pow2(gmap->xsz);
953 gmap->cstart = min_code;
954 gmap->cend = max_code + 1;
955 gmap->crange = gmap->cend - gmap->cstart;
957 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
958 fperror("failed to allocate glyph info");
963 struct glyph *g = glyphs;
964 glyphs = glyphs->next;
966 gmap->glyphs[g->code - gmap->cstart] = *g;
972 dtx_free_glyphmap(gmap);
975 glyphs = glyphs->next;
981 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
986 if(!(fp = fopen(fname, "wb"))) {
987 fprintf(stderr, "%s: failed to open file: %s: %s\n", __func__, fname, strerror(errno));
990 res = dtx_save_glyphmap_stream(fp, gmap);
995 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
998 struct glyph *g = gmap->glyphs;
1000 fprintf(fp, "P%d\n%d %d\n", opt_save_ppm ? 6 : 5, gmap->xsz, gmap->ysz);
1001 fprintf(fp, "# size: %d\n", gmap->ptsize);
1002 fprintf(fp, "# advance: %g\n", gmap->line_advance);
1003 for(i=0; i<gmap->crange; i++) {
1004 fprintf(fp, "# %d: %gx%g+%g+%g o:%g,%g adv:%g\n", g->code + gmap->cstart,
1005 g->width, g->height, g->x, g->y, g->orig_x, g->orig_y, g->advance);
1008 fprintf(fp, "255\n");
1010 num_pixels = gmap->xsz * gmap->ysz;
1011 for(i=0; i<num_pixels; i++) {
1012 int c = gmap->pixels[i];
1022 void dtx_add_glyphmap(struct dtx_font *fnt, struct dtx_glyphmap *gmap)
1024 gmap->next = fnt->gmaps;
1029 void dtx_set(enum dtx_option opt, int val)
1041 dtx_gl_setopt(opt, val);
1042 dtx_rast_setopt(opt, val);
1046 int dtx_get(enum dtx_option opt)
1055 return opt_save_ppm;
1061 if(dtx_gl_getopt(opt, &val) != -1) {
1064 if(dtx_rast_getopt(opt, &val) != -1) {
1070 void dtx_use_font(struct dtx_font *fnt, int sz)
1073 dtx_target_opengl();
1080 float dtx_line_height(void)
1082 struct dtx_glyphmap *gmap = dtx_get_glyphmap(dtx_font, 0);
1084 return gmap->line_advance;
1087 float dtx_baseline(void)
1089 struct dtx_glyphmap *gmap = dtx_get_glyphmap(dtx_font, 0);
1091 return gmap->baseline;
1094 void dtx_glyph_box(int code, struct dtx_box *box)
1097 struct dtx_glyphmap *gmap;
1098 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
1100 cidx = code - gmap->cstart;
1102 box->x = gmap->glyphs[cidx].orig_x;
1103 box->y = gmap->glyphs[cidx].orig_y;
1104 box->width = gmap->glyphs[cidx].width;
1105 box->height = gmap->glyphs[cidx].height;
1108 float dtx_glyph_width(int code)
1111 dtx_glyph_box(code, &box);
1115 float dtx_glyph_height(int code)
1118 dtx_glyph_box(code, &box);
1122 void dtx_string_box(const char *str, struct dtx_box *box)
1124 dtx_substring_box(str, 0, INT_MAX, box);
1127 void dtx_substring_box(const char *str, int start, int end, struct dtx_box *box)
1130 float pos_x = 0.0f, pos_y = 0.0f;
1131 struct glyph *g = 0;
1132 float x0, y0, x1, y1;
1137 /* skip start characters */
1138 while(*str && start > 0) {
1139 str = dtx_utf8_next_char((char*)str);
1144 while(*str && --end >= 0) {
1146 struct dtx_glyphmap *gmap;
1148 code = dtx_utf8_char_code(str);
1149 str = dtx_utf8_next_char((char*)str);
1154 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
1155 g = gmap->glyphs + code - gmap->cstart;
1157 if(px + g->orig_x < x0) {
1158 x0 = px + g->orig_x;
1160 if(py - g->orig_y < y0) {
1161 y0 = py - g->orig_y;
1163 if(px + g->orig_x + g->width > x1) {
1164 x1 = px + g->orig_x + g->width;
1166 if(py - g->orig_y + g->height > y1) {
1167 y1 = py - g->orig_y + g->height;
1174 box->width = x1 - x0;
1175 box->height = y1 - y0;
1178 float dtx_string_width(const char *str)
1182 dtx_string_box(str, &box);
1186 float dtx_string_height(const char *str)
1190 dtx_string_box(str, &box);
1194 float dtx_char_pos(const char *str, int n)
1198 struct dtx_glyphmap *gmap;
1200 for(i=0; i<n; i++) {
1203 code = dtx_utf8_char_code(str);
1204 str = dtx_utf8_next_char((char*)str);
1206 if((gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
1207 pos += gmap->glyphs[code - gmap->cstart].advance;
1213 int dtx_char_at_pt(const char *str, float pt)
1216 float prev_pos = 0.0f, pos = 0.0f;
1217 struct dtx_glyphmap *gmap;
1219 for(i=0; *str; i++) {
1220 int code = dtx_utf8_char_code(str);
1221 str = dtx_utf8_next_char((char*)str);
1223 if((gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
1224 pos += gmap->glyphs[code - gmap->cstart].advance;
1226 if(fabs(pt - prev_pos) < fabs(pt - pos)) {
1235 struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
1237 struct dtx_glyphmap *gmap;
1238 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
1244 *ypos -= gmap->line_advance;
1250 *xpos = (fmod(*xpos, 4.0) + 4.0) * gmap->glyphs[0].advance;
1263 *xpos += gmap->glyphs[code - gmap->cstart].advance;
1269 static int calc_best_size(int total_width, int max_gwidth, int max_gheight, int padding, int pow2, int *imgw, int *imgh)
1271 int xsz, ysz, num_rows;
1274 /* the widest glyph won't fit in the maximum image size */
1275 if(max_gwidth > MAX_IMG_SIZE) {
1279 for(xsz=2; xsz<=MAX_IMG_SIZE; xsz *= 2) {
1280 num_rows = total_width / xsz + 1;
1282 /* assume worst case, all last glyphs will float to the next line
1283 * so let's add extra rows for that. */
1284 num_rows += (padding + (max_gwidth + padding) * num_rows + xsz - 1) / xsz;
1286 ysz = num_rows * (max_gheight + padding) + padding;
1287 if(ysz <= 0 || ysz > MAX_IMG_SIZE) continue;
1290 ysz = next_pow2(ysz);
1292 aspect = (float)xsz / (float)ysz;
1299 if(xsz > MAX_IMG_SIZE || ysz > MAX_IMG_SIZE || ysz <= 0) {
1309 static int next_pow2(int x)
1321 static int find_pow2(int x)
1324 for(i=0; i<sizeof x * CHAR_BIT; i++) {