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

[Core/Enhancements] Allow custom hash for measure text cache #279

Open
Riquelme02 opened this issue Feb 19, 2025 · 1 comment
Open

[Core/Enhancements] Allow custom hash for measure text cache #279

Riquelme02 opened this issue Feb 19, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@Riquelme02
Copy link

It would be very helpful to allow the user to manually set the
ID used for the text measuring cache, currently calculated by
`Clay__HashTextWithConfig', to avoid conflicts when you have
strings with the same font and size but differing properties
external to Clay.

Basically, change

uint32_t id = Clay__HashTextWithConfig(text, config);

...to something like...

(pseudocode)
uint32_t id = if(config.string_hash = 0)
Clay__HashTextWithConfig(text, config);
else
config.string_hash;

These are some properties from HarfBuzz that could make the same string
with the same font and size be measured differently: language and script
(may change glyphs), direction (may reorder glyphs), OpenType features
(may change glyphs in arbitrary ranges).

There's certainly an argument to add some of these properties to Clay,
but ultimately this is all implementation-specific, depending on your
needs you may need more or fewer properties or they may need to be
represented in different ways. This is why I believe the simplest
answer is to shift the responsibility to the user - if you have a
complex string, you're in charge of providing its unique identifier.

I have worked around this by writing my string ID to the font ID and size,
which is then hashed (again) by Clay and this presumably avoids any conflict,
but it's a hack: my hash is bigger than a uint16, so it needs to be spread
across two fields. While that part would be fixed by using a larger width
it would still be a misuse of those fields since we're not actually talking
about fonts or sizes.

Mildly related: I see there is an option to hash string contents, this is the
exact kind of issue that could have been alternatively solved by letting the
user provide his own hash.

@nicbarker
Copy link
Owner

Great idea, I like this a lot! Will look into it along with our custom text wrapping function for handling utf16, etc 🙂

@nicbarker nicbarker changed the title Allow custom hash for measure text cache [Core/Enhancements] Allow custom hash for measure text cache Feb 21, 2025
@nicbarker nicbarker added the enhancement New feature or request label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants