nanovega font improvement from me. from community: DConf Online 2021 announcement, GtkD blog back to work, DCompute update

Posted 2021-09-13

See the community announcements for several new things and see below for some discussion on my new font improvement: you can load system ttfs into opengl usage libraries now.

Core D Development Statistics

In the community

Community announcements

See more at the announce forum.

What Adam is working on

Until last weekend, there were two distinct forms of font functions in the arsd library: ones meant to work with images or opengl, which required you to load a .ttf file, and ones meant to work with classical drawing contexts on operating system surfaces, which loaded fonts by name from the OS. These would only partially combine: you could kinda draw to windows with a .ttf file by putting it to in image, then drawing the image, but you couldn't load a font from the OS and draw it to opengl (well, unless you had the OS draw it to a bitmap first, then loaded that. So it was possible just annoying.)

I've now added a function, OperatingSystemFont.getTtfBytes, to help bridge this.

Comic Sans MS loaded on Windows

The procedure is simple enough:

auto sdf = new OperatingSystemFont("Comic Sans MS");
if(sdf.isNull) throw new Exception("OS load failed");

window.redrawNVGScene = delegate (nvg) {
	/* snip */
	auto bytes = sdf.getTtfBytes();
	auto font = nvg.createFontMem("test", bytes.ptr, cast(int) bytes.length, false);
	if(font == -1) throw new Exception("it didn't load");

	nvg.fontFace("test");
        nvg.fontSize(70);
        nvg.fontBlur(2.5);

        nvg.fillColor = NVGColor.white;
        nvg.text(40, 40, "Hello world!!!!");

	/* snip other stuff */
};

So first, it loads the font from the OS using simpledisplay's class, then it can bridge that into nanovega by getting the ttf bytes and loading it as a font file preloaded in memory. That's it! I could also load this in my OpenGlLimitedFont (in my game.d, this is a baked texture atlas) or the TtfFont (low-level bitmap rendering, a port of stb_ttf) structures the same way - just loading it as ttf bytes.

I plan to add convenience overloads as well to the libraries to just take the OS Font object directly, and maybe one with a fallback list it figures out automatically.

Then, with nanovega, since it can render glyphs not just as bitmaps, but as vector paths, we can have some fun:

A huge glyph with gradient fill and outline

blurring, outlines, and gradient fills all doable here, not even that complicated. These functions aren't new, but now that it can load fonts from the OS, you don't have to bundle as many and I'll add more documentation for this soon.

Of course, you may want to bundle a font with your application anyway just for consistent appearance across user systems. That still works just as well. And tip: you can use D's import() expression to compile it right into your exe too, just casting it to immutable(ubyte)[] to load it as a memory font.

I have a lot of work to do on the gui pieces in the near future so hopefully I'll have another nice update next month.

Between now and then though I will tag arsd 10.3 to include the new features described above. They are pushed to ~master at this time but not yet tagged for a dub release. If you want to try it yourself, grab simpledisplay.d master for your project.