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

Font.c « api2_2x « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 865285f1e42f36b3a2b9fd50eb8da95313d8727f (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
/*
 * ***** BEGIN GPL/BL DUAL 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. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * 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) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * This is a new part of Blender.
 *
 * Contributor(s): Joilnen Leite
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

#include "BKE_global.h"
#include "BKE_packedFile.h"
#include "DNA_vfont_types.h"
#include "DNA_packedFile_types.h"
#include "Text3d.h"
#include "Font.h"

extern PyObject *M_Text3d_LoadFont( PyObject * self, PyObject * args );

/*--------------------Python API function prototypes for the Font module----*/
PyObject *M_Font_New( PyObject * self, PyObject * args );
static PyObject *M_Font_Get( PyObject * self, PyObject * args );

/*------------------------Python API Doc strings for the Font module--------*/
char M_Font_doc[] = "The Blender Font module\n\n\
This module provides control over **Font Data** objects in Blender.\n\n\
Example::\n\n\
	from Blender import Text3d.Font\n\
	l = Text3d.Font.New()\n";
char M_Font_New_doc[] = "(name) - return a new Font of name 'name'.";
char M_Font_Get_doc[] = "(name) - return a new Font of name 'name'.";

/*----- Python method structure definition for Blender.Text3d.Font module---*/
struct PyMethodDef M_Font_methods[] = {
	{"New", ( PyCFunction ) M_Font_New, METH_VARARGS, M_Font_New_doc},
	{"Get", ( PyCFunction ) M_Font_Get, METH_VARARGS, M_Font_New_doc},
	{NULL, NULL, 0, NULL}
};

/*--------------- Python BPy_Bone methods declarations:-------------------*/
static PyObject *Font_getName( BPy_Font * self );
static PyObject *Font_setName( BPy_Font * self, PyObject * args );
static PyObject *Font_pack( BPy_Font * self, PyObject * args );
static PyObject *Font_isPacked( BPy_Font * self );

/*--------------- Python BPy_Font methods table:--------------------------*/
static PyMethodDef BPy_Font_methods[] = {
	{"GetName", ( PyCFunction ) Font_getName, METH_NOARGS,
	 "() - return Font name"},
	{"getName", ( PyCFunction ) Font_getName, METH_NOARGS,
	 "() - return Font name"},
	{"setName", ( PyCFunction ) Font_setName, METH_VARARGS,
	 "() - return Font name"},
	{"pack", ( PyCFunction ) Font_pack, METH_VARARGS,
	 "() - pack/unpack Font"},
	{"isPacked", ( PyCFunction ) Font_isPacked, METH_NOARGS,
	 "() - pack/unpack Font"},
	{NULL, NULL, 0, NULL}
};

/*--------------- Python TypeBone callback function prototypes----------*/
static void Font_dealloc( BPy_Font * font );
static PyObject *Font_getAttr( BPy_Font * bone, char *name );
static int Font_setAttr( BPy_Font * font, char *name, PyObject * v );
static int Font_compare( BPy_Font * a1, BPy_Font * a2 );
static PyObject *Font_repr( BPy_Font * font );

PyTypeObject Font_Type = {
	PyObject_HEAD_INIT( NULL )
		0,		/* ob_size */
	"Font",		/* tp_name */
	sizeof( BPy_Font ),	/* tp_basicsize */
	0,			/* tp_itemsize */
	/* methods */
	( destructor ) Font_dealloc,	/* tp_dealloc */
	0,			/* tp_print */
	( getattrfunc ) Font_getAttr,	/* tp_getattr */
	( setattrfunc ) Font_setAttr,	/* tp_setattr */
	( cmpfunc ) Font_compare,			/* tp_compare */
	( reprfunc ) Font_repr,	/* tp_repr */
	0,			/* tp_as_number */
	0,			/* tp_as_sequence */
	0,			/* tp_as_mapping */
	0,			/* tp_as_hash */
	0, 0, 0, 0, 0, 0,
	0,			/* tp_doc */
	0, 0, 0, 0, 0, 0,
	BPy_Font_methods,	/* tp_methods */
	0,			/* tp_members */
};

/*--------------- Font Module Init-----------------------------*/
PyObject *Font_Init( void )
{
	PyObject *submodule;

	Font_Type.ob_type = &PyType_Type;

	submodule = Py_InitModule3( "Blender.Text3d.Font",
				    M_Font_methods, M_Font_doc );

	return ( submodule );
}

/*--------------- Bone module internal callbacks-----------------*/
/*---------------BPy_Bone internal callbacks/methods-------------*/

//--------------- dealloc------------------------------------------
static void Font_dealloc( BPy_Font * self )
{
	PyObject_DEL( self );
}

/*---------------getattr-------------------------------------------*/
static PyObject *Font_getAttr( BPy_Font * self, char *name )
{
	return Py_FindMethod( BPy_Font_methods, ( PyObject * ) self, name );
}

/*--------------- setattr-------------------------------------------*/
static int Font_setAttr( BPy_Font * self, char *name, PyObject * value )
{
	return 0;
}

/*--------------- repr---------------------------------------------*/
static PyObject *Font_repr( BPy_Font * self )
{
	if( self->font )
		return PyString_FromFormat( "[Font \"%s\"]",
					    self->font->name );
	else
		return PyString_FromString( "NULL" );
}

/*--------------- compare------------------------------------------*/
static int Font_compare( BPy_Font * a, BPy_Font * b )
{
	VFont *pa = a->font, *pb = b->font;
	return ( pa == pb ) ? 0 : -1;
}

/*--------------- Font_CreatePyObject---------------------------------*/
PyObject *Font_CreatePyObject( struct VFont * font )
{
	BPy_Font *blen_font;

	blen_font = ( BPy_Font * ) PyObject_NEW( BPy_Font, &Font_Type );

	/*set the all important Bone flag*/
	blen_font->font = font;

	return ( ( PyObject * ) blen_font );
}

/*--------------- Font_CheckPyObject--------------------------------*/
int Font_CheckPyObject( PyObject * py_obj )
{
	return ( py_obj->ob_type == &Font_Type );
}

/*--------------- Font_FromPyObject---------------------------------*/
struct VFont *Font_FromPyObject( PyObject * py_obj )
{
	BPy_Font *blen_obj;

	blen_obj = ( BPy_Font * ) py_obj;
	if( !( ( BPy_Font * ) py_obj )->font ) {	/*test to see if linked to text3d*/
		//use python vars
		return NULL;
	} else {
		//use bone datastruct
		return ( blen_obj->font );
	}
}

/*--------------- Python Font Module methods------------------------*/

/*--------------- Blender.Text3d.Font.New()-----------------------*/
PyObject *M_Font_New( PyObject * self, PyObject * args )
{
	char *name_str = "<builtin>";
	char *parent_str = "";
	BPy_Font *py_font = NULL;	/* for Font Data object wrapper in Python */
	PyObject *tmp; 

	if( !PyArg_ParseTuple( args, "|s", &name_str ) )
		return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
						"expected string or empty argument" ) );

	/*create python font*/
	if( !S_ISDIR(BLI_exist(name_str)) )  {
		tmp= Py_BuildValue("(s)", name_str);
		py_font= M_Text3d_LoadFont (self, Py_BuildValue("(s)", name_str));
		Py_DECREF (tmp);
	}
	else
		return EXPP_incr_ret( Py_None );
	return ( PyObject * ) py_font;
}

/*--------------- Blender.Text3d.Font.Get()-----------------------*/
static PyObject *M_Font_Get( PyObject * self, PyObject * args )
{
	return M_Font_New (self, args);
}

/*--------------- Python BPy_Font methods---------------------------*/

/*--------------- BPy_Font.getName()--------------------------------*/
static PyObject *Font_getName( BPy_Font * self )
{
	PyObject *attr = NULL;

	if( self->font )
		attr = PyString_FromString( self->font->name );
	if( attr )
		return attr;

	return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
					"couldn't get Bone.name attribute" ) );
}

/*--------------- BPy_Font.setName()---------------------------------*/
static PyObject *Font_setName( BPy_Font * self, PyObject * args )
{
	char *name;
	char buf[256];

	if( !PyArg_ParseTuple( args, "s", &name ) )
		return ( EXPP_ReturnPyObjError
			 ( PyExc_AttributeError,
			   "expected string argument" ) );

	/* guarantee a null terminated string of reasonable size */
	PyOS_snprintf( buf, sizeof( buf ), "%s", name );

	if( self->font )
		BLI_strncpy( self->font->name, buf, sizeof( buf )  );
	return EXPP_incr_ret( Py_None );
}

/*--------------- BPy_Font.pack()---------------------------------*/
static PyObject *Font_pack( BPy_Font * self, PyObject * args ) 
{
	int pack= 0;
	if( !PyArg_ParseTuple( args, "i", &pack ) )
		return ( EXPP_ReturnPyObjError
			 ( PyExc_AttributeError,
			   "expected string argument" ) );
	if( pack && !self->font->packedfile ) 
		self->font->packedfile = newPackedFile(self->font->name);
	else if (self->font->packedfile)
		if (unpackVFont(self->font, PF_ASK) == RET_OK)
			G.fileflags &= ~G_AUTOPACK;
	

	return EXPP_incr_ret( Py_None );
}

/*--------------- BPy_Font.ispack()---------------------------------*/
static PyObject *Font_isPacked( BPy_Font * self ) 
{
	if (G.fileflags & G_AUTOPACK)
		return EXPP_incr_ret_True();
	else
		return EXPP_incr_ret_False();
}