55
56
#include "blf_internal_types.h"
56
57
#include "blf_internal.h"
60
# pragma GCC diagnostic error "-Wsign-conversion"
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)
167
171
gc->textures = (GLuint *)realloc((void *)gc->textures, sizeof(GLuint) * gc->ntex);
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;
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));
177
181
if (gc->p2_height > font->max_tex_size)
178
182
gc->p2_height = font->max_tex_size;
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__);
183
187
glGenTextures(1, &gc->textures[gc->cur_tex]);
184
188
glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state = gc->textures[gc->cur_tex]));
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);
234
/* search again after locking */
235
g = blf_glyph_search(font->glyph_cache, c);
237
BLI_spin_unlock(font->ft_lib_mutex);
224
241
if (font->flags & BLF_HINTING)
225
242
flags &= ~FT_LOAD_NO_HINTING;
228
245
err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO);
230
247
err = FT_Load_Glyph(font->face, (FT_UInt)index, flags);
250
BLI_spin_unlock(font->ft_lib_mutex);
234
254
/* get the glyph. */
235
255
slot = font->face->glyph;
248
268
err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
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);
254
276
g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add");
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));
276
298
g->advance = ((float)slot->advance.x) / 64.0f;
368
393
glColor4fv(color);
371
int blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
396
static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y)
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;
404
void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y)
377
408
if ((!g->width) || (!g->height))
380
411
if (g->build_tex == 0) {
381
412
GlyphCacheBLF *gc = font->glyph_cache;
437
468
g->build_tex = 1;
443
if (font->flags & BLF_SHADOW) {
450
dx = floor(x + g->pos_x);
453
y2 = y + g->pos_y - g->height;
471
blf_glyph_calc_rect(&rect, g, x, y);
455
473
if (font->flags & BLF_CLIPPING) {
456
if (!BLI_rctf_isect_pt(&font->clip_rec, dx + font->pos[0], y1 + font->pos[1]))
458
if (!BLI_rctf_isect_pt(&font->clip_rec, dx + font->pos[0], y2 + font->pos[1]))
460
if (!BLI_rctf_isect_pt(&font->clip_rec, dx1 + font->pos[0], y2 + font->pos[1]))
462
if (!BLI_rctf_isect_pt(&font->clip_rec, dx1 + font->pos[0], y1 + font->pos[1]))
474
/* intentionally check clipping without shadow offset */
475
rctf rect_test = rect;
476
BLI_rctf_translate(&rect_test, font->pos[0], font->pos[1]);
478
if (!BLI_rctf_inside_rctf(&font->clip_rec, &rect_test)) {
466
483
if (font->tex_bind_state != g->tex) {
470
487
if (font->flags & BLF_SHADOW) {
489
blf_glyph_calc_rect(&rect_ofs, g, x + font->shadow_x, y + font->shadow_y);
472
491
switch (font->shadow) {
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);
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);
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);
485
504
glColor4fv(font->orig_col);
490
dx = floor(x + g->pos_x);
493
y2 = y + g->pos_y - g->height;
496
507
switch (font->blur) {
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);
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);
504
blf_texture_draw(g->uv, dx, y1, dx1, y2);
515
blf_texture_draw(g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax);