Skip to content

'toFont' helper can affect lineHeight if called with a result object from a previous 'toFont' invocation #11297

Description

@stockiNail

Feature Proposal

Currently, if you invoke toFont helpers function passing as an argument the result object (anyway a font object), the lineHeight property is not correct because the original lineHeight overridden.

const original = {
  size: 12
};
const first = Chart.helpers.toFont(original);
const second = Chart.helpers.toFont(first);

Result:

Original: {
   "size": 12
}

First: {
   "family": "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
   "lineHeight": 14.399999999999999,
   "size": 12,
   "style": "normal",
   "weight": null,
   "string": "normal 12px 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
}

Second: {
   "family": "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
   "lineHeight": 172.79999999999998,
   "size": 12,
   "style": "normal",
   "weight": null,
   "string": "normal 12px 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
}

I think the lineHeight property should maintain always the original value and adding another property for the result lineHeight.

Possible Implementation

Current Implementation (helpers.options.ts, row 130):

  const font = {
    family: valueOrDefault(options.family, fallback.family),
    lineHeight: toLineHeight(valueOrDefault(options.lineHeight, fallback.lineHeight), size),
    size,
    style,
    weight: valueOrDefault(options.weight, fallback.weight),
    string: ''
  };

  font.string = toFontString(font);
  return font;

Possible implementation;

  const font = {
    family: valueOrDefault(options.family, fallback.family),
    lineHeight: valueOrDefault(options.lineHeight, fallback.lineHeight),
    size,
    style,
    weight: valueOrDefault(options.weight, fallback.weight),
    // new calculated properties
    string: '',
    lh: toLineHeight(valueOrDefault(options.lineHeight, fallback.lineHeight), size),
  };

  font.string = toFontString(font);
  return font;

Of course, if implemented, it will be a breaking change.
What do you think?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions