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_text.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * TEXT
7 *
8 * ===============================================================*/
9NK_LIB void
10nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
11 const char *string, int len, const struct nk_text *t,
12 nk_flags a, const struct nk_user_font *f)
13{
14 struct nk_rect label;
15 float text_width;
16
17 NK_ASSERT(o);
18 NK_ASSERT(t);
19 if (!o || !t) return;
20
21 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);
25
26 text_width = f->width(f->userdata, f->height, (const char*)string, len);
27 text_width += (2.0f * t->padding.x);
28
29 /* align in x-axis */
30 if (a & NK_TEXT_ALIGN_LEFT) {
31 label.x = b.x + t->padding.x;
32 label.w = NK_MAX(0, b.w - 2 * t->padding.x);
33 } else if (a & NK_TEXT_ALIGN_CENTERED) {
34 label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
35 label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
36 label.x = NK_MAX(b.x + t->padding.x, label.x);
37 label.w = NK_MIN(b.x + b.w, label.x + label.w);
38 if (label.w >= label.x) label.w -= label.x;
39 } else if (a & NK_TEXT_ALIGN_RIGHT) {
40 label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
41 label.w = (float)text_width + 2 * t->padding.x;
42 } else return;
43
44 /* align in y-axis */
45 if (a & NK_TEXT_ALIGN_MIDDLE) {
46 label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
47 label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
48 } else if (a & NK_TEXT_ALIGN_BOTTOM) {
49 label.y = b.y + b.h - f->height;
50 label.h = f->height;
51 }
52 nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
53}
54NK_LIB void
55nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
56 const char *string, int len, const struct nk_text *t,
57 const struct nk_user_font *f)
58{
59 float width;
60 int glyphs = 0;
61 int fitting = 0;
62 int done = 0;
63 struct nk_rect line;
64 struct nk_text text;
65 NK_INTERN nk_rune seperator[] = {' '};
66
67 NK_ASSERT(o);
68 NK_ASSERT(t);
69 if (!o || !t) return;
70
71 text.padding = nk_vec2(0,0);
72 text.background = t->background;
73 text.text = t->text;
74
75 b.w = NK_MAX(b.w, 2 * t->padding.x);
76 b.h = NK_MAX(b.h, 2 * t->padding.y);
77 b.h = b.h - 2 * t->padding.y;
78
79 line.x = b.x + t->padding.x;
80 line.y = b.y + t->padding.y;
81 line.w = b.w - 2 * t->padding.x;
82 line.h = 2 * t->padding.y + f->height;
83
84 fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
85 while (done < len) {
86 if (!fitting || line.y + line.h >= (b.y + b.h)) break;
87 nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
88 done += fitting;
89 line.y += f->height + 2 * t->padding.y;
90 fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
91 }
92}
93NK_API void
94nk_text_colored(struct nk_context *ctx, const char *str, int len,
95 nk_flags alignment, struct nk_color color)
96{
97 struct nk_window *win;
98 const struct nk_style *style;
99
100 struct nk_vec2 item_padding;
101 struct nk_rect bounds;
102 struct nk_text text;
103
104 NK_ASSERT(ctx);
105 NK_ASSERT(ctx->current);
106 NK_ASSERT(ctx->current->layout);
107 if (!ctx || !ctx->current || !ctx->current->layout) return;
108
109 win = ctx->current;
110 style = &ctx->style;
111 nk_panel_alloc_space(&bounds, ctx);
112 item_padding = style->text.padding;
113
114 text.padding.x = item_padding.x;
115 text.padding.y = item_padding.y;
116 text.background = style->window.background;
117 text.text = nk_rgb_factor(color, style->text.color_factor);
118 nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
119}
120NK_API void
121nk_text_wrap_colored(struct nk_context *ctx, const char *str,
122 int len, struct nk_color color)
123{
124 struct nk_window *win;
125 const struct nk_style *style;
126
127 struct nk_vec2 item_padding;
128 struct nk_rect bounds;
129 struct nk_text text;
130
131 NK_ASSERT(ctx);
132 NK_ASSERT(ctx->current);
133 NK_ASSERT(ctx->current->layout);
134 if (!ctx || !ctx->current || !ctx->current->layout) return;
135
136 win = ctx->current;
137 style = &ctx->style;
138 nk_panel_alloc_space(&bounds, ctx);
139 item_padding = style->text.padding;
140
141 text.padding.x = item_padding.x;
142 text.padding.y = item_padding.y;
143 text.background = style->window.background;
144 text.text = nk_rgb_factor(color, style->text.color_factor);
145 nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
146}
147#ifdef NK_INCLUDE_STANDARD_VARARGS
148NK_API void
149nk_labelf_colored(struct nk_context *ctx, nk_flags flags,
150 struct nk_color color, const char *fmt, ...)
151{
152 va_list args;
153 va_start(args, fmt);
154 nk_labelfv_colored(ctx, flags, color, fmt, args);
155 va_end(args);
156}
157NK_API void
158nk_labelf_colored_wrap(struct nk_context *ctx, struct nk_color color,
159 const char *fmt, ...)
160{
161 va_list args;
162 va_start(args, fmt);
163 nk_labelfv_colored_wrap(ctx, color, fmt, args);
164 va_end(args);
165}
166NK_API void
167nk_labelf(struct nk_context *ctx, nk_flags flags, const char *fmt, ...)
168{
169 va_list args;
170 va_start(args, fmt);
171 nk_labelfv(ctx, flags, fmt, args);
172 va_end(args);
173}
174NK_API void
175nk_labelf_wrap(struct nk_context *ctx, const char *fmt,...)
176{
177 va_list args;
178 va_start(args, fmt);
179 nk_labelfv_wrap(ctx, fmt, args);
180 va_end(args);
181}
182NK_API void
183nk_labelfv_colored(struct nk_context *ctx, nk_flags flags,
184 struct nk_color color, const char *fmt, va_list args)
185{
186 char buf[256];
187 nk_strfmt(buf, NK_LEN(buf), fmt, args);
188 nk_label_colored(ctx, buf, flags, color);
189}
190
191NK_API void
192nk_labelfv_colored_wrap(struct nk_context *ctx, struct nk_color color,
193 const char *fmt, va_list args)
194{
195 char buf[256];
196 nk_strfmt(buf, NK_LEN(buf), fmt, args);
197 nk_label_colored_wrap(ctx, buf, color);
198}
199
200NK_API void
201nk_labelfv(struct nk_context *ctx, nk_flags flags, const char *fmt, va_list args)
202{
203 char buf[256];
204 nk_strfmt(buf, NK_LEN(buf), fmt, args);
205 nk_label(ctx, buf, flags);
206}
207
208NK_API void
209nk_labelfv_wrap(struct nk_context *ctx, const char *fmt, va_list args)
210{
211 char buf[256];
212 nk_strfmt(buf, NK_LEN(buf), fmt, args);
213 nk_label_wrap(ctx, buf);
214}
215
216NK_API void
217nk_value_bool(struct nk_context *ctx, const char *prefix, int value)
218{
219 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));
220}
221NK_API void
222nk_value_int(struct nk_context *ctx, const char *prefix, int value)
223{
224 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);
225}
226NK_API void
227nk_value_uint(struct nk_context *ctx, const char *prefix, unsigned int value)
228{
229 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);
230}
231NK_API void
232nk_value_float(struct nk_context *ctx, const char *prefix, float value)
233{
234 double double_value = (double)value;
235 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
236}
237NK_API void
238nk_value_color_byte(struct nk_context *ctx, const char *p, struct nk_color c)
239{
240 nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);
241}
242NK_API void
243nk_value_color_float(struct nk_context *ctx, const char *p, struct nk_color color)
244{
245 double c[4]; nk_color_dv(c, color);
246 nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
247 p, c[0], c[1], c[2], c[3]);
248}
249NK_API void
250nk_value_color_hex(struct nk_context *ctx, const char *prefix, struct nk_color color)
251{
252 char hex[16];
253 nk_color_hex_rgba(hex, color);
254 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
255}
256#endif
257NK_API void
258nk_text(struct nk_context *ctx, const char *str, int len, nk_flags alignment)
259{
260 NK_ASSERT(ctx);
261 if (!ctx) return;
262 nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
263}
264NK_API void
265nk_text_wrap(struct nk_context *ctx, const char *str, int len)
266{
267 NK_ASSERT(ctx);
268 if (!ctx) return;
269 nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
270}
271NK_API void
272nk_label(struct nk_context *ctx, const char *str, nk_flags alignment)
273{
274 nk_text(ctx, str, nk_strlen(str), alignment);
275}
276NK_API void
277nk_label_colored(struct nk_context *ctx, const char *str, nk_flags align,
278 struct nk_color color)
279{
280 nk_text_colored(ctx, str, nk_strlen(str), align, color);
281}
282NK_API void
283nk_label_wrap(struct nk_context *ctx, const char *str)
284{
285 nk_text_wrap(ctx, str, nk_strlen(str));
286}
287NK_API void
288nk_label_colored_wrap(struct nk_context *ctx, const char *str, struct nk_color color)
289{
290 nk_text_wrap_colored(ctx, str, nk_strlen(str), color);
291}
292
main API and documentation file
nk_text_width_f width
!< max height of the font
Definition nuklear.h:4009
float height
!< user provided font handle
Definition nuklear.h:4008