11#include " mplutils.h"
22#include " ft2font.h"
3+ #include " numpy_cpp.h"
34#include " py_converters.h"
45#include " py_exceptions.h"
56
67// From Python
78#include < structmember.h>
89
10+ #include < sstream>
911#include < set>
1012
1113static PyObject *convert_xys_to_array (std::vector<double > &xys)
@@ -308,6 +310,31 @@ static void close_file_callback(FT_Stream stream)
308310 PyErr_Restore (type, value, traceback);
309311}
310312
313+ static void
314+ ft_glyph_warn (FT_ULong charcode, std::set<FT_String*> family_names)
315+ {
316+ PyObject *text_helpers = NULL , *tmp = NULL ;
317+ std::set<FT_String*>::iterator it = family_names.begin ();
318+ std::stringstream ss;
319+ ss<<*it;
320+ while (++it != family_names.end ()){
321+ ss<<" , " <<*it;
322+ }
323+
324+ if (!(text_helpers = PyImport_ImportModule (" matplotlib._text_helpers" )) ||
325+ !(tmp = PyObject_CallMethod (text_helpers,
326+ " warn_on_missing_glyph" , " (k, s)" ,
327+ charcode, ss.str ().c_str ()))) {
328+ goto exit;
329+ }
330+ exit:
331+ Py_XDECREF (text_helpers);
332+ Py_XDECREF (tmp);
333+ if (PyErr_Occurred ()) {
334+ throw mpl::exception ();
335+ }
336+ }
337+
311338static PyObject *PyFT2Font_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
312339{
313340 PyFT2Font *self;
@@ -455,7 +482,8 @@ static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
455482 Py_CLEAR (data);
456483
457484 CALL_CPP_FULL (
458- " FT2Font" , (self->x = new FT2Font (open_args, hinting_factor, fallback_fonts)),
485+ " FT2Font" ,
486+ (self->x = new FT2Font (open_args, hinting_factor, fallback_fonts, ft_glyph_warn)),
459487 Py_CLEAR (self->py_file ), -1 );
460488
461489 CALL_CPP_INIT (" FT2Font->set_kerning_factor" , (self->x ->set_kerning_factor (kerning_factor)));
@@ -555,13 +583,13 @@ static PyObject *PyFT2Font_get_kerning(PyFT2Font *self, PyObject *args)
555583{
556584 FT_UInt left, right, mode;
557585 int result;
558- int fallback = 1 ;
586+ bool fallback = true ;
559587
560588 if (!PyArg_ParseTuple (args, " III:get_kerning" , &left, &right, &mode)) {
561589 return NULL ;
562590 }
563591
564- CALL_CPP (" get_kerning" , (result = self->x ->get_kerning (left, right, mode, ( bool ) fallback)));
592+ CALL_CPP (" get_kerning" , (result = self->x ->get_kerning (left, right, mode, fallback)));
565593
566594 return PyLong_FromLong (result);
567595}
@@ -704,7 +732,7 @@ const char *PyFT2Font_load_char__doc__ =
704732static PyObject *PyFT2Font_load_char (PyFT2Font *self, PyObject *args, PyObject *kwds)
705733{
706734 long charcode;
707- int fallback = 1 ;
735+ bool fallback = true ;
708736 FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
709737 const char *names[] = { " charcode" , " flags" , NULL };
710738
@@ -717,7 +745,7 @@ static PyObject *PyFT2Font_load_char(PyFT2Font *self, PyObject *args, PyObject *
717745 }
718746
719747 FT2Font *ft_object = NULL ;
720- CALL_CPP (" load_char" , (self->x ->load_char (charcode, flags, ft_object, ( bool ) fallback)));
748+ CALL_CPP (" load_char" , (self->x ->load_char (charcode, flags, ft_object, fallback)));
721749
722750 return PyGlyph_from_FT2Font (ft_object);
723751}
@@ -743,7 +771,7 @@ static PyObject *PyFT2Font_load_glyph(PyFT2Font *self, PyObject *args, PyObject
743771{
744772 FT_UInt glyph_index;
745773 FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
746- int fallback = 1 ;
774+ bool fallback = true ;
747775 const char *names[] = { " glyph_index" , " flags" , NULL };
748776
749777 /* This makes a technically incorrect assumption that FT_Int32 is
@@ -755,7 +783,7 @@ static PyObject *PyFT2Font_load_glyph(PyFT2Font *self, PyObject *args, PyObject
755783 }
756784
757785 FT2Font *ft_object = NULL ;
758- CALL_CPP (" load_glyph" , (self->x ->load_glyph (glyph_index, flags, ft_object, ( bool ) fallback)));
786+ CALL_CPP (" load_glyph" , (self->x ->load_glyph (glyph_index, flags, ft_object, fallback)));
759787
760788 return PyGlyph_from_FT2Font (ft_object);
761789}
@@ -912,14 +940,16 @@ const char *PyFT2Font_get_glyph_name__doc__ =
912940static PyObject *PyFT2Font_get_glyph_name (PyFT2Font *self, PyObject *args)
913941{
914942 unsigned int glyph_number;
915- char buffer[ 128 ] ;
943+ std::string buffer;
916944 int fallback = 1 ;
917945
918946 if (!PyArg_ParseTuple (args, " I:get_glyph_name" , &glyph_number)) {
919947 return NULL ;
920948 }
921- CALL_CPP (" get_glyph_name" , (self->x ->get_glyph_name (glyph_number, buffer, (bool )fallback)));
922- return PyUnicode_FromString (buffer);
949+ buffer.resize (128 );
950+ CALL_CPP (" get_glyph_name" ,
951+ (self->x ->get_glyph_name (glyph_number, buffer, fallback)));
952+ return PyUnicode_FromString (buffer.c_str ());
923953}
924954
925955const char *PyFT2Font_get_charmap__doc__ =
@@ -962,13 +992,13 @@ static PyObject *PyFT2Font_get_char_index(PyFT2Font *self, PyObject *args)
962992{
963993 FT_UInt index;
964994 FT_ULong ccode;
965- int fallback = 1 ;
995+ bool fallback = true ;
966996
967997 if (!PyArg_ParseTuple (args, " k:get_char_index" , &ccode)) {
968998 return NULL ;
969999 }
9701000
971- CALL_CPP (" get_char_index" , index = self->x ->get_char_index (ccode, ( bool ) fallback));
1001+ CALL_CPP (" get_char_index" , index = self->x ->get_char_index (ccode, fallback));
9721002
9731003 return PyLong_FromLong (index);
9741004}
@@ -1270,7 +1300,20 @@ const char *PyFT2Font_get_path__doc__ =
12701300
12711301static PyObject *PyFT2Font_get_path (PyFT2Font *self, PyObject *args)
12721302{
1273- CALL_CPP (" get_path" , return self->x ->get_path ());
1303+ std::vector<double > vertices;
1304+ std::vector<unsigned char > codes;
1305+
1306+ CALL_CPP (" get_path" , self->x ->get_path (vertices, codes));
1307+
1308+ npy_intp length = codes.size ();
1309+ npy_intp vertices_dims[2 ] = { length, 2 };
1310+ numpy::array_view<double , 2 > vertices_arr (vertices_dims);
1311+ memcpy (vertices_arr.data (), vertices.data (), sizeof (double ) * vertices.size ());
1312+ npy_intp codes_dims[1 ] = { length };
1313+ numpy::array_view<unsigned char , 1 > codes_arr (codes_dims);
1314+ memcpy (codes_arr.data (), codes.data (), codes.size ());
1315+
1316+ return Py_BuildValue (" NN" , vertices_arr.pyobj (), codes_arr.pyobj ());
12741317}
12751318
12761319const char *PyFT2Font_get_image__doc__ =
0 commit comments