-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Cosmic text #10193
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
Merged
Merged
Cosmic text #10193
Changes from all 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 d13442f
update cosmic-text to 0.10.0
TotalKrill b58d3c7
Fix compilation issues when rebasing
TotalKrill cc5aceb
Fix issue where font that was not loaded yet would crash the program
TotalKrill 60d59b6
remove unused module and some unused imports
TotalKrill 8941eb4
enable multi-size font by patching latest main from cosmic-text
TotalKrill 85b90e2
Rebase fixes and cosmic-text api changes when using main git branch a…
TotalKrill 25b9634
Clear up some code that have been implemented in Cosmic-Text
TotalKrill cde816f
Some cleanup
TotalKrill c68736d
Use cosmic-texts ttf-parser version to verify the font
TotalKrill 13f0740
Remove unused
TotalKrill e8144af
--amend
TotalKrill 6ac2155
Remove Mutex from Buffer
nicoburns b8fcd5e
Merge pull request #3 from nicoburns/cosmic-text-no-buffer-mutex
TotalKrill e7f7149
rebase - add persistent buffers
bytemunch 3bdda24
fix text scaling
Dimchikkk c4ffaf8
Merge pull request #5 from Dimchikkk/cosmic-text
TotalKrill b963d83
Merge branch 'bevyengine:main' into cosmic-text
TotalKrill 7f03e2c
remove `Option` wrapper for buffer in query
bytemunch 6ea20ff
remove `create_buffer` in favor of `update_buffer`
bytemunch de80986
fix feature gate
bytemunch 2deeb4c
holy scripture to appease the Clippy
tigregalis ed658c9
Apply suggestions from code review
TotalKrill 14621b5
Apply suggestions from code review
TotalKrill 38f75e3
Move not to fonts
TotalKrill ec9d33e
Merge branch 'cosmic-text-docs' into cosmic-text
TotalKrill ac33b83
remove unused and fix comment
TotalKrill 1995716
Merge branch 'cosmic-text-persistent-buffer' into cosmic-text
TotalKrill 064550a
use the different font sizes to get the metrics for the buffer
TotalKrill 83e1184
remove todo: we can use metadata
tigregalis 9795a3b
remove mutex on font system
tigregalis 5f0cd22
remove TextError::FailedToAcquireMutex
tigregalis 5edb96c
Merge pull request #8 from tigregalis/ct-metadata-ok
TotalKrill e3731d2
Merge pull request #9 from tigregalis/ct-no-mutex
TotalKrill eb4305c
remove Patch, use crates.io release of cosmic-text
TotalKrill 4cb8a86
Fix borrow checking issues around family names
nicoburns 4c3dbcc
Merge pull request #10 from nicoburns/cosmic-text-family-names
TotalKrill b361099
default textstyle 24, and taplo fmt fix
TotalKrill 43afc67
Fix `ui_scaling` example failing to compile
rparrett a70287c
Merge pull request #11 from rparrett/cosmic-text-ui-scaling
TotalKrill d259f38
fix failing tests
TotalKrill 9317827
in clippy we trust
TotalKrill c0d7363
Change default to stay the same as before
TotalKrill 0a5f0c5
Fix enumeration and add comment as to why it is there
TotalKrill 81c1535
round line_y
Dimchikkk f3c1938
Fix merge artifact undoing #13717
rparrett 1e29a56
Merge pull request #13 from rparrett/cosmic-text-merge-artifacts
TotalKrill e2d9c1e
Merge pull request #12 from Dimchikkk/ctf
TotalKrill 89b912e
Cleanup resolved TODO comments
rparrett e8b3d1c
Merge pull request #14 from rparrett/cosmic-text-cleanup-todo
TotalKrill 435917e
fix doc comment
tigregalis 3463d18
Use cosmic-text text alignment
tigregalis 792d9d0
Update example
tigregalis 3b743c3
Merge pull request #16 from tigregalis/ct-text-alignment
TotalKrill 6e0982e
Merge pull request #15 from tigregalis/ct-fix-doc-comment
TotalKrill bbcff90
Add "word or character" wrapping
rparrett 78ae9d6
use Text2dBounds as bounds instead of Vec2
tigregalis 60098d4
rename Text2dBounds to TextBounds
tigregalis fcaa22d
move TextBounds out of text2d
tigregalis addcd0a
rename in doc comment
tigregalis f0e18a3
Merge pull request #18 from tigregalis/ct-text-bounds
TotalKrill a62d6aa
Merge pull request #17 from rparrett/cosmic-text-wrap
TotalKrill d5df246
Merge branch 'main' into cosmic-text
TotalKrill 17e3e64
Merge branch 'main' into cosmic-text
TotalKrill de4eb09
fix lint
TotalKrill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| use bevy_ecs::{component::Component, reflect::ReflectComponent}; | ||
| use bevy_math::Vec2; | ||
| use bevy_reflect::Reflect; | ||
|
|
||
| /// The maximum width and height of text. The text will wrap according to the specified size. | ||
| /// Characters out of the bounds after wrapping will be truncated. Text is aligned according to the | ||
| /// specified [`JustifyText`](crate::text::JustifyText). | ||
| /// | ||
| /// Note: only characters that are completely out of the bounds will be truncated, so this is not a | ||
|
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 know this comment is from the old
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. Non-blocking |
||
| /// reliable limit if it is necessary to contain the text strictly in the bounds. Currently this | ||
| /// component is mainly useful for text wrapping only. | ||
| #[derive(Component, Copy, Clone, Debug, Reflect)] | ||
| #[reflect(Component)] | ||
| pub struct TextBounds { | ||
| /// The maximum width of text in logical pixels. | ||
| /// If `None`, the width is unbounded. | ||
| pub width: Option<f32>, | ||
| /// The maximum height of text in logical pixels. | ||
| /// If `None`, the height is unbounded. | ||
| pub height: Option<f32>, | ||
| } | ||
|
|
||
| impl Default for TextBounds { | ||
| #[inline] | ||
| fn default() -> Self { | ||
| Self::UNBOUNDED | ||
| } | ||
| } | ||
|
|
||
| impl TextBounds { | ||
| /// Unbounded text will not be truncated or wrapped. | ||
| pub const UNBOUNDED: Self = Self { | ||
| width: None, | ||
| height: None, | ||
| }; | ||
|
|
||
| /// Creates a new `TextBounds`, bounded with the specified width and height values. | ||
| #[inline] | ||
| pub const fn new(width: f32, height: f32) -> Self { | ||
| Self { | ||
| width: Some(width), | ||
| height: Some(height), | ||
| } | ||
| } | ||
|
|
||
| /// Creates a new `TextBounds`, bounded with the specified width value and unbounded on height. | ||
| #[inline] | ||
| pub const fn new_horizontal(width: f32) -> Self { | ||
| Self { | ||
| width: Some(width), | ||
| height: None, | ||
| } | ||
| } | ||
|
|
||
| /// Creates a new `TextBounds`, bounded with the specified height value and unbounded on width. | ||
| #[inline] | ||
| pub const fn new_vertical(height: f32) -> Self { | ||
| Self { | ||
| width: None, | ||
| height: Some(height), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<Vec2> for TextBounds { | ||
| #[inline] | ||
| fn from(v: Vec2) -> Self { | ||
| Self::new(v.x, v.y) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,35 @@ | ||
| 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 | ||
| pub fn try_from_bytes( | ||
| font_data: Vec<u8>, | ||
| ) -> Result<Self, cosmic_text::ttf_parser::FaceParsingError> { | ||
| use cosmic_text::ttf_parser; | ||
| ttf_parser::Face::parse(&font_data, 0)?; | ||
| Ok(Self { | ||
| data: Arc::new(font_data), | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.