python_freetype
python_freetype copied to clipboard
Simply wrong code with cairo format_A1 for Bitmap.make_image_surface
The code around line 3315 for Bitmap.make_image_surface is simply wrong:
...
if self.pixel_mode == FT.PIXEL_MODE_MONO :
cairo_format = cairo.FORMAT_A1
elif self.pixel_mode == FT.PIXEL_MODE_GRAY :
cairo_format = cairo.FORMAT_A8
...
Freetype pixel mode mono is most-significant bit first, while cairo format A1 is host order, and least-significant-bit first on small-endian platform, such as x86/x86_64.

Here is how the bug looks like.
I have fixed this in my own code with libtiff successfully, though this seems non-ideal:
# pillow/PIL itself requires libtiff so it is assumed to be around.
# swap the bit-order from freetype's (MSB) to cairo's (host order) if needed
if ( ( byteorder == 'little' ) and (pixel_mode == FT_PIXEL_MODE_MONO ) ):
libtiff = CDLL("libtiff.so")
libtiff.TIFFReverseBits.restype = None
libtiff.TIFFReverseBits.argtypes = (c_void_p, c_int)
libtiff.TIFFReverseBits(buffer.buffer_info()[0], buffer_size)
https://github.com/ldo/python_freetype_examples/issues/1