| Crates.io | bevy_pxtxt |
| lib.rs | bevy_pxtxt |
| version | 0.2.0 |
| created_at | 2024-07-04 20:57:50.063818+00 |
| updated_at | 2024-07-22 23:23:33.38595+00 |
| description | Create fonts and render text in bevy based on a bitmap source image. |
| homepage | |
| repository | https://github.com/Icni/bevy_pxtxt |
| max_upload_size | |
| id | 1292108 |
| size | 350,169 |
Call it what you want — "pixel font," "image font," "texture font," etc. Either way, this library has it. bevy_pxtxt allows users to create fonts and render text based on images they draw themself.
The images by default are .png files, but .bmp, .jpeg, .gif, or .tiff can be used with their corresponding feature flags.
assets/moonshock.ron
(
name: "Moonshock",
image: "moonshock.png",
glyph_width: Varied (
max: 5,
min: 1,
),
spacing: 1,
ascender: 7,
descender: 2,
)
examples/sections.rs
use bevy::prelude::*;
use bevy_asset_loader::asset_collection::{AssetCollection, AssetCollectionApp};
use bevy_pxtxt::{plugin::PxtxtPlugin, pxfont::PxFont, pxtext::{PxText, PxTextBundle, PxTextSection}};
#[derive(AssetCollection, Resource)]
struct PxFontCollection {
#[asset(path = "moonshock.ron")]
pub moonshock: Handle<PxFont>,
}
fn main() {
App::new()
.add_plugins((DefaultPlugins, PxtxtPlugin::default()))
.init_collection::<PxFontCollection>()
.insert_resource(Msaa::Off)
.add_systems(Startup, setup)
.run();
}
fn setup(
fonts: Res<PxFontCollection>,
mut commands: Commands,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(PxTextBundle {
text: PxText::from_sections(
vec![
PxTextSection::new("Here's a ")
.with_color(Color::WHITE),
PxTextSection::new("c")
.with_color(Color::hsl(0., 0.9, 0.7)).underlined(),
PxTextSection::new("o")
.with_color(Color::hsl(70., 0.9, 0.7)).underlined(),
PxTextSection::new("l")
.with_color(Color::hsl(170., 0.9, 0.7)).underlined(),
PxTextSection::new("o")
.with_color(Color::hsl(220., 0.9, 0.7)).underlined(),
PxTextSection::new("r")
.with_color(Color::hsl(280., 0.9, 0.7)).underlined(),
PxTextSection::new("ful example of some pixel text.\n\n")
.with_color(Color::WHITE),
PxTextSection::new("(Using sections for different colors)")
.with_color(Color::GRAY),
], fonts.moonshock.clone()
).with_line_spacing(5),
transform: Transform::from_scale(Vec3::splat(4.0)),
..Default::default()
});
}
Result:

PxFontData FilesFonts are loaded onto PxFonts, which uses PxFontData for deserialization. This is the struct that the .ron files write.
Required Fields:
name - the name of the fontimage - the path to the image of the font, relative to the assets folderglyph_width - width of each character
Varied - automatically detect the width of characters between a max and min valueMonospace - all characters are the same widthascender - number of pixels above the base linedescender - number of pixels below the base lineOptional Fields:
char_layout - what characters are displayed on the image
StartingAt - the characters start at the given character and continue in order of ascending unicode valueRanges - the characters are given as a series of rangesListed - the characters are individually listedStartingAt(' ')spacing - space between each character within a word
1padding - padding between characters in the image
(0, 0)Sections with can receive events for left clicks, right clicks, and hovering. PickableText senses these events, when the child of an entity with PxText.
To receive these events, use EventReader<PxTextEvent> (read up on events).
See examples/bounded.rs for how bounding boxes can be used.
See examples/picking.rs for how input can be used.
See examples/sections.rs (above as well) for how different sections can be used.
Bevy Pxtxt version 0.1 is comptabile with Bevy version 0.13.
This library, including the examples and assets, is licensed under the MIT License.