more changes

This commit is contained in:
Laurenz Stampfl 2024-12-17 13:25:56 +01:00
parent 4e4a4bdafb
commit e08ccce810
5 changed files with 90 additions and 91 deletions

View File

@ -1,3 +1,6 @@
use crate::metadata::build_metadata;
use crate::outline::build_outline;
use crate::page::PageLabelExt;
use crate::util::{display_font, AbsExt, PointExt, SizeExt, TransformExt}; use crate::util::{display_font, AbsExt, PointExt, SizeExt, TransformExt};
use crate::{paint, PdfOptions}; use crate::{paint, PdfOptions};
use bytemuck::TransparentWrapper; use bytemuck::TransparentWrapper;
@ -29,8 +32,6 @@ use typst_library::visualize::{
FillRule, Geometry, Image, ImageKind, Paint, Path, PathItem, Shape, FillRule, Geometry, Image, ImageKind, Paint, Path, PathItem, Shape,
}; };
use typst_syntax::Span; use typst_syntax::Span;
use crate::outline::build_outline;
use crate::page::PageLabelExt;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct State { pub(crate) struct State {
@ -170,7 +171,7 @@ pub struct GlobalContext<'a> {
/// Mapping between locations in the document and named destinations. /// Mapping between locations in the document and named destinations.
loc_to_named: HashMap<Location, NamedDestination>, loc_to_named: HashMap<Location, NamedDestination>,
/// The languages used throughout the document. /// The languages used throughout the document.
languages: BTreeMap<Lang, usize>, pub(crate) languages: BTreeMap<Lang, usize>,
} }
impl<'a> GlobalContext<'a> { impl<'a> GlobalContext<'a> {
@ -334,56 +335,8 @@ pub fn pdf(
} }
} }
let metadata = {
let creator = format!("Typst {}", env!("CARGO_PKG_VERSION"));
let mut metadata = krilla::metadata::Metadata::new()
.creator(creator)
.keywords(
typst_document
.info
.keywords
.iter()
.map(EcoString::to_string)
.collect(),
)
.authors(
typst_document.info.author.iter().map(EcoString::to_string).collect(),
);
let lang = gc.languages.iter().max_by_key(|(_, &count)| count).map(|(&l, _)| l);
if let Some(lang) = lang {
metadata = metadata.language(lang.as_str().to_string());
}
if let Some(title) = &typst_document.info.title {
metadata = metadata.title(title.to_string());
}
if let Some(subject) = &typst_document.info.description {
metadata = metadata.subject(subject.to_string());
}
if let Some(ident) = options.ident.custom() {
metadata = metadata.subject(ident.to_string());
}
let tz = typst_document.info.date.is_auto();
if let Some(date) = typst_document
.info
.date
.unwrap_or(options.timestamp)
.and_then(|d| krilla_date(d, tz))
{
metadata = metadata.modification_date(date).creation_date(date);
}
metadata
};
document.set_outline(build_outline(&gc)); document.set_outline(build_outline(&gc));
document.set_metadata(metadata); document.set_metadata(build_metadata(&gc));
match document.finish() { match document.finish() {
Ok(r) => Ok(r), Ok(r) => Ok(r),
@ -500,38 +453,6 @@ pub fn pdf(
} }
} }
fn krilla_date(datetime: Datetime, tz: bool) -> Option<krilla::metadata::DateTime> {
let year = datetime.year().filter(|&y| y >= 0)? as u16;
let mut krilla_date = krilla::metadata::DateTime::new(year);
if let Some(month) = datetime.month() {
krilla_date = krilla_date.month(month);
}
if let Some(day) = datetime.day() {
krilla_date = krilla_date.day(day);
}
if let Some(h) = datetime.hour() {
krilla_date = krilla_date.hour(h);
}
if let Some(m) = datetime.minute() {
krilla_date = krilla_date.minute(m);
}
if let Some(s) = datetime.second() {
krilla_date = krilla_date.second(s);
}
if tz {
krilla_date = krilla_date.utc_offset_hour(0).utc_offset_minute(0);
}
Some(krilla_date)
}
pub fn process_frame( pub fn process_frame(
fc: &mut FrameContext, fc: &mut FrameContext,
frame: &Frame, frame: &Frame,

View File

@ -2,10 +2,11 @@
mod image; mod image;
mod krilla; mod krilla;
mod paint; mod metadata;
mod page;
mod util;
mod outline; mod outline;
mod page;
mod paint;
mod util;
use typst_library::diag::SourceResult; use typst_library::diag::SourceResult;
use typst_library::foundations::{Datetime, Smart}; use typst_library::foundations::{Datetime, Smart};

View File

@ -0,0 +1,78 @@
use ecow::EcoString;
use krilla::metadata::Metadata;
use typst_library::foundations::Datetime;
use crate::krilla::GlobalContext;
pub(crate) fn build_metadata(gc: &GlobalContext) -> Metadata {
let creator = format!("Typst {}", env!("CARGO_PKG_VERSION"));
let mut metadata = Metadata::new()
.creator(creator)
.keywords(gc.document.info.keywords.iter().map(EcoString::to_string).collect())
.authors(gc.document.info.author.iter().map(EcoString::to_string).collect());
let lang = gc.languages.iter().max_by_key(|(_, &count)| count).map(|(&l, _)| l);
if let Some(lang) = lang {
metadata = metadata.language(lang.as_str().to_string());
}
if let Some(title) = &gc.document.info.title {
metadata = metadata.title(title.to_string());
}
if let Some(subject) = &gc.document.info.description {
metadata = metadata.subject(subject.to_string());
}
if let Some(ident) = gc.options.ident.custom() {
metadata = metadata.subject(ident.to_string());
}
let tz = gc.document.info.date.is_auto();
if let Some(date) = gc
.document
.info
.date
.unwrap_or(gc.options.timestamp)
.and_then(|d| convert_date(d, tz))
{
metadata = metadata.modification_date(date).creation_date(date);
}
metadata
}
// TODO: Sync with recent PR
fn convert_date(datetime: Datetime, tz: bool) -> Option<krilla::metadata::DateTime> {
let year = datetime.year().filter(|&y| y >= 0)? as u16;
let mut krilla_date = krilla::metadata::DateTime::new(year);
if let Some(month) = datetime.month() {
krilla_date = krilla_date.month(month);
}
if let Some(day) = datetime.day() {
krilla_date = krilla_date.day(day);
}
if let Some(h) = datetime.hour() {
krilla_date = krilla_date.hour(h);
}
if let Some(m) = datetime.minute() {
krilla_date = krilla_date.minute(m);
}
if let Some(s) = datetime.second() {
krilla_date = krilla_date.second(s);
}
if tz {
krilla_date = krilla_date.utc_offset_hour(0).utc_offset_minute(0);
}
Some(krilla_date)
}

View File

@ -56,4 +56,4 @@ impl PageLabelExt for PageLabel {
fn arabic(number: usize) -> PageLabel { fn arabic(number: usize) -> PageLabel {
PageLabel::new(Some(NumberingStyle::Arabic), None, NonZeroUsize::new(number)) PageLabel::new(Some(NumberingStyle::Arabic), None, NonZeroUsize::new(number))
} }
} }

View File

@ -1,8 +1,8 @@
//! Basic utilities for converting typst types to krilla. //! Basic utilities for converting typst types to krilla.
use krilla::color::rgb as kr;
use krilla::geom as kg; use krilla::geom as kg;
use krilla::path as kp; use krilla::path as kp;
use krilla::color::rgb as kr;
use typst_library::layout::{Abs, Point, Size, Transform}; use typst_library::layout::{Abs, Point, Size, Transform};
use typst_library::text::Font; use typst_library::text::Font;
@ -105,8 +105,7 @@ impl ColorExt for Color {
fn to_krilla_rgb(&self) -> (kr::Color, u8) { fn to_krilla_rgb(&self) -> (kr::Color, u8) {
let components = self.to_space(ColorSpace::Srgb).to_vec4_u8(); let components = self.to_space(ColorSpace::Srgb).to_vec4_u8();
( (
kr::Color::new(components[0], components[1], components[2]) kr::Color::new(components[0], components[1], components[2]).into(),
.into(),
components[3], components[3],
) )
} }