Skip to content

Commit 689c04f

Browse files
committed
wip: adding font to font collection to all asset loader threads
1 parent 6740812 commit 689c04f

File tree

7 files changed

+89
-28
lines changed

7 files changed

+89
-28
lines changed

src/plugin.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
VelloFont, VelloRenderSettings,
33
render::{VelloCanvasSettings, VelloRenderPlugin},
4-
text::VelloFontLoader,
4+
text::{VelloFontLoader, VelloFontLoaderPlugin},
55
};
66
use bevy::{prelude::*, render::view::RenderLayers};
77
use vello::AaConfig;
@@ -32,15 +32,18 @@ impl Default for VelloPlugin {
3232

3333
impl Plugin for VelloPlugin {
3434
fn build(&self, app: &mut App) {
35-
app.add_plugins(VelloRenderPlugin {
36-
canvas_settings: VelloCanvasSettings {
37-
render_layers: self.canvas_render_layers.clone(),
35+
app.add_plugins((
36+
VelloRenderPlugin {
37+
canvas_settings: VelloCanvasSettings {
38+
render_layers: self.canvas_render_layers.clone(),
39+
},
40+
render_settings: VelloRenderSettings {
41+
use_cpu: self.use_cpu,
42+
antialiasing: self.antialiasing,
43+
},
3844
},
39-
render_settings: VelloRenderSettings {
40-
use_cpu: self.use_cpu,
41-
antialiasing: self.antialiasing,
42-
},
43-
})
45+
VelloFontLoaderPlugin,
46+
))
4447
.init_asset::<VelloFont>()
4548
.init_asset_loader::<VelloFontLoader>();
4649
#[cfg(feature = "svg")]

src/render/extract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::prelude::*;
33
use bevy::{
44
prelude::*,
55
render::{
6-
camera::ExtractedCamera, extract_component::ExtractComponent,
7-
sync_world::TemporaryRenderEntity, view::RenderLayers, Extract, MainWorld,
6+
Extract, MainWorld, camera::ExtractedCamera, extract_component::ExtractComponent,
7+
sync_world::TemporaryRenderEntity, view::RenderLayers,
88
},
99
};
1010

src/render/systems.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::{
2-
extract::{ExtractedVelloText, SSRenderTarget},
3-
prepare::PreparedAffine,
42
VelloCanvasMaterial, VelloCanvasSettings, VelloEntityCountData, VelloFrameProfileData,
53
VelloRenderItem, VelloRenderQueue, VelloRenderSettings, VelloRenderer,
4+
extract::{ExtractedVelloText, SSRenderTarget},
5+
prepare::PreparedAffine,
66
};
7-
use crate::{render::extract::ExtractedVelloScene, VelloFont};
7+
use crate::{VelloFont, render::extract::ExtractedVelloScene};
88
use bevy::{
99
prelude::*,
1010
render::{

src/text/context.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ thread_local! {
1616
});
1717
pub static LAYOUT_CONTEXT: RefCell<LayoutContext<Brush>> = RefCell::new(LayoutContext::new());
1818
}
19+
20+
// static ONE_FONT_CONTEXT: std::sync::OnceLock<FontContext> = std::sync::OnceLock::new();
21+
//
22+
// pub fn get_font_context() -> &'static FontContext {
23+
// ONE_FONT_CONTEXT.get_or_init(|| FontContext {
24+
// collection: Collection::new(CollectionOptions {
25+
// shared: true,
26+
// system_fonts: false,
27+
// }),
28+
// ..Default::default()
29+
// })
30+
// }

src/text/font.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use std::borrow::Cow;
22

33
use super::{
4+
VelloTextAnchor,
45
context::{FONT_CONTEXT, LAYOUT_CONTEXT},
56
vello_text::VelloTextSection,
6-
VelloTextAnchor,
77
};
88
use bevy::{prelude::*, reflect::TypePath, render::render_asset::RenderAsset};
99
use parley::{FontWeight, InlineBox, PositionedLayoutItem, StyleProperty};
10-
use vello::{kurbo::Affine, peniko::Fill, Scene};
10+
use vello::{Scene, kurbo::Affine, peniko::Fill};
1111

12-
#[derive(Asset, TypePath, Debug, Clone, Deref, DerefMut)]
12+
#[derive(Asset, TypePath, Debug, Clone)]
1313
pub struct VelloFont {
1414
pub family_name: String,
15+
pub bytes: Vec<u8>,
1516
}
1617

1718
impl RenderAsset for VelloFont {
@@ -30,8 +31,9 @@ impl RenderAsset for VelloFont {
3031
impl VelloFont {
3132
pub fn sizeof(&self, text: &VelloTextSection) -> Vec2 {
3233
FONT_CONTEXT.with_borrow_mut(|font_context| {
33-
println!(
34-
"font fams in collection {:?}",
34+
debug!(
35+
"Thread {:?} sizeof collection {:?}",
36+
std::thread::current().id(),
3537
font_context.collection.family_names().collect::<Vec<_>>()
3638
);
3739

@@ -66,9 +68,7 @@ impl VelloFont {
6668

6769
let layout = builder.build(&text.value);
6870

69-
let res = Vec2::new(layout.width(), layout.height());
70-
println!("sizeof: {:?}", res);
71-
res
71+
Vec2::new(layout.width(), layout.height())
7272
})
7373
})
7474
}
@@ -82,6 +82,12 @@ impl VelloFont {
8282
) {
8383
FONT_CONTEXT.with_borrow_mut(|font_context| {
8484
LAYOUT_CONTEXT.with_borrow_mut(|layout_context| {
85+
debug!(
86+
"Thread {:?} render collection {:?}",
87+
std::thread::current().id(),
88+
font_context.collection.family_names().collect::<Vec<_>>()
89+
);
90+
8591
// TODO: fix scale magic number
8692
let mut builder = layout_context.ranged_builder(font_context, &text.value, 1.0);
8793

@@ -95,6 +101,7 @@ impl VelloFont {
95101
builder.push_default(StyleProperty::LineHeight(line_height));
96102
}
97103

104+
debug!("Family name: {:?}", self.family_name);
98105
builder.push_default(StyleProperty::FontStack(parley::FontStack::Single(
99106
parley::FontFamily::Named(Cow::Borrowed(&self.family_name)),
100107
)));
@@ -120,6 +127,7 @@ impl VelloFont {
120127
let glyph_xform = synthesis
121128
.skew()
122129
.map(|angle| Affine::skew(angle.to_radians().tan() as f64, 0.0));
130+
123131
scene
124132
.draw_glyphs(font)
125133
.brush(&text.style.brush)
@@ -146,6 +154,7 @@ impl VelloFont {
146154

147155
let width = layout.width() as f64;
148156
let height = layout.height() as f64;
157+
debug!("Width: {:?}, Height: {:?}", width, height);
149158

150159
match text_anchor {
151160
VelloTextAnchor::TopLeft => {

src/text/font_loader.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
11
use super::{context::FONT_CONTEXT, font::VelloFont};
22
use crate::integrations::VectorLoaderError;
3-
use bevy::asset::{AssetLoader, LoadContext, io::Reader};
3+
use bevy::{
4+
asset::{AssetLoader, LoadContext, io::Reader},
5+
prelude::*,
6+
tasks::ComputeTaskPool,
7+
};
48

59
#[derive(Default)]
610
pub struct VelloFontLoader;
711

12+
pub struct VelloFontLoaderPlugin;
13+
14+
impl Plugin for VelloFontLoaderPlugin {
15+
fn build(&self, app: &mut App) {
16+
app.add_systems(Update, font_context_sync);
17+
}
18+
}
19+
20+
fn font_context_sync(vello_fonts: Res<Assets<VelloFont>>) {
21+
if vello_fonts.is_changed() {
22+
if let Some(compute_task_pool) = ComputeTaskPool::try_get() {
23+
let compute_threads = compute_task_pool.thread_num();
24+
25+
for _ in 0..compute_threads {
26+
for (_handle, font) in vello_fonts.iter() {
27+
let font = font.clone();
28+
29+
compute_task_pool
30+
.spawn(async move {
31+
debug!(
32+
"Thread {:?} registering font {:?}",
33+
std::thread::current().id(),
34+
font.family_name
35+
);
36+
FONT_CONTEXT.with_borrow_mut(|font_context| {
37+
font_context.collection.register_fonts(font.bytes.clone());
38+
});
39+
})
40+
.detach();
41+
}
42+
}
43+
}
44+
}
45+
}
46+
847
impl AssetLoader for VelloFontLoader {
948
type Asset = VelloFont;
1049

@@ -22,20 +61,18 @@ impl AssetLoader for VelloFontLoader {
2261
reader.read_to_end(&mut bytes).await?;
2362

2463
FONT_CONTEXT.with_borrow_mut(|font_context| {
25-
let registered_fonts = font_context.collection.register_fonts(bytes);
64+
let registered_fonts = font_context.collection.register_fonts(bytes.clone());
2665
// TODO: handle multiple fonts in the same font file
2766
let (family_id, _font_info_vec) = registered_fonts.first().unwrap();
2867
let family_name = font_context.collection.family_name(*family_id).unwrap();
2968
let vello_font = VelloFont {
3069
family_name: family_name.to_string(),
70+
bytes,
3171
};
32-
println!("font fams in collection {:?}", font_context.collection.family_names().collect::<Vec<_>>());
33-
println!("vello font {:?}", vello_font);
3472
Ok(vello_font)
3573
})
3674
}
3775

38-
3976
fn extensions(&self) -> &[&str] {
4077
&["ttf"]
4178
}

src/text/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ mod font_loader;
66
mod vello_text;
77

88
pub use font::VelloFont;
9-
pub(crate) use font_loader::VelloFontLoader;
9+
pub(crate) use font_loader::{VelloFontLoader, VelloFontLoaderPlugin};
1010
pub use vello_text::{VelloTextAnchor, VelloTextSection, VelloTextStyle};

0 commit comments

Comments
 (0)