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

blf.c « intern « blenfont « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e43d7905cf91e3997e36bf0870ba210b9019335e (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
 * $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) 2009 Blender Foundation.
 * All rights reserved.
 *
 * 
 * Contributor(s): Blender Foundation
 *
 * ***** END GPL LICENSE BLOCK *****
 */

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

#ifdef WITH_FREETYPE2

#include <ft2build.h>

#include FT_FREETYPE_H
#include FT_GLYPH_H

#endif /* WITH_FREETYPE2 */

#include "MEM_guardedalloc.h"

#include "DNA_listBase.h"
#include "DNA_vec_types.h"

#include "BKE_utildefines.h"

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

#include "BIF_gl.h"
#include "BIF_glutil.h"
#include "BLF_api.h"

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


/* Max number of font in memory.
 * Take care that now every font have a glyph cache per size/dpi,
 * so we don't need load the same font with different size, just
 * load one and call BLF_size.
 */
#define BLF_MAX_FONT 16

/* Font array. */
FontBLF *global_font[BLF_MAX_FONT];

/* Number of font. */
int global_font_num= 0;

/* Current font. */
int global_font_cur= 0;


int BLF_init(void)
{
	int i;

	for (i= 0; i < BLF_MAX_FONT; i++)
		global_font[i]= NULL;

	return(blf_font_init());
}

void BLF_exit(void)
{
	FontBLF *font;
	int i;

	for (i= 0; i < global_font_num; i++) {
		font= global_font[i];
		if(font && font->free)
			(*font->free)(font);
	}

	blf_font_exit();
}

int blf_search(char *name)
{
	FontBLF *font;
	int i;

	for (i= 0; i < BLF_MAX_FONT; i++) {
		font= global_font[i];
		if (font && (!strcmp(font->name, name)))
			return(i);
	}
	return(-1);
}

int BLF_load(char *name)
{
	FontBLF *font;
	char *filename;
	int i;

	if (!name)
		return(-1);

	/* check if we already load this font. */
	i= blf_search(name);
	if (i >= 0) {
		font= global_font[i];
		font->ref++;
		printf("Increment reference (%d): %s\n", font->ref, name);
		return(i);
	}

	if (global_font_num+1 >= BLF_MAX_FONT) {
		printf("Too many fonts!!!\n");
		return(-1);
	}

	filename= blf_dir_search(name);
	if (!filename) {
		printf("Can't found font: %s\n", name);
		return(-1);
	}

#ifdef WITH_FREETYPE2
	font= blf_font_new(name, filename);
	MEM_freeN(filename);

	if (!font) {
		printf("Can't load font: %s\n", name);
		return(-1);
	}

	global_font[global_font_num]= font;
	i= global_font_num;
	global_font_num++;
	return(i);
#endif /* WITH_FREETYPE2 */

	return(-1);
}

int BLF_load_mem(char *name, unsigned char *mem, int mem_size)
{
	FontBLF *font;
	int i;

	if (!name)
		return(-1);

	i= blf_search(name);
	if (i >= 0) {
		font= global_font[i];
		font->ref++;
		printf("Increment reference (%d): %s\n", font->ref, name);
		return(i);
	}

	if (global_font_num+1 >= BLF_MAX_FONT) {
		printf("Too many fonts!!!\n");
		return(-1);
	}

	font= blf_internal_new(name);
	if (!font) {
#ifdef WITH_FREETYPE2
		if (!mem || !mem_size) {
			printf("Can't load font: %s from memory!!\n", name);
			return(-1);
		}

		font= blf_font_new_from_mem(name, mem, mem_size);
#endif /* WITH_FREETYPE2 */

		if (!font) {
			printf("Can't load font: %s from memory!!\n", name);
			return(-1);
		}
	}

	global_font[global_font_num]= font;
	i= global_font_num;
	global_font_num++;
	return(i);
}

void BLF_set(int fontid)
{
	if (fontid >= 0 && fontid < BLF_MAX_FONT)
		global_font_cur= fontid;
}

int BLF_get(void)
{
	return(global_font_cur);
}

int BLF_type_get(void)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font)
		return(font->type);
	return(-1);
}

void BLF_enable(int option)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font)
		font->flags |= option;
}

void BLF_disable(int option)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font)
		font->flags &= ~option;
}

void BLF_aspect(float aspect)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font)
		font->aspect= aspect;
}

void BLF_position(float x, float y, float z)
{
	FontBLF *font;
	float remainder;

	font= global_font[global_font_cur];
	if (font) {
		remainder= x - floor(x);
		if (remainder > 0.4 && remainder < 0.6) {
			if (remainder < 0.5)
				x -= 0.1 * font->aspect;
			else
				x += 0.1 * font->aspect;
		}

		remainder= y - floor(y);
		if (remainder > 0.4 && remainder < 0.6) {
			if (remainder < 0.5)
				y -= 0.1 * font->aspect;
			else
				y += 0.1 * font->aspect;
		}

		font->pos[0]= x;
		font->pos[1]= y;
		font->pos[2]= z;
	}
}

void BLF_size(int size, int dpi)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font && font->size_set)
		(*font->size_set)(font, size, dpi);
}

void BLF_blur(int size)
{
	FontBLF *font;
	
	font= global_font[global_font_cur];
	if (font)
		font->blur= size;
}

void BLF_draw(char *str)
{
	FontBLF *font;

	/*
	 * The pixmap alignment hack is handle
	 * in BLF_position (old ui_rasterpos_safe).
	 */

	font= global_font[global_font_cur];
	if (font && font->draw) {
		if (font->mode == BLF_MODE_BITMAP) {
			glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
			glPushAttrib(GL_ENABLE_BIT);
			glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
			glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
			glDisable(GL_BLEND);
			glRasterPos3f(font->pos[0], font->pos[1], font->pos[2]);

			(*font->draw)(font, str);

			glPopAttrib();
			glPopClientAttrib();
		}
		else {
			glEnable(GL_BLEND);
			glEnable(GL_TEXTURE_2D);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

			glPushMatrix();
			glTranslatef(font->pos[0], font->pos[1], font->pos[2]);
			glScalef(font->aspect, font->aspect, 1.0);

			if (font->flags & BLF_ROTATION)
				glRotatef(font->angle, 0.0f, 0.0f, 1.0f);

			(*font->draw)(font, str);

			glPopMatrix();
			glDisable(GL_BLEND);
			glDisable(GL_TEXTURE_2D);
		}
	}
}

void BLF_boundbox(char *str, rctf *box)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font && font->boundbox_get)
		(*font->boundbox_get)(font, str, box);
}

float BLF_width(char *str)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font && font->width_get)
		return((*font->width_get)(font, str));
	return(0.0f);
}

float BLF_height(char *str)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font && font->height_get)
		return((*font->height_get)(font, str));
	return(0.0f);
}

void BLF_rotation(float angle)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font)
		font->angle= angle;
}

void BLF_clipping(float xmin, float ymin, float xmax, float ymax)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font) {
		font->clip_rec.xmin= xmin;
		font->clip_rec.ymin= ymin;
		font->clip_rec.xmax= xmax;
		font->clip_rec.ymax= ymax;
	}
}

void BLF_mode(int mode)
{
	FontBLF *font;

	font= global_font[global_font_cur];
	if (font)
		font->mode= mode;
}