Please add the following cairo_path_t method to cairo. void cairo_rounded_rectangle(cairo_t *cr, double x, double y, double width, double height, double radius) { cairo_new_sub_path(cr); cairo_arc(cr, x + radius, y + height - radius, radius, 90.0 * (M_PI / 180.0), 180.0 * (M_PI / 180.)); cairo_line_to(cr, x, y + radius); cairo_new_sub_path(cr); cairo_arc(cr, x + radius, y + radius, radius, 180.0 * (M_PI / 180.0), 270.0 * (M_PI / 180.)); cairo_line_to(cr, x + width - radius, y); cairo_new_sub_path(cr); cairo_arc(cr, x + width - radius, y + radius, radius, 270.0 * (M_PI / 180.0), 360.0 * (M_PI / 180.)); cairo_line_to(cr, x + width, y + height - radius); cairo_new_sub_path(cr); cairo_arc(cr, x + width - radius, y + height - radius, radius, 360.0 * (M_PI / 180.0), 90.0 * (M_PI / 180.)); cairo_line_to(cr, x + radius, y + height); }
Version with a little less math void cairo_rounded_rectangle(cairo_t *cr, double x, double y, double width, double height, double radius) { cairo_new_sub_path(cr); cairo_arc(cr, x + radius, y + height - radius, radius, 0.5 * M_PI, M_PI); cairo_line_to(cr, x, y + radius); cairo_new_sub_path(cr); cairo_arc(cr, x + radius, y + radius, radius, M_PI, 1.5 * M_PI); cairo_line_to(cr, x + width - radius, y); cairo_new_sub_path(cr); cairo_arc(cr, x + width - radius, y + radius, radius, 1.5 * M_PI, 2.0 * M_PI); cairo_line_to(cr, x + width, y + height - radius); cairo_new_sub_path(cr); cairo_arc(cr, x + width - radius, y + height - radius, radius, 2.0 * M_PI, 0.5 * M_PI); cairo_line_to(cr, x + radius, y + height); }
Note that for your rounded rectangle all your need is (hmm, without checking through the the ordering of corners): cairo_new_sub_path (cr); cairo_arc (cr, x + radius, y + height - radius, radius, 0.5 * M_PI, M_PI); cairo_arc (cr, x + radius, y + radius, radius, M_PI, 1.5 * M_PI); cairo_arc (cr, x + width - radius, y + radius, radius, 1.5 * M_PI, 2.0 * M_PI); cairo_arc (cr, x + width - radius, y + height - radius, radius, 2.0 *M_PI, 0.5 * M_PI); cairo_close_path (cr); However, there is a big difference between cairo_rectangle() and cairo_rounded_rectangle(), namely that nobody will agree on how cairo_rounded_rectangle() should work. In other words, everybody has a different aesthetic interpretation of how the rounded rectangle should look... You used constant radius of curvature around the corners, maybe they should vary in proportional to the relative edge lengths? Perhaps they should be inverted for use in snug fitting scrollbars? This is not a decision that can be made by cairo, but must be made in conjunction with the graphics designers and as demonstrated will only need a trivial function to emit the correct shape - but one that varies with intent.
Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.