Skip to content

Commit

Permalink
[Core] Add option to hash text contents to text config (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicbarker authored Jan 29, 2025
1 parent 5fae7a6 commit a1e692b
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 9 deletions.
13 changes: 7 additions & 6 deletions bindings/odin/clay-odin/clay.odin
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ TextWrapMode :: enum EnumBackingType {
}

TextElementConfig :: struct {
textColor: Color,
fontId: u16,
fontSize: u16,
letterSpacing: u16,
lineHeight: u16,
wrapMode: TextWrapMode,
textColor: Color,
fontId: u16,
fontSize: u16,
letterSpacing: u16,
lineHeight: u16,
wrapMode: TextWrapMode,
hashStringContents: bool,
}

ImageElementConfig :: struct {
Expand Down
Binary file modified bindings/odin/clay-odin/linux/clay.a
Binary file not shown.
Binary file modified bindings/odin/clay-odin/macos-arm64/clay.a
Binary file not shown.
Binary file modified bindings/odin/clay-odin/macos/clay.a
Binary file not shown.
Binary file modified bindings/odin/clay-odin/wasm/clay.o
Binary file not shown.
Binary file modified bindings/odin/clay-odin/windows/clay.lib
Binary file not shown.
16 changes: 13 additions & 3 deletions clay.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ typedef struct {
uint16_t letterSpacing;
uint16_t lineHeight;
Clay_TextElementConfigWrapMode wrapMode;
bool hashStringContents;
#ifdef CLAY_EXTEND_CONFIG_TEXT
CLAY_EXTEND_CONFIG_TEXT
#endif
Expand Down Expand Up @@ -987,9 +988,18 @@ uint32_t Clay__HashTextWithConfig(Clay_String *text, Clay_TextElementConfig *con
uint32_t hash = 0;
uintptr_t pointerAsNumber = (uintptr_t)text->chars;

hash += pointerAsNumber;
hash += (hash << 10);
hash ^= (hash >> 6);
if (config->hashStringContents) {
uint32_t maxLengthToHash = CLAY__MIN(text->length, 256);
for (int i = 0; i < maxLengthToHash; i++) {
hash += text->chars[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
} else {
hash += pointerAsNumber;
hash += (hash << 10);
hash ^= (hash >> 6);
}

hash += text->length;
hash += (hash << 10);
Expand Down
Binary file modified examples/clay-official-website/build/clay/index.wasm
Binary file not shown.

0 comments on commit a1e692b

Please sign in to comment.