Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support measuring text with font weight #884

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/picasso.js/src/core/scene-graph/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const mappedAttributes = {
strokeLinejoin: 'stroke-linejoin',
fontFamily: 'font-family',
fontSize: 'font-size',
fontStyle: 'font-style',
textDecoration: 'text-decoration',
fontWeight: 'font-weight',
baseline: 'dominant-baseline', // Special case where we have defined our own attribute name
dominantBaseline: 'dominant-baseline',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import { detectTextDirection, flipTextAnchor } from '../../../../core/utils/rtl-

export default function render(t, { g, ellipsed, doStroke }) {
const text = ellipsed || ellipsText(t, measureText);
if (t['font-weight']) {
g.font = `${t['font-weight']} ${t['font-size']} ${t['font-family']}`;
} else {
g.font = `${t['font-size']} ${t['font-family']}`;
}
g.font = [t['font-style'], t['font-weight'], t['font-size'], t['font-family']].filter((v) => !!v).join(' ');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking here but it does not seem optional from a performance perspective to

  1. Create an array
  2. filter to create another array
  3. join array

...everytime we render a text.

I would think string concat is a faster and provides a somewhat clean syntax. Some simpified tested in the browser console at least gave to a 50% performance increase compered to current method.

Example:

g.font = (t["font-style"] ?? "").concat(t['font-weight'] ?? ", t['font-size'] ?? "", t['font-family'] ?? "")

const dir = detectTextDirection(t.text);
if (g.canvas.dir !== dir) {
g.canvas.dir = dir;
Expand Down
30 changes: 21 additions & 9 deletions packages/picasso.js/src/web/text-manipulation/text-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@ function setContext() {
context = context || document.createElement('canvas').getContext('2d');
}

function setFont(fontSize, fontFamily) {
if (contextCache.fontSize === fontSize && contextCache.fontFamily === fontFamily) {
function isEqual(s1, s2) {
return (!s1 && !s2) || s1 === s2;
}

function setFont({ fontStyle, fontWeight, fontSize, fontFamily }) {
if (
isEqual(contextCache.fontStyle, fontStyle) &&
isEqual(contextCache.fontWeight, fontWeight) &&
isEqual(contextCache.fontSize, fontSize) &&
isEqual(contextCache.fontFamily, fontFamily)
) {
return;
}

context.font = fontSize + ' ' + fontFamily; // eslint-disable-line
context.font = [fontStyle, fontWeight, fontSize, fontFamily].filter((value) => !!value).join(' '); // eslint-disable-line
quanho marked this conversation as resolved.
Show resolved Hide resolved
contextCache.fontStyle = fontStyle;
contextCache.fontWeight = fontWeight;
contextCache.fontSize = fontSize;
contextCache.fontFamily = fontFamily;
}

function measureTextWidth(text, fontSize, fontFamily) {
const key = text + fontSize + fontFamily;
function measureTextWidth({ text, fontStyle, fontWeight, fontSize, fontFamily }) {
const key = text + [fontStyle, fontWeight, fontSize, fontFamily].filter((value) => !!value).join('');
quanho marked this conversation as resolved.
Show resolved Hide resolved
if (typeof widthCache[key] !== 'number') {
setContext();
setFont(fontSize, fontFamily);
setFont({ fontStyle, fontWeight, fontSize, fontFamily });
widthCache[key] = context.measureText(text).width;
}

Expand All @@ -52,6 +62,8 @@ 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
* measureText({
Expand All @@ -60,8 +72,8 @@ function measureTextHeight(fontSize) {
* fontFamily: 'Arial'
* }); // returns { width: 20, height: 12 }
*/
export function measureText({ text, fontSize, fontFamily }) {
const w = measureTextWidth(text, fontSize, fontFamily);
export function measureText({ text, fontSize, fontFamily, fontStyle, fontWeight }) {
const w = measureTextWidth({ text, fontSize, fontFamily, fontStyle, fontWeight });
quanho marked this conversation as resolved.
Show resolved Hide resolved
const h = measureTextHeight(fontSize);
return { width: w, height: h };
}
Expand Down