mirror of
https://github.com/typst/typst
synced 2025-06-08 13:16:24 +08:00
more changes
This commit is contained in:
parent
bcfb889844
commit
4e4a4bdafb
@ -157,15 +157,19 @@ impl krilla::font::Glyph for PdfGlyph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct GlobalContext<'a> {
|
pub struct GlobalContext<'a> {
|
||||||
|
/// Cache the conversion between krilla and Typst fonts (forward and backward).
|
||||||
fonts_forward: HashMap<Font, krilla::font::Font>,
|
fonts_forward: HashMap<Font, krilla::font::Font>,
|
||||||
fonts_backward: HashMap<krilla::font::Font, Font>,
|
fonts_backward: HashMap<krilla::font::Font, Font>,
|
||||||
// Note: In theory, the same image can have multiple spans
|
// Note: In theory, the same image can have multiple spans
|
||||||
// if it appears in the document multiple times. We just store the
|
// if it appears in the document multiple times. We just store the
|
||||||
// first appearance, though.
|
// first appearance, though.
|
||||||
|
/// Mapping between images and their span.
|
||||||
image_spans: HashMap<krilla::image::Image, Span>,
|
image_spans: HashMap<krilla::image::Image, Span>,
|
||||||
pub(crate) document: &'a PagedDocument,
|
pub(crate) document: &'a PagedDocument,
|
||||||
pub(crate) options: &'a PdfOptions<'a>,
|
pub(crate) options: &'a PdfOptions<'a>,
|
||||||
|
/// 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.
|
||||||
languages: BTreeMap<Lang, usize>,
|
languages: BTreeMap<Lang, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
use std::num::NonZeroUsize;
|
use std::num::NonZeroUsize;
|
||||||
|
|
||||||
use krilla::destination::XyzDestination;
|
use krilla::destination::XyzDestination;
|
||||||
use krilla::outline::{Outline, OutlineNode};
|
use krilla::outline::{Outline, OutlineNode};
|
||||||
use typst_library::foundations::{NativeElement, Packed, StyleChain};
|
use typst_library::foundations::{NativeElement, Packed, StyleChain};
|
||||||
use typst_library::layout::Abs;
|
use typst_library::layout::Abs;
|
||||||
use typst_library::model::HeadingElem;
|
use typst_library::model::HeadingElem;
|
||||||
|
|
||||||
use crate::krilla::GlobalContext;
|
use crate::krilla::GlobalContext;
|
||||||
use crate::util::AbsExt;
|
use crate::util::AbsExt;
|
||||||
|
|
||||||
/// A heading in the outline panel.
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct HeadingNode<'a> {
|
struct HeadingNode<'a> {
|
||||||
element: &'a Packed<HeadingElem>,
|
element: &'a Packed<HeadingElem>,
|
||||||
@ -28,6 +29,30 @@ impl<'a> HeadingNode<'a> {
|
|||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn to_krilla(&self, gc: &GlobalContext) -> Option<OutlineNode> {
|
||||||
|
let loc = self.element.location().unwrap();
|
||||||
|
let title = self.element.body().plain_text().to_string();
|
||||||
|
let pos = gc.document.introspector.position(loc);
|
||||||
|
let page_index = pos.page.get() - 1;
|
||||||
|
|
||||||
|
if !gc.page_excluded(page_index) {
|
||||||
|
let y = (pos.point.y - Abs::pt(10.0)).max(Abs::zero());
|
||||||
|
let dest = XyzDestination::new(
|
||||||
|
page_index,
|
||||||
|
krilla::geom::Point::from_xy(pos.point.x.to_f32(), y.to_f32()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut outline_node = OutlineNode::new(title, dest);
|
||||||
|
for child in convert_nodes(&self.children, gc) {
|
||||||
|
outline_node.push_child(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Some(outline_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn build_outline(gc: &GlobalContext) -> Outline {
|
pub(crate) fn build_outline(gc: &GlobalContext) -> Outline {
|
||||||
@ -115,35 +140,13 @@ pub(crate) fn build_outline(gc: &GlobalContext) -> Outline {
|
|||||||
|
|
||||||
let mut outline = Outline::new();
|
let mut outline = Outline::new();
|
||||||
|
|
||||||
for child in convert_heading_nodes(&tree, gc) {
|
for child in convert_nodes(&tree, gc) {
|
||||||
outline.push_child(child);
|
outline.push_child(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
outline
|
outline
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_heading_nodes(nodes: &[HeadingNode], gc: &GlobalContext) -> Vec<OutlineNode> {
|
fn convert_nodes(nodes: &[HeadingNode], gc: &GlobalContext) -> Vec<OutlineNode> {
|
||||||
nodes.iter().flat_map(|node| {
|
nodes.iter().flat_map(|node| node.to_krilla(gc)).collect()
|
||||||
let loc = node.element.location().unwrap();
|
|
||||||
let title = node.element.body().plain_text().to_string();
|
|
||||||
let pos = gc.document.introspector.position(loc);
|
|
||||||
let page_index = pos.page.get() - 1;
|
|
||||||
|
|
||||||
if !gc.page_excluded(page_index) {
|
|
||||||
let y = (pos.point.y - Abs::pt(10.0)).max(Abs::zero());
|
|
||||||
let dest = XyzDestination::new(
|
|
||||||
page_index,
|
|
||||||
krilla::geom::Point::from_xy(pos.point.x.to_f32(), y.to_f32()),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut outline_node = OutlineNode::new(title, dest);
|
|
||||||
for child in convert_heading_nodes(&node.children, gc) {
|
|
||||||
outline_node.push_child(child);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Some(outline_node);
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}).collect()
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user