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

Improve parsing of the "font" field #1486

Merged

Conversation

matanui159
Copy link

This improves the handling of the "font" field on the context to properly parse out weight/style/size. Currently weight and style are unused since the font map only takes the family name into consideration.

@CedricGuillemet CedricGuillemet self-requested a review March 25, 2025 08:52
return font;
}

Font::operator std::string() const
Copy link
Member

Choose a reason for hiding this comment

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

Is this re-encoding the original string that was passed into Parse?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, my thinking was just to store the font object itself and then re-encode to a string when the getter is called

Copy link
Member

Choose a reason for hiding this comment

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

Would it make more sense to just store the original string (the one passed to Parse) and return it directly, so that we don't have to maintain code going both ways (rather only the parse direction)?

Copy link
Author

Choose a reason for hiding this comment

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

Happy to do that. This is closer to how the spec describes the getter for the font field though

@@ -90,7 +91,7 @@ namespace Babylon::Polyfills::Internal
NativeCanvas* m_canvas;
std::shared_ptr<NVGcontext*> m_nvg;

std::string m_font{};
Font m_font;
Copy link
Member

Choose a reason for hiding this comment

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

Should this be std::optional so we can easily tell when it is invalid?

Copy link
Author

Choose a reason for hiding this comment

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

Iirc in the spec if the font is invalid it keeps the old value

Copy link
Author

Choose a reason for hiding this comment

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

spec: https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-font

If the new value is syntactically incorrect [...], then it must be ignored, without assigning a new font value.

Also for above comment:

On getting, the font attribute must return the serialized form of the current font of the context (with no 'line-height' component).

Copy link
Member

Choose a reason for hiding this comment

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

Is this initialized upon construction of the Canvas, or does it need to be Font m_font{};?

Copy link
Author

Choose a reason for hiding this comment

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

Ye its initialised to its defaults. I can add the {} to be more explicit if you want though

@CedricGuillemet CedricGuillemet merged commit dfb0a8b into BabylonJS:CanvasTest Apr 7, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants