Blender  V3.3
blf_font_default.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. All rights reserved. */
3 
10 #include <stdio.h>
11 
12 #include "BLF_api.h"
13 
14 #include "BLI_fileops.h"
15 #include "BLI_path_util.h"
16 
17 #include "BKE_appdir.h"
18 
19 static int blf_load_font_default(const char *filename, const bool unique)
20 {
22  if (dir == NULL) {
23  fprintf(stderr,
24  "%s: 'fonts' data path not found for '%s', will not be able to display text\n",
25  __func__,
26  filename);
27  return -1;
28  }
29 
30  char filepath[FILE_MAX];
31  BLI_join_dirfile(filepath, sizeof(filepath), dir, filename);
32 
33  return (unique) ? BLF_load_unique(filepath) : BLF_load(filepath);
34 }
35 
36 int BLF_load_default(const bool unique)
37 {
39  BLF_enable(font_id, BLF_DEFAULT);
40  return font_id;
41 }
42 
43 int BLF_load_mono_default(const bool unique)
44 {
47  return font_id;
48 }
49 
51 {
52  /* Load these if not already, might have been replaced by user custom. */
53  BLF_load_default(false);
54  BLF_load_mono_default(false);
55 
56  const char *datafiles_fonts_dir = BLF_DATAFILES_FONTS_DIR SEP_STR;
57  const char *path = BKE_appdir_folder_id(BLENDER_DATAFILES, datafiles_fonts_dir);
58  if (UNLIKELY(!path)) {
59  fprintf(stderr, "Font data directory \"%s\" could not be detected!\n", datafiles_fonts_dir);
60  }
61  else if (UNLIKELY(!BLI_exists(path))) {
62  fprintf(stderr, "Font data directory \"%s\" does not exist!\n", path);
63  }
64  else {
65  struct direntry *dir;
66  uint num_files = BLI_filelist_dir_contents(path, &dir);
67  for (int f = 0; f < num_files; f++) {
68  if (!FILENAME_IS_CURRPAR(dir[f].relname) && !BLI_is_dir(dir[f].path)) {
69  if (!BLF_is_loaded(dir[f].path)) {
70  int font_id = BLF_load(dir[f].path);
71  if (font_id == -1) {
72  fprintf(stderr, "Unable to load font: %s\n", dir[f].path);
73  }
74  else {
75  BLF_enable(font_id, BLF_DEFAULT);
76  /* TODO: FontBLF will later load FT_Face on demand. When this is in
77  * place we can drop this face now since we have all needed data. */
78  }
79  }
80  }
81  }
82  BLI_filelist_free(dir, num_files);
83  }
84 }
const char * BKE_appdir_folder_id(int folder_id, const char *subfolder)
Definition: appdir.c:672
@ BLENDER_DATAFILES
Definition: BKE_appdir.h:154
@ BLF_MONOSPACED
Definition: BLF_api.h:349
@ BLF_DEFAULT
Definition: BLF_api.h:351
#define BLF_DEFAULT_PROPORTIONAL_FONT
Definition: BLF_api.h:21
bool BLF_is_loaded(const char *name) ATTR_NONNULL()
Definition: blf.c:130
#define BLF_DATAFILES_FONTS_DIR
Definition: BLF_api.h:18
int BLF_load_unique(const char *name) ATTR_NONNULL()
Definition: blf.c:148
void BLF_enable(int fontid, int option)
Definition: blf.c:270
int BLF_load(const char *name) ATTR_NONNULL()
Definition: blf.c:135
#define BLF_DEFAULT_MONOSPACED_FONT
Definition: BLF_api.h:24
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:314
unsigned int BLI_filelist_dir_contents(const char *dir, struct direntry **r_filelist)
Definition: BLI_filelist.c:218
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:397
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
Definition: BLI_filelist.c:420
#define FILE_MAX
#define FILENAME_IS_CURRPAR(_n)
void BLI_join_dirfile(char *__restrict dst, size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL()
Definition: path_util.c:1531
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNLIKELY(x)
static int blf_load_font_default(const char *filename, const bool unique)
int BLF_load_mono_default(const bool unique)
void BLF_load_font_stack()
int BLF_load_default(const bool unique)
const char * relname
const char * path
#define SEP_STR
Definition: unit.c:33