alexheretic / glyph-brush

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

Documentation is unclear; I cannot get the crate to work. #135

Closed KubaP closed 3 years ago

KubaP commented 3 years ago

I'll preface this by saying that I could be doing something wrong, but I've approached this just like any other crate; include it, build it, and then try to get an example working.

This is the point I've gotten stuck at, loading a font: line 65 of example.

I've written the following:

let typeface = FontRef::try_from_slice(include_bytes!(r#"C:\Users\Kuba\Desktop\FiraCode-Regular.ttf"#)).expect("a font");
let mut glyph_brush = GlyphBrushBuilder::using_font(typeface).build();

And I get the following error:

error[E0282]: type annotations needed for `GlyphBrush<V, X, glyph_brush::ab_glyph::FontRef<'_>>`
   --> src\main.rs:236:64
    |
236 |     let mut glyph_brush = GlyphBrushBuilder::using_font(typeface).build();
    |         ---------------                                           ^^^^^ cannot infer type for type parameter `V` declared on the associated function `build`
    |         |
    |         consider giving `glyph_brush` the explicit type `GlyphBrush<V, X, glyph_brush::ab_glyph::FontRef<'_>>`, where the type parameter `V` is specified

That example doesn't include any type annotations at all. I have looked through the docs, and for the life of me I cannot figure out the type annotation required. There are no examples aside from this one.

I've managed to get something to compile by using a FontArc instead of a FontRef, and type annotating GlyphBrush<GlyphVertex> but that isn't what the example is showing. What's going wrong?

alexheretic commented 3 years ago

Thanks for raising an issue. The problem here is the vertex type V cannot be inferred by the compiler in your usage. This type is generic to accommodate generating & caching any kind of vertex data.

This works in the examples because rustc can infer the vertex type from the process_queued call, as this includes a vertex generator function.

(This is similar to let mut vec = Vec::new(); not working until you add a vec.push(123_u32); line to inform rustc the generic type.)

GlyphBrush has 4 generic types, which is a lot. It's more complex than I'd like but it's grown this way so as to be a more flexible tool.

The most direct fix for you would to be provide the vertex & "extra" types to the builder that cannot be inferred.

type Vertex = (); // todo

let mut glyph_brush = GlyphBrushBuilder::using_font(typeface).build::<Vertex, Extra>();

It sounds like we can improve the documentation of these generic types, do you think this would help?

alexheretic commented 3 years ago

How about #136?

I've added documentation for each of the generic types & provided the above workaround in the previously sparse GlyphBrushBuilder::build docs.

KubaP commented 3 years ago

Yeah, that does clear a few things up. I didn't realise that the V generic is meant to be my own type; I thought it was something part of the crate. Regarding documentation, I think the example could be more clearly documented, because it's a fair mix of this crates apis, your own types, and some basic opengl abstractions, which gets a bit confusing to understand.

I'll further get back on this topic once I've gotten a basic example working; I'm using glow rather than gl-rs so I'm having to adapt some of the opengl stuff which is taking time.

alexheretic commented 3 years ago

That's fair. Having a low-level opengl example has proved useful and fairly easy to maintain, however there is a lot of complexity there as opengl is fundamentally quite complex. I'd be happy to accept doc improvements there or anywhere else.

KubaP commented 3 years ago

No, definitely. I'll submit some improvements at some point; once I've figured out how to translate that example into my own codebase/opengl abstraction and fully understood what's going on. Turns out font rendering is pretty complex huh.