hyper/app/text-metrics.js
2016-06-30 23:01:04 -07:00

20 lines
695 B
JavaScript

const mem = new Map();
export default function getTextMetrics (family, fontSize, lineHeight) {
const id = family + '#' + fontSize + '#' + lineHeight;
const memd = mem.get(id);
if (memd) return memd;
const el = document.createElement('span');
const style = el.style;
style.display = 'inline-block';
style.fontFamily = family;
style.fontSize = fontSize;
style.lineHeight = lineHeight;
el.innerText = 'X';
document.body.appendChild(el);
const { width, height } = el.getBoundingClientRect();
const ret = { width, height };
document.body.removeChild(el);
mem.set(id, ret);
console.log('text metrics calculated for', family, fontSize, lineHeight, ret);
return ret;
}