Text rendering/layout cleanup. Needs more work but I'll commit a WIP version before going on holiday. Feel free to revert if it breaks something (passed my quick testing with no apparent functional changes).