Skia
2D Graphics Library
SkRRect Class Reference

SkRRect describes a rounded rectangle with a bounds and a pair of radii for each corner. More...

#include <SkRRect.h>

Public Types

enum  Type {
  kEmpty_Type, kRect_Type, kOval_Type, kSimple_Type,
  kNinePatch_Type, kComplex_Type, kLastType = kComplex_Type
}
 
enum  Corner { kUpperLeft_Corner, kUpperRight_Corner, kLowerRight_Corner, kLowerLeft_Corner }
 

Public Member Functions

 SkRRect ()=default
 Initializes bounds at (0, 0), the origin, with zero width and height. More...
 
 SkRRect (const SkRRect &rrect)=default
 Initializes to copy of rrect bounds and corner radii. More...
 
SkRRectoperator= (const SkRRect &rrect)=default
 Copies rrect bounds and corner radii. More...
 
Type getType () const
 
Type type () const
 
bool isEmpty () const
 
bool isRect () const
 
bool isOval () const
 
bool isSimple () const
 
bool isNinePatch () const
 
bool isComplex () const
 
SkScalar width () const
 Returns span on the x-axis. More...
 
SkScalar height () const
 Returns span on the y-axis. More...
 
SkVector getSimpleRadii () const
 Returns top-left corner radii. More...
 
void setEmpty ()
 Sets bounds to zero width and height at (0, 0), the origin. More...
 
void setRect (const SkRect &rect)
 Sets bounds to sorted rect, and sets corner radii to zero. More...
 
void setOval (const SkRect &oval)
 Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii to half oval.height(). More...
 
void setRectXY (const SkRect &rect, SkScalar xRad, SkScalar yRad)
 Sets to rounded rectangle with the same radii for all four corners. More...
 
void setNinePatch (const SkRect &rect, SkScalar leftRad, SkScalar topRad, SkScalar rightRad, SkScalar bottomRad)
 Sets bounds to rect. More...
 
void setRectRadii (const SkRect &rect, const SkVector radii[4])
 Sets bounds to rect. More...
 
const SkRectrect () const
 Returns bounds. More...
 
SkVector radii (Corner corner) const
 Returns scalar pair for radius of curve on x-axis and y-axis for one corner. More...
 
const SkRectgetBounds () const
 Returns bounds. More...
 
void inset (SkScalar dx, SkScalar dy, SkRRect *dst) const
 Copies SkRRect to dst, then insets dst bounds by dx and dy, and adjusts dst radii by dx and dy. More...
 
void inset (SkScalar dx, SkScalar dy)
 Insets bounds by dx and dy, and adjusts radii by dx and dy. More...
 
void outset (SkScalar dx, SkScalar dy, SkRRect *dst) const
 Outsets dst bounds by dx and dy, and adjusts radii by dx and dy. More...
 
void outset (SkScalar dx, SkScalar dy)
 Outsets bounds by dx and dy, and adjusts radii by dx and dy. More...
 
void offset (SkScalar dx, SkScalar dy)
 Translates SkRRect by (dx, dy). More...
 
SkRRect SK_WARN_UNUSED_RESULT makeOffset (SkScalar dx, SkScalar dy) const
 Returns SkRRect translated by (dx, dy). More...
 
bool contains (const SkRect &rect) const
 Returns true if rect is inside the bounds and corner radii, and if SkRRect and rect are not empty. More...
 
bool isValid () const
 Returns true if bounds and radii values are finite and describe a SkRRect SkRRect::Type that matches getType(). More...
 
size_t writeToMemory (void *buffer) const
 Writes SkRRect to buffer. More...
 
size_t readFromMemory (const void *buffer, size_t length)
 Reads SkRRect from buffer, reading kSizeInMemory bytes. More...
 
bool transform (const SkMatrix &matrix, SkRRect *dst) const
 Transforms by SkRRect by matrix, storing result in dst. More...
 
void dump (bool asHex) const
 Writes text representation of SkRRect to standard output. More...
 
void dump () const
 Writes text representation of SkRRect to standard output. More...
 
void dumpHex () const
 Writes text representation of SkRRect to standard output. More...
 

Static Public Member Functions

static SkRRect MakeEmpty ()
 Initializes bounds at (0, 0), the origin, with zero width and height. More...
 
static SkRRect MakeRect (const SkRect &r)
 Initializes to copy of r bounds and zeroes corner radii. More...
 
static SkRRect MakeOval (const SkRect &oval)
 Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii to half oval.height(). More...
 
static SkRRect MakeRectXY (const SkRect &rect, SkScalar xRad, SkScalar yRad)
 Sets to rounded rectangle with the same radii for all four corners. More...
 

Static Public Attributes

static constexpr size_t kSizeInMemory = 12 * sizeof(SkScalar)
 

Friends

class SkPath
 
class SkRRectPriv
 
bool operator== (const SkRRect &a, const SkRRect &b)
 Returns true if bounds and radii in a are equal to bounds and radii in b. More...
 
bool operator!= (const SkRRect &a, const SkRRect &b)
 Returns true if bounds and radii in a are not equal to bounds and radii in b. More...
 

Detailed Description

SkRRect describes a rounded rectangle with a bounds and a pair of radii for each corner.

The bounds and radii can be set so that SkRRect describes: a rectangle with sharp corners; a circle; an oval; or a rectangle with one or more rounded corners.

SkRRect allows implementing CSS properties that describe rounded corners. SkRRect may have up to eight different radii, one for each axis on each of its four corners.

SkRRect may modify the provided parameters when initializing bounds and radii. If either axis radii is zero or less: radii are stored as zero; corner is square. If corner curves overlap, radii are proportionally reduced to fit within bounds.

Member Enumeration Documentation

◆ Corner

Enumerator
kUpperLeft_Corner 

index of top-left corner radii

kUpperRight_Corner 

index of top-right corner radii

kLowerRight_Corner 

index of bottom-right corner radii

kLowerLeft_Corner 

index of bottom-left corner radii

◆ Type

Enumerator
kEmpty_Type 

zero width or height

kRect_Type 

non-zero width and height, and zeroed radii

kOval_Type 

non-zero width and height filled with radii

kSimple_Type 

non-zero width and height with equal radii

kNinePatch_Type 

non-zero width and height with axis-aligned radii

kComplex_Type 

non-zero width and height with arbitrary radii

kLastType 

largest Type value

Constructor & Destructor Documentation

◆ SkRRect() [1/2]

SkRRect::SkRRect ( )
default

Initializes bounds at (0, 0), the origin, with zero width and height.

Initializes corner radii to (0, 0), and sets type of kEmpty_Type.

Returns
empty SkRRect

◆ SkRRect() [2/2]

SkRRect::SkRRect ( const SkRRect rrect)
default

Initializes to copy of rrect bounds and corner radii.

Parameters
rrectbounds and corner to copy
Returns
copy of rrect

Member Function Documentation

◆ contains()

bool SkRRect::contains ( const SkRect rect) const

Returns true if rect is inside the bounds and corner radii, and if SkRRect and rect are not empty.

Parameters
rectarea tested for containment
Returns
true if SkRRect contains rect

example: https://fiddle.skia.org/c/@RRect_contains

◆ dump() [1/2]

void SkRRect::dump ( bool  asHex) const

Writes text representation of SkRRect to standard output.

Set asHex true to generate exact binary representations of floating point numbers.

Parameters
asHextrue if SkScalar values are written as hexadecimal

example: https://fiddle.skia.org/c/@RRect_dump

◆ dump() [2/2]

void SkRRect::dump ( ) const
inline

Writes text representation of SkRRect to standard output.

The representation may be directly compiled as C++ code. Floating point values are written with limited precision; it may not be possible to reconstruct original SkRRect from output.

◆ dumpHex()

void SkRRect::dumpHex ( ) const
inline

Writes text representation of SkRRect to standard output.

The representation may be directly compiled as C++ code. Floating point values are written in hexadecimal to preserve their exact bit pattern. The output reconstructs the original SkRRect.

◆ getBounds()

const SkRect& SkRRect::getBounds ( ) const
inline

Returns bounds.

Bounds may have zero width or zero height. Bounds right is greater than or equal to left; bounds bottom is greater than or equal to top. Result is identical to rect().

Returns
bounding box

◆ getSimpleRadii()

SkVector SkRRect::getSimpleRadii ( ) const
inline

Returns top-left corner radii.

If type() returns kEmpty_Type, kRect_Type, kOval_Type, or kSimple_Type, returns a value representative of all corner radii. If type() returns kNinePatch_Type or kComplex_Type, at least one of the remaining three corners has a different value.

Returns
corner radii for simple types

◆ getType()

Type SkRRect::getType ( ) const
inline

◆ height()

SkScalar SkRRect::height ( ) const
inline

Returns span on the y-axis.

This does not check if result fits in 32-bit float; result may be infinity.

Returns
rect().fBottom minus rect().fTop

◆ inset() [1/2]

void SkRRect::inset ( SkScalar  dx,
SkScalar  dy,
SkRRect dst 
) const

Copies SkRRect to dst, then insets dst bounds by dx and dy, and adjusts dst radii by dx and dy.

dx and dy may be positive, negative, or zero. dst may be SkRRect.

If either corner radius is zero, the corner has no curvature and is unchanged. Otherwise, if adjusted radius becomes negative, pins radius to zero. If dx exceeds half dst bounds width, dst bounds left and right are set to bounds x-axis center. If dy exceeds half dst bounds height, dst bounds top and bottom are set to bounds y-axis center.

If dx or dy cause the bounds to become infinite, dst bounds is zeroed.

Parameters
dxadded to rect().fLeft, and subtracted from rect().fRight
dyadded to rect().fTop, and subtracted from rect().fBottom
dstinsets bounds and radii

example: https://fiddle.skia.org/c/@RRect_inset

◆ inset() [2/2]

void SkRRect::inset ( SkScalar  dx,
SkScalar  dy 
)
inline

Insets bounds by dx and dy, and adjusts radii by dx and dy.

dx and dy may be positive, negative, or zero.

If either corner radius is zero, the corner has no curvature and is unchanged. Otherwise, if adjusted radius becomes negative, pins radius to zero. If dx exceeds half bounds width, bounds left and right are set to bounds x-axis center. If dy exceeds half bounds height, bounds top and bottom are set to bounds y-axis center.

If dx or dy cause the bounds to become infinite, bounds is zeroed.

Parameters
dxadded to rect().fLeft, and subtracted from rect().fRight
dyadded to rect().fTop, and subtracted from rect().fBottom

◆ isComplex()

bool SkRRect::isComplex ( ) const
inline

◆ isEmpty()

bool SkRRect::isEmpty ( ) const
inline

◆ isNinePatch()

bool SkRRect::isNinePatch ( ) const
inline

◆ isOval()

bool SkRRect::isOval ( ) const
inline

◆ isRect()

bool SkRRect::isRect ( ) const
inline

◆ isSimple()

bool SkRRect::isSimple ( ) const
inline

◆ isValid()

bool SkRRect::isValid ( ) const

Returns true if bounds and radii values are finite and describe a SkRRect SkRRect::Type that matches getType().

All SkRRect methods construct valid types, even if the input values are not valid. Invalid SkRRect data can only be generated by corrupting memory.

Returns
true if bounds and radii match type()

example: https://fiddle.skia.org/c/@RRect_isValid

◆ MakeEmpty()

static SkRRect SkRRect::MakeEmpty ( )
inlinestatic

Initializes bounds at (0, 0), the origin, with zero width and height.

Initializes corner radii to (0, 0), and sets type of kEmpty_Type.

Returns
empty SkRRect

◆ makeOffset()

SkRRect SK_WARN_UNUSED_RESULT SkRRect::makeOffset ( SkScalar  dx,
SkScalar  dy 
) const
inline

Returns SkRRect translated by (dx, dy).

Parameters
dxoffset added to rect().fLeft and rect().fRight
dyoffset added to rect().fTop and rect().fBottom
Returns
SkRRect bounds offset by (dx, dy), with unchanged corner radii

◆ MakeOval()

static SkRRect SkRRect::MakeOval ( const SkRect oval)
inlinestatic

Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii to half oval.height().

If oval bounds is empty, sets to kEmpty_Type. Otherwise, sets to kOval_Type.

Parameters
ovalbounds of oval
Returns
oval

◆ MakeRect()

static SkRRect SkRRect::MakeRect ( const SkRect r)
inlinestatic

Initializes to copy of r bounds and zeroes corner radii.

Parameters
rbounds to copy
Returns
copy of r

◆ MakeRectXY()

static SkRRect SkRRect::MakeRectXY ( const SkRect rect,
SkScalar  xRad,
SkScalar  yRad 
)
inlinestatic

Sets to rounded rectangle with the same radii for all four corners.

If rect is empty, sets to kEmpty_Type. Otherwise, if xRad and yRad are zero, sets to kRect_Type. Otherwise, if xRad is at least half rect.width() and yRad is at least half rect.height(), sets to kOval_Type. Otherwise, sets to kSimple_Type.

Parameters
rectbounds of rounded rectangle
xRadx-axis radius of corners
yRady-axis radius of corners
Returns
rounded rectangle

◆ offset()

void SkRRect::offset ( SkScalar  dx,
SkScalar  dy 
)
inline

Translates SkRRect by (dx, dy).

Parameters
dxoffset added to rect().fLeft and rect().fRight
dyoffset added to rect().fTop and rect().fBottom

◆ operator=()

SkRRect& SkRRect::operator= ( const SkRRect rrect)
default

Copies rrect bounds and corner radii.

Parameters
rrectbounds and corner to copy
Returns
copy of rrect

◆ outset() [1/2]

void SkRRect::outset ( SkScalar  dx,
SkScalar  dy,
SkRRect dst 
) const
inline

Outsets dst bounds by dx and dy, and adjusts radii by dx and dy.

dx and dy may be positive, negative, or zero.

If either corner radius is zero, the corner has no curvature and is unchanged. Otherwise, if adjusted radius becomes negative, pins radius to zero. If dx exceeds half dst bounds width, dst bounds left and right are set to bounds x-axis center. If dy exceeds half dst bounds height, dst bounds top and bottom are set to bounds y-axis center.

If dx or dy cause the bounds to become infinite, dst bounds is zeroed.

Parameters
dxsubtracted from rect().fLeft, and added to rect().fRight
dysubtracted from rect().fTop, and added to rect().fBottom
dstoutset bounds and radii

◆ outset() [2/2]

void SkRRect::outset ( SkScalar  dx,
SkScalar  dy 
)
inline

Outsets bounds by dx and dy, and adjusts radii by dx and dy.

dx and dy may be positive, negative, or zero.

If either corner radius is zero, the corner has no curvature and is unchanged. Otherwise, if adjusted radius becomes negative, pins radius to zero. If dx exceeds half bounds width, bounds left and right are set to bounds x-axis center. If dy exceeds half bounds height, bounds top and bottom are set to bounds y-axis center.

If dx or dy cause the bounds to become infinite, bounds is zeroed.

Parameters
dxsubtracted from rect().fLeft, and added to rect().fRight
dysubtracted from rect().fTop, and added to rect().fBottom

◆ radii()

SkVector SkRRect::radii ( Corner  corner) const
inline

Returns scalar pair for radius of curve on x-axis and y-axis for one corner.

Both radii may be zero. If not zero, both are positive and finite.

Returns
x-axis and y-axis radii for one corner

◆ readFromMemory()

size_t SkRRect::readFromMemory ( const void *  buffer,
size_t  length 
)

Reads SkRRect from buffer, reading kSizeInMemory bytes.

Returns kSizeInMemory, bytes read if length is at least kSizeInMemory. Otherwise, returns zero.

Parameters
buffermemory to read from
lengthsize of buffer
Returns
bytes read, or 0 if length is less than kSizeInMemory

example: https://fiddle.skia.org/c/@RRect_readFromMemory

◆ rect()

const SkRect& SkRRect::rect ( ) const
inline

Returns bounds.

Bounds may have zero width or zero height. Bounds right is greater than or equal to left; bounds bottom is greater than or equal to top. Result is identical to getBounds().

Returns
bounding box

◆ setEmpty()

void SkRRect::setEmpty ( )
inline

Sets bounds to zero width and height at (0, 0), the origin.

Sets corner radii to zero and sets type to kEmpty_Type.

◆ setNinePatch()

void SkRRect::setNinePatch ( const SkRect rect,
SkScalar  leftRad,
SkScalar  topRad,
SkScalar  rightRad,
SkScalar  bottomRad 
)

Sets bounds to rect.

Sets radii to (leftRad, topRad), (rightRad, topRad), (rightRad, bottomRad), (leftRad, bottomRad).

If rect is empty, sets to kEmpty_Type. Otherwise, if leftRad and rightRad are zero, sets to kRect_Type. Otherwise, if topRad and bottomRad are zero, sets to kRect_Type. Otherwise, if leftRad and rightRad are equal and at least half rect.width(), and topRad and bottomRad are equal at least half rect.height(), sets to kOval_Type. Otherwise, if leftRad and rightRad are equal, and topRad and bottomRad are equal, sets to kSimple_Type. Otherwise, sets to kNinePatch_Type.

Nine patch refers to the nine parts defined by the radii: one center rectangle, four edge patches, and four corner patches.

Parameters
rectbounds of rounded rectangle
leftRadleft-top and left-bottom x-axis radius
topRadleft-top and right-top y-axis radius
rightRadright-top and right-bottom x-axis radius
bottomRadleft-bottom and right-bottom y-axis radius

◆ setOval()

void SkRRect::setOval ( const SkRect oval)
inline

Sets bounds to oval, x-axis radii to half oval.width(), and all y-axis radii to half oval.height().

If oval bounds is empty, sets to kEmpty_Type. Otherwise, sets to kOval_Type.

Parameters
ovalbounds of oval

◆ setRect()

void SkRRect::setRect ( const SkRect rect)
inline

Sets bounds to sorted rect, and sets corner radii to zero.

If set bounds has width and height, and sets type to kRect_Type; otherwise, sets type to kEmpty_Type.

Parameters
rectbounds to set

◆ setRectRadii()

void SkRRect::setRectRadii ( const SkRect rect,
const SkVector  radii[4] 
)

Sets bounds to rect.

Sets radii array for individual control of all for corners.

If rect is empty, sets to kEmpty_Type. Otherwise, if one of each corner radii are zero, sets to kRect_Type. Otherwise, if all x-axis radii are equal and at least half rect.width(), and all y-axis radii are equal at least half rect.height(), sets to kOval_Type. Otherwise, if all x-axis radii are equal, and all y-axis radii are equal, sets to kSimple_Type. Otherwise, sets to kNinePatch_Type.

Parameters
rectbounds of rounded rectangle
radiicorner x-axis and y-axis radii

example: https://fiddle.skia.org/c/@RRect_setRectRadii

◆ setRectXY()

void SkRRect::setRectXY ( const SkRect rect,
SkScalar  xRad,
SkScalar  yRad 
)

Sets to rounded rectangle with the same radii for all four corners.

If rect is empty, sets to kEmpty_Type. Otherwise, if xRad or yRad is zero, sets to kRect_Type. Otherwise, if xRad is at least half rect.width() and yRad is at least half rect.height(), sets to kOval_Type. Otherwise, sets to kSimple_Type.

Parameters
rectbounds of rounded rectangle
xRadx-axis radius of corners
yRady-axis radius of corners

example: https://fiddle.skia.org/c/@RRect_setRectXY

◆ transform()

bool SkRRect::transform ( const SkMatrix matrix,
SkRRect dst 
) const

Transforms by SkRRect by matrix, storing result in dst.

Returns true if SkRRect transformed can be represented by another SkRRect. Returns false if matrix contains transformations that are not axis aligned.

Asserts in debug builds if SkRRect equals dst.

Parameters
matrixSkMatrix specifying the transform
dstSkRRect to store the result
Returns
true if transformation succeeded.

example: https://fiddle.skia.org/c/@RRect_transform

◆ type()

Type SkRRect::type ( ) const
inline

◆ width()

SkScalar SkRRect::width ( ) const
inline

Returns span on the x-axis.

This does not check if result fits in 32-bit float; result may be infinity.

Returns
rect().fRight minus rect().fLeft

◆ writeToMemory()

size_t SkRRect::writeToMemory ( void *  buffer) const

Writes SkRRect to buffer.

Writes kSizeInMemory bytes, and returns kSizeInMemory, the number of bytes written.

Parameters
bufferstorage for SkRRect
Returns
bytes written, kSizeInMemory

example: https://fiddle.skia.org/c/@RRect_writeToMemory

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const SkRRect a,
const SkRRect b 
)
friend

Returns true if bounds and radii in a are not equal to bounds and radii in b.

a and b are not equal if either contain NaN. a and b are equal if members contain zeroes with different signs.

Parameters
aSkRect bounds and radii to compare
bSkRect bounds and radii to compare
Returns
true if members are not equal

◆ operator==

bool operator== ( const SkRRect a,
const SkRRect b 
)
friend

Returns true if bounds and radii in a are equal to bounds and radii in b.

a and b are not equal if either contain NaN. a and b are equal if members contain zeroes with different signs.

Parameters
aSkRect bounds and radii to compare
bSkRect bounds and radii to compare
Returns
true if members are equal

◆ SkPath

friend class SkPath
friend

◆ SkRRectPriv

friend class SkRRectPriv
friend

Member Data Documentation

◆ kSizeInMemory

constexpr size_t SkRRect::kSizeInMemory = 12 * sizeof(SkScalar)
static

The documentation for this class was generated from the following file: