Skip to content

Commit

Permalink
Added Context.{get,set}_hairline(), according changes to ffi header, …
Browse files Browse the repository at this point in the history
…and test case.
  • Loading branch information
dtromb committed May 30, 2024
1 parent 4bafdd7 commit b7a5023
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cairocffi/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions cairocffi/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
10 changes: 10 additions & 0 deletions cairocffi/test_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit b7a5023

Please sign in to comment.