Coder Social home page Coder Social logo

Comments (8)

fschutt avatar fschutt commented on August 23, 2024

The App struct itself should have the necessary methods - add_text_cached and add_font. This can be done before the app.run() function is called (i.e. when the program starts). You can use add_text_cached and add_font to get a TextId. I.e.:

let font_data = include_bytes!("Roboto.ttf");
let font_id = FontId::External("Roboto".into());

let mut app = App::new(MyDataModel { text: None }, AppConfig::default());
app.add_font(font_data, font_id.clone());
// Calculates and caches the text layout - need to provide the font, 
// the font size and your text (+ optional letter spacing)
app.add_text_cached("My very long text", font_id, FontSize::px(10.0), None);
// Now run (calls the layout() function for the first time)
app.run(Window::new(WindowCreateOptions::default()).unwrap()).unwrap();

Be sure to delete the font at some point, it doesn't delete itself (except when the run() function ends). Hope that answers your question. Note: You must use the same values in the CSS ("10px Roboto"), otherwise, azul will re-calculate the layout on-the-fly, which could be bad for performance.

from azul.

OptimisticPeach avatar OptimisticPeach commented on August 23, 2024

Wait, so I have to both specify the size and font in the css as well?

from azul.

fschutt avatar fschutt commented on August 23, 2024

Well, you don't have to specify it, as it will fall back to the default sans-serif font. Plus, the default fonts (sans-serif, serif, cursive, etc.) are always loaded. You can of course do this:

let font_id = FontId::Builtin("sans-serif".into());

let mut app = App::new(MyDataModel { text: None }, AppConfig::default());
let text_id = app.add_text_cached("My very long text", font_id, FontSize::px(10.0), None);
app.app_state.data.modify(|state| state.text = Some(text_id));
app.run(Window::new(WindowCreateOptions::default()).unwrap()).unwrap();

If you don't specify a size, it will fallback to the default font size, which is 10px. If you don't specify a font in the CSS, it will fall back to the default font, which is sans-serif. If you want to use a custom font for everything, put * { font-family: "Roboto" } somewhere in your CSS.

FontId::Builtin is for fonts you load from the users computer, FontId::External is for fonts that you have added manually (allows for a custom font identifier).

from azul.

OptimisticPeach avatar OptimisticPeach commented on August 23, 2024

Ah, okay, so if i understand correctly:

  • A FontId refers to a cached version of text with a certain font and size,
  • If I set it as a different font (Or size) to that in the FontId, then it is recalculated on the fly,
  • If I don't specify it in either place, then I end up with the default font
  • And, if I only specify it on the rust end, then I get the font and size specified in the cached Text referred to by the FontId

from azul.

fschutt avatar fschutt commented on August 23, 2024

Yes, but I think you meant a TextId, not a FontId - yes, a TextId corresponds to a cached version of the text with font + size (essentially the cached glyph positions on the page - if the font would be scaled, the glyph positions need to be recalculated). A FontId is just a string that corresponds to a parsed-and-loaded font file (usually .ttf or .otf).

To (4) - yes, unless the font was removed (via delete_font) in the meantime - i.e. in the time where the TextId was created and the layout() function was called, then it will (I think) either fall back to the default font or just not render the text.

Right now the support for automatically adding / removing / loading / unloading fonts is a bit flaky. On webpages you can just load and unload fonts and images and the JS garbage collector takes care of deleting them, but here you have to watch out that you don't accidentally add fonts and never remove them. This is a bit tricky and I hope to improve that API in the future.

from azul.

OptimisticPeach avatar OptimisticPeach commented on August 23, 2024

Yes, I did mean TextId, thanks! This is very important for the document loader I'm writing.

from azul.

OptimisticPeach avatar OptimisticPeach commented on August 23, 2024

Hello again, how would one set the color of the text? I have tried setting the color property in the css to no avail. I'm getting no text output, so I wanted to be sure that I can see the text before I delve into debugging.

from azul.

OptimisticPeach avatar OptimisticPeach commented on August 23, 2024

Ah, my bad, it was the color property.

from azul.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.