~ubuntu-branches/ubuntu/trusty/blender/trusty-proposed

« back to all changes in this revision

Viewing changes to source/blender/blenfont/intern/blf_glyph.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2013-08-14 10:43:49 UTC
  • mfrom: (14.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20130814104349-t1d5mtwkphp12dyj
Tags: 2.68a-3
* Upload to unstable
* debian/: python3.3 Depends simplified
  - debian/control: python3.3 Depends dropped
    for blender-data package
  - 0001-blender_thumbnailer.patch refreshed
* debian/control: libavcodec b-dep versioning dropped

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
 
49
49
#include "BLI_listbase.h"
50
50
#include "BLI_rect.h"
 
51
#include "BLI_threads.h"
51
52
 
52
53
#include "BIF_gl.h"
53
54
#include "BLF_api.h"
55
56
#include "blf_internal_types.h"
56
57
#include "blf_internal.h"
57
58
 
 
59
#ifdef __GNUC__
 
60
#  pragma GCC diagnostic error "-Wsign-conversion"
 
61
#endif
58
62
 
59
 
GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, int size, int dpi)
 
63
GlyphCacheBLF *blf_glyph_cache_find(FontBLF *font, unsigned int size, unsigned int dpi)
60
64
{
61
65
        GlyphCacheBLF *p;
62
66
 
167
171
                gc->textures = (GLuint *)realloc((void *)gc->textures, sizeof(GLuint) * gc->ntex);
168
172
        }
169
173
 
170
 
        gc->p2_width = blf_next_p2((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2));
 
174
        gc->p2_width = (int)blf_next_p2((unsigned int)((gc->rem_glyphs * gc->max_glyph_width) + (gc->pad * 2)));
171
175
        if (gc->p2_width > font->max_tex_size)
172
176
                gc->p2_width = font->max_tex_size;
173
177
 
174
178
        i = (int)((gc->p2_width - (gc->pad * 2)) / gc->max_glyph_width);
175
 
        gc->p2_height = blf_next_p2(((gc->num_glyphs / i) + 1) * gc->max_glyph_height);
 
179
        gc->p2_height = (int)blf_next_p2((unsigned int)(((gc->num_glyphs / i) + 1) * gc->max_glyph_height));
176
180
 
177
181
        if (gc->p2_height > font->max_tex_size)
178
182
                gc->p2_height = font->max_tex_size;
179
183
 
180
184
        tot_mem = gc->p2_width * gc->p2_height;
181
 
        buf = (unsigned char *)MEM_callocN(tot_mem, __func__);
 
185
        buf = (unsigned char *)MEM_callocN((size_t)tot_mem, __func__);
182
186
 
183
187
        glGenTextures(1, &gc->textures[gc->cur_tex]);
184
188
        glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state = gc->textures[gc->cur_tex]));
221
225
        if (g)
222
226
                return g;
223
227
 
 
228
        /* glyphs are dynamically created as needed by font rendering. this means that
 
229
         * to make font rendering thread safe we have to do locking here. note that this
 
230
         * must be a lock for the whole library and not just per font, because the font
 
231
         * renderer uses a shared buffer internally */
 
232
        BLI_spin_lock(font->ft_lib_mutex);
 
233
 
 
234
        /* search again after locking */
 
235
        g = blf_glyph_search(font->glyph_cache, c);
 
236
        if (g) {
 
237
                BLI_spin_unlock(font->ft_lib_mutex);
 
238
                return g;
 
239
        }
 
240
 
224
241
        if (font->flags & BLF_HINTING)
225
242
                flags &= ~FT_LOAD_NO_HINTING;
226
243
        
228
245
                err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
229
246
        else
230
247
                err = FT_Load_Glyph(font->face, (FT_UInt)index, flags);  
231
 
        if (err)
 
248
 
 
249
        if (err) {
 
250
                BLI_spin_unlock(font->ft_lib_mutex);
232
251
                return NULL;
 
252
        }
233
253
 
234
254
        /* get the glyph. */
235
255
        slot = font->face->glyph;
248
268
                err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
249
269
        }
250
270
 
251
 
        if (err || slot->format != FT_GLYPH_FORMAT_BITMAP)
 
271
        if (err || slot->format != FT_GLYPH_FORMAT_BITMAP) {
 
272
                BLI_spin_unlock(font->ft_lib_mutex);
252
273
                return NULL;
 
274
        }
253
275
 
254
276
        g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add");
255
277
        g->c = c;
269
291
                        }
270
292
                }
271
293
 
272
 
                g->bitmap = (unsigned char *)MEM_mallocN(g->width * g->height, "glyph bitmap");
273
 
                memcpy((void *)g->bitmap, (void *)bitmap.buffer, g->width * g->height);
 
294
                g->bitmap = (unsigned char *)MEM_mallocN((size_t)(g->width * g->height), "glyph bitmap");
 
295
                memcpy((void *)g->bitmap, (void *)bitmap.buffer, (size_t)(g->width * g->height));
274
296
        }
275
297
 
276
298
        g->advance = ((float)slot->advance.x) / 64.0f;
286
308
 
287
309
        key = blf_hash(g->c);
288
310
        BLI_addhead(&(font->glyph_cache->bucket[key]), g);
 
311
 
 
312
        BLI_spin_unlock(font->ft_lib_mutex);
 
313
 
289
314
        return g;
290
315
}
291
316
 
368
393
        glColor4fv(color);
369
394
}
370
395
 
371
 
int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
372
 
{
373
 
        float dx, dx1;
374
 
        float y1, y2;
375
 
        float xo, yo;
 
396
static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y)
 
397
{
 
398
        rect->xmin = floor(x + g->pos_x);
 
399
        rect->xmax = rect->xmin + g->width;
 
400
        rect->ymin = y + g->pos_y;
 
401
        rect->ymax = y + g->pos_y - g->height;
 
402
}
 
403
 
 
404
void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
 
405
{
 
406
        rctf rect;
376
407
 
377
408
        if ((!g->width) || (!g->height))
378
 
                return 1;
 
409
                return;
379
410
 
380
411
        if (g->build_tex == 0) {
381
412
                GlyphCacheBLF *gc = font->glyph_cache;
437
468
                g->build_tex = 1;
438
469
        }
439
470
 
440
 
        xo = 0.0f;
441
 
        yo = 0.0f;
442
 
 
443
 
        if (font->flags & BLF_SHADOW) {
444
 
                xo = x;
445
 
                yo = y;
446
 
                x += font->shadow_x;
447
 
                y += font->shadow_y;
448
 
        }
449
 
 
450
 
        dx = floor(x + g->pos_x);
451
 
        dx1 = dx + g->width;
452
 
        y1 = y + g->pos_y;
453
 
        y2 = y + g->pos_y - g->height;
 
471
        blf_glyph_calc_rect(&rect, g, x, y);
454
472
 
455
473
        if (font->flags & BLF_CLIPPING) {
456
 
                if (!BLI_rctf_isect_pt(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
457
 
                        return 0;
458
 
                if (!BLI_rctf_isect_pt(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
459
 
                        return 0;
460
 
                if (!BLI_rctf_isect_pt(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
461
 
                        return 0;
462
 
                if (!BLI_rctf_isect_pt(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
463
 
                        return 0;
 
474
                /* intentionally check clipping without shadow offset */
 
475
                rctf rect_test = rect;
 
476
                BLI_rctf_translate(&rect_test, font->pos[0], font->pos[1]);
 
477
 
 
478
                if (!BLI_rctf_inside_rctf(&font->clip_rec, &rect_test)) {
 
479
                        return;
 
480
                }
464
481
        }
465
482
 
466
483
        if (font->tex_bind_state != g->tex) {
468
485
        }
469
486
 
470
487
        if (font->flags & BLF_SHADOW) {
 
488
                rctf rect_ofs;
 
489
                blf_glyph_calc_rect(&rect_ofs, g, x + font->shadow_x, y + font->shadow_y);
471
490
 
472
491
                switch (font->shadow) {
473
492
                        case 3:
474
 
                                blf_texture3_draw(font->shadow_col, g->uv, dx, y1, dx1, y2);
 
493
                                blf_texture3_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
475
494
                                break;
476
495
                        case 5:
477
 
                                blf_texture5_draw(font->shadow_col, g->uv, dx, y1, dx1, y2);
 
496
                                blf_texture5_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
478
497
                                break;
479
498
                        default:
480
499
                                glColor4fv(font->shadow_col);
481
 
                                blf_texture_draw(g->uv, dx, y1, dx1, y2);
 
500
                                blf_texture_draw(g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax);
482
501
                                break;
483
502
                }
484
503
 
485
504
                glColor4fv(font->orig_col);
486
 
 
487
 
                x = xo;
488
 
                y = yo;
489
 
 
490
 
                dx = floor(x + g->pos_x);
491
 
                dx1 = dx + g->width;
492
 
                y1 = y + g->pos_y;
493
 
                y2 = y + g->pos_y - g->height;
494
505
        }
495
506
 
496
507
        switch (font->blur) {
497
508
                case 3:
498
 
                        blf_texture3_draw(font->orig_col, g->uv, dx, y1, dx1, y2);
 
509
                        blf_texture3_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
499
510
                        break;
500
511
                case 5:
501
 
                        blf_texture5_draw(font->orig_col, g->uv, dx, y1, dx1, y2);
 
512
                        blf_texture5_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
502
513
                        break;
503
514
                default:
504
 
                        blf_texture_draw(g->uv, dx, y1, dx1, y2);
 
515
                        blf_texture_draw(g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
505
516
                        break;
506
517
        }
507
518
 
508
 
        return 1;
 
519
        return;
509
520
}