From 6cb9fe9064a037224b6560b69b441b72e787fa94 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 19 Mar 2021 22:36:13 +0100 Subject: =?UTF-8?q?Text=20colors=20=F0=9F=A6=A9=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/export/pdf.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'src/export') diff --git a/src/export/pdf.rs b/src/export/pdf.rs index 43c58403..d8391e2d 100644 --- a/src/export/pdf.rs +++ b/src/export/pdf.rs @@ -133,6 +133,22 @@ impl<'a> PdfExporter<'a> { // do that, we need to remember the active face. let mut face = FaceId::MAX; let mut size = Length::ZERO; + let mut fill: Option = None; + let mut change_color = |content: &mut Content, new_fill: Fill| { + if fill != Some(new_fill) { + match new_fill { + Fill::Color(Color::Rgba(c)) => { + content.fill_rgb( + c.r as f32 / 255.0, + c.g as f32 / 255.0, + c.b as f32 / 255.0, + ); + } + Fill::Image(_) => todo!(), + } + fill = Some(new_fill); + } + }; for (pos, element) in &page.elements { let x = pos.x.to_pt() as f32; @@ -152,17 +168,7 @@ impl<'a> PdfExporter<'a> { Element::Geometry(geometry) => { content.save_state(); - - match geometry.fill { - Fill::Color(Color::Rgba(c)) => { - content.fill_rgb( - c.r as f32 / 255.0, - c.g as f32 / 255.0, - c.b as f32 / 255.0, - ); - } - Fill::Image(_) => todo!(), - } + change_color(&mut content, geometry.fill); match &geometry.shape { Shape::Rect(r) => { @@ -179,9 +185,12 @@ impl<'a> PdfExporter<'a> { } Element::Text(shaped) => { + change_color(&mut content, shaped.color); + let mut text = content.text(); - // Check if we need to issue a font switching action. + // Then, also check if we need to + // issue a font switching action. if shaped.face != face || shaped.font_size != size { face = shaped.face; size = shaped.font_size; -- cgit v1.2.3