2#include "nuklear_internal.h"
9NK_INTERN
int nk_str_match_here(
const char *regexp,
const char *text);
10NK_INTERN
int nk_str_match_star(
int c,
const char *regexp,
const char *text);
11NK_LIB nk_bool nk_is_lower(
int c) {
return (c >=
'a' && c <=
'z') || (c >= 0xE0 && c <= 0xFF);}
12NK_LIB nk_bool nk_is_upper(
int c){
return (c >=
'A' && c <=
'Z') || (c >= 0xC0 && c <= 0xDF);}
13NK_LIB
int nk_to_upper(
int c) {
return (c >=
'a' && c <=
'z') ? (c - (
'a' -
'A')) : c;}
14NK_LIB
int nk_to_lower(
int c) {
return (c >=
'A' && c <=
'Z') ? (c - (
'a' +
'A')) : c;}
16#ifdef NK_MEMCPY_NEEDED
18nk_memcopy(
void *dst0,
const void *src0, nk_size length)
21 char *dst = (
char*)dst0;
22 const char *src = (
const char*)src0;
23 if (length == 0 || dst == src)
27 #define nk_wsize sizeof(nk_word)
28 #define nk_wmask (nk_wsize-1)
29 #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
30 #define NK_TLOOP1(s) do { s; } while (--t)
34 if ((t | (nk_ptr)dst) & nk_wmask) {
35 if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
38 t = nk_wsize - (t & nk_wmask);
40 NK_TLOOP1(*dst++ = *src++);
42 t = length / nk_wsize;
43 NK_TLOOP(*(nk_word*)(
void*)dst = *(
const nk_word*)(
const void*)src;
44 src += nk_wsize; dst += nk_wsize);
45 t = length & nk_wmask;
46 NK_TLOOP(*dst++ = *src++);
51 if ((t | (nk_ptr)dst) & nk_wmask) {
52 if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
57 NK_TLOOP1(*--dst = *--src);
59 t = length / nk_wsize;
60 NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
61 *(nk_word*)(
void*)dst = *(
const nk_word*)(
const void*)src);
62 t = length & nk_wmask;
63 NK_TLOOP(*--dst = *--src);
74#ifdef NK_MEMSET_NEEDED
76nk_memset(
void *ptr,
int c0, nk_size size)
78 #define nk_word unsigned
79 #define nk_wsize sizeof(nk_word)
80 #define nk_wmask (nk_wsize - 1)
81 nk_byte *dst = (nk_byte*)ptr;
85 if ((c = (nk_byte)c0) != 0) {
87 if (
sizeof(
unsigned int) > 2)
93 if (size < 3 * nk_wsize) {
94 while (size--) *dst++ = (nk_byte)c0;
99 if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
103 *dst++ = (nk_byte)c0;
110 *(nk_word*)((
void*)dst) = c;
115 t = (size & nk_wmask);
118 *dst++ = (nk_byte)c0;
128nk_zero(
void *ptr, nk_size size)
131 NK_MEMSET(ptr, 0, size);
134nk_strlen(
const char *str)
138 while (str && *str++ !=
'\0') siz++;
142nk_strtoi(
const char *str,
char **endptr)
152 while (*p ==
' ') p++;
157 while (*p && *p >=
'0' && *p <=
'9') {
158 value = value * 10 + (int) (*p -
'0');
165#ifdef NK_STRTOD_NEEDED
167nk_strtod(
const char *str,
char **endptr)
171 char *p = (
char *)str;
179 while (*p ==
' ') p++;
185 while (*p && *p !=
'.' && *p !=
'e') {
186 value = value * 10.0 + (double) (*p -
'0');
192 for(m = 0.1; *p && *p !=
'e'; p++ ) {
193 value = value + (double) (*p -
'0') * m;
203 }
else if (*p ==
'+') {
206 }
else div = nk_false;
208 for (pow = 0; *p; p++)
209 pow = pow * 10 + (
int) (*p -
'0');
211 for (m = 1.0, i = 0; i < pow; i++)
218 number = value * neg;
225nk_strtof(
const char *str,
char **endptr)
229 double_value = NK_STRTOD(str, endptr);
230 float_value = (float)double_value;
234nk_stricmp(
const char *s1,
const char *s2)
242 if (c1 <=
'Z' && c1 >=
'A') {
246 if (c2 <=
'Z' && c2 >=
'A') {
250 return ((d >= 0) << 1) - 1;
256nk_stricmpn(
const char *s1,
const char *s2,
int n)
267 if (c1 <=
'Z' && c1 >=
'A') {
271 if (c2 <=
'Z' && c2 >=
'A') {
275 return ((d >= 0) << 1) - 1;
281nk_str_match_here(
const char *regexp,
const char *text)
283 if (regexp[0] ==
'\0')
285 if (regexp[1] ==
'*')
286 return nk_str_match_star(regexp[0], regexp+2, text);
287 if (regexp[0] ==
'$' && regexp[1] ==
'\0')
288 return *text ==
'\0';
289 if (*text!=
'\0' && (regexp[0]==
'.' || regexp[0]==*text))
290 return nk_str_match_here(regexp+1, text+1);
294nk_str_match_star(
int c,
const char *regexp,
const char *text)
297 if (nk_str_match_here(regexp, text))
299 }
while (*text !=
'\0' && (*text++ == c || c ==
'.'));
303nk_strfilter(
const char *text,
const char *regexp)
311 if (regexp[0] ==
'^')
312 return nk_str_match_here(regexp+1, text);
314 if (nk_str_match_here(regexp, text))
316 }
while (*text++ !=
'\0');
320nk_strmatch_fuzzy_text(
const char *str,
int str_len,
321 const char *pattern,
int *out_score)
328 #define NK_ADJACENCY_BONUS 5
330 #define NK_SEPARATOR_BONUS 10
332 #define NK_CAMEL_BONUS 10
334 #define NK_LEADING_LETTER_PENALTY (-3)
336 #define NK_MAX_LEADING_LETTER_PENALTY (-9)
338 #define NK_UNMATCHED_LETTER_PENALTY (-1)
342 char const * pattern_iter = pattern;
344 int prev_matched = nk_false;
345 int prev_lower = nk_false;
347 int prev_separator = nk_true;
350 char const * best_letter = 0;
351 int best_letter_score = 0;
356 if (!str || !str_len || !pattern)
return 0;
357 while (str_iter < str_len)
359 const char pattern_letter = *pattern_iter;
360 const char str_letter = str[str_iter];
362 int next_match = *pattern_iter !=
'\0' &&
363 nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
364 int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);
366 int advanced = next_match && best_letter;
367 int pattern_repeat = best_letter && *pattern_iter !=
'\0';
368 pattern_repeat = pattern_repeat &&
369 nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
371 if (advanced || pattern_repeat) {
372 score += best_letter_score;
374 best_letter_score = 0;
377 if (next_match || rematch)
381 if (pattern_iter == pattern) {
382 int count = (int)(&str[str_iter] - str);
383 int penalty = NK_LEADING_LETTER_PENALTY * count;
384 if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
385 penalty = NK_MAX_LEADING_LETTER_PENALTY;
392 new_score += NK_ADJACENCY_BONUS;
396 new_score += NK_SEPARATOR_BONUS;
399 if (prev_lower && nk_is_upper(str_letter))
400 new_score += NK_CAMEL_BONUS;
407 if (new_score >= best_letter_score) {
409 if (best_letter != 0)
410 score += NK_UNMATCHED_LETTER_PENALTY;
412 best_letter = &str[str_iter];
413 best_letter_score = new_score;
415 prev_matched = nk_true;
417 score += NK_UNMATCHED_LETTER_PENALTY;
418 prev_matched = nk_false;
422 prev_lower = nk_is_lower(str_letter) != 0;
423 prev_separator = str_letter ==
'_' || str_letter ==
' ';
430 score += best_letter_score;
433 if (*pattern_iter !=
'\0')
441nk_strmatch_fuzzy_string(
char const *str,
char const *pattern,
int *out_score)
443 return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);
446nk_string_float_limit(
char *
string,
int prec)
456 if (dot == (prec+1)) {
463 return (
int)(c - string);
466nk_strrev_ascii(
char *s)
468 int len = nk_strlen(s);
472 for (; i < end; ++i) {
474 s[i] = s[len - 1 - i];
479nk_itoa(
char *s,
long n)
492 s[i++] = (char)(
'0' + (n % 10));
504nk_dtoa(
char *s,
double n)
507 int digit = 0, m = 0, m1 = 0;
515 s[0] =
'0'; s[1] =
'\0';
524 useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
525 if (neg) *(c++) =
'-';
531 n = n / (double)nk_pow(10.0, m);
540 while (n > NK_FLOAT_PRECISION || m >= 0) {
541 double weight = nk_pow(10.0, m);
543 double t = (double)n / weight;
544 digit = nk_ifloord(t);
545 n -= ((double)digit * weight);
546 *(c++) = (
char)(
'0' + (char)digit);
565 *(c++) = (
char)(
'0' + (char)(m1 % 10));
570 for (i = 0, j = m-1; i<j; i++, j--) {
582#ifdef NK_INCLUDE_STANDARD_VARARGS
583#ifndef NK_INCLUDE_STANDARD_IO
585nk_vsnprintf(
char *buf,
int buf_size,
const char *fmt, va_list args)
594 NK_ARG_FLAG_LEFT = 0x01,
595 NK_ARG_FLAG_PLUS = 0x02,
596 NK_ARG_FLAG_SPACE = 0x04,
597 NK_ARG_FLAG_NUM = 0x10,
598 NK_ARG_FLAG_ZERO = 0x20
601 char number_buffer[NK_MAX_NUMBER_BUFFER];
602 enum nk_arg_type arg_type = NK_ARG_TYPE_DEFAULT;
603 int precision = NK_DEFAULT;
604 int width = NK_DEFAULT;
609 const char *iter = fmt;
613 if (!buf || !buf_size || !fmt)
return 0;
614 for (iter = fmt; *iter && len < buf_size; iter++) {
616 while (*iter && (*iter !=
'%') && (len < buf_size))
617 buf[len++] = *iter++;
618 if (!(*iter) || len >= buf_size)
break;
623 if (*iter ==
'-') flag |= NK_ARG_FLAG_LEFT;
624 else if (*iter ==
'+') flag |= NK_ARG_FLAG_PLUS;
625 else if (*iter ==
' ') flag |= NK_ARG_FLAG_SPACE;
626 else if (*iter ==
'#') flag |= NK_ARG_FLAG_NUM;
627 else if (*iter ==
'0') flag |= NK_ARG_FLAG_ZERO;
634 if (*iter >=
'1' && *iter <=
'9') {
636 width = nk_strtoi(iter, &end);
640 }
else if (*iter ==
'*') {
641 width = va_arg(args,
int);
646 precision = NK_DEFAULT;
650 precision = va_arg(args,
int);
654 precision = nk_strtoi(iter, &end);
663 if (*(iter+1) ==
'h') {
664 arg_type = NK_ARG_TYPE_CHAR;
666 }
else arg_type = NK_ARG_TYPE_SHORT;
668 }
else if (*iter ==
'l') {
669 arg_type = NK_ARG_TYPE_LONG;
671 }
else arg_type = NK_ARG_TYPE_DEFAULT;
675 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
676 NK_ASSERT(precision == NK_DEFAULT);
677 NK_ASSERT(width == NK_DEFAULT);
680 }
else if (*iter ==
's') {
682 const char *str = va_arg(args,
const char*);
683 NK_ASSERT(str != buf &&
"buffer and argument are not allowed to overlap!");
684 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
685 NK_ASSERT(precision == NK_DEFAULT);
686 NK_ASSERT(width == NK_DEFAULT);
687 if (str == buf)
return -1;
688 while (str && *str && len < buf_size)
690 }
else if (*iter ==
'n') {
692 signed int *n = va_arg(args,
int*);
693 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
694 NK_ASSERT(precision == NK_DEFAULT);
695 NK_ASSERT(width == NK_DEFAULT);
697 }
else if (*iter ==
'c' || *iter ==
'i' || *iter ==
'd') {
700 const char *num_iter;
701 int num_len, num_print, padding;
702 int cur_precision = NK_MAX(precision, 1);
703 int cur_width = NK_MAX(width, 0);
706 if (arg_type == NK_ARG_TYPE_CHAR)
707 value = (
signed char)va_arg(args,
int);
708 else if (arg_type == NK_ARG_TYPE_SHORT)
709 value = (
signed short)va_arg(args,
int);
710 else if (arg_type == NK_ARG_TYPE_LONG)
711 value = va_arg(args,
signed long);
712 else if (*iter ==
'c')
713 value = (
unsigned char)va_arg(args,
int);
714 else value = va_arg(args,
signed int);
717 nk_itoa(number_buffer, value);
718 num_len = nk_strlen(number_buffer);
719 padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
720 if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
721 padding = NK_MAX(padding-1, 0);
724 if (!(flag & NK_ARG_FLAG_LEFT)) {
725 while (padding-- > 0 && (len < buf_size)) {
726 if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
728 else buf[len++] =
' ';
733 if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
735 else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
739 num_print = NK_MAX(cur_precision, num_len);
740 while (precision && (num_print > num_len) && (len < buf_size)) {
746 num_iter = number_buffer;
747 while (precision && *num_iter && len < buf_size)
748 buf[len++] = *num_iter++;
751 if (flag & NK_ARG_FLAG_LEFT) {
752 while ((padding-- > 0) && (len < buf_size))
755 }
else if (*iter ==
'o' || *iter ==
'x' || *iter ==
'X' || *iter ==
'u') {
757 unsigned long value = 0;
758 int num_len = 0, num_print, padding = 0;
759 int cur_precision = NK_MAX(precision, 1);
760 int cur_width = NK_MAX(width, 0);
761 unsigned int base = (*iter ==
'o') ? 8: (*iter ==
'u')? 10: 16;
764 const char *upper_output_format =
"0123456789ABCDEF";
765 const char *lower_output_format =
"0123456789abcdef";
766 const char *output_format = (*iter ==
'x') ?
767 lower_output_format: upper_output_format;
770 if (arg_type == NK_ARG_TYPE_CHAR)
771 value = (
unsigned char)va_arg(args,
int);
772 else if (arg_type == NK_ARG_TYPE_SHORT)
773 value = (
unsigned short)va_arg(args,
int);
774 else if (arg_type == NK_ARG_TYPE_LONG)
775 value = va_arg(args,
unsigned long);
776 else value = va_arg(args,
unsigned int);
780 int digit = output_format[value % base];
781 if (num_len < NK_MAX_NUMBER_BUFFER)
782 number_buffer[num_len++] = (char)digit;
786 num_print = NK_MAX(cur_precision, num_len);
787 padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
788 if (flag & NK_ARG_FLAG_NUM)
789 padding = NK_MAX(padding-1, 0);
792 if (!(flag & NK_ARG_FLAG_LEFT)) {
793 while ((padding-- > 0) && (len < buf_size)) {
794 if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
796 else buf[len++] =
' ';
801 if (num_print && (flag & NK_ARG_FLAG_NUM)) {
802 if ((*iter ==
'o') && (len < buf_size)) {
804 }
else if ((*iter ==
'x') && ((len+1) < buf_size)) {
807 }
else if ((*iter ==
'X') && ((len+1) < buf_size)) {
812 while (precision && (num_print > num_len) && (len < buf_size)) {
818 while (num_len > 0) {
819 if (precision && (len < buf_size))
820 buf[len++] = number_buffer[num_len-1];
825 if (flag & NK_ARG_FLAG_LEFT) {
826 while ((padding-- > 0) && (len < buf_size))
829 }
else if (*iter ==
'f') {
831 const char *num_iter;
832 int cur_precision = (precision < 0) ? 6: precision;
833 int prefix, cur_width = NK_MAX(width, 0);
834 double value = va_arg(args,
double);
835 int num_len = 0, frac_len = 0, dot = 0;
838 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
839 NK_DTOA(number_buffer, value);
840 num_len = nk_strlen(number_buffer);
843 num_iter = number_buffer;
844 while (*num_iter && *num_iter !=
'.')
847 prefix = (*num_iter ==
'.')?(
int)(num_iter - number_buffer)+1:0;
848 padding = NK_MAX(cur_width - (prefix + NK_MIN(cur_precision, num_len - prefix)) , 0);
849 if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
850 padding = NK_MAX(padding-1, 0);
853 if (!(flag & NK_ARG_FLAG_LEFT)) {
854 while (padding-- > 0 && (len < buf_size)) {
855 if (flag & NK_ARG_FLAG_ZERO)
857 else buf[len++] =
' ';
862 num_iter = number_buffer;
863 if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
865 else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
870 buf[len++] = *num_iter;
871 if (*num_iter ==
'.') dot = 1;
872 if (frac_len >= cur_precision)
break;
877 while (frac_len < cur_precision) {
878 if (!dot && len < buf_size) {
888 if (flag & NK_ARG_FLAG_LEFT) {
889 while ((padding-- > 0) && (len < buf_size))
894 NK_ASSERT(0 &&
"specifier is not supported!");
898 buf[(len >= buf_size)?(buf_size-1):len] = 0;
899 result = (len >= buf_size)?-1:len;
904nk_strfmt(
char *buf,
int buf_size,
const char *fmt, va_list args)
909 if (!buf || !buf_size || !fmt)
return 0;
910#ifdef NK_INCLUDE_STANDARD_IO
911 result = NK_VSNPRINTF(buf, (nk_size)buf_size, fmt, args);
912 result = (result >= buf_size) ? -1: result;
915 result = nk_vsnprintf(buf, buf_size, fmt, args);
921nk_murmur_hash(
const void * key,
int len, nk_hash seed)
924 #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
928 const nk_byte *data = (
const nk_byte*)key;
929 const nk_byte *keyptr = data;
931 const int bsize =
sizeof(k1);
932 const int nblocks = len/4;
934 const nk_uint c1 = 0xcc9e2d51;
935 const nk_uint c2 = 0x1b873593;
941 for (i = 0; i < nblocks; ++i, keyptr += bsize) {
942 k1ptr = (nk_byte*)&k1;
943 k1ptr[0] = keyptr[0];
944 k1ptr[1] = keyptr[1];
945 k1ptr[2] = keyptr[2];
946 k1ptr[3] = keyptr[3];
954 h1 = h1*5+0xe6546b64;
958 tail = (
const nk_byte*)(data + nblocks*4);
961 case 3: k1 ^= (nk_uint)(tail[2] << 16);
962 case 2: k1 ^= (nk_uint)(tail[1] << 8u);
963 case 1: k1 ^= tail[0];
984#ifdef NK_INCLUDE_STANDARD_IO
986nk_file_load(
const char* path, nk_size* siz,
const struct nk_allocator *alloc)
995 if (!path || !siz || !alloc)
998 fd = fopen(path,
"rb");
1000 fseek(fd, 0, SEEK_END);
1006 *siz = (nk_size)ret;
1007 fseek(fd, 0, SEEK_SET);
1008 buf = (
char*)alloc->alloc(alloc->userdata,0, *siz);
1014 *siz = (nk_size)fread(buf, 1,*siz, fd);
1020nk_text_clamp(
const struct nk_user_font *font,
const char *text,
1021 int text_len,
float space,
int *glyphs,
float *text_width,
1022 nk_rune *sep_list,
int sep_count)
1026 float last_width = 0;
1027 nk_rune unicode = 0;
1035 float sep_width = 0;
1036 sep_count = NK_MAX(sep_count,0);
1038 glyph_len = nk_utf_decode(text, &unicode, text_len);
1039 while (glyph_len && (width < space) && (len < text_len)) {
1041 s = font->
width(font->userdata, font->
height, text, len);
1042 for (i = 0; i < sep_count; ++i) {
1043 if (unicode != sep_list[i])
continue;
1044 sep_width = last_width = width;
1049 if (i == sep_count){
1050 last_width = sep_width = width;
1054 glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
1057 if (len >= text_len) {
1059 *text_width = last_width;
1063 *text_width = sep_width;
1064 return (!sep_len) ? len: sep_len;
1068nk_text_calculate_text_bounds(const struct
nk_user_font *font,
1069 const char *begin,
int byte_len,
float row_height,
const char **remaining,
1070 struct nk_vec2 *out_offset,
int *glyphs,
int op)
1072 float line_height = row_height;
1074 float line_width = 0.0f;
1078 nk_rune unicode = 0;
1080 if (!begin || byte_len <= 0 || !font)
1083 glyph_len = nk_utf_decode(begin, &unicode, byte_len);
1084 if (!glyph_len)
return text_size;
1085 glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
1088 while ((text_len < byte_len) && glyph_len) {
1089 if (unicode ==
'\n') {
1090 text_size.x = NK_MAX(text_size.x, line_width);
1091 text_size.y += line_height;
1094 if (op == NK_STOP_ON_NEW_LINE)
1098 glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
1102 if (unicode ==
'\r') {
1105 glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
1109 *glyphs = *glyphs + 1;
1110 text_len += glyph_len;
1111 line_width += (float)glyph_width;
1112 glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
1113 glyph_width = font->width(font->userdata, font->height, begin+text_len, glyph_len);
1117 if (text_size.x < line_width)
1118 text_size.x = line_width;
1120 *out_offset =
nk_vec2(line_width, text_size.y + line_height);
1121 if (line_width > 0 || text_size.y == 0.0f)
1122 text_size.y += line_height;
1124 *remaining = begin+text_len;
main API and documentation file
nk_text_width_f width
!< max height of the font
float height
!< user provided font handle