Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 29, 2024
1 parent f131a96 commit 469e234
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 31 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ fontdb = "0.21.0"
yoke = { version = "0.7.4", features = ["derive"] }
float-cmp = "0.9.0"

[patch.crates-io]
tiny-skia-path = {git = "https://github.com/RazrFalcon/tiny-skia", rev="eec0a47"}

[dev-dependencies]
difference = "2.0.0"
image = "0.25.1"
paste = "1.0.15"
rustybuzz = "0.18.0"
sitro = {git = "https://github.com/LaurenzV/sitro", rev = "b475ddc3"}
cosmic-text = {path = "../cosmic-text"}
cosmic-text = {git = "https://github.com/LaurenzV/cosmic-text", rev = "d8cad28"}


8 changes: 4 additions & 4 deletions src/font/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::chunk_container::ChunkContainer;
use crate::serialize::{Object, SerializerContext, SvgSettings};
use crate::surface::Surface;
use crate::type3_font::Type3ID;
use crate::util::Prehashed;
use crate::util::{Prehashed, RectWrapper};
use pdf_writer::{Chunk, Ref};
use skrifa::instance::Location;
use skrifa::outline::OutlinePen;
Expand Down Expand Up @@ -81,7 +81,7 @@ pub struct FontInfo {
checksum: u32,
location: LocationWrapper,
pub(crate) units_per_em: u16,
global_bbox: Rect,
global_bbox: RectWrapper,
postscript_name: Option<String>,
ascent: FiniteF32,
descent: FiniteF32,
Expand Down Expand Up @@ -168,7 +168,7 @@ impl FontInfo {
is_monospaced,
weight,
italic_angle,
global_bbox,
global_bbox: RectWrapper(global_bbox),
})
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ impl Font {
}

pub fn bbox(&self) -> Rect {
self.0.font_info.global_bbox
self.0.font_info.global_bbox.0
}

pub fn location_ref(&self) -> LocationRef {
Expand Down
8 changes: 4 additions & 4 deletions src/object/image.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::chunk_container::ChunkContainer;
use crate::object::color_space::DEVICE_GRAY;
use crate::serialize::{Object, SerializerContext};
use crate::util::{NameExt, Prehashed};
use crate::util::{NameExt, Prehashed, SizeWrapper};
use image::{ColorType, DynamicImage, Luma, Rgb, Rgba};
use pdf_writer::{Chunk, Finish, Name, Ref};
use std::sync::Arc;
Expand All @@ -10,7 +10,7 @@ use tiny_skia_path::Size;
#[derive(Debug, Hash, Eq, PartialEq)]
pub struct Repr {
image_data: Vec<u8>,
size: Size,
size: SizeWrapper,
mask_data: Option<Vec<u8>>,
color_type: ColorType,
}
Expand All @@ -31,12 +31,12 @@ impl Image {
image_data,
mask_data,
color_type,
size,
size: SizeWrapper(size),
})))
}

pub fn size(&self) -> Size {
self.0.size
self.0.size.0
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/object/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::surface::StreamBuilder;
use crate::transform::TransformWrapper;
use pdf_writer::{Chunk, Finish, Name, Ref};
use tiny_skia_path::Rect;
use crate::util::RectWrapper;

/// A mask.
#[derive(PartialEq, Eq, Debug, Hash, Clone)]
Expand All @@ -19,7 +20,7 @@ pub struct Mask {
/// transparencies, we create a custom mask where we call the shading operator. In this case,
/// we want to manually set the bbox of the underlying XObject to match the shape that the
/// gradient is being applied to.
custom_bbox: Option<Rect>,
custom_bbox: Option<RectWrapper>,
}

impl Mask {
Expand Down Expand Up @@ -67,7 +68,7 @@ impl Mask {
Some(Self {
stream: shading_stream,
mask_type: MaskType::Luminosity,
custom_bbox: Some(bbox),
custom_bbox: Some(RectWrapper(bbox)),
})
}
}
Expand Down Expand Up @@ -103,7 +104,7 @@ impl Object for Mask {
self.stream.clone(),
false,
true,
self.custom_bbox,
self.custom_bbox.map(|c| c.0),
));

let mut dict = chunk.indirect(root_ref).dict();
Expand Down
8 changes: 4 additions & 4 deletions src/object/shading_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::object::color_space::{Color, ColorSpace};
use crate::paint::SpreadMethod;
use crate::serialize::{Object, SerializerContext};
use crate::transform::TransformWrapper;
use crate::util::RectExt;
use crate::util::{RectExt, RectWrapper};
use crate::{LinearGradient, RadialGradient, SweepGradient};
use pdf_writer::types::FunctionShadingType;
use pdf_writer::{Chunk, Finish, Name, Ref};
Expand Down Expand Up @@ -36,7 +36,7 @@ pub struct PostScriptGradient {
pub min: FiniteF32,
pub max: FiniteF32,
pub stops: Vec<Stop>,
pub domain: Rect,
pub domain: RectWrapper,
pub spread_method: SpreadMethod,
pub gradient_type: GradientType,
}
Expand Down Expand Up @@ -110,7 +110,7 @@ where
.into_iter()
.map(|s| s.into())
.collect::<Vec<Stop>>(),
domain: get_expanded_bbox(bbox, self.transform.post_concat(ts)),
domain: RectWrapper(get_expanded_bbox(bbox, self.transform.post_concat(ts))),
spread_method: self.spread_method,
gradient_type: GradientType::Linear,
}),
Expand Down Expand Up @@ -141,7 +141,7 @@ where
.into_iter()
.map(|s| s.into())
.collect::<Vec<Stop>>(),
domain: get_expanded_bbox(bbox, transform),
domain: RectWrapper(get_expanded_bbox(bbox, transform)),
spread_method: self.spread_method,
gradient_type: GradientType::Sweep,
}),
Expand Down
10 changes: 5 additions & 5 deletions src/object/xobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::chunk_container::ChunkContainer;
use crate::serialize::{Object, SerializerContext};
use crate::stream::Stream;
use crate::util::RectExt;
use crate::util::{RectExt, RectWrapper};
use pdf_writer::{Chunk, Finish, Name, Ref};
use tiny_skia_path::Rect;

Expand All @@ -10,7 +10,7 @@ pub struct XObject {
stream: Stream,
isolated: bool,
transparency_group_color_space: bool,
custom_bbox: Option<Rect>,
custom_bbox: Option<RectWrapper>,
}

impl XObject {
Expand All @@ -24,12 +24,12 @@ impl XObject {
stream,
isolated,
transparency_group_color_space,
custom_bbox,
custom_bbox: custom_bbox.map(|c| RectWrapper(c)),
}
}

pub fn bbox(&self) -> Rect {
self.custom_bbox.unwrap_or(self.stream.bbox())
self.custom_bbox.map(|c| c.0).unwrap_or(self.stream.bbox())
}
}

Expand All @@ -53,7 +53,7 @@ impl Object for XObject {
self.stream
.resource_dictionary()
.to_pdf_resources(sc, &mut x_object);
x_object.bbox(self.custom_bbox.unwrap_or(self.stream.bbox()).to_pdf_rect());
x_object.bbox(self.custom_bbox.map(|c| c.0).unwrap_or(self.stream.bbox()).to_pdf_rect());

if self.isolated || self.transparency_group_color_space {
let mut group = x_object.group();
Expand Down
10 changes: 5 additions & 5 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::resource::{
};
use crate::serialize::{FontContainer, PDFGlyph, SerializerContext};
use crate::transform::TransformWrapper;
use crate::util::{calculate_stroke_bbox, LineCapExt, LineJoinExt, NameExt, RectExt, TransformExt};
use crate::util::{calculate_stroke_bbox, LineCapExt, LineJoinExt, NameExt, RectExt, RectWrapper, TransformExt};
use crate::{Fill, FillRule, LineCap, LineJoin, Paint, Stroke};
use float_cmp::approx_eq;
use pdf_writer::types::TextRenderingMode;
Expand All @@ -31,7 +31,7 @@ use tiny_skia_path::{FiniteF32, NormalizedF32, Path, PathSegment, Rect, Size, Tr
#[derive(Debug, Hash, Eq, PartialEq, Clone)]
struct Repr {
content: Vec<u8>,
bbox: Rect,
bbox: RectWrapper,
resource_dictionary: ResourceDictionary,
}

Expand All @@ -46,7 +46,7 @@ impl Stream {
) -> Self {
Self(Arc::new(Repr {
content,
bbox,
bbox: RectWrapper(bbox),
resource_dictionary,
}))
}
Expand All @@ -56,7 +56,7 @@ impl Stream {
}

pub(crate) fn bbox(&self) -> Rect {
self.0.bbox
self.0.bbox.0
}

pub(crate) fn resource_dictionary(&self) -> &ResourceDictionary {
Expand All @@ -66,7 +66,7 @@ impl Stream {
pub fn empty() -> Self {
Self(Arc::new(Repr {
content: vec![],
bbox: Rect::from_xywh(0.0, 0.0, 0.0, 0.0).unwrap(),
bbox: RectWrapper(Rect::from_xywh(0.0, 0.0, 0.0, 0.0).unwrap()),
resource_dictionary: ResourceDictionaryBuilder::new().finish(),
}))
}
Expand Down
45 changes: 44 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fmt;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use tiny_skia_path::{Path, PathBuilder, Rect};
use tiny_skia_path::{FiniteF32, Path, PathBuilder, Rect, Size};

pub trait NameExt {
fn to_pdf_name(&self) -> Name;
Expand Down Expand Up @@ -202,3 +202,46 @@ where
Some((key, head))
}
}

// TODO: Remove with new resvg release
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct RectWrapper(pub Rect);

impl Deref for RectWrapper {
type Target = Rect;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Eq for RectWrapper {}

impl Hash for RectWrapper {
fn hash<H: Hasher>(&self, state: &mut H) {
FiniteF32::new(self.0.left()).unwrap().hash(state);
FiniteF32::new(self.0.top()).unwrap().hash(state);
FiniteF32::new(self.0.right()).unwrap().hash(state);
FiniteF32::new(self.0.bottom()).unwrap().hash(state);
}
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub struct SizeWrapper(pub Size);

impl Deref for SizeWrapper {
type Target = Size;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Eq for SizeWrapper {}

impl Hash for SizeWrapper {
fn hash<H: Hasher>(&self, state: &mut H) {
FiniteF32::new(self.0.width()).unwrap().hash(state);
FiniteF32::new(self.0.height()).unwrap().hash(state);
}
}

0 comments on commit 469e234

Please sign in to comment.