mirror of
https://github.com/typst/typst
synced 2025-07-27 14:27:56 +08:00
Embed all fonts
This commit is contained in:
parent
1a66c8ebe7
commit
d9fb17edb3
@ -273,8 +273,7 @@ impl Packed<ImageElem> {
|
||||
ImageFormat::Vector(VectorFormat::Pdf) => {
|
||||
if engine.world.library().features.is_enabled(Feature::PdfEmbedding) {
|
||||
let document =
|
||||
PdfDocument::new(loaded.data.clone(), engine.world.clone())
|
||||
.within(loaded)?;
|
||||
PdfDocument::new(loaded.data.clone()).within(loaded)?;
|
||||
let page_num = self.page.get(styles);
|
||||
|
||||
if page_num == 0 {
|
||||
|
@ -1,13 +1,9 @@
|
||||
use crate::diag::LoadResult;
|
||||
use crate::foundations::Bytes;
|
||||
use crate::text::{FontStretch, FontStyle, FontVariant, FontWeight};
|
||||
use crate::World;
|
||||
use comemo::Tracked;
|
||||
use hayro_syntax::page::Page;
|
||||
use hayro_syntax::Pdf;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::sync::Arc;
|
||||
use typst_library::text::FontInfo;
|
||||
|
||||
struct DocumentRepr {
|
||||
pdf: Arc<Pdf>,
|
||||
@ -29,10 +25,10 @@ impl PdfDocument {
|
||||
/// Load a PDF document.
|
||||
#[comemo::memoize]
|
||||
#[typst_macros::time(name = "load pdf document")]
|
||||
pub fn new(data: Bytes, world: Tracked<dyn World + '_>) -> LoadResult<PdfDocument> {
|
||||
pub fn new(data: Bytes) -> LoadResult<PdfDocument> {
|
||||
// TODO: Remove unwraps
|
||||
let pdf = Arc::new(Pdf::new(Arc::new(data.clone())).unwrap());
|
||||
let standard_fonts = get_standard_fonts(world.clone());
|
||||
let standard_fonts = get_standard_fonts();
|
||||
|
||||
Ok(Self(Arc::new(DocumentRepr { data, pdf, standard_fonts })))
|
||||
}
|
||||
@ -64,7 +60,6 @@ impl PdfImage {
|
||||
/// Create a new PDF image. Returns `None` if the page index is not valid.
|
||||
#[comemo::memoize]
|
||||
pub fn new(document: PdfDocument, page: usize) -> Option<PdfImage> {
|
||||
// TODO: Don't allow loading if pdf-embedding feature is disabled.
|
||||
// TODO: Remove Unwrap
|
||||
let dimensions = document.0.pdf.pages().get(page)?.render_dimensions();
|
||||
|
||||
@ -110,76 +105,45 @@ impl PdfImage {
|
||||
}
|
||||
|
||||
#[comemo::memoize]
|
||||
fn get_standard_fonts(world: Tracked<dyn World + '_>) -> Arc<StandardFonts> {
|
||||
let book = world.book();
|
||||
|
||||
let get_font = |name: &str, fallback_name: Option<&str>, variant: FontVariant| {
|
||||
book.select(name, variant)
|
||||
.or_else(|| {
|
||||
if let Some(fallback_name) = fallback_name {
|
||||
book.select(fallback_name, variant)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.or_else(|| book.select_fallback(None, variant, "A"))
|
||||
.and_then(|i| world.font(i))
|
||||
.map(|font| (font.data().clone(), font.index()))
|
||||
};
|
||||
|
||||
let normal_variant = FontVariant::new(
|
||||
FontStyle::Normal,
|
||||
FontWeight::default(),
|
||||
FontStretch::default(),
|
||||
);
|
||||
let bold_variant =
|
||||
FontVariant::new(FontStyle::Normal, FontWeight::BOLD, FontStretch::default());
|
||||
let italic_variant = FontVariant::new(
|
||||
FontStyle::Italic,
|
||||
FontWeight::default(),
|
||||
FontStretch::default(),
|
||||
);
|
||||
let bold_italic_variant =
|
||||
FontVariant::new(FontStyle::Italic, FontWeight::BOLD, FontStretch::default());
|
||||
|
||||
fn get_standard_fonts() -> Arc<StandardFonts> {
|
||||
let helvetica = VariantFont {
|
||||
normal: get_font("helvetica", Some("liberation sans"), normal_variant),
|
||||
bold: get_font("helvetica", Some("liberation sans"), bold_variant),
|
||||
italic: get_font("helvetica", Some("liberation sans"), italic_variant),
|
||||
bold_italic: get_font("helvetica", Some("liberation sans"), bold_italic_variant),
|
||||
normal: Bytes::new(typst_assets::pdf::SANS),
|
||||
bold: Bytes::new(typst_assets::pdf::SANS_BOLD),
|
||||
italic: Bytes::new(typst_assets::pdf::SANS_ITALIC),
|
||||
bold_italic: Bytes::new(typst_assets::pdf::SANS_BOLD_ITALIC),
|
||||
};
|
||||
|
||||
let courier = VariantFont {
|
||||
normal: get_font("courier", Some("liberation mono"), normal_variant),
|
||||
bold: get_font("courier", Some("liberation mono"), bold_variant),
|
||||
italic: get_font("courier", Some("liberation mono"), italic_variant),
|
||||
bold_italic: get_font("courier", Some("liberation mono"), bold_italic_variant),
|
||||
normal: Bytes::new(typst_assets::pdf::FIXED),
|
||||
bold: Bytes::new(typst_assets::pdf::FIXED_BOLD),
|
||||
italic: Bytes::new(typst_assets::pdf::FIXED_ITALIC),
|
||||
bold_italic: Bytes::new(typst_assets::pdf::FIXED_BOLD_ITALIC),
|
||||
};
|
||||
|
||||
let times = VariantFont {
|
||||
normal: get_font("times", Some("liberation serif"), normal_variant),
|
||||
bold: get_font("times", Some("liberation serif"), bold_variant),
|
||||
italic: get_font("times", Some("liberation serif"), italic_variant),
|
||||
bold_italic: get_font("times", Some("liberation serif"), bold_italic_variant),
|
||||
normal: Bytes::new(typst_assets::pdf::SERIF),
|
||||
bold: Bytes::new(typst_assets::pdf::SERIF_BOLD),
|
||||
italic: Bytes::new(typst_assets::pdf::SERIF_ITALIC),
|
||||
bold_italic: Bytes::new(typst_assets::pdf::SERIF_BOLD_ITALIC),
|
||||
};
|
||||
|
||||
let symbol = Some(Bytes::new(typst_assets::pdf::SYMBOL));
|
||||
let zapf_dingbats = Some(Bytes::new(typst_assets::pdf::DING_BATS));
|
||||
let symbol = Bytes::new(typst_assets::pdf::SYMBOL);
|
||||
let zapf_dingbats = Bytes::new(typst_assets::pdf::DING_BATS);
|
||||
|
||||
Arc::new(StandardFonts { helvetica, courier, times, symbol, zapf_dingbats })
|
||||
}
|
||||
|
||||
pub struct VariantFont {
|
||||
pub normal: Option<(Bytes, u32)>,
|
||||
pub bold: Option<(Bytes, u32)>,
|
||||
pub italic: Option<(Bytes, u32)>,
|
||||
pub bold_italic: Option<(Bytes, u32)>,
|
||||
pub normal: Bytes,
|
||||
pub bold: Bytes,
|
||||
pub italic: Bytes,
|
||||
pub bold_italic: Bytes,
|
||||
}
|
||||
|
||||
pub struct StandardFonts {
|
||||
pub helvetica: VariantFont,
|
||||
pub courier: VariantFont,
|
||||
pub times: VariantFont,
|
||||
pub symbol: Option<Bytes>,
|
||||
pub zapf_dingbats: Option<Bytes>,
|
||||
pub symbol: Bytes,
|
||||
pub zapf_dingbats: Bytes,
|
||||
}
|
||||
|
@ -124,15 +124,13 @@ fn build_pdf_texture(pdf: &PdfImage, w: u32, h: u32) -> Option<sk::Pixmap> {
|
||||
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().map(|d| (d, 0)),
|
||||
StandardFont::Symbol => sf.symbol.clone().map(|d| (d, 0)),
|
||||
StandardFont::ZapfDingBats => sf.zapf_dingbats.clone(),
|
||||
StandardFont::Symbol => sf.symbol.clone(),
|
||||
};
|
||||
|
||||
bytes.map(|d| {
|
||||
let font_data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(d.0.clone());
|
||||
let font_data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(bytes.clone());
|
||||
|
||||
(font_data, d.1)
|
||||
})
|
||||
Some((font_data, 0))
|
||||
};
|
||||
|
||||
let interpreter_settings = InterpreterSettings {
|
||||
|
@ -116,15 +116,13 @@ fn pdf_to_png(pdf: &PdfImage, w: u32, h: u32) -> Vec<u8> {
|
||||
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().map(|d| (d, 0)),
|
||||
StandardFont::Symbol => sf.symbol.clone().map(|d| (d, 0)),
|
||||
StandardFont::ZapfDingBats => sf.zapf_dingbats.clone(),
|
||||
StandardFont::Symbol => sf.symbol.clone(),
|
||||
};
|
||||
|
||||
bytes.map(|d| {
|
||||
let font_data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(d.0.clone());
|
||||
let font_data: Arc<dyn AsRef<[u8]> + Send + Sync> = Arc::new(bytes.clone());
|
||||
|
||||
(font_data, d.1)
|
||||
})
|
||||
Some((font_data, 0))
|
||||
};
|
||||
|
||||
let interpreter_settings = InterpreterSettings {
|
||||
|
Loading…
x
Reference in New Issue
Block a user