Skip to content

Commit

Permalink
Merge pull request #47 from nixon-voxell/feature/bevy_vello
Browse files Browse the repository at this point in the history
Renaming `typst_motiongfx` to `bevy_typst`, use `bevy_vello` instead of bevy_vello_renderer`
  • Loading branch information
nixon-voxell committed May 22, 2024
2 parents 474bc03 + a7961ee commit 8df1ee3
Show file tree
Hide file tree
Showing 78 changed files with 646 additions and 673 deletions.
523 changes: 303 additions & 220 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 16 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
[package]
name = "bevy_motiongfx"
[workspace]
resolver = "2"
members = ["crates/*", ]

[workspace.package]
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/nixon-voxell/bevy_motiongfx"

[package]
name = "bevy_motiongfx"
categories = ["graphics", "gui", "rendering", "motion-graphics", "vector-graphics"]
description = "Motion graphics creation tool in Bevy. (Highly inspired by Motion Canvas and Manim)"
exclude = ["assets/", "tools/", ".github/", "crates/", "examples/wasm/assets/"]
exclude = ["/assets/", "/.github/", "/examples/"]
keywords = ["motion-graphics", "game", "gamedev", "graphics", "bevy", "vello"]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/nixon-voxell/bevy_motiongfx"

[workspace]
members = ["crates/*", ]
resolver = "2"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
motiongfx_core = { path = "crates/motiongfx_core" }
motiongfx_vello = { path = "crates/motiongfx_vello" }
motiongfx_typst = { path = "crates/motiongfx_typst" }
bevy_typst = { path = "crates/bevy_typst" }
bevy_vello_graphics = { path = "crates/bevy_vello_graphics" }

[dev-dependencies]
Expand Down
Binary file removed assets/fonts/DejaVuSansMono-Bold.ttf
Binary file not shown.
Binary file removed assets/fonts/DejaVuSansMono-BoldOblique.ttf
Binary file not shown.
Binary file removed assets/fonts/DejaVuSansMono-Oblique.ttf
Binary file not shown.
Binary file removed assets/fonts/DejaVuSansMono.ttf
Binary file not shown.
Binary file removed assets/fonts/FiraMath-Regular.otf
Binary file not shown.
Binary file removed assets/fonts/IBMPlexSans-Bold.ttf
Binary file not shown.
Binary file removed assets/fonts/IBMPlexSans-Light.ttf
Binary file not shown.
Binary file removed assets/fonts/IBMPlexSans-Medium.ttf
Binary file not shown.
Binary file removed assets/fonts/IBMPlexSans-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/IBMPlexSansCondensed-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/IBMPlexSansDevanagari-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/IBMPlexSerif-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/InriaSerif-Bold.ttf
Binary file not shown.
Binary file removed assets/fonts/InriaSerif-BoldItalic.ttf
Binary file not shown.
Binary file removed assets/fonts/InriaSerif-Italic.ttf
Binary file not shown.
Binary file removed assets/fonts/InriaSerif-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/LinLibertine_R.ttf
Binary file not shown.
Binary file removed assets/fonts/LinLibertine_RB.ttf
Binary file not shown.
Binary file removed assets/fonts/LinLibertine_RBI.ttf
Binary file not shown.
Binary file removed assets/fonts/LinLibertine_RI.ttf
Binary file not shown.
Binary file removed assets/fonts/NewCM10-Bold.otf
Binary file not shown.
Binary file removed assets/fonts/NewCM10-BoldItalic.otf
Binary file not shown.
Binary file removed assets/fonts/NewCM10-Italic.otf
Binary file not shown.
Binary file removed assets/fonts/NewCM10-Regular.otf
Binary file not shown.
Binary file removed assets/fonts/NewCMMath-Book.otf
Binary file not shown.
Binary file removed assets/fonts/NewCMMath-Regular.otf
Binary file not shown.
Binary file removed assets/fonts/NotoColorEmoji.ttf
Binary file not shown.
Binary file removed assets/fonts/NotoSansArabic-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/NotoSansSymbols2-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/NotoSansThai-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/NotoSerifCJKjp-Regular.otf
Binary file not shown.
Binary file removed assets/fonts/NotoSerifCJKkr-Regular.otf
Binary file not shown.
Binary file removed assets/fonts/NotoSerifCJKsc-Bold.otf
Binary file not shown.
Binary file removed assets/fonts/NotoSerifCJKsc-Regular.otf
Binary file not shown.
Binary file removed assets/fonts/NotoSerifCJKtc-Bold.otf
Binary file not shown.
Binary file removed assets/fonts/NotoSerifCJKtc-Regular.otf
Binary file not shown.
Binary file removed assets/fonts/NotoSerifHebrew-Bold.ttf
Binary file not shown.
Binary file removed assets/fonts/NotoSerifHebrew-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/PTSans-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/Roboto-Regular.ttf
Binary file not shown.
Binary file removed assets/fonts/TwitterColorEmoji.ttf
Binary file not shown.
Binary file removed assets/fonts/Ubuntu-Regular.ttf
Binary file not shown.
30 changes: 30 additions & 0 deletions crates/bevy_typst/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "bevy_typst"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
bevy = "0.13"
bevy_vello = "0.3"
# typst dependencies
typst = "0.11"
typst-svg = "0.11"
typst-assets = { version = "0.11", optional = true }
comemo = "0.4" # in sync with typst
fontdb = { version = "0.16", features = ["memmap", "fontconfig"] }
chrono = { version = "0.4.24", default-features = false, features = ["clock", "std"] }
ureq = "2"
dirs = "5"
tar = "0.4"
flate2 = "1"
codespan-reporting = "0.11"
ecow = { version = "0.2", features = ["serde"] }
env_proxy = "0.4"

[lints]
workspace = true

[features]
default = ["embed-fonts"]
embed-fonts = ["dep:typst-assets"]
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)]
pub 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 [`RawTypstAssetLoader`]
#[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),
}
54 changes: 54 additions & 0 deletions crates/bevy_typst/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::path::PathBuf;

use bevy::prelude::*;
use typst::{diag::SourceResult, eval::Tracer, model::Document};

use world::TypstWorld;

pub mod fonts;
pub mod world;

mod download;
mod package;

#[derive(Default)]
pub struct TypstCompilerPlugin {
font_paths: Vec<PathBuf>,
}

impl TypstCompilerPlugin {
pub fn new(font_paths: Vec<PathBuf>) -> Self {
Self { font_paths }
}
}

impl Plugin for TypstCompilerPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(TypstCompiler::new(&self.font_paths));
}
}

/// A resource compiler for compiling Typst content.
#[derive(Resource)]
pub struct TypstCompiler {
world: TypstWorld,
tracer: Tracer,
}

impl TypstCompiler {
pub fn new(font_paths: &[PathBuf]) -> Self {
let mut assets = PathBuf::from(".");
assets.push("assets");
Self {
world: TypstWorld::new(assets, font_paths).unwrap(),
tracer: Tracer::new(),
}
}

/// Compile a raw Typst string into a document.
pub fn compile(&mut self, text: &str) -> SourceResult<Document> {
let source = self.world.get_main_source_mut();
source.replace(text);
typst::compile(&self.world, &mut self.tracer)
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use fontdb::{Database, Source};
use typst::text::{Font, FontBook, FontInfo};

/// Searches for fonts.
#[derive(Debug, Default, Clone)]
pub struct FontSearcher {
/// Metadata about all discovered fonts.
pub book: FontBook,
Expand All @@ -14,6 +15,7 @@ pub struct FontSearcher {
}

/// Holds details about the location of a font and lazily the font itself.
#[derive(Debug, Clone)]
pub struct FontSlot {
/// The path at which the font can be found on the system.
path: PathBuf,
Expand All @@ -37,14 +39,6 @@ impl FontSlot {
}

impl FontSearcher {
/// Create a new, empty system searcher.
pub fn new() -> Self {
Self {
book: FontBook::new(),
fonts: vec![],
}
}

/// Search everything that is available.
pub fn search(&mut self, font_paths: &[PathBuf]) {
let mut db = Database::new();
Expand Down Expand Up @@ -80,15 +74,15 @@ 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);
for data in typst_assets::fonts() {
let buffer = typst::foundations::Bytes::from_static(data);
for (i, font) in Font::iter(buffer).enumerate() {
self.book.push(font.info().clone());
self.fonts.push(FontSlot {
Expand All @@ -97,28 +91,6 @@ impl FontSearcher {
font: OnceLock::from(Some(font)),
});
}
};

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

// Embed default fonts.
add!("LinLibertine_R.ttf");
add!("LinLibertine_RB.ttf");
add!("LinLibertine_RBI.ttf");
add!("LinLibertine_RI.ttf");
add!("NewCMMath-Book.otf");
add!("NewCMMath-Regular.otf");
add!("NewCM10-Regular.otf");
add!("NewCM10-Bold.otf");
add!("NewCM10-Italic.otf");
add!("NewCM10-BoldItalic.otf");
add!("DejaVuSansMono.ttf");
add!("DejaVuSansMono-Bold.ttf");
add!("DejaVuSansMono-Oblique.ttf");
add!("DejaVuSansMono-BoldOblique.ttf");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use codespan_reporting::term::{
use ecow::eco_format;
use typst::{
diag::{PackageError, PackageResult},
syntax::PackageSpec,
syntax::package::PackageSpec,
};

use crate::download::download_with_progress;
use super::download::download_with_progress;

/// Make a package available in the on-disk cache.
pub fn prepare_package(spec: &PackageSpec) -> PackageResult<PathBuf> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{
fs, mem,
path::{Path, PathBuf},
sync::OnceLock,
sync::{Mutex, OnceLock},
};

use bevy::utils::HashMap;
use chrono::{DateTime, Datelike, Local};
use comemo::Prehashed;
use parking_lot::Mutex;
use typst::{
diag::{FileError, FileResult, StrResult},
foundations::{Bytes, Datetime},
Expand All @@ -16,8 +15,8 @@ use typst::{
Library, World,
};

use crate::fonts::{FontSearcher, FontSlot};
use crate::package;
use super::fonts::{FontSearcher, FontSlot};
use super::package;

pub struct TypstWorld {
/// The root relative to which absolute paths are resolved.
Expand All @@ -40,7 +39,7 @@ pub struct TypstWorld {
impl TypstWorld {
/// Create a new [`TypstWorld`].
pub fn new(root: PathBuf, font_paths: &[PathBuf]) -> StrResult<Self> {
let mut searcher = FontSearcher::new();
let mut searcher = FontSearcher::default();
searcher.search(font_paths);

Ok(Self {
Expand All @@ -54,16 +53,20 @@ impl TypstWorld {
})
}

pub fn set_source(&mut self, text: String) {
self.main = Source::detached(text);
pub fn get_main_source(&self) -> &Source {
&self.main
}

pub fn get_main_source_mut(&mut self) -> &mut Source {
&mut self.main
}

/// Access the canonical slot for the given file id.
fn slot<F, T>(&self, id: FileId, f: F) -> T
where
F: FnOnce(&mut FileSlot) -> T,
{
let mut map = self.slots.lock();
let mut map = self.slots.lock().unwrap();
f(map.entry(id).or_insert_with(|| FileSlot::new(id)))
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_typst/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod prelude {
pub use crate::compiler::{world::TypstWorld, TypstCompiler, TypstCompilerPlugin};
}

pub mod asset;
pub mod compiler;
3 changes: 2 additions & 1 deletion crates/bevy_vello_graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
name = "bevy_vello_graphics"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
motiongfx_core = { path = "../motiongfx_core", optional = true }
bevy_vello_renderer = { git = "https://github.com/nixon-voxell/bevy_vello_renderer", rev = "49ef841842b3170f225614c7d3ffffba31522150" }
bevy_vello = "0.3"
bevy = "0.13"

[features]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_vello_graphics/src/bezpath.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_vello_renderer::vello::kurbo;
use bevy_vello::prelude::*;

use super::VelloVector;

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_vello_graphics/src/brush.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_vello_renderer::vello::{kurbo, peniko};
use bevy_vello::prelude::*;

#[derive(Default, Clone)]
pub struct Brush {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_vello_graphics/src/circle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_vello_renderer::vello::kurbo;
use bevy_vello::prelude::*;
#[cfg(feature = "motiongfx")]
use motiongfx_core::f32lerp::F32Lerp;

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_vello_graphics/src/fill.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_vello_renderer::vello::peniko;
use bevy_vello::prelude::*;

use crate::brush::Brush;

Expand Down
Loading

0 comments on commit 8df1ee3

Please sign in to comment.