Skip to content

Commit

Permalink
refactor: remove fontStyle which does not affect measuring text
Browse files Browse the repository at this point in the history
  • Loading branch information
quanho committed Nov 14, 2024
1 parent 032de01 commit f70ada1
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/picasso.js/src/web/text-manipulation/text-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,25 @@ function isEqual(s1, s2) {
return (!s1 && !s2) || s1 === s2;
}

function setFont({ fontStyle, fontWeight, fontSize, fontFamily }) {
function setFont({ fontWeight, fontSize, fontFamily }) {
if (
isEqual(contextCache.fontStyle, fontStyle) &&
isEqual(contextCache.fontWeight, fontWeight) &&
isEqual(contextCache.fontSize, fontSize) &&
isEqual(contextCache.fontFamily, fontFamily)
) {
return;
}
context.font = [fontStyle, fontWeight, fontSize, fontFamily].filter((value) => !!value).join(' '); // eslint-disable-line
contextCache.fontStyle = fontStyle;
context.font = [fontWeight, fontSize, fontFamily].filter((value) => !!value).join(' '); // eslint-disable-line
contextCache.fontWeight = fontWeight;
contextCache.fontSize = fontSize;
contextCache.fontFamily = fontFamily;
}

function measureTextWidth({ text, fontStyle, fontWeight, fontSize, fontFamily }) {
const key = `${text} ${fontStyle} ${fontWeight} ${fontSize} ${fontFamily}`;
function measureTextWidth({ text, fontWeight, fontSize, fontFamily }) {
const key = `${text} ${fontWeight} ${fontSize} ${fontFamily}`;
if (typeof widthCache[key] !== 'number') {
setContext();
setFont({ fontStyle, fontWeight, fontSize, fontFamily });
setFont({ fontWeight, fontSize, fontFamily });
widthCache[key] = context.measureText(text).width;
}

Expand All @@ -62,7 +60,6 @@ function measureTextHeight(fontSize) {
* @param {string} opts.text - Text to measure
* @param {string} opts.fontSize - Font size with a unit definition, ex. 'px' or 'em'
* @param {string} opts.fontFamily - Font family
* @param {string} opts.fontStyle - Font style, e.g 'italic'
* @param {string} opts.fontWeight - Font weight, e.g. 'bold'
* @return {object} Width and height of text in pixels
* @example
Expand All @@ -72,8 +69,8 @@ function measureTextHeight(fontSize) {
* fontFamily: 'Arial'
* }); // returns { width: 20, height: 12 }
*/
export function measureText({ text, fontSize, fontFamily, fontStyle, fontWeight }) {
const w = measureTextWidth({ text, fontSize, fontFamily, fontStyle, fontWeight });
export function measureText({ text, fontSize, fontFamily, fontWeight }) {
const w = measureTextWidth({ text, fontSize, fontFamily, fontWeight });
const h = measureTextHeight(fontSize);
return { width: w, height: h };
}
Expand Down

0 comments on commit f70ada1

Please sign in to comment.