Refactored palette.js and view.js to reuse calculateTextWidth