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