doxygen: prevent GPL license block from being parsed as doxygen comment.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include FT_BITMAP_H
40
41 #include "MEM_guardedalloc.h"
42
43 #include "DNA_vec_types.h"
44 #include "DNA_userdef_types.h"
45
46 #include "BLI_blenlib.h"
47
48 #include "BIF_gl.h"
49 #include "BLF_api.h"
50
51 #include "blf_internal_types.h"
52 #include "blf_internal.h"
53
54
55 GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, int size, int dpi)
56 {
57         GlyphCacheBLF *p;
58
59         p= (GlyphCacheBLF *)font->cache.first;
60         while (p) {
61                 if (p->size == size && p->dpi == dpi)
62                         return(p);
63                 p= p->next;
64         }
65         return(NULL);
66 }
67
68 /* Create a new glyph cache for the current size and dpi. */
69 GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
70 {
71         GlyphCacheBLF *gc;
72         int i;
73
74         gc= (GlyphCacheBLF *)MEM_mallocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
75         gc->next= NULL;
76         gc->prev= NULL;
77         gc->size= font->size;
78         gc->dpi= font->dpi;
79
80         for (i= 0; i < 257; i++) {
81                 gc->bucket[i].first= NULL;
82                 gc->bucket[i].last= NULL;
83         }
84
85         gc->textures= (GLuint *)malloc(sizeof(GLuint)*256);
86         gc->ntex= 256;
87         gc->cur_tex= -1;
88         gc->x_offs= 0;
89         gc->y_offs= 0;
90         gc->pad= 3;
91
92         gc->num_glyphs= font->face->num_glyphs;
93         gc->rem_glyphs= font->face->num_glyphs;
94         gc->ascender= ((float)font->face->size->metrics.ascender) / 64.0f;
95         gc->descender= ((float)font->face->size->metrics.descender) / 64.0f;
96
97         if (FT_IS_SCALABLE(font->face)) {
98                 gc->max_glyph_width= (float)((font->face->bbox.xMax - font->face->bbox.xMin) *
99                                         (((float)font->face->size->metrics.x_ppem) /
100                                          ((float)font->face->units_per_EM)));
101
102                 gc->max_glyph_height= (float)((font->face->bbox.yMax - font->face->bbox.yMin) *
103                                         (((float)font->face->size->metrics.y_ppem) /
104                                          ((float)font->face->units_per_EM)));
105         }
106         else {
107                 gc->max_glyph_width= ((float)font->face->size->metrics.max_advance) / 64.0f;
108                 gc->max_glyph_height= ((float)font->face->size->metrics.height) / 64.0f;
109         }
110
111         gc->p2_width= 0;
112         gc->p2_height= 0;
113
114         BLI_addhead(&font->cache, gc);
115         return(gc);
116 }
117
118 void blf_glyph_cache_clear(FontBLF *font)
119 {
120         GlyphCacheBLF *gc;
121         GlyphBLF *g;
122         int i;
123
124         for(gc=font->cache.first; gc; gc=gc->next) {
125                 for (i= 0; i < 257; i++) {
126                         while (gc->bucket[i].first) {
127                                 g= gc->bucket[i].first;
128                                 BLI_remlink(&(gc->bucket[i]), g);
129                                 blf_glyph_free(g);
130                         }
131                 }
132         }
133 }
134
135 void blf_glyph_cache_free(GlyphCacheBLF *gc)
136 {
137         GlyphBLF *g;
138         int i;
139
140         for (i= 0; i < 257; i++) {
141                 while (gc->bucket[i].first) {
142                         g= gc->bucket[i].first;
143                         BLI_remlink(&(gc->bucket[i]), g);
144                         blf_glyph_free(g);
145                 }
146         }
147
148         if (gc->cur_tex+1 > 0)
149                 glDeleteTextures(gc->cur_tex+1, gc->textures);
150         free((void *)gc->textures);
151         MEM_freeN(gc);
152 }
153
154 static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
155 {
156         int tot_mem, i;
157         unsigned char *buf;
158
159         /* move the index. */
160         gc->cur_tex++;
161
162         if (gc->cur_tex >= gc->ntex) {
163                 gc->ntex *= 2;
164                 gc->textures= (GLuint *)realloc((void *)gc->textures, sizeof(GLuint)*gc->ntex);
165         }
166
167         gc->p2_width= blf_next_p2((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2));
168         if (gc->p2_width > font->max_tex_size)
169                 gc->p2_width= font->max_tex_size;
170
171         i= (int)((gc->p2_width - (gc->pad * 2)) / gc->max_glyph_width);
172         gc->p2_height= blf_next_p2(((gc->num_glyphs / i) + 1) * gc->max_glyph_height);
173
174         if (gc->p2_height > font->max_tex_size)
175                 gc->p2_height= font->max_tex_size;
176
177         tot_mem= gc->p2_width * gc->p2_height;
178         buf= (unsigned char *)malloc(tot_mem);
179         memset((void *)buf, 0, tot_mem);
180
181         glGenTextures(1, &gc->textures[gc->cur_tex]);
182         glBindTexture(GL_TEXTURE_2D, gc->textures[gc->cur_tex]);
183         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
184         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
185         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
186         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
187
188         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buf);
189         free((void *)buf);
190 }
191
192 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
193 {
194         GlyphBLF *p;
195         unsigned int key;
196
197         key= blf_hash(c);
198         p= gc->bucket[key].first;
199         while (p) {
200                 if (p->c == c)
201                         return(p);
202                 p= p->next;
203         }
204         return(NULL);
205 }
206
207 GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
208 {
209         FT_GlyphSlot slot;
210         GlyphBLF *g;
211         FT_Error err;
212         FT_Bitmap bitmap, tempbitmap;
213         int sharp = (U.text_render & USER_TEXT_DISABLE_AA);
214         FT_BBox bbox;
215         unsigned int key;
216
217         g= blf_glyph_search(font->glyph_cache, c);
218         if (g)
219                 return(g);
220
221         if (sharp)
222                 err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
223         else
224                 err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP); /* Sure about NO_* flags? */
225         if (err)
226                 return(NULL);
227
228         /* get the glyph. */
229         slot= font->face->glyph;
230
231         if (sharp) {
232                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
233
234                 /* Convert result from 1 bit per pixel to 8 bit per pixel */
235                 /* Accum errors for later, fine if not interested beyond "ok vs any error" */
236                 FT_Bitmap_New(&tempbitmap);
237                 err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1); /* Does Blender use Pitch 1 always? It works so far */
238                 err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap);
239                 err += FT_Bitmap_Done(font->ft_lib, &tempbitmap);
240         } else {
241                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
242         }
243
244         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
245                 return(NULL);
246
247         g= (GlyphBLF *)MEM_mallocN(sizeof(GlyphBLF), "blf_glyph_add");
248         g->next= NULL;
249         g->prev= NULL;
250         g->c= c;
251         g->idx= (FT_UInt)index;
252         g->tex= 0;
253         g->build_tex= 0;
254         g->bitmap= NULL;
255         g->xoff= -1;
256         g->yoff= -1;
257         g->uv[0][0]= 0.0f;
258         g->uv[0][1]= 0.0f;
259         g->uv[1][0]= 0.0f;
260         g->uv[1][1]= 0.0f;
261         bitmap= slot->bitmap;
262         g->width= bitmap.width;
263         g->height= bitmap.rows;
264
265         if (g->width && g->height) {
266                 if (sharp) {
267                         /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */
268                         int i;
269                         for (i=0; i < (g->width * g->height); i++) {
270                                 bitmap.buffer[i] = 255 * bitmap.buffer[i];
271                         }
272                 }
273
274                 g->bitmap= (unsigned char *)MEM_mallocN(g->width * g->height, "glyph bitmap");
275                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, g->width * g->height);
276         }
277
278         g->advance= ((float)slot->advance.x) / 64.0f;
279         g->pos_x= slot->bitmap_left;
280         g->pos_y= slot->bitmap_top;
281         g->pitch= slot->bitmap.pitch;
282
283         FT_Outline_Get_CBox(&(slot->outline), &bbox);
284         g->box.xmin= ((float)bbox.xMin) / 64.0f;
285         g->box.xmax= ((float)bbox.xMax) / 64.0f;
286         g->box.ymin= ((float)bbox.yMin) / 64.0f;
287         g->box.ymax= ((float)bbox.yMax) / 64.0f;
288
289         key= blf_hash(g->c);
290         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
291         return(g);
292 }
293
294 void blf_glyph_free(GlyphBLF *g)
295 {
296         /* don't need free the texture, the GlyphCache already
297          * have a list of all the texture and free it.
298          */
299         if (g->bitmap)
300                 MEM_freeN(g->bitmap);
301         MEM_freeN(g);
302 }
303
304 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
305 {
306         
307         glBegin(GL_QUADS);
308         glTexCoord2f(uv[0][0], uv[0][1]);
309         glVertex2f(dx, y1);
310         
311         glTexCoord2f(uv[0][0], uv[1][1]);
312         glVertex2f(dx, y2);
313         
314         glTexCoord2f(uv[1][0], uv[1][1]);
315         glVertex2f(dx1, y2);
316         
317         glTexCoord2f(uv[1][0], uv[0][1]);
318         glVertex2f(dx1, y1);
319         glEnd();
320         
321 }
322
323 static void blf_texture5_draw(float uv[2][2], float x1, float y1, float x2, float y2)
324 {
325         float soft[25]= {
326                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f, 
327                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
328                 2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f, 
329                 1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f, 
330                 1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
331         
332         float color[4], *fp= soft;
333         int dx, dy;
334         
335         glGetFloatv(GL_CURRENT_COLOR, color);
336         
337         for(dx=-2; dx<3; dx++) {
338                 for(dy=-2; dy<3; dy++, fp++) {
339                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
340                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
341                 }
342         }
343         
344         glColor4fv(color);
345 }
346
347 static void blf_texture3_draw(float uv[2][2], float x1, float y1, float x2, float y2)
348 {
349         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};
350         float color[4], *fp= soft;
351         int dx, dy;
352         
353         glGetFloatv(GL_CURRENT_COLOR, color);
354         
355         for(dx=-1; dx<2; dx++) {
356                 for(dy=-1; dy<2; dy++, fp++) {
357                         glColor4f(color[0], color[1], color[2], fp[0]*color[3]);
358                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
359                 }
360         }
361         
362         glColor4fv(color);
363 }
364
365 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
366 {
367         GlyphCacheBLF *gc;
368         GLint cur_tex;
369         float dx, dx1;
370         float y1, y2;
371         float xo, yo;
372         float color[4];
373
374         if ((!g->width) || (!g->height))
375                 return(1);
376
377         if (g->build_tex == 0) {
378                 gc= font->glyph_cache;
379
380                 if (font->max_tex_size == -1)
381                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->max_tex_size);
382
383                 if (gc->cur_tex == -1) {
384                         blf_glyph_cache_texture(font, gc);
385                         gc->x_offs= gc->pad;
386                         gc->y_offs= gc->pad;
387                 }
388
389                 if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
390                         gc->x_offs= gc->pad;
391                         gc->y_offs += gc->max_glyph_height;
392
393                         if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
394                                 gc->y_offs= gc->pad;
395                                 blf_glyph_cache_texture(font, gc);
396                         }
397                 }
398
399                 g->tex= gc->textures[gc->cur_tex];
400                 g->xoff= gc->x_offs;
401                 g->yoff= gc->y_offs;
402
403                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
404                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
405                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
406                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
407
408                 glBindTexture(GL_TEXTURE_2D, g->tex);
409                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap);
410                 glPopClientAttrib();
411
412                 g->uv[0][0]= ((float)g->xoff) / ((float)gc->p2_width);
413                 g->uv[0][1]= ((float)g->yoff) / ((float)gc->p2_height);
414                 g->uv[1][0]= ((float)(g->xoff + g->width)) / ((float)gc->p2_width);
415                 g->uv[1][1]= ((float)(g->yoff + g->height)) / ((float)gc->p2_height);
416
417                 /* update the x offset for the next glyph. */
418                 gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
419
420                 gc->rem_glyphs--;
421                 g->build_tex= 1;
422         }
423
424         xo= 0.0f;
425         yo= 0.0f;
426
427         if (font->flags & BLF_SHADOW) {
428                 xo= x;
429                 yo= y;
430                 x += font->shadow_x;
431                 y += font->shadow_y;
432         }
433
434         dx= floor(x + g->pos_x);
435         dx1= dx + g->width;
436         y1= y + g->pos_y;
437         y2= y + g->pos_y - g->height;
438
439         if (font->flags & BLF_CLIPPING) {
440                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
441                         return(0);
442                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
443                         return(0);
444                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
445                         return(0);
446                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
447                         return(0);
448         }
449
450         glGetIntegerv(GL_TEXTURE_2D_BINDING_EXT, &cur_tex);
451         if (cur_tex != g->tex)
452                 glBindTexture(GL_TEXTURE_2D, g->tex);
453
454         if (font->flags & BLF_SHADOW) {
455                 glGetFloatv(GL_CURRENT_COLOR, color);
456                 glColor4fv(font->shadow_col);
457
458                 if (font->shadow == 3)
459                         blf_texture3_draw(g->uv, dx, y1, dx1, y2);
460                 else if (font->shadow == 5)
461                         blf_texture5_draw(g->uv, dx, y1, dx1, y2);
462                 else
463                         blf_texture_draw(g->uv, dx, y1, dx1, y2);
464
465                 glColor4fv(color);
466                 x= xo;
467                 y= yo;
468
469                 dx= floor(x + g->pos_x);
470                 dx1= dx + g->width;
471                 y1= y + g->pos_y;
472                 y2= y + g->pos_y - g->height;
473         }
474
475         if (font->blur==3)
476                 blf_texture3_draw(g->uv, dx, y1, dx1, y2);
477         else if (font->blur==5)
478                 blf_texture5_draw(g->uv, dx, y1, dx1, y2);
479         else
480                 blf_texture_draw(g->uv, dx, y1, dx1, y2);
481
482         return(1);
483 }