Blender  V2.93
Macros | Functions | Variables
blf_font.c File Reference
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ft2build.h>
#include "MEM_guardedalloc.h"
#include "DNA_vec_types.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_math_color_blend.h"
#include "BLI_rect.h"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_threads.h"
#include "BLF_api.h"
#include "UI_interface.h"
#include "GPU_batch.h"
#include "GPU_matrix.h"
#include "blf_internal.h"
#include "blf_internal_types.h"
#include "BLI_strict_flags.h"

Go to the source code of this file.

Macros

#define BLF_UTF8_NEXT_FAST(_font, _gc, _g, _str, _i, _c, _glyph_ascii_table)
 
#define BLF_KERNING_VARS(_font, _has_kerning, _kern_mode)
 
#define BLF_KERNING_STEP_FAST(_font, _kern_mode, _g_prev, _g, _c_prev, _c, _pen_x)
 
#define BLF_KERNING_STEP(_font, _kern_mode, _g_prev, _g, _delta, _pen_x)
 

Functions

int blf_font_init (void)
 
void blf_font_exit (void)
 
void blf_font_size (FontBLF *font, unsigned int size, unsigned int dpi)
 
static GlyphBLF ** blf_font_ensure_ascii_table (FontBLF *font, GlyphCacheBLF *gc)
 
static void blf_font_ensure_ascii_kerning (FontBLF *font, GlyphCacheBLF *gc, const FT_UInt kern_mode)
 
static void blf_font_draw_ex (FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, struct ResultBLF *r_info, int pen_y)
 
void blf_font_draw (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
 
static void blf_font_draw_ascii_ex (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, int pen_y)
 
void blf_font_draw_ascii (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
 
int blf_font_draw_mono (FontBLF *font, const char *str, size_t len, int cwidth)
 
static void blf_font_draw_buffer_ex (FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, struct ResultBLF *r_info, int pen_y)
 
void blf_font_draw_buffer (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
 
static bool blf_font_width_to_strlen_glyph_process (FontBLF *font, const bool has_kerning, const FT_UInt kern_mode, const uint c_prev, const uint c, GlyphBLF *g_prev, GlyphBLF *g, int *pen_x, const int width_i)
 
size_t blf_font_width_to_strlen (FontBLF *font, const char *str, size_t len, float width, float *r_width)
 
size_t blf_font_width_to_rstrlen (FontBLF *font, const char *str, size_t len, float width, float *r_width)
 
static void blf_font_boundbox_ex (FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, rctf *box, struct ResultBLF *r_info, int pen_y)
 
void blf_font_boundbox (FontBLF *font, const char *str, size_t len, rctf *r_box, struct ResultBLF *r_info)
 
void blf_font_width_and_height (FontBLF *font, const char *str, size_t len, float *r_width, float *r_height, struct ResultBLF *r_info)
 
float blf_font_width (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
 
float blf_font_height (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
 
float blf_font_fixed_width (FontBLF *font)
 
int blf_font_count_missing_chars (FontBLF *font, const char *str, const size_t len, int *r_tot_chars)
 
void blf_font_free (FontBLF *font)
 
static void blf_font_fill (FontBLF *font)
 
FontBLFblf_font_new (const char *name, const char *filename)
 
void blf_font_attach_from_mem (FontBLF *font, const unsigned char *mem, int mem_size)
 
FontBLFblf_font_new_from_mem (const char *name, const unsigned char *mem, int mem_size)
 
int blf_font_height_max (FontBLF *font)
 
int blf_font_width_max (FontBLF *font)
 
float blf_font_descender (FontBLF *font)
 
float blf_font_ascender (FontBLF *font)
 
Glyph Batching
static void blf_batch_draw_init (void)
 
static void blf_batch_draw_exit (void)
 
void blf_batch_draw_begin (FontBLF *font)
 
static GPUTextureblf_batch_cache_texture_load (void)
 
void blf_batch_draw (void)
 
static void blf_batch_draw_end (void)
 
Word-Wrap Support
static void blf_font_wrap_apply (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, void(*callback)(FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, int pen_y, void *userdata), void *userdata)
 
static void blf_font_draw__wrap_cb (FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, int pen_y, void *UNUSED(userdata))
 
void blf_font_draw__wrap (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
 
static void blf_font_boundbox_wrap_cb (FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, int pen_y, void *userdata)
 
void blf_font_boundbox__wrap (FontBLF *font, const char *str, size_t len, rctf *box, struct ResultBLF *r_info)
 
static void blf_font_draw_buffer__wrap_cb (FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, int pen_y, void *UNUSED(userdata))
 
void blf_font_draw_buffer__wrap (FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info)
 
Glyph Bound Box with Callback
static void blf_font_boundbox_foreach_glyph_ex (FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, BLF_GlyphBoundsFn user_fn, void *user_data, struct ResultBLF *r_info, int pen_y)
 
void blf_font_boundbox_foreach_glyph (FontBLF *font, const char *str, size_t len, BLF_GlyphBoundsFn user_fn, void *user_data, struct ResultBLF *r_info)
 

Variables

BatchBLF g_batch
 
static FT_Library ft_lib
 
static SpinLock ft_lib_mutex
 
static SpinLock blf_glyph_cache_mutex
 

Detailed Description

Deals with drawing text to OpenGL or bitmap buffers.

Also low level functions for managing FontBLF.

Definition in file blf_font.c.

Macro Definition Documentation

◆ BLF_KERNING_STEP

#define BLF_KERNING_STEP (   _font,
  _kern_mode,
  _g_prev,
  _g,
  _delta,
  _pen_x 
)
Value:
{ \
if (_g_prev) { \
_delta.x = _delta.y = 0; \
if (FT_Get_Kerning((_font)->face, (_g_prev)->idx, (_g)->idx, _kern_mode, &(_delta)) == 0) { \
_pen_x += (int)_delta.x >> 6; \
} \
} \
} \
(void)0

Definition at line 397 of file blf_font.c.

◆ BLF_KERNING_STEP_FAST

#define BLF_KERNING_STEP_FAST (   _font,
  _kern_mode,
  _g_prev,
  _g,
  _c_prev,
  _c,
  _pen_x 
)
Value:
{ \
if (_g_prev) { \
FT_Vector _delta; \
if (_c_prev < 0x80 && _c < 0x80) { \
_pen_x += (_font)->kerning_cache->table[_c][_c_prev]; \
} \
else if (FT_Get_Kerning((_font)->face, (_g_prev)->idx, (_g)->idx, _kern_mode, &(_delta)) == \
0) { \
_pen_x += (int)_delta.x >> 6; \
} \
} \
} \
(void)0

Definition at line 382 of file blf_font.c.

◆ BLF_KERNING_VARS

#define BLF_KERNING_VARS (   _font,
  _has_kerning,
  _kern_mode 
)
Value:
const bool _has_kerning = FT_HAS_KERNING((_font)->face) != 0; \
const FT_UInt _kern_mode = (_has_kerning == 0) ? 0 : \
(((_font)->flags & BLF_KERNING_DEFAULT) ? \
ft_kerning_default : \
(FT_UInt)FT_KERNING_UNFITTED)
#define BLF_KERNING_DEFAULT
Definition: BLF_api.h:272

Definition at line 372 of file blf_font.c.

◆ BLF_UTF8_NEXT_FAST

#define BLF_UTF8_NEXT_FAST (   _font,
  _gc,
  _g,
  _str,
  _i,
  _c,
  _glyph_ascii_table 
)
Value:
if (((_c) = (_str)[_i]) < 0x80) { \
_g = (_glyph_ascii_table)[_c]; \
_i++; \
} \
else if ((_c = BLI_str_utf8_as_unicode_step(_str, &(_i))) != BLI_UTF8_ERR) { \
if ((_g = blf_glyph_search(_gc, _c)) == NULL) { \
_g = blf_glyph_add(_font, _gc, FT_Get_Char_Index((_font)->face, _c), _c); \
} \
} \
else { \
_g = NULL; \
} \
(void)0
#define BLI_UTF8_ERR
unsigned int BLI_str_utf8_as_unicode_step(const char *__restrict p, size_t *__restrict index) ATTR_NONNULL()
Definition: string_utf8.c:585
GlyphBLF * blf_glyph_add(FontBLF *font, GlyphCacheBLF *gc, unsigned int index, unsigned int c)
Definition: blf_glyph.c:238
GlyphBLF * blf_glyph_search(GlyphCacheBLF *gc, unsigned int c)
Definition: blf_glyph.c:222

Definition at line 357 of file blf_font.c.

Function Documentation

◆ blf_batch_cache_texture_load()

static GPUTexture* blf_batch_cache_texture_load ( void  )
static

◆ blf_batch_draw()

void blf_batch_draw ( void  )

◆ blf_batch_draw_begin()

void blf_batch_draw_begin ( FontBLF font)

◆ blf_batch_draw_end()

static void blf_batch_draw_end ( void  )
static

Definition at line 250 of file blf_font.c.

References BatchBLF::active, blf_batch_draw(), and g_batch.

Referenced by blf_font_draw_ascii_ex(), blf_font_draw_ex(), and blf_font_draw_mono().

◆ blf_batch_draw_exit()

static void blf_batch_draw_exit ( void  )
static

Definition at line 112 of file blf_font.c.

References BatchBLF::batch, g_batch, and GPU_BATCH_DISCARD_SAFE.

Referenced by blf_font_exit().

◆ blf_batch_draw_init()

static void blf_batch_draw_init ( void  )
static

◆ blf_font_ascender()

float blf_font_ascender ( FontBLF font)

◆ blf_font_attach_from_mem()

void blf_font_attach_from_mem ( FontBLF font,
const unsigned char *  mem,
int  mem_size 
)

Definition at line 1387 of file blf_font.c.

References FontBLF::face.

Referenced by BLF_metrics_attach().

◆ blf_font_boundbox()

void blf_font_boundbox ( FontBLF font,
const char *  str,
size_t  len,
rctf r_box,
struct ResultBLF r_info 
)

◆ blf_font_boundbox__wrap()

void blf_font_boundbox__wrap ( FontBLF font,
const char *  str,
size_t  len,
rctf box,
struct ResultBLF r_info 
)

◆ blf_font_boundbox_ex()

static void blf_font_boundbox_ex ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
size_t  len,
rctf box,
struct ResultBLF r_info,
int  pen_y 
)
static

◆ blf_font_boundbox_foreach_glyph()

void blf_font_boundbox_foreach_glyph ( FontBLF font,
const char *  str,
size_t  len,
BLF_GlyphBoundsFn  user_fn,
void *  user_data,
struct ResultBLF r_info 
)

◆ blf_font_boundbox_foreach_glyph_ex()

static void blf_font_boundbox_foreach_glyph_ex ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
size_t  len,
BLF_GlyphBoundsFn  user_fn,
void *  user_data,
struct ResultBLF r_info,
int  pen_y 
)
static

◆ blf_font_boundbox_wrap_cb()

static void blf_font_boundbox_wrap_cb ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
size_t  len,
int  pen_y,
void *  userdata 
)
static

Definition at line 1043 of file blf_font.c.

References blf_font_boundbox_ex(), BLI_rctf_union(), len, NULL, and str.

Referenced by blf_font_boundbox__wrap().

◆ blf_font_count_missing_chars()

int blf_font_count_missing_chars ( FontBLF font,
const char *  str,
const size_t  len,
int *  r_tot_chars 
)

Definition at line 1254 of file blf_font.c.

References BLI_str_utf8_as_unicode_step(), BLI_UTF8_ERR, Freestyle::c, len, and str.

Referenced by BLF_thumb_preview().

◆ blf_font_descender()

float blf_font_descender ( FontBLF font)

◆ blf_font_draw()

void blf_font_draw ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info 
)

Definition at line 461 of file blf_font.c.

References blf_font_draw_ex(), blf_glyph_cache_acquire(), blf_glyph_cache_release(), len, and str.

Referenced by BLF_draw_ex().

◆ blf_font_draw__wrap()

void blf_font_draw__wrap ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info 
)

Definition at line 1037 of file blf_font.c.

References blf_font_draw__wrap_cb(), blf_font_wrap_apply(), len, NULL, and str.

Referenced by BLF_draw_ascii_ex(), and BLF_draw_ex().

◆ blf_font_draw__wrap_cb()

static void blf_font_draw__wrap_cb ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
size_t  len,
int  pen_y,
void *  UNUSEDuserdata 
)
static

Definition at line 1028 of file blf_font.c.

References blf_font_draw_ex(), len, NULL, and str.

Referenced by blf_font_draw__wrap().

◆ blf_font_draw_ascii()

void blf_font_draw_ascii ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info 
)

Definition at line 512 of file blf_font.c.

References blf_font_draw_ascii_ex(), len, and str.

Referenced by BLF_draw_ascii_ex().

◆ blf_font_draw_ascii_ex()

static void blf_font_draw_ascii_ex ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info,
int  pen_y 
)
static

◆ blf_font_draw_buffer()

void blf_font_draw_buffer ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info 
)

◆ blf_font_draw_buffer__wrap()

void blf_font_draw_buffer__wrap ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info 
)

Definition at line 1073 of file blf_font.c.

References blf_font_draw_buffer__wrap_cb(), blf_font_wrap_apply(), len, NULL, and str.

Referenced by BLF_draw_buffer_ex().

◆ blf_font_draw_buffer__wrap_cb()

static void blf_font_draw_buffer__wrap_cb ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
size_t  len,
int  pen_y,
void *  UNUSEDuserdata 
)
static

Definition at line 1064 of file blf_font.c.

References blf_font_draw_buffer_ex(), len, NULL, and str.

Referenced by blf_font_draw_buffer__wrap().

◆ blf_font_draw_buffer_ex()

static void blf_font_draw_buffer_ex ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
size_t  len,
struct ResultBLF r_info,
int  pen_y 
)
static

◆ blf_font_draw_ex()

static void blf_font_draw_ex ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
size_t  len,
struct ResultBLF r_info,
int  pen_y 
)
static

◆ blf_font_draw_mono()

int blf_font_draw_mono ( FontBLF font,
const char *  str,
size_t  len,
int  cwidth 
)

◆ blf_font_ensure_ascii_kerning()

static void blf_font_ensure_ascii_kerning ( FontBLF font,
GlyphCacheBLF gc,
const FT_UInt  kern_mode 
)
static

◆ blf_font_ensure_ascii_table()

static GlyphBLF** blf_font_ensure_ascii_table ( FontBLF font,
GlyphCacheBLF gc 
)
static

◆ blf_font_exit()

void blf_font_exit ( void  )

Definition at line 269 of file blf_font.c.

References blf_batch_draw_exit(), blf_glyph_cache_mutex, BLI_spin_end(), ft_lib, and ft_lib_mutex.

Referenced by BLF_exit().

◆ blf_font_fill()

static void blf_font_fill ( FontBLF font)
static

◆ blf_font_fixed_width()

float blf_font_fixed_width ( FontBLF font)

◆ blf_font_free()

void blf_font_free ( FontBLF font)

◆ blf_font_height()

float blf_font_height ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info 
)

◆ blf_font_height_max()

int blf_font_height_max ( FontBLF font)

◆ blf_font_init()

int blf_font_init ( void  )

Definition at line 261 of file blf_font.c.

References blf_glyph_cache_mutex, BLI_spin_init(), ft_lib, ft_lib_mutex, and g_batch.

Referenced by BLF_init().

◆ blf_font_new()

FontBLF* blf_font_new ( const char *  name,
const char *  filename 
)

◆ blf_font_new_from_mem()

FontBLF* blf_font_new_from_mem ( const char *  name,
const unsigned char *  mem,
int  mem_size 
)

◆ blf_font_size()

void blf_font_size ( FontBLF font,
unsigned int  size,
unsigned int  dpi 
)

◆ blf_font_width()

float blf_font_width ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info 
)

◆ blf_font_width_and_height()

void blf_font_width_and_height ( FontBLF font,
const char *  str,
size_t  len,
float r_width,
float r_height,
struct ResultBLF r_info 
)

◆ blf_font_width_max()

int blf_font_width_max ( FontBLF font)

◆ blf_font_width_to_rstrlen()

size_t blf_font_width_to_rstrlen ( FontBLF font,
const char *  str,
size_t  len,
float  width,
float r_width 
)

◆ blf_font_width_to_strlen()

size_t blf_font_width_to_strlen ( FontBLF font,
const char *  str,
size_t  len,
float  width,
float r_width 
)

◆ blf_font_width_to_strlen_glyph_process()

static bool blf_font_width_to_strlen_glyph_process ( FontBLF font,
const bool  has_kerning,
const FT_UInt  kern_mode,
const uint  c_prev,
const uint  c,
GlyphBLF g_prev,
GlyphBLF g,
int *  pen_x,
const int  width_i 
)
static

◆ blf_font_wrap_apply()

static void blf_font_wrap_apply ( FontBLF font,
const char *  str,
size_t  len,
struct ResultBLF r_info,
void(*)(FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t len, int pen_y, void *userdata)  callback,
void *  userdata 
)
static

Generic function to add word-wrap support for other existing functions.

Wraps on spaces and respects newlines. Intentionally ignores non-unix newlines, tabs and more advanced text formatting.

Note
If we want rich text - we better have a higher level API to handle that (color, bold, switching fonts... etc).

Implementation Detail (utf8).

Take care with single byte offsets here, since this is utf8 we can't be sure a single byte is a single character.

This is only done when we know for sure the character is ascii (newline or a space).

Definition at line 921 of file blf_font.c.

References GlyphBLF::advance_i, blf_font_ensure_ascii_table(), blf_glyph_cache_acquire(), blf_glyph_cache_release(), BLF_KERNING_STEP, BLF_KERNING_VARS, BLF_UTF8_NEXT_FAST, BLI_UTF8_ERR, GlyphBLF::c, Freestyle::c, callback, GlyphCacheBLF::glyph_height_max, len, ResultBLF::lines, NULL, str, UNLIKELY, ResultBLF::width, blender::gpu::wrap(), FontBLF::wrap_width, and wrap_width().

Referenced by blf_font_boundbox__wrap(), blf_font_draw__wrap(), and blf_font_draw_buffer__wrap().

Variable Documentation

◆ blf_glyph_cache_mutex

SpinLock blf_glyph_cache_mutex
static

Definition at line 72 of file blf_font.c.

Referenced by blf_font_exit(), blf_font_fill(), blf_font_free(), and blf_font_init().

◆ ft_lib

FT_Library ft_lib
static

◆ ft_lib_mutex

SpinLock ft_lib_mutex
static

Definition at line 71 of file blf_font.c.

Referenced by blf_font_exit(), blf_font_fill(), and blf_font_init().

◆ g_batch

BatchBLF g_batch