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