From 2cbf21ed5ff3e0ff549838bade4830bf6e070df2 Mon Sep 17 00:00:00 2001 From: Leif Gehrmann Date: Fri, 7 Oct 2022 20:46:03 +0100 Subject: [PATCH] Support pangocffi v0.11.0 + Use `property()` for setting resolution (#29) --- pangocairocffi/create_update_functions.py | 4 +-- pangocairocffi/font_functions.py | 8 ++--- pangocairocffi/font_map.py | 44 ++++++++++------------- pangocairocffi/render_functions.py | 6 ++-- requirements.txt | 2 +- setup.cfg | 2 +- tests/test_end_to_end.py | 6 ++-- tests/test_error_underline.py | 6 ++-- tests/test_extents.py | 16 ++++----- tests/test_font_map.py | 6 ++-- tests/test_glyph_item.py | 10 +++--- tox.ini | 2 +- 12 files changed, 53 insertions(+), 59 deletions(-) diff --git a/pangocairocffi/create_update_functions.py b/pangocairocffi/create_update_functions.py index deaa629..7c5ff4c 100644 --- a/pangocairocffi/create_update_functions.py +++ b/pangocairocffi/create_update_functions.py @@ -43,7 +43,7 @@ def update_context( cairo_t_pointer = _get_cairo_t_from_cairo_ctx(cairo_context) pangocairo.pango_cairo_update_context( cairo_t_pointer, - pango_context.get_pointer() + pango_context.pointer ) @@ -86,4 +86,4 @@ def update_layout( a Pango layout """ cairo_t_pointer = _get_cairo_t_from_cairo_ctx(cairo_context) - pangocairo.pango_cairo_update_layout(cairo_t_pointer, layout.get_pointer()) + pangocairo.pango_cairo_update_layout(cairo_t_pointer, layout.pointer) diff --git a/pangocairocffi/font_functions.py b/pangocairocffi/font_functions.py index 1b21380..2520483 100644 --- a/pangocairocffi/font_functions.py +++ b/pangocairocffi/font_functions.py @@ -32,7 +32,7 @@ def set_resolution(context: Context, dpi: float) -> None: 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) + pangocairo.pango_cairo_context_set_resolution(context.pointer, dpi) def get_resolution(context: Context) -> float: @@ -45,7 +45,7 @@ def get_resolution(context: Context) -> float: 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()) + return pangocairo.pango_cairo_context_get_resolution(context.pointer) def set_font_options( @@ -65,7 +65,7 @@ def set_font_options( """ if options is None: options = ffi.NULL - context_pointer = context.get_pointer() + context_pointer = context.pointer pangocairo.pango_cairo_context_set_font_options(context_pointer, options) @@ -82,7 +82,7 @@ def get_font_options(context: Context) -> Optional[ffi.CData]: a cairo_font_options_t pointer previously set on the context, otherwise ``None``. """ - context_pointer = context.get_pointer() + context_pointer = context.pointer font_option_pointer = pangocairo.pango_cairo_context_get_font_options( context_pointer ) diff --git a/pangocairocffi/font_map.py b/pangocairocffi/font_map.py index bfa2fa7..bee5bac 100644 --- a/pangocairocffi/font_map.py +++ b/pangocairocffi/font_map.py @@ -45,7 +45,8 @@ def _init_pointer(self, pointer: ffi.CData): pointer = ffi.cast('PangoCairoFontMap *', pointer) self._pointer = pointer - def get_pointer(self) -> ffi.CData: + @property + def pointer(self) -> ffi.CData: """ Returns the pointer to the font map @@ -122,7 +123,7 @@ def set_default( """ fontmap_pointer = ffi.NULL if fontmap is not None: - fontmap_pointer = fontmap.get_pointer() + fontmap_pointer = fontmap.pointer pangocairo.pango_cairo_font_map_set_default(fontmap_pointer) @classmethod @@ -143,7 +144,7 @@ def from_cairo_font_type( def __eq__(self, other): if isinstance(other, PangoCairoFontMap): - return self.get_pointer() == other.get_pointer() + return self.pointer == other.pointer return NotImplemented def get_cairo_font_type_pointer(self) -> ffi.CData: @@ -155,33 +156,26 @@ def get_cairo_font_type_pointer(self) -> ffi.CData: the pointer to the ``cairo_font_type_t``. """ return pangocairo.pango_cairo_font_map_get_font_type( - self.get_pointer() + self.pointer ) - def set_resolution(self, dpi: float) -> None: - """ - Sets the resolution for the ``fontmap``. This is a scale factor between - points specified in a Pango FontDescription 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 dpi: - the resolution in "dots per inch". (Physical inches aren't - actually involved; the terminology is conventional.) - """ - pangocairo.pango_cairo_font_map_set_resolution(self.get_pointer(), dpi) - - def get_resolution(self) -> float: - """ - Returns the resolution for the ``fontmap``. + def _set_resolution(self, dpi: float) -> None: + pangocairo.pango_cairo_font_map_set_resolution(self.pointer, dpi) - :return: - the resolution in "dots per inch" - """ + def _get_resolution(self) -> float: return pangocairo.pango_cairo_font_map_get_resolution( - self.get_pointer() + self.pointer ) + resolution: float = property(_get_resolution, _set_resolution) + """ + The resolution for the ``fontmap`` in "dots per inch". This is a scale + factor between points specified in a Pango FontDescription and Cairo units. + The default value is 96, meaning that a 10 point font will be 13 units + high. (10 * 96. / 72. = 13.3). (Physical inches aren't actually involved; + the terminology is conventional.) + """ + def create_context(self) -> Context: """ Creates a Pango ``Context`` connected to ``fontmap``. This is @@ -192,6 +186,6 @@ def create_context(self) -> Context: the newly allocated Pango ``Context``. """ context_pointer = pango.pango_font_map_create_context( - self.get_pointer() + self.pointer ) return Context.from_pointer(context_pointer, gc=True) diff --git a/pangocairocffi/render_functions.py b/pangocairocffi/render_functions.py index 3cc8c1f..c4ea1e3 100644 --- a/pangocairocffi/render_functions.py +++ b/pangocairocffi/render_functions.py @@ -60,7 +60,7 @@ def show_glyph_item( pangocairo.pango_cairo_show_glyph_item( cairo_context_pointer, text_pointer, - glyph_item.get_pointer() + glyph_item.pointer ) @@ -102,7 +102,7 @@ def show_layout( cairo_context_pointer = _get_cairo_t_from_cairo_ctx(cairo_context) pangocairo.pango_cairo_show_layout( cairo_context_pointer, - layout.get_pointer() + layout.pointer ) @@ -206,7 +206,7 @@ def layout_path( cairo_context_pointer = _get_cairo_t_from_cairo_ctx(cairo_context) pangocairo.pango_cairo_layout_path( cairo_context_pointer, - layout.get_pointer() + layout.pointer ) diff --git a/requirements.txt b/requirements.txt index bbc0d8f..5850a99 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ cffi >= 1.1.0 cairocffi >= 1.0.2 -pangocffi >= 0.8.0 +pangocffi >= 0.11.0 flake8 coverage pytest diff --git a/setup.cfg b/setup.cfg index 03c332e..5dd8520 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,7 +36,7 @@ setup_requires = install_requires = cffi >= 1.1.0 cairocffi >= 1.0.2 - pangocffi >= 0.10.0 + pangocffi >= 0.11.0 python_requires = >= 3.6 [options.package_data] diff --git a/tests/test_end_to_end.py b/tests/test_end_to_end.py index 39f1e45..cf50ca9 100644 --- a/tests/test_end_to_end.py +++ b/tests/test_end_to_end.py @@ -13,9 +13,9 @@ def test_pdf(): context.rotate(-0.1) layout = pangocairocffi.create_layout(context) - layout.set_width(pangocffi.units_from_double(width)) - layout.set_alignment(pangocffi.Alignment.CENTER) - layout.set_markup('Hi from Παν語') + layout.width = pangocffi.units_from_double(width) + layout.alignment = pangocffi.Alignment.CENTER + layout.apply_markup('Hi from Παν語') pangocairocffi.show_layout(context, layout) diff --git a/tests/test_error_underline.py b/tests/test_error_underline.py index 5cf7a37..cea5da2 100644 --- a/tests/test_error_underline.py +++ b/tests/test_error_underline.py @@ -22,9 +22,9 @@ def test_pdf(): ctx = cairocffi.Context(surface) layout = pangocairocffi.create_layout(ctx) - layout.set_width(pangocffi.units_from_double(width)) - layout.set_alignment(pangocffi.Alignment.CENTER) - layout.set_markup('Hi from Παν語') + layout.width = pangocffi.units_from_double(width) + layout.alignment = pangocffi.Alignment.CENTER + layout.apply_markup('Hi from Παν語') layout_logical_extent = layout.get_extents()[1] layout_baseline = pangocffi.units_to_double(layout.get_baseline()) diff --git a/tests/test_extents.py b/tests/test_extents.py index 5701cf2..de4f9d2 100644 --- a/tests/test_extents.py +++ b/tests/test_extents.py @@ -343,9 +343,9 @@ def _show_label( ): context.translate(position[0], position[1]) label = pangocairocffi.create_layout(context) - label.set_width(pangocffi.units_from_double(width)) - label.set_alignment(pangocffi.Alignment.LEFT) - label.set_markup('%s' % text) + label.width = pangocffi.units_from_double(width) + label.alignment = pangocffi.Alignment.LEFT + label.apply_markup('%s' % text) pangocairocffi.show_layout(context, label) context.translate(-position[0], -position[1]) @@ -359,11 +359,11 @@ def test_pdf(): ctx.translate(width / 2, 10) layout = pangocairocffi.create_layout(ctx) - layout.set_width(pangocffi.units_from_double(width / 2)) - layout.set_alignment(pangocffi.Alignment.CENTER) - layout.set_markup('Hi from Παν語\n' - 'The text layout engine library for displaying ' - 'multi-language text!') + layout.width = pangocffi.units_from_double(width / 2) + layout.alignment = pangocffi.Alignment.CENTER + layout.apply_markup('Hi from Παν語\n' + 'The text layout engine library for displaying ' + 'multi-language text!') translate_y = 110 label_pos = (-width / 2 + 30, 30) diff --git a/tests/test_font_map.py b/tests/test_font_map.py index 86e40fd..812eb5b 100644 --- a/tests/test_font_map.py +++ b/tests/test_font_map.py @@ -6,10 +6,10 @@ def test_font_map(): font_map_new = PangoCairoFontMap() assert font_map_new != font_map_default - resolution = font_map_new.get_resolution() + resolution = font_map_new.resolution assert resolution == 96 - font_map_new.set_resolution(123) - assert font_map_new.get_resolution() == 123 + font_map_new.resolution = 123 + assert font_map_new.resolution == 123 PangoCairoFontMap.set_default(font_map_new) assert font_map_new == PangoCairoFontMap.get_default() diff --git a/tests/test_glyph_item.py b/tests/test_glyph_item.py index 2e1f75b..02dee06 100644 --- a/tests/test_glyph_item.py +++ b/tests/test_glyph_item.py @@ -18,7 +18,7 @@ def render_run_glyph_items( a Pango layout """ layout_iter = layout.get_iter() - layout_text = layout.get_text() + layout_text = layout.text alternate = False while True: @@ -119,7 +119,7 @@ def render_cluster_glyph_items( """ layout_run_iter = layout.get_iter() layout_cluster_iter = layout.get_iter() - layout_text = layout.get_text() + layout_text = layout.text alternate = False while True: @@ -164,9 +164,9 @@ def test_pdf(): ctx.translate(width / 4, 100) layout = pangocairocffi.create_layout(ctx) - layout.set_width(pangocffi.units_from_double(width / 2)) - layout.set_alignment(pangocffi.Alignment.CENTER) - layout.set_markup( + layout.width = pangocffi.units_from_double(width / 2) + layout.alignment = pangocffi.Alignment.CENTER + layout.apply_markup( 'Hi from Παν語\n' 'The text layout engine library for ' 'displaying multi-language text!\n' diff --git a/tox.ini b/tox.ini index 6734dac..87deaf4 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py36, py37, py38, py39 +envlist = py36, py37, py38, py39, py310 [gh-actions] python =