Bug 1115778 - Part 1: Shuffle around some math functions and Canvas polyfilling to ease implementation of part 2

Plus some slight drive-by cleanup.
Bug 1115778 - Part 2: Always draw crisp 1px lines for TextField bounds
Merge pull request #1974 from tschneidereit/master

Bug 1115778 - Always draw crisp 1px lines for TextField bounds