linebender / parley

Rich text layout library
Apache License 2.0
158 stars 18 forks source link

Implement fonts not installed in the system #68

Closed NoodlesOfWrath closed 1 month ago

NoodlesOfWrath commented 1 month ago

When trying to create an app, it's useful to be able to use fonts directly from their files. Something like FontFamily::Local("../MyFont.tff"). Is there any way to do this currently? If not what will it take to implement? If you point me in the right direction I would be happy to help if this is planned. Thanks!

xorgy commented 1 month ago

Yes this is currently doable, this is a documentation issue (and possibly the API could be improved).

There is a way to do it with https://docs.rs/fontique/0.1.0/fontique/struct.Collection.html#method.register_fonts You load the file into a Vec<u8> (currently) and then pass it to this method on your Collection, and that makes it available in the Collection by the name it has in the provided file. This API is likely to be improved in the future, to be more friendly to zero-copy (not possible with Vec<u8>) and memory-mapped fonts; probably we will expose a variant of this which accepts a Path or File and memory maps it itself.

Feel free to let me know if you figure out what you want to do with it in your use case, and we can add an example that is similar to that in the future. :+ )

nicoburns commented 1 month ago

To add: fontique::Collection is a property of parley::FontContext

NoodlesOfWrath commented 1 month ago

Thanks! How much data does parley::FontContext and fontique::Collection hold? In the example, it is created new when rendering, but should it be handled differently in a larger renderer or is creating a new one everytime something is changed fine?

dfrg commented 1 month ago

Loading and categorizing fonts is fairly slow so you should create one Collection per application (or thread).