@@ -11,6 +11,29 @@ FT_Library _ft2Library;
1111#define FT2FontObject_Check (v ) ((v)->ob_type == &FT2Font_Type)
1212#define GlyphObject_Check (v ) ((v)->ob_type == &Glyph_Type)
1313
14+ #define SETATTR (o ,setattr_func ,name ,PyBuilder ,val ) \
15+ { \
16+ PyObject *pval =PyBuilder(val);\
17+ if (pval == NULL) {PyErr_NoMemory(); return NULL;}\
18+ int gsetResult = setattr_func(o, name, pval);\
19+ Py_DECREF(pval);\
20+ if (gsetResult == -1) {\
21+ PyErr_SetString(PyExc_RuntimeError, "Could not set attr");\
22+ return NULL;\
23+ }\
24+ }
25+
26+ #define SETATTR_PYOBJ (o ,setattr_func ,name ,pval ) \
27+ { \
28+ int gsetResult = setattr_func(o, name, pval);\
29+ Py_DECREF(pval);\
30+ if (gsetResult == -1) {\
31+ PyErr_SetString(PyExc_RuntimeError, "Could not set attr");\
32+ return NULL;\
33+ }\
34+ }
35+
36+
1437
1538static PyMethodDef Glyph_methods [] = {
1639 {NULL , NULL } /* sentinel */
@@ -171,35 +194,36 @@ newFT2FontObject(PyObject *args)
171194 if ( ps_name == NULL )
172195 ps_name = "UNAVAILABLE" ;
173196
174- FT2Font_setattr (self , "postscript_name" , PyString_FromString (ps_name ));
175- FT2Font_setattr (self , "num_faces" , PyInt_FromLong (self -> face -> num_faces ));
176- FT2Font_setattr (self , "family_name" , PyString_FromString (self -> face -> family_name ));
177- FT2Font_setattr (self , "style_name" , PyString_FromString (self -> face -> style_name ));
178- FT2Font_setattr (self , "face_flags" , PyInt_FromLong (self -> face -> face_flags ));
179- FT2Font_setattr (self , "style_flags" , PyInt_FromLong (self -> face -> style_flags ));
180- FT2Font_setattr (self , "num_glyphs" , PyInt_FromLong (self -> face -> num_glyphs ));
181- FT2Font_setattr (self , "num_fixed_sizes" , PyInt_FromLong (self -> face -> num_fixed_sizes ));
182- FT2Font_setattr (self , "num_charmaps" , PyInt_FromLong (self -> face -> num_charmaps ));
197+ SETATTR (self , FT2Font_setattr , "postscript_name" , PyString_FromString , ps_name );
198+ SETATTR (self , FT2Font_setattr , "num_faces" , PyInt_FromLong , self -> face -> num_faces );
199+ SETATTR (self , FT2Font_setattr , "family_name" , PyString_FromString , self -> face -> family_name );
200+ SETATTR (self , FT2Font_setattr , "style_name" , PyString_FromString , self -> face -> style_name );
201+ SETATTR (self , FT2Font_setattr , "face_flags" , PyInt_FromLong , self -> face -> face_flags );
202+ SETATTR (self , FT2Font_setattr , "style_flags" , PyInt_FromLong , self -> face -> style_flags );
203+ SETATTR (self , FT2Font_setattr , "num_glyphs" , PyInt_FromLong , self -> face -> num_glyphs );
204+ SETATTR (self , FT2Font_setattr , "num_fixed_sizes" , PyInt_FromLong , self -> face -> num_fixed_sizes );
205+ SETATTR (self , FT2Font_setattr , "num_charmaps" , PyInt_FromLong , self -> face -> num_charmaps );
206+
183207
184208 int scalable ;
185209 scalable = FT_IS_SCALABLE ( self -> face );
186- FT2Font_setattr (self , "scalable" , PyInt_FromLong ( scalable ));
210+ SETATTR (self , FT2Font_setattr , "scalable" , PyInt_FromLong , scalable );
187211
188212 if (scalable ) {
189- FT2Font_setattr (self , "units_per_EM" , PyInt_FromLong (self -> face -> units_per_EM ));
213+ SETATTR (self , FT2Font_setattr , "units_per_EM" , PyInt_FromLong , self -> face -> units_per_EM );
214+
190215 PyObject * bbox = Py_BuildValue
191216 ("(llll)" ,
192217 self -> face -> bbox .xMin , self -> face -> bbox .yMin ,
193218 self -> face -> bbox .xMax , self -> face -> bbox .yMax );
194- FT2Font_setattr (self , "bbox" , bbox );
195- FT2Font_setattr (self , "ascender" , PyInt_FromLong (self -> face -> ascender ));
196- FT2Font_setattr (self , "descender" , PyInt_FromLong (self -> face -> descender ));
197- FT2Font_setattr (self , "height" , PyInt_FromLong (self -> face -> height ));
198- FT2Font_setattr (self , "max_advance_width" , PyInt_FromLong (self -> face -> max_advance_width ));
199- FT2Font_setattr (self , "max_advance_height" , PyInt_FromLong (self -> face -> max_advance_height ));
200- FT2Font_setattr (self , "underline_position" , PyInt_FromLong (self -> face -> underline_position ));
201- FT2Font_setattr (self , "underline_thickness" , PyInt_FromLong (self -> face -> underline_thickness ));
202-
219+ SETATTR_PYOBJ (self , FT2Font_setattr , "bbox" , bbox );
220+ SETATTR (self , FT2Font_setattr , "ascender" , PyInt_FromLong , self -> face -> ascender );
221+ SETATTR (self , FT2Font_setattr , "descender" , PyInt_FromLong , self -> face -> descender );
222+ SETATTR (self , FT2Font_setattr , "height" , PyInt_FromLong , self -> face -> height );
223+ SETATTR (self , FT2Font_setattr , "max_advance_width" , PyInt_FromLong , self -> face -> max_advance_width );
224+ SETATTR (self , FT2Font_setattr , "max_advance_height" , PyInt_FromLong , self -> face -> max_advance_height );
225+ SETATTR (self , FT2Font_setattr , "underline_position" , PyInt_FromLong , self -> face -> underline_position );
226+ SETATTR (self , FT2Font_setattr , "underline_thickness" , PyInt_FromLong , self -> face -> underline_thickness );
203227
204228 }
205229
@@ -262,6 +286,12 @@ FT2Font_dealloc(FT2FontObject *self)
262286
263287}
264288
289+ char FT2Font_set_size__doc__ [] =
290+ "set_size(ptsize, dpi)\n"
291+ "\n"
292+ "Set the point size and dpi of the text.\n"
293+ ;
294+
265295static PyObject *
266296FT2Font_set_size (FT2FontObject * self , PyObject * args )
267297{
@@ -376,6 +406,12 @@ void load_glyphs(FT2FontObject *self) {
376406}
377407
378408
409+ char FT2Font_set_text__doc__ [] =
410+ "set_text(s, angle)\n"
411+ "\n"
412+ "Set the text string and angle.\n"
413+ "You must call this before draw_glyphs_to_bitmap\n"
414+ ;
379415
380416static PyObject *
381417FT2Font_set_text (FT2FontObject * self , PyObject * args )
@@ -405,13 +441,12 @@ char FT2Font_load_char__doc__[] =
405441"Return value is a Glyph object, with attributes\n"
406442" width # glyph width\n"
407443" height # glyph height\n"
408- "\n"
444+ " bbox # the glyph bbox (xmin, ymin, xmax, ymax) \n"
409445" horiBearingX # left side bearing in horizontal layouts\n"
410446" horiBearingY # top side bearing in horizontal layouts\n"
411447" horiAdvance # advance width for horizontal layout\n"
412- "\n"
413448" vertBearingX # left side bearing in vertical layouts\n"
414- " vertBearingY #top side bearing in vertical layouts\n"
449+ " vertBearingY #top side bearing in vertical layouts\n"
415450" vertAdvance # advance height for vertical layout\n"
416451;
417452static GlyphObject *
@@ -447,27 +482,45 @@ FT2Font_load_char(FT2FontObject *self, PyObject *args)
447482
448483 if (gm == NULL ) {
449484 PyErr_SetString (PyExc_RuntimeError ,
450- "Could not create glyph metrix object" );
485+ "Could not create glyph metrics object" );
451486
452487 return NULL ;
453488 }
454489
490+ FT_BBox bbox ;
491+ FT_Glyph_Get_CBox ( self -> glyphs [self -> num_glyphs ], ft_glyph_bbox_subpixels , & bbox );
492+
455493 gm -> glyph_num = self -> num_glyphs ++ ;
456494
457495 gm -> x_attr = NULL ;
458496
459- Glyph_setattr (gm , "width" , PyInt_FromLong (self -> face -> glyph -> metrics .width ));
460- Glyph_setattr (gm , "height" , PyInt_FromLong (self -> face -> glyph -> metrics .height ));
461- Glyph_setattr (gm , "horiBearingX" , PyInt_FromLong (self -> face -> glyph -> metrics .horiBearingX ));
462- Glyph_setattr (gm , "horiBearingY" , PyInt_FromLong (self -> face -> glyph -> metrics .horiBearingY ));
463- Glyph_setattr (gm , "horiAdvance" , PyInt_FromLong (self -> face -> glyph -> metrics .horiAdvance ));
464- Glyph_setattr (gm , "vertBearingX" , PyInt_FromLong (self -> face -> glyph -> metrics .vertBearingX ));
465- Glyph_setattr (gm , "vertBearingY" , PyInt_FromLong (self -> face -> glyph -> metrics .vertBearingY ));
466- Glyph_setattr (gm , "vertAdvance" , PyInt_FromLong (self -> face -> glyph -> metrics .vertAdvance ));
497+ SETATTR (gm , Glyph_setattr , "width" , PyInt_FromLong , self -> face -> glyph -> metrics .width );
498+ SETATTR (gm , Glyph_setattr , "height" , PyInt_FromLong , self -> face -> glyph -> metrics .height );
499+ SETATTR (gm , Glyph_setattr , "horiBearingX" , PyInt_FromLong , self -> face -> glyph -> metrics .horiBearingX );
500+ SETATTR (gm , Glyph_setattr , "horiBearingY" , PyInt_FromLong , self -> face -> glyph -> metrics .horiBearingY );
501+ SETATTR (gm , Glyph_setattr , "horiAdvance" , PyInt_FromLong , self -> face -> glyph -> metrics .horiAdvance );
502+ SETATTR (gm , Glyph_setattr , "vertBearingX" , PyInt_FromLong , self -> face -> glyph -> metrics .vertBearingX );
503+
504+ SETATTR (gm , Glyph_setattr , "vertBearingY" , PyInt_FromLong , self -> face -> glyph -> metrics .vertBearingY );
505+ SETATTR (gm , Glyph_setattr , "vertAdvance" , PyInt_FromLong , self -> face -> glyph -> metrics .vertAdvance );
506+
507+ PyObject * pbbox = Py_BuildValue
508+ ("(llll)" ,
509+ bbox .xMin , bbox .yMin , bbox .xMax , bbox .yMax );
510+ SETATTR_PYOBJ (gm , Glyph_setattr , "bbox" , pbbox );
511+
512+
467513
468514 return gm ;
469515}
470516
517+ char FT2Font_get_width_height__doc__ [] =
518+ "w, h = get_width_height()\n"
519+ "\n"
520+ "Get the width and height in 26.6 subpixels of the current string set by set_text\n"
521+ "The rotation of the string is accounted for. To get width and height\n"
522+ "in pixels, divide these values by 64\n"
523+ ;
471524static PyObject *
472525FT2Font_get_width_height (FT2FontObject * self , PyObject * args )
473526{
@@ -477,8 +530,8 @@ FT2Font_get_width_height(FT2FontObject *self, PyObject *args)
477530
478531 compute_string_bbox (self , & bbox );
479532 return Py_BuildValue ("(ll)" ,
480- (bbox .xMax - bbox .xMin )/ 64 ,
481- (bbox .yMax - bbox .yMin )/ 64 );
533+ (bbox .xMax - bbox .xMin ),
534+ (bbox .yMax - bbox .yMin ));
482535}
483536
484537
@@ -506,6 +559,11 @@ draw_bitmap( FT_Bitmap* bitmap,
506559}
507560
508561
562+ char FT2Font_write_bitmap__doc__ [] =
563+ "write_bitmap(fname)\n"
564+ "\n"
565+ "Write the bitmap to file fname\n"
566+ ;
509567
510568static PyObject *
511569FT2Font_write_bitmap (FT2FontObject * self , PyObject * args )
@@ -534,7 +592,12 @@ FT2Font_write_bitmap(FT2FontObject *self, PyObject *args)
534592
535593}
536594
537-
595+ char FT2Font_draw_glyphs_to_bitmap__doc__ [] =
596+ "draw_glyphs_to_bitmap()\n"
597+ "\n"
598+ "Draw the glyphs that were loaded by set_text to the bitmap\n"
599+ "The bitmap size will be automatically set to include the glyphs\n"
600+ ;
538601static PyObject *
539602FT2Font_draw_glyphs_to_bitmap (FT2FontObject * self , PyObject * args )
540603{
@@ -652,7 +715,7 @@ FT2Font_draw_glyph_to_bitmap(FT2FontObject *self, PyObject *args)
652715
653716 //printf("draw_glyph_to_bitmap to image at %ld, %lu, %lu, %lu\n",
654717 //x, y, self->image.width, self->image.height);
655- // printf("bitmap props %d, %d\n", bitmap->left, bitmap->top);
718+ printf ("\tbitmap props %d, %d\n" , bitmap -> left , bitmap -> top );
656719
657720
658721 draw_bitmap ( & bitmap -> bitmap ,
@@ -671,14 +734,14 @@ FT2Font_draw_glyph_to_bitmap(FT2FontObject *self, PyObject *args)
671734}
672735
673736static PyMethodDef FT2Font_methods [] = {
674- {"write_bitmap" , (PyCFunction )FT2Font_write_bitmap , METH_VARARGS },
737+ {"write_bitmap" , (PyCFunction )FT2Font_write_bitmap , METH_VARARGS , FT2Font_write_bitmap__doc__ },
675738 {"set_bitmap_size" , (PyCFunction )FT2Font_set_bitmap_size , METH_VARARGS , FT2Font_load_char__doc__ },
676- {"draw_glyph_to_bitmap" , (PyCFunction )FT2Font_draw_glyph_to_bitmap , METH_VARARGS },
677- {"draw_glyphs_to_bitmap" , (PyCFunction )FT2Font_draw_glyphs_to_bitmap , METH_VARARGS },
739+ {"draw_glyph_to_bitmap" , (PyCFunction )FT2Font_draw_glyph_to_bitmap , METH_VARARGS , FT2Font_draw_glyph_to_bitmap__doc__ },
740+ {"draw_glyphs_to_bitmap" , (PyCFunction )FT2Font_draw_glyphs_to_bitmap , METH_VARARGS , FT2Font_draw_glyphs_to_bitmap__doc__ },
678741 {"load_char" , (PyCFunction )FT2Font_load_char , METH_VARARGS , FT2Font_load_char__doc__ },
679- {"set_text" , (PyCFunction )FT2Font_set_text , METH_VARARGS },
680- {"set_size" , (PyCFunction )FT2Font_set_size , METH_VARARGS },
681- {"get_width_height" , (PyCFunction )FT2Font_get_width_height , METH_VARARGS },
742+ {"set_text" , (PyCFunction )FT2Font_set_text , METH_VARARGS , FT2Font_set_text__doc__ },
743+ {"set_size" , (PyCFunction )FT2Font_set_size , METH_VARARGS , FT2Font_set_size__doc__ },
744+ {"get_width_height" , (PyCFunction )FT2Font_get_width_height , METH_VARARGS , FT2Font_get_width_height__doc__ },
682745 {NULL , NULL } /* sentinel */
683746};
684747
0 commit comments