use std::sync::{Arc, Mutex};
use bevy::prelude::*;
use parley::{FontContext, LayoutContext};
pub struct NannouTextCxInner {
pub font: FontContext,
pub layout: LayoutContext<Color>,
}
#[derive(Resource, Clone)]
pub struct SharedTextCx(pub Arc<Mutex<NannouTextCxInner>>);
pub(crate) fn init_shared_text_cx(app: &mut App) {
let mut font = FontContext::default();
font.collection.make_shared();
#[cfg(feature = "notosans")]
{
let registered = font
.collection
.register_fonts(notosans::REGULAR_TTF.to_vec().into(), None);
if let Some((family_id, _)) = registered.first() {
font.collection.set_generic_families(
parley::GenericFamily::SansSerif,
std::iter::once(*family_id),
);
}
}
let mut font_cx = bevy::text::FontCx::default();
font_cx.context = font.clone();
app.insert_resource(font_cx);
let inner = NannouTextCxInner {
font,
layout: LayoutContext::new(),
};
app.insert_resource(SharedTextCx(Arc::new(Mutex::new(inner))));
}