~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/blender/python/api2_2x/Font.c

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version. The Blender
 
8
 * Foundation also sells licenses for use in proprietary software under
 
9
 * the Blender License.  See http://www.blender.org/BL/ for information
 
10
 * about this.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software Foundation,
 
19
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
20
 *
 
21
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
22
 * All rights reserved.
 
23
 *
 
24
 * This is a new part of Blender.
 
25
 *
 
26
 * Contributor(s): Joilnen Leite
 
27
 *
 
28
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
29
 */
 
30
 
 
31
#include "BKE_global.h"
 
32
#include "BKE_packedFile.h"
 
33
#include "DNA_vfont_types.h"
 
34
#include "DNA_packedFile_types.h"
 
35
#include "Text3d.h"
 
36
#include "Font.h"
 
37
 
 
38
extern PyObject *M_Text3d_LoadFont( PyObject * self, PyObject * args );
 
39
 
 
40
/*--------------------Python API function prototypes for the Font module----*/
 
41
PyObject *M_Font_New( PyObject * self, PyObject * args );
 
42
static PyObject *M_Font_Get( PyObject * self, PyObject * args );
 
43
 
 
44
/*------------------------Python API Doc strings for the Font module--------*/
 
45
char M_Font_doc[] = "The Blender Font module\n\n\
 
46
This module provides control over **Font Data** objects in Blender.\n\n\
 
47
Example::\n\n\
 
48
        from Blender import Text3d.Font\n\
 
49
        l = Text3d.Font.New()\n";
 
50
char M_Font_New_doc[] = "(name) - return a new Font of name 'name'.";
 
51
char M_Font_Get_doc[] = "(name) - return a new Font of name 'name'.";
 
52
 
 
53
/*----- Python method structure definition for Blender.Text3d.Font module---*/
 
54
struct PyMethodDef M_Font_methods[] = {
 
55
        {"New", ( PyCFunction ) M_Font_New, METH_VARARGS, M_Font_New_doc},
 
56
        {"Get", ( PyCFunction ) M_Font_Get, METH_VARARGS, M_Font_New_doc},
 
57
        {NULL, NULL, 0, NULL}
 
58
};
 
59
 
 
60
/*--------------- Python BPy_Bone methods declarations:-------------------*/
 
61
static PyObject *Font_getName( BPy_Font * self );
 
62
static PyObject *Font_setName( BPy_Font * self, PyObject * args );
 
63
static PyObject *Font_pack( BPy_Font * self, PyObject * args );
 
64
static PyObject *Font_isPacked( BPy_Font * self );
 
65
 
 
66
/*--------------- Python BPy_Font methods table:--------------------------*/
 
67
static PyMethodDef BPy_Font_methods[] = {
 
68
        {"GetName", ( PyCFunction ) Font_getName, METH_NOARGS,
 
69
         "() - return Font name"},
 
70
        {"getName", ( PyCFunction ) Font_getName, METH_NOARGS,
 
71
         "() - return Font name"},
 
72
        {"setName", ( PyCFunction ) Font_setName, METH_VARARGS,
 
73
         "() - return Font name"},
 
74
        {"pack", ( PyCFunction ) Font_pack, METH_VARARGS,
 
75
         "() - pack/unpack Font"},
 
76
        {"isPacked", ( PyCFunction ) Font_isPacked, METH_NOARGS,
 
77
         "() - pack/unpack Font"},
 
78
        {NULL, NULL, 0, NULL}
 
79
};
 
80
 
 
81
/*--------------- Python TypeBone callback function prototypes----------*/
 
82
static void Font_dealloc( BPy_Font * font );
 
83
static PyObject *Font_getAttr( BPy_Font * bone, char *name );
 
84
static int Font_setAttr( BPy_Font * font, char *name, PyObject * v );
 
85
static int Font_compare( BPy_Font * a1, BPy_Font * a2 );
 
86
static PyObject *Font_repr( BPy_Font * font );
 
87
 
 
88
PyTypeObject Font_Type = {
 
89
        PyObject_HEAD_INIT( NULL )
 
90
                0,              /* ob_size */
 
91
        "Font",         /* tp_name */
 
92
        sizeof( BPy_Font ),     /* tp_basicsize */
 
93
        0,                      /* tp_itemsize */
 
94
        /* methods */
 
95
        ( destructor ) Font_dealloc,    /* tp_dealloc */
 
96
        0,                      /* tp_print */
 
97
        ( getattrfunc ) Font_getAttr,   /* tp_getattr */
 
98
        ( setattrfunc ) Font_setAttr,   /* tp_setattr */
 
99
        ( cmpfunc ) Font_compare,                       /* tp_compare */
 
100
        ( reprfunc ) Font_repr, /* tp_repr */
 
101
        0,                      /* tp_as_number */
 
102
        0,                      /* tp_as_sequence */
 
103
        0,                      /* tp_as_mapping */
 
104
        0,                      /* tp_as_hash */
 
105
        0, 0, 0, 0, 0, 0,
 
106
        0,                      /* tp_doc */
 
107
        0, 0, 0, 0, 0, 0,
 
108
        BPy_Font_methods,       /* tp_methods */
 
109
        0,                      /* tp_members */
 
110
};
 
111
 
 
112
/*--------------- Font Module Init-----------------------------*/
 
113
PyObject *Font_Init( void )
 
114
{
 
115
        PyObject *submodule;
 
116
 
 
117
        Font_Type.ob_type = &PyType_Type;
 
118
 
 
119
        submodule = Py_InitModule3( "Blender.Text3d.Font",
 
120
                                    M_Font_methods, M_Font_doc );
 
121
 
 
122
        return ( submodule );
 
123
}
 
124
 
 
125
/*--------------- Bone module internal callbacks-----------------*/
 
126
/*---------------BPy_Bone internal callbacks/methods-------------*/
 
127
 
 
128
//--------------- dealloc------------------------------------------
 
129
static void Font_dealloc( BPy_Font * self )
 
130
{
 
131
        PyObject_DEL( self );
 
132
}
 
133
 
 
134
/*---------------getattr-------------------------------------------*/
 
135
static PyObject *Font_getAttr( BPy_Font * self, char *name )
 
136
{
 
137
        return Py_FindMethod( BPy_Font_methods, ( PyObject * ) self, name );
 
138
}
 
139
 
 
140
/*--------------- setattr-------------------------------------------*/
 
141
static int Font_setAttr( BPy_Font * self, char *name, PyObject * value )
 
142
{
 
143
        return 0;
 
144
}
 
145
 
 
146
/*--------------- repr---------------------------------------------*/
 
147
static PyObject *Font_repr( BPy_Font * self )
 
148
{
 
149
        if( self->font )
 
150
                return PyString_FromFormat( "[Font \"%s\"]",
 
151
                                            self->font->name );
 
152
        else
 
153
                return PyString_FromString( "NULL" );
 
154
}
 
155
 
 
156
/*--------------- compare------------------------------------------*/
 
157
static int Font_compare( BPy_Font * a, BPy_Font * b )
 
158
{
 
159
        VFont *pa = a->font, *pb = b->font;
 
160
        return ( pa == pb ) ? 0 : -1;
 
161
}
 
162
 
 
163
/*--------------- Font_CreatePyObject---------------------------------*/
 
164
PyObject *Font_CreatePyObject( struct VFont * font )
 
165
{
 
166
        BPy_Font *blen_font;
 
167
 
 
168
        blen_font = ( BPy_Font * ) PyObject_NEW( BPy_Font, &Font_Type );
 
169
 
 
170
        /*set the all important Bone flag*/
 
171
        blen_font->font = font;
 
172
 
 
173
        return ( ( PyObject * ) blen_font );
 
174
}
 
175
 
 
176
/*--------------- Font_CheckPyObject--------------------------------*/
 
177
int Font_CheckPyObject( PyObject * py_obj )
 
178
{
 
179
        return ( py_obj->ob_type == &Font_Type );
 
180
}
 
181
 
 
182
/*--------------- Font_FromPyObject---------------------------------*/
 
183
struct VFont *Font_FromPyObject( PyObject * py_obj )
 
184
{
 
185
        BPy_Font *blen_obj;
 
186
 
 
187
        blen_obj = ( BPy_Font * ) py_obj;
 
188
        if( !( ( BPy_Font * ) py_obj )->font ) {        /*test to see if linked to text3d*/
 
189
                //use python vars
 
190
                return NULL;
 
191
        } else {
 
192
                //use bone datastruct
 
193
                return ( blen_obj->font );
 
194
        }
 
195
}
 
196
 
 
197
/*--------------- Python Font Module methods------------------------*/
 
198
 
 
199
/*--------------- Blender.Text3d.Font.New()-----------------------*/
 
200
PyObject *M_Font_New( PyObject * self, PyObject * args )
 
201
{
 
202
        char *name_str = "<builtin>";
 
203
//      char *parent_str = "";
 
204
        BPy_Font *py_font = NULL;       /* for Font Data object wrapper in Python */
 
205
        PyObject *tmp; 
 
206
 
 
207
        if( !PyArg_ParseTuple( args, "|s", &name_str ) )
 
208
                return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
 
209
                                                "expected string or empty argument" ) );
 
210
 
 
211
        /*create python font*/
 
212
        if( !S_ISDIR(BLI_exist(name_str)) )  {
 
213
                tmp= Py_BuildValue("(s)", name_str);
 
214
                py_font= (BPy_Font *) M_Text3d_LoadFont (self, Py_BuildValue("(s)", name_str));
 
215
                Py_DECREF (tmp);
 
216
        }
 
217
        else
 
218
                return EXPP_incr_ret( Py_None );
 
219
        return ( PyObject * ) py_font;
 
220
}
 
221
 
 
222
/*--------------- Blender.Text3d.Font.Get()-----------------------*/
 
223
static PyObject *M_Font_Get( PyObject * self, PyObject * args )
 
224
{
 
225
        return M_Font_New (self, args);
 
226
}
 
227
 
 
228
/*--------------- Python BPy_Font methods---------------------------*/
 
229
 
 
230
/*--------------- BPy_Font.getName()--------------------------------*/
 
231
static PyObject *Font_getName( BPy_Font * self )
 
232
{
 
233
        PyObject *attr = NULL;
 
234
 
 
235
        if( self->font )
 
236
                attr = PyString_FromString( self->font->name );
 
237
        if( attr )
 
238
                return attr;
 
239
 
 
240
        return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
241
                                        "couldn't get Bone.name attribute" ) );
 
242
}
 
243
 
 
244
/*--------------- BPy_Font.setName()---------------------------------*/
 
245
static PyObject *Font_setName( BPy_Font * self, PyObject * args )
 
246
{
 
247
        char *name;
 
248
        char buf[256];
 
249
 
 
250
        if( !PyArg_ParseTuple( args, "s", &name ) )
 
251
                return ( EXPP_ReturnPyObjError
 
252
                         ( PyExc_AttributeError,
 
253
                           "expected string argument" ) );
 
254
 
 
255
        /* guarantee a null terminated string of reasonable size */
 
256
        PyOS_snprintf( buf, sizeof( buf ), "%s", name );
 
257
 
 
258
        if( self->font )
 
259
                BLI_strncpy( self->font->name, buf, sizeof( buf )  );
 
260
        return EXPP_incr_ret( Py_None );
 
261
}
 
262
 
 
263
/*--------------- BPy_Font.pack()---------------------------------*/
 
264
static PyObject *Font_pack( BPy_Font * self, PyObject * args ) 
 
265
{
 
266
        int pack= 0;
 
267
        if( !PyArg_ParseTuple( args, "i", &pack ) )
 
268
                return ( EXPP_ReturnPyObjError
 
269
                         ( PyExc_AttributeError,
 
270
                           "expected string argument" ) );
 
271
        if( pack && !self->font->packedfile ) 
 
272
                self->font->packedfile = newPackedFile(self->font->name);
 
273
        else if (self->font->packedfile)
 
274
                if (unpackVFont(self->font, PF_ASK) == RET_OK)
 
275
                        G.fileflags &= ~G_AUTOPACK;
 
276
        
 
277
 
 
278
        return EXPP_incr_ret( Py_None );
 
279
}
 
280
 
 
281
/*--------------- BPy_Font.ispack()---------------------------------*/
 
282
static PyObject *Font_isPacked( BPy_Font * self ) 
 
283
{
 
284
        if (G.fileflags & G_AUTOPACK)
 
285
                return EXPP_incr_ret_True();
 
286
        else
 
287
                return EXPP_incr_ret_False();
 
288
}
 
289