Skip to content

Commit 242f35e

Browse files
authored
Merge pull request #836 from sleeptightAnsiC/fix__nk_widget_text__default_align
nk_widget_text: use left alignment by default instead of silent failing + small refactor
2 parents 9851a51 + 6af4e06 commit 242f35e

4 files changed

Lines changed: 31 additions & 11 deletions

File tree

clib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuklear",
3-
"version": "4.12.7",
3+
"version": "4.12.8",
44
"repo": "Immediate-Mode-UI/Nuklear",
55
"description": "A small ANSI C gui toolkit",
66
"keywords": ["gl", "ui", "toolkit"],

nuklear.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23808,13 +23808,16 @@ nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
2380823808
if (!o || !t) return;
2380923809

2381023810
b.h = NK_MAX(b.h, 2 * t->padding.y);
23811-
label.x = 0; label.w = 0;
23812-
label.y = b.y + t->padding.y;
23813-
label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
2381423811

2381523812
text_width = f->width(f->userdata, f->height, (const char*)string, len);
2381623813
text_width += (2.0f * t->padding.x);
2381723814

23815+
/* use top-left alignment by default */
23816+
if (!(a & (NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_CENTERED | NK_TEXT_ALIGN_RIGHT)))
23817+
a |= NK_TEXT_ALIGN_LEFT;
23818+
if (!(a & (NK_TEXT_ALIGN_TOP | NK_TEXT_ALIGN_MIDDLE | NK_TEXT_ALIGN_BOTTOM)))
23819+
a |= NK_TEXT_ALIGN_TOP;
23820+
2381823821
/* align in x-axis */
2381923822
if (a & NK_TEXT_ALIGN_LEFT) {
2382023823
label.x = b.x + t->padding.x;
@@ -23828,16 +23831,20 @@ nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
2382823831
} else if (a & NK_TEXT_ALIGN_RIGHT) {
2382923832
label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
2383023833
label.w = (float)text_width + 2 * t->padding.x;
23831-
} else return;
23834+
}
2383223835

2383323836
/* align in y-axis */
23834-
if (a & NK_TEXT_ALIGN_MIDDLE) {
23837+
if (a & NK_TEXT_ALIGN_TOP) {
23838+
label.y = b.y + t->padding.y;
23839+
label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
23840+
} else if (a & NK_TEXT_ALIGN_MIDDLE) {
2383523841
label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
2383623842
label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
2383723843
} else if (a & NK_TEXT_ALIGN_BOTTOM) {
2383823844
label.y = b.y + b.h - f->height;
2383923845
label.h = f->height;
2384023846
}
23847+
2384123848
nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
2384223849
}
2384323850
NK_LIB void
@@ -30719,6 +30726,9 @@ nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
3071930726
/// - [y]: Minor version with non-breaking API and library changes
3072030727
/// - [z]: Patch version with no direct changes to the API
3072130728
///
30729+
/// - 2025/10/08 (4.12.8) - Fix nk_widget_text to use NK_TEXT_ALIGN_LEFT by default,
30730+
/// instead of silently failing when no x-axis alignment is provided,
30731+
/// and refactor this function to keep the code style consistent
3072230732
/// - 2025/04/06 (4.12.7) - Fix text input navigation and mouse scrolling
3072330733
/// - 2025/03/29 (4.12.6) - Fix unitialized data in nk_input_char
3072430734
/// - 2025/03/05 (4.12.5) - Fix scrolling knob also scrolling parent window, remove dead code

src/CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
/// - [y]: Minor version with non-breaking API and library changes
88
/// - [z]: Patch version with no direct changes to the API
99
///
10+
/// - 2025/10/08 (4.12.8) - Fix nk_widget_text to use NK_TEXT_ALIGN_LEFT by default,
11+
/// instead of silently failing when no x-axis alignment is provided,
12+
/// and refactor this function to keep the code style consistent
1013
/// - 2025/04/06 (4.12.7) - Fix text input navigation and mouse scrolling
1114
/// - 2025/03/29 (4.12.6) - Fix unitialized data in nk_input_char
1215
/// - 2025/03/05 (4.12.5) - Fix scrolling knob also scrolling parent window, remove dead code

src/nuklear_text.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
1919
if (!o || !t) return;
2020

2121
b.h = NK_MAX(b.h, 2 * t->padding.y);
22-
label.x = 0; label.w = 0;
23-
label.y = b.y + t->padding.y;
24-
label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
2522

2623
text_width = f->width(f->userdata, f->height, (const char*)string, len);
2724
text_width += (2.0f * t->padding.x);
2825

26+
/* use top-left alignment by default */
27+
if (!(a & (NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_CENTERED | NK_TEXT_ALIGN_RIGHT)))
28+
a |= NK_TEXT_ALIGN_LEFT;
29+
if (!(a & (NK_TEXT_ALIGN_TOP | NK_TEXT_ALIGN_MIDDLE | NK_TEXT_ALIGN_BOTTOM)))
30+
a |= NK_TEXT_ALIGN_TOP;
31+
2932
/* align in x-axis */
3033
if (a & NK_TEXT_ALIGN_LEFT) {
3134
label.x = b.x + t->padding.x;
@@ -39,16 +42,20 @@ nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
3942
} else if (a & NK_TEXT_ALIGN_RIGHT) {
4043
label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
4144
label.w = (float)text_width + 2 * t->padding.x;
42-
} else return;
45+
}
4346

4447
/* align in y-axis */
45-
if (a & NK_TEXT_ALIGN_MIDDLE) {
48+
if (a & NK_TEXT_ALIGN_TOP) {
49+
label.y = b.y + t->padding.y;
50+
label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
51+
} else if (a & NK_TEXT_ALIGN_MIDDLE) {
4652
label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
4753
label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
4854
} else if (a & NK_TEXT_ALIGN_BOTTOM) {
4955
label.y = b.y + b.h - f->height;
5056
label.h = f->height;
5157
}
58+
5259
nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
5360
}
5461
NK_LIB void

0 commit comments

Comments
 (0)