GUI library for rich text?

(no, I am definitely not writing my own text editor)

I need a library that can do GUI stuff for me. I don’t care at all about buttons, forms, integration with OS, etc. I basically want just to draw text, but I need support for rich text (different fonts, colors, wavy underlines, that sort of thing). What are some good options in Zig space here? (I probably would need bindings to some C library, which I am fine with).

Alternatively, is “do it yourself” a reasonable path here, under constraint that I want the stuff to run on Mac, Linux and Windows at least? If so, what are some good options to study?

I guess freetype (for rendering) and harfbuzz (for shaping) should still be a good option.

I think you’re competent enough to do it on your own, but I guess building these things would be then your life for quite some time :wink:

This article describes some of the challenges of text rendering Text rendering hates you

Complementary is this article Text editing hates you too.

2 Likes

Speaking from experience, for 98% of programmers and applications, do it yourself is not a practical option if you want a good results, unless your use case is extremely simple or you’re willing to put in a lot of work.

OS and browser companies have invested multiple man-decades of work to make it happen. Unfortunately each system does it differently.

I can’t tell you much on the portability side. In a previous life back in the late 90s I added Unicode support to Visual Studio, including the text editor. We supported Windows 95/98, (an ANSI OS) and Windows NT (Unicode). As a “plain text” editor, the problem space around layout was much simpler. It was all implemented over the span of a year and a half (IIRC), but I was working on lots of other things at the same time. Being Windows only, we used only Windows APIs – Uniscribe for the shaping and glyph handling. There was no API support for font linking - I had to cook my own, which including some limited parsing of TTF to calculate a font’s glyph coverage.

If you love text, language, typography, it can be enjoyable to do. It’s not bad if your use cases are simple (e.g. your layout is rendering to a box, and you don’t need (for example) tables, and you don’t need multiple font sizes, and you can constrain what languages you support.

Unfortunately, the complexity curve is a steep one with each new capability you want to have. To keep the effort in bounds, you have to strictly limit what you expect to achieve, on many dimensions.

Multiple languages? Then you need font-linking, and a way to figure out how to match them up so that the Chinese or Arabic within your other text is not only legible but looks good. Picking the same point size isn’t gonna do that. Do you need to support selecting text for Copy? Then you’re almost halfway to supporting editing, in terms of the display requirements and caret positioning logic.

Need to wrap lines? Then you need line-breaking, which can get quite complex (Bidi, Kinsoku rules, …), and many people don’t realize that it can’t be done strictly by analyzing characters if you want quality line breaks not in the middle of words. Thai doesn’t use spaces: a dictionary is required.

Harfbuzz is probably the only game in town for script analysis and portable shaping. I get the impression is that Pango has the best integration for that, but I have no direct experience.

4 Likes

There’s this new thing called ‘skribidi’ by Mikko Mononen:

I haven’t worked with it yet, so not sure how easy or hard it is to integrate into an UI system, but it’s all in C so it should be easy to use from Zig, and Mikko definitely knows his shit :wink:

5 Likes

I have not used it with Zig, but Pango (often paired with Cairo) would likely be worth looking into. It is a C library, but has a pretty good API that abstracts away all of the nitty-gritty mess that is font rendering, text layout, styles, etc. IIRC, it is the backbone GTK uses for its rendering, so it is robust and battle-tested.

I am unsure what level of abstraction you are looking for, but based on your question I gather that you are looking for a somewhat ready-made solution, which would make this a good fit. It also works on the the 3 major desktops, and perhaps even the BSD-family?

2 Likes

Ghostty’s code base may be good to look at. They have taken great pains to get their font rendering correct. Under the hood they use harfbuzz and freetype.

Here are the config options… many for fonts

Could be a good reference implementation.

3 Likes