rev |
line source |
nuclear@5
|
1 /*
|
nuclear@5
|
2 libdrawtext - a simple library for fast text rendering in OpenGL
|
nuclear@5
|
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@5
|
4
|
nuclear@5
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@5
|
6 it under the terms of the GNU Lesser General Public License as published by
|
nuclear@5
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@5
|
8 (at your option) any later version.
|
nuclear@5
|
9
|
nuclear@5
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@5
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@5
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@5
|
13 GNU Lesser General Public License for more details.
|
nuclear@5
|
14
|
nuclear@5
|
15 You should have received a copy of the GNU Lesser General Public License
|
nuclear@5
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@5
|
17 */
|
nuclear@0
|
18 #ifndef NO_FREETYPE
|
nuclear@0
|
19 #define USE_FREETYPE
|
nuclear@0
|
20 #endif
|
nuclear@0
|
21
|
nuclear@0
|
22 #include <stdio.h>
|
nuclear@0
|
23 #include <stdlib.h>
|
nuclear@0
|
24 #include <string.h>
|
nuclear@6
|
25 #include <math.h>
|
nuclear@0
|
26 #include <limits.h>
|
nuclear@0
|
27 #include <ctype.h>
|
nuclear@6
|
28 #include <float.h>
|
nuclear@0
|
29 #include <errno.h>
|
nuclear@0
|
30 #ifdef USE_FREETYPE
|
nuclear@0
|
31 #include <ft2build.h>
|
nuclear@0
|
32 #include FT_FREETYPE_H
|
nuclear@0
|
33 #endif
|
nuclear@0
|
34 #include "drawtext.h"
|
nuclear@0
|
35 #include "drawtext_impl.h"
|
nuclear@0
|
36
|
nuclear@0
|
37 #define FTSZ_TO_PIXELS(x) ((x) / 64)
|
nuclear@0
|
38 #define MAX_IMG_WIDTH 4096
|
nuclear@0
|
39
|
nuclear@0
|
40
|
nuclear@0
|
41 #ifdef USE_FREETYPE
|
nuclear@0
|
42 static int init_freetype(void);
|
nuclear@0
|
43 static void cleanup(void);
|
nuclear@0
|
44
|
nuclear@0
|
45 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh);
|
nuclear@0
|
46 static int next_pow2(int x);
|
nuclear@0
|
47
|
nuclear@0
|
48 static FT_Library ft;
|
nuclear@0
|
49
|
nuclear@0
|
50
|
nuclear@0
|
51 static int init_done;
|
nuclear@0
|
52
|
nuclear@0
|
53 static int init_freetype(void)
|
nuclear@0
|
54 {
|
nuclear@0
|
55 if(!init_done) {
|
nuclear@0
|
56 if(FT_Init_FreeType(&ft) != 0) {
|
nuclear@0
|
57 return -1;
|
nuclear@0
|
58 }
|
nuclear@0
|
59 atexit(cleanup);
|
nuclear@0
|
60 init_done = 1;
|
nuclear@0
|
61 }
|
nuclear@0
|
62 return 0;
|
nuclear@0
|
63 }
|
nuclear@0
|
64
|
nuclear@0
|
65 static void cleanup(void)
|
nuclear@0
|
66 {
|
nuclear@0
|
67 if(init_done) {
|
nuclear@0
|
68 FT_Done_FreeType(ft);
|
nuclear@0
|
69 }
|
nuclear@0
|
70 }
|
nuclear@0
|
71
|
nuclear@0
|
72 struct dtx_font *dtx_open_font(const char *fname, int sz)
|
nuclear@0
|
73 {
|
nuclear@0
|
74 struct dtx_font *fnt;
|
nuclear@0
|
75
|
nuclear@0
|
76 init_freetype();
|
nuclear@0
|
77
|
nuclear@0
|
78 if(!(fnt = calloc(1, sizeof *fnt))) {
|
nuclear@0
|
79 fperror("failed to allocate font structure");
|
nuclear@0
|
80 return 0;
|
nuclear@0
|
81 }
|
nuclear@0
|
82
|
nuclear@0
|
83 if(FT_New_Face(ft, fname, 0, (FT_Face*)&fnt->face) != 0) {
|
nuclear@0
|
84 fprintf(stderr, "failed to open font file: %s\n", fname);
|
nuclear@0
|
85 return 0;
|
nuclear@0
|
86 }
|
nuclear@0
|
87
|
nuclear@0
|
88 /* pre-create the extended ASCII range glyphmap */
|
nuclear@0
|
89 if(sz) {
|
nuclear@0
|
90 dtx_prepare_range(fnt, sz, 0, 256);
|
nuclear@14
|
91
|
nuclear@14
|
92 if(!dtx_font) {
|
nuclear@14
|
93 dtx_use_font(fnt, sz);
|
nuclear@14
|
94 }
|
nuclear@0
|
95 }
|
nuclear@0
|
96
|
nuclear@0
|
97 return fnt;
|
nuclear@0
|
98 }
|
nuclear@0
|
99
|
nuclear@0
|
100 void dtx_close_font(struct dtx_font *fnt)
|
nuclear@0
|
101 {
|
nuclear@0
|
102 if(!fnt) return;
|
nuclear@0
|
103
|
nuclear@0
|
104 FT_Done_Face(fnt->face);
|
nuclear@0
|
105
|
nuclear@0
|
106 /* destroy the glyphmaps */
|
nuclear@0
|
107 while(fnt->gmaps) {
|
nuclear@0
|
108 void *tmp = fnt->gmaps;
|
nuclear@0
|
109 fnt->gmaps = fnt->gmaps->next;
|
nuclear@0
|
110 dtx_free_glyphmap(tmp);
|
nuclear@0
|
111 }
|
nuclear@0
|
112
|
nuclear@0
|
113 free(fnt);
|
nuclear@0
|
114 }
|
nuclear@0
|
115
|
nuclear@0
|
116 void dtx_prepare(struct dtx_font *fnt, int sz)
|
nuclear@0
|
117 {
|
nuclear@0
|
118 dtx_get_font_glyphmap_range(fnt, sz, 0, 256);
|
nuclear@0
|
119 }
|
nuclear@0
|
120
|
nuclear@0
|
121 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
|
nuclear@0
|
122 {
|
nuclear@0
|
123 dtx_get_font_glyphmap_range(fnt, sz, cstart, cend);
|
nuclear@0
|
124 }
|
nuclear@0
|
125
|
nuclear@0
|
126 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
|
nuclear@0
|
127 {
|
nuclear@0
|
128 struct dtx_glyphmap *gm;
|
nuclear@0
|
129
|
nuclear@0
|
130 /* check to see if the last we've given out fits the bill */
|
nuclear@0
|
131 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
|
nuclear@0
|
132 return fnt->last_gmap;
|
nuclear@0
|
133 }
|
nuclear@0
|
134
|
nuclear@0
|
135 /* otherwise search for the appropriate glyphmap */
|
nuclear@0
|
136 gm = fnt->gmaps;
|
nuclear@0
|
137 while(gm) {
|
nuclear@0
|
138 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
|
nuclear@0
|
139 fnt->last_gmap = gm;
|
nuclear@0
|
140 return gm;
|
nuclear@0
|
141 }
|
nuclear@0
|
142 gm = gm->next;
|
nuclear@0
|
143 }
|
nuclear@0
|
144 return 0;
|
nuclear@0
|
145 }
|
nuclear@0
|
146
|
nuclear@0
|
147 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
|
nuclear@0
|
148 {
|
nuclear@0
|
149 struct dtx_glyphmap *gm;
|
nuclear@0
|
150
|
nuclear@0
|
151 /* search the available glyphmaps to see if we've got one that includes
|
nuclear@0
|
152 * the requested range
|
nuclear@0
|
153 */
|
nuclear@0
|
154 gm = fnt->gmaps;
|
nuclear@0
|
155 while(gm) {
|
nuclear@0
|
156 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
|
nuclear@0
|
157 return gm;
|
nuclear@0
|
158 }
|
nuclear@0
|
159 gm = gm->next;
|
nuclear@0
|
160 }
|
nuclear@0
|
161
|
nuclear@0
|
162 /* not found, create one and add it to the list */
|
nuclear@0
|
163 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
|
nuclear@0
|
164 return 0;
|
nuclear@0
|
165 }
|
nuclear@0
|
166 return gm;
|
nuclear@0
|
167 }
|
nuclear@0
|
168
|
nuclear@0
|
169 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
|
nuclear@0
|
170 {
|
nuclear@0
|
171 FT_Face face = fnt->face;
|
nuclear@0
|
172 struct dtx_glyphmap *gmap;
|
nuclear@0
|
173 int i, j;
|
nuclear@0
|
174 int gx, gy;
|
nuclear@0
|
175 int padding = 4;
|
nuclear@0
|
176 int total_width = padding;
|
nuclear@0
|
177 int max_height = 0;
|
nuclear@0
|
178
|
nuclear@0
|
179 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
|
nuclear@0
|
180
|
nuclear@3
|
181 if(!(gmap = calloc(1, sizeof *gmap))) {
|
nuclear@0
|
182 return 0;
|
nuclear@0
|
183 }
|
nuclear@0
|
184
|
nuclear@0
|
185 gmap->ptsize = sz;
|
nuclear@0
|
186 gmap->cstart = cstart;
|
nuclear@0
|
187 gmap->cend = cend;
|
nuclear@0
|
188 gmap->crange = cend - cstart;
|
nuclear@0
|
189 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
|
nuclear@0
|
190
|
nuclear@0
|
191 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
|
nuclear@0
|
192 free(gmap);
|
nuclear@0
|
193 return 0;
|
nuclear@0
|
194 }
|
nuclear@0
|
195
|
nuclear@0
|
196 for(i=0; i<gmap->crange; i++) {
|
nuclear@0
|
197 int h;
|
nuclear@0
|
198
|
nuclear@0
|
199 FT_Load_Char(face, i + cstart, 0);
|
nuclear@0
|
200 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
|
nuclear@0
|
201
|
nuclear@0
|
202 if(h > max_height) {
|
nuclear@0
|
203 max_height = h;
|
nuclear@0
|
204 }
|
nuclear@0
|
205 total_width += FTSZ_TO_PIXELS(face->glyph->metrics.width) + padding;
|
nuclear@0
|
206 }
|
nuclear@0
|
207
|
nuclear@0
|
208 calc_best_size(total_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
|
nuclear@0
|
209
|
nuclear@0
|
210 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
|
nuclear@0
|
211 free(gmap->glyphs);
|
nuclear@0
|
212 free(gmap);
|
nuclear@0
|
213 return 0;
|
nuclear@0
|
214 }
|
nuclear@0
|
215 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
|
nuclear@0
|
216
|
nuclear@0
|
217 gx = padding;
|
nuclear@0
|
218 gy = padding;
|
nuclear@0
|
219
|
nuclear@0
|
220 for(i=0; i<gmap->crange; i++) {
|
nuclear@0
|
221 float gwidth, gheight;
|
nuclear@0
|
222 unsigned char *src, *dst;
|
nuclear@0
|
223 FT_GlyphSlot glyph;
|
nuclear@0
|
224
|
nuclear@0
|
225 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
|
nuclear@0
|
226 glyph = face->glyph;
|
nuclear@0
|
227 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
|
nuclear@0
|
228 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
|
nuclear@0
|
229
|
nuclear@0
|
230 if(gx > gmap->xsz - gwidth - padding) {
|
nuclear@0
|
231 gx = padding;
|
nuclear@0
|
232 gy += max_height + padding;
|
nuclear@0
|
233 }
|
nuclear@0
|
234
|
nuclear@0
|
235 src = glyph->bitmap.buffer;
|
nuclear@0
|
236 dst = gmap->pixels + gy * gmap->xsz + gx;
|
nuclear@0
|
237
|
nuclear@0
|
238 for(j=0; j<glyph->bitmap.rows; j++) {
|
nuclear@0
|
239 memcpy(dst, src, glyph->bitmap.width);
|
nuclear@0
|
240 dst += gmap->xsz;
|
nuclear@0
|
241 src += glyph->bitmap.pitch;
|
nuclear@0
|
242 }
|
nuclear@0
|
243
|
nuclear@0
|
244 gmap->glyphs[i].code = i;
|
nuclear@0
|
245 gmap->glyphs[i].x = gx - 1;
|
nuclear@0
|
246 gmap->glyphs[i].y = gy - 1;
|
nuclear@0
|
247 gmap->glyphs[i].width = gwidth + 2;
|
nuclear@0
|
248 gmap->glyphs[i].height = gheight + 2;
|
nuclear@0
|
249 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
|
nuclear@0
|
250 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
|
nuclear@0
|
251 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
|
nuclear@0
|
252 /* also precalc normalized */
|
nuclear@0
|
253 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
|
nuclear@0
|
254 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
|
nuclear@0
|
255 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
|
nuclear@0
|
256 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
|
nuclear@0
|
257
|
nuclear@0
|
258 gx += gwidth + padding;
|
nuclear@0
|
259 }
|
nuclear@0
|
260
|
nuclear@0
|
261 /* add it to the glyphmaps list of the font */
|
nuclear@0
|
262 gmap->next = fnt->gmaps;
|
nuclear@0
|
263 fnt->gmaps = gmap;
|
nuclear@0
|
264
|
nuclear@0
|
265 return gmap;
|
nuclear@0
|
266 }
|
nuclear@0
|
267 #endif /* USE_FREETYPE */
|
nuclear@0
|
268
|
nuclear@0
|
269 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
|
nuclear@0
|
270 {
|
nuclear@0
|
271 if(gmap) {
|
nuclear@0
|
272 free(gmap->pixels);
|
nuclear@0
|
273 free(gmap->glyphs);
|
nuclear@0
|
274 free(gmap);
|
nuclear@0
|
275 }
|
nuclear@0
|
276 }
|
nuclear@0
|
277
|
nuclear@0
|
278 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
|
nuclear@0
|
279 {
|
nuclear@0
|
280 return gmap->pixels;
|
nuclear@0
|
281 }
|
nuclear@0
|
282
|
nuclear@0
|
283 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
|
nuclear@0
|
284 {
|
nuclear@0
|
285 return gmap->xsz;
|
nuclear@0
|
286 }
|
nuclear@0
|
287
|
nuclear@0
|
288 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
|
nuclear@0
|
289 {
|
nuclear@0
|
290 return gmap->ysz;
|
nuclear@0
|
291 }
|
nuclear@0
|
292
|
nuclear@0
|
293 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
|
nuclear@0
|
294 {
|
nuclear@0
|
295 FILE *fp;
|
nuclear@0
|
296 struct dtx_glyphmap *gmap;
|
nuclear@0
|
297
|
nuclear@0
|
298 if(!(fp = fopen(fname, "r"))) {
|
nuclear@0
|
299 return 0;
|
nuclear@0
|
300 }
|
nuclear@0
|
301 gmap = dtx_load_glyphmap_stream(fp);
|
nuclear@0
|
302 fclose(fp);
|
nuclear@0
|
303 return gmap;
|
nuclear@0
|
304 }
|
nuclear@0
|
305
|
nuclear@0
|
306 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
|
nuclear@0
|
307 {
|
nuclear@0
|
308 char buf[512];
|
nuclear@0
|
309 int hdr_lines = 0;
|
nuclear@0
|
310 struct dtx_glyphmap *gmap;
|
nuclear@0
|
311 struct glyph *glyphs = 0;
|
nuclear@0
|
312 int min_code = INT_MAX;
|
nuclear@0
|
313 int max_code = INT_MIN;
|
nuclear@0
|
314 int i, max_pixval, num_pixels;
|
nuclear@0
|
315
|
nuclear@0
|
316 if(!(gmap = calloc(1, sizeof *gmap))) {
|
nuclear@0
|
317 fperror("failed to allocate glyphmap");
|
nuclear@0
|
318 return 0;
|
nuclear@0
|
319 }
|
nuclear@0
|
320
|
nuclear@0
|
321 while(hdr_lines < 3) {
|
nuclear@0
|
322 char *line = buf;
|
nuclear@0
|
323 if(!fgets(buf, sizeof buf, fp)) {
|
nuclear@0
|
324 fperror("unexpected end of file");
|
nuclear@0
|
325 goto err;
|
nuclear@0
|
326 }
|
nuclear@0
|
327
|
nuclear@0
|
328 while(isspace(*line)) {
|
nuclear@0
|
329 line++;
|
nuclear@0
|
330 }
|
nuclear@0
|
331
|
nuclear@0
|
332 if(line[0] == '#') {
|
nuclear@0
|
333 struct glyph *g;
|
nuclear@16
|
334 int c, res;
|
nuclear@16
|
335 float x, y, xsz, ysz;
|
nuclear@0
|
336
|
nuclear@0
|
337 res = sscanf(line + 1, "%d: %fx%f+%f+%f\n", &c, &xsz, &ysz, &x, &y);
|
nuclear@0
|
338 if(res != 5) {
|
nuclear@16
|
339 fprintf(stderr, "%s: invalid glyph info line\n", __FUNCTION__);
|
nuclear@0
|
340 goto err;
|
nuclear@0
|
341 }
|
nuclear@0
|
342
|
nuclear@0
|
343 if(!(g = malloc(sizeof *g))) {
|
nuclear@0
|
344 fperror("failed to allocate glyph");
|
nuclear@0
|
345 goto err;
|
nuclear@0
|
346 }
|
nuclear@0
|
347 g->code = c;
|
nuclear@0
|
348 g->x = x;
|
nuclear@0
|
349 g->y = y;
|
nuclear@0
|
350 g->width = xsz;
|
nuclear@0
|
351 g->height = ysz;
|
nuclear@0
|
352 g->next = glyphs;
|
nuclear@0
|
353 glyphs = g;
|
nuclear@0
|
354
|
nuclear@0
|
355 if(c < min_code) {
|
nuclear@0
|
356 min_code = c;
|
nuclear@0
|
357 }
|
nuclear@0
|
358 if(c > max_code) {
|
nuclear@0
|
359 max_code = c;
|
nuclear@0
|
360 }
|
nuclear@0
|
361 } else {
|
nuclear@0
|
362 switch(hdr_lines) {
|
nuclear@0
|
363 case 0:
|
nuclear@0
|
364 if(0[line] != 'P' || 1[line] != '6') {
|
nuclear@16
|
365 fprintf(stderr, "%s: invalid file format (magic)\n", __FUNCTION__);
|
nuclear@0
|
366 goto err;
|
nuclear@0
|
367 }
|
nuclear@0
|
368 break;
|
nuclear@0
|
369
|
nuclear@0
|
370 case 1:
|
nuclear@0
|
371 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
|
nuclear@16
|
372 fprintf(stderr, "%s: invalid file format (dim)\n", __FUNCTION__);
|
nuclear@0
|
373 goto err;
|
nuclear@0
|
374 }
|
nuclear@0
|
375 break;
|
nuclear@0
|
376
|
nuclear@0
|
377 case 2:
|
nuclear@0
|
378 {
|
nuclear@0
|
379 char *endp;
|
nuclear@0
|
380 max_pixval = strtol(line, &endp, 10);
|
nuclear@0
|
381 if(endp == line) {
|
nuclear@16
|
382 fprintf(stderr, "%s: invalid file format (maxval)\n", __FUNCTION__);
|
nuclear@0
|
383 goto err;
|
nuclear@0
|
384 }
|
nuclear@0
|
385 }
|
nuclear@0
|
386 break;
|
nuclear@0
|
387
|
nuclear@0
|
388 default:
|
nuclear@0
|
389 break; /* can't happen */
|
nuclear@0
|
390 }
|
nuclear@0
|
391 hdr_lines++;
|
nuclear@0
|
392 }
|
nuclear@0
|
393 }
|
nuclear@0
|
394
|
nuclear@0
|
395 num_pixels = gmap->xsz * gmap->ysz;
|
nuclear@0
|
396 if(!(gmap->pixels = malloc(num_pixels))) {
|
nuclear@0
|
397 fperror("failed to allocate pixels");
|
nuclear@0
|
398 goto err;
|
nuclear@0
|
399 }
|
nuclear@0
|
400
|
nuclear@0
|
401 for(i=0; i<num_pixels; i++) {
|
nuclear@0
|
402 long c = fgetc(fp);
|
nuclear@0
|
403 if(c == -1) {
|
nuclear@0
|
404 fprintf(stderr, "unexpected end of file while reading pixels\n");
|
nuclear@0
|
405 goto err;
|
nuclear@0
|
406 }
|
nuclear@0
|
407 gmap->pixels[i] = 255 * c / max_pixval;
|
nuclear@0
|
408 fseek(fp, 2, SEEK_CUR);
|
nuclear@0
|
409 }
|
nuclear@0
|
410
|
nuclear@0
|
411 gmap->cstart = min_code;
|
nuclear@0
|
412 gmap->cend = max_code + 1;
|
nuclear@0
|
413 gmap->crange = gmap->cend - gmap->cstart;
|
nuclear@0
|
414
|
nuclear@0
|
415 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
|
nuclear@0
|
416 fperror("failed to allocate glyph info");
|
nuclear@0
|
417 goto err;
|
nuclear@0
|
418 }
|
nuclear@0
|
419
|
nuclear@0
|
420 while(glyphs) {
|
nuclear@0
|
421 struct glyph *g = glyphs;
|
nuclear@0
|
422 glyphs = glyphs->next;
|
nuclear@0
|
423
|
nuclear@0
|
424 gmap->glyphs[g->code - gmap->cstart] = *g;
|
nuclear@0
|
425 free(g);
|
nuclear@0
|
426 }
|
nuclear@0
|
427 return gmap;
|
nuclear@0
|
428
|
nuclear@0
|
429 err:
|
nuclear@0
|
430 dtx_free_glyphmap(gmap);
|
nuclear@0
|
431 while(glyphs) {
|
nuclear@0
|
432 void *tmp = glyphs;
|
nuclear@0
|
433 glyphs = glyphs->next;
|
nuclear@0
|
434 free(tmp);
|
nuclear@0
|
435 }
|
nuclear@0
|
436 return 0;
|
nuclear@0
|
437 }
|
nuclear@0
|
438
|
nuclear@0
|
439 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
|
nuclear@0
|
440 {
|
nuclear@0
|
441 FILE *fp;
|
nuclear@0
|
442 int res;
|
nuclear@0
|
443
|
nuclear@0
|
444 if(!(fp = fopen(fname, "wb"))) {
|
nuclear@16
|
445 fprintf(stderr, "%s: failed to open file: %s: %s\n", __FUNCTION__, fname, strerror(errno));
|
nuclear@0
|
446 return -1;
|
nuclear@0
|
447 }
|
nuclear@0
|
448 res = dtx_save_glyphmap_stream(fp, gmap);
|
nuclear@0
|
449 fclose(fp);
|
nuclear@0
|
450 return res;
|
nuclear@0
|
451 }
|
nuclear@0
|
452
|
nuclear@0
|
453 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
|
nuclear@0
|
454 {
|
nuclear@0
|
455 int i, num_pixels;
|
nuclear@0
|
456 struct glyph *g = gmap->glyphs;
|
nuclear@0
|
457
|
nuclear@0
|
458 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
|
nuclear@0
|
459 for(i=0; i<gmap->crange; i++) {
|
nuclear@0
|
460 fprintf(fp, "# %d: %fx%f+%f+%f\n", g->code, g->width, g->height, g->x, g->y);
|
nuclear@0
|
461 g++;
|
nuclear@0
|
462 }
|
nuclear@0
|
463 fprintf(fp, "255\n");
|
nuclear@0
|
464
|
nuclear@0
|
465 num_pixels = gmap->xsz * gmap->ysz;
|
nuclear@0
|
466 for(i=0; i<num_pixels; i++) {
|
nuclear@0
|
467 int c = gmap->pixels[i];
|
nuclear@0
|
468 fputc(c, fp);
|
nuclear@0
|
469 fputc(c, fp);
|
nuclear@0
|
470 fputc(c, fp);
|
nuclear@0
|
471 }
|
nuclear@0
|
472 return 0;
|
nuclear@0
|
473 }
|
nuclear@0
|
474
|
nuclear@0
|
475
|
nuclear@6
|
476 void dtx_use_font(struct dtx_font *fnt, int sz)
|
nuclear@6
|
477 {
|
nuclear@6
|
478 dtx_gl_init();
|
nuclear@6
|
479
|
nuclear@6
|
480 dtx_font = fnt;
|
nuclear@6
|
481 dtx_font_sz = sz;
|
nuclear@6
|
482 }
|
nuclear@6
|
483
|
nuclear@13
|
484 float dtx_line_height(void)
|
nuclear@13
|
485 {
|
nuclear@13
|
486 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, '\n');
|
nuclear@13
|
487
|
nuclear@13
|
488 return gmap->line_advance;
|
nuclear@13
|
489 }
|
nuclear@13
|
490
|
nuclear@6
|
491 void dtx_glyph_box(int code, struct dtx_box *box)
|
nuclear@6
|
492 {
|
nuclear@6
|
493 int cidx;
|
nuclear@6
|
494 struct dtx_glyphmap *gmap;
|
nuclear@6
|
495 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
|
nuclear@6
|
496
|
nuclear@6
|
497 cidx = code - gmap->cstart;
|
nuclear@6
|
498
|
nuclear@6
|
499 box->x = gmap->glyphs[cidx].orig_x;
|
nuclear@6
|
500 box->y = gmap->glyphs[cidx].orig_y;
|
nuclear@6
|
501 box->width = gmap->glyphs[cidx].width;
|
nuclear@6
|
502 box->height = gmap->glyphs[cidx].height;
|
nuclear@6
|
503 }
|
nuclear@6
|
504
|
nuclear@6
|
505 float dtx_glyph_width(int code)
|
nuclear@6
|
506 {
|
nuclear@6
|
507 struct dtx_box box;
|
nuclear@6
|
508 dtx_glyph_box(code, &box);
|
nuclear@6
|
509 return box.width;
|
nuclear@6
|
510 }
|
nuclear@6
|
511
|
nuclear@6
|
512 float dtx_glyph_height(int code)
|
nuclear@6
|
513 {
|
nuclear@6
|
514 struct dtx_box box;
|
nuclear@6
|
515 dtx_glyph_box(code, &box);
|
nuclear@6
|
516 return box.height;
|
nuclear@6
|
517 }
|
nuclear@6
|
518
|
nuclear@6
|
519 void dtx_string_box(const char *str, struct dtx_box *box)
|
nuclear@6
|
520 {
|
nuclear@6
|
521 int code;
|
nuclear@6
|
522 float pos_x = 0.0f, pos_y = 0.0f;
|
nuclear@6
|
523 struct glyph *g = 0;
|
nuclear@6
|
524 float x0, y0, x1, y1;
|
nuclear@6
|
525
|
nuclear@6
|
526 x0 = y0 = FLT_MAX;
|
nuclear@6
|
527 x1 = y1 = -FLT_MAX;
|
nuclear@6
|
528
|
nuclear@6
|
529 while(*str) {
|
nuclear@6
|
530 float px, py;
|
nuclear@6
|
531 struct dtx_glyphmap *gmap;
|
nuclear@6
|
532
|
nuclear@6
|
533 code = dtx_utf8_char_code(str);
|
nuclear@6
|
534 str = dtx_utf8_next_char((char*)str);
|
nuclear@6
|
535
|
nuclear@6
|
536 px = pos_x;
|
nuclear@6
|
537 py = pos_y;
|
nuclear@6
|
538
|
nuclear@6
|
539 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
|
nuclear@6
|
540 g = gmap->glyphs + code - gmap->cstart;
|
nuclear@6
|
541
|
nuclear@6
|
542 if(px + g->orig_x < x0) {
|
nuclear@6
|
543 x0 = px + g->orig_x;
|
nuclear@6
|
544 }
|
nuclear@7
|
545 if(py - g->orig_y < y0) {
|
nuclear@7
|
546 y0 = py - g->orig_y;
|
nuclear@6
|
547 }
|
nuclear@6
|
548 if(px + g->orig_x + g->width > x1) {
|
nuclear@6
|
549 x1 = px + g->orig_x + g->width;
|
nuclear@6
|
550 }
|
nuclear@7
|
551 if(py - g->orig_y + g->height > y1) {
|
nuclear@7
|
552 y1 = py - g->orig_y + g->height;
|
nuclear@6
|
553 }
|
nuclear@6
|
554 }
|
nuclear@6
|
555 }
|
nuclear@6
|
556
|
nuclear@6
|
557 box->x = x0;
|
nuclear@6
|
558 box->y = y0;
|
nuclear@6
|
559 box->width = x1 - x0;
|
nuclear@6
|
560 box->height = y1 - y0;
|
nuclear@6
|
561 }
|
nuclear@6
|
562
|
nuclear@6
|
563 float dtx_string_width(const char *str)
|
nuclear@6
|
564 {
|
nuclear@6
|
565 struct dtx_box box;
|
nuclear@6
|
566
|
nuclear@6
|
567 dtx_string_box(str, &box);
|
nuclear@6
|
568 return box.width;
|
nuclear@6
|
569 }
|
nuclear@6
|
570
|
nuclear@6
|
571 float dtx_string_height(const char *str)
|
nuclear@6
|
572 {
|
nuclear@6
|
573 struct dtx_box box;
|
nuclear@6
|
574
|
nuclear@6
|
575 dtx_string_box(str, &box);
|
nuclear@6
|
576 return box.height;
|
nuclear@6
|
577 }
|
nuclear@6
|
578
|
nuclear@6
|
579 float dtx_char_pos(const char *str, int n)
|
nuclear@6
|
580 {
|
nuclear@6
|
581 int i;
|
nuclear@6
|
582 float pos = 0.0;
|
nuclear@6
|
583 struct dtx_glyphmap *gmap;
|
nuclear@6
|
584
|
nuclear@6
|
585 for(i=0; i<n; i++) {
|
nuclear@6
|
586 int code = dtx_utf8_char_code(str);
|
nuclear@6
|
587 str = dtx_utf8_next_char((char*)str);
|
nuclear@6
|
588
|
nuclear@6
|
589 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
|
nuclear@6
|
590 pos += gmap->glyphs[i].advance;
|
nuclear@6
|
591 }
|
nuclear@6
|
592 return pos;
|
nuclear@6
|
593 }
|
nuclear@6
|
594
|
nuclear@7
|
595 int dtx_char_at_pt(const char *str, float pt)
|
nuclear@7
|
596 {
|
nuclear@7
|
597 int i;
|
nuclear@7
|
598 float prev_pos = 0.0f, pos = 0.0f;
|
nuclear@7
|
599 struct dtx_glyphmap *gmap;
|
nuclear@7
|
600
|
nuclear@7
|
601 for(i=0; *str; i++) {
|
nuclear@7
|
602 int code = dtx_utf8_char_code(str);
|
nuclear@7
|
603 str = dtx_utf8_next_char((char*)str);
|
nuclear@7
|
604
|
nuclear@7
|
605 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
|
nuclear@7
|
606 pos += gmap->glyphs[i].advance;
|
nuclear@7
|
607
|
nuclear@7
|
608 if(fabs(pt - prev_pos) < fabs(pt - pos)) {
|
nuclear@7
|
609 break;
|
nuclear@7
|
610 }
|
nuclear@7
|
611 prev_pos = pos;
|
nuclear@7
|
612 }
|
nuclear@7
|
613 return i;
|
nuclear@7
|
614 }
|
nuclear@6
|
615
|
nuclear@6
|
616 struct dtx_glyphmap *dtx_proc_char(int code, float *xpos, float *ypos)
|
nuclear@6
|
617 {
|
nuclear@6
|
618 struct dtx_glyphmap *gmap;
|
nuclear@6
|
619 gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code);
|
nuclear@6
|
620
|
nuclear@6
|
621 switch(code) {
|
nuclear@6
|
622 case '\n':
|
nuclear@6
|
623 *xpos = 0.0;
|
nuclear@6
|
624 if(gmap) {
|
nuclear@6
|
625 *ypos -= gmap->line_advance;
|
nuclear@6
|
626 }
|
nuclear@6
|
627 return 0;
|
nuclear@6
|
628
|
nuclear@6
|
629 case '\t':
|
nuclear@6
|
630 if(gmap) {
|
nuclear@6
|
631 *xpos = (fmod(*xpos, 4.0) + 4.0) * gmap->glyphs[0].advance;
|
nuclear@6
|
632 }
|
nuclear@6
|
633 return 0;
|
nuclear@6
|
634
|
nuclear@6
|
635 case '\r':
|
nuclear@6
|
636 *xpos = 0.0;
|
nuclear@6
|
637 return 0;
|
nuclear@6
|
638
|
nuclear@6
|
639 default:
|
nuclear@6
|
640 break;
|
nuclear@6
|
641 }
|
nuclear@6
|
642
|
nuclear@6
|
643 if(gmap) {
|
nuclear@6
|
644 *xpos += gmap->glyphs[code - gmap->cstart].advance;
|
nuclear@6
|
645 }
|
nuclear@6
|
646 return gmap;
|
nuclear@6
|
647 }
|
nuclear@6
|
648
|
nuclear@0
|
649 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
|
nuclear@0
|
650 {
|
nuclear@0
|
651 int xsz, ysz, num_rows;
|
nuclear@0
|
652 float aspect;
|
nuclear@0
|
653
|
nuclear@0
|
654 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
|
nuclear@0
|
655 num_rows = total_width / xsz + 1;
|
nuclear@0
|
656
|
nuclear@0
|
657 /* take into account the one extra padding for each row after the first */
|
nuclear@0
|
658 num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
|
nuclear@0
|
659
|
nuclear@0
|
660 ysz = num_rows * (max_glyph_height + padding) + padding;
|
nuclear@0
|
661 if(pow2) {
|
nuclear@0
|
662 ysz = next_pow2(ysz);
|
nuclear@0
|
663 }
|
nuclear@0
|
664 aspect = (float)xsz / (float)ysz;
|
nuclear@0
|
665
|
nuclear@0
|
666 if(aspect >= 1.0) {
|
nuclear@0
|
667 break;
|
nuclear@0
|
668 }
|
nuclear@0
|
669 }
|
nuclear@0
|
670
|
nuclear@0
|
671 if(xsz > MAX_IMG_WIDTH) {
|
nuclear@0
|
672 xsz = MAX_IMG_WIDTH;
|
nuclear@0
|
673 }
|
nuclear@0
|
674
|
nuclear@0
|
675 *imgw = xsz;
|
nuclear@0
|
676 *imgh = ysz;
|
nuclear@0
|
677 }
|
nuclear@0
|
678
|
nuclear@0
|
679
|
nuclear@0
|
680 static int next_pow2(int x)
|
nuclear@0
|
681 {
|
nuclear@0
|
682 x--;
|
nuclear@0
|
683 x = (x >> 1) | x;
|
nuclear@0
|
684 x = (x >> 2) | x;
|
nuclear@0
|
685 x = (x >> 4) | x;
|
nuclear@0
|
686 x = (x >> 8) | x;
|
nuclear@0
|
687 x = (x >> 16) | x;
|
nuclear@0
|
688 return x + 1;
|
nuclear@0
|
689 }
|