Skip to content

Conversation

@2asoft
Copy link
Contributor

@2asoft 2asoft commented Sep 5, 2020

After the tremendous work done to update stb_truetype to a newer version, Nuklear lost the ability to control stb_truetype's memory management via Nuklear's allocators, instead delegating it to the default (malloc/free) allocation scheme.
In cases where Nuklear is compiled without the default allocation scheme, the entire library would fail to build.
More details in this comment.

This PR implements stb_truetype STBTT_malloc/STBTT_free to delegate to the nk_allocator used by nk_font_atlas.
It also leaves the ability for the consumer to define own STBTT_malloc/STBTT_free implementations if they so desire.

#define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
#endif

#ifdef NK_IMPLEMENTATION
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is rebuilt with paq.sh.

@@ -1,3 +1,3 @@
#!/bin/sh
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added redirect to prebuilt nuklear.h file, as in paq.bat.

#define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
#endif

#ifdef NK_IMPLEMENTATION
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from here into src/nuklear_internal.h.
These are internal implementation details that do not need to be exposed.

for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
it = config_iter;
do {if (!stbtt_InitFont(&baker->build[i++].info, (const unsigned char*)it->ttf_blob, 0))
struct stbtt_fontinfo *font_info = &baker->build[i++].info;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stb_truetype passes the userdata field of stbtt_fontinfo as the second argument to STBTT_malloc/STBTT_free.
Set the userdata here to point to the allocator (alloc), and use it in the custom implementations of STBTT_malloc/STBTT_free.

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);
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);

#ifdef NK_INCLUDE_FONT_BAKING
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved here from src/nuklear_internal.h.
These are internal implementation details that do not need to be exposed.

@2asoft
Copy link
Contributor Author

2asoft commented Oct 2, 2020

@dumblob could you please take a look at this PR?
I've verified locally that it works with the demo code in repo.
Do you think there's any additional verification that needs to be done here?

@dumblob
Copy link
Member

dumblob commented Oct 2, 2020

@2asoft sorry for the delay. Yeah, I've looked at it and technically it LGTM. Please take a look at https://github.com/Immediate-Mode-UI/Nuklear#reviewers-guide to add some more bits to have this PR accepted 😉 (e.g. version bump), thanks!

2asoft added 3 commits October 2, 2020 22:35
…by default And allow the consumer to customize by redefining STBTT_malloc/STBTT_free
@2asoft 2asoft force-pushed the stbtt_memory_management_through_allocator branch from 488deb4 to d30e07f Compare October 3, 2020 05:49
@2asoft
Copy link
Contributor Author

2asoft commented Oct 3, 2020

Thanks. I updated the version, and as far as I can tell all the recommendations in the guide are covered.

@dumblob dumblob merged commit 6b2c5c4 into Immediate-Mode-UI:master Oct 5, 2020
@dumblob
Copy link
Member

dumblob commented Oct 5, 2020

Thanks for patience and great feedback!

Btw. would you want to get commit permissions to the repo?

It's basically only about reviews (essentially the things from the Reviewer's guide). As you see, there are only very few reviewers currently and the library development is very slow which is a pity, but we can't afford more time.

@2asoft 2asoft deleted the stbtt_memory_management_through_allocator branch October 7, 2020 01:46
@2asoft
Copy link
Contributor Author

2asoft commented Oct 7, 2020

I'll gladly take the commit bit, but I can't devote a large amount of time to Nuklear at the moment.
It does align with some of my current interests so I will be able to spend some time on it, but things may change in the future.

@dumblob
Copy link
Member

dumblob commented Oct 7, 2020

@2asoft invitation sent out 😉.

Just follow the reviewers guidelines (especially the point "at least two reviewers (both different from the PR author) shall approve nuklear.h changes and at least one shall approve changes to other files". That's it. And take your time.

@dumblob
Copy link
Member

dumblob commented Oct 7, 2020

Btw. as you noticed we sometimes break these guidelines, despite we always try hard to stick with them (otherwise issues like #198 tend to appear).

@dumblob
Copy link
Member

dumblob commented Oct 10, 2020

@2asoft I'm glad you managed - so welcome to our organization 😉!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants