SkRRect Reference


class SkRRect {
public:
    SkRRect() = default;
    SkRRect(const SkRRect& rrect) = default;
    SkRRect& operator=(const SkRRect& rrect) = default;

    enum Type {
        kEmpty_Type,
        kRect_Type,
        kOval_Type,
        kSimple_Type,
        kNinePatch_Type,
        kComplex_Type,
        kLastType       = kComplex_Type,
    };

    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;
    SkScalar height() const;
    SkVector getSimpleRadii() const;
    void setEmpty();
    void setRect(const SkRect& rect);
    static SkRRect MakeEmpty();
    static SkRRect MakeRect(const SkRect& r);
    static SkRRect MakeOval(const SkRect& oval);
    static SkRRect MakeRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad);
    void setOval(const SkRect& oval);
    void setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad);
    void setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
                      SkScalar rightRad, SkScalar bottomRad);
    void setRectRadii(const SkRect& rect, const SkVector radii[4]);

    enum Corner {
        kUpperLeft_Corner,
        kUpperRight_Corner,
        kLowerRight_Corner,
        kLowerLeft_Corner,
    };

    const SkRect& rect() const;
    SkVector radii(Corner corner) const;
    const SkRect& getBounds() const;
    friend bool operator==(const SkRRect& a, const SkRRect& b);
    friend bool operator!=(const SkRRect& a, const SkRRect& b);
    void inset(SkScalar dx, SkScalar dy, SkRRect* dst) const;
    void inset(SkScalar dx, SkScalar dy);
    void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const;
    void outset(SkScalar dx, SkScalar dy);
    void offset(SkScalar dx, SkScalar dy);
    SkRRect makeOffset(SkScalar dx, SkScalar dy) const;
    bool contains(const SkRect& rect) const;
    bool isValid() const;

    static constexpr size_t kSizeInMemory = 12 * sizeof(SkScalar);

    size_t writeToMemory(void* buffer) const;
    size_t readFromMemory(const void* buffer, size_t length);
    bool transform(const SkMatrix& matrix, SkRRect* dst) const;
    void dump(bool asHex) const;
    void dump() const;
    void dumpHex() const;
};

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.


SkRRect()

Initializes bounds at (0, 0), the origin, with zero width and height. Initializes corner radii to (0, 0), and sets type of kEmpty_Type.

Return Value

empty SkRRect

Example

See Also

setEmpty isEmpty


SkRRect(const SkRRect& rrect)

Initializes to copy of rrect bounds and corner radii.

Parameters

rrect bounds and corner to copy

Return Value

copy of rrect

Example

See Also

operator=(const SkRRect& rrect) MakeRect


SkRRect& operator=(const SkRRect& rrect)

Copies rrect bounds and corner radii.

Parameters

rrect bounds and corner to copy

Return Value

copy of rrect

Example

See Also

SkRRect(const SkRRect& rrect) MakeRect


    enum Type {
        kEmpty_Type,
        kRect_Type,
        kOval_Type,
        kSimple_Type,
        kNinePatch_Type,
        kComplex_Type,
        kLastType = kComplex_Type,
    };

Type describes possible specializations of Round_Rect. Each Type is exclusive; a Round_Rect may only have one type.

Type members become progressively less restrictive; larger values of Type have more degrees of freedom than smaller values.

Constants

Const Value Description
SkRRect::kEmpty_Type 0 Round_Rect has zero width or height. All radii are zero.
SkRRect::kRect_Type 1 Round_Rect has width and height. All radii are zero.
SkRRect::kOval_Type 2 Round_Rect has width and height. All four x-radii are equal, and at least half the width. All four y-radii are equal, and at least half the height.
SkRRect::kSimple_Type 3 Round_Rect has width and height. All four x-radii are equal and greater than zero, and all four y-radii are equal and greater than zero. Either x-radii are less than half the width, or y-radii is less than half the height, or both.
SkRRect::kNinePatch_Type 4 Round_Rect has width and height. Left x-radii are equal, top y-radii are equal, right x-radii are equal, and bottom y-radii are equal. The radii do not describe Rect, Oval, or simple type. The centers of the corner ellipses form an axis-aligned rectangle that divides the Round_Rect into nine rectangular patches; an interior rectangle, four edges, and four corners.
SkRRect::kComplex_Type 5 both radii are non-zero.
SkRRect::kLastType 5 largest Type value

Example

See Also

Rect Path


Type getType() const

Returns Type, one of: kEmpty_Type, kRect_Type, kOval_Type, kSimple_Type, kNinePatch_Type, kComplex_Type .

Return Value

Type

Example

rrect2 is not a Rect; inset() has made it empty.

See Also

Type type


Type type() const

Returns Type, one of: kEmpty_Type, kRect_Type, kOval_Type, kSimple_Type, kNinePatch_Type, kComplex_Type .

Return Value

Type

Example

inset() has made rrect2 empty.

See Also

Type getType


bool isEmpty() const

Example

See Also

SkRect::isEmpty height width


bool isRect() const

Example

See Also

isEmpty radii


bool isOval() const

Example

The first radii are scaled down proportionately until both x-axis and y-axis fit within the bounds. After scaling, x-axis radius is smaller than half the width; left Round_Rect is not an oval. The second radii are equal to half the dimensions; right Round_Rect is an oval.

See Also

isEmpty isSimple SkCanvas::drawOval


bool isSimple() const

Example

See Also

isEmpty isRect isOval isNinePatch


bool isNinePatch() const

Example

See Also

isEmpty isRect isOval isSimple isComplex


bool isComplex() const

Example

See Also

isEmpty isRect isOval isSimple isNinePatch


SkScalar width() const

Returns span on the x-axis. This does not check if result fits in 32-bit float; result may be infinity.

Return Value

rect().fRight minus rect().fLeft

Example

SkRRect::MakeRect sorts its input, so width() is always zero or larger.

Example Output

unsorted width: 5
large width: inf

See Also

SkRect::width height getBounds


SkScalar height() const

Returns span on the y-axis. This does not check if result fits in 32-bit float; result may be infinity.

Return Value

rect().fBottom minus rect().fTop

Example

SkRRect::MakeRect sorts its input, so height() is always zero or larger.

Example Output

unsorted height: 5
large height: inf

See Also

SkRect::height width getBounds


SkVector getSimpleRadii() const

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.

Return Value

corner radii for simple types

Example

See Also

radii getBounds getType isSimple


void setEmpty()

Sets bounds to zero width and height at (0, 0), the origin. Sets corner radii to zero and sets type to kEmpty_Type.

Example

Nothing blue is drawn because Round_Rect is set to empty.

See Also

MakeEmpty setRect


void setRect(const SkRect& rect)

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

rect bounds to set

Example

See Also

MakeRect setRectXY


static SkRRect MakeEmpty()

Initializes bounds at (0, 0), the origin, with zero width and height. Initializes corner radii to (0, 0), and sets type of kEmpty_Type.

Return Value

empty SkRRect

Example

See Also

SkRRect() SkRect::MakeEmpty


static SkRRect MakeRect(const SkRect& r)

Initializes to copy of r bounds and zeroes corner radii.

Parameters

r bounds to copy

Return Value

copy of r

Example

See Also

setRect MakeOval MakeRectXY


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(). If oval bounds is empty, sets to kEmpty_Type. Otherwise, sets to kOval_Type.

Parameters

oval bounds of oval

Return Value

oval

Example

See Also

setOval MakeRect MakeRectXY


static SkRRect MakeRectXY(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 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

rect bounds of rounded rectangle
xRad x-axis radius of corners
yRad y-axis radius of corners

Return Value

rounded rectangle

Example

See Also

setRectXY


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(). If oval bounds is empty, sets to kEmpty_Type. Otherwise, sets to kOval_Type.

Parameters

oval bounds of oval

Example

See Also

MakeOval


void 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

rect bounds of rounded rectangle
xRad x-axis radius of corners
yRad y-axis radius of corners

Example

See Also

MakeRectXY SkPath::addRoundRect


void 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

rect bounds of rounded rectangle
leftRad left-top and left-bottom x-axis radius
topRad left-top and right-top y-axis radius
rightRad right-top and right-bottom x-axis radius
bottomRad left-bottom and right-bottom y-axis radius

Example

See Also

setRectRadii


void 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

rect bounds of rounded rectangle
radii corner x-axis and y-axis radii

Example

See Also

setNinePatch SkPath::addRoundRect


    enum Corner {
        kUpperLeft_Corner,
        kUpperRight_Corner,
        kLowerRight_Corner,
        kLowerLeft_Corner,
    };

The radii are stored: top-left, top-right, bottom-right, bottom-left.

Constants

Const Value Description
SkRRect::kUpperLeft_Corner 0 index of top-left corner radii
SkRRect::kUpperRight_Corner 1 index of top-right corner radii
SkRRect::kLowerRight_Corner 2 index of bottom-right corner radii
SkRRect::kLowerLeft_Corner 3 index of bottom-left corner radii

Example

See Also

radii


const SkRect& rect() const

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().

Return Value

bounding box

Example

#### Example Output ~~~~ left bounds: (nan) 0 left bounds: (inf) 0 left bounds: (100) 60 left bounds: (50) 50 left bounds: (25) 25 ~~~~

See Also

getBounds


SkVector radii(Corner corner) const

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.

Parameters

corner one of: kUpperLeft_Corner, kUpperRight_Corner,

kLowerRight_Corner, kLowerLeft_Corner

Return Value

x-axis and y-axis radii for one corner

Example

Finite values are scaled proportionately to fit; other values are set to zero. Scaled values cannot be larger than 25, half the bounding Round_Rect width. Small scaled values are halved to scale in proportion to the y-axis corner radius, which is twice the bounds height.

Example Output

left corner: (nan) 0
left corner: (inf) 0
left corner: (100) 25
left corner: (50) 25
left corner: (25) 12.5

See Also

Corner


const SkRect& getBounds() const

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().

Return Value

bounding box

Example

See Also

rect


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

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

a SkRect bounds and radii to compare
b SkRect bounds and radii to compare

Return Value

true if members are equal

Example

See Also

operator!=(const SkRRect& a, const SkRRect& b)


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.

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

Parameters

a SkRect bounds and radii to compare
b SkRect bounds and radii to compare

Return Value

true if members are not equal

Example

See Also

operator==(const SkRRect& a, const SkRRect& b)


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. 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

dx added to rect().fLeft, and subtracted from rect().fRight
dy added to rect().fTop, and subtracted from rect().fBottom
dst insets bounds and radii

Example

See Also

outset offset makeOffset


void inset(SkScalar dx, SkScalar dy)

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

dx added to rect().fLeft, and subtracted from rect().fRight
dy added to rect().fTop, and subtracted from rect().fBottom

Example

See Also

outset offset makeOffset


void outset(SkScalar dx, SkScalar dy, SkRRect* dst) const

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

dx subtracted from rect().fLeft, and added to rect().fRight
dy subtracted from rect().fTop, and added to rect().fBottom
dst outset bounds and radii

Example

See Also

inset offset makeOffset


void outset(SkScalar dx, SkScalar dy)

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

dx subtracted from rect().fLeft, and added to rect().fRight
dy subtracted from rect().fTop, and added to rect().fBottom

Example

See Also

inset offset makeOffset


void offset(SkScalar dx, SkScalar dy)

Translates SkRRect by (dx, dy).

Parameters

dx offset added to rect().fLeft and rect().fRight
dy offset added to rect().fTop and rect().fBottom

Example

See Also

makeOffset inset outset


SkRRect makeOffset(SkScalar dx, SkScalar dy) const

Returns SkRRect translated by (dx, dy).

Parameters

dx offset added to rect().fLeft and rect().fRight
dy offset added to rect().fTop and rect().fBottom

Return Value

SkRRect bounds offset by (dx, dy), with unchanged corner radii

Example

See Also

offset inset outset


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.

Parameters

rect area tested for containment

Return Value

true if SkRRect contains rect

Example

See Also

SkRect::contains


bool 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.

Return Value

true if bounds and radii match type()

Example

See Also

Type getType

Constants

Const Value Description
SkRRect::kSizeInMemory 48 Space required to write the contents of SkRRect into a buffer; always a multiple of four.


size_t writeToMemory(void* buffer) const

Writes SkRRect to buffer. Writes kSizeInMemory bytes, and returns kSizeInMemory, the number of bytes written.

Parameters

buffer storage for SkRRect

Return Value

bytes written, kSizeInMemory

Example

See Also

readFromMemory


size_t 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

buffer memory to read from
length size of buffer

Return Value

bytes read, or 0 if length is less than kSizeInMemory

Example

See Also

writeToMemory


bool 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 other than scale and translate.

Asserts in debug builds if SkRRect equals dst.

Parameters

matrix SkMatrix specifying the transform
dst SkRRect to store the result

Return Value

true if transformation succeeded.

Example

See Also

SkPath::transform


void 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

asHex true if SkScalar values are written as hexadecimal

Example

#### Example Output ~~~~ SkRect::MakeLTRB(0.857143f, 0.666667f, 0.857143f, 0.666667f); const SkPoint corners[] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, }; SkRect::MakeLTRB(SkBits2Float(0x3f5b6db7), /* 0.857143 */ SkBits2Float(0x3f2aaaab), /* 0.666667 */ SkBits2Float(0x3f5b6db7), /* 0.857143 */ SkBits2Float(0x3f2aaaab) /* 0.666667 */); const SkPoint corners[] = { { SkBits2Float(0x00000000), SkBits2Float(0x00000000) }, /* 0.000000 0.000000 */ { SkBits2Float(0x00000000), SkBits2Float(0x00000000) }, /* 0.000000 0.000000 */ { SkBits2Float(0x00000000), SkBits2Float(0x00000000) }, /* 0.000000 0.000000 */ { SkBits2Float(0x00000000), SkBits2Float(0x00000000) }, /* 0.000000 0.000000 */ }; ~~~~

See Also

dumpHex SkRect::dump SkPath::dump SkPathMeasure::dump


void dump() const

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.

Example

#### Example Output ~~~~ SkRect::MakeLTRB(0.857143f, 0.666667f, 0.857143f, 0.666667f); const SkPoint corners[] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, }; rrect is not equal to copy ~~~~

See Also

dumpHex SkRect::dump SkPath::dump SkPathMeasure::dump


void dumpHex() const

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.

Example

#### Example Output ~~~~ SkRect::MakeLTRB(SkBits2Float(0x3f5b6db7), /* 0.857143 */ SkBits2Float(0x3f2aaaab), /* 0.666667 */ SkBits2Float(0x3f5b6db7), /* 0.857143 */ SkBits2Float(0x3f2aaaab) /* 0.666667 */); const SkPoint corners[] = { { SkBits2Float(0x00000000), SkBits2Float(0x00000000) }, /* 0.000000 0.000000 */ { SkBits2Float(0x00000000), SkBits2Float(0x00000000) }, /* 0.000000 0.000000 */ { SkBits2Float(0x00000000), SkBits2Float(0x00000000) }, /* 0.000000 0.000000 */ { SkBits2Float(0x00000000), SkBits2Float(0x00000000) }, /* 0.000000 0.000000 */ }; rrect is equal to copy ~~~~

See Also

dump SkRect::dumpHex SkPath::dumpHex