Skip to content
This repository was archived by the owner on Jan 26, 2025. It is now read-only.

Commit d78a7c2

Browse files
committed
feat: Optimize reference images
When storing reference images are now optimized by default again which can be disabled with `--no-optimize-references`. Closes #72.
1 parent 435eb5a commit d78a7c2

File tree

13 files changed

+235
-45
lines changed

13 files changed

+235
-45
lines changed

Cargo.lock

Lines changed: 148 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ insta = "1.39.0"
3636
ignore = "0.4.22"
3737
native-tls = "0.2.12"
3838
once_cell = "1.19.0"
39+
oxipng = "9.1.3"
3940
pest_derive = "2.7.10"
4041
pest = "2.7.10"
4142
png = "0.17.13"

crates/typst-test-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fontdb.workspace = true
3535
insta = { workspace = true, features = ["yaml"] }
3636
native-tls.workspace = true
3737
once_cell.workspace = true
38+
oxipng.workspace = true
3839
rayon.workspace = true
3940
semver.workspace = true
4041
serde.workspace = true

crates/typst-test-cli/src/cli/add.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::io::Write;
2+
use std::ops::Not;
23

34
use color_eyre::eyre;
45
use lib::doc::render::ppi_to_ppp;
@@ -8,9 +9,9 @@ use termcolor::Color;
89
use typst::diag::Warned;
910
use typst_syntax::{FileId, Source, VirtualPath};
1011

11-
use super::{CompileArgs, Context, RenderArgs};
12+
use super::{CompileArgs, Context, ExportArgs};
1213
use crate::cli::OperationFailure;
13-
use crate::ui;
14+
use crate::{ui, DEFAULT_OPTIMIZE_OPTIONS};
1415

1516
#[derive(clap::Args, Debug, Clone)]
1617
#[group(id = "add-args")]
@@ -34,7 +35,7 @@ pub struct Args {
3435
pub compile: CompileArgs,
3536

3637
#[command(flatten)]
37-
pub render: RenderArgs,
38+
pub export: ExportArgs,
3839

3940
/// The name of the test to add
4041
pub test: Id,
@@ -72,11 +73,22 @@ pub fn run(ctx: &mut Context, args: &Args) -> eyre::Result<()> {
7273
} = Document::compile(
7374
Source::new(FileId::new_fake(VirtualPath::new("")), template.to_owned()),
7475
&world,
75-
ppi_to_ppp(args.render.pixel_per_inch),
76+
ppi_to_ppp(args.export.render.pixel_per_inch),
7677
);
7778
let doc = output?;
7879

79-
Test::create(paths, id, template, Some(Reference::Persistent(doc)))?;
80+
Test::create(
81+
paths,
82+
id,
83+
template,
84+
Some(Reference::Persistent(
85+
doc,
86+
args.export
87+
.no_optimize_references
88+
.not()
89+
.then(|| Box::new(DEFAULT_OPTIMIZE_OPTIONS.clone())),
90+
)),
91+
)?;
8092
};
8193
} else {
8294
Test::create_default(paths, id)?;

crates/typst-test-cli/src/cli/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,13 @@ pub struct ExportArgs {
392392
#[command(flatten)]
393393
pub render: RenderArgs,
394394

395-
/// Whether to save temporary output, such as ephemeral references
395+
/// Whether to skip saving temporary output, such as ephemeral references
396396
#[arg(long, global = true)]
397397
pub no_save_temporary: bool,
398+
399+
/// Whether to skip optimizing reference images
400+
#[arg(long, global = true)]
401+
pub no_optimize_references: bool,
398402
}
399403

400404
#[derive(clap::Args, Debug, Clone)]

crates/typst-test-cli/src/cli/run.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub fn run(ctx: &mut Context, args: &Args) -> eyre::Result<()> {
6060
&world,
6161
RunnerConfig {
6262
promote_warnings: args.compile.promote_warnings,
63+
optimize: !args.export.no_optimize_references,
6364
fail_fast: !args.run.no_fail_fast,
6465
pixel_per_pt: render::ppi_to_ppp(args.export.render.pixel_per_inch),
6566
action: Action::Run {

crates/typst-test-cli/src/cli/update.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn run(ctx: &mut Context, args: &Args) -> eyre::Result<()> {
4040
&world,
4141
RunnerConfig {
4242
promote_warnings: args.compile.promote_warnings,
43+
optimize: !args.export.no_optimize_references,
4344
fail_fast: !args.run.no_fail_fast,
4445
pixel_per_pt: render::ppi_to_ppp(args.export.render.pixel_per_inch),
4546
action: Action::Update {
@@ -66,13 +67,6 @@ pub fn run(ctx: &mut Context, args: &Args) -> eyre::Result<()> {
6667
);
6768
let result = runner.run(&reporter)?;
6869

69-
if project.vcs().is_some() {
70-
ctx.ui.warning_hinted(
71-
"Updated references are not compressed, but persisted in a repository",
72-
"Consider using a program like `oxipng` to reduce repository bloat",
73-
)?;
74-
}
75-
7670
if !result.is_complete_pass() {
7771
eyre::bail!(TestFailure);
7872
}

crates/typst-test-cli/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use clap::Parser;
88
use cli::Context;
99
use color_eyre::eyre;
1010
use lib::config::{Config, ConfigLayer};
11+
use once_cell::sync::Lazy;
1112
use termcolor::{StandardStream, WriteColor};
1213
use tracing::level_filters::LevelFilter;
1314
use tracing_subscriber::filter::Targets;
@@ -26,6 +27,10 @@ mod runner;
2627
mod ui;
2728
mod world;
2829

30+
/// The default optimization options to use.
31+
pub static DEFAULT_OPTIMIZE_OPTIONS: Lazy<oxipng::Options> =
32+
Lazy::new(oxipng::Options::max_compression);
33+
2934
fn main() -> ExitCode {
3035
match main_impl() {
3136
Ok(code) => code,

0 commit comments

Comments
 (0)