Skip to content

Commit

Permalink
Add tag to change the font family (#12)
Browse files Browse the repository at this point in the history
* Add font tag to change the font family

* Update README with font tag

* Expand static example with font family change
  • Loading branch information
TimJentzsch authored Jul 28, 2024
1 parent c8f4e06 commit 5b37e87
Show file tree
Hide file tree
Showing 53 changed files with 21 additions and 3 deletions.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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

0 comments on commit 5b37e87

Please sign in to comment.