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