alexheretic / glyph-brush

Fast GPU cached text rendering
Apache License 2.0
688 stars 52 forks source link

Allow glyph brush to be created without a font #82

Closed chinedufn closed 4 years ago

chinedufn commented 4 years ago

Problem

Right now in order to create a glyph brush you need to use the GlyphBrushBuilder and add at least one font.

If your application loads fonts during runtime then you cannot create your brush until your font loads.

This means that you need to use a trick such as having brush: RefCell<Option<GlyphBrush<...>>> and replacing the None with Some(brush) whenever your first font loads

Potential Solution

Perhaps there could be a GlyphBrushBuilder::new() method that had all of the same default settings as add_font_bytes and the like? In fact the add_font_bytes etc could just wrap the new() method.

alexheretic commented 4 years ago

This was deliberate to ensure that font 0 always existed, so Section::default() will always make sense. Otherwise it's possible for a default text section to cause glyph_brush to panic.

One workaround is just to load one static font into the brush and not necessarily use it. Otherwise managing a later initialization is best.

alexheretic commented 4 years ago

I guess I want to avoid people just constructing in an obvious way Builder::new().build() and then getting a panic later. It's surprising this way.

But maybe we could have GlyphBrushBuilder::without_fonts() with suitable doc warnings that if you use a GlyphBrush without any fonts it'll panic. This way it should be less surprising to new users.

chinedufn commented 4 years ago

Thanks for the context - how does this look?

https://github.com/chinedufn/glyph-brush/commit/13fb2226510b4bd0684b00424b7996e685228fb2