Backport some FoggyText optimizations.