forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft2font_wrapper.cpp
1792 lines (1536 loc) · 61.3 KB
/
ft2font_wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mplutils.h"
#include "ft2font.h"
#include "file_compat.h"
#include "py_converters.h"
#include "py_exceptions.h"
#include "numpy_cpp.h"
// From Python
#include <structmember.h>
#define STRINGIFY(s) XSTRINGIFY(s)
#define XSTRINGIFY(s) #s
static PyObject *convert_xys_to_array(std::vector<double> &xys)
{
npy_intp dims[] = {(npy_intp)xys.size() / 2, 2 };
if (dims[0] > 0) {
return PyArray_SimpleNewFromData(2, dims, NPY_DOUBLE, &xys[0]);
} else {
return PyArray_SimpleNew(2, dims, NPY_DOUBLE);
}
}
/**********************************************************************
* FT2Image
* */
typedef struct
{
PyObject_HEAD
FT2Image *x;
Py_ssize_t shape[2];
Py_ssize_t strides[2];
Py_ssize_t suboffsets[2];
} PyFT2Image;
static PyObject *PyFT2Image_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyFT2Image *self;
self = (PyFT2Image *)type->tp_alloc(type, 0);
self->x = NULL;
return (PyObject *)self;
}
static int PyFT2Image_init(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
double width;
double height;
if (!PyArg_ParseTuple(args, "dd:FT2Image", &width, &height)) {
return -1;
}
CALL_CPP_INIT("FT2Image", (self->x = new FT2Image(width, height)));
return 0;
}
static void PyFT2Image_dealloc(PyFT2Image *self)
{
delete self->x;
Py_TYPE(self)->tp_free((PyObject *)self);
}
const char *PyFT2Image_draw_rect__doc__ =
"draw_rect(x0, y0, x1, y1)\n"
"\n"
"Draw a rect to the image.\n"
"\n";
static PyObject *PyFT2Image_draw_rect(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
double x0, y0, x1, y1;
if (!PyArg_ParseTuple(args, "dddd:draw_rect", &x0, &y0, &x1, &y1)) {
return NULL;
}
CALL_CPP("draw_rect", (self->x->draw_rect(x0, y0, x1, y1)));
Py_RETURN_NONE;
}
const char *PyFT2Image_draw_rect_filled__doc__ =
"draw_rect_filled(x0, y0, x1, y1)\n"
"\n"
"Draw a filled rect to the image.\n"
"\n";
static PyObject *PyFT2Image_draw_rect_filled(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
double x0, y0, x1, y1;
if (!PyArg_ParseTuple(args, "dddd:draw_rect_filled", &x0, &y0, &x1, &y1)) {
return NULL;
}
CALL_CPP("draw_rect_filled", (self->x->draw_rect_filled(x0, y0, x1, y1)));
Py_RETURN_NONE;
}
const char *PyFT2Image_as_str__doc__ =
"s = image.as_str()\n"
"\n"
"[*Deprecated*]\n"
"Return the image buffer as a string\n"
"\n";
static PyObject *PyFT2Image_as_str(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_FutureWarning,
"FT2Image.as_str is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
return PyBytes_FromStringAndSize((const char *)self->x->get_buffer(),
self->x->get_width() * self->x->get_height());
}
const char *PyFT2Image_as_rgba_str__doc__ =
"s = image.as_rgba_str()\n"
"\n"
"[*Deprecated*]\n"
"Return the image buffer as a RGBA string\n"
"\n";
static PyObject *PyFT2Image_as_rgba_str(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_FutureWarning,
"FT2Image.as_rgba_str is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
npy_intp dims[] = {(npy_intp)self->x->get_height(), (npy_intp)self->x->get_width(), 4 };
numpy::array_view<unsigned char, 3> result(dims);
unsigned char *src = self->x->get_buffer();
unsigned char *end = src + (self->x->get_width() * self->x->get_height());
unsigned char *dst = result.data();
while (src != end) {
*dst++ = 0;
*dst++ = 0;
*dst++ = 0;
*dst++ = *src++;
}
return result.pyobj();
}
const char *PyFT2Image_as_array__doc__ =
"x = image.as_array()\n"
"\n"
"[*Deprecated*]\n"
"Return the image buffer as a width x height numpy array of ubyte \n"
"\n";
static PyObject *PyFT2Image_as_array(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_FutureWarning,
"FT2Image.as_array is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
npy_intp dims[] = {(npy_intp)self->x->get_height(), (npy_intp)self->x->get_width() };
return PyArray_SimpleNewFromData(2, dims, NPY_UBYTE, self->x->get_buffer());
}
static PyObject *PyFT2Image_get_width(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_FutureWarning,
"FT2Image.get_width is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
return PyLong_FromLong(self->x->get_width());
}
static PyObject *PyFT2Image_get_height(PyFT2Image *self, PyObject *args, PyObject *kwds)
{
if (PyErr_WarnEx(PyExc_FutureWarning,
"FT2Image.get_height is deprecated since Matplotlib 3.2 and "
"will be removed in Matplotlib 3.4; convert the FT2Image "
"to a NumPy array with np.asarray instead.",
1)) {
return NULL;
}
return PyLong_FromLong(self->x->get_height());
}
static int PyFT2Image_get_buffer(PyFT2Image *self, Py_buffer *buf, int flags)
{
FT2Image *im = self->x;
Py_INCREF(self);
buf->obj = (PyObject *)self;
buf->buf = im->get_buffer();
buf->len = im->get_width() * im->get_height();
buf->readonly = 0;
buf->format = (char *)"B";
buf->ndim = 2;
self->shape[0] = im->get_height();
self->shape[1] = im->get_width();
buf->shape = self->shape;
self->strides[0] = im->get_width();
self->strides[1] = 1;
buf->strides = self->strides;
buf->suboffsets = NULL;
buf->itemsize = 1;
buf->internal = NULL;
return 1;
}
static PyTypeObject PyFT2ImageType;
static PyTypeObject *PyFT2Image_init_type(PyObject *m, PyTypeObject *type)
{
static PyMethodDef methods[] = {
{"draw_rect", (PyCFunction)PyFT2Image_draw_rect, METH_VARARGS, PyFT2Image_draw_rect__doc__},
{"draw_rect_filled", (PyCFunction)PyFT2Image_draw_rect_filled, METH_VARARGS, PyFT2Image_draw_rect_filled__doc__},
{"as_str", (PyCFunction)PyFT2Image_as_str, METH_NOARGS, PyFT2Image_as_str__doc__},
{"as_rgba_str", (PyCFunction)PyFT2Image_as_rgba_str, METH_NOARGS, PyFT2Image_as_rgba_str__doc__},
{"as_array", (PyCFunction)PyFT2Image_as_array, METH_NOARGS, PyFT2Image_as_array__doc__},
{"get_width", (PyCFunction)PyFT2Image_get_width, METH_NOARGS, NULL},
{"get_height", (PyCFunction)PyFT2Image_get_height, METH_NOARGS, NULL},
{NULL}
};
static PyBufferProcs buffer_procs;
memset(&buffer_procs, 0, sizeof(PyBufferProcs));
buffer_procs.bf_getbuffer = (getbufferproc)PyFT2Image_get_buffer;
memset(type, 0, sizeof(PyTypeObject));
type->tp_name = "matplotlib.ft2font.FT2Image";
type->tp_basicsize = sizeof(PyFT2Image);
type->tp_dealloc = (destructor)PyFT2Image_dealloc;
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
type->tp_methods = methods;
type->tp_new = PyFT2Image_new;
type->tp_init = (initproc)PyFT2Image_init;
type->tp_as_buffer = &buffer_procs;
if (PyType_Ready(type) < 0) {
return NULL;
}
if (PyModule_AddObject(m, "FT2Image", (PyObject *)type)) {
return NULL;
}
return type;
}
/**********************************************************************
* Glyph
* */
typedef struct
{
PyObject_HEAD
size_t glyphInd;
long width;
long height;
long horiBearingX;
long horiBearingY;
long horiAdvance;
long linearHoriAdvance;
long vertBearingX;
long vertBearingY;
long vertAdvance;
FT_BBox bbox;
} PyGlyph;
static PyTypeObject PyGlyphType;
static PyObject *
PyGlyph_new(const FT_Face &face, const FT_Glyph &glyph, size_t ind, long hinting_factor)
{
PyGlyph *self;
self = (PyGlyph *)PyGlyphType.tp_alloc(&PyGlyphType, 0);
self->glyphInd = ind;
FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_subpixels, &self->bbox);
self->width = face->glyph->metrics.width / hinting_factor;
self->height = face->glyph->metrics.height;
self->horiBearingX = face->glyph->metrics.horiBearingX / hinting_factor;
self->horiBearingY = face->glyph->metrics.horiBearingY;
self->horiAdvance = face->glyph->metrics.horiAdvance;
self->linearHoriAdvance = face->glyph->linearHoriAdvance / hinting_factor;
self->vertBearingX = face->glyph->metrics.vertBearingX;
self->vertBearingY = face->glyph->metrics.vertBearingY;
self->vertAdvance = face->glyph->metrics.vertAdvance;
return (PyObject *)self;
}
static void PyGlyph_dealloc(PyGlyph *self)
{
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *PyGlyph_get_bbox(PyGlyph *self, void *closure)
{
return Py_BuildValue(
"llll", self->bbox.xMin, self->bbox.yMin, self->bbox.xMax, self->bbox.yMax);
}
static PyTypeObject *PyGlyph_init_type(PyObject *m, PyTypeObject *type)
{
static PyMemberDef members[] = {
{(char *)"width", T_LONG, offsetof(PyGlyph, width), READONLY, (char *)""},
{(char *)"height", T_LONG, offsetof(PyGlyph, height), READONLY, (char *)""},
{(char *)"horiBearingX", T_LONG, offsetof(PyGlyph, horiBearingX), READONLY, (char *)""},
{(char *)"horiBearingY", T_LONG, offsetof(PyGlyph, horiBearingY), READONLY, (char *)""},
{(char *)"horiAdvance", T_LONG, offsetof(PyGlyph, horiAdvance), READONLY, (char *)""},
{(char *)"linearHoriAdvance", T_LONG, offsetof(PyGlyph, linearHoriAdvance), READONLY, (char *)""},
{(char *)"vertBearingX", T_LONG, offsetof(PyGlyph, vertBearingX), READONLY, (char *)""},
{(char *)"vertBearingY", T_LONG, offsetof(PyGlyph, vertBearingY), READONLY, (char *)""},
{(char *)"vertAdvance", T_LONG, offsetof(PyGlyph, vertAdvance), READONLY, (char *)""},
{NULL}
};
static PyGetSetDef getset[] = {
{(char *)"bbox", (getter)PyGlyph_get_bbox, NULL, NULL, NULL},
{NULL}
};
memset(type, 0, sizeof(PyTypeObject));
type->tp_name = "matplotlib.ft2font.Glyph";
type->tp_basicsize = sizeof(PyGlyph);
type->tp_dealloc = (destructor)PyGlyph_dealloc;
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
type->tp_members = members;
type->tp_getset = getset;
if (PyType_Ready(type) < 0) {
return NULL;
}
/* Don't need to add to module, since you can't create glyphs
directly from Python */
return type;
}
/**********************************************************************
* FT2Font
* */
typedef struct
{
PyObject_HEAD
FT2Font *x;
PyObject *fname;
PyObject *py_file;
FILE *fp;
int close_file;
mpl_off_t offset;
FT_StreamRec stream;
FT_Byte *mem;
size_t mem_size;
Py_ssize_t shape[2];
Py_ssize_t strides[2];
Py_ssize_t suboffsets[2];
} PyFT2Font;
static unsigned long read_from_file_callback(FT_Stream stream,
unsigned long offset,
unsigned char *buffer,
unsigned long count)
{
PyFT2Font *def = (PyFT2Font *)stream->descriptor.pointer;
if (fseek(def->fp, offset, SEEK_SET) == -1) {
return 0;
}
if (count > 0) {
return fread(buffer, 1, count, def->fp);
}
return 0;
}
static void close_file_callback(FT_Stream stream)
{
PyFT2Font *def = (PyFT2Font *)stream->descriptor.pointer;
if (mpl_PyFile_DupClose(def->py_file, def->fp, def->offset)) {
throw std::runtime_error("Couldn't close file");
}
if (def->close_file) {
mpl_PyFile_CloseFile(def->py_file);
}
Py_DECREF(def->py_file);
def->py_file = NULL;
}
static int convert_open_args(PyFT2Font *self, PyObject *py_file_arg, FT_Open_Args *open_args)
{
PyObject *py_file = NULL;
int close_file = 0;
FILE *fp;
PyObject *data = NULL;
char *data_ptr;
Py_ssize_t data_len;
long file_size;
FT_Byte *new_memory;
mpl_off_t offset = 0;
int result = 0;
memset((void *)open_args, 0, sizeof(FT_Open_Args));
if (PyBytes_Check(py_file_arg) || PyUnicode_Check(py_file_arg)) {
if ((py_file = mpl_PyFile_OpenFile(py_file_arg, (char *)"rb")) == NULL) {
goto exit;
}
close_file = 1;
} else {
Py_INCREF(py_file_arg);
py_file = py_file_arg;
}
if ((fp = mpl_PyFile_Dup(py_file, (char *)"rb", &offset))) {
Py_INCREF(py_file);
self->py_file = py_file;
self->close_file = close_file;
self->fp = fp;
self->offset = offset;
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
self->stream.base = NULL;
self->stream.size = (unsigned long)file_size;
self->stream.pos = 0;
self->stream.descriptor.pointer = self;
self->stream.read = &read_from_file_callback;
self->stream.close = &close_file_callback;
open_args->flags = FT_OPEN_STREAM;
open_args->stream = &self->stream;
} else {
if (PyObject_HasAttrString(py_file_arg, "read") &&
(data = PyObject_CallMethod(py_file_arg, (char *)"read", (char *)""))) {
if (PyBytes_AsStringAndSize(data, &data_ptr, &data_len)) {
goto exit;
}
if (self->mem) {
free(self->mem);
}
self->mem = (FT_Byte *)malloc((self->mem_size + data_len) * sizeof(FT_Byte));
if (self->mem == NULL) {
goto exit;
}
new_memory = self->mem + self->mem_size;
self->mem_size += data_len;
memcpy(new_memory, data_ptr, data_len);
open_args->flags = FT_OPEN_MEMORY;
open_args->memory_base = new_memory;
open_args->memory_size = data_len;
open_args->stream = NULL;
} else {
PyErr_SetString(PyExc_TypeError,
"First argument must be a path or file object reading bytes");
goto exit;
}
}
result = 1;
exit:
Py_XDECREF(py_file);
Py_XDECREF(data);
return result;
}
static PyTypeObject PyFT2FontType;
static PyObject *PyFT2Font_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyFT2Font *self;
self = (PyFT2Font *)type->tp_alloc(type, 0);
self->x = NULL;
self->fname = NULL;
self->py_file = NULL;
self->fp = NULL;
self->close_file = 0;
self->offset = 0;
memset(&self->stream, 0, sizeof(FT_StreamRec));
self->mem = 0;
self->mem_size = 0;
return (PyObject *)self;
}
const char *PyFT2Font_init__doc__ =
"FT2Font(ttffile)\n"
"\n"
"Create a new FT2Font object\n"
"The following global font attributes are defined:\n"
" num_faces number of faces in file\n"
" face_flags face flags (int type); see the ft2font constants\n"
" style_flags style flags (int type); see the ft2font constants\n"
" num_glyphs number of glyphs in the face\n"
" family_name face family name\n"
" style_name face style name\n"
" num_fixed_sizes number of bitmap in the face\n"
" scalable face is scalable\n"
"\n"
"The following are available, if scalable is true:\n"
" bbox face global bounding box (xmin, ymin, xmax, ymax)\n"
" units_per_EM number of font units covered by the EM\n"
" ascender ascender in 26.6 units\n"
" descender descender in 26.6 units\n"
" height height in 26.6 units; used to compute a default\n"
" line spacing (baseline-to-baseline distance)\n"
" max_advance_width maximum horizontal cursor advance for all glyphs\n"
" max_advance_height same for vertical layout\n"
" underline_position vertical position of the underline bar\n"
" underline_thickness vertical thickness of the underline\n"
" postscript_name PostScript name of the font\n";
static void PyFT2Font_fail(PyFT2Font *self)
{
free(self->mem);
self->mem = NULL;
Py_XDECREF(self->py_file);
self->py_file = NULL;
}
static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
PyObject *fname;
FT_Open_Args open_args;
long hinting_factor = 8;
int kerning_factor = 0;
const char *names[] = { "filename", "hinting_factor", "_kerning_factor", NULL };
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "O|l$i:FT2Font", (char **)names, &fname,
&hinting_factor, &kerning_factor)) {
return -1;
}
if (!convert_open_args(self, fname, &open_args)) {
return -1;
}
CALL_CPP_FULL(
"FT2Font", (self->x = new FT2Font(open_args, hinting_factor)), PyFT2Font_fail(self), -1);
CALL_CPP_INIT("FT2Font->set_kerning_factor", (self->x->set_kerning_factor(kerning_factor)));
Py_INCREF(fname);
self->fname = fname;
return 0;
}
static void PyFT2Font_dealloc(PyFT2Font *self)
{
delete self->x;
free(self->mem);
Py_XDECREF(self->py_file);
Py_XDECREF(self->fname);
Py_TYPE(self)->tp_free((PyObject *)self);
}
const char *PyFT2Font_clear__doc__ =
"clear()\n"
"\n"
"Clear all the glyphs, reset for a new set_text";
static PyObject *PyFT2Font_clear(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
CALL_CPP("clear", (self->x->clear()));
Py_RETURN_NONE;
}
const char *PyFT2Font_set_size__doc__ =
"set_size(ptsize, dpi)\n"
"\n"
"Set the point size and dpi of the text.\n";
static PyObject *PyFT2Font_set_size(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
double ptsize;
double dpi;
if (!PyArg_ParseTuple(args, "dd:set_size", &ptsize, &dpi)) {
return NULL;
}
CALL_CPP("set_size", (self->x->set_size(ptsize, dpi)));
Py_RETURN_NONE;
}
const char *PyFT2Font_set_charmap__doc__ =
"set_charmap(i)\n"
"\n"
"Make the i-th charmap current\n";
static PyObject *PyFT2Font_set_charmap(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
int i;
if (!PyArg_ParseTuple(args, "i:set_charmap", &i)) {
return NULL;
}
CALL_CPP("set_charmap", (self->x->set_charmap(i)));
Py_RETURN_NONE;
}
const char *PyFT2Font_select_charmap__doc__ =
"select_charmap(i)\n"
"\n"
"select charmap i where i is one of the FT_Encoding number\n";
static PyObject *PyFT2Font_select_charmap(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
unsigned long i;
if (!PyArg_ParseTuple(args, "k:select_charmap", &i)) {
return NULL;
}
CALL_CPP("select_charmap", self->x->select_charmap(i));
Py_RETURN_NONE;
}
const char *PyFT2Font_get_kerning__doc__ =
"dx = get_kerning(left, right, mode)\n"
"\n"
"Get the kerning between left char and right glyph indices\n"
"mode is a kerning mode constant\n"
" KERNING_DEFAULT - Return scaled and grid-fitted kerning distances\n"
" KERNING_UNFITTED - Return scaled but un-grid-fitted kerning distances\n"
" KERNING_UNSCALED - Return the kerning vector in original font units\n";
static PyObject *PyFT2Font_get_kerning(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
FT_UInt left, right, mode;
int result;
if (!PyArg_ParseTuple(args, "III:get_kerning", &left, &right, &mode)) {
return NULL;
}
CALL_CPP("get_kerning", (result = self->x->get_kerning(left, right, mode)));
return PyLong_FromLong(result);
}
const char *PyFT2Font_set_text__doc__ =
"set_text(s, angle)\n"
"\n"
"Set the text string and angle.\n"
"You must call this before draw_glyphs_to_bitmap\n"
"A sequence of x,y positions is returned";
static PyObject *PyFT2Font_set_text(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
PyObject *textobj;
double angle = 0.0;
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
std::vector<double> xys;
const char *names[] = { "string", "angle", "flags", NULL };
/* This makes a technically incorrect assumption that FT_Int32 is
int. In theory it can also be long, if the size of int is less
than 32 bits. This is very unlikely on modern platforms. */
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "O|di:set_text", (char **)names, &textobj, &angle, &flags)) {
return NULL;
}
std::vector<uint32_t> codepoints;
size_t size;
if (PyUnicode_Check(textobj)) {
size = PyUnicode_GET_SIZE(textobj);
codepoints.resize(size);
Py_UNICODE *unistr = PyUnicode_AsUnicode(textobj);
for (size_t i = 0; i < size; ++i) {
codepoints[i] = unistr[i];
}
} else if (PyBytes_Check(textobj)) {
size = PyBytes_Size(textobj);
codepoints.resize(size);
char *bytestr = PyBytes_AsString(textobj);
for (size_t i = 0; i < size; ++i) {
codepoints[i] = bytestr[i];
}
} else {
PyErr_SetString(PyExc_TypeError, "String must be str or bytes");
return NULL;
}
uint32_t* codepoints_array = NULL;
if (size > 0) {
codepoints_array = &codepoints[0];
}
CALL_CPP("set_text", self->x->set_text(size, codepoints_array, angle, flags, xys));
return convert_xys_to_array(xys);
}
const char *PyFT2Font_get_num_glyphs__doc__ =
"get_num_glyphs()\n"
"\n"
"Return the number of loaded glyphs\n";
static PyObject *PyFT2Font_get_num_glyphs(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
return PyLong_FromLong(self->x->get_num_glyphs());
}
const char *PyFT2Font_load_char__doc__ =
"load_char(charcode, flags=LOAD_FORCE_AUTOHINT)\n"
"\n"
"Load character with charcode in current fontfile and set glyph.\n"
"The flags argument can be a bitwise-or of the LOAD_XXX constants.\n"
"Return value is a Glyph object, with attributes\n"
" width # glyph width\n"
" height # glyph height\n"
" bbox # the glyph bbox (xmin, ymin, xmax, ymax)\n"
" horiBearingX # left side bearing in horizontal layouts\n"
" horiBearingY # top side bearing in horizontal layouts\n"
" horiAdvance # advance width for horizontal layout\n"
" vertBearingX # left side bearing in vertical layouts\n"
" vertBearingY # top side bearing in vertical layouts\n"
" vertAdvance # advance height for vertical layout\n";
static PyObject *PyFT2Font_load_char(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
long charcode;
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
const char *names[] = { "charcode", "flags", NULL };
/* This makes a technically incorrect assumption that FT_Int32 is
int. In theory it can also be long, if the size of int is less
than 32 bits. This is very unlikely on modern platforms. */
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "l|i:load_char", (char **)names, &charcode, &flags)) {
return NULL;
}
CALL_CPP("load_char", (self->x->load_char(charcode, flags)));
return PyGlyph_new(self->x->get_face(),
self->x->get_last_glyph(),
self->x->get_last_glyph_index(),
self->x->get_hinting_factor());
}
const char *PyFT2Font_load_glyph__doc__ =
"load_glyph(glyphindex, flags=LOAD_FORCE_AUTOHINT)\n"
"\n"
"Load character with glyphindex in current fontfile and set glyph.\n"
"The flags argument can be a bitwise-or of the LOAD_XXX constants.\n"
"Return value is a Glyph object, with attributes\n"
" width # glyph width\n"
" height # glyph height\n"
" bbox # the glyph bbox (xmin, ymin, xmax, ymax)\n"
" horiBearingX # left side bearing in horizontal layouts\n"
" horiBearingY # top side bearing in horizontal layouts\n"
" horiAdvance # advance width for horizontal layout\n"
" vertBearingX # left side bearing in vertical layouts\n"
" vertBearingY # top side bearing in vertical layouts\n"
" vertAdvance # advance height for vertical layout\n";
static PyObject *PyFT2Font_load_glyph(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
FT_UInt glyph_index;
FT_Int32 flags = FT_LOAD_FORCE_AUTOHINT;
const char *names[] = { "glyph_index", "flags", NULL };
/* This makes a technically incorrect assumption that FT_Int32 is
int. In theory it can also be long, if the size of int is less
than 32 bits. This is very unlikely on modern platforms. */
if (!PyArg_ParseTupleAndKeywords(
args, kwds, "I|i:load_glyph", (char **)names, &glyph_index, &flags)) {
return NULL;
}
CALL_CPP("load_glyph", (self->x->load_glyph(glyph_index, flags)));
return PyGlyph_new(self->x->get_face(),
self->x->get_last_glyph(),
self->x->get_last_glyph_index(),
self->x->get_hinting_factor());
}
const char *PyFT2Font_get_width_height__doc__ =
"w, h = get_width_height()\n"
"\n"
"Get the width and height in 26.6 subpixels of the current string set by set_text\n"
"The rotation of the string is accounted for. To get width and height\n"
"in pixels, divide these values by 64\n";
static PyObject *PyFT2Font_get_width_height(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
long width, height;
CALL_CPP("get_width_height", (self->x->get_width_height(&width, &height)));
return Py_BuildValue("ll", width, height);
}
const char *PyFT2Font_get_bitmap_offset__doc__ =
"x, y = get_bitmap_offset()\n"
"\n"
"Get the offset in 26.6 subpixels for the bitmap if ink hangs left or below (0, 0).\n"
"Since matplotlib only supports left-to-right text, y is always 0.\n";
static PyObject *PyFT2Font_get_bitmap_offset(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
long x, y;
CALL_CPP("get_bitmap_offset", (self->x->get_bitmap_offset(&x, &y)));
return Py_BuildValue("ll", x, y);
}
const char *PyFT2Font_get_descent__doc__ =
"d = get_descent()\n"
"\n"
"Get the descent of the current string set by set_text in 26.6 subpixels.\n"
"The rotation of the string is accounted for. To get the descent\n"
"in pixels, divide this value by 64.\n";
static PyObject *PyFT2Font_get_descent(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
long descent;
CALL_CPP("get_descent", (descent = self->x->get_descent()));
return PyLong_FromLong(descent);
}
const char *PyFT2Font_draw_glyphs_to_bitmap__doc__ =
"draw_glyphs_to_bitmap()\n"
"\n"
"Draw the glyphs that were loaded by set_text to the bitmap\n"
"The bitmap size will be automatically set to include the glyphs\n";
static PyObject *PyFT2Font_draw_glyphs_to_bitmap(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
bool antialiased = true;
const char *names[] = { "antialiased", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&:draw_glyphs_to_bitmap",
(char **)names, &convert_bool, &antialiased)) {
return NULL;
}
CALL_CPP("draw_glyphs_to_bitmap", (self->x->draw_glyphs_to_bitmap(antialiased)));
Py_RETURN_NONE;
}
const char *PyFT2Font_get_xys__doc__ =
"get_xys()\n"
"\n"
"Get the xy locations of the current glyphs\n";
static PyObject *PyFT2Font_get_xys(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
bool antialiased = true;
std::vector<double> xys;
const char *names[] = { "antialiased", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&:get_xys",
(char **)names, &convert_bool, &antialiased)) {
return NULL;
}
CALL_CPP("get_xys", (self->x->get_xys(antialiased, xys)));
return convert_xys_to_array(xys);
}
const char *PyFT2Font_draw_glyph_to_bitmap__doc__ =
"draw_glyph_to_bitmap(bitmap, x, y, glyph)\n"
"\n"
"Draw a single glyph to the bitmap at pixel locations x,y\n"
"Note it is your responsibility to set up the bitmap manually\n"
"with set_bitmap_size(w,h) before this call is made.\n"
"\n"
"If you want automatic layout, use set_text in combinations with\n"
"draw_glyphs_to_bitmap. This function is intended for people who\n"
"want to render individual glyphs at precise locations, eg, a\n"
"a glyph returned by load_char\n";
static PyObject *PyFT2Font_draw_glyph_to_bitmap(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
PyFT2Image *image;
double xd, yd;
PyGlyph *glyph;
bool antialiased = true;
const char *names[] = { "image", "x", "y", "glyph", "antialiased", NULL };
if (!PyArg_ParseTupleAndKeywords(args,
kwds,
"O!ddO!|O&:draw_glyph_to_bitmap",
(char **)names,
&PyFT2ImageType,
&image,
&xd,
&yd,
&PyGlyphType,
&glyph,
&convert_bool,
&antialiased)) {
return NULL;
}
CALL_CPP("draw_glyph_to_bitmap",
self->x->draw_glyph_to_bitmap(*(image->x), xd, yd, glyph->glyphInd, antialiased));
Py_RETURN_NONE;
}
const char *PyFT2Font_get_glyph_name__doc__ =
"get_glyph_name(index)\n"
"\n"
"Retrieves the ASCII name of a given glyph in a face.\n"
"\n"
"Due to Matplotlib's internal design, for fonts that do not contain glyph \n"
"names (per FT_FACE_FLAG_GLYPH_NAMES), this returns a made-up name which \n"
"does *not* roundtrip through `.get_name_index`.\n";
static PyObject *PyFT2Font_get_glyph_name(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
unsigned int glyph_number;
char buffer[128];
if (!PyArg_ParseTuple(args, "I:get_glyph_name", &glyph_number)) {
return NULL;
}
CALL_CPP("get_glyph_name", (self->x->get_glyph_name(glyph_number, buffer)));
return PyUnicode_FromString(buffer);
}
const char *PyFT2Font_get_charmap__doc__ =
"get_charmap()\n"
"\n"
"Returns a dictionary that maps the character codes of the selected charmap\n"
"(Unicode by default) to their corresponding glyph indices.\n";
static PyObject *PyFT2Font_get_charmap(PyFT2Font *self, PyObject *args, PyObject *kwds)
{
PyObject *charmap;
if (!(charmap = PyDict_New())) {
return NULL;
}
FT_UInt index;
FT_ULong code = FT_Get_First_Char(self->x->get_face(), &index);
while (index != 0) {
PyObject *key = NULL, *val = NULL;
bool error = (!(key = PyLong_FromLong(code))
|| !(val = PyLong_FromLong(index))
|| (PyDict_SetItem(charmap, key, val) == -1));
Py_XDECREF(key);
Py_XDECREF(val);
if (error) {
Py_DECREF(charmap);
return NULL;
}
code = FT_Get_Next_Char(self->x->get_face(), code, &index);
}
return charmap;
}
const char *PyFT2Font_get_char_index__doc__ =