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@0
|
25 #include <limits.h>
|
nuclear@0
|
26 #include <ctype.h>
|
nuclear@0
|
27 #include <errno.h>
|
nuclear@0
|
28 #ifdef USE_FREETYPE
|
nuclear@0
|
29 #include <ft2build.h>
|
nuclear@0
|
30 #include FT_FREETYPE_H
|
nuclear@0
|
31 #endif
|
nuclear@0
|
32 #include "drawtext.h"
|
nuclear@0
|
33 #include "drawtext_impl.h"
|
nuclear@0
|
34
|
nuclear@0
|
35 #define FTSZ_TO_PIXELS(x) ((x) / 64)
|
nuclear@0
|
36 #define MAX_IMG_WIDTH 4096
|
nuclear@0
|
37
|
nuclear@0
|
38
|
nuclear@0
|
39 #ifdef USE_FREETYPE
|
nuclear@0
|
40 static int init_freetype(void);
|
nuclear@0
|
41 static void cleanup(void);
|
nuclear@0
|
42
|
nuclear@0
|
43 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh);
|
nuclear@0
|
44 static int next_pow2(int x);
|
nuclear@0
|
45
|
nuclear@0
|
46 static FT_Library ft;
|
nuclear@0
|
47
|
nuclear@0
|
48
|
nuclear@0
|
49 static int init_done;
|
nuclear@0
|
50
|
nuclear@0
|
51 static int init_freetype(void)
|
nuclear@0
|
52 {
|
nuclear@0
|
53 if(!init_done) {
|
nuclear@0
|
54 if(FT_Init_FreeType(&ft) != 0) {
|
nuclear@0
|
55 return -1;
|
nuclear@0
|
56 }
|
nuclear@0
|
57 atexit(cleanup);
|
nuclear@0
|
58 init_done = 1;
|
nuclear@0
|
59 }
|
nuclear@0
|
60 return 0;
|
nuclear@0
|
61 }
|
nuclear@0
|
62
|
nuclear@0
|
63 static void cleanup(void)
|
nuclear@0
|
64 {
|
nuclear@0
|
65 if(init_done) {
|
nuclear@0
|
66 FT_Done_FreeType(ft);
|
nuclear@0
|
67 }
|
nuclear@0
|
68 }
|
nuclear@0
|
69
|
nuclear@0
|
70 struct dtx_font *dtx_open_font(const char *fname, int sz)
|
nuclear@0
|
71 {
|
nuclear@0
|
72 struct dtx_font *fnt;
|
nuclear@0
|
73
|
nuclear@0
|
74 init_freetype();
|
nuclear@0
|
75
|
nuclear@0
|
76 if(!(fnt = calloc(1, sizeof *fnt))) {
|
nuclear@0
|
77 fperror("failed to allocate font structure");
|
nuclear@0
|
78 return 0;
|
nuclear@0
|
79 }
|
nuclear@0
|
80
|
nuclear@0
|
81 if(FT_New_Face(ft, fname, 0, (FT_Face*)&fnt->face) != 0) {
|
nuclear@0
|
82 fprintf(stderr, "failed to open font file: %s\n", fname);
|
nuclear@0
|
83 return 0;
|
nuclear@0
|
84 }
|
nuclear@0
|
85
|
nuclear@0
|
86 /* pre-create the extended ASCII range glyphmap */
|
nuclear@0
|
87 if(sz) {
|
nuclear@0
|
88 dtx_prepare_range(fnt, sz, 0, 256);
|
nuclear@0
|
89 }
|
nuclear@0
|
90
|
nuclear@0
|
91 return fnt;
|
nuclear@0
|
92 }
|
nuclear@0
|
93
|
nuclear@0
|
94 void dtx_close_font(struct dtx_font *fnt)
|
nuclear@0
|
95 {
|
nuclear@0
|
96 if(!fnt) return;
|
nuclear@0
|
97
|
nuclear@0
|
98 FT_Done_Face(fnt->face);
|
nuclear@0
|
99
|
nuclear@0
|
100 /* destroy the glyphmaps */
|
nuclear@0
|
101 while(fnt->gmaps) {
|
nuclear@0
|
102 void *tmp = fnt->gmaps;
|
nuclear@0
|
103 fnt->gmaps = fnt->gmaps->next;
|
nuclear@0
|
104 dtx_free_glyphmap(tmp);
|
nuclear@0
|
105 }
|
nuclear@0
|
106
|
nuclear@0
|
107 free(fnt);
|
nuclear@0
|
108 }
|
nuclear@0
|
109
|
nuclear@0
|
110 void dtx_prepare(struct dtx_font *fnt, int sz)
|
nuclear@0
|
111 {
|
nuclear@0
|
112 dtx_get_font_glyphmap_range(fnt, sz, 0, 256);
|
nuclear@0
|
113 }
|
nuclear@0
|
114
|
nuclear@0
|
115 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend)
|
nuclear@0
|
116 {
|
nuclear@0
|
117 dtx_get_font_glyphmap_range(fnt, sz, cstart, cend);
|
nuclear@0
|
118 }
|
nuclear@0
|
119
|
nuclear@0
|
120 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code)
|
nuclear@0
|
121 {
|
nuclear@0
|
122 struct dtx_glyphmap *gm;
|
nuclear@0
|
123
|
nuclear@0
|
124 /* check to see if the last we've given out fits the bill */
|
nuclear@0
|
125 if(fnt->last_gmap && code >= fnt->last_gmap->cstart && code < fnt->last_gmap->cend && fnt->last_gmap->ptsize == sz) {
|
nuclear@0
|
126 return fnt->last_gmap;
|
nuclear@0
|
127 }
|
nuclear@0
|
128
|
nuclear@0
|
129 /* otherwise search for the appropriate glyphmap */
|
nuclear@0
|
130 gm = fnt->gmaps;
|
nuclear@0
|
131 while(gm) {
|
nuclear@0
|
132 if(code >= gm->cstart && code < gm->cend && sz == gm->ptsize) {
|
nuclear@0
|
133 fnt->last_gmap = gm;
|
nuclear@0
|
134 return gm;
|
nuclear@0
|
135 }
|
nuclear@0
|
136 gm = gm->next;
|
nuclear@0
|
137 }
|
nuclear@0
|
138 return 0;
|
nuclear@0
|
139 }
|
nuclear@0
|
140
|
nuclear@0
|
141 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
|
nuclear@0
|
142 {
|
nuclear@0
|
143 struct dtx_glyphmap *gm;
|
nuclear@0
|
144
|
nuclear@0
|
145 /* search the available glyphmaps to see if we've got one that includes
|
nuclear@0
|
146 * the requested range
|
nuclear@0
|
147 */
|
nuclear@0
|
148 gm = fnt->gmaps;
|
nuclear@0
|
149 while(gm) {
|
nuclear@0
|
150 if(gm->cstart <= cstart && gm->cend >= cend && gm->ptsize == sz) {
|
nuclear@0
|
151 return gm;
|
nuclear@0
|
152 }
|
nuclear@0
|
153 gm = gm->next;
|
nuclear@0
|
154 }
|
nuclear@0
|
155
|
nuclear@0
|
156 /* not found, create one and add it to the list */
|
nuclear@0
|
157 if(!(gm = dtx_create_glyphmap_range(fnt, sz, cstart, cend))) {
|
nuclear@0
|
158 return 0;
|
nuclear@0
|
159 }
|
nuclear@0
|
160 return gm;
|
nuclear@0
|
161 }
|
nuclear@0
|
162
|
nuclear@0
|
163 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend)
|
nuclear@0
|
164 {
|
nuclear@0
|
165 FT_Face face = fnt->face;
|
nuclear@0
|
166 struct dtx_glyphmap *gmap;
|
nuclear@0
|
167 int i, j;
|
nuclear@0
|
168 int gx, gy;
|
nuclear@0
|
169 int padding = 4;
|
nuclear@0
|
170 int total_width = padding;
|
nuclear@0
|
171 int max_height = 0;
|
nuclear@0
|
172
|
nuclear@0
|
173 FT_Set_Char_Size(fnt->face, 0, sz * 64, 72, 72);
|
nuclear@0
|
174
|
nuclear@3
|
175 if(!(gmap = calloc(1, sizeof *gmap))) {
|
nuclear@0
|
176 return 0;
|
nuclear@0
|
177 }
|
nuclear@0
|
178
|
nuclear@0
|
179 gmap->ptsize = sz;
|
nuclear@0
|
180 gmap->cstart = cstart;
|
nuclear@0
|
181 gmap->cend = cend;
|
nuclear@0
|
182 gmap->crange = cend - cstart;
|
nuclear@0
|
183 gmap->line_advance = FTSZ_TO_PIXELS((float)face->size->metrics.height);
|
nuclear@0
|
184
|
nuclear@0
|
185 if(!(gmap->glyphs = malloc(gmap->crange * sizeof *gmap->glyphs))) {
|
nuclear@0
|
186 free(gmap);
|
nuclear@0
|
187 return 0;
|
nuclear@0
|
188 }
|
nuclear@0
|
189
|
nuclear@0
|
190 for(i=0; i<gmap->crange; i++) {
|
nuclear@0
|
191 int h;
|
nuclear@0
|
192
|
nuclear@0
|
193 FT_Load_Char(face, i + cstart, 0);
|
nuclear@0
|
194 h = FTSZ_TO_PIXELS(face->glyph->metrics.height);
|
nuclear@0
|
195
|
nuclear@0
|
196 if(h > max_height) {
|
nuclear@0
|
197 max_height = h;
|
nuclear@0
|
198 }
|
nuclear@0
|
199 total_width += FTSZ_TO_PIXELS(face->glyph->metrics.width) + padding;
|
nuclear@0
|
200 }
|
nuclear@0
|
201
|
nuclear@0
|
202 calc_best_size(total_width, max_height, padding, 1, &gmap->xsz, &gmap->ysz);
|
nuclear@0
|
203
|
nuclear@0
|
204 if(!(gmap->pixels = malloc(gmap->xsz * gmap->ysz))) {
|
nuclear@0
|
205 free(gmap->glyphs);
|
nuclear@0
|
206 free(gmap);
|
nuclear@0
|
207 return 0;
|
nuclear@0
|
208 }
|
nuclear@0
|
209 memset(gmap->pixels, 0, gmap->xsz * gmap->ysz);
|
nuclear@0
|
210
|
nuclear@0
|
211 gx = padding;
|
nuclear@0
|
212 gy = padding;
|
nuclear@0
|
213
|
nuclear@0
|
214 for(i=0; i<gmap->crange; i++) {
|
nuclear@0
|
215 float gwidth, gheight;
|
nuclear@0
|
216 unsigned char *src, *dst;
|
nuclear@0
|
217 FT_GlyphSlot glyph;
|
nuclear@0
|
218
|
nuclear@0
|
219 FT_Load_Char(face, i + cstart, FT_LOAD_RENDER);
|
nuclear@0
|
220 glyph = face->glyph;
|
nuclear@0
|
221 gwidth = FTSZ_TO_PIXELS((float)glyph->metrics.width);
|
nuclear@0
|
222 gheight = FTSZ_TO_PIXELS((float)glyph->metrics.height);
|
nuclear@0
|
223
|
nuclear@0
|
224 if(gx > gmap->xsz - gwidth - padding) {
|
nuclear@0
|
225 gx = padding;
|
nuclear@0
|
226 gy += max_height + padding;
|
nuclear@0
|
227 }
|
nuclear@0
|
228
|
nuclear@0
|
229 src = glyph->bitmap.buffer;
|
nuclear@0
|
230 dst = gmap->pixels + gy * gmap->xsz + gx;
|
nuclear@0
|
231
|
nuclear@0
|
232 for(j=0; j<glyph->bitmap.rows; j++) {
|
nuclear@0
|
233 memcpy(dst, src, glyph->bitmap.width);
|
nuclear@0
|
234 dst += gmap->xsz;
|
nuclear@0
|
235 src += glyph->bitmap.pitch;
|
nuclear@0
|
236 }
|
nuclear@0
|
237
|
nuclear@0
|
238 gmap->glyphs[i].code = i;
|
nuclear@0
|
239 gmap->glyphs[i].x = gx - 1;
|
nuclear@0
|
240 gmap->glyphs[i].y = gy - 1;
|
nuclear@0
|
241 gmap->glyphs[i].width = gwidth + 2;
|
nuclear@0
|
242 gmap->glyphs[i].height = gheight + 2;
|
nuclear@0
|
243 gmap->glyphs[i].orig_x = -FTSZ_TO_PIXELS((float)glyph->metrics.horiBearingX) + 1;
|
nuclear@0
|
244 gmap->glyphs[i].orig_y = FTSZ_TO_PIXELS((float)glyph->metrics.height - glyph->metrics.horiBearingY) + 1;
|
nuclear@0
|
245 gmap->glyphs[i].advance = FTSZ_TO_PIXELS((float)glyph->metrics.horiAdvance);
|
nuclear@0
|
246 /* also precalc normalized */
|
nuclear@0
|
247 gmap->glyphs[i].nx = (float)gmap->glyphs[i].x / (float)gmap->xsz;
|
nuclear@0
|
248 gmap->glyphs[i].ny = (float)gmap->glyphs[i].y / (float)gmap->ysz;
|
nuclear@0
|
249 gmap->glyphs[i].nwidth = (float)gmap->glyphs[i].width / (float)gmap->xsz;
|
nuclear@0
|
250 gmap->glyphs[i].nheight = (float)gmap->glyphs[i].height / (float)gmap->ysz;
|
nuclear@0
|
251
|
nuclear@0
|
252 gx += gwidth + padding;
|
nuclear@0
|
253 }
|
nuclear@0
|
254
|
nuclear@0
|
255 /* add it to the glyphmaps list of the font */
|
nuclear@0
|
256 gmap->next = fnt->gmaps;
|
nuclear@0
|
257 fnt->gmaps = gmap;
|
nuclear@0
|
258
|
nuclear@0
|
259 return gmap;
|
nuclear@0
|
260 }
|
nuclear@0
|
261 #endif /* USE_FREETYPE */
|
nuclear@0
|
262
|
nuclear@0
|
263 void dtx_free_glyphmap(struct dtx_glyphmap *gmap)
|
nuclear@0
|
264 {
|
nuclear@0
|
265 if(gmap) {
|
nuclear@0
|
266 free(gmap->pixels);
|
nuclear@0
|
267 free(gmap->glyphs);
|
nuclear@0
|
268 free(gmap);
|
nuclear@0
|
269 }
|
nuclear@0
|
270 }
|
nuclear@0
|
271
|
nuclear@0
|
272 unsigned char *dtx_get_glyphmap_pixels(struct dtx_glyphmap *gmap)
|
nuclear@0
|
273 {
|
nuclear@0
|
274 return gmap->pixels;
|
nuclear@0
|
275 }
|
nuclear@0
|
276
|
nuclear@0
|
277 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap)
|
nuclear@0
|
278 {
|
nuclear@0
|
279 return gmap->xsz;
|
nuclear@0
|
280 }
|
nuclear@0
|
281
|
nuclear@0
|
282 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap)
|
nuclear@0
|
283 {
|
nuclear@0
|
284 return gmap->ysz;
|
nuclear@0
|
285 }
|
nuclear@0
|
286
|
nuclear@0
|
287 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname)
|
nuclear@0
|
288 {
|
nuclear@0
|
289 FILE *fp;
|
nuclear@0
|
290 struct dtx_glyphmap *gmap;
|
nuclear@0
|
291
|
nuclear@0
|
292 if(!(fp = fopen(fname, "r"))) {
|
nuclear@0
|
293 return 0;
|
nuclear@0
|
294 }
|
nuclear@0
|
295 gmap = dtx_load_glyphmap_stream(fp);
|
nuclear@0
|
296 fclose(fp);
|
nuclear@0
|
297 return gmap;
|
nuclear@0
|
298 }
|
nuclear@0
|
299
|
nuclear@0
|
300 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp)
|
nuclear@0
|
301 {
|
nuclear@0
|
302 char buf[512];
|
nuclear@0
|
303 int hdr_lines = 0;
|
nuclear@0
|
304 struct dtx_glyphmap *gmap;
|
nuclear@0
|
305 struct glyph *glyphs = 0;
|
nuclear@0
|
306 int min_code = INT_MAX;
|
nuclear@0
|
307 int max_code = INT_MIN;
|
nuclear@0
|
308 int i, max_pixval, num_pixels;
|
nuclear@0
|
309
|
nuclear@0
|
310 if(!(gmap = calloc(1, sizeof *gmap))) {
|
nuclear@0
|
311 fperror("failed to allocate glyphmap");
|
nuclear@0
|
312 return 0;
|
nuclear@0
|
313 }
|
nuclear@0
|
314
|
nuclear@0
|
315 while(hdr_lines < 3) {
|
nuclear@0
|
316 char *line = buf;
|
nuclear@0
|
317 if(!fgets(buf, sizeof buf, fp)) {
|
nuclear@0
|
318 fperror("unexpected end of file");
|
nuclear@0
|
319 goto err;
|
nuclear@0
|
320 }
|
nuclear@0
|
321
|
nuclear@0
|
322 while(isspace(*line)) {
|
nuclear@0
|
323 line++;
|
nuclear@0
|
324 }
|
nuclear@0
|
325
|
nuclear@0
|
326 if(line[0] == '#') {
|
nuclear@0
|
327 struct glyph *g;
|
nuclear@0
|
328 int c;
|
nuclear@0
|
329 float x, y, xsz, ysz, res;
|
nuclear@0
|
330
|
nuclear@0
|
331 res = sscanf(line + 1, "%d: %fx%f+%f+%f\n", &c, &xsz, &ysz, &x, &y);
|
nuclear@0
|
332 if(res != 5) {
|
nuclear@0
|
333 fprintf(stderr, "%s: invalid glyph info line\n", __func__);
|
nuclear@0
|
334 goto err;
|
nuclear@0
|
335 }
|
nuclear@0
|
336
|
nuclear@0
|
337 if(!(g = malloc(sizeof *g))) {
|
nuclear@0
|
338 fperror("failed to allocate glyph");
|
nuclear@0
|
339 goto err;
|
nuclear@0
|
340 }
|
nuclear@0
|
341 g->code = c;
|
nuclear@0
|
342 g->x = x;
|
nuclear@0
|
343 g->y = y;
|
nuclear@0
|
344 g->width = xsz;
|
nuclear@0
|
345 g->height = ysz;
|
nuclear@0
|
346 g->next = glyphs;
|
nuclear@0
|
347 glyphs = g;
|
nuclear@0
|
348
|
nuclear@0
|
349 if(c < min_code) {
|
nuclear@0
|
350 min_code = c;
|
nuclear@0
|
351 }
|
nuclear@0
|
352 if(c > max_code) {
|
nuclear@0
|
353 max_code = c;
|
nuclear@0
|
354 }
|
nuclear@0
|
355 } else {
|
nuclear@0
|
356 switch(hdr_lines) {
|
nuclear@0
|
357 case 0:
|
nuclear@0
|
358 if(0[line] != 'P' || 1[line] != '6') {
|
nuclear@0
|
359 fprintf(stderr, "%s: invalid file format (magic)\n", __func__);
|
nuclear@0
|
360 goto err;
|
nuclear@0
|
361 }
|
nuclear@0
|
362 break;
|
nuclear@0
|
363
|
nuclear@0
|
364 case 1:
|
nuclear@0
|
365 if(sscanf(line, "%d %d", &gmap->xsz, &gmap->ysz) != 2) {
|
nuclear@0
|
366 fprintf(stderr, "%s: invalid file format (dim)\n", __func__);
|
nuclear@0
|
367 goto err;
|
nuclear@0
|
368 }
|
nuclear@0
|
369 break;
|
nuclear@0
|
370
|
nuclear@0
|
371 case 2:
|
nuclear@0
|
372 {
|
nuclear@0
|
373 char *endp;
|
nuclear@0
|
374 max_pixval = strtol(line, &endp, 10);
|
nuclear@0
|
375 if(endp == line) {
|
nuclear@0
|
376 fprintf(stderr, "%s: invalid file format (maxval)\n", __func__);
|
nuclear@0
|
377 goto err;
|
nuclear@0
|
378 }
|
nuclear@0
|
379 }
|
nuclear@0
|
380 break;
|
nuclear@0
|
381
|
nuclear@0
|
382 default:
|
nuclear@0
|
383 break; /* can't happen */
|
nuclear@0
|
384 }
|
nuclear@0
|
385 hdr_lines++;
|
nuclear@0
|
386 }
|
nuclear@0
|
387 }
|
nuclear@0
|
388
|
nuclear@0
|
389 num_pixels = gmap->xsz * gmap->ysz;
|
nuclear@0
|
390 if(!(gmap->pixels = malloc(num_pixels))) {
|
nuclear@0
|
391 fperror("failed to allocate pixels");
|
nuclear@0
|
392 goto err;
|
nuclear@0
|
393 }
|
nuclear@0
|
394
|
nuclear@0
|
395 for(i=0; i<num_pixels; i++) {
|
nuclear@0
|
396 long c = fgetc(fp);
|
nuclear@0
|
397 if(c == -1) {
|
nuclear@0
|
398 fprintf(stderr, "unexpected end of file while reading pixels\n");
|
nuclear@0
|
399 goto err;
|
nuclear@0
|
400 }
|
nuclear@0
|
401 gmap->pixels[i] = 255 * c / max_pixval;
|
nuclear@0
|
402 fseek(fp, 2, SEEK_CUR);
|
nuclear@0
|
403 }
|
nuclear@0
|
404
|
nuclear@0
|
405 gmap->cstart = min_code;
|
nuclear@0
|
406 gmap->cend = max_code + 1;
|
nuclear@0
|
407 gmap->crange = gmap->cend - gmap->cstart;
|
nuclear@0
|
408
|
nuclear@0
|
409 if(!(gmap->glyphs = calloc(gmap->crange, sizeof *gmap->glyphs))) {
|
nuclear@0
|
410 fperror("failed to allocate glyph info");
|
nuclear@0
|
411 goto err;
|
nuclear@0
|
412 }
|
nuclear@0
|
413
|
nuclear@0
|
414 while(glyphs) {
|
nuclear@0
|
415 struct glyph *g = glyphs;
|
nuclear@0
|
416 glyphs = glyphs->next;
|
nuclear@0
|
417
|
nuclear@0
|
418 gmap->glyphs[g->code - gmap->cstart] = *g;
|
nuclear@0
|
419 free(g);
|
nuclear@0
|
420 }
|
nuclear@0
|
421 return gmap;
|
nuclear@0
|
422
|
nuclear@0
|
423 err:
|
nuclear@0
|
424 dtx_free_glyphmap(gmap);
|
nuclear@0
|
425 while(glyphs) {
|
nuclear@0
|
426 void *tmp = glyphs;
|
nuclear@0
|
427 glyphs = glyphs->next;
|
nuclear@0
|
428 free(tmp);
|
nuclear@0
|
429 }
|
nuclear@0
|
430 return 0;
|
nuclear@0
|
431 }
|
nuclear@0
|
432
|
nuclear@0
|
433 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap)
|
nuclear@0
|
434 {
|
nuclear@0
|
435 FILE *fp;
|
nuclear@0
|
436 int res;
|
nuclear@0
|
437
|
nuclear@0
|
438 if(!(fp = fopen(fname, "wb"))) {
|
nuclear@0
|
439 fprintf(stderr, "%s: failed to open file: %s: %s\n", __func__, fname, strerror(errno));
|
nuclear@0
|
440 return -1;
|
nuclear@0
|
441 }
|
nuclear@0
|
442 res = dtx_save_glyphmap_stream(fp, gmap);
|
nuclear@0
|
443 fclose(fp);
|
nuclear@0
|
444 return res;
|
nuclear@0
|
445 }
|
nuclear@0
|
446
|
nuclear@0
|
447 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap)
|
nuclear@0
|
448 {
|
nuclear@0
|
449 int i, num_pixels;
|
nuclear@0
|
450 struct glyph *g = gmap->glyphs;
|
nuclear@0
|
451
|
nuclear@0
|
452 fprintf(fp, "P6\n%d %d\n", gmap->xsz, gmap->ysz);
|
nuclear@0
|
453 for(i=0; i<gmap->crange; i++) {
|
nuclear@0
|
454 fprintf(fp, "# %d: %fx%f+%f+%f\n", g->code, g->width, g->height, g->x, g->y);
|
nuclear@0
|
455 g++;
|
nuclear@0
|
456 }
|
nuclear@0
|
457 fprintf(fp, "255\n");
|
nuclear@0
|
458
|
nuclear@0
|
459 num_pixels = gmap->xsz * gmap->ysz;
|
nuclear@0
|
460 for(i=0; i<num_pixels; i++) {
|
nuclear@0
|
461 int c = gmap->pixels[i];
|
nuclear@0
|
462 fputc(c, fp);
|
nuclear@0
|
463 fputc(c, fp);
|
nuclear@0
|
464 fputc(c, fp);
|
nuclear@0
|
465 }
|
nuclear@0
|
466 return 0;
|
nuclear@0
|
467 }
|
nuclear@0
|
468
|
nuclear@0
|
469
|
nuclear@0
|
470 static void calc_best_size(int total_width, int max_glyph_height, int padding, int pow2, int *imgw, int *imgh)
|
nuclear@0
|
471 {
|
nuclear@0
|
472 int xsz, ysz, num_rows;
|
nuclear@0
|
473 float aspect;
|
nuclear@0
|
474
|
nuclear@0
|
475 for(xsz=2; xsz<=MAX_IMG_WIDTH; xsz *= 2) {
|
nuclear@0
|
476 num_rows = total_width / xsz + 1;
|
nuclear@0
|
477
|
nuclear@0
|
478 /* take into account the one extra padding for each row after the first */
|
nuclear@0
|
479 num_rows = (total_width + padding * (num_rows - 1)) / xsz + 1;
|
nuclear@0
|
480
|
nuclear@0
|
481 ysz = num_rows * (max_glyph_height + padding) + padding;
|
nuclear@0
|
482 if(pow2) {
|
nuclear@0
|
483 ysz = next_pow2(ysz);
|
nuclear@0
|
484 }
|
nuclear@0
|
485 aspect = (float)xsz / (float)ysz;
|
nuclear@0
|
486
|
nuclear@0
|
487 if(aspect >= 1.0) {
|
nuclear@0
|
488 break;
|
nuclear@0
|
489 }
|
nuclear@0
|
490 }
|
nuclear@0
|
491
|
nuclear@0
|
492 if(xsz > MAX_IMG_WIDTH) {
|
nuclear@0
|
493 xsz = MAX_IMG_WIDTH;
|
nuclear@0
|
494 }
|
nuclear@0
|
495
|
nuclear@0
|
496 *imgw = xsz;
|
nuclear@0
|
497 *imgh = ysz;
|
nuclear@0
|
498 }
|
nuclear@0
|
499
|
nuclear@0
|
500
|
nuclear@0
|
501 static int next_pow2(int x)
|
nuclear@0
|
502 {
|
nuclear@0
|
503 x--;
|
nuclear@0
|
504 x = (x >> 1) | x;
|
nuclear@0
|
505 x = (x >> 2) | x;
|
nuclear@0
|
506 x = (x >> 4) | x;
|
nuclear@0
|
507 x = (x >> 8) | x;
|
nuclear@0
|
508 x = (x >> 16) | x;
|
nuclear@0
|
509 return x + 1;
|
nuclear@0
|
510 }
|