Aug 28 2008
Diagrams: Encapsulating diagrams in other diagrams
One of the first things I wanted to do when I added basic text support to the Diagrams package was to put the text in speech bubbles. But it was really difficult for the same reason the text support in the first place was difficult — you couldn’t tell ahead of time how big the bounding box around the text would be. It all depends on font styles and whether you use monospaced or proportional fonts.
So my latest addition has been an “encapsulating” layout Wrap. It’s used with a single helper function called wrapWith. You pass in the inner Diagram that you want wrapped, and you pass in a function to create the outer Diagram. The function has a type signature of Double -> Double -> Diagram, where the two Doubles are the width and height respectively of the inner Diagram.
A short example. roundrect takes width and height arguments and creates a rectangle with rounded corners. With this we can write text into a box:
text 16 "Hello, World" `wrapWith` roundrect
The layout doesn’t add any padding, so the bubble looks a bit close around the text. To loosen things up a bit we can add some explicit padding:
pad 10 10 (text 16 "Hello, World") `wrapWith` roundrect
This gives the text a bit of room to breathe. If you want to adapt the padding around the inner diagram proportionally you can use the empty function, which creates a big invisible rectangle in the specific dimensions you specify:
-- Add 30 percent to the width and height around the text box. (text 16 "Hello, World") `wrapWith` (\x y -> empty (x*1.3) (y*1.3)) `wrapWith` roundrect
You can see that several diagrams can be wrapped around each other like onion skins. The three snippets produce these three results, respectively:
