Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_util.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * UTIL
7 *
8 * ===============================================================*/
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;}
15
16#ifdef NK_MEMCPY_NEEDED
17NK_LIB void*
18nk_memcopy(void *dst0, const void *src0, nk_size length)
19{
20 nk_ptr t;
21 char *dst = (char*)dst0;
22 const char *src = (const char*)src0;
23 if (length == 0 || dst == src)
24 goto done;
25
26 #define nk_word int
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)
31
32 if (dst < src) {
33 t = (nk_ptr)src; /* only need low bits */
34 if ((t | (nk_ptr)dst) & nk_wmask) {
35 if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
36 t = length;
37 else
38 t = nk_wsize - (t & nk_wmask);
39 length -= t;
40 NK_TLOOP1(*dst++ = *src++);
41 }
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++);
47 } else {
48 src += length;
49 dst += length;
50 t = (nk_ptr)src;
51 if ((t | (nk_ptr)dst) & nk_wmask) {
52 if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
53 t = length;
54 else
55 t &= nk_wmask;
56 length -= t;
57 NK_TLOOP1(*--dst = *--src);
58 }
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);
64 }
65 #undef nk_word
66 #undef nk_wsize
67 #undef nk_wmask
68 #undef NK_TLOOP
69 #undef NK_TLOOP1
70done:
71 return (dst0);
72}
73#endif
74#ifdef NK_MEMSET_NEEDED
75NK_LIB void
76nk_memset(void *ptr, int c0, nk_size size)
77{
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;
82 unsigned c = 0;
83 nk_size t = 0;
84
85 if ((c = (nk_byte)c0) != 0) {
86 c = (c << 8) | c; /* at least 16-bits */
87 if (sizeof(unsigned int) > 2)
88 c = (c << 16) | c; /* at least 32-bits*/
89 }
90
91 /* too small of a word count */
92 dst = (nk_byte*)ptr;
93 if (size < 3 * nk_wsize) {
94 while (size--) *dst++ = (nk_byte)c0;
95 return;
96 }
97
98 /* align destination */
99 if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
100 t = nk_wsize -t;
101 size -= t;
102 do {
103 *dst++ = (nk_byte)c0;
104 } while (--t != 0);
105 }
106
107 /* fill word */
108 t = size / nk_wsize;
109 do {
110 *(nk_word*)((void*)dst) = c;
111 dst += nk_wsize;
112 } while (--t != 0);
113
114 /* fill trailing bytes */
115 t = (size & nk_wmask);
116 if (t != 0) {
117 do {
118 *dst++ = (nk_byte)c0;
119 } while (--t != 0);
120 }
121
122 #undef nk_word
123 #undef nk_wsize
124 #undef nk_wmask
125}
126#endif
127NK_LIB void
128nk_zero(void *ptr, nk_size size)
129{
130 NK_ASSERT(ptr);
131 NK_MEMSET(ptr, 0, size);
132}
133NK_API int
134nk_strlen(const char *str)
135{
136 int siz = 0;
137 NK_ASSERT(str);
138 while (str && *str++ != '\0') siz++;
139 return siz;
140}
141NK_API int
142nk_strtoi(const char *str, char **endptr)
143{
144 int neg = 1;
145 const char *p = str;
146 int value = 0;
147
148 NK_ASSERT(str);
149 if (!str) return 0;
150
151 /* skip whitespace */
152 while (*p == ' ') p++;
153 if (*p == '-') {
154 neg = -1;
155 p++;
156 }
157 while (*p && *p >= '0' && *p <= '9') {
158 value = value * 10 + (int) (*p - '0');
159 p++;
160 }
161 if (endptr)
162 *endptr = (char *)p;
163 return neg*value;
164}
165#ifdef NK_STRTOD_NEEDED
166NK_API double
167nk_strtod(const char *str, char **endptr)
168{
169 double m;
170 double neg = 1.0;
171 char *p = (char *)str;
172 double value = 0;
173 double number = 0;
174
175 NK_ASSERT(str);
176 if (!str) return 0;
177
178 /* skip whitespace */
179 while (*p == ' ') p++;
180 if (*p == '-') {
181 neg = -1.0;
182 p++;
183 }
184
185 while (*p && *p != '.' && *p != 'e') {
186 value = value * 10.0 + (double) (*p - '0');
187 p++;
188 }
189
190 if (*p == '.') {
191 p++;
192 for(m = 0.1; *p && *p != 'e'; p++ ) {
193 value = value + (double) (*p - '0') * m;
194 m *= 0.1;
195 }
196 }
197 if (*p == 'e') {
198 int i, pow, div;
199 p++;
200 if (*p == '-') {
201 div = nk_true;
202 p++;
203 } else if (*p == '+') {
204 div = nk_false;
205 p++;
206 } else div = nk_false;
207
208 for (pow = 0; *p; p++)
209 pow = pow * 10 + (int) (*p - '0');
210
211 for (m = 1.0, i = 0; i < pow; i++)
212 m *= 10.0;
213
214 if (div)
215 value /= m;
216 else value *= m;
217 }
218 number = value * neg;
219 if (endptr)
220 *endptr = p;
221 return number;
222}
223#endif
224NK_API float
225nk_strtof(const char *str, char **endptr)
226{
227 float float_value;
228 double double_value;
229 double_value = NK_STRTOD(str, endptr);
230 float_value = (float)double_value;
231 return float_value;
232}
233NK_API int
234nk_stricmp(const char *s1, const char *s2)
235{
236 nk_int c1,c2,d;
237 do {
238 c1 = *s1++;
239 c2 = *s2++;
240 d = c1 - c2;
241 while (d) {
242 if (c1 <= 'Z' && c1 >= 'A') {
243 d += ('a' - 'A');
244 if (!d) break;
245 }
246 if (c2 <= 'Z' && c2 >= 'A') {
247 d -= ('a' - 'A');
248 if (!d) break;
249 }
250 return ((d >= 0) << 1) - 1;
251 }
252 } while (c1);
253 return 0;
254}
255NK_API int
256nk_stricmpn(const char *s1, const char *s2, int n)
257{
258 int c1,c2,d;
259 NK_ASSERT(n >= 0);
260 do {
261 c1 = *s1++;
262 c2 = *s2++;
263 if (!n--) return 0;
264
265 d = c1 - c2;
266 while (d) {
267 if (c1 <= 'Z' && c1 >= 'A') {
268 d += ('a' - 'A');
269 if (!d) break;
270 }
271 if (c2 <= 'Z' && c2 >= 'A') {
272 d -= ('a' - 'A');
273 if (!d) break;
274 }
275 return ((d >= 0) << 1) - 1;
276 }
277 } while (c1);
278 return 0;
279}
280NK_INTERN int
281nk_str_match_here(const char *regexp, const char *text)
282{
283 if (regexp[0] == '\0')
284 return 1;
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);
291 return 0;
292}
293NK_INTERN int
294nk_str_match_star(int c, const char *regexp, const char *text)
295{
296 do {/* a '* matches zero or more instances */
297 if (nk_str_match_here(regexp, text))
298 return 1;
299 } while (*text != '\0' && (*text++ == c || c == '.'));
300 return 0;
301}
302NK_API int
303nk_strfilter(const char *text, const char *regexp)
304{
305 /*
306 c matches any literal character c
307 . matches any single character
308 ^ matches the beginning of the input string
309 $ matches the end of the input string
310 * matches zero or more occurrences of the previous character*/
311 if (regexp[0] == '^')
312 return nk_str_match_here(regexp+1, text);
313 do { /* must look even if string is empty */
314 if (nk_str_match_here(regexp, text))
315 return 1;
316 } while (*text++ != '\0');
317 return 0;
318}
319NK_API int
320nk_strmatch_fuzzy_text(const char *str, int str_len,
321 const char *pattern, int *out_score)
322{
323 /* Returns true if each character in pattern is found sequentially within str
324 * if found then out_score is also set. Score value has no intrinsic meaning.
325 * Range varies with pattern. Can only compare scores with same search pattern. */
326
327 /* bonus for adjacent matches */
328 #define NK_ADJACENCY_BONUS 5
329 /* bonus if match occurs after a separator */
330 #define NK_SEPARATOR_BONUS 10
331 /* bonus if match is uppercase and prev is lower */
332 #define NK_CAMEL_BONUS 10
333 /* penalty applied for every letter in str before the first match */
334 #define NK_LEADING_LETTER_PENALTY (-3)
335 /* maximum penalty for leading letters */
336 #define NK_MAX_LEADING_LETTER_PENALTY (-9)
337 /* penalty for every letter that doesn't matter */
338 #define NK_UNMATCHED_LETTER_PENALTY (-1)
339
340 /* loop variables */
341 int score = 0;
342 char const * pattern_iter = pattern;
343 int str_iter = 0;
344 int prev_matched = nk_false;
345 int prev_lower = nk_false;
346 /* true so if first letter match gets separator bonus*/
347 int prev_separator = nk_true;
348
349 /* use "best" matched letter if multiple string letters match the pattern */
350 char const * best_letter = 0;
351 int best_letter_score = 0;
352
353 /* loop over strings */
354 NK_ASSERT(str);
355 NK_ASSERT(pattern);
356 if (!str || !str_len || !pattern) return 0;
357 while (str_iter < str_len)
358 {
359 const char pattern_letter = *pattern_iter;
360 const char str_letter = str[str_iter];
361
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);
365
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);
370
371 if (advanced || pattern_repeat) {
372 score += best_letter_score;
373 best_letter = 0;
374 best_letter_score = 0;
375 }
376
377 if (next_match || rematch)
378 {
379 int new_score = 0;
380 /* Apply penalty for each letter before the first pattern match */
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;
386
387 score += penalty;
388 }
389
390 /* apply bonus for consecutive bonuses */
391 if (prev_matched)
392 new_score += NK_ADJACENCY_BONUS;
393
394 /* apply bonus for matches after a separator */
395 if (prev_separator)
396 new_score += NK_SEPARATOR_BONUS;
397
398 /* apply bonus across camel case boundaries */
399 if (prev_lower && nk_is_upper(str_letter))
400 new_score += NK_CAMEL_BONUS;
401
402 /* update pattern iter IFF the next pattern letter was matched */
403 if (next_match)
404 ++pattern_iter;
405
406 /* update best letter in str which may be for a "next" letter or a rematch */
407 if (new_score >= best_letter_score) {
408 /* apply penalty for now skipped letter */
409 if (best_letter != 0)
410 score += NK_UNMATCHED_LETTER_PENALTY;
411
412 best_letter = &str[str_iter];
413 best_letter_score = new_score;
414 }
415 prev_matched = nk_true;
416 } else {
417 score += NK_UNMATCHED_LETTER_PENALTY;
418 prev_matched = nk_false;
419 }
420
421 /* separators should be more easily defined */
422 prev_lower = nk_is_lower(str_letter) != 0;
423 prev_separator = str_letter == '_' || str_letter == ' ';
424
425 ++str_iter;
426 }
427
428 /* apply score for last match */
429 if (best_letter)
430 score += best_letter_score;
431
432 /* did not match full pattern */
433 if (*pattern_iter != '\0')
434 return nk_false;
435
436 if (out_score)
437 *out_score = score;
438 return nk_true;
439}
440NK_API int
441nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score)
442{
443 return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);
444}
445NK_LIB int
446nk_string_float_limit(char *string, int prec)
447{
448 int dot = 0;
449 char *c = string;
450 while (*c) {
451 if (*c == '.') {
452 dot = 1;
453 c++;
454 continue;
455 }
456 if (dot == (prec+1)) {
457 *c = 0;
458 break;
459 }
460 if (dot > 0) dot++;
461 c++;
462 }
463 return (int)(c - string);
464}
465NK_INTERN void
466nk_strrev_ascii(char *s)
467{
468 int len = nk_strlen(s);
469 int end = len / 2;
470 int i = 0;
471 char t;
472 for (; i < end; ++i) {
473 t = s[i];
474 s[i] = s[len - 1 - i];
475 s[len -1 - i] = t;
476 }
477}
478NK_LIB char*
479nk_itoa(char *s, long n)
480{
481 long i = 0;
482 if (n == 0) {
483 s[i++] = '0';
484 s[i] = 0;
485 return s;
486 }
487 if (n < 0) {
488 s[i++] = '-';
489 n = -n;
490 }
491 while (n > 0) {
492 s[i++] = (char)('0' + (n % 10));
493 n /= 10;
494 }
495 s[i] = 0;
496 if (s[0] == '-')
497 ++s;
498
499 nk_strrev_ascii(s);
500 return s;
501}
502#ifdef NK_DTOA_NEEDED
503NK_LIB char*
504nk_dtoa(char *s, double n)
505{
506 int useExp = 0;
507 int digit = 0, m = 0, m1 = 0;
508 char *c = s;
509 int neg = 0;
510
511 NK_ASSERT(s);
512 if (!s) return 0;
513
514 if (n == 0.0) {
515 s[0] = '0'; s[1] = '\0';
516 return s;
517 }
518
519 neg = (n < 0);
520 if (neg) n = -n;
521
522 /* calculate magnitude */
523 m = nk_log10(n);
524 useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
525 if (neg) *(c++) = '-';
526
527 /* set up for scientific notation */
528 if (useExp) {
529 if (m < 0)
530 m -= 1;
531 n = n / (double)nk_pow(10.0, m);
532 m1 = m;
533 m = 0;
534 }
535 if (m < 1.0) {
536 m = 0;
537 }
538
539 /* convert the number */
540 while (n > NK_FLOAT_PRECISION || m >= 0) {
541 double weight = nk_pow(10.0, m);
542 if (weight > 0) {
543 double t = (double)n / weight;
544 digit = nk_ifloord(t);
545 n -= ((double)digit * weight);
546 *(c++) = (char)('0' + (char)digit);
547 }
548 if (m == 0 && n > 0)
549 *(c++) = '.';
550 m--;
551 }
552
553 if (useExp) {
554 /* convert the exponent */
555 int i, j;
556 *(c++) = 'e';
557 if (m1 > 0) {
558 *(c++) = '+';
559 } else {
560 *(c++) = '-';
561 m1 = -m1;
562 }
563 m = 0;
564 while (m1 > 0) {
565 *(c++) = (char)('0' + (char)(m1 % 10));
566 m1 /= 10;
567 m++;
568 }
569 c -= m;
570 for (i = 0, j = m-1; i<j; i++, j--) {
571 /* swap without temporary */
572 c[i] ^= c[j];
573 c[j] ^= c[i];
574 c[i] ^= c[j];
575 }
576 c += m;
577 }
578 *(c) = '\0';
579 return s;
580}
581#endif
582#ifdef NK_INCLUDE_STANDARD_VARARGS
583#ifndef NK_INCLUDE_STANDARD_IO
584NK_INTERN int
585nk_vsnprintf(char *buf, int buf_size, const char *fmt, va_list args)
586{
587 enum nk_arg_type {
588 NK_ARG_TYPE_CHAR,
589 NK_ARG_TYPE_SHORT,
590 NK_ARG_TYPE_DEFAULT,
591 NK_ARG_TYPE_LONG
592 };
593 enum nk_arg_flags {
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
599 };
600
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;
605 nk_flags flag = 0;
606
607 int len = 0;
608 int result = -1;
609 const char *iter = fmt;
610
611 NK_ASSERT(buf);
612 NK_ASSERT(buf_size);
613 if (!buf || !buf_size || !fmt) return 0;
614 for (iter = fmt; *iter && len < buf_size; iter++) {
615 /* copy all non-format characters */
616 while (*iter && (*iter != '%') && (len < buf_size))
617 buf[len++] = *iter++;
618 if (!(*iter) || len >= buf_size) break;
619 iter++;
620
621 /* flag arguments */
622 while (*iter) {
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;
628 else break;
629 iter++;
630 }
631
632 /* width argument */
633 width = NK_DEFAULT;
634 if (*iter >= '1' && *iter <= '9') {
635 char *end;
636 width = nk_strtoi(iter, &end);
637 if (end == iter)
638 width = -1;
639 else iter = end;
640 } else if (*iter == '*') {
641 width = va_arg(args, int);
642 iter++;
643 }
644
645 /* precision argument */
646 precision = NK_DEFAULT;
647 if (*iter == '.') {
648 iter++;
649 if (*iter == '*') {
650 precision = va_arg(args, int);
651 iter++;
652 } else {
653 char *end;
654 precision = nk_strtoi(iter, &end);
655 if (end == iter)
656 precision = -1;
657 else iter = end;
658 }
659 }
660
661 /* length modifier */
662 if (*iter == 'h') {
663 if (*(iter+1) == 'h') {
664 arg_type = NK_ARG_TYPE_CHAR;
665 iter++;
666 } else arg_type = NK_ARG_TYPE_SHORT;
667 iter++;
668 } else if (*iter == 'l') {
669 arg_type = NK_ARG_TYPE_LONG;
670 iter++;
671 } else arg_type = NK_ARG_TYPE_DEFAULT;
672
673 /* specifier */
674 if (*iter == '%') {
675 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
676 NK_ASSERT(precision == NK_DEFAULT);
677 NK_ASSERT(width == NK_DEFAULT);
678 if (len < buf_size)
679 buf[len++] = '%';
680 } else if (*iter == 's') {
681 /* string */
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)
689 buf[len++] = *str++;
690 } else if (*iter == 'n') {
691 /* current length callback */
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);
696 if (n) *n = len;
697 } else if (*iter == 'c' || *iter == 'i' || *iter == 'd') {
698 /* signed integer */
699 long value = 0;
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);
704
705 /* retrieve correct value type */
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);
715
716 /* convert number to string */
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);
722
723 /* fill left padding up to a total of `width` characters */
724 if (!(flag & NK_ARG_FLAG_LEFT)) {
725 while (padding-- > 0 && (len < buf_size)) {
726 if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
727 buf[len++] = '0';
728 else buf[len++] = ' ';
729 }
730 }
731
732 /* copy string value representation into buffer */
733 if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
734 buf[len++] = '+';
735 else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
736 buf[len++] = ' ';
737
738 /* fill up to precision number of digits with '0' */
739 num_print = NK_MAX(cur_precision, num_len);
740 while (precision && (num_print > num_len) && (len < buf_size)) {
741 buf[len++] = '0';
742 num_print--;
743 }
744
745 /* copy string value representation into buffer */
746 num_iter = number_buffer;
747 while (precision && *num_iter && len < buf_size)
748 buf[len++] = *num_iter++;
749
750 /* fill right padding up to width characters */
751 if (flag & NK_ARG_FLAG_LEFT) {
752 while ((padding-- > 0) && (len < buf_size))
753 buf[len++] = ' ';
754 }
755 } else if (*iter == 'o' || *iter == 'x' || *iter == 'X' || *iter == 'u') {
756 /* unsigned integer */
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;
762
763 /* print oct/hex/dec value */
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;
768
769 /* retrieve correct value type */
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);
777
778 do {
779 /* convert decimal number into hex/oct number */
780 int digit = output_format[value % base];
781 if (num_len < NK_MAX_NUMBER_BUFFER)
782 number_buffer[num_len++] = (char)digit;
783 value /= base;
784 } while (value > 0);
785
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);
790
791 /* fill left padding up to a total of `width` characters */
792 if (!(flag & NK_ARG_FLAG_LEFT)) {
793 while ((padding-- > 0) && (len < buf_size)) {
794 if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
795 buf[len++] = '0';
796 else buf[len++] = ' ';
797 }
798 }
799
800 /* fill up to precision number of digits */
801 if (num_print && (flag & NK_ARG_FLAG_NUM)) {
802 if ((*iter == 'o') && (len < buf_size)) {
803 buf[len++] = '0';
804 } else if ((*iter == 'x') && ((len+1) < buf_size)) {
805 buf[len++] = '0';
806 buf[len++] = 'x';
807 } else if ((*iter == 'X') && ((len+1) < buf_size)) {
808 buf[len++] = '0';
809 buf[len++] = 'X';
810 }
811 }
812 while (precision && (num_print > num_len) && (len < buf_size)) {
813 buf[len++] = '0';
814 num_print--;
815 }
816
817 /* reverse number direction */
818 while (num_len > 0) {
819 if (precision && (len < buf_size))
820 buf[len++] = number_buffer[num_len-1];
821 num_len--;
822 }
823
824 /* fill right padding up to width characters */
825 if (flag & NK_ARG_FLAG_LEFT) {
826 while ((padding-- > 0) && (len < buf_size))
827 buf[len++] = ' ';
828 }
829 } else if (*iter == 'f') {
830 /* floating point */
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;
836 int padding = 0;
837
838 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
839 NK_DTOA(number_buffer, value);
840 num_len = nk_strlen(number_buffer);
841
842 /* calculate padding */
843 num_iter = number_buffer;
844 while (*num_iter && *num_iter != '.')
845 num_iter++;
846
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);
851
852 /* fill left padding up to a total of `width` characters */
853 if (!(flag & NK_ARG_FLAG_LEFT)) {
854 while (padding-- > 0 && (len < buf_size)) {
855 if (flag & NK_ARG_FLAG_ZERO)
856 buf[len++] = '0';
857 else buf[len++] = ' ';
858 }
859 }
860
861 /* copy string value representation into buffer */
862 num_iter = number_buffer;
863 if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
864 buf[len++] = '+';
865 else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
866 buf[len++] = ' ';
867 while (*num_iter) {
868 if (dot) frac_len++;
869 if (len < buf_size)
870 buf[len++] = *num_iter;
871 if (*num_iter == '.') dot = 1;
872 if (frac_len >= cur_precision) break;
873 num_iter++;
874 }
875
876 /* fill number up to precision */
877 while (frac_len < cur_precision) {
878 if (!dot && len < buf_size) {
879 buf[len++] = '.';
880 dot = 1;
881 }
882 if (len < buf_size)
883 buf[len++] = '0';
884 frac_len++;
885 }
886
887 /* fill right padding up to width characters */
888 if (flag & NK_ARG_FLAG_LEFT) {
889 while ((padding-- > 0) && (len < buf_size))
890 buf[len++] = ' ';
891 }
892 } else {
893 /* Specifier not supported: g,G,e,E,p,z */
894 NK_ASSERT(0 && "specifier is not supported!");
895 return result;
896 }
897 }
898 buf[(len >= buf_size)?(buf_size-1):len] = 0;
899 result = (len >= buf_size)?-1:len;
900 return result;
901}
902#endif
903NK_LIB int
904nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args)
905{
906 int result = -1;
907 NK_ASSERT(buf);
908 NK_ASSERT(buf_size);
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;
913 buf[buf_size-1] = 0;
914#else
915 result = nk_vsnprintf(buf, buf_size, fmt, args);
916#endif
917 return result;
918}
919#endif
920NK_API nk_hash
921nk_murmur_hash(const void * key, int len, nk_hash seed)
922{
923 /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
924 #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
925
926 nk_uint h1 = seed;
927 nk_uint k1;
928 const nk_byte *data = (const nk_byte*)key;
929 const nk_byte *keyptr = data;
930 nk_byte *k1ptr;
931 const int bsize = sizeof(k1);
932 const int nblocks = len/4;
933
934 const nk_uint c1 = 0xcc9e2d51;
935 const nk_uint c2 = 0x1b873593;
936 const nk_byte *tail;
937 int i;
938
939 /* body */
940 if (!key) return 0;
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];
947
948 k1 *= c1;
949 k1 = NK_ROTL(k1,15);
950 k1 *= c2;
951
952 h1 ^= k1;
953 h1 = NK_ROTL(h1,13);
954 h1 = h1*5+0xe6546b64;
955 }
956
957 /* tail */
958 tail = (const nk_byte*)(data + nblocks*4);
959 k1 = 0;
960 switch (len & 3) {
961 case 3: k1 ^= (nk_uint)(tail[2] << 16); /* fallthrough */
962 case 2: k1 ^= (nk_uint)(tail[1] << 8u); /* fallthrough */
963 case 1: k1 ^= tail[0];
964 k1 *= c1;
965 k1 = NK_ROTL(k1,15);
966 k1 *= c2;
967 h1 ^= k1;
968 break;
969 default: break;
970 }
971
972 /* finalization */
973 h1 ^= (nk_uint)len;
974 /* fmix32 */
975 h1 ^= h1 >> 16;
976 h1 *= 0x85ebca6b;
977 h1 ^= h1 >> 13;
978 h1 *= 0xc2b2ae35;
979 h1 ^= h1 >> 16;
980
981 #undef NK_ROTL
982 return h1;
983}
984#ifdef NK_INCLUDE_STANDARD_IO
985NK_LIB char*
986nk_file_load(const char* path, nk_size* siz, const struct nk_allocator *alloc)
987{
988 char *buf;
989 FILE *fd;
990 long ret;
991
992 NK_ASSERT(path);
993 NK_ASSERT(siz);
994 NK_ASSERT(alloc);
995 if (!path || !siz || !alloc)
996 return 0;
997
998 fd = fopen(path, "rb");
999 if (!fd) return 0;
1000 fseek(fd, 0, SEEK_END);
1001 ret = ftell(fd);
1002 if (ret < 0) {
1003 fclose(fd);
1004 return 0;
1005 }
1006 *siz = (nk_size)ret;
1007 fseek(fd, 0, SEEK_SET);
1008 buf = (char*)alloc->alloc(alloc->userdata,0, *siz);
1009 NK_ASSERT(buf);
1010 if (!buf) {
1011 fclose(fd);
1012 return 0;
1013 }
1014 *siz = (nk_size)fread(buf, 1,*siz, fd);
1015 fclose(fd);
1016 return buf;
1017}
1018#endif
1019NK_LIB int
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)
1023{
1024 int i = 0;
1025 int glyph_len = 0;
1026 float last_width = 0;
1027 nk_rune unicode = 0;
1028 float width = 0;
1029 int len = 0;
1030 int g = 0;
1031 float s;
1032
1033 int sep_len = 0;
1034 int sep_g = 0;
1035 float sep_width = 0;
1036 sep_count = NK_MAX(sep_count,0);
1037
1038 glyph_len = nk_utf_decode(text, &unicode, text_len);
1039 while (glyph_len && (width < space) && (len < text_len)) {
1040 len += glyph_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;
1045 sep_g = g+1;
1046 sep_len = len;
1047 break;
1048 }
1049 if (i == sep_count){
1050 last_width = sep_width = width;
1051 sep_g = g+1;
1052 }
1053 width = s;
1054 glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
1055 g++;
1056 }
1057 if (len >= text_len) {
1058 *glyphs = g;
1059 *text_width = last_width;
1060 return len;
1061 } else {
1062 *glyphs = sep_g;
1063 *text_width = sep_width;
1064 return (!sep_len) ? len: sep_len;
1065 }
1066}
1067NK_LIB struct nk_vec2
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)
1071{
1072 float line_height = row_height;
1073 struct nk_vec2 text_size = nk_vec2(0,0);
1074 float line_width = 0.0f;
1075
1076 float glyph_width;
1077 int glyph_len = 0;
1078 nk_rune unicode = 0;
1079 int text_len = 0;
1080 if (!begin || byte_len <= 0 || !font)
1081 return nk_vec2(0,row_height);
1082
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);
1086
1087 *glyphs = 0;
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;
1092 line_width = 0;
1093 *glyphs+=1;
1094 if (op == NK_STOP_ON_NEW_LINE)
1095 break;
1096
1097 text_len++;
1098 glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
1099 continue;
1100 }
1101
1102 if (unicode == '\r') {
1103 text_len++;
1104 *glyphs+=1;
1105 glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
1106 continue;
1107 }
1108
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);
1114 continue;
1115 }
1116
1117 if (text_size.x < line_width)
1118 text_size.x = line_width;
1119 if (out_offset)
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;
1123 if (remaining)
1124 *remaining = begin+text_len;
1125 return text_size;
1126}
1127
main API and documentation file
nk_text_width_f width
!< max height of the font
Definition nuklear.h:4025
float height
!< user provided font handle
Definition nuklear.h:4024