UI: remove non-unicode font and simplify default font loading code

There is no need to have another font embedded in the Blender executable, we
can assume the bundled font exists. In the future we may provide a fallback
if the font specified by the user in the preferences is missing a character,
but that can use our bundled international font.

Differential Revision: https://developer.blender.org/D6854
This commit is contained in:
2020-02-17 14:07:18 +01:00
parent 9070999c21
commit 68e341e9d5
13 changed files with 74 additions and 224 deletions

View File

@@ -63,50 +63,6 @@ macro(suffix_relpaths
unset(_file)
endmacro()
macro(data_to_c
file_from file_to
list_to_add)
list(APPEND ${list_to_add} ${file_to})
get_filename_component(_file_to_path ${file_to} PATH)
add_custom_command(
OUTPUT ${file_to}
COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
COMMAND "$<TARGET_FILE:datatoc>" ${file_from} ${file_to}
DEPENDS ${file_from} datatoc)
unset(_file_to_path)
endmacro()
# same as above but generates the var name and output automatic.
function(data_to_c_simple
file_from
list_to_add
)
# remove ../'s
get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from} REALPATH)
get_filename_component(_file_to ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
list(APPEND ${list_to_add} ${_file_to})
source_group(Generated FILES ${_file_to})
list(APPEND ${list_to_add} ${file_from})
set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
get_filename_component(_file_to_path ${_file_to} PATH)
message(OUTPUT " ${_file_to}")
add_custom_command(
OUTPUT ${_file_to}
COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
DEPENDS ${_file_from} datatoc)
set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
endfunction()
# -----------------------------------------------------------------------------
# Defines
@@ -311,13 +267,6 @@ target_link_libraries(gears_cpp
# MultiTest (C)
set(data_to_c_files)
data_to_c(
${CMAKE_SOURCE_DIR}/../../../release/datafiles/bfont.ttf
${CMAKE_CURRENT_BINARY_DIR}/bfont.ttf.c
data_to_c_files
)
add_executable(multitest_c
${CMAKE_SOURCE_DIR}/multitest/Basic.c
${CMAKE_SOURCE_DIR}/multitest/EventToBuf.c
@@ -326,7 +275,6 @@ add_executable(multitest_c
${CMAKE_SOURCE_DIR}/multitest/Util.c
${CMAKE_SOURCE_DIR}/multitest/WindowData.c
${CMAKE_SOURCE_DIR}/multitest/stubs.c
${data_to_c_files}
)

View File

@@ -595,7 +595,7 @@ LoggerWindow *loggerwindow_new(MultiTestApp *app)
lw->app = app;
lw->win = win;
lw->font = BLF_load_mem("default", (unsigned char *)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
lw->font = BLF_load_default(false);
BLF_size(lw->font, 11, 72);
lw->fontheight = BLF_height(lw->font, "A_", 2);

Binary file not shown.

Binary file not shown.

View File

@@ -238,11 +238,9 @@ void BLF_thumb_preview(const char *filename,
int h,
int channels) ATTR_NONNULL();
/* blf_font_i18.c */
unsigned char *BLF_get_unifont(int *unifont_size);
void BLF_free_unifont(void);
unsigned char *BLF_get_unifont_mono(int *unifont_size);
void BLF_free_unifont_mono(void);
/* blf_font_default.c */
int BLF_load_default(const bool unique);
int BLF_load_mono_default(const bool unique);
#ifdef DEBUG
void BLF_state_print(int fontid);

View File

@@ -41,7 +41,7 @@ set(SRC
intern/blf.c
intern/blf_dir.c
intern/blf_font.c
intern/blf_font_i18n.c
intern/blf_font_default.c
intern/blf_glyph.c
intern/blf_thumbs.c
intern/blf_util.c

View File

@@ -0,0 +1,59 @@
/*
* 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) 2011 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup blf
*
* API for loading default font files.
*/
#include <stdio.h>
#include "BLF_api.h"
#include "BLI_path_util.h"
#include "BKE_appdir.h"
static int blf_load_font_default(const char *filename, const bool unique)
{
const char *dir = BKE_appdir_folder_id(BLENDER_DATAFILES, "fonts");
if (dir == NULL) {
fprintf(stderr,
"%s: 'fonts' data path not found for '%s', will not be able to display text\n",
__func__,
filename);
return -1;
}
char filepath[FILE_MAX];
BLI_join_dirfile(filepath, sizeof(filepath), dir, filename);
return (unique) ? BLF_load_unique(filepath) : BLF_load(filepath);
}
int BLF_load_default(const bool unique)
{
return blf_load_font_default("droidsans.ttf", unique);
}
int BLF_load_mono_default(const bool unique)
{
return blf_load_font_default("bmonofont-i18n.ttf", unique);
}

View File

@@ -1,113 +0,0 @@
/*
* 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) 2011 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup blf
*
* API for accessing font files.
*/
#include <stdlib.h>
#include <string.h>
#include "BLF_api.h"
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "BKE_appdir.h"
#ifdef WITH_INTERNATIONAL
# include "BLI_fileops.h"
# include "BLI_string.h"
struct FontBuf {
const char *filename;
uchar *data;
int data_len;
};
static struct FontBuf unifont_ttf = {"droidsans.ttf"};
static struct FontBuf unifont_mono_ttf = {"bmonofont-i18n.ttf"};
static void fontbuf_load(struct FontBuf *fb)
{
const char *fontpath = BKE_appdir_folder_id(BLENDER_DATAFILES, "fonts");
if (fontpath) {
char unifont_path[1024];
BLI_snprintf(unifont_path, sizeof(unifont_path), "%s/%s", fontpath, fb->filename);
fb->data = (uchar *)BLI_file_ungzip_to_mem(unifont_path, &fb->data_len);
}
else {
printf("%s: 'fonts' data path not found for '%s', continuing\n", __func__, fb->filename);
}
}
static void fontbuf_free(struct FontBuf *fb)
{
MEM_SAFE_FREE(fb->data);
fb->data_len = 0;
}
static uchar *fontbuf_get_mem(struct FontBuf *fb, int *r_size)
{
if (fb->data == NULL) {
fontbuf_load(fb);
}
*r_size = fb->data_len;
return fb->data;
}
#endif /* WITH_INTERNATIONAL */
uchar *BLF_get_unifont(int *r_unifont_size)
{
#ifdef WITH_INTERNATIONAL
return fontbuf_get_mem(&unifont_ttf, r_unifont_size);
#else
UNUSED_VARS(r_unifont_size);
return NULL;
#endif
}
uchar *BLF_get_unifont_mono(int *r_unifont_size)
{
#ifdef WITH_INTERNATIONAL
return fontbuf_get_mem(&unifont_mono_ttf, r_unifont_size);
#else
UNUSED_VARS(r_unifont_size);
return NULL;
#endif
}
void BLF_free_unifont(void)
{
#ifdef WITH_INTERNATIONAL
fontbuf_free(&unifont_ttf);
#endif
}
void BLF_free_unifont_mono(void)
{
#ifdef WITH_INTERNATIONAL
fontbuf_free(&unifont_mono_ttf);
#endif
}

View File

@@ -766,8 +766,6 @@ set_property(GLOBAL PROPERTY ICON_GEOM_NAMES
)
data_to_c_simple(../../../../release/datafiles/bfont.pfb SRC)
data_to_c_simple(../../../../release/datafiles/bfont.ttf SRC)
data_to_c_simple(../../../../release/datafiles/bmonofont.ttf SRC)
if(WITH_BLENDER)
# blender only (not player)

View File

@@ -60,12 +60,6 @@ extern char datatoc_splash_2x_png[];
extern int datatoc_bfont_pfb_size;
extern char datatoc_bfont_pfb[];
extern int datatoc_bfont_ttf_size;
extern char datatoc_bfont_ttf[];
extern int datatoc_bmonofont_ttf_size;
extern char datatoc_bmonofont_ttf[];
/* Brush icon datafiles */
/* TODO: this could be simplified by putting all
* the brush icons in one file */

View File

@@ -422,8 +422,6 @@ void uiStyleInit(void)
{
uiFont *font;
uiStyle *style = U.uistyles.first;
int monofont_size = datatoc_bmonofont_ttf_size;
uchar *monofont_ttf = (uchar *)datatoc_bmonofont_ttf;
/* recover from uninitialized dpi */
if (U.dpi == 0) {
@@ -463,37 +461,15 @@ void uiStyleInit(void)
}
for (font = U.uifonts.first; font; font = font->next) {
const bool unique = false;
if (font->uifont_id == UIFONT_DEFAULT) {
#ifdef WITH_INTERNATIONAL
int font_size = datatoc_bfont_ttf_size;
uchar *font_ttf = (uchar *)datatoc_bfont_ttf;
static int last_font_size = 0;
/* use unicode font if available */
font_ttf = BLF_get_unifont(&font_size);
if (!font_ttf) {
/* fall back if not found */
font_size = datatoc_bfont_ttf_size;
font_ttf = (uchar *)datatoc_bfont_ttf;
}
/* relload only if needed */
if (last_font_size != font_size) {
BLF_unload("default");
last_font_size = font_size;
}
font->blf_id = BLF_load_mem("default", font_ttf, font_size);
#else
font->blf_id = BLF_load_mem("default", (uchar *)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
#endif
font->blf_id = BLF_load_default(unique);
}
else {
font->blf_id = BLF_load(font->filename);
if (font->blf_id == -1) {
font->blf_id = BLF_load_mem("default", (uchar *)datatoc_bfont_ttf, datatoc_bfont_ttf_size);
font->blf_id = BLF_load_default(unique);
}
}
@@ -519,25 +495,17 @@ void uiStyleInit(void)
ui_style_new(&U.uistyles, "Default Style", UIFONT_DEFAULT);
}
#ifdef WITH_INTERNATIONAL
/* use unicode font for text editor and interactive console */
monofont_ttf = BLF_get_unifont_mono(&monofont_size);
if (!monofont_ttf) {
/* fall back if not found */
monofont_size = datatoc_bmonofont_ttf_size;
monofont_ttf = (uchar *)datatoc_bmonofont_ttf;
}
#endif
/* XXX, this should be moved into a style,
* but for now best only load the monospaced font once. */
BLI_assert(blf_mono_font == -1);
/* Use unique font loading to avoid thread safety issues with mono font
* used for render metadata stamp in threads. */
if (U.font_path_ui_mono[0]) {
blf_mono_font = BLF_load_unique(U.font_path_ui_mono);
}
if (blf_mono_font == -1) {
blf_mono_font = BLF_load_mem_unique("monospace", monofont_ttf, monofont_size);
const bool unique = true;
blf_mono_font = BLF_load_mono_default(unique);
}
BLF_size(blf_mono_font, 12 * U.pixelsize, 72);
@@ -580,7 +548,8 @@ void uiStyleInit(void)
* keep for now though, since without this there is no way to display many unicode chars.
*/
if (blf_mono_font_render == -1) {
blf_mono_font_render = BLF_load_mem_unique("monospace", monofont_ttf, monofont_size);
const bool unique = true;
blf_mono_font_render = BLF_load_mono_default(unique);
}
BLF_size(blf_mono_font_render, 12 * U.pixelsize, 72);

View File

@@ -609,8 +609,6 @@ void WM_exit_ex(bContext *C, const bool do_python)
}
#ifdef WITH_INTERNATIONAL
BLF_free_unifont();
BLF_free_unifont_mono();
BLT_lang_free();
#endif

View File

@@ -1292,8 +1292,7 @@ static char *wm_main_playanim_intern(int argc, const char **argv)
/* initialize the font */
BLF_init();
ps.fontid = BLF_load_mem(
"monospace", (unsigned char *)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
ps.fontid = BLF_load_mono_default(false);
BLF_size(ps.fontid, 11, 72);
ps.ibufx = ibuf->x;