Welcome to mirror list, hosted at ThFree Co, Russian Federation.

blf_dir.c « intern « blenfont « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8534a8c583fc1a850369f98b51e40d7b517d3bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2009 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup blf
 *
 * Manage search paths for font files.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ft2build.h>

#include FT_FREETYPE_H
#include FT_GLYPH_H

#include "MEM_guardedalloc.h"

#include "DNA_vec_types.h"

#include "BLI_fileops.h"
#include "BLI_listbase.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"

#include "BLF_api.h"
#include "blf_internal.h"
#include "blf_internal_types.h"

static ListBase global_font_dir = {NULL, NULL};

static DirBLF *blf_dir_find(const char *path)
{
  DirBLF *p;

  p = global_font_dir.first;
  while (p) {
    if (BLI_path_cmp(p->path, path) == 0) {
      return p;
    }
    p = p->next;
  }
  return NULL;
}

void BLF_dir_add(const char *path)
{
  DirBLF *dir;

  dir = blf_dir_find(path);
  if (dir) { /* already in the list ? just return. */
    return;
  }

  dir = (DirBLF *)MEM_callocN(sizeof(DirBLF), "BLF_dir_add");
  dir->path = BLI_strdup(path);
  BLI_addhead(&global_font_dir, dir);
}

void BLF_dir_rem(const char *path)
{
  DirBLF *dir;

  dir = blf_dir_find(path);
  if (dir) {
    BLI_remlink(&global_font_dir, dir);
    MEM_freeN(dir->path);
    MEM_freeN(dir);
  }
}

char **BLF_dir_get(int *ndir)
{
  DirBLF *p;
  char **dirs;
  char *path;
  int i, count;

  count = BLI_listbase_count(&global_font_dir);
  if (!count) {
    return NULL;
  }

  dirs = (char **)MEM_callocN(sizeof(char *) * count, "BLF_dir_get");
  p = global_font_dir.first;
  i = 0;
  while (p) {
    path = BLI_strdup(p->path);
    dirs[i] = path;
    p = p->next;
  }
  *ndir = i;
  return dirs;
}

void BLF_dir_free(char **dirs, int count)
{
  for (int i = 0; i < count; i++) {
    char *path = dirs[i];
    MEM_freeN(path);
  }
  MEM_freeN(dirs);
}

char *blf_dir_search(const char *file)
{
  BLI_assert_msg(!BLI_path_is_rel(file), "Relative paths must always be expanded!");

  DirBLF *dir;
  char full_path[FILE_MAX];
  char *s = NULL;

  for (dir = global_font_dir.first; dir; dir = dir->next) {
    BLI_join_dirfile(full_path, sizeof(full_path), dir->path, file);
    if (BLI_exists(full_path)) {
      s = BLI_strdup(full_path);
      break;
    }
  }

  if (!s) {
    /* This may be an absolute path which exists. */
    if (BLI_exists(file)) {
      s = BLI_strdup(file);
    }
  }

  return s;
}

char *blf_dir_metrics_search(const char *filepath)
{
  char *mfile;
  char *s;

  mfile = BLI_strdup(filepath);
  s = strrchr(mfile, '.');
  if (s) {
    if (BLI_strnlen(s, 4) < 4) {
      MEM_freeN(mfile);
      return NULL;
    }
    s++;
    s[0] = 'a';
    s[1] = 'f';
    s[2] = 'm';

    /* First check `.afm`. */
    if (BLI_exists(mfile)) {
      return mfile;
    }

    /* And now check `.pfm`. */
    s[0] = 'p';

    if (BLI_exists(mfile)) {
      return mfile;
    }
  }
  MEM_freeN(mfile);
  return NULL;
}