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

Skip to content

Commit 8ba1981

Browse files
committed
Move FT2Font to new PyCXX extension type API
1 parent 0d78abb commit 8ba1981

3 files changed

Lines changed: 38 additions & 27 deletions

File tree

src/_backend_agg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,8 @@ RendererAgg::draw_text_image(const Py::Tuple& args)
918918
}
919919
else
920920
{
921-
FT2Image *image = dynamic_cast<FT2Image*>(Py::getPythonExtensionBase(image_obj.ptr()));
921+
FT2Image* image = static_cast<FT2Image *>(
922+
Py::getPythonExtensionBase(image_obj.ptr()));
922923
if (!image->get_buffer())
923924
{
924925
throw Py::ValueError(

src/ft2font.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ FT2Image::FT2Image(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwd
6060
_isDirty(true),
6161
_buffer(NULL),
6262
_width(0), _height(0),
63-
_rgbCopy(NULL),
64-
_rgbaCopy(NULL)
63+
_rgbCopy(),
64+
_rgbaCopy()
6565
{
6666
_VERBOSE("FT2Image::FT2Image");
6767

@@ -72,6 +72,12 @@ FT2Image::FT2Image(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwd
7272
resize(width, height);
7373
}
7474

75+
FT2Image::~FT2Image() {
76+
delete [] _buffer;
77+
_buffer = NULL;
78+
}
79+
80+
7581
Py::PythonClassObject<FT2Image> FT2Image::factory(int width, int height)
7682
{
7783
Py::Callable class_type(type());
@@ -343,6 +349,8 @@ PYCXX_VARARGS_METHOD_DECL(FT2Image, py_as_array)
343349
void
344350
FT2Image::makeRgbCopy()
345351
{
352+
FT2Image* rgbCopy;
353+
346354
if (!_isDirty)
347355
{
348356
return;
@@ -351,14 +359,16 @@ FT2Image::makeRgbCopy()
351359
if (!_rgbCopy.ptr())
352360
{
353361
_rgbCopy = factory(_width * 3, _height);
362+
rgbCopy = Py::PythonClassObject<FT2Image>(_rgbCopy).getCxxObject();
354363
}
355364
else
356365
{
357-
_rgbCopy.getCxxObject()->resize(_width * 3, _height);
366+
rgbCopy = Py::PythonClassObject<FT2Image>(_rgbCopy).getCxxObject();
367+
rgbCopy->resize(_width * 3, _height);
358368
}
359369
unsigned char *src = _buffer;
360370
unsigned char *src_end = src + (_width * _height);
361-
unsigned char *dst = _rgbCopy.getCxxObject()->_buffer;
371+
unsigned char *dst = rgbCopy->_buffer;
362372

363373
unsigned char tmp;
364374
while (src != src_end)
@@ -384,12 +394,14 @@ FT2Image::py_as_rgb_str(const Py::Tuple & args)
384394

385395
makeRgbCopy();
386396

387-
return _rgbCopy.getCxxObject()->py_as_str(args);
397+
return Py::PythonClassObject<FT2Image>(_rgbCopy).getCxxObject()->py_as_str(args);
388398
}
389399
PYCXX_VARARGS_METHOD_DECL(FT2Image, py_as_rgb_str)
390400

391401
void FT2Image::makeRgbaCopy()
392402
{
403+
FT2Image* rgbaCopy;
404+
393405
if (!_isDirty)
394406
{
395407
return;
@@ -398,14 +410,16 @@ void FT2Image::makeRgbaCopy()
398410
if (_rgbaCopy.ptr())
399411
{
400412
_rgbaCopy = factory(_width * 4, _height);
413+
rgbaCopy = Py::PythonClassObject<FT2Image>(_rgbaCopy).getCxxObject();
401414
}
402415
else
403416
{
404-
_rgbaCopy.getCxxObject()->resize(_width * 4, _height);
417+
rgbaCopy = Py::PythonClassObject<FT2Image>(_rgbaCopy).getCxxObject();
418+
rgbaCopy->resize(_width * 4, _height);
405419
}
406420
unsigned char *src = _buffer;
407421
unsigned char *src_end = src + (_width * _height);
408-
unsigned char *dst = _rgbaCopy.getCxxObject()->_buffer;
422+
unsigned char *dst = rgbaCopy->_buffer;
409423

410424
while (src != src_end)
411425
{
@@ -430,7 +444,7 @@ FT2Image::py_as_rgba_str(const Py::Tuple & args)
430444

431445
makeRgbaCopy();
432446

433-
return _rgbaCopy.getCxxObject()->py_as_str(args);
447+
return Py::PythonClassObject<FT2Image>(_rgbaCopy).getCxxObject()->py_as_str(args);
434448
}
435449
PYCXX_VARARGS_METHOD_DECL(FT2Image, py_as_rgba_str)
436450

@@ -454,7 +468,8 @@ FT2Image::py_get_height(const Py::Tuple & args)
454468
}
455469
PYCXX_VARARGS_METHOD_DECL(FT2Image, py_get_height)
456470

457-
Glyph* Glyph::factory(const FT_Face& face, const FT_Glyph& glyph, size_t ind)
471+
Py::PythonClassObject<Glyph> Glyph::factory(
472+
const FT_Face& face, const FT_Glyph& glyph, size_t ind)
458473
{
459474
Py::Callable class_type(type());
460475
Py::PythonClassObject<Glyph> obj = Py::PythonClassObject<Glyph>(
@@ -485,7 +500,7 @@ Glyph* Glyph::factory(const FT_Face& face, const FT_Glyph& glyph, size_t ind)
485500
o->setattro("bbox", abbox);
486501
o->setattro("path", o->get_path(face));
487502

488-
return o;
503+
return obj;
489504
}
490505

491506
Glyph::~Glyph()
@@ -769,7 +784,7 @@ Glyph::get_path(const FT_Face& face)
769784

770785
FT2Font::FT2Font(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds) :
771786
Py::PythonClass<FT2Font>::PythonClass(self, args, kwds),
772-
image(NULL)
787+
image()
773788
{
774789
args.verify_length(1);
775790
std::string facefile = Py::String(args[0]);
@@ -1283,8 +1298,7 @@ FT2Font::load_char(const Py::Tuple & args, const Py::Dict & kwargs)
12831298
12841299
size_t num = glyphs.size(); //the index into the glyphs list
12851300
glyphs.push_back(thisGlyph);
1286-
Glyph* gm = Glyph::factory(face, thisGlyph, num);
1287-
return Py::asObject(gm);
1301+
return Glyph::factory(face, thisGlyph, num);
12881302
}
12891303
PYCXX_KEYWORDS_METHOD_DECL(FT2Font, load_char)
12901304
@@ -1334,8 +1348,7 @@ FT2Font::load_glyph(const Py::Tuple & args, const Py::Dict & kwargs)
13341348
13351349
size_t num = glyphs.size(); //the index into the glyphs list
13361350
glyphs.push_back(thisGlyph);
1337-
Glyph* gm = Glyph::factory(face, thisGlyph, num);
1338-
return Py::asObject(gm);
1351+
return Glyph::factory(face, thisGlyph, num);
13391352
}
13401353
PYCXX_KEYWORDS_METHOD_DECL(FT2Font, load_glyph)
13411354
@@ -1420,7 +1433,8 @@ FT2Font::draw_glyphs_to_bitmap(const Py::Tuple & args)
14201433
FT_Int x = (FT_Int)(bitmap->left - (string_bbox.xMin / 64.));
14211434
FT_Int y = (FT_Int)((string_bbox.yMax / 64.) - bitmap->top + 1);
14221435
1423-
image.getCxxObject()->draw_bitmap(&bitmap->bitmap, x, y);
1436+
FT2Image* image_cxx = Py::PythonClassObject<FT2Image>(image).getCxxObject();
1437+
image_cxx->draw_bitmap(&bitmap->bitmap, x, y);
14241438
}
14251439
14261440
return Py::Object();
@@ -1973,7 +1987,7 @@ Py::Object
19731987
FT2Font::get_image(const Py::Tuple &args)
19741988
{
19751989
args.verify_length(0);
1976-
if (image.ptr())
1990+
if (!image.isNone())
19771991
{
19781992
return image;
19791993
}

src/ft2font.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ extern "C"
2525
class FT2Image : public Py::PythonClass<FT2Image>
2626
{
2727
public:
28-
// FT2Image();
2928
FT2Image(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds);
30-
virtual ~FT2Image() {
31-
delete [] _buffer;
32-
_buffer = NULL;
33-
}
29+
virtual ~FT2Image();
3430
static Py::PythonClassObject<FT2Image> factory(int width, int height);
3531

3632
static void init_type();
@@ -78,8 +74,8 @@ class FT2Image : public Py::PythonClass<FT2Image>
7874
unsigned char *_buffer;
7975
unsigned long _width;
8076
unsigned long _height;
81-
Py::PythonClassObject<FT2Image> _rgbCopy;
82-
Py::PythonClassObject<FT2Image> _rgbaCopy;
77+
Py::Object _rgbCopy;
78+
Py::Object _rgbaCopy;
8379

8480
void makeRgbCopy();
8581
void makeRgbaCopy();
@@ -93,7 +89,7 @@ class Glyph : public Py::PythonClass<Glyph>
9389
Glyph(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds) :
9490
Py::PythonClass<Glyph>::PythonClass(self, args, kwds) { }
9591
virtual ~Glyph();
96-
static Glyph* factory(const FT_Face&, const FT_Glyph&, size_t);
92+
static Py::PythonClassObject<Glyph> factory(const FT_Face&, const FT_Glyph&, size_t);
9793
int setattro(const Py::String &name, const Py::Object &value);
9894
Py::Object getattro(const Py::String &name);
9995
static void init_type(void);
@@ -138,7 +134,7 @@ class FT2Font : public Py::PythonClass<FT2Font>
138134
Py::Object attach_file(const Py::Tuple & args);
139135
int setattro(const Py::String &name, const Py::Object &value);
140136
Py::Object getattro(const Py::String &name);
141-
Py::PythonClassObject<FT2Image> image;
137+
Py::Object image;
142138

143139
private:
144140
Py::Dict __dict__;

0 commit comments

Comments
 (0)