1
1
#include " mplutils.h"
2
2
#include " ft2font.h"
3
+ #include " numpy_cpp.h"
3
4
#include " py_converters.h"
4
5
#include " py_exceptions.h"
5
6
6
7
// From Python
7
8
#include < structmember.h>
8
9
10
+ #include < sstream>
9
11
#include < set>
10
12
11
13
static PyObject *convert_xys_to_array (std::vector<double > &xys)
@@ -308,6 +310,31 @@ static void close_file_callback(FT_Stream stream)
308
310
PyErr_Restore (type, value, traceback);
309
311
}
310
312
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
+
311
338
static PyObject *PyFT2Font_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
312
339
{
313
340
PyFT2Font *self;
@@ -455,7 +482,8 @@ static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
455
482
Py_CLEAR (data);
456
483
457
484
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)),
459
487
Py_CLEAR (self->py_file ), -1 );
460
488
461
489
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)
555
583
{
556
584
FT_UInt left, right, mode;
557
585
int result;
558
- int fallback = 1 ;
586
+ bool fallback = true ;
559
587
560
588
if (!PyArg_ParseTuple (args, " III:get_kerning" , &left, &right, &mode)) {
561
589
return NULL ;
562
590
}
563
591
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)));
565
593
566
594
return PyLong_FromLong (result);
567
595
}
@@ -704,7 +732,7 @@ const char *PyFT2Font_load_char__doc__ =
704
732
static PyObject *PyFT2Font_load_char (PyFT2Font *self, PyObject *args, PyObject *kwds)
705
733
{
706
734
long charcode;
707
- int fallback = 1 ;
735
+ bool fallback = true ;
708
736
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
709
737
const char *names[] = { " charcode" , " flags" , NULL };
710
738
@@ -717,7 +745,7 @@ static PyObject *PyFT2Font_load_char(PyFT2Font *self, PyObject *args, PyObject *
717
745
}
718
746
719
747
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)));
721
749
722
750
return PyGlyph_from_FT2Font (ft_object);
723
751
}
@@ -743,7 +771,7 @@ static PyObject *PyFT2Font_load_glyph(PyFT2Font *self, PyObject *args, PyObject
743
771
{
744
772
FT_UInt glyph_index;
745
773
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
746
- int fallback = 1 ;
774
+ bool fallback = true ;
747
775
const char *names[] = { " glyph_index" , " flags" , NULL };
748
776
749
777
/* This makes a technically incorrect assumption that FT_Int32 is
@@ -755,7 +783,7 @@ static PyObject *PyFT2Font_load_glyph(PyFT2Font *self, PyObject *args, PyObject
755
783
}
756
784
757
785
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)));
759
787
760
788
return PyGlyph_from_FT2Font (ft_object);
761
789
}
@@ -912,14 +940,16 @@ const char *PyFT2Font_get_glyph_name__doc__ =
912
940
static PyObject *PyFT2Font_get_glyph_name (PyFT2Font *self, PyObject *args)
913
941
{
914
942
unsigned int glyph_number;
915
- char buffer[ 128 ] ;
943
+ std::string buffer;
916
944
int fallback = 1 ;
917
945
918
946
if (!PyArg_ParseTuple (args, " I:get_glyph_name" , &glyph_number)) {
919
947
return NULL ;
920
948
}
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 ());
923
953
}
924
954
925
955
const char *PyFT2Font_get_charmap__doc__ =
@@ -962,13 +992,13 @@ static PyObject *PyFT2Font_get_char_index(PyFT2Font *self, PyObject *args)
962
992
{
963
993
FT_UInt index ;
964
994
FT_ULong ccode;
965
- int fallback = 1 ;
995
+ bool fallback = true ;
966
996
967
997
if (!PyArg_ParseTuple (args, " k:get_char_index" , &ccode)) {
968
998
return NULL ;
969
999
}
970
1000
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));
972
1002
973
1003
return PyLong_FromLong (index );
974
1004
}
@@ -1270,7 +1300,20 @@ const char *PyFT2Font_get_path__doc__ =
1270
1300
1271
1301
static PyObject *PyFT2Font_get_path (PyFT2Font *self, PyObject *args)
1272
1302
{
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 ());
1274
1317
}
1275
1318
1276
1319
const char *PyFT2Font_get_image__doc__ =
0 commit comments