Blender  V3.3
blf_dir.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2009 Blender Foundation. All rights reserved. */
3 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include <ft2build.h>
15 
16 #include FT_FREETYPE_H
17 #include FT_GLYPH_H
18 
19 #include "MEM_guardedalloc.h"
20 
21 #include "DNA_vec_types.h"
22 
23 #include "BLI_fileops.h"
24 #include "BLI_listbase.h"
25 #include "BLI_path_util.h"
26 #include "BLI_string.h"
27 #include "BLI_threads.h"
28 #include "BLI_utildefines.h"
29 
30 #include "BLF_api.h"
31 #include "blf_internal.h"
32 #include "blf_internal_types.h"
33 
35 
36 static DirBLF *blf_dir_find(const char *path)
37 {
38  DirBLF *p;
39 
41  while (p) {
42  if (BLI_path_cmp(p->path, path) == 0) {
43  return p;
44  }
45  p = p->next;
46  }
47  return NULL;
48 }
49 
50 void BLF_dir_add(const char *path)
51 {
52  DirBLF *dir;
53 
54  dir = blf_dir_find(path);
55  if (dir) { /* already in the list ? just return. */
56  return;
57  }
58 
59  dir = (DirBLF *)MEM_callocN(sizeof(DirBLF), "BLF_dir_add");
60  dir->path = BLI_strdup(path);
62 }
63 
64 void BLF_dir_rem(const char *path)
65 {
66  DirBLF *dir;
67 
68  dir = blf_dir_find(path);
69  if (dir) {
71  MEM_freeN(dir->path);
72  MEM_freeN(dir);
73  }
74 }
75 
76 char **BLF_dir_get(int *ndir)
77 {
78  DirBLF *p;
79  char **dirs;
80  char *path;
81  int i, count;
82 
84  if (!count) {
85  return NULL;
86  }
87 
88  dirs = (char **)MEM_callocN(sizeof(char *) * count, "BLF_dir_get");
90  i = 0;
91  while (p) {
92  path = BLI_strdup(p->path);
93  dirs[i] = path;
94  p = p->next;
95  }
96  *ndir = i;
97  return dirs;
98 }
99 
100 void BLF_dir_free(char **dirs, int count)
101 {
102  for (int i = 0; i < count; i++) {
103  char *path = dirs[i];
104  MEM_freeN(path);
105  }
106  MEM_freeN(dirs);
107 }
108 
109 char *blf_dir_search(const char *file)
110 {
111  BLI_assert_msg(!BLI_path_is_rel(file), "Relative paths must always be expanded!");
112 
113  DirBLF *dir;
114  char full_path[FILE_MAX];
115  char *s = NULL;
116 
117  for (dir = global_font_dir.first; dir; dir = dir->next) {
118  BLI_join_dirfile(full_path, sizeof(full_path), dir->path, file);
119  if (BLI_exists(full_path)) {
120  s = BLI_strdup(full_path);
121  break;
122  }
123  }
124 
125  if (!s) {
126  /* This may be an absolute path which exists. */
127  if (BLI_exists(file)) {
128  s = BLI_strdup(file);
129  }
130  }
131 
132  return s;
133 }
134 
135 char *blf_dir_metrics_search(const char *filepath)
136 {
137  char *mfile;
138  char *s;
139 
140  mfile = BLI_strdup(filepath);
141  s = strrchr(mfile, '.');
142  if (s) {
143  if (BLI_strnlen(s, 4) < 4) {
144  MEM_freeN(mfile);
145  return NULL;
146  }
147  s++;
148  s[0] = 'a';
149  s[1] = 'f';
150  s[2] = 'm';
151 
152  /* First check `.afm`. */
153  if (BLI_exists(mfile)) {
154  return mfile;
155  }
156 
157  /* And now check `.pfm`. */
158  s[0] = 'p';
159 
160  if (BLI_exists(mfile)) {
161  return mfile;
162  }
163  }
164  MEM_freeN(mfile);
165  return NULL;
166 }
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
File and directory operations.
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:314
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:60
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:347
#define FILE_MAX
#define BLI_path_cmp
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
size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:899
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:42
Read Guarded memory(de)allocation.
void BLF_dir_rem(const char *path)
Definition: blf_dir.c:64
void BLF_dir_free(char **dirs, int count)
Definition: blf_dir.c:100
static DirBLF * blf_dir_find(const char *path)
Definition: blf_dir.c:36
char * blf_dir_metrics_search(const char *filepath)
Definition: blf_dir.c:135
char ** BLF_dir_get(int *ndir)
Definition: blf_dir.c:76
char * blf_dir_search(const char *file)
Definition: blf_dir.c:109
void BLF_dir_add(const char *path)
Definition: blf_dir.c:50
static ListBase global_font_dir
Definition: blf_dir.c:34
FILE * file
int count
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
struct DirBLF * next
void * first
Definition: DNA_listBase.h:31