From 989d170dc7318ca3cbaa5b76760eb14f4e6a8605 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 28 Nov 2022 12:40:16 +0100 Subject: Fragments --- src/export/pdf/mod.rs | 2 +- src/export/pdf/outline.rs | 33 ++++++++++++--------------------- src/export/pdf/page.rs | 23 ++--------------------- 3 files changed, 15 insertions(+), 43 deletions(-) (limited to 'src/export') diff --git a/src/export/pdf/mod.rs b/src/export/pdf/mod.rs index 7e5a3c06..8f9d9637 100644 --- a/src/export/pdf/mod.rs +++ b/src/export/pdf/mod.rs @@ -12,7 +12,7 @@ use std::hash::Hash; use pdf_writer::types::Direction; use pdf_writer::{Finish, Name, PdfWriter, Ref, TextStr}; -use self::outline::{Heading, HeadingNode}; +use self::outline::HeadingNode; use self::page::Page; use crate::doc::{Document, Lang, Metadata}; use crate::font::Font; diff --git a/src/export/pdf/outline.rs b/src/export/pdf/outline.rs index add167b4..e7a356c1 100644 --- a/src/export/pdf/outline.rs +++ b/src/export/pdf/outline.rs @@ -4,43 +4,34 @@ use super::{AbsExt, PdfContext, RefExt}; use crate::geom::{Abs, Point}; use crate::util::EcoString; -/// A heading that can later be linked in the outline panel. +/// A heading in the outline panel. #[derive(Debug, Clone)] -pub struct Heading { +pub struct HeadingNode { pub content: EcoString, pub level: usize, pub position: Point, pub page: Ref, -} - -/// A node in the outline tree. -#[derive(Debug, Clone)] -pub struct HeadingNode { - pub heading: Heading, pub children: Vec, } impl HeadingNode { - pub fn leaf(heading: Heading) -> Self { - HeadingNode { heading, children: Vec::new() } - } - pub fn len(&self) -> usize { 1 + self.children.iter().map(Self::len).sum::() } - pub fn insert(&mut self, other: Heading, level: usize) -> bool { - if level >= other.level { + #[allow(unused)] + pub fn try_insert(&mut self, child: Self, level: usize) -> bool { + if level >= child.level { return false; } - if let Some(child) = self.children.last_mut() { - if child.insert(other.clone(), level + 1) { + if let Some(last) = self.children.last_mut() { + if last.try_insert(child.clone(), level + 1) { return true; } } - self.children.push(Self::leaf(other)); + self.children.push(child); true } } @@ -74,10 +65,10 @@ pub fn write_outline_item( outline.count(-(node.children.len() as i32)); } - outline.title(TextStr(&node.heading.content)); - outline.dest_direct().page(node.heading.page).xyz( - node.heading.position.x.to_f32(), - (node.heading.position.y + Abs::pt(3.0)).to_f32(), + outline.title(TextStr(&node.content)); + outline.dest_direct().page(node.page).xyz( + node.position.x.to_f32(), + (node.position.y + Abs::pt(3.0)).to_f32(), None, ); diff --git a/src/export/pdf/page.rs b/src/export/pdf/page.rs index 7c479425..fc714e7a 100644 --- a/src/export/pdf/page.rs +++ b/src/export/pdf/page.rs @@ -2,10 +2,8 @@ use pdf_writer::types::{ActionType, AnnotationType, ColorSpaceOperand}; use pdf_writer::writers::ColorSpace; use pdf_writer::{Content, Filter, Finish, Name, Rect, Ref, Str}; -use super::{ - deflate, AbsExt, EmExt, Heading, HeadingNode, PdfContext, RefExt, D65_GRAY, SRGB, -}; -use crate::doc::{Destination, Element, Frame, Group, Role, Text}; +use super::{deflate, AbsExt, EmExt, PdfContext, RefExt, D65_GRAY, SRGB}; +use crate::doc::{Destination, Element, Frame, Group, Text}; use crate::font::Font; use crate::geom::{ self, Abs, Color, Em, Geometry, Numeric, Paint, Point, Ratio, Shape, Size, Stroke, @@ -281,23 +279,6 @@ impl PageContext<'_, '_> { /// Encode a frame into the content stream. fn write_frame(ctx: &mut PageContext, frame: &Frame) { - if let Some(Role::Heading { level, outlined: true }) = frame.role() { - let heading = Heading { - position: Point::new(ctx.state.transform.tx, ctx.state.transform.ty), - content: frame.text(), - page: ctx.page_ref, - level: level.get(), - }; - - if let Some(last) = ctx.parent.heading_tree.last_mut() { - if !last.insert(heading.clone(), 1) { - ctx.parent.heading_tree.push(HeadingNode::leaf(heading)) - } - } else { - ctx.parent.heading_tree.push(HeadingNode::leaf(heading)) - } - } - for &(pos, ref element) in frame.elements() { let x = pos.x.to_f32(); let y = pos.y.to_f32(); -- cgit v1.2.3