Cleanup: split BLF default into own file

This avoids accidents using user-preferences in the main BLF API,
which could cause preferences to be used unintentionally
(such as stamping into renders or creating generated images).

As well as uses of BLF when preferences aren't loaded
such as animation playback.
This commit is contained in:
Campbell Barton 2020-10-22 16:29:15 +11:00
parent fe963b5a41
commit cf5ae6718c
4 changed files with 104 additions and 65 deletions

View File

@ -38,6 +38,7 @@ 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

View File

@ -37,10 +37,6 @@
#include "MEM_guardedalloc.h"
#include "DNA_listBase.h"
#include "DNA_userdef_types.h"
#include "DNA_vec_types.h"
#include "BLI_math.h"
#include "BLI_threads.h"
@ -48,9 +44,6 @@
#include "IMB_colormanagement.h"
#include "UI_interface.h"
#include "GPU_immediate.h"
#include "GPU_matrix.h"
#include "GPU_shader.h"
@ -64,9 +57,6 @@
*/
#define BLF_MAX_FONT 16
/* call BLF_default_set first! */
#define ASSERT_DEFAULT_SET BLI_assert(global_font_default != -1)
#define BLF_RESULT_CHECK_INIT(r_info) \
if (r_info) { \
memset(r_info, 0, sizeof(*(r_info))); \
@ -76,10 +66,6 @@
/* Font array. */
static FontBLF *global_font[BLF_MAX_FONT] = {NULL};
/* Default size and dpi, for BLF_draw_default. */
static int global_font_default = -1;
static int global_font_dpi = 72;
/* XXX, should these be made into global_font_'s too? */
int blf_mono_font = -1;
int blf_mono_font_render = -1;
@ -98,16 +84,11 @@ int BLF_init(void)
global_font[i] = NULL;
}
global_font_dpi = 72;
BLF_default_dpi(72);
return blf_font_init();
}
void BLF_default_dpi(int dpi)
{
global_font_dpi = dpi;
}
void BLF_exit(void)
{
for (int i = 0; i < BLF_MAX_FONT; i++) {
@ -132,6 +113,11 @@ void BLF_cache_clear(void)
}
}
bool blf_font_id_is_valid(int fontid)
{
return blf_get(fontid) != NULL;
}
static int blf_search(const char *name)
{
for (int i = 0; i < BLF_MAX_FONT; i++) {
@ -155,20 +141,6 @@ static int blf_search_available(void)
return -1;
}
void BLF_default_set(int fontid)
{
FontBLF *font = blf_get(fontid);
if (font || fontid == -1) {
global_font_default = fontid;
}
}
int BLF_default(void)
{
ASSERT_DEFAULT_SET;
return global_font_default;
}
bool BLF_has_glyph(int fontid, unsigned int unicode)
{
FontBLF *font = blf_get(fontid);
@ -515,37 +487,6 @@ void BLF_batch_draw_end(void)
g_batch.enabled = false;
}
void BLF_draw_default(float x, float y, float z, const char *str, size_t len)
{
ASSERT_DEFAULT_SET;
const uiStyle *style = UI_style_get();
BLF_size(global_font_default, style->widgetlabel.points, global_font_dpi);
BLF_position(global_font_default, x, y, z);
BLF_draw(global_font_default, str, len);
}
/* same as above but call 'BLF_draw_ascii' */
void BLF_draw_default_ascii(float x, float y, float z, const char *str, size_t len)
{
ASSERT_DEFAULT_SET;
const uiStyle *style = UI_style_get();
BLF_size(global_font_default, style->widgetlabel.points, global_font_dpi);
BLF_position(global_font_default, x, y, z);
BLF_draw_ascii(global_font_default, str, len); /* XXX, use real length */
}
int BLF_set_default(void)
{
ASSERT_DEFAULT_SET;
const uiStyle *style = UI_style_get();
BLF_size(global_font_default, style->widgetlabel.points, global_font_dpi);
return global_font_default;
}
static void blf_draw_gl__start(FontBLF *font)
{
/*

View File

@ -0,0 +1,95 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2009 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup blf
*
* Default API, that uses Blender's user preferences for the default size.
*/
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include "DNA_userdef_types.h"
#include "BLI_assert.h"
#include "BLF_api.h"
#include "UI_interface.h"
#include "blf_internal.h"
/* call BLF_default_set first! */
#define ASSERT_DEFAULT_SET BLI_assert(global_font_default != -1)
/* Default size and dpi, for BLF_draw_default. */
static int global_font_default = -1;
static int global_font_dpi = 72;
void BLF_default_dpi(int dpi)
{
global_font_dpi = dpi;
}
void BLF_default_set(int fontid)
{
if ((fontid == -1) || blf_font_id_is_valid(fontid)) {
global_font_default = fontid;
}
}
int BLF_default(void)
{
ASSERT_DEFAULT_SET;
return global_font_default;
}
int BLF_set_default(void)
{
ASSERT_DEFAULT_SET;
const uiStyle *style = UI_style_get();
BLF_size(global_font_default, style->widgetlabel.points, global_font_dpi);
return global_font_default;
}
void BLF_draw_default(float x, float y, float z, const char *str, size_t len)
{
ASSERT_DEFAULT_SET;
const uiStyle *style = UI_style_get();
BLF_size(global_font_default, style->widgetlabel.points, global_font_dpi);
BLF_position(global_font_default, x, y, z);
BLF_draw(global_font_default, str, len);
}
/* same as above but call 'BLF_draw_ascii' */
void BLF_draw_default_ascii(float x, float y, float z, const char *str, size_t len)
{
ASSERT_DEFAULT_SET;
const uiStyle *style = UI_style_get();
BLF_size(global_font_default, style->widgetlabel.points, global_font_dpi);
BLF_position(global_font_default, x, y, z);
BLF_draw_ascii(global_font_default, str, len); /* XXX, use real length */
}

View File

@ -43,6 +43,8 @@ char *blf_dir_metrics_search(const char *filename);
int blf_font_init(void);
void blf_font_exit(void);
bool blf_font_id_is_valid(int fontid);
void blf_draw_buffer__start(struct FontBLF *font);
void blf_draw_buffer__end(void);