From 34428762d22980e2e9e369162a934c00de8f9af6 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 5 Mar 2024 05:53:58 -0500 Subject: [PATCH] Simplify some Cairo drawing Instead of generating paths twice, once for filling and once for stroking, use the path-preserving `fill_preserve` operator instead. As well, do the filling before stroking for consistency, or else the stroke might be accidentally covered, as occurred with the child rings. Also, simplify the map marker which seems to have drawn the elliptical portion twice. --- gramps/gui/widgets/fanchart.py | 32 +++----------- gramps/plugins/lib/maps/lifewaylayer.py | 3 +- gramps/plugins/lib/maps/markerlayer.py | 58 +++++++++---------------- 3 files changed, 27 insertions(+), 66 deletions(-) diff --git a/gramps/gui/widgets/fanchart.py b/gramps/gui/widgets/fanchart.py index 5e371ac5b19..629b4731196 100644 --- a/gramps/gui/widgets/fanchart.py +++ b/gramps/gui/widgets/fanchart.py @@ -577,22 +577,8 @@ def draw_radbox( ctx.line_to(radiusin * math.cos(stop_rad), radiusin * math.sin(stop_rad)) ctx.arc_negative(0, 0, radiusin, stop_rad, start_rad) ctx.close_path() - ##path = ctx.copy_path() # not working correct ctx.set_source_rgba(color[0], color[1], color[2], color[3]) - ctx.fill() - # and again for the border - ctx.move_to(radiusout * math.cos(start_rad), radiusout * math.sin(start_rad)) - ctx.arc(0, 0, radiusout, start_rad, stop_rad) - if (start_rad - stop_rad) % (2 * math.pi) > 1e-5: - radial_motion_type = ctx.line_to - else: - radial_motion_type = ctx.move_to - radial_motion_type(radiusin * math.cos(stop_rad), radiusin * math.sin(stop_rad)) - ctx.arc_negative(0, 0, radiusin, stop_rad, start_rad) - radial_motion_type( - radiusout * math.cos(start_rad), radiusout * math.sin(start_rad) - ) - ##ctx.append_path(path) # not working correct + ctx.fill_preserve() ctx.set_source_rgb(0, 0, 0) # black if thick: ctx.set_line_width(3) @@ -618,24 +604,16 @@ def draw_innerring(self, ctx, person, userdata, start, inc): ctx.line_to(rmax * math.cos(thetamax), rmax * math.sin(thetamax)) ctx.arc_negative(0, 0, rmax, thetamax, thetamin) ctx.close_path() - ##path = ctx.copy_path() # not working correct - ctx.set_source_rgb(0, 0, 0) # black - ctx.set_line_width(1) - ctx.stroke() - # now again to fill if person: red, green, blue, alpha = self.background_box(person, -1, userdata) else: red = green = blue = 255 alpha = 1 - ctx.move_to(rmin * math.cos(thetamin), rmin * math.sin(thetamin)) - ctx.arc(0, 0, rmin, thetamin, thetamax) - ctx.line_to(rmax * math.cos(thetamax), rmax * math.sin(thetamax)) - ctx.arc_negative(0, 0, rmax, thetamax, thetamin) - ctx.close_path() - ##ctx.append_path(path) # not working correct ctx.set_source_rgba(red / 255.0, green / 255.0, blue / 255.0, alpha) - ctx.fill() + ctx.fill_preserve() + ctx.set_source_rgb(0, 0, 0) # black + ctx.set_line_width(1) + ctx.stroke() def draw_person( self, diff --git a/gramps/plugins/lib/maps/lifewaylayer.py b/gramps/plugins/lib/maps/lifewaylayer.py index 784d6c7398a..df3f3bca6c9 100644 --- a/gramps/plugins/lib/maps/lifewaylayer.py +++ b/gramps/plugins/lib/maps/lifewaylayer.py @@ -140,10 +140,9 @@ def do_draw(self, gpsmap, ctx): ctx.move_to(coord_x1, coord_y1) ctx.translate(coord_x1, coord_y1 / coz) ctx.arc(0.0, 0.0, cox, 0.0, 2 * pi) - ctx.fill() + ctx.fill_preserve() ctx.set_source_rgba(1.0, 0.0, 0.0, 0.5) ctx.set_line_width(2.0) - ctx.arc(0.0, 0.0, cox, 0.0, 2 * pi) ctx.stroke() ctx.restore() diff --git a/gramps/plugins/lib/maps/markerlayer.py b/gramps/plugins/lib/maps/markerlayer.py index 29d2de2d432..1c10812ac41 100644 --- a/gramps/plugins/lib/maps/markerlayer.py +++ b/gramps/plugins/lib/maps/markerlayer.py @@ -192,47 +192,31 @@ def draw_marker(ctx, x01, y01, size, color): width = 48.0 * size height = width / 2 color = Gdk.color_parse(color) - ctx.set_source_rgba( - float(color.red / 65535.0), - float(color.green / 65535.0), - float(color.blue / 65535.0), - 1.0, - ) # transparency - ctx.set_line_width(2.0) - ctx.move_to(x01, y01) - ctx.line_to((x01 + (height / 3)), (y01 - height * 2)) - ctx.line_to((x01 - (height / 3)), (y01 - height * 2)) - ctx.fill() - ctx.set_source_rgba(1.0, 0.0, 0.0, 0.5) + fill_color = ( + color.red / 65535.0, + color.green / 65535.0, + color.blue / 65535.0, + 1.0, # transparency + ) + stroke_color = (1.0, 0.0, 0.0, 0.5) + ctx.move_to(x01, y01) - ctx.line_to((x01 + (height / 3)), (y01 - height * 2)) - ctx.line_to((x01 - (height / 3)), (y01 - height * 2)) - ctx.line_to(x01, y01) - ctx.stroke() - ctx.save() - ctx.translate(x01 + width / 4 - (width / 4), y01 - height * 2 - (width / 4)) - ctx.scale(width / 2.0, height / 2.0) - ctx.arc(0.0, 0.0, 1.0, 0.0, 2 * PI) - ctx.fill_preserve() - ctx.set_source_rgba(1.0, 0.0, 0.0, 0.5) + ctx.line_to(x01 + width / 6, y01 - height * 2) + ctx.line_to(x01 - width / 6, y01 - height * 2) + ctx.close_path() ctx.set_line_width(2.0) - ctx.arc(0.0, 0.0, 1.0, 0.0, 2 * PI) - ctx.restore() + ctx.set_source_rgba(*fill_color) + ctx.fill_preserve() + ctx.set_source_rgba(*stroke_color) ctx.stroke() + ctx.save() - ctx.set_source_rgba( - float(color.red / 65535.0), - float(color.green / 65535.0), - float(color.blue / 65535.0), - 1.0, - ) # transparency - # ctx.translate(x01 + width/4 - 12.0 , y01 - height*2 - 12.0) - ctx.translate(x01 + width / 4 - (width / 4), y01 - height * 2 - (width / 4)) - ctx.scale(width / 2.0, height / 2.0) - ctx.arc(0.0, 0.0, 1.0, 0.0, 2 * PI) - ctx.fill_preserve() - ctx.set_source_rgba(1.0, 0.0, 0.0, 0.5) - ctx.set_line_width(2.0) + ctx.translate(x01, y01 - height * 2 - width / 4) + ctx.scale(width / 2, height / 2) ctx.arc(0.0, 0.0, 1.0, 0.0, 2 * PI) ctx.restore() + ctx.set_line_width(2.0) + ctx.set_source_rgba(*fill_color) + ctx.fill_preserve() + ctx.set_source_rgba(*stroke_color) ctx.stroke()