-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add OpenType font variations #24088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OpenType font variations #24088
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| //! This example demonstrates how to use font variations to control variable font axes. | ||
|
|
||
| use bevy::prelude::*; | ||
| use bevy::text::{FontVariationTag, FontVariations}; | ||
|
|
||
| fn main() { | ||
| App::new() | ||
| .add_plugins(DefaultPlugins) | ||
| .add_systems(Startup, setup) | ||
| .run(); | ||
| } | ||
|
|
||
| fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
| let font: FontSource = asset_server.load("fonts/MonaSans-VariableFont.ttf").into(); | ||
|
|
||
| commands.spawn(Camera2d); | ||
|
|
||
| commands.spawn(( | ||
| Node { | ||
| flex_direction: FlexDirection::Column, | ||
| align_self: AlignSelf::Center, | ||
| justify_self: JustifySelf::Center, | ||
| align_items: AlignItems::Center, | ||
| ..default() | ||
| }, | ||
| children![ | ||
| ( | ||
| Text::new("Font Variations (wght axis)"), | ||
| TextFont { | ||
| font: font.clone(), | ||
| font_size: FontSize::Px(32.0), | ||
| ..default() | ||
| }, | ||
| Underline, | ||
| ), | ||
| ( | ||
| Node { | ||
| flex_direction: FlexDirection::Column, | ||
| padding: px(8.).all(), | ||
| row_gap: px(8.), | ||
| ..default() | ||
| }, | ||
| Children::spawn(SpawnIter( | ||
| [100, 200, 300, 400, 500, 600, 700, 800, 900] | ||
| .into_iter() | ||
| .map(move |weight| ( | ||
| Text(format!("wght {weight}")), | ||
| TextFont { | ||
| font: font.clone(), | ||
| font_size: FontSize::Px(32.0), | ||
| font_variations: FontVariations::builder() | ||
| .set(FontVariationTag::WEIGHT, weight as f32) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like a code smell that we should just be unifying the APIs here somehow.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can totally do that. The question is what would we call the unified struct and builder. I was thinking struct FontVariables {
features: Vec<...>,
variations: Vec<...>
}and a corresponding builder. However, this is less discoverable than
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not so sure about unifying the APIs. I agree it would be more user friendly, but parley maintains separate lists and it might complicate font inheritance and change detection. So personally I think it would be better to leave it like this for now.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be okay though, I'm not certain. |
||
| .build(), | ||
| ..default() | ||
| }, | ||
| )) | ||
| )), | ||
| ), | ||
| ], | ||
| )); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels a bit empty just to have the wght tag, it would be nice to have a few more here.
And the doc comment for weight could mention the values will be clamped to some subrange inside 1..1000.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise, no complaints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.