Improvements to bmesh edge rotate
[blender-staging.git] / source / blender / blenfont / intern / blf_glyph.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenfont/intern/blf_glyph.c
28  *  \ingroup blf
29  */
30
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include <ft2build.h>
38
39 #include FT_FREETYPE_H
40 #include FT_GLYPH_H
41 #include FT_OUTLINE_H
42 #include FT_BITMAP_H
43
44 #include "MEM_guardedalloc.h"
45
46 #include "DNA_vec_types.h"
47 #include "DNA_userdef_types.h"
48
49 #include "BLI_blenlib.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
76         gc= (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
77         gc->next= NULL;
78         gc->prev= NULL;
79         gc->size= font->size;
80         gc->dpi= font->dpi;
81
82         memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table));
83         memset(gc->bucket, 0, sizeof(gc->bucket));
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                 memset(gc->glyph_ascii_table, 0, sizeof(gc->glyph_ascii_table));
134         }
135 }
136
137 void blf_glyph_cache_free(GlyphCacheBLF *gc)
138 {
139         GlyphBLF *g;
140         int i;
141
142         for (i= 0; i < 257; i++) {
143                 while (gc->bucket[i].first) {
144                         g= gc->bucket[i].first;
145                         BLI_remlink(&(gc->bucket[i]), g);
146                         blf_glyph_free(g);
147                 }
148         }
149
150         if (gc->cur_tex+1 > 0)
151                 glDeleteTextures(gc->cur_tex+1, gc->textures);
152         free((void *)gc->textures);
153         MEM_freeN(gc);
154 }
155
156 static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc)
157 {
158         int tot_mem, i;
159         unsigned char *buf;
160
161         /* move the index. */
162         gc->cur_tex++;
163
164         if (gc->cur_tex >= gc->ntex) {
165                 gc->ntex *= 2;
166                 gc->textures= (GLuint *)realloc((void *)gc->textures, sizeof(GLuint)*gc->ntex);
167         }
168
169         gc->p2_width= blf_next_p2((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2));
170         if (gc->p2_width > font->max_tex_size)
171                 gc->p2_width= font->max_tex_size;
172
173         i= (int)((gc->p2_width - (gc->pad * 2)) / gc->max_glyph_width);
174         gc->p2_height= blf_next_p2(((gc->num_glyphs / i) + 1) * gc->max_glyph_height);
175
176         if (gc->p2_height > font->max_tex_size)
177                 gc->p2_height= font->max_tex_size;
178
179         tot_mem= gc->p2_width * gc->p2_height;
180         buf= (unsigned char *)malloc(tot_mem);
181         memset((void *)buf, 0, tot_mem);
182
183         glGenTextures(1, &gc->textures[gc->cur_tex]);
184         glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state= gc->textures[gc->cur_tex]));
185         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
186         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
187         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
188         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
189
190         glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buf);
191         free((void *)buf);
192 }
193
194 GlyphBLF *blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
195 {
196         GlyphBLF *p;
197         unsigned int key;
198
199         key= blf_hash(c);
200         p= gc->bucket[key].first;
201         while (p) {
202                 if (p->c == c)
203                         return p;
204                 p= p->next;
205         }
206         return NULL;
207 }
208
209 GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c)
210 {
211         FT_GlyphSlot slot;
212         GlyphBLF *g;
213         FT_Error err;
214         FT_Bitmap bitmap, tempbitmap;
215         int sharp = (U.text_render & USER_TEXT_DISABLE_AA);
216         FT_BBox bbox;
217         unsigned int key;
218
219         g= blf_glyph_search(font->glyph_cache, c);
220         if (g)
221                 return g;
222
223         if (sharp)
224                 err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
225         else
226                 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? */
227         if (err)
228                 return NULL;
229
230         /* get the glyph. */
231         slot= font->face->glyph;
232
233         if (sharp) {
234                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
235
236                 /* Convert result from 1 bit per pixel to 8 bit per pixel */
237                 /* Accum errors for later, fine if not interested beyond "ok vs any error" */
238                 FT_Bitmap_New(&tempbitmap);
239                 err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1); /* Does Blender use Pitch 1 always? It works so far */
240                 err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap);
241                 err += FT_Bitmap_Done(font->ft_lib, &tempbitmap);
242         } else {
243                 err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
244         }
245
246         if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
247                 return NULL;
248
249         g= (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add");
250         g->c= c;
251         g->idx= (FT_UInt)index;
252         g->xoff= -1;
253         g->yoff= -1;
254         bitmap= slot->bitmap;
255         g->width= bitmap.width;
256         g->height= bitmap.rows;
257
258         if (g->width && g->height) {
259                 if (sharp) {
260                         /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */
261                         int i;
262                         for (i=0; i < (g->width * g->height); i++) {
263                                 bitmap.buffer[i] = 255 * bitmap.buffer[i];
264                         }
265                 }
266
267                 g->bitmap= (unsigned char *)MEM_mallocN(g->width * g->height, "glyph bitmap");
268                 memcpy((void *)g->bitmap, (void *)bitmap.buffer, g->width * g->height);
269         }
270
271         g->advance= ((float)slot->advance.x) / 64.0f;
272         g->pos_x= slot->bitmap_left;
273         g->pos_y= slot->bitmap_top;
274         g->pitch= slot->bitmap.pitch;
275
276         FT_Outline_Get_CBox(&(slot->outline), &bbox);
277         g->box.xmin= ((float)bbox.xMin) / 64.0f;
278         g->box.xmax= ((float)bbox.xMax) / 64.0f;
279         g->box.ymin= ((float)bbox.yMin) / 64.0f;
280         g->box.ymax= ((float)bbox.yMax) / 64.0f;
281
282         key= blf_hash(g->c);
283         BLI_addhead(&(font->glyph_cache->bucket[key]), g);
284         return g;
285 }
286
287 void blf_glyph_free(GlyphBLF *g)
288 {
289         /* don't need free the texture, the GlyphCache already
290          * have a list of all the texture and free it.
291          */
292         if (g->bitmap)
293                 MEM_freeN(g->bitmap);
294         MEM_freeN(g);
295 }
296
297 static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2)
298 {
299         
300         glBegin(GL_QUADS);
301         glTexCoord2f(uv[0][0], uv[0][1]);
302         glVertex2f(dx, y1);
303         
304         glTexCoord2f(uv[0][0], uv[1][1]);
305         glVertex2f(dx, y2);
306         
307         glTexCoord2f(uv[1][0], uv[1][1]);
308         glVertex2f(dx1, y2);
309         
310         glTexCoord2f(uv[1][0], uv[0][1]);
311         glVertex2f(dx1, y1);
312         glEnd();
313         
314 }
315
316 static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
317 {
318         float soft[25]= {1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f,
319                          1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f,
320                          2/60.0f, 5/60.0f, 8/60.0f, 5/60.0f, 2/60.0f,
321                          1/60.0f, 3/60.0f, 5/60.0f, 3/60.0f, 1/60.0f,
322                          1/60.0f, 1/60.0f, 2/60.0f, 1/60.0f, 1/60.0f};
323         
324         float color[4], *fp= soft;
325         int dx, dy;
326
327         color[0]= shadow_col[0];
328         color[1]= shadow_col[1];
329         color[2]= shadow_col[2];
330         
331         for(dx=-2; dx<3; dx++) {
332                 for(dy=-2; dy<3; dy++, fp++) {
333                         color[3]= *(fp) * shadow_col[3];
334                         glColor4fv(color);
335                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
336                 }
337         }
338         
339         glColor4fv(color);
340 }
341
342 static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2)
343 {
344         float soft[9]= {1/16.0f, 2/16.0f, 1/16.0f,
345                         2/16.0f,4/16.0f, 2/16.0f,
346                         1/16.0f, 2/16.0f, 1/16.0f};
347
348         float color[4], *fp= soft;
349         int dx, dy;
350
351         color[0]= shadow_col[0];
352         color[1]= shadow_col[1];
353         color[2]= shadow_col[2];
354
355         for(dx=-1; dx<2; dx++) {
356                 for(dy=-1; dy<2; dy++, fp++) {
357                         color[3]= *(fp) * shadow_col[3];
358                         glColor4fv(color);
359                         blf_texture_draw(uv, x1+dx, y1+dy, x2+dx, y2+dy);
360                 }
361         }
362         
363         glColor4fv(color);
364 }
365
366 int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
367 {
368         float dx, dx1;
369         float y1, y2;
370         float xo, yo;
371
372         if ((!g->width) || (!g->height))
373                 return 1;
374
375         if (g->build_tex == 0) {
376                 GlyphCacheBLF *gc= font->glyph_cache;
377
378                 if (font->max_tex_size == -1)
379                         glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint *)&font->max_tex_size);
380
381                 if (gc->cur_tex == -1) {
382                         blf_glyph_cache_texture(font, gc);
383                         gc->x_offs= gc->pad;
384                         gc->y_offs= gc->pad;
385                 }
386
387                 if (gc->x_offs > (gc->p2_width - gc->max_glyph_width)) {
388                         gc->x_offs= gc->pad;
389                         gc->y_offs += gc->max_glyph_height;
390
391                         if (gc->y_offs > (gc->p2_height - gc->max_glyph_height)) {
392                                 gc->y_offs= gc->pad;
393                                 blf_glyph_cache_texture(font, gc);
394                         }
395                 }
396
397                 g->tex= gc->textures[gc->cur_tex];
398                 g->xoff= gc->x_offs;
399                 g->yoff= gc->y_offs;
400
401                 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
402                 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
403                 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
404                 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
405
406                 glBindTexture(GL_TEXTURE_2D, g->tex);
407                 glTexSubImage2D(GL_TEXTURE_2D, 0, g->xoff, g->yoff, g->width, g->height, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap);
408                 glPopClientAttrib();
409
410                 g->uv[0][0]= ((float)g->xoff) / ((float)gc->p2_width);
411                 g->uv[0][1]= ((float)g->yoff) / ((float)gc->p2_height);
412                 g->uv[1][0]= ((float)(g->xoff + g->width)) / ((float)gc->p2_width);
413                 g->uv[1][1]= ((float)(g->yoff + g->height)) / ((float)gc->p2_height);
414
415                 /* update the x offset for the next glyph. */
416                 gc->x_offs += (int)(g->box.xmax - g->box.xmin + gc->pad);
417
418                 gc->rem_glyphs--;
419                 g->build_tex= 1;
420         }
421
422         xo= 0.0f;
423         yo= 0.0f;
424
425         if (font->flags & BLF_SHADOW) {
426                 xo= x;
427                 yo= y;
428                 x += font->shadow_x;
429                 y += font->shadow_y;
430         }
431
432         dx= floor(x + g->pos_x);
433         dx1= dx + g->width;
434         y1= y + g->pos_y;
435         y2= y + g->pos_y - g->height;
436
437         if (font->flags & BLF_CLIPPING) {
438                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
439                         return 0;
440                 if (!BLI_in_rctf(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
441                         return 0;
442                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
443                         return 0;
444                 if (!BLI_in_rctf(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
445                         return 0;
446         }
447
448         if (font->tex_bind_state != g->tex) {
449                 glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state= g->tex));
450         }
451
452         if (font->flags & BLF_SHADOW) {
453
454                 switch(font->shadow) {
455                         case 3:
456                                 blf_texture3_draw(font->shadow_col, g->uv, dx, y1, dx1, y2);
457                                 break;
458                         case 5:
459                                 blf_texture5_draw(font->shadow_col, g->uv, dx, y1, dx1, y2);
460                                 break;
461                         default:
462                                 glColor4fv(font->shadow_col);
463                                 blf_texture_draw(g->uv, dx, y1, dx1, y2);
464                                 break;
465                 }
466
467                 glColor4fv(font->orig_col);
468
469                 x= xo;
470                 y= yo;
471
472                 dx= floor(x + g->pos_x);
473                 dx1= dx + g->width;
474                 y1= y + g->pos_y;
475                 y2= y + g->pos_y - g->height;
476         }
477
478         switch(font->blur) {
479                 case 3:
480                         blf_texture3_draw(font->orig_col, g->uv, dx, y1, dx1, y2);
481                         break;
482                 case 5:
483                         blf_texture5_draw(font->orig_col, g->uv, dx, y1, dx1, y2);
484                         break;
485                 default:
486                         blf_texture_draw(g->uv, dx, y1, dx1, y2);
487                         break;
488         }
489
490         return 1;
491 }