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