def make_image_surface(bitmap, copy=True):
    if (type(bitmap) == FT_Bitmap):
        # bare FT_Bitmap
        content = bitmap
    else:
        # wrapped Bitmap instance
        content = bitmap._FT_Bitmap
    "creates a Cairo ImageSurface containing (a copy of) the Bitmap pixels."
    if content.pixel_mode == FT_PIXEL_MODE_MONO:
        cairo_format = FORMAT_A1
    elif content.pixel_mode == FT_PIXEL_MODE_GRAY:
        cairo_format = FORMAT_A8
    else:
        raise NotImplementedError("unsupported bitmap format %d" %
                                  content.pixel_mode)
    src_pitch = content.pitch
    dst_pitch = ImageSurface.format_stride_for_width(cairo_format,
                                                     content.width)
    if not copy and dst_pitch == src_pitch and content.buffer != None:
        pixels = content.buffer
    else:
        pixels = to_array(content, content.pixel_mode, dst_pitch)
    result = ImageSurface.create_for_data(pixels, cairo_format, content.width,
                                          content.rows, dst_pitch)
    return result
示例#2
0
def _make_image_surface(bitmap, copy=True):
    """Convert FreeType bitmap to Cairo ImageSurface.

    Special thanks to Hintak and his example code:
    https://github.com/rougier/freetype-py/blob/master/examples/bitmap_to_surface.py


    TODO (M Foley) understand this better and see if a more elegant
    solution exists."""
    content = bitmap._FT_Bitmap
    cairo_format = FORMAT_A8

    src_pitch = content.pitch
    dst_pitch = ImageSurface.format_stride_for_width(cairo_format,
                                                     content.width)

    pixels = _to_array(content, content.pixel_mode, dst_pitch)
    result = ImageSurface.create_for_data(pixels, cairo_format, content.width,
                                          content.rows, dst_pitch)
    return result
示例#3
0
def make_image_surface(bitmap, copy=True):
    if (type(bitmap) == FT_Bitmap):
        # bare FT_Bitmap
        content = bitmap
    else:
        # wrapped Bitmap instance
        content = bitmap._FT_Bitmap
    "creates a Cairo ImageSurface containing (a copy of) the Bitmap pixels."
    if (False and (byteorder == 'little')
            and (content.pixel_mode == FT_PIXEL_MODE_MONO)):
        try:
            libtiff = CDLL("not")
        except:
            library = get_handle()
            dest = c_void_p()
            error = FT_Bitmap_Init(dest)
            if error: raise FT_Exception(error)
            error = FT_Bitmap_Convert(library, byref(content), dest, 1)
            if error: raise FT_Exception(error)
            content = dest
    if content.pixel_mode == FT_PIXEL_MODE_MONO:
        cairo_format = FORMAT_A1
    elif content.pixel_mode == FT_PIXEL_MODE_GRAY:
        cairo_format = FORMAT_A8
    else:
        raise NotImplementedError("unsupported bitmap format %d" %
                                  content.pixel_mode)
    src_pitch = content.pitch
    dst_pitch = ImageSurface.format_stride_for_width(cairo_format,
                                                     content.width)
    if not copy and dst_pitch == src_pitch and content.buffer != None:
        pixels = content.buffer
    else:
        pixels = to_array(content, content.pixel_mode, dst_pitch)
    result = ImageSurface.create_for_data(pixels, cairo_format, content.width,
                                          content.rows, dst_pitch)
    return result