-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add font resolve support and builder api
- Loading branch information
Showing
3 changed files
with
113 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use fontdb::Database; | ||
use fontkit::FontKey; | ||
#[cfg(not(target_arch = "wasm32"))] | ||
use napi::bindgen_prelude::{Buffer, Either, Error as NapiError}; | ||
#[cfg(not(target_arch = "wasm32"))] | ||
use napi_derive::napi; | ||
use ouroboros::self_referencing; | ||
use roxmltree::Document; | ||
|
||
use crate::options::JsOptions; | ||
|
||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)] | ||
#[cfg_attr(not(target_arch = "wasm32"), napi)] | ||
#[ouroboros::self_referencing] | ||
pub struct ResvgBuilder { | ||
font_db: fontdb::Database, | ||
js_options: JsOptions, | ||
data: String, | ||
#[borrows(data)] | ||
#[covariant] | ||
doc: Document<'this>, | ||
} | ||
|
||
#[napi(js_name = "FontKey")] | ||
pub struct FontKeyWrapper(FontKey); | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
#[napi] | ||
impl ResvgBuilder { | ||
#[napi(constructor)] | ||
pub fn new_napi( | ||
svg: Either<String, Buffer>, | ||
options: Option<String>, | ||
) -> Result<ResvgBuilder, NapiError> { | ||
let js_options: JsOptions = options | ||
.and_then(|o| serde_json::from_str(o.as_str()).ok()) | ||
.unwrap_or_default(); | ||
let _ = env_logger::builder() | ||
.filter_level(js_options.log_level) | ||
.try_init(); | ||
let mut opts = js_options.to_usvg_options(); | ||
crate::options::tweak_usvg_options(&mut opts); | ||
let data = match svg { | ||
Either::A(a) => a, | ||
Either::B(b) => std::str::from_utf8(b.as_ref()) | ||
.map_err(|e| napi::Error::from_reason(format!("{}", e)))? | ||
.to_string(), | ||
}; | ||
ResvgBuilderTryBuilder { | ||
font_db: Database::new(), | ||
js_options, | ||
data, | ||
doc_builder: |input| Document::parse(input), | ||
} | ||
.try_build() | ||
.map_err(|e| napi::Error::from_reason(format!("{}", e))) | ||
} | ||
|
||
#[napi] | ||
pub fn texts_to_resolve(&self) -> Vec<FontKeyWrapper> { | ||
vec![] | ||
} | ||
|
||
// fn new_inner( | ||
// svg: &Either<String, Buffer>, | ||
// options: Option<String>, | ||
// ) -> Result<Resvg, NapiError> { | ||
// let opts_ref = opts.to_ref(); | ||
// // Parse the SVG string into a tree. | ||
// let tree = match svg { | ||
// Either::A(a) => usvg::Tree::from_str(a.as_str(), &opts_ref), | ||
// Either::B(b) => usvg::Tree::from_data(b.as_ref(), &opts_ref), | ||
// } | ||
// .map_err(|e| napi::Error::from_reason(format!("{}", e)))?; | ||
// Ok(Resvg { tree, js_options }) | ||
// } | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
#[wasm_bindgen] | ||
impl ResvgBuilder { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(svg: IStringOrBuffer, options: Option<String>) -> Result<Resvg, js_sys::Error> { | ||
let js_options: JsOptions = options | ||
.and_then(|o| serde_json::from_str(o.as_str()).ok()) | ||
.unwrap_or_default(); | ||
|
||
let mut opts = js_options.to_usvg_options(); | ||
options::tweak_usvg_options(&mut opts); | ||
let opts_ref = opts.to_ref(); | ||
let tree = if js_sys::Uint8Array::instanceof(&svg) { | ||
let uintarray = js_sys::Uint8Array::unchecked_from_js_ref(&svg); | ||
let svg_buffer = uintarray.to_vec(); | ||
usvg::Tree::from_data(&svg_buffer, &opts_ref).map_err(Error::from) | ||
} else if let Some(s) = svg.as_string() { | ||
usvg::Tree::from_str(s.as_str(), &opts_ref).map_err(Error::from) | ||
} else { | ||
Err(Error::InvalidInput) | ||
}?; | ||
Ok(Resvg { tree, js_options }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters