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

Proposal: Support for different NVGcontext types #667

Open
mulle-nat opened this issue Feb 6, 2024 · 0 comments
Open

Proposal: Support for different NVGcontext types #667

mulle-nat opened this issue Feb 6, 2024 · 0 comments

Comments

@mulle-nat
Copy link
Contributor

When you use different threads for layouting and drawing, you will notice that one aspect of nanovg becomes very limiting: you always need a full blown graphics context for everything. This usually means also an OS window. But for layouting you usually only need some font metrics, which can be provided by FreeType (or STB). Neither of them need OpenGL to work.

Because the font implementation of nanovg is somewhat abstracted (over STB and FreeType) and hidden, you run the risk of recreating a lot of code that is very similiar but still slightly different to nanovg, when you need to use FreeType or STB directly.

The solution IMO is to create nvgContext with different types (I added another NVG_PATH_CONTEXT type for experimental purposes):

enum NVGcontextType {
	NVG_FULL_CONTEXT = 0,
	NVG_PATH_CONTEXT = 1,
	NVG_FONT_CONTEXT = 2
};
struct NVGparams {
	void* userPtr;
	int edgeAntiAlias;
// @mulle-nanovg@ >>
	enum NVGcontextType   contextType;
...

During creation of a NVG_FONT_CONTEXT context nanovg will not attempt to access any OpenGL functionality. At runtime each function in nanovg is asserted for compliance.

e.g.

void nvgTextBox(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end)
{
	NOT_AVAILABLE_IN_FONT_CONTEXT( ctx);
	NOT_AVAILABLE_IN_PATH_CONTEXT( ctx);
...

Now you can write layout code, that uses nanovg functions to determine font metrics.
You can view the actual working code in my nanovg fork.

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

1 participant