Skip to content

Commit 6b2c5c4

Browse files
authored
Merge pull request Immediate-Mode-UI#186 from 2asoft/stbtt_memory_management_through_allocator
memory allocation: stb_truetype memory management through the nk_font_atlas allocator (otherwise nuklear.h will not build when NK_INCLUDE_DEFAULT_ALLOCATOR is not defined)
2 parents 7022a95 + d30e07f commit 6b2c5c4

File tree

9 files changed

+65
-31
lines changed

9 files changed

+65
-31
lines changed

nuklear.h

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5701,18 +5701,6 @@ template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
57015701
#define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
57025702
#endif
57035703

5704-
#ifdef NK_IMPLEMENTATION
5705-
#define STB_RECT_PACK_IMPLEMENTATION
5706-
#define STB_TRUETYPE_IMPLEMENTATION
5707-
#endif
5708-
5709-
#ifndef STBTT_malloc
5710-
static nk_handle fictional_handle = {0};
5711-
5712-
#define STBTT_malloc(x,u) nk_malloc( fictional_handle, 0, x )
5713-
#define STBTT_free(x,u) nk_mfree( fictional_handle , x)
5714-
#endif
5715-
57165704
#endif /* NK_NUKLEAR_H_ */
57175705

57185706
#ifdef NK_IMPLEMENTATION
@@ -6043,6 +6031,32 @@ NK_LIB void nk_draw_property(struct nk_command_buffer *out, const struct nk_styl
60436031
NK_LIB void nk_do_property(nk_flags *ws, struct nk_command_buffer *out, struct nk_rect property, const char *name, struct nk_property_variant *variant, float inc_per_pixel, char *buffer, int *len, int *state, int *cursor, int *select_begin, int *select_end, const struct nk_style_property *style, enum nk_property_filter filter, struct nk_input *in, const struct nk_user_font *font, struct nk_text_edit *text_edit, enum nk_button_behavior behavior);
60446032
NK_LIB void nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant, float inc_per_pixel, const enum nk_property_filter filter);
60456033

6034+
#ifdef NK_INCLUDE_FONT_BAKING
6035+
6036+
#define STB_RECT_PACK_IMPLEMENTATION
6037+
#define STB_TRUETYPE_IMPLEMENTATION
6038+
6039+
/* Allow consumer to define own STBTT_malloc/STBTT_free, and use the font atlas' allocator otherwise */
6040+
#ifndef STBTT_malloc
6041+
static void*
6042+
nk_stbtt_malloc(nk_size size, void *user_data) {
6043+
struct nk_allocator *alloc = (struct nk_allocator *) user_data;
6044+
return alloc->alloc(alloc->userdata, 0, size);
6045+
}
6046+
6047+
static void
6048+
nk_stbtt_free(void *ptr, void *user_data) {
6049+
struct nk_allocator *alloc = (struct nk_allocator *) user_data;
6050+
alloc->free(alloc->userdata, ptr);
6051+
}
6052+
6053+
#define STBTT_malloc(x,u) nk_stbtt_malloc(x,u)
6054+
#define STBTT_free(x,u) nk_stbtt_free(x,u)
6055+
6056+
#endif // STBTT_malloc
6057+
6058+
#endif // NK_INCLUDE_FONT_BAKING
6059+
60466060
#endif
60476061

60486062

@@ -16419,7 +16433,9 @@ nk_font_bake_pack(struct nk_font_baker *baker,
1641916433
/* setup font baker from temporary memory */
1642016434
for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
1642116435
it = config_iter;
16422-
do {if (!stbtt_InitFont(&baker->build[i++].info, (const unsigned char*)it->ttf_blob, 0))
16436+
struct stbtt_fontinfo *font_info = &baker->build[i++].info;
16437+
font_info->userdata = alloc;
16438+
do {if (!stbtt_InitFont(font_info, (const unsigned char*)it->ttf_blob, 0))
1642316439
return nk_false;
1642416440
} while ((it = it->n) != config_iter);
1642516441
}
@@ -28293,7 +28309,7 @@ nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
2829328309
sym = style->combo.sym_hover;
2829428310
else if (is_clicked)
2829528311
sym = style->combo.sym_active;
28296-
else
28312+
else
2829728313
sym = style->combo.sym_normal;
2829828314

2829928315
/* represents whether or not the combo's button symbol should be drawn */
@@ -29154,6 +29170,7 @@ nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
2915429170
/// - [yy]: Minor version with non-breaking API and library changes
2915529171
/// - [zz]: Bug fix version with no direct changes to API
2915629172
///
29173+
/// - 2020/09/05 (4.05.0) - Use the nk_font_atlas allocator for stb_truetype memory management.
2915729174
/// - 2020/09/04 (4.04.1) - Replace every boolean int by nk_bool
2915829175
/// - 2020/09/04 (4.04.0) - Add nk_bool with NK_INCLUDE_STANDARD_BOOL
2915929176
/// - 2020/06/13 (4.03.1) - Fix nk_pool allocation sizes.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuklear",
3-
"version": "4.03.1",
3+
"version": "4.05.0",
44
"repo": "Immediate-Mode-UI/Nuklear",
55
"description": "A small ANSI C gui toolkit",
66
"keywords": ["gl", "ui", "toolkit"],

src/CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/// - [yy]: Minor version with non-breaking API and library changes
99
/// - [zz]: Bug fix version with no direct changes to API
1010
///
11+
/// - 2020/09/05 (4.05.0) - Use the nk_font_atlas allocator for stb_truetype memory management.
1112
/// - 2020/09/04 (4.04.1) - Replace every boolean int by nk_bool
1213
/// - 2020/09/04 (4.04.0) - Add nk_bool with NK_INCLUDE_STANDARD_BOOL
1314
/// - 2020/06/13 (4.03.1) - Fix nk_pool allocation sizes.

src/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def print_help():
99
"""usage: python single_header_packer.py --macro <macro> [--intro <files>] --extern <files> --pub <files> --priv1 <files> --priv2 <files> [--outro <files>]
1010
1111
where <files> can be a comma-separated list of files. e.g. --priv *.c,inc/*.h
12-
12+
1313
The 'extern' files are placed between 'priv1' and 'priv2'.
1414
1515
The resulting code is packed as follows:

src/nuklear.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5480,16 +5480,4 @@ template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
54805480
#define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
54815481
#endif
54825482

5483-
#ifdef NK_IMPLEMENTATION
5484-
#define STB_RECT_PACK_IMPLEMENTATION
5485-
#define STB_TRUETYPE_IMPLEMENTATION
5486-
#endif
5487-
5488-
#ifndef STBTT_malloc
5489-
static nk_handle fictional_handle = {0};
5490-
5491-
#define STBTT_malloc(x,u) nk_malloc( fictional_handle, 0, x )
5492-
#define STBTT_free(x,u) nk_mfree( fictional_handle , x)
5493-
#endif
5494-
54955483
#endif /* NK_NUKLEAR_H_ */

src/nuklear_combo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
102102
sym = style->combo.sym_hover;
103103
else if (is_clicked)
104104
sym = style->combo.sym_active;
105-
else
105+
else
106106
sym = style->combo.sym_normal;
107107

108108
/* represents whether or not the combo's button symbol should be drawn */

src/nuklear_font.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ nk_font_bake_pack(struct nk_font_baker *baker,
190190
/* setup font baker from temporary memory */
191191
for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
192192
it = config_iter;
193-
do {if (!stbtt_InitFont(&baker->build[i++].info, (const unsigned char*)it->ttf_blob, 0))
193+
struct stbtt_fontinfo *font_info = &baker->build[i++].info;
194+
font_info->userdata = alloc;
195+
do {if (!stbtt_InitFont(font_info, (const unsigned char*)it->ttf_blob, 0))
194196
return nk_false;
195197
} while ((it = it->n) != config_iter);
196198
}

src/nuklear_internal.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,31 @@ NK_LIB void nk_draw_property(struct nk_command_buffer *out, const struct nk_styl
324324
NK_LIB void nk_do_property(nk_flags *ws, struct nk_command_buffer *out, struct nk_rect property, const char *name, struct nk_property_variant *variant, float inc_per_pixel, char *buffer, int *len, int *state, int *cursor, int *select_begin, int *select_end, const struct nk_style_property *style, enum nk_property_filter filter, struct nk_input *in, const struct nk_user_font *font, struct nk_text_edit *text_edit, enum nk_button_behavior behavior);
325325
NK_LIB void nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant, float inc_per_pixel, const enum nk_property_filter filter);
326326

327+
#ifdef NK_INCLUDE_FONT_BAKING
328+
329+
#define STB_RECT_PACK_IMPLEMENTATION
330+
#define STB_TRUETYPE_IMPLEMENTATION
331+
332+
/* Allow consumer to define own STBTT_malloc/STBTT_free, and use the font atlas' allocator otherwise */
333+
#ifndef STBTT_malloc
334+
static void*
335+
nk_stbtt_malloc(nk_size size, void *user_data) {
336+
struct nk_allocator *alloc = (struct nk_allocator *) user_data;
337+
return alloc->alloc(alloc->userdata, 0, size);
338+
}
339+
340+
static void
341+
nk_stbtt_free(void *ptr, void *user_data) {
342+
struct nk_allocator *alloc = (struct nk_allocator *) user_data;
343+
alloc->free(alloc->userdata, ptr);
344+
}
345+
346+
#define STBTT_malloc(x,u) nk_stbtt_malloc(x,u)
347+
#define STBTT_free(x,u) nk_stbtt_free(x,u)
348+
349+
#endif // STBTT_malloc
350+
351+
#endif // NK_INCLUDE_FONT_BAKING
352+
327353
#endif
328354

src/paq.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/sh
2-
python build.py --macro NK --intro HEADER --pub nuklear.h --priv1 nuklear_internal.h,nuklear_math.c,nuklear_util.c,nuklear_color.c,nuklear_utf8.c,nuklear_buffer.c,nuklear_string.c,nuklear_draw.c,nuklear_vertex.c --extern stb_rect_pack.h,stb_truetype.h --priv2 nuklear_font.c,nuklear_input.c,nuklear_style.c,nuklear_context.c,nuklear_pool.c,nuklear_page_element.c,nuklear_table.c,nuklear_panel.c,nuklear_window.c,nuklear_popup.c,nuklear_contextual.c,nuklear_menu.c,nuklear_layout.c,nuklear_tree.c,nuklear_group.c,nuklear_list_view.c,nuklear_widget.c,nuklear_text.c,nuklear_image.c,nuklear_button.c,nuklear_toggle.c,nuklear_selectable.c,nuklear_slider.c,nuklear_progress.c,nuklear_scrollbar.c,nuklear_text_editor.c,nuklear_edit.c,nuklear_property.c,nuklear_chart.c,nuklear_color_picker.c,nuklear_combo.c,nuklear_tooltip.c --outro LICENSE,CHANGELOG,CREDITS
2+
python build.py --macro NK --intro HEADER --pub nuklear.h --priv1 nuklear_internal.h,nuklear_math.c,nuklear_util.c,nuklear_color.c,nuklear_utf8.c,nuklear_buffer.c,nuklear_string.c,nuklear_draw.c,nuklear_vertex.c --extern stb_rect_pack.h,stb_truetype.h --priv2 nuklear_font.c,nuklear_input.c,nuklear_style.c,nuklear_context.c,nuklear_pool.c,nuklear_page_element.c,nuklear_table.c,nuklear_panel.c,nuklear_window.c,nuklear_popup.c,nuklear_contextual.c,nuklear_menu.c,nuklear_layout.c,nuklear_tree.c,nuklear_group.c,nuklear_list_view.c,nuklear_widget.c,nuklear_text.c,nuklear_image.c,nuklear_button.c,nuklear_toggle.c,nuklear_selectable.c,nuklear_slider.c,nuklear_progress.c,nuklear_scrollbar.c,nuklear_text_editor.c,nuklear_edit.c,nuklear_property.c,nuklear_chart.c,nuklear_color_picker.c,nuklear_combo.c,nuklear_tooltip.c --outro LICENSE,CHANGELOG,CREDITS > ../nuklear.h
33

0 commit comments

Comments
 (0)