Blender  V3.3
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 "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.

Functions

Font Query: Attributes
static ft_pix blf_font_height_max_ft_pix (struct FontBLF *font)
 
static ft_pix blf_font_width_max_ft_pix (struct FontBLF *font)
 
int blf_font_height_max (FontBLF *font)
 
int blf_font_width_max (FontBLF *font)
 
int blf_font_descender (FontBLF *font)
 
int blf_font_ascender (FontBLF *font)
 
char * blf_display_name (FontBLF *font)
 
FreeType Utilities (Internal)
static ft_pix blf_unscaled_F26Dot6_to_pixels (FontBLF *font, FT_Pos value)
 
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)
 
Glyph Stepping Utilities (Internal)
BLI_INLINE GlyphBLFblf_glyph_from_utf8_and_step (FontBLF *font, GlyphCacheBLF *gc, const char *str, size_t str_len, size_t *i_p)
 
BLI_INLINE ft_pix blf_kerning (FontBLF *font, const GlyphBLF *g_prev, const GlyphBLF *g)
 
Text Drawing: GPU
static void blf_font_draw_ex (FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, struct ResultBLF *r_info, ft_pix pen_y)
 
void blf_font_draw (FontBLF *font, const char *str, const size_t str_len, struct ResultBLF *r_info)
 
int blf_font_draw_mono (FontBLF *font, const char *str, const size_t str_len, int cwidth)
 
Text Drawing: Buffer
static void blf_font_draw_buffer_ex (FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, struct ResultBLF *r_info, ft_pix pen_y)
 
void blf_font_draw_buffer (FontBLF *font, const char *str, const size_t str_len, struct ResultBLF *r_info)
 
Text Evaluation: Width to String Length

Use to implement exported functions:

static bool blf_font_width_to_strlen_glyph_process (FontBLF *font, GlyphBLF *g_prev, GlyphBLF *g, ft_pix *pen_x, const int width_i)
 
size_t blf_font_width_to_strlen (FontBLF *font, const char *str, const size_t str_len, int width, int *r_width)
 
size_t blf_font_width_to_rstrlen (FontBLF *font, const char *str, const size_t str_len, int width, int *r_width)
 
Text Evaluation: Glyph Bound Box with Callback
static void blf_font_boundbox_ex (FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, rcti *box, struct ResultBLF *r_info, ft_pix pen_y)
 
void blf_font_boundbox (FontBLF *font, const char *str, const size_t str_len, rcti *r_box, struct ResultBLF *r_info)
 
void blf_font_width_and_height (FontBLF *font, const char *str, const size_t str_len, float *r_width, float *r_height, struct ResultBLF *r_info)
 
float blf_font_width (FontBLF *font, const char *str, const size_t str_len, struct ResultBLF *r_info)
 
float blf_font_height (FontBLF *font, const char *str, const size_t str_len, struct ResultBLF *r_info)
 
float blf_font_fixed_width (FontBLF *font)
 
static void blf_font_boundbox_foreach_glyph_ex (FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, BLF_GlyphBoundsFn user_fn, void *user_data, struct ResultBLF *r_info, ft_pix pen_y)
 
void blf_font_boundbox_foreach_glyph (FontBLF *font, const char *str, const size_t str_len, BLF_GlyphBoundsFn user_fn, void *user_data, struct ResultBLF *r_info)
 
Text Evaluation: Word-Wrap with Callback
static void blf_font_wrap_apply (FontBLF *font, const char *str, const size_t str_len, struct ResultBLF *r_info, void(*callback)(FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, ft_pix pen_y, void *userdata), void *userdata)
 
static void blf_font_draw__wrap_cb (FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, ft_pix pen_y, void *UNUSED(userdata))
 
void blf_font_draw__wrap (FontBLF *font, const char *str, const size_t str_len, struct ResultBLF *r_info)
 
static void blf_font_boundbox_wrap_cb (FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, ft_pix pen_y, void *userdata)
 
void blf_font_boundbox__wrap (FontBLF *font, const char *str, const size_t str_len, rcti *box, struct ResultBLF *r_info)
 
static void blf_font_draw_buffer__wrap_cb (FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, ft_pix pen_y, void *UNUSED(userdata))
 
void blf_font_draw_buffer__wrap (FontBLF *font, const char *str, const size_t str_len, struct ResultBLF *r_info)
 
Text Evaluation: Count Missing Characters
int blf_font_count_missing_chars (FontBLF *font, const char *str, const size_t str_len, int *r_tot_chars)
 
Font Subsystem Init/Exit
int blf_font_init (void)
 
void blf_font_exit (void)
 
void BLF_cache_flush_set_fn (void(*cache_flush_fn)(void))
 
Font New/Free
static void blf_font_fill (FontBLF *font)
 
FontBLFblf_font_new (const char *name, const char *filepath)
 
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)
 
void blf_font_free (FontBLF *font)
 
Font Configure
bool blf_font_size (FontBLF *font, float size, unsigned int dpi)
 

Variables

BatchBLF g_batch
 
static FT_Library ft_lib
 
static SpinLock ft_lib_mutex
 
static SpinLock blf_glyph_cache_mutex
 
static void(* blf_draw_cache_flush )(void) = NULL
 

Detailed Description

Deals with drawing text to OpenGL or bitmap buffers.

Also low level functions for managing FontBLF.

Definition in 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 262 of file blf_font.c.

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

Referenced by blf_font_draw_ex(), and blf_font_draw_mono().

◆ blf_batch_draw_exit()

static void blf_batch_draw_exit ( void  )
static

Definition at line 124 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_cache_flush_set_fn()

void BLF_cache_flush_set_fn ( void(*)(void cache_flush_fn)

Optional cache flushing function, called before blf_batch_draw.

Definition at line 1188 of file blf_font.c.

References blf_draw_cache_flush.

Referenced by uiStyleInit().

◆ blf_display_name()

char* blf_display_name ( FontBLF font)

Definition at line 1158 of file blf_font.c.

References BLI_sprintfN(), FontBLF::face, and NULL.

Referenced by BLF_display_name_from_file().

◆ blf_font_ascender()

int blf_font_ascender ( FontBLF font)

Definition at line 1153 of file blf_font.c.

References FontBLF::face, and ft_pix_to_int().

Referenced by BLF_ascender(), and BLF_thumb_preview().

◆ blf_font_attach_from_mem()

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

Definition at line 1329 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,
const size_t  str_len,
rcti r_box,
struct ResultBLF r_info 
)

◆ blf_font_boundbox__wrap()

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

◆ blf_font_boundbox_ex()

static void blf_font_boundbox_ex ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
const size_t  str_len,
rcti box,
struct ResultBLF r_info,
ft_pix  pen_y 
)
static

◆ blf_font_boundbox_foreach_glyph()

void blf_font_boundbox_foreach_glyph ( FontBLF font,
const char *  str,
const size_t  str_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,
const size_t  str_len,
BLF_GlyphBoundsFn  user_fn,
void user_data,
struct ResultBLF r_info,
ft_pix  pen_y 
)
static

◆ blf_font_boundbox_wrap_cb()

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

Definition at line 1026 of file blf_font.c.

References blf_font_boundbox_ex(), BLI_rcti_union(), 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  str_len,
int *  r_tot_chars 
)

Definition at line 1074 of file blf_font.c.

References BLI_str_utf8_as_unicode_step(), Freestyle::c, GLYPH_ASCII_TABLE_SIZE, and str.

Referenced by BLF_thumb_preview().

◆ blf_font_descender()

int blf_font_descender ( FontBLF font)

Definition at line 1148 of file blf_font.c.

References FontBLF::face, and ft_pix_to_int().

Referenced by BLF_descender().

◆ blf_font_draw()

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

Definition at line 371 of file blf_font.c.

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

Referenced by BLF_draw_ex().

◆ blf_font_draw__wrap()

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

Definition at line 1017 of file blf_font.c.

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

Referenced by BLF_draw_ex().

◆ blf_font_draw__wrap_cb()

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

Definition at line 1008 of file blf_font.c.

References blf_font_draw_ex(), NULL, and str.

Referenced by blf_font_draw__wrap().

◆ blf_font_draw_buffer()

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

◆ blf_font_draw_buffer__wrap()

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

Definition at line 1060 of file blf_font.c.

References blf_font_draw_buffer__wrap_cb(), blf_font_wrap_apply(), 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,
const size_t  str_len,
ft_pix  pen_y,
void UNUSEDuserdata 
)
static

Definition at line 1051 of file blf_font.c.

References blf_font_draw_buffer_ex(), 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,
const size_t  str_len,
struct ResultBLF r_info,
ft_pix  pen_y 
)
static

◆ blf_font_draw_ex()

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

◆ blf_font_draw_mono()

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

◆ blf_font_exit()

void blf_font_exit ( void  )

Definition at line 1180 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,
const size_t  str_len,
struct ResultBLF r_info 
)

◆ blf_font_height_max()

int blf_font_height_max ( FontBLF font)

Definition at line 1122 of file blf_font.c.

References blf_font_height_max_ft_pix(), and ft_pix_to_int().

Referenced by BLF_height_max().

◆ blf_font_height_max_ft_pix()

static ft_pix blf_font_height_max_ft_pix ( struct FontBLF font)
static

Definition at line 1106 of file blf_font.c.

References FontBLF::face, ft_pix_from_int(), and MAX2.

Referenced by blf_font_height_max(), and blf_font_wrap_apply().

◆ blf_font_init()

int blf_font_init ( void  )

Definition at line 1172 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 *  filepath 
)

◆ blf_font_new_from_mem()

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

◆ blf_font_size()

bool blf_font_size ( struct FontBLF font,
float  size,
unsigned int  dpi 
)

Change font's output size. Returns true if successful in changing the size.

Definition at line 1408 of file blf_font.c.

References FontBLF::dpi, FontBLF::face, float(), size(), and FontBLF::size.

Referenced by BLF_size(), and BLF_thumb_preview().

◆ blf_font_width()

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

◆ blf_font_width_and_height()

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

◆ blf_font_width_max()

int blf_font_width_max ( FontBLF font)

Definition at line 1143 of file blf_font.c.

References blf_font_width_max_ft_pix(), and ft_pix_to_int().

Referenced by BLF_width_max().

◆ blf_font_width_max_ft_pix()

static ft_pix blf_font_width_max_ft_pix ( struct FontBLF font)
static

Definition at line 1127 of file blf_font.c.

References FontBLF::face, ft_pix_from_int(), and MAX2.

Referenced by blf_font_width_max().

◆ blf_font_width_to_rstrlen()

size_t blf_font_width_to_rstrlen ( FontBLF font,
const char *  str,
const size_t  str_len,
int  width,
int *  r_width 
)

◆ blf_font_width_to_strlen()

size_t blf_font_width_to_strlen ( FontBLF font,
const char *  str,
const size_t  str_len,
int  width,
int *  r_width 
)

◆ blf_font_width_to_strlen_glyph_process()

static bool blf_font_width_to_strlen_glyph_process ( FontBLF font,
GlyphBLF g_prev,
GlyphBLF g,
ft_pix pen_x,
const int  width_i 
)
static

◆ blf_font_wrap_apply()

static void blf_font_wrap_apply ( FontBLF font,
const char *  str,
const size_t  str_len,
struct ResultBLF r_info,
void(*)(FontBLF *font, GlyphCacheBLF *gc, const char *str, const size_t str_len, ft_pix 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 907 of file blf_font.c.

References blf_font_height_max_ft_pix(), blf_glyph_cache_acquire(), blf_glyph_cache_release(), blf_glyph_from_utf8_and_step(), blf_kerning(), GlyphBLF::c, callback, FT_PIX_FLOOR, ft_pix_from_int(), ft_pix_round_advance(), ft_pix_to_int(), usdtokens::g(), ResultBLF::lines, NULL, str, UNLIKELY, ResultBLF::width, blender::bke::image::partial_update::wrap(), FontBLF::wrap_width, and wrap_width().

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

◆ blf_glyph_from_utf8_and_step()

BLI_INLINE GlyphBLF* blf_glyph_from_utf8_and_step ( FontBLF font,
GlyphCacheBLF gc,
const char *  str,
size_t  str_len,
size_t *  i_p 
)

◆ blf_kerning()

BLI_INLINE ft_pix blf_kerning ( FontBLF font,
const GlyphBLF g_prev,
const GlyphBLF g 
)

◆ blf_unscaled_F26Dot6_to_pixels()

static ft_pix blf_unscaled_F26Dot6_to_pixels ( FontBLF font,
FT_Pos  value 
)
static

Definition at line 70 of file blf_font.c.

References FontBLF::face, and scaled().

Referenced by blf_kerning().

Variable Documentation

◆ blf_draw_cache_flush

void(* blf_draw_cache_flush) (void) ( void  ) = NULL
static

Definition at line 60 of file blf_font.c.

Referenced by blf_batch_draw(), and BLF_cache_flush_set_fn().

◆ blf_glyph_cache_mutex

SpinLock blf_glyph_cache_mutex
static

Definition at line 57 of file blf_font.c.

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

◆ ft_lib

FT_Library ft_lib
static

◆ ft_lib_mutex

SpinLock ft_lib_mutex
static

Definition at line 56 of file blf_font.c.

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

◆ g_batch

BatchBLF g_batch