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

blf_lang.c « intern « blenfont « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0aaf399cf586a7708e8d0709bffbd518e31ac1e3 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2008 Blender Foundation.
 * All rights reserved.
 * 
 * Contributor(s): Blender Foundation.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

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

#include "MEM_guardedalloc.h"

#include "DNA_listBase.h"

#include "BKE_utildefines.h"

#include "BLI_blenlib.h"
#include "BLI_linklist.h"	/* linknode */
#include "BLI_string.h"

#include "blf_internal_types.h"

// XXX 2.50 Remove this later.
#ifdef WITH_FREETYPE2
#include "FTF_Api.h"
#endif

static ListBase global_lang= { NULL, NULL };
static int global_tot_lang= 0;
static int global_err_lang= 0;

int BLF_lang_error(void)
{
	return(global_err_lang);
}

char *BLF_lang_pup(void)
{
	LangBLF *lme;
	static char string[1024];
	static char tmp[1024];

	if(global_tot_lang == 0)
		sprintf(string, "Choose Language: %%t|Language:  English %%x0");
	else {
		lme= global_lang.first;
		sprintf(string, "Choose Language: %%t");
		while (lme) {
			sprintf(tmp, "|Language:  %s %%x%d", lme->language, lme->id);
			strcat(string, tmp);
			lme= lme->next;
		}
	}

	return(string);
}

LangBLF *blf_lang_find_by_id(short langid)
{
	LangBLF *p;

	p= global_lang.first;
	while (p) {
		if (p->id == langid)
			return(p);
		p= p->next;
	}
	return(NULL);
}

char *BLF_lang_find_code(short langid)
{
	LangBLF *p;

	p= blf_lang_find_by_id(langid);
	if (p)
		return(p->code);
	return(NULL);
}

void BLF_lang_set(int id)
{
#ifdef WITH_FREETYPE2
	LangBLF *lme;

	// XXX 2.50 Remove this later, with ftfont
	lme= blf_lang_find_by_id(id);
	if(lme) FTF_SetLanguage(lme->code);
	else FTF_SetLanguage("en_US");
#endif
}

static void blf_lang_split(char *line, LangBLF* lme)
{
	char *dpointchar= strchr(line, ':');

	if (dpointchar) {
		lme->code= BLI_strdup(dpointchar+1);
		*(dpointchar)=0;
		lme->language= BLI_strdup(line);
	} else {
		lme->code= NULL;
		lme->language= NULL;
		/* XXX 2.50 bad call error("Invalid language file");
		 * If we set this to NULL, the function blf_lang_new
		 * drop the line and increment the error lang value
		 * so the init code can call BLF_lang_error to get
		 * the number of invalid lines and show the error.
		 */
	}
}

LangBLF *blf_lang_find(char *s, int find_by)
{
	LangBLF *p;

	p= global_lang.first;
	while (p) {
		if (find_by == BLF_LANG_FIND_BY_LINE) {
			if (BLI_streq(s, p->line))
				return(p);
		}
		else if (find_by == BLF_LANG_FIND_BY_CODE) {
			if (BLI_streq(s, p->code))
				return(p);
		}
		else if (find_by == BLF_LANG_FIND_BY_LANGUAGE) {
			if (BLI_streq(s, p->language))
				return(p);
		}
		p= p->next;
	}
	return(NULL);
}

static void blf_lang_new(char *line)
{
	LangBLF *lme;

	lme= blf_lang_find(line, BLF_LANG_FIND_BY_LINE);
	if (!lme) {
		lme= MEM_mallocN(sizeof(LangBLF), "blf_lang_new");
		lme->next= NULL;
		lme->prev= NULL;
		lme->line = BLI_strdup(line);
		blf_lang_split(line, lme);
		
		if (lme->code && lme->language) {
			lme->id = global_tot_lang;
			global_tot_lang++;
			BLI_addhead(&global_lang, lme);
		}
		else {
			global_err_lang++;
			MEM_freeN(lme->line);
			MEM_freeN(lme);
		}
	}
}

int BLF_lang_init(void) 
{
	char name[FILE_MAXDIR+FILE_MAXFILE];
	LinkNode *l, *lines;
	
	/* .Blanguages, http://www.blender3d.org/cms/Installation_Policy.352.0.html*/
#if defined (__APPLE__) || (WIN32)
	BLI_make_file_string("/", name, BLI_gethome(), ".Blanguages");
#else
	BLI_make_file_string("/", name, BLI_gethome(), ".blender/.Blanguages");
#endif

	lines= BLI_read_file_as_lines(name);

	if(lines == NULL) {
		/* If not found in home, try current dir 
		 * (Resources folder of app bundle on OS X) */
#if defined (__APPLE__)
		char *bundlePath = BLI_getbundle();
		strcpy(name, bundlePath);
		strcat(name, "/Contents/Resources/.Blanguages");
#else
		/* Check the CWD. Takes care of the case where users
		 * unpack blender tarball; cd blender-dir; ./blender */
		strcpy(name, ".blender/.Blanguages");
#endif
		lines= BLI_read_file_as_lines(name);

		if(lines == NULL) {
			/* If not found in .blender, try current dir */
			strcpy(name, ".Blanguages");
			lines= BLI_read_file_as_lines(name);
			if(lines == NULL) {
// XXX 2.50				if(G.f & G_DEBUG)
				printf("File .Blanguages not found\n");
				return(0);
			}
		}
	}

	for (l= lines; l; l= l->next) {
		char *line= l->link;
			
		if (!BLI_streq(line, "")) {
			blf_lang_new(line);
		}
	}

	BLI_free_file_lines(lines);
	return(1);
}

void BLF_lang_exit(void)
{
	LangBLF *p;

	while (global_lang.first) {
		p= global_lang.first;
		BLI_remlink(&global_lang, p);
		MEM_freeN(p->line);
		MEM_freeN(p->language);
		MEM_freeN(p->code);
		MEM_freeN(p);
	}
}