gsk: Add gsk_rounded_rect_shrink()

... and replace _gtk_rounded_box_grow() and _gtk_rounded_box_shrink()
with it.
This commit is contained in:
Benjamin Otte 2016-12-18 17:10:45 +01:00
parent 1760e0d3f3
commit a9809e5d30
9 changed files with 91 additions and 85 deletions

View File

@ -72,5 +72,6 @@ gsk_rounded_rect_init_copy
gsk_rounded_rect_init_from_rect
gsk_rounded_rect_normalize
gsk_rounded_rect_offset
gsk_rounded_rect_shrink
gsk_rounded_rect_is_rectilinear
</SECTION>

View File

@ -205,6 +205,78 @@ gsk_rounded_rect_offset (GskRoundedRect *self,
return self;
}
static void
border_radius_shrink (graphene_size_t *corner,
double width,
double height)
{
if (corner->width > 0)
corner->width -= width;
if (corner->height > 0)
corner->height -= height;
if (corner->width <= 0 || corner->height <= 0)
{
corner->width = 0;
corner->height = 0;
}
}
/**
* gsk_rounded_rect_shrink:
* @self: The @GskRoundedRect to shrink or grow
* @top: How far to move the top side downwards
* @right: How far to move the right side to the left
* @bottom: How far to move the bottom side upwards
* @left: How far to move the left side to the right
*
* Shrinks (or grows) the given rectangle by moving the 4 sides
* according to the offsets given. The corner radii will be changed
* in a way that tries to keep the center of the corner circle intact.
* This emulates CSS behavior.
*
* This function also works for growing rectangles if you pass
* negative values for the @top, @right, @bottom or @left.
*
* Returns: @self
**/
GskRoundedRect *
gsk_rounded_rect_shrink (GskRoundedRect *self,
float top,
float right,
float bottom,
float left)
{
if (self->bounds.size.width - left - right < 0)
{
self->bounds.origin.x += left * self->bounds.size.width / (left + right);
self->bounds.size.width = 0;
}
else
{
self->bounds.origin.x += left;
self->bounds.size.width -= left + right;
}
if (self->bounds.size.height - bottom - top < 0)
{
self->bounds.origin.y += top * self->bounds.size.height / (top + bottom);
self->bounds.size.height = 0;
}
else
{
self->bounds.origin.y += top;
self->bounds.size.height -= top + bottom;
}
border_radius_shrink (&self->corner[GSK_CORNER_TOP_LEFT], left, top);
border_radius_shrink (&self->corner[GSK_CORNER_TOP_RIGHT], right, top);
border_radius_shrink (&self->corner[GSK_CORNER_BOTTOM_RIGHT], right, bottom);
border_radius_shrink (&self->corner[GSK_CORNER_BOTTOM_LEFT], left, bottom);
return self;
}
/**
* gsk_rounded_rect_is_rectilinear:
* @self: the #GskRoundedRect to check

View File

@ -87,6 +87,12 @@ GDK_AVAILABLE_IN_3_90
GskRoundedRect * gsk_rounded_rect_offset (GskRoundedRect *self,
float dx,
float dy);
GDK_AVAILABLE_IN_3_90
GskRoundedRect * gsk_rounded_rect_shrink (GskRoundedRect *self,
float top,
float right,
float bottom,
float left);
GDK_AVAILABLE_IN_3_90
gboolean gsk_rounded_rect_is_rectilinear (GskRoundedRect *self);

View File

@ -925,7 +925,7 @@ _gtk_css_shadow_value_paint_box (const GtkCssValue *shadow,
gsk_rounded_rect_path (padding_box, cr);
outside = spread + clip_radius + MAX (fabs (x), fabs (y));
clip_box = *padding_box;
_gtk_rounded_box_grow (&clip_box, outside, outside, outside, outside);
gsk_rounded_rect_shrink (&clip_box, -outside, -outside, -outside, -outside);
_gtk_rounded_box_clip_path (&clip_box, cr);
cairo_clip (cr);
@ -935,12 +935,12 @@ _gtk_css_shadow_value_paint_box (const GtkCssValue *shadow,
gsk_rounded_rect_offset (&box, x, y);
if (shadow->inset)
_gtk_rounded_box_shrink (&box, spread, spread, spread, spread);
gsk_rounded_rect_shrink (&box, spread, spread, spread, spread);
else /* Outset */
_gtk_rounded_box_grow (&box, spread, spread, spread, spread);
gsk_rounded_rect_shrink (&box, -spread, -spread, -spread, -spread);
clip_box = *padding_box;
_gtk_rounded_box_shrink (&clip_box, -clip_radius, -clip_radius, -clip_radius, -clip_radius);
gsk_rounded_rect_shrink (&clip_box, -clip_radius, -clip_radius, -clip_radius, -clip_radius);
if (!needs_blur (shadow))
draw_shadow (shadow, cr, &box, &clip_box, GTK_BLUR_NONE);

View File

@ -986,7 +986,7 @@ gtk_render_content_path (GtkStyleContext *context,
_gtk_rounded_box_init_rect (&box, x, y, width, height);
_gtk_rounded_box_apply_border_radius_for_style (&box, gtk_style_context_lookup_style (context), 0);
_gtk_rounded_box_shrink (&box,
gsk_rounded_rect_shrink (&box,
_gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_TOP_WIDTH), 100)
+ _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_PADDING_TOP), 100),
_gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH), 100)

View File

@ -555,12 +555,12 @@ gtk_theming_background_init (GtkThemingBackground *bg,
_gtk_rounded_box_apply_border_radius_for_style (&bg->boxes[GTK_CSS_AREA_BORDER_BOX], bg->style, junction);
bg->boxes[GTK_CSS_AREA_PADDING_BOX] = bg->boxes[GTK_CSS_AREA_BORDER_BOX];
_gtk_rounded_box_shrink (&bg->boxes[GTK_CSS_AREA_PADDING_BOX],
gsk_rounded_rect_shrink (&bg->boxes[GTK_CSS_AREA_PADDING_BOX],
border.top, border.right,
border.bottom, border.left);
bg->boxes[GTK_CSS_AREA_CONTENT_BOX] = bg->boxes[GTK_CSS_AREA_PADDING_BOX];
_gtk_rounded_box_shrink (&bg->boxes[GTK_CSS_AREA_CONTENT_BOX],
gsk_rounded_rect_shrink (&bg->boxes[GTK_CSS_AREA_CONTENT_BOX],
padding.top, padding.right,
padding.bottom, padding.left);
}

View File

@ -362,7 +362,7 @@ render_frame_fill (cairo_t *cr,
guint i, j;
padding_box = *border_box;
_gtk_rounded_box_shrink (&padding_box,
gsk_rounded_rect_shrink (&padding_box,
border_width[GTK_CSS_TOP],
border_width[GTK_CSS_RIGHT],
border_width[GTK_CSS_BOTTOM],
@ -486,7 +486,7 @@ render_frame_stroke (cairo_t *cr,
border_width[0] != border_width[3] ;
stroke_box = *border_box;
_gtk_rounded_box_shrink (&stroke_box,
gsk_rounded_rect_shrink (&stroke_box,
border_width[GTK_CSS_TOP] / 2.0,
border_width[GTK_CSS_RIGHT] / 2.0,
border_width[GTK_CSS_BOTTOM] / 2.0,
@ -513,7 +513,7 @@ render_frame_stroke (cairo_t *cr,
GskRoundedRect padding_box;
padding_box = *border_box;
_gtk_rounded_box_shrink (&padding_box,
gsk_rounded_rect_shrink (&padding_box,
border_width[GTK_CSS_TOP],
border_width[GTK_CSS_RIGHT],
border_width[GTK_CSS_BOTTOM],
@ -640,7 +640,7 @@ render_border (cairo_t *cr,
render_frame_fill (cr, border_box, other_border, colors, dont_draw);
other_box = *border_box;
_gtk_rounded_box_shrink (&other_box,
gsk_rounded_rect_shrink (&other_box,
2 * other_border[GTK_CSS_TOP],
2 * other_border[GTK_CSS_RIGHT],
2 * other_border[GTK_CSS_BOTTOM],
@ -674,7 +674,7 @@ render_border (cairo_t *cr,
render_frame_fill (cr, border_box, other_border, colors, dont_draw);
other_box = *border_box;
_gtk_rounded_box_shrink (&other_box,
gsk_rounded_rect_shrink (&other_box,
other_border[GTK_CSS_TOP],
other_border[GTK_CSS_RIGHT],
other_border[GTK_CSS_BOTTOM],

View File

@ -151,68 +151,6 @@ _gtk_rounded_box_apply_outline_radius_for_style (GskRoundedRect *box,
_gtk_rounded_box_apply_border_radius (box, corner, junction);
}
static void
gtk_css_border_radius_grow (graphene_size_t *corner,
double width,
double height)
{
if (corner->width)
corner->width += width;
if (corner->height)
corner->height += height;
if (corner->width <= 0 || corner->height <= 0)
{
corner->width = 0;
corner->height = 0;
}
}
void
_gtk_rounded_box_grow (GskRoundedRect *box,
double top,
double right,
double bottom,
double left)
{
if (box->bounds.size.width + left + right < 0)
{
box->bounds.origin.x -= left * box->bounds.size.width / (left + right);
box->bounds.size.width = 0;
}
else
{
box->bounds.origin.x -= left;
box->bounds.size.width += left + right;
}
if (box->bounds.size.height + bottom + top < 0)
{
box->bounds.origin.y -= top * box->bounds.size.height / (top + bottom);
box->bounds.size.height = 0;
}
else
{
box->bounds.origin.y -= top;
box->bounds.size.height += top + bottom;
}
gtk_css_border_radius_grow (&box->corner[GSK_CORNER_TOP_LEFT], left, top);
gtk_css_border_radius_grow (&box->corner[GSK_CORNER_TOP_RIGHT], right, top);
gtk_css_border_radius_grow (&box->corner[GSK_CORNER_BOTTOM_RIGHT], right, bottom);
gtk_css_border_radius_grow (&box->corner[GSK_CORNER_BOTTOM_LEFT], left, bottom);
}
void
_gtk_rounded_box_shrink (GskRoundedRect *box,
double top,
double right,
double bottom,
double left)
{
_gtk_rounded_box_grow (box, -top, -right, -bottom, -left);
}
typedef struct {
double angle1;
double angle2;

View File

@ -42,17 +42,6 @@ void _gtk_rounded_box_apply_outline_radius_for_style (GskRoundedRect
GtkCssStyle *style,
GtkJunctionSides junction);
void _gtk_rounded_box_grow (GskRoundedRect *box,
double top,
double right,
double bottom,
double left);
void _gtk_rounded_box_shrink (GskRoundedRect *box,
double top,
double right,
double bottom,
double left);
double _gtk_rounded_box_guess_length (const GskRoundedRect *box,
GtkCssSide side);