Skip to content

Commit

Permalink
fix(export::svg): reuse reference in a transformed item (#443)
Browse files Browse the repository at this point in the history
* test: check bug in ieee rendering

* fix(export::svg): reuse reference in a transformed item
  • Loading branch information
Myriad-Dreamin authored Jan 3, 2024
1 parent fea8842 commit f528532
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 7 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ members = [

"tests/common",
"tests/heap-profile",
"tests/incremental",
"tests/integration",
"tests/std",
]
Expand Down
14 changes: 7 additions & 7 deletions core/src/vector/flat_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,15 @@ where
) {
let child_ref = &transformed.1;
let state = state.pre_apply(&transformed.0);
if matches!(prev_item_, Some(ir::FlatSvgItem::Item(ir::TransformedRef(_item, prev_ref)))
if prev_ref == child_ref)
{
// assert!(item != &transformed.0);
ts.render_diff_item_ref_at(state, self, Point::default(), child_ref, child_ref);
return;
match prev_item_ {
// if both items are transformed, we can reuse the internal item with transforming it a
// bit.
Some(ir::FlatSvgItem::Item(ir::TransformedRef(_item, prev_ref))) => {
ts.render_diff_item_ref_at(state, self, Point::default(), child_ref, prev_ref);
}
_ => ts.render_item_ref(state, self, child_ref),
}
// failed to reuse
ts.render_item_ref(state, self, child_ref);
}

/// Render a diff text into the underlying context.
Expand Down
27 changes: 27 additions & 0 deletions tests/incremental/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "typst-ts-incremental-test"
authors.workspace = true
version.workspace = true
license.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
typst.workspace = true
typst-syntax.workspace = true
comemo.workspace = true

sha2.workspace = true
anyhow.workspace = true
tokio.workspace = true

typst-ts-dev-server.workspace = true
typst-ts-test-common.workspace = true
typst-ts-core.workspace = true
hex.workspace = true
typst-ts-compiler.workspace = true
typst-ts-svg-exporter.workspace = true

[features]
generate = []
105 changes: 105 additions & 0 deletions tests/incremental/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use std::path::Path;

use typst::model::Document;
use typst_ts_compiler::{
service::{CompileDriver, CompileExporter, Compiler},
ShadowApi, TypstSystemWorld,
};
use typst_ts_core::{
config::CompileOpts,
exporter_builtins::GroupExporter,
vector::{
incr::{IncrDocClient, IncrDocServer},
ir::{Abs, Point, Rect},
stream::BytesModuleStream,
},
};
use typst_ts_svg_exporter::IncrSvgDocClient;

fn get_driver(
workspace_dir: &Path,
entry_file_path: &Path,
exporter: GroupExporter<Document>,
) -> CompileExporter<CompileDriver> {
let project_base = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
let font_path = project_base.join("assets/fonts");
let world = TypstSystemWorld::new(CompileOpts {
root_dir: workspace_dir.to_owned(),
no_system_fonts: true,
font_paths: vec![font_path],
..CompileOpts::default()
})
.unwrap();

let driver = CompileDriver {
world,
entry_file: entry_file_path.to_owned(),
};

CompileExporter::new(driver).with_exporter(exporter)
}

pub fn test_compiler(
workspace_dir: &Path,
entry_file_path: &Path,
exporter: GroupExporter<Document>,
) {
let mut driver = get_driver(workspace_dir, entry_file_path, exporter);
let mut content = { std::fs::read_to_string(entry_file_path).expect("Could not read file") };

let mut incr_server = IncrDocServer::default();
let mut incr_client = IncrDocClient::default();
let mut incr_svg_client = IncrSvgDocClient::default();

let window = Rect {
lo: Point::new(Abs::from(0.), Abs::from(0.)),
hi: Point::new(Abs::from(1e33), Abs::from(1e33)),
};

let mut diff = vec![];

// checkout the entry file
let main_id = driver.main_id();

let doc = driver
.with_shadow_file_by_id(main_id, content.as_bytes().into(), |driver| {
driver.compile(&mut Default::default())
})
.unwrap();
let server_delta = incr_server.pack_delta(doc);
let server_delta = BytesModuleStream::from_slice(&server_delta).checkout_owned();
incr_client.merge_delta(server_delta);

for i in 0..200 {
println!("Iteration {}", i);

content = content.replace("@netwok2020", "@netwok2020 x");

let doc = driver
.with_shadow_file_by_id(main_id, content.as_bytes().into(), |driver| {
driver.compile(&mut Default::default())
})
.unwrap();

let server_delta = incr_server.pack_delta(doc);
let sd = server_delta.len();
let server_delta = BytesModuleStream::from_slice(&server_delta).checkout_owned();
incr_client.merge_delta(server_delta);
incr_client.set_layout(incr_client.doc.layouts[0].unwrap_single());
let cd = incr_svg_client.render_in_window(&mut incr_client, window);
// std::fs::write(format!("{}.svg", i), cd.clone()).unwrap();
diff.push((sd, cd.len()));

comemo::evict(10);
}

println!("diff: {:?}", diff);
}

pub fn main() {
let workspace_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
let entry_file_path = workspace_dir.join("fuzzers/corpora/typst-templates/ieee/main.typ");

let noop_exporter = GroupExporter::new(vec![]);
test_compiler(&workspace_dir, &entry_file_path, noop_exporter);
}

0 comments on commit f528532

Please sign in to comment.