Skip to content

Commit

Permalink
feat: move fonts folder, add RawTypstAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
nixon-voxell committed May 21, 2024
1 parent 9a2c224 commit 87c58fd
Show file tree
Hide file tree
Showing 50 changed files with 81 additions and 92 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions crates/bevy_typst/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
[package]
name = "bevy_typst"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
motiongfx_core = { path = "../motiongfx_core" }
motiongfx_vello = { path = "../motiongfx_vello" }
motiongfx_typst_macros = { path = "macros" }

bevy = "0.13"
bevy_vello = "0.3"
# typst dependencies
Expand All @@ -28,3 +23,7 @@ env_proxy = "0.4"

[lints]
workspace = true

[features]
default = ["embed-fonts"]
embed-fonts = []
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 0 additions & 16 deletions crates/bevy_typst/macros/Cargo.toml

This file was deleted.

22 changes: 0 additions & 22 deletions crates/bevy_typst/macros/src/lib.rs

This file was deleted.

63 changes: 63 additions & 0 deletions crates/bevy_typst/src/asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use bevy::{
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext},
prelude::*,
utils::{
thiserror::{self, Error},
BoxedFuture,
},
};

pub struct TypstAssetPlugin;

impl Plugin for TypstAssetPlugin {
fn build(&self, app: &mut App) {
app.init_asset::<RawTypstAsset>()
.init_asset_loader::<RawTypstAssetLoader>();
}
}

#[derive(Asset, TypePath)]
pub struct RawTypstAsset(String);

impl RawTypstAsset {
pub fn content(&self) -> &str {
&self.0
}
}

#[derive(Default)]
struct RawTypstAssetLoader;

impl AssetLoader for RawTypstAssetLoader {
type Asset = RawTypstAsset;

type Settings = ();

type Error = RawTypstAssetLoaderError;

fn load<'a>(
&'a self,
reader: &'a mut Reader,
_settings: &'a Self::Settings,
_load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
Box::pin(async move {
let mut string = String::new();
reader.read_to_string(&mut string).await?;
Ok(RawTypstAsset(string))
})
}

fn extensions(&self) -> &[&str] {
&["typ"]
}
}

/// Possible errors that can be produced by [`BvhAssetLoader`]
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum RawTypstAssetLoaderError {
/// An [Io](std::io) Error
#[error("Could not load typst file: {0}")]
Io(#[from] std::io::Error),
}
6 changes: 3 additions & 3 deletions crates/bevy_typst/src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ impl FontSearcher {
}

// Embedded fonts have lowest priority.
// #[cfg(feature = "embed-fonts")]
#[cfg(feature = "embed-fonts")]
self.add_embedded();
}

/// Add fonts that are embedded in the binary.
// #[cfg(feature = "embed-fonts")]
#[cfg(feature = "embed-fonts")]
fn add_embedded(&mut self) {
let mut process = |bytes: &'static [u8]| {
let buffer = typst::foundations::Bytes::from_static(bytes);
Expand All @@ -101,7 +101,7 @@ impl FontSearcher {

macro_rules! add {
($filename:literal) => {
process(include_bytes!(concat!("../../../assets/fonts/", $filename)));
process(include_bytes!(concat!("../fonts/", $filename)));
};
}

Expand Down
39 changes: 7 additions & 32 deletions crates/bevy_typst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod prelude {
pub use crate::{world::TypstWorld, TypstCompiler, TypstCompilerPlugin};
}

pub mod asset;
mod download;
mod fonts;
mod package;
Expand All @@ -34,35 +35,7 @@ impl Plugin for TypstCompilerPlugin {
}
}

/// A compiler for compiling Typst content.
///
/// This compiler can be accessed from the resource:
/// ```
/// use bevy::prelude::*;
/// use motiongfx_vello::prelude::*;
/// use motiongfx_typst::TypstCompiler;
///
/// pub fn compile(
/// mut commands: Commands,
/// mut typst_compiler: ResMut<TypstCompiler>,
/// mut scenes: ResMut<Assets<VelloScene>>,
/// ) {
/// let content: String = String::from(
/// r###"
/// = Introduction
/// + First element.
/// + Second element.
/// "###,
/// );
///
/// match typst_compiler.compile_flatten(&mut commands, &mut scenes, content) {
/// Ok(tree) => {
/// println!("{:#?}", tree.size);
/// }
/// Err(_) => todo!(),
/// }
/// }
/// ```
/// A resource compiler for compiling Typst content.
#[derive(Resource)]
pub struct TypstCompiler {
world: TypstWorld,
Expand All @@ -79,9 +52,11 @@ impl TypstCompiler {
}
}

// TODO: take a look at typst_ide for getting FrameItem to svg output relation
pub fn compile_text(&mut self, text: String) -> Result<usvg::Tree, EcoVec<SourceDiagnostic>> {
self.world.set_source(text);
pub fn compile_string(
&mut self,
string: String,
) -> Result<usvg::Tree, EcoVec<SourceDiagnostic>> {
self.world.set_source(string);
let document = typst::compile(&self.world, &mut self.tracer)?;

let svg = typst_svg::svg_merged(&document, Abs::zero());
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_vello_graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "bevy_vello_graphics"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
motiongfx_core = { path = "../motiongfx_core", optional = true }
Expand Down

0 comments on commit 87c58fd

Please sign in to comment.