[PATCH 1/2] print: Use cairo_save/cairo_restore to manage temporary cairo changes

Carl Worth cworth at cworth.org
Thu Jan 31 19:58:43 PST 2013


The print_weight_data function was making several cairo_translate
calls and then doing some final cairo_translate calls with the goal of
manually undoing any changes to cairo's state. Obviously, this is
fragile.

Cairo provides save/restore calls which solve this problem in a robust
way. Switch to using these instead.

Signed-off-by: Carl Worth <cworth at cworth.org>
---

Pretty much every drawing function in print.c should have similar
treatment applied. I'm just doing this one to enable my next patch,
(which fixes the line spacing in the "weight system" box), and to
teach you guys. So it should be straightforward to similarly fix the
other functions.

Also, this commit has a side effect of changing the layout of the text
in the box just to the left of the weight-system box. Apparently, the
"undo translation" code here was buggy and was not returning to the
exact starting point. So the rendering code for the box to the left
was relying on a buggy starting point. It should be fixed, of course.

 print.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/print.c b/print.c
index 222bf8d..50147f7 100644
--- a/print.c
+++ b/print.c
@@ -290,6 +290,8 @@ static void print_weight_data (struct dive *dive, cairo_t *cr, int maxwidth, int
 	char buffer[80];
 	PangoLayout *layout;
 
+	cairo_save(cr);
+
 	layout = pango_cairo_create_layout(cr);
 	pango_layout_set_height(layout,maxheight);
 	pango_layout_set_width(layout, maxwidth);
@@ -339,10 +341,9 @@ static void print_weight_data (struct dive *dive, cairo_t *cr, int maxwidth, int
 			unit_weight);
 	pango_layout_set_text(layout, buffer, -1);
 	pango_cairo_show_layout(cr, layout);
-	/* undo translations */
-	cairo_translate (cr, 0, -(weightsystemcounter * height) / (4 * (double) PANGO_SCALE));
-	cairo_translate (cr, 0, -height / (3 * (double) PANGO_SCALE));
 	g_object_unref (layout);
+
+	cairo_restore(cr);
 }
 
 /* Print the dive OTUs */
-- 
1.7.10.4



More information about the subsurface mailing list