python api for ID property access by Joseph Eager, copied from blender 2.4x.
[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         if (gc->cur_tex+1 > 0)
135                 glDeleteTextures(gc->cur_tex+1, gc->textures);
136         free((void *)gc->textures);
137         MEM_freeN(gc);
138 }
139
140 static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
141 {
142         int tot_mem, i;
143         unsigned char *buf;
144
145         /* move the index. */
146         gc->cur_tex++;
147
148         if (gc->cur_tex >= gc->ntex) {
149                 gc->ntex *= 2;
150                 gc->textures= (GLuint *)realloc((void *)gc->textures, sizeof(GLuint)*gc->ntex);
151         }
152
153         gc->p2_width= blf_next_p2((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2));
154         if (gc->p2_width > font->max_tex_size)
155                 gc->p2_width= font->max_tex_size;
156
157         i= (int)((gc->p2_width - (gc->pad * 2)) / gc->max_glyph_width);
158         gc->p2_height= blf_next_p2(((gc->num_glyphs / i) + 1) * gc->max_glyph_height);
159
160         if (gc->p2_height > font->max_tex_size)
161                 gc->p2_height= font->max_tex_size;
162
163         tot_mem= gc->p2_width * gc->p2_height;
164         buf= (unsigned char *)malloc(tot_mem);
165         memset((void *)buf, 0, tot_mem);
166
167         glGenTextures(1, &gc->textures[gc->cur_tex]);
168         glBindTexture(GL_TEXTURE_2D, gc->textures[gc->cur_tex]);
169         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
170         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
171         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
172         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
173
174         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buf);
175         free((void *)buf);
176 }
177
178 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
179 {
180         GlyphBLF *p;
181         unsigned int key;
182
183         key= blf_hash(c);
184         p= gc->bucket[key].first;
185         while (p) {
186                 if (p->c == c)
187                         return(p);
188                 p= p->next;
189         }
190         return(NULL);
191 }
192
193 GlyphBLF *blf_glyph_add(FontBLF *font, FT_UInt index, unsigned int c)
194 {
195         FT_GlyphSlot slot;
196         GlyphBLF *g;
197         FT_Error err;
198         FT_Bitmap bitmap;
199         FT_BBox bbox;
200         unsigned int key;
201
202         g= blf_glyph_search(font->glyph_cache, c);
203         if (g)
204                 return(g);
205
206         err= FT_Load_Glyph(font->face, index, FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP);
207         if (err)
208                 return(NULL);
209
210         /* get the glyph. */
211         slot= font->face->glyph;
212
213         err= FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
214         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
215                 return(NULL);
216
217         g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
218         g->next= NULL;
219         g->prev= NULL;
220         g->c= c;
221         g->idx= index;
222         g->tex= 0;
223         g->build_tex= 0;
224         g->bitmap= NULL;
225         g->xoff= -1;
226         g->yoff= -1;
227         g->uv[0][0]= 0.0f;
228         g->uv[0][1]= 0.0f;
229         g->uv[1][0]= 0.0f;
230         g->uv[1][1]= 0.0f;
231         bitmap= slot->bitmap;
232         g->width= bitmap.width;
233         g->height= bitmap.rows;
234
235         if (g->width && g->height) {
236                 g->bitmap= (unsigned char *)MEM_mallocN(g->width * g->height, "glyph bitmap");
237                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, g->width * g->height);
238         }
239
240         g->advance= ((float)slot->advance.x) / 64.0f;
241         g->pos_x= slot->bitmap_left;
242         g->pos_y= slot->bitmap_top;
243         g->pitch= slot->bitmap.pitch;
244
245         FT_Outline_Get_CBox(&(slot->outline), &bbox);
246         g->box.xmin= ((float)bbox.xMin) / 64.0f;
247         g->box.xmax= ((float)bbox.xMax) / 64.0f;
248         g->box.ymin= ((float)bbox.yMin) / 64.0f;
249         g->box.ymax= ((float)bbox.yMax) / 64.0f;
250
251         key= blf_hash(g->c);
252         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
253         return(g);
254 }
255
256 void blf_glyph_free(GlyphBLF *g)
257 {
258         /* don't need free the texture, the GlyphCache already
259          * have a list of all the texture and free it.
260          */
261         if (g->bitmap)
262                 MEM_freeN(g->bitmap);
263         MEM_freeN(g);
264 }
265
266 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
267 {
268         
269         glBegin(GL_QUADS);
270         glTexCoord2f(uv[0][0], uv[0][1]);
271         glVertex2f(dx, y1);
272         
273         glTexCoord2f(uv[0][0], uv[1][1]);
274         glVertex2f(dx, y2);
275         
276         glTexCoord2f(uv[1][0], uv[1][1]);
277         glVertex2f(dx1, y2);
278         
279         glTexCoord2f(uv[1][0], uv[0][1]);
280         glVertex2f(dx1, y1);
281         glEnd();
282         
283 }
284
285 static void blf_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2)
286 {
287         float soft[25]= {
288                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f, 
289                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
290                 2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f, 
291                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
292                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
293         
294         float color[4], *fp= soft;
295         int dx, dy;
296         
297         glGetFloatv(GL_CURRENT_COLOR, color);
298         
299         for(dx=-2; dx<3; dx++) {
300                 for(dy=-2; dy<3; dy++, fp++) {
301                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
302                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
303                 }
304         }
305         
306         glColor4fv(color);
307 }
308
309 static void blf_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2)
310 {
311         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};
312         float color[4], *fp= soft;
313         int dx, dy;
314         
315         glGetFloatv(GL_CURRENT_COLOR, color);
316         
317         for(dx=-1; dx<2; dx++) {
318                 for(dy=-1; dy<2; dy++, fp++) {
319                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
320                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
321                 }
322         }
323         
324         glColor4fv(color);
325 }
326
327 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
328 {
329         GlyphCacheBLF *gc;
330         GLint cur_tex;
331         float dx, dx1;
332         float y1, y2;
333         float xo, yo;
334         float color[4];
335
336         if ((!g->width) || (!g->height))
337                 return(1);
338
339         if (g->build_tex == 0) {
340                 gc= font->glyph_cache;
341
342                 if (font->max_tex_size == -1)
343                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->max_tex_size);
344
345                 if (gc->cur_tex == -1) {
346                         blf_glyph_cache_texture(font, gc);
347                         gc->x_offs= gc->pad;
348                         gc->y_offs= gc->pad;
349                 }
350
351                 if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
352                         gc->x_offs= gc->pad;
353                         gc->y_offs += gc->max_glyph_height;
354
355                         if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
356                                 gc->y_offs= gc->pad;
357                                 blf_glyph_cache_texture(font, gc);
358                         }
359                 }
360
361                 g->tex= gc->textures[gc->cur_tex];
362                 g->xoff= gc->x_offs;
363                 g->yoff= gc->y_offs;
364
365                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
366                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
367                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
368                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
369
370                 glBindTexture(GL_TEXTURE_2D, g->tex);
371                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap);
372                 glPopClientAttrib();
373
374                 g->uv[0][0]= ((float)g->xoff) / ((float)gc->p2_width);
375                 g->uv[0][1]= ((float)g->yoff) / ((float)gc->p2_height);
376                 g->uv[1][0]= ((float)(g->xoff + g->width)) / ((float)gc->p2_width);
377                 g->uv[1][1]= ((float)(g->yoff + g->height)) / ((float)gc->p2_height);
378
379                 /* update the x offset for the next glyph. */
380                 gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
381
382                 gc->rem_glyphs--;
383                 g->build_tex= 1;
384         }
385
386         xo= 0.0f;
387         yo= 0.0f;
388
389         if (font->flags & BLF_SHADOW) {
390                 xo= x;
391                 yo= y;
392                 x += font->shadow_x;
393                 y += font->shadow_y;
394         }
395
396         dx= floor(x + g->pos_x);
397         dx1= dx + g->width;
398         y1= y + g->pos_y;
399         y2= y + g->pos_y - g->height;
400
401         if (font->flags & BLF_CLIPPING) {
402                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
403                         return(0);
404                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
405                         return(0);
406                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
407                         return(0);
408                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
409                         return(0);
410         }
411
412         glGetIntegerv(GL_TEXTURE_2D_BINDING_EXT, &cur_tex);
413         if (cur_tex != g->tex)
414                 glBindTexture(GL_TEXTURE_2D, g->tex);
415
416         if (font->flags & BLF_SHADOW) {
417                 glGetFloatv(GL_CURRENT_COLOR, color);
418                 glColor4fv(font->shadow_col);
419
420                 if (font->shadow == 3)
421                         blf_texture3_draw(g->uv, dx, y1, dx1, y2);
422                 else if (font->shadow == 5)
423                         blf_texture5_draw(g->uv, dx, y1, dx1, y2);
424                 else
425                         blf_texture_draw(g->uv, dx, y1, dx1, y2);
426
427                 glColor4fv(color);
428                 x= xo;
429                 y= yo;
430
431                 dx= floor(x + g->pos_x);
432                 dx1= dx + g->width;
433                 y1= y + g->pos_y;
434                 y2= y + g->pos_y - g->height;
435         }
436
437         if (font->blur==3)
438                 blf_texture3_draw(g->uv, dx, y1, dx1, y2);
439         else if (font->blur==5)
440                 blf_texture5_draw(g->uv, dx, y1, dx1, y2);
441         else
442                 blf_texture_draw(g->uv, dx, y1, dx1, y2);
443
444         return(1);
445 }