Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4a0da6

Browse files
committed
added ps print to agg; fixed ref probs in ft2face
svn path=/trunk/matplotlib/; revision=156
1 parent f4be38c commit d4a0da6

4 files changed

Lines changed: 116 additions & 44 deletions

File tree

TODO

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,10 @@
231231
-- DONE 2004-02-23 with points_to_pixels. fonts too big on wx
232232

233233
-- test agg/freetype2 with ms freetype fonts in path. Be sure to
234-
remove ~/.fonts.cache after changing yout ttfpath
234+
remove ~/.fonts.cache after changing yout ttfpath
235+
236+
-- why doesn't dir reveal any attrs on my Glyph and FT2Font objs?
237+
238+
-- get module docs for ft2font working right
239+
240+
-- kwargs in plot to set lineprops?

examples/ftface_props.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
print 'Num glyphs :', font.num_glyphs # number of glyphs in the face
3030
print 'Family name :', font.family_name # face family name
3131
print 'Syle name :', font.style_name # face syle name
32+
print 'PS name :', font.postscript_name # the postscript name
3233
print 'Num fixed :', font.num_fixed_sizes # number of embedded bitmap in face
3334

3435
# the following are only available if face.scalable
@@ -65,3 +66,5 @@
6566
print 'Mult. masters :', font.style_flags & FT_FACE_FLAG_MULTIPLE_MASTERS != 0
6667
print 'Glyph names :', font.style_flags & FT_FACE_FLAG_GLYPH_NAMES != 0
6768

69+
font.jdh = 'hi'
70+
print dir(font)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
BUILD_GTKGD = 0
2626

2727
# build TK GUI with Agg renderer ; requires Tkinter Python extension and Tk includes
28-
BUILD_TKAGG = 0
28+
BUILD_TKAGG = 1
2929

3030
# build the freetype2 interface - highly experimental and broken!
3131
# Unless your name is John Hunter, you probably don't want this

src/ft2font.c

Lines changed: 105 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1538
static 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+
265295
static PyObject *
266296
FT2Font_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

380416
static PyObject *
381417
FT2Font_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
;
417452
static 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+
;
471524
static PyObject *
472525
FT2Font_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

510568
static PyObject *
511569
FT2Font_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+
;
538601
static PyObject *
539602
FT2Font_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

673736
static 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

Comments
 (0)