BLF: move to C++

Also see #103343.

Pull Request: https://projects.blender.org/blender/blender/pulls/110268
This commit is contained in:
Jacques Lucke
2023-07-20 10:41:36 +02:00
parent 3c2c167d82
commit 38fe6d1dec
11 changed files with 157 additions and 132 deletions

View File

@@ -15,14 +15,14 @@ set(INC_SYS
)
set(SRC
intern/blf.c
intern/blf_default.c
intern/blf_dir.c
intern/blf_font.c
intern/blf_font_default.c
intern/blf_glyph.c
intern/blf_thumbs.c
intern/blf_util.c
intern/blf.cc
intern/blf_default.cc
intern/blf_dir.cc
intern/blf_font.cc
intern/blf_font_default.cc
intern/blf_glyph.cc
intern/blf_thumbs.cc
intern/blf_util.cc
BLF_api.h
intern/blf_internal.h

View File

@@ -44,7 +44,7 @@
} \
((void)0)
FontBLF *global_font[BLF_MAX_FONT] = {NULL};
FontBLF *global_font[BLF_MAX_FONT] = {nullptr};
/* XXX: should these be made into global_font_'s too? */
@@ -56,13 +56,13 @@ static FontBLF *blf_get(int fontid)
if (fontid >= 0 && fontid < BLF_MAX_FONT) {
return global_font[fontid];
}
return NULL;
return nullptr;
}
int BLF_init(void)
{
for (int i = 0; i < BLF_MAX_FONT; i++) {
global_font[i] = NULL;
global_font[i] = nullptr;
}
return blf_font_init();
@@ -74,7 +74,7 @@ void BLF_exit(void)
FontBLF *font = global_font[i];
if (font) {
blf_font_free(font);
global_font[i] = NULL;
global_font[i] = nullptr;
}
}
@@ -93,14 +93,14 @@ void BLF_cache_clear(void)
bool blf_font_id_is_valid(int fontid)
{
return blf_get(fontid) != NULL;
return blf_get(fontid) != nullptr;
}
static int blf_search_by_mem_name(const char *mem_name)
{
for (int i = 0; i < BLF_MAX_FONT; i++) {
const FontBLF *font = global_font[i];
if (font == NULL || font->mem_name == NULL) {
if (font == nullptr || font->mem_name == nullptr) {
continue;
}
if (font && STREQ(font->mem_name, mem_name)) {
@@ -247,7 +247,7 @@ void BLF_unload(const char *filepath)
{
for (int i = 0; i < BLF_MAX_FONT; i++) {
FontBLF *font = global_font[i];
if (font == NULL || font->filepath == NULL) {
if (font == nullptr || font->filepath == nullptr) {
continue;
}
@@ -257,7 +257,7 @@ void BLF_unload(const char *filepath)
if (font->reference_count == 0) {
blf_font_free(font);
global_font[i] = NULL;
global_font[i] = nullptr;
}
}
}
@@ -272,7 +272,7 @@ void BLF_unload_id(int fontid)
if (font->reference_count == 0) {
blf_font_free(font);
global_font[fontid] = NULL;
global_font[fontid] = nullptr;
}
}
}
@@ -283,7 +283,7 @@ void BLF_unload_all(void)
FontBLF *font = global_font[i];
if (font) {
blf_font_free(font);
global_font[i] = NULL;
global_font[i] = nullptr;
}
}
blf_mono_font = -1;
@@ -567,7 +567,7 @@ void BLF_draw(int fontid, const char *str, const size_t str_len)
/* Avoid bgl usage to corrupt BLF drawing. */
GPU_bgl_end();
BLF_draw_ex(fontid, str, str_len, NULL);
BLF_draw_ex(fontid, str, str_len, nullptr);
}
int BLF_draw_mono(int fontid, const char *str, const size_t str_len, int cwidth)
@@ -692,7 +692,7 @@ void BLF_boundbox_ex(
void BLF_boundbox(int fontid, const char *str, const size_t str_len, rcti *r_box)
{
BLF_boundbox_ex(fontid, str, str_len, r_box, NULL);
BLF_boundbox_ex(fontid, str, str_len, r_box, nullptr);
}
void BLF_width_and_height(
@@ -701,7 +701,7 @@ void BLF_width_and_height(
FontBLF *font = blf_get(fontid);
if (font) {
blf_font_width_and_height(font, str, str_len, r_width, r_height, NULL);
blf_font_width_and_height(font, str, str_len, r_width, r_height, nullptr);
}
else {
*r_width = *r_height = 0.0f;
@@ -723,7 +723,7 @@ float BLF_width_ex(int fontid, const char *str, const size_t str_len, struct Res
float BLF_width(int fontid, const char *str, const size_t str_len)
{
return BLF_width_ex(fontid, str, str_len, NULL);
return BLF_width_ex(fontid, str, str_len, nullptr);
}
float BLF_fixed_width(int fontid)
@@ -752,7 +752,7 @@ float BLF_height_ex(int fontid, const char *str, const size_t str_len, struct Re
float BLF_height(int fontid, const char *str, const size_t str_len)
{
return BLF_height_ex(fontid, str, str_len, NULL);
return BLF_height_ex(fontid, str, str_len, nullptr);
}
int BLF_height_max(int fontid)
@@ -909,7 +909,7 @@ void BLF_draw_buffer_ex(int fontid,
}
void BLF_draw_buffer(int fontid, const char *str, const size_t str_len)
{
BLF_draw_buffer_ex(fontid, str, str_len, NULL);
BLF_draw_buffer_ex(fontid, str, str_len, nullptr);
}
char *BLF_display_name_from_file(const char *filepath)
@@ -917,7 +917,7 @@ char *BLF_display_name_from_file(const char *filepath)
/* While listing font directories this function can be called simultaneously from a greater
* number of threads than we want the FreeType cache to keep open at a time. Therefore open
* with own FT_Library object and use FreeType calls directly to avoid any contention. */
char *name = NULL;
char *name = nullptr;
FT_Library ft_library;
if (FT_Init_FreeType(&ft_library) == FT_Err_Ok) {
FT_Face face;
@@ -949,7 +949,7 @@ void BLF_state_print(int fontid)
printf(" flag: %d\n", font->flags);
}
else {
printf("fontid %d (NULL)\n", fontid);
printf("fontid %d (nullptr)\n", fontid);
}
fflush(stdout);
}

View File

@@ -37,7 +37,7 @@ char *blf_dir_metrics_search(const char *filepath)
if (s) {
if (BLI_strnlen(s, 4) < 4) {
MEM_freeN(mfile);
return NULL;
return nullptr;
}
s++;
s[0] = 'a';
@@ -57,5 +57,5 @@ char *blf_dir_metrics_search(const char *filepath)
}
}
MEM_freeN(mfile);
return NULL;
return nullptr;
}

View File

@@ -57,15 +57,15 @@
BatchBLF g_batch;
/* freetype2 handle ONLY for this file! */
static FT_Library ft_lib = NULL;
static FTC_Manager ftc_manager = NULL;
static FTC_CMapCache ftc_charmap_cache = NULL;
static FT_Library ft_lib = nullptr;
static FTC_Manager ftc_manager = nullptr;
static FTC_CMapCache ftc_charmap_cache = nullptr;
/* Lock for FreeType library, used around face creation and deletion. */
static ThreadMutex ft_lib_mutex;
/* May be set to #UI_widgetbase_draw_cache_flush. */
static void (*blf_draw_cache_flush)(void) = NULL;
static void (*blf_draw_cache_flush)(void) = nullptr;
static ft_pix blf_font_height_max_ft_pix(FontBLF *font);
static ft_pix blf_font_width_max_ft_pix(FontBLF *font);
@@ -80,9 +80,9 @@ static ft_pix blf_font_width_max_ft_pix(FontBLF *font);
*/
static void blf_face_finalizer(void *object)
{
FT_Face face = object;
FT_Face face = static_cast<FT_Face>(object);
FontBLF *font = (FontBLF *)face->generic.data;
font->face = NULL;
font->face = nullptr;
}
/**
@@ -92,7 +92,7 @@ static void blf_face_finalizer(void *object)
*/
static FT_Error blf_cache_face_requester(FTC_FaceID faceID,
FT_Library lib,
FT_Pointer UNUSED(reqData),
FT_Pointer /*reqData*/,
FT_Face *face)
{
FontBLF *font = (FontBLF *)faceID;
@@ -103,7 +103,8 @@ static FT_Error blf_cache_face_requester(FTC_FaceID faceID,
err = FT_New_Face(lib, font->filepath, 0, face);
}
else if (font->mem) {
err = FT_New_Memory_Face(lib, font->mem, (FT_Long)font->mem_size, 0, face);
err = FT_New_Memory_Face(
lib, static_cast<const FT_Byte *>(font->mem), (FT_Long)font->mem_size, 0, face);
}
BLI_mutex_unlock(&ft_lib_mutex);
@@ -114,7 +115,7 @@ static FT_Error blf_cache_face_requester(FTC_FaceID faceID,
}
else {
/* Clear this on error to avoid exception in FTC_Manager_LookupFace. */
*face = NULL;
*face = nullptr;
}
return err;
@@ -125,9 +126,9 @@ static FT_Error blf_cache_face_requester(FTC_FaceID faceID,
*/
static void blf_size_finalizer(void *object)
{
FT_Size size = object;
FT_Size size = static_cast<FT_Size>(object);
FontBLF *font = (FontBLF *)size->generic.data;
font->ft_size = NULL;
font->ft_size = nullptr;
}
/** \} */
@@ -200,7 +201,7 @@ static void blf_batch_draw_init(void)
GPU_vertbuf_data_alloc(vbo, 4);
/* We render a quad as a triangle strip and instance it for each glyph. */
g_batch.batch = GPU_batch_create_ex(GPU_PRIM_TRI_STRIP, vbo, NULL, GPU_BATCH_OWNS_VBO);
g_batch.batch = GPU_batch_create_ex(GPU_PRIM_TRI_STRIP, vbo, nullptr, GPU_BATCH_OWNS_VBO);
GPU_batch_instbuf_set(g_batch.batch, g_batch.verts, true);
}
@@ -211,7 +212,7 @@ static void blf_batch_draw_exit(void)
void blf_batch_draw_begin(FontBLF *font)
{
if (g_batch.batch == NULL) {
if (g_batch.batch == nullptr) {
blf_batch_draw_init();
}
@@ -318,7 +319,7 @@ void blf_batch_draw(void)
GPU_blend(GPU_BLEND_ALPHA);
/* We need to flush widget base first to ensure correct ordering. */
if (blf_draw_cache_flush != NULL) {
if (blf_draw_cache_flush != nullptr) {
blf_draw_cache_flush();
}
@@ -418,7 +419,7 @@ static void blf_font_draw_ex(FontBLF *font,
struct ResultBLF *r_info,
const ft_pix pen_y)
{
GlyphBLF *g, *g_prev = NULL;
GlyphBLF *g, *g_prev = nullptr;
ft_pix pen_x = 0;
size_t i = 0;
@@ -432,7 +433,7 @@ static void blf_font_draw_ex(FontBLF *font,
while ((i < str_len) && str[i]) {
g = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i);
if (UNLIKELY(g == NULL)) {
if (UNLIKELY(g == nullptr)) {
continue;
}
pen_x += blf_kerning(font, g_prev, g);
@@ -474,7 +475,7 @@ int blf_font_draw_mono(FontBLF *font, const char *str, const size_t str_len, int
while ((i < str_len) && str[i]) {
g = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i);
if (UNLIKELY(g == NULL)) {
if (UNLIKELY(g == nullptr)) {
continue;
}
/* do not return this loop if clipped, we want every character tested */
@@ -619,7 +620,7 @@ static void blf_font_draw_buffer_ex(FontBLF *font,
struct ResultBLF *r_info,
ft_pix pen_y)
{
GlyphBLF *g, *g_prev = NULL;
GlyphBLF *g, *g_prev = nullptr;
ft_pix pen_x = ft_pix_from_int(font->pos[0]);
ft_pix pen_y_basis = ft_pix_from_int(font->pos[1]) + pen_y;
size_t i = 0;
@@ -632,7 +633,7 @@ static void blf_font_draw_buffer_ex(FontBLF *font,
while ((i < str_len) && str[i]) {
g = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i);
if (UNLIKELY(g == NULL)) {
if (UNLIKELY(g == nullptr)) {
continue;
}
pen_x += blf_kerning(font, g_prev, g);
@@ -672,7 +673,7 @@ void blf_font_draw_buffer(FontBLF *font,
static bool blf_font_width_to_strlen_glyph_process(
FontBLF *font, GlyphBLF *g_prev, GlyphBLF *g, ft_pix *pen_x, const int width_i)
{
if (UNLIKELY(g == NULL)) {
if (UNLIKELY(g == nullptr)) {
return false; /* continue the calling loop. */
}
*pen_x += blf_kerning(font, g_prev, g);
@@ -693,7 +694,7 @@ size_t blf_font_width_to_strlen(
GlyphCacheBLF *gc = blf_glyph_cache_acquire(font);
const int width_i = (int)width;
for (i_prev = i = 0, width_new = pen_x = 0, g_prev = NULL; (i < str_len) && str[i];
for (i_prev = i = 0, width_new = pen_x = 0, g_prev = nullptr; (i < str_len) && str[i];
i_prev = i, width_new = pen_x, g_prev = g)
{
g = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i);
@@ -729,13 +730,13 @@ size_t blf_font_width_to_rstrlen(
i_tmp = i;
g = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i_tmp);
for (width_new = pen_x = 0; (s != NULL);
i = i_prev, s = s_prev, g = g_prev, g_prev = NULL, width_new = pen_x)
for (width_new = pen_x = 0; (s != nullptr);
i = i_prev, s = s_prev, g = g_prev, g_prev = nullptr, width_new = pen_x)
{
s_prev = BLI_str_find_prev_char_utf8(s, str);
i_prev = (size_t)(s_prev - str);
if (s_prev != NULL) {
if (s_prev != nullptr) {
i_tmp = i_prev;
g_prev = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i_tmp);
BLI_assert(i_tmp == i);
@@ -768,7 +769,7 @@ static void blf_font_boundbox_ex(FontBLF *font,
struct ResultBLF *r_info,
ft_pix pen_y)
{
GlyphBLF *g, *g_prev = NULL;
GlyphBLF *g, *g_prev = nullptr;
ft_pix pen_x = 0;
size_t i = 0;
@@ -780,7 +781,7 @@ static void blf_font_boundbox_ex(FontBLF *font,
while ((i < str_len) && str[i]) {
g = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i);
if (UNLIKELY(g == NULL)) {
if (UNLIKELY(g == nullptr)) {
continue;
}
pen_x += blf_kerning(font, g_prev, g);
@@ -925,7 +926,7 @@ void blf_font_boundbox_foreach_glyph(FontBLF *font,
BLF_GlyphBoundsFn user_fn,
void *user_data)
{
GlyphBLF *g, *g_prev = NULL;
GlyphBLF *g, *g_prev = nullptr;
ft_pix pen_x = 0;
size_t i = 0, i_curr;
@@ -940,7 +941,7 @@ void blf_font_boundbox_foreach_glyph(FontBLF *font,
i_curr = i;
g = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i);
if (UNLIKELY(g == NULL)) {
if (UNLIKELY(g == nullptr)) {
continue;
}
pen_x += blf_kerning(font, g_prev, g);
@@ -961,19 +962,20 @@ void blf_font_boundbox_foreach_glyph(FontBLF *font,
blf_glyph_cache_release(font);
}
typedef struct CursorPositionForeachGlyph_Data {
struct CursorPositionForeachGlyph_Data {
/** Horizontal position to test. */
int location_x;
/** Write the character offset here. */
size_t r_offset;
} CursorPositionForeachGlyph_Data;
};
static bool blf_cursor_position_foreach_glyph(const char *UNUSED(str),
static bool blf_cursor_position_foreach_glyph(const char * /*str*/,
const size_t str_step_ofs,
const rcti *bounds,
void *user_data)
{
CursorPositionForeachGlyph_Data *data = user_data;
CursorPositionForeachGlyph_Data *data = static_cast<CursorPositionForeachGlyph_Data *>(
user_data);
if (data->location_x < (bounds->xmin + bounds->xmax) / 2) {
data->r_offset = str_step_ofs;
return false;
@@ -986,10 +988,10 @@ size_t blf_str_offset_from_cursor_position(FontBLF *font,
size_t str_len,
int location_x)
{
CursorPositionForeachGlyph_Data data = {
.location_x = location_x,
.r_offset = (size_t)-1,
};
CursorPositionForeachGlyph_Data data{};
data.location_x = location_x;
data.r_offset = (size_t)-1;
blf_font_boundbox_foreach_glyph(font, str, str_len, blf_cursor_position_foreach_glyph, &data);
if (data.r_offset == (size_t)-1) {
@@ -1004,17 +1006,17 @@ size_t blf_str_offset_from_cursor_position(FontBLF *font,
return data.r_offset;
}
typedef struct StrOffsetToGlyphBounds_Data {
struct StrOffsetToGlyphBounds_Data {
size_t str_offset;
rcti bounds;
} StrOffsetToGlyphBounds_Data;
};
static bool blf_str_offset_foreach_glyph(const char *UNUSED(str),
static bool blf_str_offset_foreach_glyph(const char * /*str*/,
const size_t str_step_ofs,
const rcti *bounds,
void *user_data)
{
StrOffsetToGlyphBounds_Data *data = user_data;
StrOffsetToGlyphBounds_Data *data = static_cast<StrOffsetToGlyphBounds_Data *>(user_data);
if (data->str_offset == str_step_ofs) {
data->bounds = *bounds;
return false;
@@ -1027,10 +1029,10 @@ void blf_str_offset_to_glyph_bounds(FontBLF *font,
size_t str_offset,
rcti *glyph_bounds)
{
StrOffsetToGlyphBounds_Data data = {
.str_offset = str_offset,
.bounds = {0},
};
StrOffsetToGlyphBounds_Data data{};
data.str_offset = str_offset;
data.bounds = {0};
blf_font_boundbox_foreach_glyph(font, str, str_offset + 1, blf_str_offset_foreach_glyph, &data);
*glyph_bounds = data.bounds;
}
@@ -1062,7 +1064,7 @@ static void blf_font_wrap_apply(FontBLF *font,
void *userdata),
void *userdata)
{
GlyphBLF *g, *g_prev = NULL;
GlyphBLF *g, *g_prev = nullptr;
ft_pix pen_x = 0;
ft_pix pen_y = 0;
size_t i = 0;
@@ -1087,7 +1089,7 @@ static void blf_font_wrap_apply(FontBLF *font,
g = blf_glyph_from_utf8_and_step(font, gc, str, str_len, &i);
if (UNLIKELY(g == NULL)) {
if (UNLIKELY(g == nullptr)) {
continue;
}
pen_x += blf_kerning(font, g_prev, g);
@@ -1129,7 +1131,7 @@ static void blf_font_wrap_apply(FontBLF *font,
i = wrap.last[1];
pen_x = 0;
pen_y -= line_height;
g_prev = NULL;
g_prev = nullptr;
lines += 1;
continue;
}
@@ -1155,16 +1157,16 @@ static void blf_font_draw__wrap_cb(FontBLF *font,
const char *str,
const size_t str_len,
ft_pix pen_y,
void *UNUSED(userdata))
void * /*userdata*/)
{
blf_font_draw_ex(font, gc, str, str_len, NULL, pen_y);
blf_font_draw_ex(font, gc, str, str_len, nullptr, pen_y);
}
void blf_font_draw__wrap(FontBLF *font,
const char *str,
const size_t str_len,
struct ResultBLF *r_info)
{
blf_font_wrap_apply(font, str, str_len, r_info, blf_font_draw__wrap_cb, NULL);
blf_font_wrap_apply(font, str, str_len, r_info, blf_font_draw__wrap_cb, nullptr);
}
/* blf_font_boundbox__wrap */
@@ -1175,10 +1177,10 @@ static void blf_font_boundbox_wrap_cb(FontBLF *font,
ft_pix pen_y,
void *userdata)
{
rcti *box = userdata;
rcti *box = static_cast<rcti *>(userdata);
rcti box_single;
blf_font_boundbox_ex(font, gc, str, str_len, &box_single, NULL, pen_y);
blf_font_boundbox_ex(font, gc, str, str_len, &box_single, nullptr, pen_y);
BLI_rcti_union(box, &box_single);
}
void blf_font_boundbox__wrap(
@@ -1198,16 +1200,16 @@ static void blf_font_draw_buffer__wrap_cb(FontBLF *font,
const char *str,
const size_t str_len,
ft_pix pen_y,
void *UNUSED(userdata))
void * /*userdata*/)
{
blf_font_draw_buffer_ex(font, gc, str, str_len, NULL, pen_y);
blf_font_draw_buffer_ex(font, gc, str, str_len, nullptr, pen_y);
}
void blf_font_draw_buffer__wrap(FontBLF *font,
const char *str,
const size_t str_len,
struct ResultBLF *r_info)
{
blf_font_wrap_apply(font, str, str_len, r_info, blf_font_draw_buffer__wrap_cb, NULL);
blf_font_wrap_apply(font, str, str_len, r_info, blf_font_draw_buffer__wrap_cb, nullptr);
}
/** \} */
@@ -1255,7 +1257,7 @@ int blf_font_ascender(FontBLF *font)
char *blf_display_name(FontBLF *font)
{
if (!blf_ensure_face(font) || !font->face->family_name) {
return NULL;
return nullptr;
}
return BLI_sprintfN("%s %s", font->face->family_name, font->face->style_name);
}
@@ -1278,7 +1280,7 @@ int blf_font_init(void)
BLF_CACHE_MAX_SIZES,
BLF_CACHE_BYTES,
blf_cache_face_requester,
NULL,
nullptr,
&ftc_manager);
if (err == FT_Err_Ok) {
/* Create a charmap cache to speed up glyph index lookups. */
@@ -1337,14 +1339,14 @@ static void blf_font_fill(FontBLF *font)
font->flags = 0;
font->size = 0;
BLI_listbase_clear(&font->cache);
font->kerning_cache = NULL;
font->kerning_cache = nullptr;
#if BLF_BLUR_ENABLE
font->blur = 0;
#endif
font->tex_size_max = -1;
font->buf_info.fbuf = NULL;
font->buf_info.cbuf = NULL;
font->buf_info.fbuf = nullptr;
font->buf_info.cbuf = nullptr;
font->buf_info.dims[0] = 0;
font->buf_info.dims[1] = 0;
font->buf_info.ch = 0;
@@ -1375,7 +1377,11 @@ bool blf_ensure_face(FontBLF *font)
err = FT_New_Face(font->ft_lib, font->filepath, 0, &font->face);
}
if (font->mem) {
err = FT_New_Memory_Face(font->ft_lib, font->mem, (FT_Long)font->mem_size, 0, &font->face);
err = FT_New_Memory_Face(font->ft_lib,
static_cast<const FT_Byte *>(font->mem),
(FT_Long)font->mem_size,
0,
&font->face);
}
if (!err) {
font->face->generic.data = font;
@@ -1462,7 +1468,8 @@ bool blf_ensure_face(FontBLF *font)
if (FT_HAS_KERNING(font) && !font->kerning_cache) {
/* Create kerning cache table and fill with value indicating "unset". */
font->kerning_cache = MEM_mallocN(sizeof(KerningCacheBLF), __func__);
font->kerning_cache = static_cast<KerningCacheBLF *>(
MEM_mallocN(sizeof(KerningCacheBLF), __func__));
for (uint i = 0; i < KERNING_CACHE_TABLE_SIZE; i++) {
for (uint j = 0; j < KERNING_CACHE_TABLE_SIZE; j++) {
font->kerning_cache->ascii_table[i][j] = KERNING_ENTRY_UNSET;
@@ -1513,7 +1520,7 @@ static const struct FaceDetails static_face_details[] = {
/**
* Create a new font from filename OR memory pointer.
* For normal operation pass NULL as FT_Library object. Pass a custom FT_Library if you
* For normal operation pass nullptr as FT_Library object. Pass a custom FT_Library if you
* want to use the font without its lifetime being managed by the FreeType cache subsystem.
*/
static FontBLF *blf_font_new_impl(const char *filepath,
@@ -1524,8 +1531,8 @@ static FontBLF *blf_font_new_impl(const char *filepath,
{
FontBLF *font = (FontBLF *)MEM_callocN(sizeof(FontBLF), "blf_font_new");
font->mem_name = mem_name ? BLI_strdup(mem_name) : NULL;
font->filepath = filepath ? BLI_strdup(filepath) : NULL;
font->mem_name = mem_name ? BLI_strdup(mem_name) : nullptr;
font->filepath = filepath ? BLI_strdup(filepath) : nullptr;
if (mem) {
font->mem = (void *)mem;
font->mem_size = mem_size;
@@ -1565,7 +1572,7 @@ static FontBLF *blf_font_new_impl(const char *filepath,
if (face_needed) {
if (!blf_ensure_face(font)) {
blf_font_free(font);
return NULL;
return nullptr;
}
}
@@ -1581,12 +1588,12 @@ static FontBLF *blf_font_new_impl(const char *filepath,
FontBLF *blf_font_new_from_filepath(const char *filepath)
{
return blf_font_new_impl(filepath, NULL, NULL, 0, NULL);
return blf_font_new_impl(filepath, nullptr, nullptr, 0, nullptr);
}
FontBLF *blf_font_new_from_mem(const char *mem_name, const uchar *mem, const size_t mem_size)
{
return blf_font_new_impl(NULL, mem_name, mem, mem_size, NULL);
return blf_font_new_impl(nullptr, mem_name, mem, mem_size, nullptr);
}
void blf_font_attach_from_mem(FontBLF *font, const uchar *mem, const size_t mem_size)
@@ -1622,7 +1629,7 @@ void blf_font_free(FontBLF *font)
FT_Done_Face(font->face);
}
BLI_mutex_unlock(&ft_lib_mutex);
font->face = NULL;
font->face = nullptr;
}
if (font->filepath) {
MEM_freeN(font->filepath);

View File

@@ -24,7 +24,7 @@
static int blf_load_font_default(const char *filename, const bool unique)
{
const char *dir = BKE_appdir_folder_id(BLENDER_DATAFILES, BLF_DATAFILES_FONTS_DIR);
if (dir == NULL) {
if (dir == nullptr) {
fprintf(stderr,
"%s: 'fonts' data path not found for '%s', will not be able to display text\n",
__func__,
@@ -74,7 +74,8 @@ static void blf_load_datafiles_dir(void)
const char *filepath = file_list[i].path;
if (!BLI_path_extension_check_n(
filepath, ".ttf", ".ttc", ".otf", ".otc", ".woff", ".woff2", NULL)) {
filepath, ".ttf", ".ttc", ".otf", ".otc", ".woff", ".woff2", nullptr))
{
continue;
}
if (BLF_is_loaded(filepath)) {

View File

@@ -77,15 +77,15 @@ static GlyphCacheBLF *blf_glyph_cache_find(const FontBLF *font, const float size
}
gc = gc->next;
}
return NULL;
return nullptr;
}
static GlyphCacheBLF *blf_glyph_cache_new(FontBLF *font)
{
GlyphCacheBLF *gc = (GlyphCacheBLF *)MEM_callocN(sizeof(GlyphCacheBLF), "blf_glyph_cache_new");
gc->next = NULL;
gc->prev = NULL;
gc->next = nullptr;
gc->prev = nullptr;
gc->size = font->size;
gc->bold = ((font->flags & BLF_BOLD) != 0);
gc->italic = ((font->flags & BLF_ITALIC) != 0);
@@ -141,7 +141,7 @@ static void blf_glyph_cache_free(GlyphCacheBLF *gc)
{
GlyphBLF *g;
for (uint i = 0; i < ARRAY_SIZE(gc->bucket); i++) {
while ((g = BLI_pophead(&gc->bucket[i]))) {
while ((g = static_cast<GlyphBLF *>(BLI_pophead(&gc->bucket[i])))) {
blf_glyph_free(g);
}
}
@@ -160,7 +160,7 @@ void blf_glyph_cache_clear(FontBLF *font)
BLI_mutex_lock(&font->glyph_cache_mutex);
while ((gc = BLI_pophead(&font->cache))) {
while ((gc = static_cast<GlyphCacheBLF *>(BLI_pophead(&font->cache)))) {
blf_glyph_cache_free(gc);
}
@@ -170,7 +170,7 @@ void blf_glyph_cache_clear(FontBLF *font)
/**
* Try to find a glyph in cache.
*
* \return NULL if not found.
* \return nullptr if not found.
*/
static GlyphBLF *blf_glyph_cache_find_glyph(const GlyphCacheBLF *gc, uint charcode)
{
@@ -178,14 +178,14 @@ static GlyphBLF *blf_glyph_cache_find_glyph(const GlyphCacheBLF *gc, uint charco
return gc->glyph_ascii_table[charcode];
}
GlyphBLF *g = gc->bucket[blf_hash(charcode)].first;
GlyphBLF *g = static_cast<GlyphBLF *>(gc->bucket[blf_hash(charcode)].first);
while (g) {
if (g->c == charcode) {
return g;
}
g = g->next;
}
return NULL;
return nullptr;
}
#ifdef BLF_GAMMA_CORRECT_GLYPHS
@@ -268,7 +268,7 @@ static GlyphBLF *blf_glyph_cache_add_glyph(
}
#endif /* BLF_GAMMA_CORRECT_GLYPHS */
}
g->bitmap = MEM_mallocN((size_t)buffer_size, "glyph bitmap");
g->bitmap = static_cast<unsigned char *>(MEM_mallocN((size_t)buffer_size, "glyph bitmap"));
memcpy(g->bitmap, glyph->bitmap.buffer, (size_t)buffer_size);
}
@@ -570,10 +570,10 @@ static const struct UnicodeBlock *blf_charcode_to_unicode_block(const uint charc
/* Binary search for other blocks. */
int min = 0;
int max = ARRAY_SIZE(unicode_blocks) - 1;
int max = int(ARRAY_SIZE(unicode_blocks)) - 1;
if (charcode < unicode_blocks[0].first || charcode > unicode_blocks[max].last) {
return NULL;
return nullptr;
}
while (max >= min) {
@@ -589,7 +589,7 @@ static const struct UnicodeBlock *blf_charcode_to_unicode_block(const uint charc
}
}
return NULL;
return nullptr;
}
static int blf_charcode_to_coverage_bit(uint charcode)
@@ -671,7 +671,7 @@ static FT_UInt blf_glyph_index_from_charcode(FontBLF **font, const uint charcode
}
/* Last look in anything else. Also check if we have a last-resort font. */
FontBLF *last_resort = NULL;
FontBLF *last_resort = nullptr;
for (int i = 0; i < BLF_MAX_FONT; i++) {
FontBLF *f = global_font[i];
if (!f || f == *font || !(f->flags & BLF_DEFAULT)) {
@@ -743,7 +743,7 @@ static FT_GlyphSlot blf_glyph_load(FontBLF *font, FT_UInt glyph_index)
if (FT_Load_Glyph(font->face, glyph_index, load_flags) == FT_Err_Ok) {
return font->face->glyph;
}
return NULL;
return nullptr;
}
/** \} */
@@ -761,7 +761,7 @@ static bool blf_glyph_render_bitmap(FontBLF *font, FT_GlyphSlot glyph)
FT_RENDER_MODE_NORMAL;
/* Render the glyph curves to a bitmap. */
FT_Error err = FT_Render_Glyph(glyph, render_mode);
FT_Error err = FT_Render_Glyph(glyph, FT_Render_Mode(render_mode));
if (err != FT_Err_Ok) {
return false;
}
@@ -805,7 +805,7 @@ static const FT_Var_Axis *blf_var_axis_by_tag(const FT_MM_Var *variations,
{
*r_axis_index = -1;
if (!variations) {
return NULL;
return nullptr;
}
for (int i = 0; i < (int)variations->num_axis; i++) {
if (variations->axis[i].tag == tag) {
@@ -814,7 +814,7 @@ static const FT_Var_Axis *blf_var_axis_by_tag(const FT_MM_Var *variations,
break;
}
}
return NULL;
return nullptr;
}
/**
@@ -1052,7 +1052,7 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font,
FT_GlyphSlot glyph = blf_glyph_load(glyph_font, glyph_index);
if (!glyph) {
return NULL;
return nullptr;
}
if ((settings_font->flags & BLF_MONOSPACED) && (settings_font != glyph_font)) {
@@ -1077,7 +1077,7 @@ static FT_GlyphSlot blf_glyph_render(FontBLF *settings_font,
if (blf_glyph_render_bitmap(glyph_font, glyph)) {
return glyph;
}
return NULL;
return nullptr;
}
GlyphBLF *blf_glyph_ensure(FontBLF *font, GlyphCacheBLF *gc, const uint charcode)
@@ -1092,7 +1092,7 @@ GlyphBLF *blf_glyph_ensure(FontBLF *font, GlyphCacheBLF *gc, const uint charcode
FT_UInt glyph_index = blf_glyph_index_from_charcode(&font_with_glyph, charcode);
if (!blf_ensure_face(font_with_glyph)) {
return NULL;
return nullptr;
}
FT_GlyphSlot glyph = blf_glyph_render(
@@ -1161,13 +1161,13 @@ static void blf_texture_draw(const uchar color[4],
{
/* Only one vertex per glyph, geometry shader expand it into a quad. */
/* TODO: Get rid of Geom Shader because it's not optimal AT ALL for the GPU. */
copy_v4_fl4(GPU_vertbuf_raw_step(&g_batch.pos_step),
copy_v4_fl4(static_cast<float *>(GPU_vertbuf_raw_step(&g_batch.pos_step)),
(float)(x1 + g_batch.ofs[0]),
(float)(y1 + g_batch.ofs[1]),
(float)(x2 + g_batch.ofs[0]),
(float)(y2 + g_batch.ofs[1]));
copy_v4_v4_uchar(GPU_vertbuf_raw_step(&g_batch.col_step), color);
copy_v2_v2_int(GPU_vertbuf_raw_step(&g_batch.glyph_size_step), glyph_size);
copy_v4_v4_uchar(static_cast<unsigned char *>(GPU_vertbuf_raw_step(&g_batch.col_step)), color);
copy_v2_v2_int(static_cast<int *>(GPU_vertbuf_raw_step(&g_batch.glyph_size_step)), glyph_size);
*((int *)GPU_vertbuf_raw_step(&g_batch.offset_step)) = offset;
g_batch.glyph_len++;
@@ -1215,7 +1215,7 @@ void blf_glyph_draw(FontBLF *font, GlyphCacheBLF *gc, GlyphBLF *g, const int x,
return;
}
if (g->glyph_cache == NULL) {
if (g->glyph_cache == nullptr) {
if (font->tex_size_max == -1) {
font->tex_size_max = GPU_max_texture_size();
}
@@ -1230,14 +1230,15 @@ void blf_glyph_draw(FontBLF *font, GlyphCacheBLF *gc, GlyphBLF *g, const int x,
int h = bitmap_len / w + 1;
gc->bitmap_len_alloc = w * h;
gc->bitmap_result = MEM_reallocN(gc->bitmap_result, (size_t)gc->bitmap_len_alloc);
gc->bitmap_result = static_cast<char *>(
MEM_reallocN(gc->bitmap_result, (size_t)gc->bitmap_len_alloc));
/* Keep in sync with the texture. */
if (gc->texture) {
GPU_texture_free(gc->texture);
}
gc->texture = GPU_texture_create_2d(
__func__, w, h, 1, GPU_R8, GPU_TEXTURE_USAGE_SHADER_READ, NULL);
__func__, w, h, 1, GPU_R8, GPU_TEXTURE_USAGE_SHADER_READ, nullptr);
gc->bitmap_len_landed = 0;
}

View File

@@ -8,6 +8,10 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
struct FontBLF;
struct GlyphBLF;
struct GlyphCacheBLF;
@@ -178,3 +182,7 @@ extern FT_Error FT_New_Face__win32_compat(FT_Library library,
FT_Face *aface);
# endif
#endif
#ifdef __cplusplus
}
#endif

View File

@@ -11,6 +11,10 @@
#include "GPU_texture.h"
#include "GPU_vertex_buffer.h"
#ifdef __cplusplus
extern "C" {
#endif
#include FT_MULTIPLE_MASTERS_H /* Variable font support. */
/** Maximum variation axes per font. */
@@ -342,3 +346,7 @@ typedef struct FontBLF {
/** Mutex lock for glyph cache. */
ThreadMutex glyph_cache_mutex;
} FontBLF;
#ifdef __cplusplus
}
#endif

View File

@@ -34,14 +34,14 @@
#include "BLI_strict_flags.h"
/* Maximum length of text sample in char32_t, including NULL terminator. */
/* Maximum length of text sample in char32_t, including nullptr terminator. */
#define BLF_SAMPLE_LEN 5
typedef struct UnicodeSample {
struct UnicodeSample {
char32_t sample[BLF_SAMPLE_LEN];
int field; /* OS/2 table ulUnicodeRangeX field (1-4). */
FT_ULong mask; /* OS/2 table ulUnicodeRangeX bit mask. */
} UnicodeSample;
int field; /* 'OS/2' table ulUnicodeRangeX field (1-4). */
FT_ULong mask; /* 'OS/2' table ulUnicodeRangeX bit mask. */
};
/* The seemingly arbitrary order that follows is to help quickly find the most-likely designed
* intent of the font. Many feature-specific fonts contain Latin, Greek, & Coptic characters so
@@ -304,10 +304,10 @@ static const char32_t *blf_get_sample_text(FT_Face face)
return sample;
}
bool BLF_thumb_preview(const char *filename, uchar *buf, int w, int h, int UNUSED(channels))
bool BLF_thumb_preview(const char *filename, uchar *buf, int w, int h, int /*channels*/)
{
/* Use own FT_Library and direct FreeType calls as this is called from multiple threads. */
FT_Library ft_lib = NULL;
FT_Library ft_lib = nullptr;
if (FT_Init_FreeType(&ft_lib) != FT_Err_Ok) {
return false;
}