Skip to content

Commit

Permalink
feat: support outlined border for table
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Mar 8, 2024
1 parent ca55ce8 commit 43e9cbe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ fontdue = "0.8.0"
image = { version = "0.24.8", features = ["webp-encoder", "avif-encoder"], optional = true }
once_cell = "1.19.0"
regex = "1.10.3"
resvg = { version = "0.38.0", default-features = false, features = [ "text", "system-fonts" ], optional = true }
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
snafu = "0.8.0"
resvg = { version = "0.40.0", default-features = false, features = [ "text", "system-fonts" ], optional = true }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
snafu = "0.8.1"
substring = "1.4.5"

[features]
Expand Down
1 change: 1 addition & 0 deletions asset/table_chart/basic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions src/charts/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,12 @@ impl Rect {
attrs.push((ATTR_STROKE_OPACITY, convert_opacity(&color)));
}
if let Some(color) = self.fill {
attrs.push((ATTR_FILL, color.hex()));
attrs.push((ATTR_FILL_OPACITY, convert_opacity(&color)));
if color.is_transparent() {
attrs.push((ATTR_FILL, "none".to_string()));
} else {
attrs.push((ATTR_FILL, color.hex()));
attrs.push((ATTR_FILL_OPACITY, convert_opacity(&color)));
}
}

SVGTag {
Expand Down
8 changes: 4 additions & 4 deletions src/charts/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use once_cell::sync::OnceCell;
use resvg::{tiny_skia, usvg};
use snafu::{ResultExt, Snafu};
use std::io::Cursor;
use usvg::{fontdb, TreeParsing, TreePostProc};
use usvg::fontdb;

#[derive(Debug, Snafu)]
pub enum Error {
Expand Down Expand Up @@ -39,9 +39,9 @@ pub(crate) fn get_or_init_fontdb(fonts: Option<Vec<&[u8]>>) -> &fontdb::Database

fn save_image(svg: &str, format: image::ImageOutputFormat) -> Result<Vec<u8>> {
let fontdb = get_or_init_fontdb(None);
let mut tree = usvg::Tree::from_str(svg, &usvg::Options::default()).context(ParseSnafu {})?;
tree.postprocess(usvg::PostProcessingSteps::default(), fontdb);
let pixmap_size = tree.size.to_int_size();
let tree =
usvg::Tree::from_str(svg, &usvg::Options::default(), fontdb).context(ParseSnafu {})?;
let pixmap_size = tree.size().to_int_size();
let mut pixmap =
tiny_skia::Pixmap::new(pixmap_size.width(), pixmap_size.height()).ok_or(Error::Size {
width: pixmap_size.width(),
Expand Down
17 changes: 16 additions & 1 deletion src/charts/table_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct TableChart {
pub spans: Vec<f32>,
pub text_aligns: Vec<Align>,
pub border_color: Color,
pub outlined: bool,

pub header_row_padding: Box,
pub header_row_height: f32,
Expand Down Expand Up @@ -217,6 +218,9 @@ impl TableChart {
if let Some(border_color) = get_color_from_value(&data, "border_color") {
self.border_color = border_color;
}
if let Some(outlined) = get_bool_from_value(&data, "outlined") {
self.outlined = outlined;
}
Ok(data)
}
/// Creates a table chart from json.
Expand Down Expand Up @@ -557,7 +561,17 @@ impl TableChart {
}
top += row_height;
}

if self.outlined {
c.rect(Rect {
color: Some(self.border_color),
fill: Some(Color::transparent()),
left: 1.0,
top: 0.0,
width: c.width() - 1.0,
height: top - 1.0,
..Default::default()
});
}
c.height = c.margin.top + top;
self.height = c.height;

Expand Down Expand Up @@ -602,6 +616,7 @@ mod tests {
background_color: Some("#3bb357".into()),
font_color: Some(("#fff").into()),
}];
table_chart.outlined = true;
assert_eq!(
include_str!("../../asset/table_chart/basic.svg"),
table_chart.svg().unwrap()
Expand Down

0 comments on commit 43e9cbe

Please sign in to comment.