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

Cosmic text #10193

Merged
merged 65 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
9b60187
Copied each affected file from cosmic-text branch, to fix some weird …
TotalKrill Oct 20, 2023
d13442f
update cosmic-text to 0.10.0
TotalKrill Oct 23, 2023
b58d3c7
Fix compilation issues when rebasing
TotalKrill Jun 7, 2024
cc5aceb
Fix issue where font that was not loaded yet would crash the program
TotalKrill Jun 7, 2024
60d59b6
remove unused module and some unused imports
TotalKrill Jun 7, 2024
8941eb4
enable multi-size font by patching latest main from cosmic-text
TotalKrill Jun 7, 2024
85b90e2
Rebase fixes and cosmic-text api changes when using main git branch a…
TotalKrill Jun 14, 2024
25b9634
Clear up some code that have been implemented in Cosmic-Text
TotalKrill Jun 14, 2024
cde816f
Some cleanup
TotalKrill Jun 15, 2024
c68736d
Use cosmic-texts ttf-parser version to verify the font
TotalKrill Jun 15, 2024
13f0740
Remove unused
TotalKrill Jun 15, 2024
e8144af
--amend
TotalKrill Jun 16, 2024
6ac2155
Remove Mutex from Buffer
nicoburns Jun 17, 2024
b8fcd5e
Merge pull request #3 from nicoburns/cosmic-text-no-buffer-mutex
TotalKrill Jun 17, 2024
e7f7149
rebase - add persistent buffers
bytemunch Jun 17, 2024
3bdda24
fix text scaling
Dimchikkk Jun 17, 2024
c4ffaf8
Merge pull request #5 from Dimchikkk/cosmic-text
TotalKrill Jun 17, 2024
b963d83
Merge branch 'bevyengine:main' into cosmic-text
TotalKrill Jun 17, 2024
7f03e2c
remove `Option` wrapper for buffer in query
bytemunch Jun 17, 2024
6ea20ff
remove `create_buffer` in favor of `update_buffer`
bytemunch Jun 17, 2024
de80986
fix feature gate
bytemunch Jun 17, 2024
2deeb4c
holy scripture to appease the Clippy
tigregalis Jun 17, 2024
ed658c9
Apply suggestions from code review
TotalKrill Jun 17, 2024
14621b5
Apply suggestions from code review
TotalKrill Jun 17, 2024
38f75e3
Move not to fonts
TotalKrill Jun 17, 2024
ec9d33e
Merge branch 'cosmic-text-docs' into cosmic-text
TotalKrill Jun 17, 2024
ac33b83
remove unused and fix comment
TotalKrill Jun 17, 2024
1995716
Merge branch 'cosmic-text-persistent-buffer' into cosmic-text
TotalKrill Jun 17, 2024
064550a
use the different font sizes to get the metrics for the buffer
TotalKrill Jun 17, 2024
83e1184
remove todo: we can use metadata
tigregalis Jun 18, 2024
9795a3b
remove mutex on font system
tigregalis Jun 18, 2024
5f0cd22
remove TextError::FailedToAcquireMutex
tigregalis Jun 18, 2024
5edb96c
Merge pull request #8 from tigregalis/ct-metadata-ok
TotalKrill Jun 18, 2024
e3731d2
Merge pull request #9 from tigregalis/ct-no-mutex
TotalKrill Jun 18, 2024
eb4305c
remove Patch, use crates.io release of cosmic-text
TotalKrill Jun 18, 2024
4cb8a86
Fix borrow checking issues around family names
nicoburns Jun 19, 2024
4c3dbcc
Merge pull request #10 from nicoburns/cosmic-text-family-names
TotalKrill Jun 19, 2024
b361099
default textstyle 24, and taplo fmt fix
TotalKrill Jun 19, 2024
43afc67
Fix `ui_scaling` example failing to compile
rparrett Jun 19, 2024
a70287c
Merge pull request #11 from rparrett/cosmic-text-ui-scaling
TotalKrill Jun 19, 2024
d259f38
fix failing tests
TotalKrill Jun 19, 2024
9317827
in clippy we trust
TotalKrill Jun 19, 2024
c0d7363
Change default to stay the same as before
TotalKrill Jun 19, 2024
0a5f0c5
Fix enumeration and add comment as to why it is there
TotalKrill Jun 19, 2024
81c1535
round line_y
Dimchikkk Jun 20, 2024
f3c1938
Fix merge artifact undoing #13717
rparrett Jun 20, 2024
1e29a56
Merge pull request #13 from rparrett/cosmic-text-merge-artifacts
TotalKrill Jun 20, 2024
e2d9c1e
Merge pull request #12 from Dimchikkk/ctf
TotalKrill Jun 20, 2024
89b912e
Cleanup resolved TODO comments
rparrett Jun 23, 2024
e8b3d1c
Merge pull request #14 from rparrett/cosmic-text-cleanup-todo
TotalKrill Jun 23, 2024
435917e
fix doc comment
tigregalis Jun 23, 2024
3463d18
Use cosmic-text text alignment
tigregalis Jun 23, 2024
792d9d0
Update example
tigregalis Jun 23, 2024
3b743c3
Merge pull request #16 from tigregalis/ct-text-alignment
TotalKrill Jun 23, 2024
6e0982e
Merge pull request #15 from tigregalis/ct-fix-doc-comment
TotalKrill Jun 23, 2024
bbcff90
Add "word or character" wrapping
rparrett Jun 23, 2024
78ae9d6
use Text2dBounds as bounds instead of Vec2
tigregalis Jun 24, 2024
60098d4
rename Text2dBounds to TextBounds
tigregalis Jun 24, 2024
fcaa22d
move TextBounds out of text2d
tigregalis Jun 24, 2024
addcd0a
rename in doc comment
tigregalis Jun 24, 2024
f0e18a3
Merge pull request #18 from tigregalis/ct-text-bounds
TotalKrill Jun 24, 2024
a62d6aa
Merge pull request #17 from rparrett/cosmic-text-wrap
TotalKrill Jun 26, 2024
d5df246
Merge branch 'main' into cosmic-text
TotalKrill Jun 26, 2024
17e3e64
Merge branch 'main' into cosmic-text
TotalKrill Jul 4, 2024
de4eb09
fix lint
TotalKrill Jul 4, 2024
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: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,6 @@ wayland = ["bevy_internal/wayland"]
# X11 display server support
x11 = ["bevy_internal/x11"]

# Enable rendering of font glyphs using subpixel accuracy
subpixel_glyph_atlas = ["bevy_internal/subpixel_glyph_atlas"]

# Enable systems that allow for automated testing on CI
bevy_ci_testing = ["bevy_internal/bevy_ci_testing"]

Expand Down
3 changes: 0 additions & 3 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ async-io = ["bevy_tasks/async-io"]
wayland = ["bevy_winit/wayland"]
x11 = ["bevy_winit/x11"]

# enable rendering of font glyphs using subpixel accuracy
subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"]

# Transmission textures in `StandardMaterial`:
pbr_transmission_textures = [
"bevy_pbr?/pbr_transmission_textures",
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,15 @@ pub fn extract_sprites(
.map(|e| (commands.spawn_empty().id(), e)),
);
} else {
let atlas_rect = sheet.and_then(|s| s.texture_rect(&texture_atlases));
let atlas_rect =
sheet.and_then(|s| s.texture_rect(&texture_atlases).map(|r| r.as_rect()));
let rect = match (atlas_rect, sprite.rect) {
(None, None) => None,
(None, Some(sprite_rect)) => Some(sprite_rect),
(Some(atlas_rect), None) => Some(atlas_rect.as_rect()),
(Some(atlas_rect), None) => Some(atlas_rect),
(Some(atlas_rect), Some(mut sprite_rect)) => {
sprite_rect.min += atlas_rect.min.as_vec2();
sprite_rect.max += atlas_rect.min.as_vec2();
sprite_rect.min += atlas_rect.min;
TotalKrill marked this conversation as resolved.
Show resolved Hide resolved
sprite_rect.max += atlas_rect.min;

Some(sprite_rect)
}
Expand Down
40 changes: 20 additions & 20 deletions crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,6 @@ pub struct TextureAtlasLayout {
pub(crate) texture_handles: Option<HashMap<AssetId<Image>, usize>>,
}

/// Component used to draw a specific section of a texture.
///
/// It stores a handle to [`TextureAtlasLayout`] and the index of the current section of the atlas.
/// The texture atlas contains various *sections* of a given texture, allowing users to have a single
/// image file for either sprite animation or global mapping.
/// You can change the texture [`index`](Self::index) of the atlas to animate the sprite or display only a *section* of the texture
/// for efficient rendering of related game objects.
///
/// Check the following examples for usage:
/// - [`animated sprite sheet example`](https://github.com/bevyengine/bevy/blob/latest/examples/2d/sprite_sheet.rs)
/// - [`sprite animation event example`](https://github.com/bevyengine/bevy/blob/latest/examples/2d/sprite_animation.rs)
/// - [`texture atlas example`](https://github.com/bevyengine/bevy/blob/latest/examples/2d/texture_atlas.rs)
#[derive(Component, Default, Debug, Clone, Reflect)]
pub struct TextureAtlas {
/// Texture atlas layout handle
pub layout: Handle<TextureAtlasLayout>,
/// Texture atlas section index
pub index: usize,
}

impl TextureAtlasLayout {
/// Create a new empty layout with custom `dimensions`
pub fn new_empty(dimensions: UVec2) -> Self {
Expand Down Expand Up @@ -149,6 +129,26 @@ impl TextureAtlasLayout {
}
}

/// Component used to draw a specific section of a texture.
TotalKrill marked this conversation as resolved.
Show resolved Hide resolved
///
/// It stores a handle to [`TextureAtlasLayout`] and the index of the current section of the atlas.
/// The texture atlas contains various *sections* of a given texture, allowing users to have a single
/// image file for either sprite animation or global mapping.
/// You can change the texture [`index`](Self::index) of the atlas to animate the sprite or display only a *section* of the texture
/// for efficient rendering of related game objects.
///
/// Check the following examples for usage:
/// - [`animated sprite sheet example`](https://github.com/bevyengine/bevy/blob/latest/examples/2d/sprite_sheet.rs)
/// - [`sprite animation event example`](https://github.com/bevyengine/bevy/blob/latest/examples/2d/sprite_animation.rs)
/// - [`texture atlas example`](https://github.com/bevyengine/bevy/blob/latest/examples/2d/texture_atlas.rs)
#[derive(Component, Default, Debug, Clone, Reflect)]
pub struct TextureAtlas {
/// Texture atlas layout handle
pub layout: Handle<TextureAtlasLayout>,
/// Texture atlas section index
pub index: usize,
}

impl TextureAtlas {
/// Retrieves the current texture [`URect`] of the sprite sheet according to the section `index`
pub fn texture_rect(&self, texture_atlases: &Assets<TextureAtlasLayout>) -> Option<URect> {
Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
subpixel_glyph_atlas = []
default_font = []

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.14.0-dev" }
bevy_asset = { path = "../bevy_asset", version = "0.14.0-dev" }
bevy_color = { path = "../bevy_color", version = "0.14.0-dev" }
bevy_derive = { path = "../bevy_derive", version = "0.14.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev" }
bevy_math = { path = "../bevy_math", version = "0.14.0-dev" }
bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
Expand All @@ -29,10 +29,11 @@ bevy_window = { path = "../bevy_window", version = "0.14.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.14.0-dev" }

# other
ab_glyph = "0.2.6"
glyph_brush_layout = "0.2.1"
cosmic-text = "0.12"
thiserror = "1.0"
serde = { version = "1", features = ["derive"] }
unicode-bidi = "0.3.13"
sys-locale = "0.3.0"

[dev-dependencies]
approx = "0.5.1"
Expand Down
11 changes: 9 additions & 2 deletions crates/bevy_text/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use ab_glyph::GlyphId;
use cosmic_text::CacheKey;
use thiserror::Error;

#[derive(Debug, PartialEq, Eq, Error)]
/// Errors related to the textsystem
pub enum TextError {
/// Font was not found, this could be that the font has not yet been loaded, or
/// that the font failed to load for some other reason
#[error("font not found")]
NoSuchFont,
/// Failed to add glyph to a newly created atlas for some reason
#[error("failed to add glyph to newly-created atlas {0:?}")]
FailedToAddGlyph(GlyphId),
FailedToAddGlyph(u16),
/// Failed to get scaled glyph image for cache key
#[error("failed to get scaled glyph image for cache key: {0:?}")]
FailedToGetGlyphImage(CacheKey),
}
71 changes: 27 additions & 44 deletions crates/bevy_text/src/font.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
use ab_glyph::{FontArc, FontVec, InvalidFont, OutlinedGlyph};
use std::sync::Arc;

use bevy_asset::Asset;
use bevy_reflect::TypePath;
use bevy_render::{
render_asset::RenderAssetUsages,
render_resource::{Extent3d, TextureDimension, TextureFormat},
texture::Image,
};

#[derive(Asset, TypePath, Debug, Clone)]
/// An [`Asset`] that contains the data for a loaded font, if loaded as an asset.
///
/// Loaded by [`FontLoader`](crate::FontLoader).
///
/// # A note on fonts
///
/// `Font` may differ from the everyday notion of what a "font" is.
/// A font *face* (e.g. Fira Sans Semibold Italic) is part of a font *family* (e.g. Fira Sans),
/// and is distinguished from other font faces in the same family
/// by its style (e.g. italic), its weight (e.g. bold) and its stretch (e.g. condensed).
///
/// Bevy currently loads a single font face as a single `Font` asset.
#[derive(Debug, TypePath, Clone, Asset)]
pub struct Font {
pub font: FontArc,
/// Content of a font file as bytes
pub data: Arc<Vec<u8>>,
}

impl Font {
pub fn try_from_bytes(font_data: Vec<u8>) -> Result<Self, InvalidFont> {
let font = FontVec::try_from_vec(font_data)?;
let font = FontArc::new(font);
Ok(Font { font })
}

pub fn get_outlined_glyph_texture(outlined_glyph: OutlinedGlyph) -> Image {
let bounds = outlined_glyph.px_bounds();
// Increase the length of the glyph texture by 2-pixels on each axis to make space
// for a pixel wide transparent border along its edges.
let width = bounds.width() as usize + 2;
let height = bounds.height() as usize + 2;
let mut alpha = vec![0.0; width * height];
outlined_glyph.draw(|x, y, v| {
// Displace the glyph by 1 pixel on each axis so that it is drawn in the center of the texture.
// This leaves a pixel wide transparent border around the glyph.
alpha[(y + 1) as usize * width + x as usize + 1] = v;
});

// TODO: make this texture grayscale
Image::new(
Extent3d {
width: width as u32,
height: height as u32,
depth_or_array_layers: 1,
},
TextureDimension::D2,
alpha
.iter()
.flat_map(|a| vec![255, 255, 255, (*a * 255.0) as u8])
.collect::<Vec<u8>>(),
TextureFormat::Rgba8UnormSrgb,
// This glyph image never needs to reach the render world because it's placed
// into a font texture atlas that'll be used for rendering.
RenderAssetUsages::MAIN_WORLD,
)
/// Creates a [Font] from bytes, without any validation of the content
pub fn try_from_bytes(
font_data: Vec<u8>,
) -> Result<Self, cosmic_text::ttf_parser::FaceParsingError> {
// TODO: validate font, restore `try_from_bytes`
use cosmic_text::ttf_parser;
ttf_parser::Face::parse(&font_data, 0)?;
Ok(Self {
data: Arc::new(font_data),
})
}
}
Loading