Skip to content
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 tag to change the font family #12

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ See `examples` for more usage patterns!
### Supported Tags

- `b`: \[b]**bold**\[/b] text
- `i`: \[i]*italic*\[/i] text
- `i`: \[i]_italic_\[/i] text
- `c`: \[c=\#ff0000]<span style="color: red">colored</span>\[/c] text
- Register named colors via `ResMut<ColorMap>` and use the names instead of hex values
- `m`: \[m=foo]text with marker component\[/m]
- Register marker components via `BbcodeSettings::with_marker` and use them to update text dynamically
- `font`: \[font="Fira Sans"]change the font family\[/font]

## License

Expand Down
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());

commands.spawn(BbcodeBundle::from_content(
"test [b]bold with [i]italic[/i][/b] and [c=#ff00ff]color[/c]",
r#"test [b]bold with [i]italic[/i][/b] and [c=#ff00ff]color[/c] and [font="JetBrains Mono"]different font[/font]"#,
// Use the "Fira Sans" font family with a default font size of 40
BbcodeSettings::new("Fira Sans", 40., Color::WHITE),
));
Expand Down
19 changes: 18 additions & 1 deletion src/bevy/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use super::{

#[derive(Debug, Clone)]
struct BbcodeContext {
/// The name of the font family to use for the text.
font_family: String,
/// Whether the text should be written **bold**.
is_bold: bool,
/// Whether the text should be written *italic*.
Expand Down Expand Up @@ -69,6 +71,17 @@ impl BbcodeContext {
self.clone()
}
}
"font" => {
if let Some(font_family) = tag.simple_param() {
Self {
font_family: font_family.clone(),
..self.clone()
}
} else {
warn!("Missing font family name on [{}] tag", tag.name());
self.clone()
}
}
_ => self.clone(),
}
}
Expand Down Expand Up @@ -103,6 +116,7 @@ pub fn convert_bbcode(
construct_recursively(
&mut entity_commands,
BbcodeContext {
font_family: settings.font_family.clone(),
is_bold: false,
is_italic: false,
color: settings.color.clone(),
Expand All @@ -128,7 +142,10 @@ fn construct_recursively(
match **node {
BbcodeNode::Text(ref text) => {
let font_query = fontdb::Query {
families: &[fontdb::Family::Name(&settings.font_family)],
families: &[
fontdb::Family::Name(&context.font_family),
fontdb::Family::Name(&settings.font_family),
],
weight: if context.is_bold {
fontdb::Weight::BOLD
} else {
Expand Down