moderngl / moderngl-window

A cross platform utility library for ModernGL making window creation and resource loading simple
MIT License
244 stars 57 forks source link

Support basic text/glyph rendering #25

Open einarf opened 5 years ago

einarf commented 5 years ago

It would be nice to support basic text rendering. I already have a very simple version in demosys-py that could be ported over and possibly improved: https://github.com/Contraz/demosys-py/tree/master/demosys/effects/text

Right now it's just using a texture generated with freetype-py loaded as a TextureArray.

This part is pretty interesting:

From PyPI, recommended: pip install freetype-py. This will install the library with a bundled FreeType binary, so you’re ready to go on Windows, macOS and Linux (all with 32 and 64 bit x86 architecture support).

This means we could provide cross platform support for generating text on the fly in moderngl_window. Still it should probably be an optional dependency.

https://github.com/rougier/freetype-py

lordmauve commented 4 years ago

Doing some research previously I found that rendering freetype glyphs is not enough for correct text rendering; you to use Harfbuzz for shaping Unicode code point sequences, which gives positioned glyphs from the font including chains of accents. Harfbuzz is the only complete open source implementation of this and almost everything uses it.

I don't know if freetype-py uses Harfbuzz itself, but there are Python bindings for Harfbuzz available.

einarf commented 3 years ago

Considering adding a simple bitmapped monospaced text rendere using veramono just so we have something for now.

naveen521kk commented 3 years ago

After https://github.com/ManimCommunity/ManimPango/issues/28 it should be possible to use ManimPango :)

einarf commented 3 years ago

After ManimCommunity/ManimPango#28 it should be possible to use ManimPango :)

Yep! That would be fun to try out. After this is done :)

matanox commented 2 years ago

If I may join this conversation, does text rendering have to be a feature of moderngl? Would user code be able to alternatively use any particular other (python) library to draw text into a bitmap, then embed it in a texture provided to moderngl for actual rendering? or will that fall short of handling all the concerns inherent to getting clean text on a display?

einarf commented 2 years ago

If I may join this conversation, does text rendering have to be a feature of moderngl? Would user code be able to alternatively use any particular other (python) library to draw text into a bitmap, then embed it in a texture provided to moderngl for actual rendering? or will that fall short of handling all the concerns inherent to getting clean text on a display?

Text rendering will never be a part of moderngl. You can of course use any library you want to implement this yourself. This repository is not moderngl, but moderngl-window. it's a windowing and utility library for moderngl.

Making a good text renderer in moderngl would have the same challenges as any other library or language. I did already make a bitmapped text renderer and the plan was at least to use freetype-py to get something started. It would use the (incomplete) texture atlas in the master branch (or a specific variant for glyphs)

matanox commented 2 years ago

Thanks for commenting and I guess I had missed the most related thread in moderngl which was probably https://github.com/moderngl/moderngl/issues/203.

I guess you mean that perhaps (or at most) moderngl would go one step in the direction of enabling better text rendering performance by implementing a texture atlas, which would allow user code to choose a font library such as freetype-py and throw its outputs into the atlas. And that one can currently, without such a feature added in moderngl, and at the cost of (especially in dynamic scenes) incurring less than optimal performance, cast fonts rendered with other libraries, into a plain moderngl texture if they are okay with having less optimal performance.

And that this all without something like harfbuzz, would probably work in a visually correct, or in a visually pleasing way, only for orthographically feature-poor languages like English and not for many/most other of the world's languages that have character adjoining rules and/or accents that need to be laid out in specific orders from their unicode representations.

Maybe I'm a bit behind on rendering pipelines and architectures though, so I may well suffice with what I learned from your earlier comment.

lordmauve commented 2 years ago

Just wanted to comment in this thread that to get towards state of the art font rendering you need harfbuzz, sure, but you should be looking towards something like msdf-atlas-gen to generate the atlases.