logo
pub trait Dimensions {
    fn bounding_box(&self) -> Rectangle;
}
Expand description

Adds the ability to get the bounding box of an item.

The exact definition of the bounding box depends on the item:

  • Primitives (Rectangle, Circle, …)

    For unstyled primitives the bounding box is defined as the smallest rectangle that surrounds the entire primitive.

  • Styled primitives and other Drawables (Image, Text, …)

    The bounding box of a drawable is defined as the smallest rectangle that contains all drawn pixels. While all builtin Drawables in embedded-graphics provide an implementation of this trait, this might not be true for third party drawables.

    Note that a styled primitive can have a different bounding box than the underlying unstyled primitive; depending on the stroke width and alignment the bounding box of the styled primitive may be larger.

  • DrawTargets (displays, simulator, …)

    The bounding box of a draw target is defined as the area that should be used for drawing operations. For most display drivers the top left corner of the bounding box will be at the origin but other draw targets can have different positions of the top left corner.

The bounding box will be returned as a Rectangle. The methods provided by Rectangle make it easy to implement additional functions like hit testing (by using contains) or drawing a focus rectangle around a drawable (by converting the rectangle into a Styled).

Implementation notes

Dimensions should be implemented for Drawables if the bounding box is known before Drawable::draw is executed. The implementation must return a rectangle that contains all drawn pixels. MockDisplay::affected_area can be a used in unit tests to make sure a drawable returns a bounding box with the correct dimensions.

DrawTargets (display drivers, etc) are required to implement Dimensions. The implementation must return a rectangle representing the drawing area. For display drivers it is recommended to implement OriginDimensions instead of implementing Dimensions directly, if the top left corner of the display area is at the origin (0, 0).

The bounding box of ImageDrawables must always start at the origin, therefore OriginDimensions must be implemented instead of this trait.

Required methods

Returns the bounding box.

Implementors