-
Notifications
You must be signed in to change notification settings - Fork 141
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 cosmic text example #739
base: main
Are you sure you want to change the base?
Add cosmic text example #739
Conversation
Thanks, it's really good to see this. Would you consider making this a test scene (i.e. in the I think that would be useful because this new example has a lot of interleaving of logic between cosmic text and Vello and winit. If not, I think we can still land this nearly as-is, although it being another usage of winit is likely to cause us pain in future when we need to perform updates. |
Sure, I will do that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good and runs as expected. Thanks for pulling this together
The exact integration with scenes could be cleaner, given that there's only one scene; if my guidance isn't clear enough, let me know and I can apply it myself.
|
||
for (font_id, index) in font_faces { | ||
if let Some(font) = font_system.get_font(font_id) { | ||
let resource = Arc::new(font.data().to_vec()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two allocations here, one to put the data in the vec, and one to allocate the Arc
. The first could be avoided by creating an adapter which implements AsRef<[u8]>
by containing an Arc<Font>
and using the data parameter. The second would use https://docs.rs/bytemuck/latest/bytemuck/allocation/trait.TransparentWrapperAlloc.html#method.wrap_arc, and is definitely out-of-scope.
The best resolution to this would probably be to have a comment: "These allocations are avoidable, but used for simplicity.", maybe with a link to this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in time for this patch, but improving this is also a design goal for splitting these resource handles out of Peniko. (linebender/peniko#68)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have used an adapter and added a link to this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A wrapper/adapter is fine, but we should probably just submit a PR to cosmic-text to make Font
implement AsRef<[u8]>
. Alternatively, the correct type can be obtained using unsafe { font_system.db().make_shared_face_data(font_id).unwrap() }
.
examples/scenes/src/lib.rs
Outdated
@@ -34,6 +40,8 @@ pub struct SceneParams<'a> { | |||
pub resolution: Option<Vec2>, | |||
pub base_color: Option<vello::peniko::Color>, | |||
pub complexity: usize, | |||
#[cfg(feature = "cosmic_text")] | |||
pub cosmic_text_scene_state: &'a CosmicTextSceneState, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is underdocumented, so I can understand this being unclear, but test scenes can have state.
The main result of this is that that we can make this feature gate be "localised", by making the cosmic_text_scene::text_scene
function return an impl TestScene
, which will be retained.
For an example, see the MMark
scene.
This does lead to some things which are unidiomatic, such as not sharing the fonts between the scenes, but I think for this peripheral functionality, keeping it as localised as possible is better than improving the efficiency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
examples/scenes/src/test_scenes.rs
Outdated
@@ -53,6 +61,7 @@ macro_rules! scene { | |||
} | |||
|
|||
export_scenes!( | |||
#[cfg(feature = "cosmic_text")] cosmic_text_scene(cosmic_text), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can use:
#[cfg(feature = "cosmic_text")] cosmic_text_scene(cosmic_text), | |
#[cfg(feature = "cosmic_text")] cosmic_text_scene(crate::cosmic_text_scene::cosmic_text(), "cosmic text", false), |
examples/scenes/src/test_scenes.rs
Outdated
@@ -53,6 +61,7 @@ macro_rules! scene { | |||
} | |||
|
|||
export_scenes!( | |||
#[cfg(feature = "cosmic_text")] cosmic_text_scene(cosmic_text), | |||
splash_with_tiger(impls::splash_with_tiger(), "splash_with_tiger", false), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The splash screen should still be the first screen, to show the controls to anyone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -0,0 +1,425 @@ | |||
// Copyright 2024 the Vello Authors | |||
// SPDX-License-Identifier: Apache-2.0 OR MIT | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A tiny bit of documentation would be good:
//! An example integrating Cosmic text with Vello. | |
//! This renders a read-only snapshot of a simplified text editor using system fonts. | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
buffer_glyphs | ||
} | ||
|
||
// Copied directly from cosmic_text. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a link to the source?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Thanks for the feedback. I will work on this tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What needs to happen to get this out of draft? It's looking quite good
} | ||
} | ||
|
||
pub struct CosmicTextSceneState { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"State" is rarely a meaningful suffix; I think this would be clearer if it were removed.
} | ||
} | ||
|
||
let text = "おはよう (ja) (ohayō) 🌅✨ (morning), こんにちは (ja) (konnichi wa) ☀️😊 (daytime), こんばんは (ja) (konban wa) 🌙🌟 (evening)"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future reviewers:
These translations do also appear to be correct.
examples/headless/Cargo.toml
Outdated
@@ -20,3 +20,6 @@ pollster = { workspace = true } | |||
env_logger = "0.11.5" | |||
png = "0.17.14" | |||
futures-intrusive = { workspace = true } | |||
|
|||
[features] | |||
cosmic_text = ["scenes/cosmic_text"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our style is to have a final newline at the end of all files.
(This also applies to the other files where you've chosen to omit this.)
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
pub struct CosmicData {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be able to also document that this exists at the top level of the repository, in https://github.com/linebender/vello?tab=readme-ov-file#integrations
} | ||
|
||
// Copied directly from https://github.com/pop-os/cosmic-text/blob/58c2ccd1fb3daf0abc792f9dd52b5766b7125ccd/src/edit/editor.rs#L66 | ||
fn cursor_position(cursor: &Cursor, run: &LayoutRun) -> Option<(i32, i32)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is exposed via https://docs.rs/cosmic-text/latest/cosmic_text/struct.Editor.html#method.cursor_position; I'd much prefer we used that if at all feasible.
vello_fonts, | ||
buffer_glyphs: create_glyphs_for_editor( | ||
&editor, | ||
Color::from_rgba8(255, 255, 255, 255), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use vello::peniko::color::palette
and then these can be palette::css::WHITE
, palette::css::RED
, palette::css::BLUE
, and palette::css::WHITE
.
I'm working on fixing up other samples to do similar after the color changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use vello::peniko::color
Ew. We should really add color
to the examples as a top-level dependency.
}; | ||
|
||
if let Some(editor_info) = &editor_info { | ||
if text_color.components != editor_info.selected_text_color.components { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the right way to compare, but I understand that it is the easier for now.
We're addressing this in linebender/color#76 but it'll take some time, so this can get fixed after something else lands and gets propagated here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you rebase on top of the current main
as of a couple of minutes ago, the color now impl's PartialEq
, so you can just if text_color != ...
instead of looking at the components.
|
||
if let Some((last_font, last_glyph_color)) = last_font { | ||
if last_font != glyph.font_id | ||
|| last_glyph_color.components != glyph_color.components |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above.
Is there anything left here other than what I just commented on? Or is this basically ready to go? It would be nice to land it soon! |
I will rebase this hopefully in the next day or two |
Add an example for cosmic text.
Related:
306