Skip to content

Commit

Permalink
Merge pull request #227 from dtromb/#226-hairline
Browse files Browse the repository at this point in the history
Added Context.{get,set}_hairline(), according changes to ffi header, …
  • Loading branch information
liZe authored Jun 18, 2024
2 parents 4bafdd7 + caf8bcf commit 996a754
Show file tree
Hide file tree
Showing 3 changed files with 41 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
23 changes: 23 additions & 0 deletions cairocffi/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2255,3 +2255,26 @@ 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))
12 changes: 12 additions & 0 deletions cairocffi/test_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,3 +1267,15 @@ def test_from_null_pointer():
for class_ in [Surface, Context, Pattern, FontFace, ScaledFont]:
with pytest.raises(ValueError):
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)
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 996a754

Please sign in to comment.