Пример #1
0
    def test_layout_setting_properties():
        context = Context()
        layout = Layout(context)

        layout.set_width(300)
        assert layout.get_width() == 300

        layout.set_height(400)
        assert layout.get_height() == 400

        assert layout.get_spacing() == 0
        layout.set_spacing(30)
        assert layout.get_spacing() == 30

        layout.set_alignment(Alignment.CENTER)
        assert layout.get_alignment() is Alignment.CENTER

        layout.set_ellipsize(EllipsizeMode.MIDDLE)
        assert layout.get_ellipsize() is EllipsizeMode.MIDDLE

        ink_rect, logical_rect = layout.get_extents()
        assert logical_rect.width == 0
        assert logical_rect.height == 0

        width, height = layout.get_size()
        assert width == 0
        assert height == 0

        baseline = layout.get_baseline()
        assert baseline == 0

        line_count = layout.get_line_count()
        assert line_count == 1
Пример #2
0
    def test_layout_iter_properties():
        context = Context()
        layout = Layout(context)
        layout_iter = layout.get_iter()

        assert layout_iter.next_run() is False
        assert layout_iter.next_char() is False
        assert layout_iter.next_cluster() is False
        assert layout_iter.next_line() is False
        assert layout_iter.at_last_line() is True
        assert layout_iter.get_index() == 0
        assert layout_iter.get_baseline() == 0

        assert isinstance(layout_iter.get_char_extents(), Rectangle)

        cluster_ink, cluster_logical = layout_iter.get_cluster_extents()
        assert isinstance(cluster_ink, Rectangle)
        assert isinstance(cluster_logical, Rectangle)

        run_ink, run_logical = layout_iter.get_run_extents()
        assert isinstance(run_ink, Rectangle)
        assert isinstance(run_logical, Rectangle)

        y0, y1 = layout_iter.get_line_yrange()
        assert y0 == 0
        assert y1 == 0

        line_ink, line_logical = layout_iter.get_line_extents()
        assert isinstance(line_ink, Rectangle)
        assert isinstance(line_logical, Rectangle)

        layout_ink, layout_logical = layout_iter.get_layout_extents()
        assert isinstance(layout_ink, Rectangle)
        assert isinstance(layout_logical, Rectangle)
Пример #3
0
    def test_layout_setting_text():
        context = Context()
        layout = Layout(context)

        layout.set_width(300)

        layout.set_text('Hi from Pango')
        layout.set_markup('<span font="italic 30">Hi from Παν語</span>')
def get_resolution(context: Context) -> float:
    """
    Returns the resolution for the Pango context.

    :param context:
        a Pango context
    :return:
        the resolution in "dots per inch". A negative value will be returned
        if no resolution has previously been set.
    """
    return pangocairo.pango_cairo_context_get_resolution(context.get_pointer())
Пример #5
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)
Пример #6
0
def get_text_height(pctx: pangocffi.Context, style: TextStyle,
                    resolution_scale: float):
    font = FontDescription()
    font.set_family(style.font)
    font.set_absolute_size(to_pango_units(style.size))
    ret = pangocffi.pango.pango_context_get_metrics(pctx.get_pointer(),
                                                    font.get_pointer(),
                                                    pangocffi.ffi.NULL)

    descent = from_pango_units(
        pangocffi.pango.pango_font_metrics_get_descent(ret))
    ascent = from_pango_units(
        pangocffi.pango.pango_font_metrics_get_ascent(ret))
    return (ascent + descent) * resolution_scale
def set_resolution(context: Context, dpi: float) -> None:
    """
    Sets the resolution for the context. This is a scale factor between points
    specified in a PangoFontDescription and Cairo units. The default value is
    96, meaning that a 10 point font will be 13 units high.
    (10 * 96. / 72. = 13.3).

    :param context:
        a Pango context
    :param dpi:
        the resolution in "dots per inch". (Physical inches aren't actually
        involved; the terminology is conventional.) A 0 or negative value
        means to use the resolution from the font map.
    """
    pangocairo.pango_cairo_context_set_resolution(context.get_pointer(), dpi)
Пример #8
0
    def test_set_attributes(self):
        context = Context()
        layout = Layout(context)
        layout.set_text("Working?")
        attr = Attribute.from_size(5, 1, 4)
        attr_list = AttrList()

        attr_list.insert(attr)

        layout.set_attributes(attr_list)
        layout.get_attributes()

        # Resetting the attributes
        layout.set_attributes(None)
        layout.get_attributes()
Пример #9
0
def update_context(cairo_context: cairocffi.Context,
                   pango_context: pangocffi.Context) -> None:
    """
    Updates a PangoContext previously created for use with Cairo to match the
    current transformation and target surface of a Cairo context. If any
    layouts have been created for the context, it's necessary to call
    pango_layout_context_changed() on those layouts.

    :param cairo_context:
        a Cairo context
    :param pango_context:
        a Pango context, from a pango-cairo font map
    """
    cairo_t_pointer = _get_cairo_t_from_cairo_ctx(cairo_context)
    pangocairo.pango_cairo_update_context(cairo_t_pointer,
                                          pango_context.get_pointer())
def set_font_options(
        context: Context,
        options: Optional[ffi.CData]
) -> None:
    """
    Sets the font options used when rendering text with this context.
    These options override any options that pango_cairo_update_context()
    derives from the target surface.

    :param context:
        a Pango context
    :param options:
        a cairo_font_options_t, or ``None`` to unset any previously set
        options.
    """
    if options is None:
        options = ffi.NULL
    context_pointer = context.get_pointer()
    pangocairo.pango_cairo_context_set_font_options(context_pointer, options)
def get_font_options(context: Context) -> Optional[ffi.CData]:
    """
        Retrieves any font rendering options previously set with
        pango_cairo_context_set_font_options(). This function does not report
        options that are derived from the target surface by
        pango_cairo_update_context()

        :param context:
            a Pango Context
        :return:
            a cairo_font_options_t pointer previously set on the context,
            otherwise ``None``.
    """
    context_pointer = context.get_pointer()
    font_option_pointer = pangocairo.pango_cairo_context_get_font_options(
        context_pointer
    )
    if font_option_pointer == ffi.NULL:
        return None
    return font_option_pointer
Пример #12
0
    def test_layout_setting_font_description():
        context = Context()
        layout = Layout(context)

        # Assert that the font description is not set
        assert layout.get_font_description() is None

        # Creating the font description
        desc = FontDescription()
        desc.set_family('sans-serif')
        layout.set_font_description(desc)

        # Verifying the font description was set
        same_desc = layout.get_font_description()
        assert same_desc.get_family() == desc.get_family()

        # Changing the font description
        desc.set_family('serif')
        assert same_desc.get_family() != desc.get_family()

        # Resetting the font description
        layout.set_font_description(None)
        assert layout.get_font_description() is None
Пример #13
0
 def test_context_init_identical_context(self):
     context = Context()
     identical_context = Context.from_pointer(context.get_pointer())
     assert identical_context == context
Пример #14
0
 def test_layout_iter_pointer():
     context = Context()
     layout = Layout(context)
     layout_iter = layout.get_iter()
     assert isinstance(layout_iter.get_pointer(), ffi.CData)
Пример #15
0
 def test_layout_returns_identical_context():
     context = Context()
     layout = Layout(context)
     identical_context = layout.get_context()
     assert identical_context.get_pointer() == context.get_pointer()
Пример #16
0
 def get_pango_context_as_class(self) -> Context:
     return Context.from_pointer(self.pango_context, gc=True)
Пример #17
0
 def test_layout_not_implemented_equality(self):
     context = Context()
     layout = Layout(context)
     assert ('not an object' != layout)
Пример #18
0
 def test_layout_get_pointer_returns_identical_layout():
     context = Context()
     layout = Layout(context)
     identical_layout = Layout.from_pointer(layout.get_pointer())
     assert identical_layout.get_pointer() == layout.get_pointer()
Пример #19
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
Пример #20
0
    def test_layout_iterator_run_returns_none():
        context = Context()
        layout = Layout(context)
        layout_iter = layout.get_iter()

        assert layout_iter.get_run() is None
Пример #21
0
    def test_context_properties(self):
        context = Context()

        desc = FontDescription()
        desc.set_family('sans-serif')
        context.set_font_description(desc)
        assert context.get_font_description().get_family() == 'sans-serif'

        context.set_base_gravity(Gravity.EAST)
        assert context.get_base_gravity() == Gravity.EAST
        assert context.get_gravity() == Gravity.EAST

        context.set_gravity_hint(GravityHint.STRONG)
        assert context.get_gravity_hint() == GravityHint.STRONG
Пример #22
0
 def test_context_returns_null_from_null_pointer(self):
     with self.assertRaises(ValueError):
         Context.from_pointer(ffi.NULL)