Skip to content

Proposal: Support for different NVGcontext types #667

@mulle-nat

Description

@mulle-nat

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions