Cleanup a little to add "draw to buffer" (and bring back stamp).
[blender.git] / source / blender / blenfont / intern / blf_glyph.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #include <ft2build.h>
35
36 #include FT_FREETYPE_H
37 #include FT_GLYPH_H
38 #include FT_OUTLINE_H
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_listBase.h"
43 #include "DNA_vec_types.h"
44
45 #include "BKE_utildefines.h"
46
47 #include "BLI_blenlib.h"
48 #include "BLI_linklist.h"       /* linknode */
49 #include "BLI_string.h"
50
51 #include "BIF_gl.h"
52 #include "BLF_api.h"
53
54 #include "blf_internal_types.h"
55 #include "blf_internal.h"
56
57
58 GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, int size, int dpi)
59 {
60         GlyphCacheBLF *p;
61
62         p= (GlyphCacheBLF *)font->cache.first;
63         while (p) {
64                 if (p->size == size && p->dpi == dpi)
65                         return(p);
66                 p= p->next;
67         }
68         return(NULL);
69 }
70
71 /* Create a new glyph cache for the current size and dpi. */
72 GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
73 {
74         GlyphCacheBLF *gc;
75         int i;
76
77         gc= (GlyphCacheBLF *)MEM_mallocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
78         gc->next= NULL;
79         gc->prev= NULL;
80         gc->size= font->size;
81         gc->dpi= font->dpi;
82
83         for (i= 0; i < 257; i++) {
84                 gc->bucket[i].first= NULL;
85                 gc->bucket[i].last= NULL;
86         }
87
88         gc->textures= (GLuint *)malloc(sizeof(GLuint)*256);
89         gc->ntex= 256;
90         gc->cur_tex= -1;
91         gc->x_offs= 0;
92         gc->y_offs= 0;
93         gc->pad= 3;
94
95         gc->num_glyphs= font->face->num_glyphs;
96         gc->rem_glyphs= font->face->num_glyphs;
97         gc->ascender= ((float)font->face->size->metrics.ascender) / 64.0f;
98         gc->descender= ((float)font->face->size->metrics.descender) / 64.0f;
99
100         if (FT_IS_SCALABLE(font->face)) {
101                 gc->max_glyph_width= (float)((font->face->bbox.xMax - font->face->bbox.xMin) *
102                                         (((float)font->face->size->metrics.x_ppem) /
103                                          ((float)font->face->units_per_EM)));
104
105                 gc->max_glyph_height= (float)((font->face->bbox.yMax - font->face->bbox.yMin) *
106                                         (((float)font->face->size->metrics.y_ppem) /
107                                          ((float)font->face->units_per_EM)));
108         }
109         else {
110                 gc->max_glyph_width= ((float)font->face->size->metrics.max_advance) / 64.0f;
111                 gc->max_glyph_height= ((float)font->face->size->metrics.height) / 64.0f;
112         }
113
114         gc->p2_width= 0;
115         gc->p2_height= 0;
116
117         BLI_addhead(&font->cache, gc);
118         return(gc);
119 }
120
121 void blf_glyph_cache_free(GlyphCacheBLF *gc)
122 {
123         GlyphBLF *g;
124         int i;
125
126         for (i= 0; i < 257; i++) {
127                 while (gc->bucket[i].first) {
128                         g= gc->bucket[i].first;
129                         BLI_remlink(&(gc->bucket[i]), g);
130                         blf_glyph_free(g);
131                 }
132         }
133
134         glDeleteTextures(gc->cur_tex+1, gc->textures);
135         free((void *)gc->textures);
136         MEM_freeN(gc);
137 }
138
139 void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
140 {
141         int tot_mem, i;
142         unsigned char *buf;
143
144         /* move the index. */
145         gc->cur_tex++;
146
147         if (gc->cur_tex >= gc->ntex) {
148                 gc->ntex *= 2;
149                 gc->textures= (GLuint *)realloc((void *)gc->textures, sizeof(GLuint)*gc->ntex);
150         }
151
152         gc->p2_width= blf_next_p2((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2));
153         if (gc->p2_width > font->max_tex_size)
154                 gc->p2_width= font->max_tex_size;
155
156         i= (int)((gc->p2_width - (gc->pad * 2)) / gc->max_glyph_width);
157         gc->p2_height= blf_next_p2(((gc->num_glyphs / i) + 1) * gc->max_glyph_height);
158
159         if (gc->p2_height > font->max_tex_size)
160                 gc->p2_height= font->max_tex_size;
161
162         tot_mem= gc->p2_width * gc->p2_height;
163         buf= (unsigned char *)malloc(tot_mem);
164         memset((void *)buf, 0, tot_mem);
165
166         glGenTextures(1, &gc->textures[gc->cur_tex]);
167         glBindTexture(GL_TEXTURE_2D, gc->textures[gc->cur_tex]);
168         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
169         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
170         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
171         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
172
173         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buf);
174         free((void *)buf);
175 }
176
177 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
178 {
179         GlyphBLF *p;
180         unsigned int key;
181
182         key= blf_hash(c);
183         p= gc->bucket[key].first;
184         while (p) {
185                 if (p->c == c)
186                         return(p);
187                 p= p->next;
188         }
189         return(NULL);
190 }
191
192 GlyphBLF *blf_glyph_add(FontBLF *font, FT_UInt index, unsigned int c)
193 {
194         FT_GlyphSlot slot;
195         GlyphBLF *g;
196         FT_Error err;
197         FT_Bitmap bitmap;
198         FT_BBox bbox;
199         unsigned int key;
200
201         g= blf_glyph_search(font->glyph_cache, c);
202         if (g)
203                 return(g);
204
205         err= FT_Load_Glyph(font->face, index, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
206         if (err)
207                 return(NULL);
208
209         /* get the glyph. */
210         slot= font->face->glyph;
211
212         err= FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
213         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
214                 return(NULL);
215
216         g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
217         g->next= NULL;
218         g->prev= NULL;
219         g->c= c;
220         g->tex= 0;
221         g->build_tex= 0;
222         g->bitmap= NULL;
223         g->xoff= -1;
224         g->yoff= -1;
225         g->uv[0][0]= 0.0f;
226         g->uv[0][1]= 0.0f;
227         g->uv[1][0]= 0.0f;
228         g->uv[1][1]= 0.0f;
229         bitmap= slot->bitmap;
230         g->width= bitmap.width;
231         g->height= bitmap.rows;
232
233         if (g->width && g->height) {
234                 g->bitmap= (unsigned char *)MEM_mallocN(g->width * g->height, "glyph bitmap");
235                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, g->width * g->height);
236         }
237
238         g->advance= ((float)slot->advance.x) / 64.0f;
239         g->pos_x= slot->bitmap_left;
240         g->pos_y= slot->bitmap_top;
241
242         FT_Outline_Get_CBox(&(slot->outline), &bbox);
243         g->box.xmin= ((float)bbox.xMin) / 64.0f;
244         g->box.xmax= ((float)bbox.xMax) / 64.0f;
245         g->box.ymin= ((float)bbox.yMin) / 64.0f;
246         g->box.ymax= ((float)bbox.yMax) / 64.0f;
247
248         key= blf_hash(g->c);
249         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
250         return(g);
251 }
252
253 void blf_glyph_free(GlyphBLF *g)
254 {
255         /* don't need free the texture, the GlyphCache already
256          * have a list of all the texture and free it.
257          */
258         if (g->bitmap)
259                 MEM_freeN(g->bitmap);
260         MEM_freeN(g);
261 }
262
263 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
264 {
265         
266         glBegin(GL_QUADS);
267         glTexCoord2f(uv[0][0], uv[0][1]);
268         glVertex2f(dx, y1);
269         
270         glTexCoord2f(uv[0][0], uv[1][1]);
271         glVertex2f(dx, y2);
272         
273         glTexCoord2f(uv[1][0], uv[1][1]);
274         glVertex2f(dx1, y2);
275         
276         glTexCoord2f(uv[1][0], uv[0][1]);
277         glVertex2f(dx1, y1);
278         glEnd();
279         
280 }
281
282 static void blf_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2)
283 {
284         float soft[25]= {
285                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f, 
286                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
287                 2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f, 
288                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
289                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
290         
291         float color[4], *fp= soft;
292         int dx, dy;
293         
294         glGetFloatv(GL_CURRENT_COLOR, color);
295         
296         for(dx=-2; dx<3; dx++) {
297                 for(dy=-2; dy<3; dy++, fp++) {
298                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
299                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
300                 }
301         }
302         
303         glColor4fv(color);
304 }
305
306 static void blf_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2)
307 {
308         float soft[9]= {1/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 4/16.0f, 2/16.0f, 1/16.0f, 2/16.0f, 1/16.0f};
309         float color[4], *fp= soft;
310         int dx, dy;
311         
312         glGetFloatv(GL_CURRENT_COLOR, color);
313         
314         for(dx=-1; dx<2; dx++) {
315                 for(dy=-1; dy<2; dy++, fp++) {
316                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
317                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
318                 }
319         }
320         
321         glColor4fv(color);
322 }
323
324 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
325 {
326         GlyphCacheBLF *gc;
327         GLint cur_tex;
328         float dx, dx1;
329         float y1, y2;
330         float xo, yo;
331         float color[4];
332
333         if ((!g->width) || (!g->height))
334                 return(1);
335
336         if (g->build_tex == 0) {
337                 gc= font->glyph_cache;
338
339                 if (font->max_tex_size == -1)
340                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->max_tex_size);
341
342                 if (gc->cur_tex == -1) {
343                         blf_glyph_cache_texture(font, gc);
344                         gc->x_offs= gc->pad;
345                         gc->y_offs= gc->pad;
346                 }
347
348                 if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
349                         gc->x_offs= gc->pad;
350                         gc->y_offs += gc->max_glyph_height;
351
352                         if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
353                                 gc->y_offs= gc->pad;
354                                 blf_glyph_cache_texture(font, gc);
355                         }
356                 }
357
358                 g->tex= gc->textures[gc->cur_tex];
359                 g->xoff= gc->x_offs;
360                 g->yoff= gc->y_offs;
361
362                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
363                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
364                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
365                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
366
367                 glBindTexture(GL_TEXTURE_2D, g->tex);
368                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap);
369                 glPopClientAttrib();
370
371                 g->uv[0][0]= ((float)g->xoff) / ((float)gc->p2_width);
372                 g->uv[0][1]= ((float)g->yoff) / ((float)gc->p2_height);
373                 g->uv[1][0]= ((float)(g->xoff + g->width)) / ((float)gc->p2_width);
374                 g->uv[1][1]= ((float)(g->yoff + g->height)) / ((float)gc->p2_height);
375
376                 /* update the x offset for the next glyph. */
377                 gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
378
379                 gc->rem_glyphs--;
380                 g->build_tex= 1;
381         }
382
383         xo= 0.0f;
384         yo= 0.0f;
385
386         if (font->flags & BLF_SHADOW) {
387                 xo= x;
388                 yo= y;
389                 x += font->shadow_x;
390                 y += font->shadow_y;
391         }
392
393         dx= floor(x + g->pos_x);
394         dx1= dx + g->width;
395         y1= y + g->pos_y;
396         y2= y + g->pos_y - g->height;
397
398         if (font->flags & BLF_CLIPPING) {
399                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
400                         return(0);
401                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
402                         return(0);
403                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
404                         return(0);
405                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
406                         return(0);
407         }
408
409         glGetIntegerv(GL_TEXTURE_2D_BINDING_EXT, &cur_tex);
410         if (cur_tex != g->tex)
411                 glBindTexture(GL_TEXTURE_2D, g->tex);
412
413         if (font->flags & BLF_SHADOW) {
414                 glGetFloatv(GL_CURRENT_COLOR, color);
415                 glColor4fv(font->shadow_col);
416
417                 if (font->shadow == 3)
418                         blf_texture3_draw(g->uv, dx, y1, dx1, y2);
419                 else if (font->shadow == 5)
420                         blf_texture5_draw(g->uv, dx, y1, dx1, y2);
421                 else
422                         blf_texture_draw(g->uv, dx, y1, dx1, y2);
423
424                 glColor4fv(color);
425                 x= xo;
426                 y= yo;
427
428                 dx= floor(x + g->pos_x);
429                 dx1= dx + g->width;
430                 y1= y + g->pos_y;
431                 y2= y + g->pos_y - g->height;
432         }
433
434         if (font->blur==3)
435                 blf_texture3_draw(g->uv, dx, y1, dx1, y2);
436         else if (font->blur==5)
437                 blf_texture5_draw(g->uv, dx, y1, dx1, y2);
438         else
439                 blf_texture_draw(g->uv, dx, y1, dx1, y2);
440
441         return(1);
442 }