From b7a50236d1aa7c415e4d64b5de5df2499adeabde Mon Sep 17 00:00:00 2001 From: dtromb Date: Thu, 30 May 2024 17:54:22 -0500 Subject: [PATCH 1/3] Added Context.{get,set}_hairline(), according changes to ffi header, and test case. --- cairocffi/constants.py | 6 ++++++ cairocffi/context.py | 22 ++++++++++++++++++++++ cairocffi/test_cairo.py | 10 ++++++++++ 3 files changed, 38 insertions(+) diff --git a/cairocffi/constants.py b/cairocffi/constants.py index a045d36..afe5a5e 100644 --- a/cairocffi/constants.py +++ b/cairocffi/constants.py @@ -482,6 +482,9 @@ void cairo_set_line_width (cairo_t *cr, double width); +void +cairo_set_hairline (cairo_t *cr, cairo_bool_t set_hairline); + typedef enum _cairo_line_cap { CAIRO_LINE_CAP_BUTT, CAIRO_LINE_CAP_ROUND, @@ -1098,6 +1101,9 @@ double cairo_get_line_width (cairo_t *cr); +cairo_bool_t +cairo_get_hairline (cairo_t *cr); + cairo_line_cap_t cairo_get_line_cap (cairo_t *cr); diff --git a/cairocffi/context.py b/cairocffi/context.py index ac5e475..e11faa4 100644 --- a/cairocffi/context.py +++ b/cairocffi/context.py @@ -2255,3 +2255,25 @@ def tag_end(self, tag_name): """ cairo.cairo_tag_end(self._pointer, _encode_string(tag_name)) self._check_status() + + def set_hairline(self, enabled): + """Sets lines within the cairo context to be hairlines. + + Hairlines are logically zero-width lines that are drawn at the thinnest renderable width possible in the current context. + + :type enabled: bool + :param enabled: whether or not to set hairline mode + + *New in cairo 1.18.* + """ + cairo.cairo_set_hairline(self._pointer, int(enabled)) + self._check_status() + + def get_hairline(self): + """Returns whether or not hairline mode is set, as set by cairo_set_hairline(). + + :returns: whether hairline mode is set + + *New in cairo 1.18.* + """ + return bool(cairo.cairo_get_hairline(self._pointer)) diff --git a/cairocffi/test_cairo.py b/cairocffi/test_cairo.py index cc7e871..2e38789 100644 --- a/cairocffi/test_cairo.py +++ b/cairocffi/test_cairo.py @@ -1267,3 +1267,13 @@ def test_from_null_pointer(): for class_ in [Surface, Context, Pattern, FontFace, ScaledFont]: with pytest.raises(ValueError): class_._from_pointer(cairocffi.ffi.NULL, 'unused') + + +def test_hairline(): + for extents in [None, (0, 0, 140, 80)]: + surface = RecordingSurface(cairocffi.CONTENT_COLOR_ALPHA, extents) + ctx = Context(surface) + ctx.set_hairline(True) + assert(ctx.get_hairline() is True) + ctx.set_hairline(False) + assert(ctx.get_hairline() is False) From cd9689ed8417ebc64c449f81c660acf77f93088d Mon Sep 17 00:00:00 2001 From: dtromb Date: Fri, 31 May 2024 16:13:32 -0500 Subject: [PATCH 2/3] Made test case for new 1.18 functionality condition on the correct version of the library actually being present. --- cairocffi/test_cairo.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cairocffi/test_cairo.py b/cairocffi/test_cairo.py index 2e38789..95a5eb5 100644 --- a/cairocffi/test_cairo.py +++ b/cairocffi/test_cairo.py @@ -1269,6 +1269,8 @@ def test_from_null_pointer(): class_._from_pointer(cairocffi.ffi.NULL, 'unused') +@pytest.mark.xfail(cairo_version() < 11800, + reason='Cairo version too low') def test_hairline(): for extents in [None, (0, 0, 140, 80)]: surface = RecordingSurface(cairocffi.CONTENT_COLOR_ALPHA, extents) From caf8bcf5e5ec903b288164807dc065c259576dad Mon Sep 17 00:00:00 2001 From: dtromb Date: Fri, 31 May 2024 16:20:51 -0500 Subject: [PATCH 3/3] Fixed long comment lines to pass ruff check. --- cairocffi/context.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cairocffi/context.py b/cairocffi/context.py index e11faa4..1b4b8af 100644 --- a/cairocffi/context.py +++ b/cairocffi/context.py @@ -2259,7 +2259,8 @@ def tag_end(self, tag_name): def set_hairline(self, enabled): """Sets lines within the cairo context to be hairlines. - Hairlines are logically zero-width lines that are drawn at the thinnest renderable width possible in the current context. + Hairlines are logically zero-width lines that are drawn at the thinnest + renderable width possible in the current context. :type enabled: bool :param enabled: whether or not to set hairline mode