Esempio n. 1
0
    def create_context(self) -> Context:
        """
        Creates a Pango ``Context`` connected to ``fontmap``. This is
        equivalent to ``pango.Context()`` followed by
        ``context.set_font_map()``.

        :return:
            the newly allocated Pango ``Context``.
        """
        context_pointer = pango.pango_font_map_create_context(
            self.get_pointer())
        return Context.from_pointer(context_pointer, gc=True)
Esempio n. 2
0
    def get_pango_context() -> Context:
        ffi = FFI()
        ffi.include(ffi_builder)
        ffi.cdef('''
            /* Cairo */
            typedef void cairo_t;
            typedef struct _cairo_surface cairo_surface_t;

            typedef enum _cairo_status {
                CAIRO_STATUS_SUCCESS = 0,

                CAIRO_STATUS_NO_MEMORY,
                CAIRO_STATUS_INVALID_RESTORE,
                CAIRO_STATUS_INVALID_POP_GROUP,
                CAIRO_STATUS_NO_CURRENT_POINT,
                CAIRO_STATUS_INVALID_MATRIX,
                CAIRO_STATUS_INVALID_STATUS,
                CAIRO_STATUS_NULL_POINTER,
                CAIRO_STATUS_INVALID_STRING,
                CAIRO_STATUS_INVALID_PATH_DATA,
                CAIRO_STATUS_READ_ERROR,
                CAIRO_STATUS_WRITE_ERROR,
                CAIRO_STATUS_SURFACE_FINISHED,
                CAIRO_STATUS_SURFACE_TYPE_MISMATCH,
                CAIRO_STATUS_PATTERN_TYPE_MISMATCH,
                CAIRO_STATUS_INVALID_CONTENT,
                CAIRO_STATUS_INVALID_FORMAT,
                CAIRO_STATUS_INVALID_VISUAL,
                CAIRO_STATUS_FILE_NOT_FOUND,
                CAIRO_STATUS_INVALID_DASH,
                CAIRO_STATUS_INVALID_DSC_COMMENT,
                CAIRO_STATUS_INVALID_INDEX,
                CAIRO_STATUS_CLIP_NOT_REPRESENTABLE,
                CAIRO_STATUS_TEMP_FILE_ERROR,
                CAIRO_STATUS_INVALID_STRIDE,
                CAIRO_STATUS_FONT_TYPE_MISMATCH,
                CAIRO_STATUS_USER_FONT_IMMUTABLE,
                CAIRO_STATUS_USER_FONT_ERROR,
                CAIRO_STATUS_NEGATIVE_COUNT,
                CAIRO_STATUS_INVALID_CLUSTERS,
                CAIRO_STATUS_INVALID_SLANT,
                CAIRO_STATUS_INVALID_WEIGHT,
                CAIRO_STATUS_INVALID_SIZE,
                CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED,
                CAIRO_STATUS_DEVICE_TYPE_MISMATCH,
                CAIRO_STATUS_DEVICE_ERROR,
                CAIRO_STATUS_INVALID_MESH_CONSTRUCTION,
                CAIRO_STATUS_DEVICE_FINISHED,
                CAIRO_STATUS_JBIG2_GLOBAL_MISSING,
                CAIRO_STATUS_PNG_ERROR,
                CAIRO_STATUS_FREETYPE_ERROR,
                CAIRO_STATUS_WIN32_GDI_ERROR,
                CAIRO_STATUS_TAG_ERROR,

                CAIRO_STATUS_LAST_STATUS
            } cairo_status_t;

            typedef cairo_status_t (*cairo_write_func_t) (
                void * closure,
                const unsigned char *data,
                unsigned int length
            );
            cairo_surface_t * cairo_pdf_surface_create_for_stream (
                cairo_write_func_t write_func,
                void *closure,
                double width_in_points,
                double height_in_points
            );
            void cairo_surface_destroy (cairo_surface_t *surface);
            cairo_t * cairo_create (cairo_surface_t *target);
            void cairo_destroy (cairo_t *cr);

            PangoContext * pango_cairo_create_context (cairo_t *cr);
        ''')
        ffi.set_source('pangocffi._generated.ffi', None)
        cairo = ffi.dlopen('cairo')
        pangocairo = ffi.dlopen('pangocairo-1.0')

        cairo_surface_t = cairo.cairo_pdf_surface_create_for_stream(
            ffi.NULL, ffi.NULL, 10, 10)
        cairo_t = cairo.cairo_create(cairo_surface_t)

        pango_pointer = pangocairo.pango_cairo_create_context(cairo_t)
        pango_pointer = pangocffi.ffi.cast('PangoContext *', pango_pointer)
        pango_pointer = pangocffi.ffi.gc(pango_pointer,
                                         pangocffi.gobject.g_object_unref)

        ContextCreator.cairo = cairo
        ContextCreator.cairo_surface_t = cairo_surface_t
        ContextCreator.cairo_t = cairo_t
        ContextCreator.pango_context = Context.from_pointer(pango_pointer)

        return ContextCreator.pango_context
Esempio n. 3
0
 def test_context_returns_null_from_null_pointer(self):
     with self.assertRaises(ValueError):
         Context.from_pointer(ffi.NULL)
Esempio n. 4
0
 def test_context_init_identical_context(self):
     context = Context()
     identical_context = Context.from_pointer(context.get_pointer())
     assert identical_context == context
Esempio n. 5
0
 def get_pango_context_as_class(self) -> Context:
     return Context.from_pointer(self.pango_context, gc=True)