diff --git a/Cargo.lock b/Cargo.lock index d08dc9775..7b12838fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3258,6 +3258,7 @@ dependencies = [ "comemo", "ecow", "flate2", + "hayro", "image", "ttf-parser", "typst-library", diff --git a/crates/typst-render/src/image.rs b/crates/typst-render/src/image.rs index 83fd71eff..89b87ce78 100644 --- a/crates/typst-render/src/image.rs +++ b/crates/typst-render/src/image.rs @@ -7,7 +7,7 @@ use tiny_skia::IntSize; use typst_library::foundations::Smart; use typst_library::layout::Size; use typst_library::text::{FontBook, FontStretch, FontStyle, FontVariant, FontWeight}; -use typst_library::visualize::{Image, ImageKind, ImageScaling}; +use typst_library::visualize::{Image, ImageKind, ImageScaling, PdfImage}; use crate::{AbsExt, State}; @@ -101,59 +101,62 @@ fn build_texture(image: &Image, w: u32, h: u32) -> Option> { texture } - ImageKind::Pdf(pdf) => { - let sf = pdf.standard_fonts().clone(); - - let select_standard_font = move |font: StandardFont| -> Option { - let bytes = match font { - StandardFont::Helvetica => sf.helvetica.normal.clone(), - StandardFont::HelveticaBold => sf.helvetica.bold.clone(), - StandardFont::HelveticaOblique => sf.helvetica.italic.clone(), - StandardFont::HelveticaBoldOblique => { - sf.helvetica.bold_italic.clone() - } - StandardFont::Courier => sf.courier.normal.clone(), - StandardFont::CourierBold => sf.courier.bold.clone(), - StandardFont::CourierOblique => sf.courier.italic.clone(), - StandardFont::CourierBoldOblique => sf.courier.bold_italic.clone(), - StandardFont::TimesRoman => sf.times.normal.clone(), - StandardFont::TimesBold => sf.times.bold.clone(), - StandardFont::TimesItalic => sf.times.italic.clone(), - StandardFont::TimesBoldItalic => sf.times.bold_italic.clone(), - StandardFont::ZapfDingBats => sf.zapf_dingbats.clone(), - StandardFont::Symbol => sf.symbol.clone(), - }; - - bytes.map(|d| { - let font_data: Arc + Send + Sync> = - Arc::new(d.clone()); - - font_data - }) - }; - - let interpreter_settings = InterpreterSettings { - font_resolver: Arc::new(move |query| match query { - FontQuery::Standard(s) => select_standard_font(*s), - FontQuery::Fallback(f) => { - select_standard_font(f.pick_standard_font()) - } - }), - warning_sink: Arc::new(|_| {}), - }; - let page = pdf.page(); - - let render_settings = RenderSettings { - x_scale: w as f32 / pdf.width(), - y_scale: h as f32 / pdf.height(), - width: Some(w as u16), - height: Some(h as u16), - }; - let hayro_pix = hayro::render(page, &interpreter_settings, &render_settings); - - sk::Pixmap::from_vec(hayro_pix.take_u8(), IntSize::from_wh(w, h)?)? - } + ImageKind::Pdf(pdf) => build_pdf_texture(pdf, w, h)?, }; Some(Arc::new(texture)) } + +fn build_pdf_texture(pdf: &PdfImage, w: u32, h: u32) -> Option { + let sf = pdf.standard_fonts().clone(); + + let select_standard_font = move |font: StandardFont| -> Option { + let bytes = match font { + StandardFont::Helvetica => sf.helvetica.normal.clone(), + StandardFont::HelveticaBold => sf.helvetica.bold.clone(), + StandardFont::HelveticaOblique => sf.helvetica.italic.clone(), + StandardFont::HelveticaBoldOblique => { + sf.helvetica.bold_italic.clone() + } + StandardFont::Courier => sf.courier.normal.clone(), + StandardFont::CourierBold => sf.courier.bold.clone(), + StandardFont::CourierOblique => sf.courier.italic.clone(), + StandardFont::CourierBoldOblique => sf.courier.bold_italic.clone(), + StandardFont::TimesRoman => sf.times.normal.clone(), + StandardFont::TimesBold => sf.times.bold.clone(), + StandardFont::TimesItalic => sf.times.italic.clone(), + StandardFont::TimesBoldItalic => sf.times.bold_italic.clone(), + StandardFont::ZapfDingBats => sf.zapf_dingbats.clone(), + StandardFont::Symbol => sf.symbol.clone(), + }; + + bytes.map(|d| { + let font_data: Arc + Send + Sync> = + Arc::new(d.clone()); + + font_data + }) + }; + + let interpreter_settings = InterpreterSettings { + font_resolver: Arc::new(move |query| match query { + FontQuery::Standard(s) => select_standard_font(*s), + FontQuery::Fallback(f) => { + select_standard_font(f.pick_standard_font()) + } + }), + warning_sink: Arc::new(|_| {}), + }; + let page = pdf.page(); + + let render_settings = RenderSettings { + x_scale: w as f32 / pdf.width(), + y_scale: h as f32 / pdf.height(), + width: Some(w as u16), + height: Some(h as u16), + }; + + let hayro_pix = hayro::render(page, &interpreter_settings, &render_settings); + + sk::Pixmap::from_vec(hayro_pix.take_u8(), IntSize::from_wh(w, h)?) +} diff --git a/crates/typst-svg/Cargo.toml b/crates/typst-svg/Cargo.toml index 5416621e5..e1984d54e 100644 --- a/crates/typst-svg/Cargo.toml +++ b/crates/typst-svg/Cargo.toml @@ -21,6 +21,7 @@ base64 = { workspace = true } comemo = { workspace = true } ecow = { workspace = true } flate2 = { workspace = true } +hayro = { workspace = true } image = { workspace = true } ttf-parser = { workspace = true } xmlparser = { workspace = true }