Comments (8)
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.
Wait, so I have to both specify the size and font in the css as well?
from azul.
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.
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.
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.
Yes, I did mean TextId, thanks! This is very important for the document loader I'm writing.
from azul.
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.
Ah, my bad, it was the color
property.
from azul.
Related Issues (20)
- If would like to split out text shaping what is the plan? HOT 3
- Cargo run error, could nto find IOReader in bincode
- couldn't read D:\soft\cargo/lib/azul-dll-0.0.1/target/release/azul.dll HOT 9
- Cannot compile Azul on M1 MacBook Pro due to older WInit dependency HOT 1
- License not updated on official website HOT 2
- Links broken on azul.rs HOT 6
- Build from crates.io command doesn't work HOT 1
- What's the deal with `extern "C"`? HOT 1
- Timers, Threads and Animations guide and after grade abnormal display HOT 1
- Error from link.exe HOT 1
- Support for more programming languages other than python? HOT 2
- [Suggestion] Game example?
- It is not clear from the README whether azul supports Rust & C, Rust & C++ or Rust & C & C++ HOT 2
- [BUG] 404 error for azul.dll file HOT 3
- get rid of four year old xcb dependency HOT 1
- C header fails to parse with clang
- text_layout word_positions_to_inline_text_layout seems to loose words HOT 2
- azul.pyd not working on win7 and win11 HOT 1
- azul didn't create a window HOT 3
- Cannot compile `azul-text-layout` due using old version of freetype
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azul.