SmartArt
PowerPoint fits SmartArt by guessing, and you can see it in the numbers
SmartArt doesn't calculate the size that fits. It searches for it, halving the gap each step, and stops when it's close enough. That leaves fingerprints.
SmartArt is the feature in PowerPoint and Word where you type a list and get a diagram: a process with arrows, a cycle, an org chart, a pyramid. Behind it is a whole layout engine with its own language of constraints and rules. Microsoft documents the language. It doesn't document how the engine actually solves it.
When a diagram doesn't fit its frame, PowerPoint scales it down. My version did the obvious thing: compute how much too big it is and divide. The result was always very slightly off. Not by much. A fraction of a percent. But a fraction of a percent is enough to make a word wrap or not.
The numbers were too round
Yesterday I looked at the scale factors PowerPoint actually uses, and they have a strange pattern. A scale of 0.36 wasn't 0.36 or the exact value it should have been. It's 1484/4096. Other drawings land on multiples of 1/2048, 1/8192. Always a fraction with a power of two at the bottom.
That's the fingerprint of a binary search. PowerPoint doesn't calculate the scale. It tries one, checks whether everything fits, then halves the range and tries again:
- Start with the range 0 to 1.
- Try the middle. If it fits, keep the upper half. If not, keep the lower half.
- Stop when the range is narrower than 0.2% of its lower end.
- Use the lower end, which is known to fit.
Because it stops on a relative precision, small scales stop on finer fractions than large ones. Which is exactly the pattern in the numbers.
Implemented this way, it matches in 100% of drawings for a whole list of layouts. Basic Process, Venn, Chevrons, Equation and more.
Searching everywhere
Once I knew to look, the search is everywhere:
- Text with no size limit grows by doubling until it fits, then takes five halving steps back. So sizes land on a grid of 32 steps per doubling. A box that started at 20 mm ends at 67.5 mm, in steps of 1.25 mm.
- Boxes that must stay the same size grow one after another, each continuing the search from where the previous one stopped.
- Rings of shapes search for their spacing, not their radius.
Each of these explains a group of drawings that had been mysteriously off by a point or two.
Decisions at the natural size
The other big discovery of the day: PowerPoint makes its layout decisions before scaling. It first lays out the diagram at its "natural" size, with every text starting at 65 pt, decides what goes where, and only then scales the whole thing to the frame.
So a word that doesn't fit at 65 pt can change the whole layout, even though the final text is 14 pt. Looking at the final drawing, that makes no sense at all. Once you know about the natural size, it's obvious.
Why bother
Because otherwise you're forever adding fudge factors. Until yesterday, the code had little corrections like "multiply by 0.98 here". Most of them are gone now, replaced by the one thing PowerPoint actually does. A handful of layouts still don't match, mostly rings of shapes. Those are next.