From 7045f5facee2099074947a55d941c4dbbba6b076 Mon Sep 17 00:00:00 2001 From: Jack <34489450+fenjalien@users.noreply.github.com> Date: Sat, 19 Aug 2023 21:40:47 +0100 Subject: [PATCH] update to typst v0.7.0 --- compiler/Cargo.lock | 370 +++++++++++++++++++++++++++++++++++++++-- compiler/Cargo.toml | 4 +- compiler/src/lib.rs | 70 +------- compiler/src/paths.rs | 6 +- compiler/src/render.rs | 71 ++++++++ package.json | 4 +- 6 files changed, 445 insertions(+), 80 deletions(-) create mode 100644 compiler/src/render.rs diff --git a/compiler/Cargo.lock b/compiler/Cargo.lock index 9b434ff..2fb6360 100644 --- a/compiler/Cargo.lock +++ b/compiler/Cargo.lock @@ -91,6 +91,12 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" +[[package]] +name = "bit_field" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" + [[package]] name = "bitflags" version = "1.3.2" @@ -216,6 +222,55 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "csv" version = "1.2.2" @@ -263,6 +318,12 @@ dependencies = [ "serde", ] +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + [[package]] name = "enum-ordinalize" version = "3.1.13" @@ -282,6 +343,22 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "88bffebc5d80432c9b140ee17875ff173a8ab62faad5b257da912bd2f6c1c0a1" +[[package]] +name = "exr" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1e481eb11a482815d3e9d618db8c42a93207134662873809335a92327440c18" +dependencies = [ + "bit_field", + "flume", + "half", + "lebe", + "miniz_oxide", + "rayon-core", + "smallvec", + "zune-inflate", +] + [[package]] name = "fancy-regex" version = "0.7.1" @@ -327,6 +404,19 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +[[package]] +name = "flume" +version = "0.10.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1657b4441c3403d9f7b3409e47575237dac27b1b5726df654a6ecbf92f0f7577" +dependencies = [ + "futures-core", + "futures-sink", + "nanorand", + "pin-project", + "spin", +] + [[package]] name = "fnv" version = "1.0.7" @@ -354,6 +444,31 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures-core" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" + +[[package]] +name = "futures-sink" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" + +[[package]] +name = "getrandom" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + [[package]] name = "gif" version = "0.12.0" @@ -364,6 +479,15 @@ dependencies = [ "weezl", ] +[[package]] +name = "half" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b4af3693f1b705df946e9fe5631932443781d0aabb423b62fcd4d73f6d2fd0" +dependencies = [ + "crunchy", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -404,10 +528,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] -name = "hypher" -version = "0.1.1" +name = "hermit-abi" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0cf92443ef10ecfc1b8b4b65a93e31d983f020a355699d83874b12a7c797ac3" +checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" + +[[package]] +name = "hypher" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "927f74d31cdbfd8de201340c0bc474c52c9145193ccd8885fcede14005591aad" [[package]] name = "icu_collections" @@ -546,11 +676,14 @@ dependencies = [ "bytemuck", "byteorder", "color_quant", + "exr", "gif", "jpeg-decoder", "num-rational", "num-traits", "png", + "qoi", + "tiff", ] [[package]] @@ -567,6 +700,7 @@ checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown 0.12.3", + "serde", ] [[package]] @@ -599,6 +733,9 @@ name = "jpeg-decoder" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e" +dependencies = [ + "rayon", +] [[package]] name = "js-sys" @@ -624,6 +761,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lebe" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" + [[package]] name = "libc" version = "0.2.147" @@ -636,6 +779,15 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4" +[[package]] +name = "line-wrap" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30344350a2a51da54c1d53be93fade8a237e545dbcc4bdbe635413f2117cab9" +dependencies = [ + "safemem", +] + [[package]] name = "linked-hash-map" version = "0.5.6" @@ -658,6 +810,16 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a04a5b2b6f54acba899926491d0a6c59d98012938ca2ab5befb281c034e8f94" +[[package]] +name = "lock_api" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.19" @@ -679,6 +841,15 @@ dependencies = [ "libc", ] +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -689,6 +860,15 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "nanorand" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3" +dependencies = [ + "getrandom", +] + [[package]] name = "num-bigint" version = "0.4.3" @@ -731,6 +911,16 @@ dependencies = [ "libm", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "numerals" version = "0.1.4" @@ -755,6 +945,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "oklab" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "467e40ada50d13bab19019e3707862b5076ca15841f31ee1474c40397c1b9f11" +dependencies = [ + "rgb", +] + [[package]] name = "once_cell" version = "1.18.0" @@ -769,9 +968,9 @@ checksum = "b4b27ab7be369122c218afc2079489cdcb4b517c0a3fc386ff11e1fedfcc2b35" [[package]] name = "pdf-writer" -version = "0.7.1" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30900f178ea696fc5d9637171f98aaa93d5aae54f0726726df68fc3e32810db6" +checksum = "86af2eb3faa4614bc7fda8bd578c25e76a17ff3b1577be034b81e0c20527e204" dependencies = [ "bitflags 1.3.2", "itoa", @@ -808,6 +1007,26 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" +[[package]] +name = "pin-project" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.23", +] + [[package]] name = "pin-project-lite" version = "0.2.10" @@ -823,6 +1042,20 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "plist" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdc0001cfea3db57a2e24bc0d818e9e20e554b5f97fabb9bc231dc240269ae06" +dependencies = [ + "base64", + "indexmap 1.9.3", + "line-wrap", + "quick-xml", + "serde", + "time", +] + [[package]] name = "png" version = "0.17.9" @@ -870,6 +1103,24 @@ dependencies = [ "cc", ] +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "quick-xml" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81b9228215d82c7b61490fec1de287136b5de6f5700f6e58ea9ad61a7964ca51" +dependencies = [ + "memchr", +] + [[package]] name = "quote" version = "1.0.29" @@ -904,6 +1155,28 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +[[package]] +name = "rayon" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "rctree" version = "0.5.0" @@ -939,8 +1212,11 @@ version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "142e83d8ae8c8c639f304698a5567b229ba65caba867bf4387bbc0ae158827cf" dependencies = [ + "gif", + "jpeg-decoder", "log", "pico-args", + "png", "rgb", "svgtypes", "tiny-skia", @@ -1006,6 +1282,12 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe232bdf6be8c8de797b22184ee71118d63780ea42ac85b61d1baa6d3b782ae9" +[[package]] +name = "safemem" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" + [[package]] name = "same-file" version = "1.0.6" @@ -1015,6 +1297,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serde" version = "1.0.166" @@ -1114,6 +1402,15 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -1172,8 +1469,9 @@ checksum = "09eab8a83bff89ba2200bd4c59be45c7c787f988431b936099a5a266c957f2f9" [[package]] name = "svg2pdf" -version = "0.5.0" -source = "git+https://github.com/typst/svg2pdf#39daf9fc2ee84b62b0e3b174ff8c9017f655af6b" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c966e59fd4afd959edcc226687f751a7d05c94d0477cca1a4c2b15a7220f2b24" dependencies = [ "image", "miniz_oxide", @@ -1238,12 +1536,14 @@ dependencies = [ "fnv", "lazy_static", "once_cell", + "plist", "regex-syntax 0.6.29", "serde", "serde_derive", "serde_json", "thiserror", "walkdir", + "yaml-rust", ] [[package]] @@ -1266,6 +1566,17 @@ dependencies = [ "syn 2.0.23", ] +[[package]] +name = "tiff" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7449334f9ff2baf290d55d73983a7d6fa15e01198faef72af07e2a8db851e471" +dependencies = [ + "flate2", + "jpeg-decoder", + "weezl", +] + [[package]] name = "time" version = "0.3.22" @@ -1425,8 +1736,8 @@ checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" [[package]] name = "typst" -version = "0.6.0" -source = "git+https://github.com/typst/typst.git?tag=v0.6.0#2dfd44fedd99ab9414c17f358179e1c37e953f30" +version = "0.7.0" +source = "git+https://github.com/typst/typst.git?tag=v0.7.0#da8367e189b02918a8fe1a98fd3059fd11a82cd9" dependencies = [ "bitflags 2.3.3", "bytemuck", @@ -1439,6 +1750,7 @@ dependencies = [ "indexmap 1.9.3", "log", "miniz_oxide", + "oklab", "once_cell", "pdf-writer", "pixglyph", @@ -1457,6 +1769,7 @@ dependencies = [ "tracing", "ttf-parser", "typst-macros", + "typst-syntax", "unicode-general-category", "unicode-ident", "unicode-math-class", @@ -1468,8 +1781,8 @@ dependencies = [ [[package]] name = "typst-library" -version = "0.6.0" -source = "git+https://github.com/typst/typst.git?tag=v0.6.0#2dfd44fedd99ab9414c17f358179e1c37e953f30" +version = "0.7.0" +source = "git+https://github.com/typst/typst.git?tag=v0.7.0#da8367e189b02918a8fe1a98fd3059fd11a82cd9" dependencies = [ "az", "chinese-number", @@ -1507,8 +1820,8 @@ dependencies = [ [[package]] name = "typst-macros" -version = "0.6.0" -source = "git+https://github.com/typst/typst.git?tag=v0.6.0#2dfd44fedd99ab9414c17f358179e1c37e953f30" +version = "0.7.0" +source = "git+https://github.com/typst/typst.git?tag=v0.7.0#da8367e189b02918a8fe1a98fd3059fd11a82cd9" dependencies = [ "heck", "proc-macro2", @@ -1516,6 +1829,22 @@ dependencies = [ "syn 2.0.23", ] +[[package]] +name = "typst-syntax" +version = "0.7.0" +source = "git+https://github.com/typst/typst.git?tag=v0.7.0#da8367e189b02918a8fe1a98fd3059fd11a82cd9" +dependencies = [ + "comemo", + "ecow", + "once_cell", + "serde", + "tracing", + "unicode-ident", + "unicode-math-class", + "unicode-segmentation", + "unscanny", +] + [[package]] name = "unic-langid" version = "0.9.1" @@ -1702,6 +2031,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.87" @@ -1925,3 +2260,12 @@ dependencies = [ "syn 1.0.109", "synstructure", ] + +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index 437748f..e102766 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -12,8 +12,8 @@ crate-type = ["cdylib"] [dependencies] # Everything to do with Typst -typst = { git = "https://github.com/typst/typst.git", tag = "v0.6.0" } -typst-library = { git = "https://github.com/typst/typst.git", tag = "v0.6.0" } +typst = { git = "https://github.com/typst/typst.git", tag = "v0.7.0" } +typst-library = { git = "https://github.com/typst/typst.git", tag = "v0.7.0" } comemo = "0.3" siphasher = "0.3.10" diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index c9d116d..e68273d 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -3,24 +3,22 @@ use fast_image_resize as fr; use std::{ cell::{OnceCell, RefCell, RefMut}, collections::HashMap, - num::NonZeroU32, path::{Path, PathBuf}, - str::FromStr, }; use typst::{ diag::{EcoString, FileError, FileResult, PackageError, PackageResult}, - eval::{Datetime, Library}, - file::{FileId, PackageSpec}, + eval::{Bytes, Datetime, Library, Tracer}, font::{Font, FontBook}, - geom::{Color, RgbaColor}, syntax::Source, - util::{Bytes, PathExt}, + syntax::{FileId, PackageSpec}, + util::PathExt, World, }; -use wasm_bindgen::{prelude::*, Clamped}; +use wasm_bindgen::prelude::*; use web_sys::ImageData; mod paths; +mod render; use crate::paths::{PathHash, PathSlot}; @@ -102,62 +100,10 @@ impl SystemWorld { source: Ok(Source::new(self.main, text)), }, ); - - match typst::compile(self) { + let mut tracer = Tracer::default(); + match typst::compile(self, &mut tracer) { Ok(document) => { - let mut pixmap = typst::export::render( - &document.pages[0], - pixel_per_pt, - Color::Rgba(RgbaColor::from_str(&fill)?), - ); - - let width = pixmap.width(); - let height = pixmap.height(); - // Create src image - let mut src_image = fr::Image::from_slice_u8( - NonZeroU32::new(width).unwrap(), - NonZeroU32::new(height).unwrap(), - pixmap.data_mut(), - fr::PixelType::U8x4, - ) - .unwrap(); - - // Multiple RGB channels of source image by alpha channel - let alpha_mul_div = fr::MulDiv::default(); - alpha_mul_div - .multiply_alpha_inplace(&mut src_image.view_mut()) - .unwrap(); - - let dst_width = NonZeroU32::new(if display { - size - } else { - ((size as f32 / height as f32) * width as f32) as u32 - }) - .unwrap_or(NonZeroU32::MIN); - let dst_height = NonZeroU32::new(if display { - ((size as f32 / width as f32) * height as f32) as u32 - } else { - size - }) - .unwrap_or(NonZeroU32::MIN); - - // Create container for data of destination image - let mut dst_image = fr::Image::new(dst_width, dst_height, src_image.pixel_type()); - // Get mutable view of destination image data - let mut dst_view = dst_image.view_mut(); - - // Resize source image into buffer of destination image - self.resizer - .resize(&src_image.view(), &mut dst_view) - .unwrap(); - - alpha_mul_div.divide_alpha_inplace(&mut dst_view).unwrap(); - - ImageData::new_with_u8_clamped_array_and_sh( - Clamped(dst_image.buffer()), - dst_width.get(), - dst_height.get(), - ) + render::to_image(&mut self.resizer, document, size, display, fill, pixel_per_pt) } Err(errors) => Err(format!( "{:?}", diff --git a/compiler/src/paths.rs b/compiler/src/paths.rs index b34e86c..9c1c164 100644 --- a/compiler/src/paths.rs +++ b/compiler/src/paths.rs @@ -1,7 +1,11 @@ use std::{cell::OnceCell, hash::Hash, path::PathBuf}; use siphasher::sip128::{Hasher128, SipHasher13}; -use typst::{diag::FileResult, file::FileId, syntax::Source, util::Bytes}; +use typst::{ + diag::FileResult, + eval::Bytes, + syntax::{FileId, Source}, +}; /// Holds canonical data for all pahts pointing to the same entity. /// diff --git a/compiler/src/render.rs b/compiler/src/render.rs new file mode 100644 index 0000000..fc8f047 --- /dev/null +++ b/compiler/src/render.rs @@ -0,0 +1,71 @@ +use std::{num::NonZeroU32, str::FromStr}; + +use fast_image_resize as fr; +use fr::Resizer; +use typst::{ + doc::Document, + geom::{Color, RgbaColor}, +}; +use wasm_bindgen::Clamped; +use web_sys::ImageData; + +pub fn to_image( + resizer: &mut Resizer, + document: Document, + size: u32, + display: bool, + fill: String, + pixel_per_pt: f32, +) -> Result { + let mut pixmap = typst::export::render( + &document.pages[0], + pixel_per_pt, + Color::Rgba(RgbaColor::from_str(&fill)?), + ); + + let width = pixmap.width(); + let height = pixmap.height(); + // Create src image + let mut src_image = fr::Image::from_slice_u8( + NonZeroU32::new(width).unwrap(), + NonZeroU32::new(height).unwrap(), + pixmap.data_mut(), + fr::PixelType::U8x4, + ) + .unwrap(); + + // Multiple RGB channels of source image by alpha channel + let alpha_mul_div = fr::MulDiv::default(); + alpha_mul_div + .multiply_alpha_inplace(&mut src_image.view_mut()) + .unwrap(); + + let dst_width = NonZeroU32::new(if display { + size + } else { + ((size as f32 / height as f32) * width as f32) as u32 + }) + .unwrap_or(NonZeroU32::MIN); + let dst_height = NonZeroU32::new(if display { + ((size as f32 / width as f32) * height as f32) as u32 + } else { + size + }) + .unwrap_or(NonZeroU32::MIN); + + // Create container for data of destination image + let mut dst_image = fr::Image::new(dst_width, dst_height, src_image.pixel_type()); + // Get mutable view of destination image data + let mut dst_view = dst_image.view_mut(); + + // Resize source image into buffer of destination image + resizer.resize(&src_image.view(), &mut dst_view).unwrap(); + + alpha_mul_div.divide_alpha_inplace(&mut dst_view).unwrap(); + + return ImageData::new_with_u8_clamped_array_and_sh( + Clamped(dst_image.buffer()), + dst_width.get(), + dst_height.get(), + ); +} diff --git a/package.json b/package.json index 784ea74..a117fa0 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "scripts": { "build-dev": "node esbuild.config.mjs", "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", - "wasm": "wasm-pack build compiler --target web", - "wasm-dev": "wasm-pack build compiler --target web --dev", + "wasm": "wasm-pack build compiler --target web --out-dir ../pkg", + "wasm-dev": "wasm-pack build compiler --target web --dev --out-dir ../pkg", "wasm-build-dev": "npm run wasm-dev && npm run build-dev", "wasm-build": "npm run wasm && npm run build", "version": "node version-bump.mjs && git add manifest.json versions.json"