Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/nuklear_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,7 @@ nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
/* make sure text fits inside bounds */
text_width = font->width(font->userdata, font->height, string, length);
if (text_width > r.w){
int glyphs = 0;
float txt_width = (float)text_width;
length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width, 0,0);
length = nk_text_clamp(font, string, length, r.w, NULL, NULL, 0,0);
}

if (!length) return;
Expand Down
6 changes: 2 additions & 4 deletions src/nuklear_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
const char *string, int len, const struct nk_text *t,
const struct nk_user_font *f)
{
float width;
int glyphs = 0;
int fitting = 0;
int done = 0;
struct nk_rect line;
Expand All @@ -81,13 +79,13 @@ nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
line.w = b.w - 2 * t->padding.x;
line.h = 2 * t->padding.y + f->height;

fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
fitting = nk_text_clamp(f, string, len, line.w, NULL, NULL, seperator,NK_LEN(seperator));
while (done < len) {
if (!fitting || line.y + line.h >= (b.y + b.h)) break;
nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
done += fitting;
line.y += f->height + 2 * t->padding.y;
fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
fitting = nk_text_clamp(f, &string[done], len - done, line.w, NULL, NULL, seperator,NK_LEN(seperator));
}
}
NK_API void
Expand Down
68 changes: 36 additions & 32 deletions src/nuklear_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1018,52 +1018,56 @@ nk_file_load(const char* path, nk_size* siz, const struct nk_allocator *alloc)
}
#endif
NK_LIB int
nk_text_clamp(const struct nk_user_font *font, const char *text,
int text_len, float space, int *glyphs, float *text_width,
nk_rune *sep_list, int sep_count)
nk_text_clamp(const struct nk_user_font* font, const char* text,
int text_len, float space, int* glyphs, float* text_width,
nk_rune* sep_list, int sep_count)

{
int i = 0;
int glyph_len = 0;
float last_width = 0;

nk_rune unicode = 0;
float width = 0;
int len = 0;
int g = 0;
float s;

int sep_len = 0;
int sep_g = 0;
float sep_width = 0;
sep_count = NK_MAX(sep_count,0);
float ret_width = 0;
int ret_g = 0;
int ret_len = 0;

sep_count = NK_MAX(sep_count, 0);

while ((width < space) && (len < text_len)) {
if (!ret_g) {
ret_width = width;
ret_len = len;
}

glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
if (!glyph_len) break;

glyph_len = nk_utf_decode(text, &unicode, text_len);
while (glyph_len && (width < space) && (len < text_len)) {
len += glyph_len;
s = font->width(font->userdata, font->height, text, len);
width = font->width(font->userdata, font->height, text, len);

for (i = 0; i < sep_count; ++i) {
if (unicode != sep_list[i]) continue;
sep_width = last_width = width;
sep_g = g+1;
sep_len = len;
break;
}
if (i == sep_count){
last_width = sep_width = width;
sep_g = g+1;
if (unicode == sep_list[i]) {
ret_len = len;
ret_g = g + 1;
break;
}
}
width = s;
glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
g++;
}
if (len >= text_len) {
*glyphs = g;
*text_width = last_width;
return len;
} else {
*glyphs = sep_g;
*text_width = sep_width;
return (!sep_len) ? len: sep_len;
}

if (width < space) {
ret_g = g;
ret_width = width;
ret_len = len;
} else if (!ret_g) ret_g = NK_MAX(g - 1, 0);

if (glyphs) *glyphs = ret_g;
if (text_width) *text_width = ret_width;
return ret_len;
}
NK_LIB struct nk_vec2
nk_text_calculate_text_bounds(const struct nk_user_font *font,
Expand Down