Remove bitmap mode from blenfont, only draw with textures.
[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         GlyphCacheBLF *gc;
196         GlyphBLF *g;
197         FT_Error err;
198         FT_Bitmap bitmap;
199         FT_BBox bbox;
200         unsigned int key;
201         int do_new;
202
203         g= blf_glyph_search(font->glyph_cache, c);
204         if (g)
205                 do_new= 0;
206         else
207                 do_new= 1;
208
209         err= FT_Load_Glyph(font->face, index, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
210         if (err)
211                 return(NULL);
212
213         /* get the glyph. */
214         slot= font->face->glyph;
215
216         err= FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
217         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
218                 return(NULL);
219
220         if (do_new) {
221                 g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
222                 g->next= NULL;
223                 g->prev= NULL;
224                 g->c= c;
225         }
226
227         gc= font->glyph_cache;
228         if (gc->cur_tex == -1) {
229                 blf_glyph_cache_texture(font, gc);
230                 gc->x_offs= gc->pad;
231                 gc->y_offs= gc->pad;
232         }
233
234         if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
235                 gc->x_offs= gc->pad;
236                 gc->y_offs += gc->max_glyph_height;
237
238                 if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
239                         gc->y_offs= gc->pad;
240                         blf_glyph_cache_texture(font, gc);
241                 }
242         }
243
244         bitmap= slot->bitmap;
245         g->tex= gc->textures[gc->cur_tex];
246
247         g->xoff= gc->x_offs;
248         g->yoff= gc->y_offs;
249         g->width= bitmap.width;
250         g->height= bitmap.rows;
251
252         if (g->width && g->height) {
253                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
254                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
255                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
256                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
257
258                 glBindTexture(GL_TEXTURE_2D, g->tex);
259                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.buffer);
260                 glPopClientAttrib();
261         }
262
263         g->advance= ((float)slot->advance.x) / 64.0f;
264         g->pos_x= slot->bitmap_left;
265         g->pos_y= slot->bitmap_top;
266
267         FT_Outline_Get_CBox(&(slot->outline), &bbox);
268         g->box.xmin= ((float)bbox.xMin) / 64.0f;
269         g->box.xmax= ((float)bbox.xMax) / 64.0f;
270         g->box.ymin= ((float)bbox.yMin) / 64.0f;
271         g->box.ymax= ((float)bbox.yMax) / 64.0f;
272
273         g->uv[0][0]= ((float)g->xoff) / ((float)gc->p2_width);
274         g->uv[0][1]= ((float)g->yoff) / ((float)gc->p2_height);
275         g->uv[1][0]= ((float)(g->xoff + g->width)) / ((float)gc->p2_width);
276         g->uv[1][1]= ((float)(g->yoff + g->height)) / ((float)gc->p2_height);
277
278         /* update the x offset for the next glyph. */
279         gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
280
281         if (do_new) {
282                 key= blf_hash(g->c);
283                 BLI_addhead(&(gc->bucket[key]), g);
284                 gc->rem_glyphs--;
285         }
286
287         return(g);
288 }
289
290 void blf_glyph_free(GlyphBLF *g)
291 {
292         /* don't need free the texture, the GlyphCache already
293          * have a list of all the texture and free it.
294          */
295         MEM_freeN(g);
296 }
297
298 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
299 {
300         
301         glBegin(GL_QUADS);
302         glTexCoord2f(uv[0][0], uv[0][1]);
303         glVertex2f(dx, y1);
304         
305         glTexCoord2f(uv[0][0], uv[1][1]);
306         glVertex2f(dx, y2);
307         
308         glTexCoord2f(uv[1][0], uv[1][1]);
309         glVertex2f(dx1, y2);
310         
311         glTexCoord2f(uv[1][0], uv[0][1]);
312         glVertex2f(dx1, y1);
313         glEnd();
314         
315 }
316
317 static void blf_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2)
318 {
319         float soft[25]= {
320                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f, 
321                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
322                 2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f, 
323                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
324                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
325         
326         float color[4], *fp= soft;
327         int dx, dy;
328         
329         glGetFloatv(GL_CURRENT_COLOR, color);
330         
331         for(dx=-2; dx<3; dx++) {
332                 for(dy=-2; dy<3; dy++, fp++) {
333                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
334                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
335                 }
336         }
337         
338         glColor4fv(color);
339 }
340
341 static void blf_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2)
342 {
343         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};
344         float color[4], *fp= soft;
345         int dx, dy;
346         
347         glGetFloatv(GL_CURRENT_COLOR, color);
348         
349         for(dx=-1; dx<2; dx++) {
350                 for(dy=-1; dy<2; dy++, fp++) {
351                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
352                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
353                 }
354         }
355         
356         glColor4fv(color);
357 }
358
359 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
360 {
361         GLint cur_tex;
362         float dx, dx1;
363         float y1, y2;
364         float xo, yo;
365         float color[4];
366
367         xo= 0.0f;
368         yo= 0.0f;
369
370         if (font->flags & BLF_SHADOW) {
371                 xo= x;
372                 yo= y;
373                 x += font->shadow_x;
374                 y += font->shadow_y;
375         }
376
377         dx= floor(x + g->pos_x);
378         dx1= dx + g->width;
379         y1= y + g->pos_y;
380         y2= y + g->pos_y - g->height;
381
382         if (font->flags & BLF_CLIPPING) {
383                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
384                         return(0);
385                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
386                         return(0);
387                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
388                         return(0);
389                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
390                         return(0);
391         }
392
393         glGetIntegerv(GL_TEXTURE_2D_BINDING_EXT, &cur_tex);
394         if (cur_tex != g->tex)
395                 glBindTexture(GL_TEXTURE_2D, g->tex);
396
397         if (font->flags & BLF_SHADOW) {
398                 glGetFloatv(GL_CURRENT_COLOR, color);
399                 glColor4fv(font->shadow_col);
400
401                 if (font->shadow == 3)
402                         blf_texture3_draw(g->uv, dx, y1, dx1, y2);
403                 else if (font->shadow == 5)
404                         blf_texture5_draw(g->uv, dx, y1, dx1, y2);
405                 else
406                         blf_texture_draw(g->uv, dx, y1, dx1, y2);
407
408                 glColor4fv(color);
409                 x= xo;
410                 y= yo;
411
412                 dx= floor(x + g->pos_x);
413                 dx1= dx + g->width;
414                 y1= y + g->pos_y;
415                 y2= y + g->pos_y - g->height;
416         }
417
418         if (font->blur==3)
419                 blf_texture3_draw(g->uv, dx, y1, dx1, y2);
420         else if (font->blur==5)
421                 blf_texture5_draw(g->uv, dx, y1, dx1, y2);
422         else
423                 blf_texture_draw(g->uv, dx, y1, dx1, y2);
424
425         return(1);
426 }