Skip to content

Commit

Permalink
fix: issues in 0.12.0 nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Jan 15, 2025
1 parent 74b3de5 commit 6ea624a
Show file tree
Hide file tree
Showing 33 changed files with 1,950 additions and 1,190 deletions.
2,290 changes: 1,355 additions & 935 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 7 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@ resolver = "2"
members = ["crates/*"]

[workspace.dependencies]
typst = "0.10.0"
typst-svg = "0.10.0"
typst = "0.12.0"
typst-svg = "0.12.0"

serde = { version = "1", features = ["derive"] }
serde_json = "1"

typst-ts-compiler = { version = "0.4.2-rc6", default-features = false, features = [
] }
typst-ts-core = { version = "0.4.2-rc6", default-features = false, features = [
"flat-vector",
"vector-bbox",
"no-content-hint",
reflexo-typst = { version = "=0.5.1", default-features = false, features = [
"browser-compile",
] }

[profile.release]
Expand All @@ -36,10 +32,9 @@ codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic

[patch.crates-io]
typst = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.10.0-half" }
typst-svg = { git = "https://github.com/Myriad-Dreamin/typst.git", branch = "typst.ts-v0.10.0-half" }
typst-ts-core = { git = "https://github.com/Myriad-Dreamin/typst.ts", branch = "fix-wasm-host", package = "typst-ts-core" }
typst-ts-compiler = { git = "https://github.com/Myriad-Dreamin/typst.ts", branch = "fix-wasm-host", package = "typst-ts-compiler" }
typst = { git = "https://github.com/Myriad-Dreamin/typst.git", tag = "typst.ts/v0.5.0-rc9" }
typst-svg = { git = "https://github.com/Myriad-Dreamin/typst.git", tag = "typst.ts/v0.5.0-rc9" }
reflexo-typst = { git = "https://github.com/Myriad-Dreamin/typst.ts", tag = "v0.5.1", package = "reflexo-typst" }
# typst-ts-core = { path = "../../rust/typst.ts/core" }
# typst-ts-compiler = { path = "../../rust/typst.ts/compiler" }

Expand Down
3 changes: 1 addition & 2 deletions crates/embedded-typst/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ crate-type = ["cdylib"]
[dependencies]
typst.workspace = true
typst-svg.workspace = true
typst-ts-core.workspace = true
typst-ts-compiler.workspace = true
reflexo-typst.workspace = true
serde.workspace = true
serde_json.workspace = true
comemo = "0.3.0"
Expand Down
98 changes: 54 additions & 44 deletions crates/embedded-typst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@ use std::{
collections::HashMap,
hash::Hash,
io::Read,
ops::Deref,
path::{Path, PathBuf},
str::FromStr,
sync::{Arc, Mutex},
};

use base64::Engine;
use reflexo_typst::{
font::{pure::MemoryFontBuilder, FontResolverImpl},
hash::{FingerprintHasher, FingerprintSipHasher},
package::{dummy::DummyRegistry, PackageRegistry, PackageSpec},
typst::prelude::EcoVec,
vfs::dummy::DummyAccessModel,
CompilerUniverse, EntryManager, EntryReader, EntryState, ShadowApi, TypstFileId,
};
use serde::{Deserialize, Serialize, Serializer};
use typst::{
diag::{eco_format, PackageError},
foundations::Bytes,
syntax::{PackageSpec, VirtualPath},
};
use typst_ts_compiler::{
font::pure::MemoryFontBuilder, service::WorkspaceProvider, vfs::dummy::DummyAccessModel,
ShadowApi,
};
use typst_ts_core::{
hash::{FingerprintHasher, FingerprintSipHasher},
package::{dummy::DummyRegistry, Registry},
typst::prelude::EcoVec,
TypstFileId,
syntax::VirtualPath,
};

use wasm_minimal_protocol::*;
Expand Down Expand Up @@ -105,7 +102,7 @@ impl DataRef {
}
}

/// Alloccates a data reference
/// Allocates a data reference
fn allocate_data_inner(kind: &[u8], data: &[u8], path: Option<&[u8]>) -> StrResult<Vec<u8>> {
let kind = std::str::from_utf8(kind).map_err(|e| e.to_string())?;
let data = ImmutBytes(Bytes::from(data));
Expand Down Expand Up @@ -162,7 +159,7 @@ pub fn encode_base64(data: &[u8]) -> Vec<u8> {
.into_bytes()
}

/// Alloccates a data reference, return the key of the data reference.
/// Allocates a data reference, return the key of the data reference.
///
/// # Panics
///
Expand All @@ -176,7 +173,7 @@ pub fn allocate_data(kind: &[u8], data: &[u8]) -> StrResult<Vec<u8>> {
allocate_data_inner(kind, data, None)
}

/// Alloccates a file reference, return the key of the data reference.
/// Allocates a file reference, return the key of the data reference.
///
/// # Panics
///
Expand Down Expand Up @@ -295,7 +292,7 @@ fn resolve_world(ctx: &[u8]) -> StrResult<Arc<Mutex<WorldRepr>>> {
let mut fb = MemoryFontBuilder::new();
let mut pb = MemoryPackageBuilder::default();

let vfs = typst_ts_compiler::vfs::Vfs::new(DummyAccessModel);
let mut vfs = reflexo_typst::vfs::Vfs::new(DummyAccessModel);

for data in ctx.data {
match data {
Expand All @@ -321,7 +318,13 @@ fn resolve_world(ctx: &[u8]) -> StrResult<Arc<Mutex<WorldRepr>>> {
}

// Creates a world.
let world = WorldRepr::new_raw(PathBuf::from("/"), vfs, DummyRegistry, fb.into());
let world = WorldRepr::new_raw(
EntryState::new_workspace(PathBuf::from("/").into()),
None,
vfs,
DummyRegistry,
Arc::new(fb.into()),
);

Ok(Arc::new(Mutex::new(world)))
}
Expand All @@ -343,7 +346,7 @@ fn resolve_world(ctx: &[u8]) -> StrResult<Arc<Mutex<WorldRepr>>> {
let world = resolve_world_inner(ctx)?;

// Adds files to the world.
let world_mut = world.lock().unwrap();
let mut world_mut = world.lock().unwrap();
for f in files.into_iter() {
let path = match &f {
DataRef::File { path, .. } => path,
Expand Down Expand Up @@ -382,34 +385,38 @@ pub fn svg(context: &[u8], input: &[u8]) -> StrResult<Vec<u8>> {
return Ok(t);
}
};
let mut world = world.lock().unwrap();
world.reset();
let mut verse = world.lock().unwrap();
verse.reset();

// Prepare main file.
let entry_file = Path::new("/main.typ");
world.set_main_id(TypstFileId::new(None, VirtualPath::new(entry_file)));
world
let entry = verse
.entry_state()
.select_in_workspace(TypstFileId::new(None, VirtualPath::new(entry_file)));
let _ = verse.mutate_entry(entry);
verse
.map_shadow(entry_file, Bytes::from(input.to_owned()))
.map_err(|e| e.to_string())?;

// Compile.
let mut tracer = Default::default();
let doc = typst::compile(world.deref(), &mut tracer).map_err(
|e: EcoVec<typst::diag::SourceDiagnostic>| {
let mut error_log = String::new();
for c in e.into_iter() {
error_log.push_str(&format!(
"{:?}\n",
typst_ts_core::error::diag_from_std(c, Some(world.deref()))
));
}
error_log
},
)?;
let world = &verse.snapshot();
// todo: warning
let doc =
typst::compile(world)
.output
.map_err(|e: EcoVec<typst::diag::SourceDiagnostic>| {
let mut error_log = String::new();
for c in e.into_iter() {
error_log.push_str(&format!(
"{:?}\n",
reflexo_typst::error::diag_from_std(c, Some(world))
));
}
error_log
})?;

// Query header.
let header =
typst_ts_compiler::service::query::retrieve(world.deref(), "<embedded-typst>", &doc);
let header = reflexo_typst::query::retrieve(world, "<embedded-typst>", &doc);
let header = match header {
Ok(header) => serde_json::to_vec(&header).map_err(|e| e.to_string())?,
Err(e) => serde_json::to_vec(&e).map_err(|e| e.to_string())?,
Expand All @@ -431,14 +438,16 @@ pub fn svg(context: &[u8], input: &[u8]) -> StrResult<Vec<u8>> {
#[derive(Debug, Clone, Copy)]
pub struct WasmCompilerFeat;

impl typst_ts_compiler::world::CompilerFeat for WasmCompilerFeat {
impl reflexo_typst::world::CompilerFeat for WasmCompilerFeat {
/// It accesses no file system.
type FontResolver = FontResolverImpl;
/// It accesses no file system.
type AccessModel = DummyAccessModel;
/// It cannot load any package.
type Registry = DummyRegistry;
}

type WorldRepr = typst_ts_compiler::world::CompilerWorld<WasmCompilerFeat>;
type WorldRepr = reflexo_typst::world::CompilerUniverse<WasmCompilerFeat>;

/// The compiler world in wasm environment.
#[allow(unused)]
Expand All @@ -454,7 +463,7 @@ impl TypstWasmWorld {
/// Create a new [`TypstWasmWorld`].
pub fn new() -> Self {
// Creates a virtual file system.
let vfs = typst_ts_compiler::vfs::Vfs::new(DummyAccessModel);
let vfs = reflexo_typst::vfs::Vfs::new(DummyAccessModel);

// Adds embedded fonts.
let mut fb = MemoryFontBuilder::new();
Expand All @@ -463,11 +472,12 @@ impl TypstWasmWorld {
}

// Creates a world.
Self(WorldRepr::new_raw(
PathBuf::from("/"),
Self(CompilerUniverse::new_raw(
EntryState::new_workspace(PathBuf::from("/").into()),
None,
vfs,
DummyRegistry,
fb.into(),
Arc::new(fb.into()),
))
}
}
Expand Down Expand Up @@ -495,7 +505,7 @@ impl MemoryPackageBuilder {
#[derive(Default, Debug)]
pub struct MemoryPackageRegistry(HashMap<PackageSpec, Arc<Path>>);

impl Registry for MemoryPackageRegistry {
impl PackageRegistry for MemoryPackageRegistry {
/// Resolves a package.
fn resolve(&self, spec: &PackageSpec) -> Result<Arc<Path>, PackageError> {
self.0
Expand Down
76 changes: 38 additions & 38 deletions src/basic/png.typ
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
#let xor(a, b, bits: 32) = {
import calc: *
let x = 0
for k in range(bits) {
x += int(odd(a) != odd(b)) * pow(2, k)
a = quo(a, 2)
b = quo(b, 2)
}
x
import calc: *
let x = 0
for k in range(bits) {
x += int(odd(a) != odd(b)) * pow(2, k)
a = quo(a, 2)
b = quo(b, 2)
}
x
}

#let slice(x, bits: 32) = calc.rem(x, calc.pow(2, bits))

#let le32(n) = {
assert(n >= 0 and n < calc.pow(2, 32))
for _ in range(4) {
(calc.rem(n, 256),)
n = calc.quo(n, 256)
}
assert(n >= 0 and n < calc.pow(2, 32))
for _ in range(4) {
(calc.rem(n, 256),)
n = calc.quo(n, 256)
}
}

#let be32(n) = le32(n).rev()

#let crc32-table = for n in range(256) {
let c = n
for k in range(8) {
if calc.odd(c) {
c = xor(0xEDB88320, calc.quo(c, 2))
} else {
c = calc.quo(c, 2)
}
let c = n
for k in range(8) {
if calc.odd(c) {
c = xor(0xEDB88320, calc.quo(c, 2))
} else {
c = calc.quo(c, 2)
}
(c,)
}
(c,)
}

#let crc32(bytes) = {
let crc = 0xFFFFFFFF
for n in bytes {
let x = crc32-table.at(xor(crc, n, bits: 8))
crc = xor(x, calc.quo(crc, 256))
}
xor(crc, 0xFFFFFFFF)
let crc = 0xFFFFFFFF
for n in bytes {
let x = crc32-table.at(xor(crc, n, bits: 8))
crc = xor(x, calc.quo(crc, 256))
}
xor(crc, 0xFFFFFFFF)
}

#let adler32(bytes) = {
let (a, b) = (1, 0)
for n in bytes {
a = calc.rem(a + n, 65521)
b = calc.rem(a + b, 65521)
}
b * calc.pow(2, 16) + a
let (a, b) = (1, 0)
for n in bytes {
a = calc.rem(a + n, 65521)
b = calc.rem(a + b, 65521)
}
b * calc.pow(2, 16) + a
}

#let chunk(type, data) = {
let type = array(bytes(type))
be32(data.len())
type
data
be32(crc32(type + data))
let type = array(bytes(type))
be32(data.len())
type
data
be32(crc32(type + data))
}
13 changes: 8 additions & 5 deletions src/basic/reference-data-process.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#show: book.ref-page.with(title: [参考:数据读写与数据处理])

#let table-lnk(name, ref, it, scope: (:), res: none, ..args) = (
align(center + horizon, link("todo", name)),
align(center + horizon, link("todo", name)),
it,
align(horizon, {
set heading(bookmarked: false, outlined: false)
eval(it.text, mode: "markup", scope: scope)
}),
align(
horizon,
{
set heading(bookmarked: false, outlined: false)
eval(it.text, mode: "markup", scope: scope)
},
),
)

== read
Expand Down
13 changes: 8 additions & 5 deletions src/basic/reference-date.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#show: book.ref-page.with(title: [参考:时间类型])

#let table-lnk(name, ref, it, scope: (:), res: none, ..args) = (
align(center + horizon, link("todo", name)),
align(center + horizon, link("todo", name)),
it,
align(horizon, {
set heading(bookmarked: false, outlined: false)
eval(it.text, mode: "markup", scope: scope)
}),
align(
horizon,
{
set heading(bookmarked: false, outlined: false)
eval(it.text, mode: "markup", scope: scope)
},
),
)

== 日期
Expand Down
Loading

0 comments on commit 6ea624a

Please sign in to comment.