Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use Nuklear without baking font? #77

Open
mindoff6502 opened this issue Mar 10, 2020 · 4 comments
Open

Can I use Nuklear without baking font? #77

mindoff6502 opened this issue Mar 10, 2020 · 4 comments

Comments

@mindoff6502
Copy link

mindoff6502 commented Mar 10, 2020

I mean I need dynamic font stash to add font glyph dynamicly.
The demo seems baking all characters with font size info.Which I don't need.

Think I will use multiple font and different font size.
For some font I just need some of the character,lets's say
Chapter 1 with font size 96.
Other dialogues font with font 24.And so on.no need for all.

@dumblob
Copy link
Member

dumblob commented Mar 10, 2020

It should be possible. Btw. as it stands in the API you can, pardon, actually must choose which glyph ranges you want to bake. Just read the doc in nuklear.h 😉.

See also #58 , #20 and the old wiki https://github.com/vurtun/nuklear/wiki/UTF-8-Support (feel free to copy over the old wiki here in this project to help us finish the project migration 😉 ).

@mindoff6502
Copy link
Author

mindoff6502 commented Mar 27, 2020

Thank you for replying.

OK,I think I got it wrong.Atlas does not bake all the characters because the memory didn't rise up.

Then should it be any problem causing chinese characters do not display at bigger font size?

I test font size with less equal than 30 with only one font,and everything goes well.

If I raise font size over 30,say 31 or 32 or 48 or bigger.

Nothing displayed,just a black rect on screen,I think it's that window size displayed,just a square black.

This is for one font case.

If I create 2 fonts,even with font size 24,the demo app will go black.

And since the memory is about 52,000 KB from font size 12 to 24,it doesn't seem to be the texture problem.

So I doubt something else may cause black rect,no character displayed.

Does Nuklear UI pre allocate something for none used glyph and overflow or exceed some limit which causing this?

I tried to change many ttf fonts,the result stays the same.

@dumblob
Copy link
Member

dumblob commented Mar 27, 2020

@mindoff6502 there was recently a huge update of the font backend. Did you use the latest Nuklear version from the master branch for your tests? If not, could you please retest with the current version in the master branch?

@mindoff6502
Copy link
Author

mindoff6502 commented Mar 28, 2020

Hi,thanks for update,I update to the latest master.

Still get black rect with bigger font size.

Though font size raise from 30 to 42.But with bigger font size 44 or 48 or 64 or 96,still get black rect.

Testing this with Windows 7 64 bit,VS2017.

Example using glfw_opengl3.

Font using

https://github.com/Tamshen/Freecommercialfont/raw/master/C002_%E5%BA%9E%E9%97%A8%E6%AD%A3%E9%81%93%E7%B3%BB%E5%88%97/%E5%BA%9E%E9%97%A8%E6%AD%A3%E9%81%93%E6%A0%87%E9%A2%98%E4%BD%93.ttf

You can download this font and rename to cnfont01.ttf

And there are more chinese fonts if you need for testing.

They got lots of ttf and otf font files.

https://github.com/Tamshen/Freecommercialfont

And here is my testing code

main.cpp

/* nuklear - 1.32.0 - public domain */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#include <time.h>

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_GL3_IMPLEMENTATION
#define NK_KEYSTATE_BASED_INPUT
#include "../../nuklear.h"
#include "nuklear_glfw_gl3.h"

#define WINDOW_WIDTH 1200
#define WINDOW_HEIGHT 800

#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024

static void error_callback(int e, const char *d)
{
printf("Error %d: %s\n", e, d);
}

int main(void)
{
/* Platform */
static GLFWwindow *win;
int width = 0, height = 0;
struct nk_context *ctx;
struct nk_colorf bg;

/* GLFW */
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
	fprintf(stdout, "[GFLW] failed to init!\n");
	exit(1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef APPLE
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
glfwMakeContextCurrent(win);
glfwGetWindowSize(win, &width, &height);

/* OpenGL */
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glewExperimental = 1;
if (glewInit() != GLEW_OK) {
	fprintf(stderr, "Failed to setup GLEW\n");
	exit(1);
}

ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS);

nk_font *myfont = nullptr;
float fontSize = 44;
struct nk_font_config cfg1 = nk_font_config(fontSize);

{
	struct nk_font_atlas *atlas;
	nk_glfw3_font_stash_begin(&atlas);

	cfg1.range = nk_font_chinese_glyph_ranges();

	myfont = nk_font_atlas_add_from_file(atlas, "./Asset/Font/cnfont01.ttf", fontSize, &cfg1);

	nk_glfw3_font_stash_end();

	nk_style_set_font(ctx, &myfont->handle);

}

bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
while (!glfwWindowShouldClose(win))
{
	/* Input */
	glfwPollEvents();
	nk_glfw3_new_frame();

	/* GUI */
	if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
		NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
		NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE))
	{
	}
	nk_end(ctx);

	/* Draw */
	glfwGetWindowSize(win, &width, &height);
	glViewport(0, 0, width, height);
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(bg.r, bg.g, bg.b, bg.a);

	nk_glfw3_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
	glfwSwapBuffers(win);
}
nk_glfw3_shutdown();
glfwTerminate();
return 0;

}

//====================
And I should make more clear what I am looking for.

What I am looking for is a GUI with dynamic glyph.

The famious Dear Imgui does not work for me,because it bake all the character at once and need to set character code range at the beginning.

Since I need multi fonts and multi font size with chinese character,I can't use a font with 2 chinese characters with font size 96 at the beginning and have to bake all 20,000 unsued characters in texture,that's stupid.

Sure I can lower the range with used character,but font characters need may change from time to time.So it's also not a good idea to set character range.

So for font rendering I turned to fontstash,the realtime font renderring which doesn't need to care about character range and font size

Which is also what Unity3D font renderring does.
And other framework such as SFML does.

Now what I need is a GUI work with this kind of font renderring.

For weeks I have tried a few GUI lib.

Imgui
Nuklear
microui
nanogui

They all got their advantages and flaws.

Imgui can use bigger font size,but need to set character range,and bake lot of unused texture at once,not convenient.

Nuklear has problem with bigger font size,which I am facing now.

microui easy and short,no custom font support,still a good project to learn.

nanogui,strange way to use custom font,packing 3 fonts in a dll,and depending on nanovg which is a little hard to adapt to my project,and have problem dealing with Unicode text input.

So I am hoping Nuklear may work with larger font size.

Still if none works,I will have to dive into how to write a custom GUI.

Writing GUI is an unknown zone to me,and it seems lots of work need to be done,won't be easy.

Source code is huge and C style,which is hard to trace and understand from cpp.

//====================
I start to read those GUI source codes.

Not as hard as I thought.

Maybe I can write a simple GUI for my need before the official fixup.

Anyway thanks to all those open source GUIs

Learning new knowledge is kind of fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants