Blender  V2.93
blf_dir.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <ft2build.h>
31 
32 #include FT_FREETYPE_H
33 #include FT_GLYPH_H
34 
35 #include "MEM_guardedalloc.h"
36 
37 #include "DNA_vec_types.h"
38 
39 #include "BLI_fileops.h"
40 #include "BLI_listbase.h"
41 #include "BLI_path_util.h"
42 #include "BLI_string.h"
43 #include "BLI_threads.h"
44 #include "BLI_utildefines.h"
45 
46 #include "BLF_api.h"
47 #include "blf_internal.h"
48 #include "blf_internal_types.h"
49 
50 #include "BKE_global.h"
51 #include "BKE_main.h"
52 
54 
55 static DirBLF *blf_dir_find(const char *path)
56 {
57  DirBLF *p;
58 
60  while (p) {
61  if (BLI_path_cmp(p->path, path) == 0) {
62  return p;
63  }
64  p = p->next;
65  }
66  return NULL;
67 }
68 
69 void BLF_dir_add(const char *path)
70 {
71  DirBLF *dir;
72 
73  dir = blf_dir_find(path);
74  if (dir) { /* already in the list ? just return. */
75  return;
76  }
77 
78  dir = (DirBLF *)MEM_callocN(sizeof(DirBLF), "BLF_dir_add");
79  dir->path = BLI_strdup(path);
81 }
82 
83 void BLF_dir_rem(const char *path)
84 {
85  DirBLF *dir;
86 
87  dir = blf_dir_find(path);
88  if (dir) {
90  MEM_freeN(dir->path);
91  MEM_freeN(dir);
92  }
93 }
94 
95 char **BLF_dir_get(int *ndir)
96 {
97  DirBLF *p;
98  char **dirs;
99  char *path;
100  int i, count;
101 
103  if (!count) {
104  return NULL;
105  }
106 
107  dirs = (char **)MEM_callocN(sizeof(char *) * count, "BLF_dir_get");
109  i = 0;
110  while (p) {
111  path = BLI_strdup(p->path);
112  dirs[i] = path;
113  p = p->next;
114  }
115  *ndir = i;
116  return dirs;
117 }
118 
119 void BLF_dir_free(char **dirs, int count)
120 {
121  for (int i = 0; i < count; i++) {
122  char *path = dirs[i];
123  MEM_freeN(path);
124  }
125  MEM_freeN(dirs);
126 }
127 
128 char *blf_dir_search(const char *file)
129 {
130  DirBLF *dir;
131  char full_path[FILE_MAX];
132  char *s = NULL;
133 
134  for (dir = global_font_dir.first; dir; dir = dir->next) {
135  BLI_join_dirfile(full_path, sizeof(full_path), dir->path, file);
136  if (BLI_exists(full_path)) {
137  s = BLI_strdup(full_path);
138  break;
139  }
140  }
141 
142  if (!s) {
143  /* Assume file is either an absolute path, or a relative path to current directory. */
144  BLI_strncpy(full_path, file, sizeof(full_path));
146  if (BLI_exists(full_path)) {
147  s = BLI_strdup(full_path);
148  }
149  }
150 
151  return s;
152 }
153 
158 char *blf_dir_metrics_search(const char *filename)
159 {
160  char *mfile;
161  char *s;
162 
163  mfile = BLI_strdup(filename);
164  s = strrchr(mfile, '.');
165  if (s) {
166  if (BLI_strnlen(s, 4) < 4) {
167  MEM_freeN(mfile);
168  return NULL;
169  }
170  s++;
171  s[0] = 'a';
172  s[1] = 'f';
173  s[2] = 'm';
174 
175  /* first check .afm */
176  if (BLI_exists(mfile)) {
177  return mfile;
178  }
179 
180  /* and now check .pfm */
181  s[0] = 'p';
182 
183  if (BLI_exists(mfile)) {
184  return mfile;
185  }
186  }
187  MEM_freeN(mfile);
188  return NULL;
189 }
#define G_MAIN
Definition: BKE_global.h:232
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:349
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:87
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define FILE_MAX
void BLI_join_dirfile(char *__restrict dst, const size_t maxlen, const char *__restrict dir, const char *__restrict file) ATTR_NONNULL()
Definition: path_util.c:1737
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:1016
#define BLI_path_cmp
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
size_t BLI_strnlen(const char *str, const size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:878
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
Read Guarded memory(de)allocation.
void BLF_dir_rem(const char *path)
Definition: blf_dir.c:83
void BLF_dir_free(char **dirs, int count)
Definition: blf_dir.c:119
static DirBLF * blf_dir_find(const char *path)
Definition: blf_dir.c:55
char ** BLF_dir_get(int *ndir)
Definition: blf_dir.c:95
char * blf_dir_search(const char *file)
Definition: blf_dir.c:128
void BLF_dir_add(const char *path)
Definition: blf_dir.c:69
static ListBase global_font_dir
Definition: blf_dir.c:53
char * blf_dir_metrics_search(const char *filename)
Definition: blf_dir.c:158
FILE * file
int count
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
struct DirBLF * next
void * first
Definition: DNA_listBase.h:47