diff --git a/lib/gears/shape.lua b/lib/gears/shape.lua index e9f50e8f53..94e535f9db 100644 --- a/lib/gears/shape.lua +++ b/lib/gears/shape.lua @@ -317,6 +317,65 @@ function module.partially_rounded_rect(cr, width, height, tl, tr, br, bl, rad) cr:close_path() end +--- A rounded rect with individually defined corner radii. +-- +-- @DOC_gears_shape_individually_rounded_rect_EXAMPLE@ +-- +-- @param cr A cairo context +-- @tparam number width The shape width +-- @tparam number height The shape height +-- @tparam number tl The top left corner's radius +-- @tparam number tr The top right corner's radius +-- @tparam number br The bottom right corner's radius +-- @tparam number bl The bottom left corner's radius +-- @noreturn +-- @staticfct gears.shape.individually_rounded_rect +function module.individually_rounded_rect(cr, width, height, tl, tr, br, bl) + local corners = {tl = tl, tr = tr, br = br, bl = bl} + for key, val in pairs(corners) do + if width / 2 < val then + corners[key] = width / 2 + end + if height / 2 < val then + corners[key] = height / 2 + end + end + + -- In case there is already some other path on the cairo context: + -- Make sure the close_path() below goes to the right position. + cr:new_sub_path() + + -- Top left + if tl then + cr:arc( corners.tl, corners.tl, corners.tl, math.pi, 3*(math.pi/2)) + else + cr:move_to(0,0) + end + + -- Top right + if tr then + cr:arc( width-corners.tr, corners.tr, corners.tr, 3*(math.pi/2), math.pi*2) + else + cr:line_to(width, 0) + end + + -- Bottom right + if br then + cr:arc( width-corners.br, height-corners.br, corners.br, math.pi*2 , math.pi/2) + else + cr:line_to(width, height) + end + + -- Bottom left + if bl then + cr:arc( corners.bl, height-corners.bl, corners.bl, math.pi/2, math.pi) + else + cr:line_to(0, height) + end + + cr:close_path() +end + --- A rounded rectangle with a triangle at the top. -- -- @DOC_gears_shape_infobubble_EXAMPLE@ diff --git a/tests/examples/gears/shape/individually_rounded_rect.lua b/tests/examples/gears/shape/individually_rounded_rect.lua new file mode 100644 index 0000000000..a79c908f28 --- /dev/null +++ b/tests/examples/gears/shape/individually_rounded_rect.lua @@ -0,0 +1,13 @@ +--DOC_GEN_IMAGE --DOC_HIDE +local shape,cr,show = ... --DOC_HIDE + +shape.individually_rounded_rect(cr, 70, 70, 0, 10, 20, 30) +show(cr) --DOC_HIDE + +shape.individually_rounded_rect(cr, 70, 70, 10, 0, 35, 5) +show(cr) --DOC_HIDE + +shape.individually_rounded_rect(cr, 70, 70, 30, 20, 10, 1) +show(cr) --DOC_HIDE + +--DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80