Blender V2.61 - r43446

blf_dir.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2009 Blender Foundation.
00019  * All rights reserved.
00020  * 
00021  * Contributor(s): Blender Foundation.
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 
00035 #include <ft2build.h>
00036 
00037 #include FT_FREETYPE_H
00038 #include FT_GLYPH_H
00039 
00040 #include "MEM_guardedalloc.h"
00041 
00042 #include "DNA_vec_types.h"
00043 
00044 #include "BKE_utildefines.h"
00045 
00046 #include "BLI_blenlib.h"
00047 
00048 #include "BIF_gl.h"
00049 
00050 #include "BLF_api.h"
00051 #include "blf_internal_types.h"
00052 #include "blf_internal.h"
00053 
00054 static ListBase global_font_dir= { NULL, NULL };
00055 
00056 static DirBLF *blf_dir_find(const char *path)
00057 {
00058     DirBLF *p;
00059     
00060     p= global_font_dir.first;
00061     while (p) {
00062         if (BLI_path_cmp(p->path, path) == 0)
00063             return p;
00064         p= p->next;
00065     }
00066     return NULL;
00067 }
00068 
00069 void BLF_dir_add(const char *path)
00070 {
00071     DirBLF *dir;
00072     
00073     dir= blf_dir_find(path);
00074     if (dir) /* already in the list ? just return. */
00075         return;
00076     
00077     dir= (DirBLF *)MEM_callocN(sizeof(DirBLF), "BLF_dir_add");
00078     dir->path= BLI_strdup(path);
00079     BLI_addhead(&global_font_dir, dir);
00080 }
00081 
00082 void BLF_dir_rem(const char *path)
00083 {
00084     DirBLF *dir;
00085     
00086     dir= blf_dir_find(path);
00087     if (dir) {
00088         BLI_remlink(&global_font_dir, dir);
00089         MEM_freeN(dir->path);
00090         MEM_freeN(dir);
00091     }
00092 }
00093 
00094 char **BLF_dir_get(int *ndir)
00095 {
00096     DirBLF *p;
00097     char **dirs;
00098     char *path;
00099     int i, count;
00100     
00101     count= BLI_countlist(&global_font_dir);
00102     if (!count)
00103         return NULL;
00104     
00105     dirs= (char **)MEM_callocN(sizeof(char *) * count, "BLF_dir_get");
00106     p= global_font_dir.first;
00107     i= 0;
00108     while (p) {
00109         path= BLI_strdup(p->path);
00110         dirs[i]= path;
00111         p= p->next;
00112     }
00113     *ndir= i;
00114     return dirs;
00115 }
00116 
00117 void BLF_dir_free(char **dirs, int count)
00118 {
00119     char *path;
00120     int i;
00121     
00122     for (i= 0; i < count; i++) {
00123         path= dirs[i];
00124         MEM_freeN(path);
00125     }
00126     MEM_freeN(dirs);
00127 }
00128 
00129 char *blf_dir_search(const char *file)
00130 {
00131     DirBLF *dir;
00132     char full_path[FILE_MAX];
00133     char *s= NULL;
00134 
00135     for(dir=global_font_dir.first; dir; dir= dir->next) {
00136         BLI_join_dirfile(full_path, sizeof(full_path), dir->path, file);
00137         if (BLI_exists(full_path)) {
00138             s= BLI_strdup(full_path);
00139             break;
00140         }
00141     }
00142 
00143     if (!s) {
00144         /* check the current directory, why not ? */
00145         if (BLI_exists(file))
00146             s= BLI_strdup(file);
00147     }
00148 
00149     return s;
00150 }
00151 
00152 #if 0 // UNUSED
00153 int blf_dir_split(const char *str, char *file, int *size)
00154 {
00155     int i, len;
00156     char *s;
00157     
00158     /* Window, Linux or Mac, this is always / */
00159     s= strrchr(str, '/');
00160     if (s) {
00161         len= s - str;
00162         for (i= 0; i < len; i++)
00163             file[i]= str[i];
00164 
00165         file[i]= '.';
00166         file[i+1]= 't';
00167         file[i+2]= 't';
00168         file[i+3]= 'f';
00169         file[i+4]= '\0';
00170         s++;
00171         *size= atoi(s);
00172         return 1;
00173     }
00174     return 0;
00175 }
00176 #endif
00177 
00178 /* Some font have additional file with metrics information,
00179  * in general, the extension of the file is: .afm or .pfm
00180  */
00181 char *blf_dir_metrics_search(const char *filename)
00182 {
00183     char *mfile;
00184     char *s;
00185 
00186     mfile= BLI_strdup(filename);
00187     s= strrchr(mfile, '.');
00188     if (s) {
00189         if (BLI_strnlen(s, 4) < 4) {
00190             MEM_freeN(mfile);
00191             return NULL;
00192         }
00193         s++;
00194         s[0]= 'a';
00195         s[1]= 'f';
00196         s[2]= 'm';
00197 
00198         /* first check .afm */
00199         if (BLI_exists(s))
00200             return s;
00201 
00202         /* and now check .pfm */
00203         s[0]= 'p';
00204 
00205         if (BLI_exists(s))
00206             return s;
00207     }
00208     MEM_freeN(mfile);
00209     return NULL;
00210 }