demo(chart): Demonstrate text bounds measurement inconsistencies#2800
Draft
awahab07 wants to merge 1 commit into
Draft
demo(chart): Demonstrate text bounds measurement inconsistencies#2800awahab07 wants to merge 1 commit into
awahab07 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Draft / demonstration only — not intended to merge.
Adds a storybook story to demonstrate how canvas-based
measureText()fails to account for inherited CSS font properties (font-feature-settings,font-variant-numeric,letter-spacing,font-kerning), leading to incorrect text layout across multiple chart types.Details
Problem
All chart types use an off-screen canvas (
document.createElement('canvas')) for text measurement. This canvas is detached from the DOM, so it cannot inherit CSS font properties from parent elements. When properties likefont-feature-settings: 'tnum'orletter-spacingare applied by the consuming app (e.g. Kibana/Lens),measureText()still reports the non-featured width — causing a measurement-vs-rendering mismatch.Why this can't be fixed via
ctx.font: The Canvas 2Dctx.fontonly accepts CSSfontshorthand, which does not includefont-feature-settings,font-variant-numeric,letter-spacing, orfont-kerning. TheCanvasTextDrawingStylesinterface does exposeletterSpacing,fontKerning, andwordSpacing— but elastic-charts does not currently use them, andfontFeatureSettings/fontVariantNumerichave no canvas API equivalent at all.What the story demonstrates
The story (Test Cases → Font Measurement Test) includes 4 individually resizable charts with toggleable OpenType font features, letter-spacing, and font-kerning:
maximiseFontSize(), Y-axis widthKey observations
measureText(). The DOM text inherits all CSS properties; the measurement does not.letter-spacingandfont-kerningare completely absent from the measurement code — not accounted for at all, regardless of whether the canvas is in-DOM or off-screen.Browser quirks
font-feature-settingsfrom ancestor CSS and apply it duringmeasureText()andfillText(), but this behavior is not guaranteed by the spec and is inconsistent even within Chrome (e.g. treemap renders withtnumapplied, but heatmap does not, despite both using the same in-DOM canvas with identical computed styles).font-feature-settingsinto canvas text shaping at all —measureText()andfillText()ignore the canvas element's computedfont-feature-settings, even when directly set as an inline style on the element.ctx.fontvalues are "computed relative to the font of the canvas element", but does not explicitly state whether non-shorthand properties likefont-feature-settingsshould participate in canvas text shaping.How to test
Issues
Related to Kibana/Lens OpenType font feature adoption (elastic/kibana#251576).
Checklist