nanovg icon indicating copy to clipboard operation
nanovg copied to clipboard

Proposal: Support for different NVGcontext types

Open mulle-nat opened this issue 5 months ago • 0 comments

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.

mulle-nat avatar Feb 06 '24 15:02 mulle-nat