fix importing

system fonts do not load by default
image is drawn in a slightly better way
This commit is contained in:
Jack
2023-04-03 19:11:26 +01:00
parent 2b0507f076
commit 6995776fbd
3 changed files with 87 additions and 31 deletions

67
Cargo.lock generated
View File

@ -278,6 +278,12 @@ dependencies = [
"weezl",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hayagriva"
version = "0.2.1"
@ -343,6 +349,16 @@ dependencies = [
"png",
]
[[package]]
name = "indexmap"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "isolang"
version = "2.2.0"
@ -373,7 +389,7 @@ checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e"
[[package]]
name = "js-sys"
version = "0.3.61"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#525769565a99aebd74a78cdfb045eab0f903f888"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad927b4f886714ddfad2cd95"
dependencies = [
"wasm-bindgen",
]
@ -584,9 +600,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.54"
version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534"
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
dependencies = [
"unicode-ident",
]
@ -757,7 +773,7 @@ checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.11",
"syn 2.0.13",
]
[[package]]
@ -771,6 +787,18 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b"
dependencies = [
"indexmap",
"ryu",
"serde",
"yaml-rust",
]
[[package]]
name = "simplecss"
version = "0.2.1"
@ -860,9 +888,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.11"
version = "2.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21e3787bb71465627110e7d87ed4faaa36c1f61042ee67badb9e2ef173accc40"
checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec"
dependencies = [
"proc-macro2",
"quote",
@ -913,7 +941,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.11",
"syn 2.0.13",
]
[[package]]
@ -975,7 +1003,7 @@ checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a"
[[package]]
name = "typst"
version = "0.0.0"
source = "git+https://github.com/typst/typst.git#e13fc04c3e973da60d5f4e9cc6fc38105cd2ddf9"
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
dependencies = [
"bitflags",
"bytemuck",
@ -1012,7 +1040,7 @@ dependencies = [
[[package]]
name = "typst-library"
version = "0.0.0"
source = "git+https://github.com/typst/typst.git#e13fc04c3e973da60d5f4e9cc6fc38105cd2ddf9"
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
dependencies = [
"comemo",
"csv",
@ -1026,6 +1054,7 @@ dependencies = [
"roxmltree",
"rustybuzz",
"serde_json",
"serde_yaml",
"smallvec",
"syntect",
"ttf-parser 0.18.1",
@ -1041,7 +1070,7 @@ dependencies = [
[[package]]
name = "typst-macros"
version = "0.0.0"
source = "git+https://github.com/typst/typst.git#e13fc04c3e973da60d5f4e9cc6fc38105cd2ddf9"
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
dependencies = [
"heck",
"proc-macro2",
@ -1181,7 +1210,7 @@ dependencies = [
[[package]]
name = "wasm-bindgen"
version = "0.2.84"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#525769565a99aebd74a78cdfb045eab0f903f888"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad927b4f886714ddfad2cd95"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
@ -1190,21 +1219,21 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.84"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#525769565a99aebd74a78cdfb045eab0f903f888"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad927b4f886714ddfad2cd95"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.11",
"syn 2.0.13",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.34"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#525769565a99aebd74a78cdfb045eab0f903f888"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad927b4f886714ddfad2cd95"
dependencies = [
"cfg-if",
"js-sys",
@ -1215,7 +1244,7 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.84"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#525769565a99aebd74a78cdfb045eab0f903f888"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad927b4f886714ddfad2cd95"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -1224,11 +1253,11 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.84"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#525769565a99aebd74a78cdfb045eab0f903f888"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad927b4f886714ddfad2cd95"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.11",
"syn 2.0.13",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -1236,12 +1265,12 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.84"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#525769565a99aebd74a78cdfb045eab0f903f888"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad927b4f886714ddfad2cd95"
[[package]]
name = "web-sys"
version = "0.3.61"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#525769565a99aebd74a78cdfb045eab0f903f888"
source = "git+https://github.com/fenjalien/wasm-bindgen.git#584383028e0fdb4bad927b4f886714ddfad2cd95"
dependencies = [
"js-sys",
"wasm-bindgen",

34
main.ts
View File

@ -1,4 +1,4 @@
import { App, HexString, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { App, FileSystemAdapter, HexString, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
// @ts-ignore
@ -11,12 +11,14 @@ interface TypstPluginSettings {
noFill: boolean,
fill: HexString,
pixel_per_pt: number,
search_system: boolean,
}
const DEFAULT_SETTINGS: TypstPluginSettings = {
noFill: false,
fill: "#ffffff",
pixel_per_pt: 1
pixel_per_pt: 1,
search_system: false,
}
export default class TypstPlugin extends Plugin {
@ -25,26 +27,26 @@ export default class TypstPlugin extends Plugin {
async onload() {
await typstInit(typst_wasm_bin)
this.loadSettings()
let notice = new Notice("Loading fonts for Typst...")
this.compiler = await new typst.SystemWorld(this.app.vault.getRoot().path);
await this.loadSettings()
let notice = new Notice("Loading fonts for Typst...");
this.compiler = await new typst.SystemWorld((this.app.vault.adapter as FileSystemAdapter).getBasePath(), this.settings.search_system);
notice.hide();
notice = new Notice("Finished loading fonts for Typst", 5000);
this.addSettingTab(new TypstSettingTab(this.app, this));
this.registerMarkdownCodeBlockProcessor("typst", (source, el, ctx) => {
this.registerMarkdownCodeBlockProcessor("typst", async (source, el, ctx) => {
try {
const image = this.compiler.compile(source, this.settings.pixel_per_pt, `${this.settings.fill}${this.settings.noFill ? "00" : "ff"}`);
const bitmap = await createImageBitmap(image, { resizeWidth: 700, resizeHeight: image.height * (700 / image.width), resizeQuality: "high" })
let canvas = el.createEl("canvas", {
cls: "obsidian-typst",
attr: {
width: image.width,
height: image.height,
width: bitmap.width,
height: bitmap.height,
}
});
let ctx = canvas.getContext("2d");
ctx?.putImageData(image, 0, 0);
ctx?.drawImage(bitmap, 0, 0);
} catch (error) {
console.error(error);
}
@ -141,5 +143,17 @@ class TypstSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}
))
new Setting(containerEl)
.setName("Search System Fonts")
.setDesc(`Whether the plugin should search for system fonts.
This is off by default as it takes around 20 seconds to complete but it gives access to more fonts.
Requires reload of plugin.`)
.addToggle((toggle) => {
toggle.setValue(this.plugin.settings.search_system)
.onChange(async (value) => {
this.plugin.settings.search_system = value;
await this.plugin.saveSettings();
})
})
}
}

View File

@ -55,9 +55,13 @@ pub struct SystemWorld {
#[wasm_bindgen]
impl SystemWorld {
#[wasm_bindgen(constructor)]
pub async fn new(root: String) -> Result<SystemWorld, JsValue> {
pub async fn new(root: String, search_system: bool) -> Result<SystemWorld, JsValue> {
let mut searcher = FontSearcher::new();
searcher.search_system().await?;
if search_system {
searcher.search_system().await?;
} else {
searcher.add_embedded();
}
Ok(Self {
root: PathBuf::from(root),
@ -77,6 +81,11 @@ impl SystemWorld {
pixel_per_pt: f32,
fill: String,
) -> Result<ImageData, JsValue> {
self.sources.as_mut().clear();
self.hashes.borrow_mut().clear();
self.paths.borrow_mut().clear();
self.main = self.insert("<user input>".as_ref(), source);
match typst::compile(self) {
Ok(document) => {
@ -110,6 +119,8 @@ impl World for SystemWorld {
}
fn resolve(&self, path: &Path) -> FileResult<SourceId> {
let path = self.root.join(path);
let path = path.as_path();
self.slot(path)?
.source
.get_or_init(|| {
@ -136,6 +147,8 @@ impl World for SystemWorld {
}
fn file(&self, path: &Path) -> FileResult<Buffer> {
let path = self.root.join(path);
let path = path.as_path();
self.slot(path)?
.buffer
.get_or_init(|| read(path).map(Buffer::from))