mirror of
https://github.com/mii443/obsidian-typst.git
synced 2025-08-23 00:25:43 +00:00
fix importing
system fonts do not load by default image is drawn in a slightly better way
This commit is contained in:
67
Cargo.lock
generated
67
Cargo.lock
generated
@ -278,6 +278,12 @@ dependencies = [
|
|||||||
"weezl",
|
"weezl",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.12.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hayagriva"
|
name = "hayagriva"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
@ -343,6 +349,16 @@ dependencies = [
|
|||||||
"png",
|
"png",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "indexmap"
|
||||||
|
version = "1.9.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "isolang"
|
name = "isolang"
|
||||||
version = "2.2.0"
|
version = "2.2.0"
|
||||||
@ -373,7 +389,7 @@ checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.61"
|
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 = [
|
dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
@ -584,9 +600,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.54"
|
version = "1.0.56"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534"
|
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
@ -757,7 +773,7 @@ checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.11",
|
"syn 2.0.13",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -771,6 +787,18 @@ dependencies = [
|
|||||||
"serde",
|
"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]]
|
[[package]]
|
||||||
name = "simplecss"
|
name = "simplecss"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
@ -860,9 +888,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.11"
|
version = "2.0.13"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "21e3787bb71465627110e7d87ed4faaa36c1f61042ee67badb9e2ef173accc40"
|
checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -913,7 +941,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.11",
|
"syn 2.0.13",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -975,7 +1003,7 @@ checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "typst"
|
name = "typst"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
source = "git+https://github.com/typst/typst.git#e13fc04c3e973da60d5f4e9cc6fc38105cd2ddf9"
|
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
@ -1012,7 +1040,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "typst-library"
|
name = "typst-library"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
source = "git+https://github.com/typst/typst.git#e13fc04c3e973da60d5f4e9cc6fc38105cd2ddf9"
|
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"comemo",
|
"comemo",
|
||||||
"csv",
|
"csv",
|
||||||
@ -1026,6 +1054,7 @@ dependencies = [
|
|||||||
"roxmltree",
|
"roxmltree",
|
||||||
"rustybuzz",
|
"rustybuzz",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"serde_yaml",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"syntect",
|
"syntect",
|
||||||
"ttf-parser 0.18.1",
|
"ttf-parser 0.18.1",
|
||||||
@ -1041,7 +1070,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "typst-macros"
|
name = "typst-macros"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
source = "git+https://github.com/typst/typst.git#e13fc04c3e973da60d5f4e9cc6fc38105cd2ddf9"
|
source = "git+https://github.com/typst/typst.git#2c735294cd5e47f1f1eb6402a3b8c500dccd047b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
@ -1181,7 +1210,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen"
|
name = "wasm-bindgen"
|
||||||
version = "0.2.84"
|
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 = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"wasm-bindgen-macro",
|
"wasm-bindgen-macro",
|
||||||
@ -1190,21 +1219,21 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-backend"
|
name = "wasm-bindgen-backend"
|
||||||
version = "0.2.84"
|
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 = [
|
dependencies = [
|
||||||
"bumpalo",
|
"bumpalo",
|
||||||
"log",
|
"log",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.11",
|
"syn 2.0.13",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-futures"
|
name = "wasm-bindgen-futures"
|
||||||
version = "0.4.34"
|
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 = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
@ -1215,7 +1244,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-macro"
|
name = "wasm-bindgen-macro"
|
||||||
version = "0.2.84"
|
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 = [
|
dependencies = [
|
||||||
"quote",
|
"quote",
|
||||||
"wasm-bindgen-macro-support",
|
"wasm-bindgen-macro-support",
|
||||||
@ -1224,11 +1253,11 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-macro-support"
|
name = "wasm-bindgen-macro-support"
|
||||||
version = "0.2.84"
|
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 = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn 2.0.11",
|
"syn 2.0.13",
|
||||||
"wasm-bindgen-backend",
|
"wasm-bindgen-backend",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
@ -1236,12 +1265,12 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-shared"
|
name = "wasm-bindgen-shared"
|
||||||
version = "0.2.84"
|
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]]
|
[[package]]
|
||||||
name = "web-sys"
|
name = "web-sys"
|
||||||
version = "0.3.61"
|
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 = [
|
dependencies = [
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
|
34
main.ts
34
main.ts
@ -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
|
// @ts-ignore
|
||||||
@ -11,12 +11,14 @@ interface TypstPluginSettings {
|
|||||||
noFill: boolean,
|
noFill: boolean,
|
||||||
fill: HexString,
|
fill: HexString,
|
||||||
pixel_per_pt: number,
|
pixel_per_pt: number,
|
||||||
|
search_system: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_SETTINGS: TypstPluginSettings = {
|
const DEFAULT_SETTINGS: TypstPluginSettings = {
|
||||||
noFill: false,
|
noFill: false,
|
||||||
fill: "#ffffff",
|
fill: "#ffffff",
|
||||||
pixel_per_pt: 1
|
pixel_per_pt: 1,
|
||||||
|
search_system: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class TypstPlugin extends Plugin {
|
export default class TypstPlugin extends Plugin {
|
||||||
@ -25,26 +27,26 @@ export default class TypstPlugin extends Plugin {
|
|||||||
|
|
||||||
async onload() {
|
async onload() {
|
||||||
await typstInit(typst_wasm_bin)
|
await typstInit(typst_wasm_bin)
|
||||||
this.loadSettings()
|
await this.loadSettings()
|
||||||
let notice = new Notice("Loading fonts for Typst...")
|
let notice = new Notice("Loading fonts for Typst...");
|
||||||
this.compiler = await new typst.SystemWorld(this.app.vault.getRoot().path);
|
this.compiler = await new typst.SystemWorld((this.app.vault.adapter as FileSystemAdapter).getBasePath(), this.settings.search_system);
|
||||||
notice.hide();
|
notice.hide();
|
||||||
notice = new Notice("Finished loading fonts for Typst", 5000);
|
notice = new Notice("Finished loading fonts for Typst", 5000);
|
||||||
|
|
||||||
this.addSettingTab(new TypstSettingTab(this.app, this));
|
this.addSettingTab(new TypstSettingTab(this.app, this));
|
||||||
this.registerMarkdownCodeBlockProcessor("typst", (source, el, ctx) => {
|
this.registerMarkdownCodeBlockProcessor("typst", async (source, el, ctx) => {
|
||||||
try {
|
try {
|
||||||
const image = this.compiler.compile(source, this.settings.pixel_per_pt, `${this.settings.fill}${this.settings.noFill ? "00" : "ff"}`);
|
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", {
|
let canvas = el.createEl("canvas", {
|
||||||
cls: "obsidian-typst",
|
cls: "obsidian-typst",
|
||||||
attr: {
|
attr: {
|
||||||
width: image.width,
|
width: bitmap.width,
|
||||||
height: image.height,
|
height: bitmap.height,
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
let ctx = canvas.getContext("2d");
|
let ctx = canvas.getContext("2d");
|
||||||
ctx?.putImageData(image, 0, 0);
|
ctx?.drawImage(bitmap, 0, 0);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
@ -141,5 +143,17 @@ class TypstSettingTab extends PluginSettingTab {
|
|||||||
await this.plugin.saveSettings();
|
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();
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17
src/lib.rs
17
src/lib.rs
@ -55,9 +55,13 @@ pub struct SystemWorld {
|
|||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl SystemWorld {
|
impl SystemWorld {
|
||||||
#[wasm_bindgen(constructor)]
|
#[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();
|
let mut searcher = FontSearcher::new();
|
||||||
searcher.search_system().await?;
|
if search_system {
|
||||||
|
searcher.search_system().await?;
|
||||||
|
} else {
|
||||||
|
searcher.add_embedded();
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
root: PathBuf::from(root),
|
root: PathBuf::from(root),
|
||||||
@ -77,6 +81,11 @@ impl SystemWorld {
|
|||||||
pixel_per_pt: f32,
|
pixel_per_pt: f32,
|
||||||
fill: String,
|
fill: String,
|
||||||
) -> Result<ImageData, JsValue> {
|
) -> 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);
|
self.main = self.insert("<user input>".as_ref(), source);
|
||||||
match typst::compile(self) {
|
match typst::compile(self) {
|
||||||
Ok(document) => {
|
Ok(document) => {
|
||||||
@ -110,6 +119,8 @@ impl World for SystemWorld {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn resolve(&self, path: &Path) -> FileResult<SourceId> {
|
fn resolve(&self, path: &Path) -> FileResult<SourceId> {
|
||||||
|
let path = self.root.join(path);
|
||||||
|
let path = path.as_path();
|
||||||
self.slot(path)?
|
self.slot(path)?
|
||||||
.source
|
.source
|
||||||
.get_or_init(|| {
|
.get_or_init(|| {
|
||||||
@ -136,6 +147,8 @@ impl World for SystemWorld {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn file(&self, path: &Path) -> FileResult<Buffer> {
|
fn file(&self, path: &Path) -> FileResult<Buffer> {
|
||||||
|
let path = self.root.join(path);
|
||||||
|
let path = path.as_path();
|
||||||
self.slot(path)?
|
self.slot(path)?
|
||||||
.buffer
|
.buffer
|
||||||
.get_or_init(|| read(path).map(Buffer::from))
|
.get_or_init(|| read(path).map(Buffer::from))
|
||||||
|
Reference in New Issue
Block a user