Establishing the size of a picture is sometimes needed for complex layout, and the Size algebra provides this.
The core method,
boundingBox, gets the BoundingBox of a picture.
import doodle.core.* import doodle.java2d.* import doodle.syntax.all.* val circle = Picture .circle(100) .strokeColor(Color.midnightBlue) val circleBoundingBox: Picture[BoundingBox] = circle.boundingBox
As the type annotation suggests, this returns a
Picture[BoundingBox] rather than a
In other words, the
BoundingBox will be calculated when the
Picture is rendered and is not immediately available.
This is necessary because the size may depend on backend-specific calculations, such as the space occupied by text.
To make use of the
BoundingBox in the creation of a picture we can use the
flatMap method on
val boundingBox = circleBoundingBox.flatMap(bb => Picture .roundedRectangle(bb.width, bb.height, 3.0) .strokeColor(Color.hotpink) .strokeWidth(3.0) ) // boundingBox: Picture[FromGifBase64 & Basic & Bitmap & FromBufferedImage & FromPngBase64 & FromJpgBase64, Unit] = doodle.algebra.Picture$$anon$1@54300362 val picture = circle.on(boundingBox) // picture: Picture[Bitmap & FromBufferedImage & FromPngBase64 & FromGifBase64 & Basic & FromJpgBase64, Unit] = doodle.syntax.LayoutSyntax$$anon$1@61584692
This produces the picture shown below.
Size algebra defines a few utilities derived from the
widthgets the height of the enclosing bounding box, as a
heightgets the height of the enclosing bounding box, as a
sizegets the width and height of the enclosing bounding box, as a tuple
Size algebra supports all the features described above.
Image does not currently support these operations.