Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom font setting #112

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions charming/src/component/radar_coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::Serialize;
use crate::{
datatype::CompositeValue,
element::{
font_settings::{FontFamily, FontStyle, FontWeight},
AxisLabel, AxisLine, AxisTick, Color, Formatter, Padding, Shape, SplitArea, SplitLine,
},
};
Expand All @@ -23,13 +24,13 @@ pub struct RadarAxisName {
color: Option<Color>,

#[serde(skip_serializing_if = "Option::is_none")]
font_style: Option<String>,
font_style: Option<FontStyle>,

#[serde(skip_serializing_if = "Option::is_none")]
font_weight: Option<String>,
font_weight: Option<FontWeight>,

#[serde(skip_serializing_if = "Option::is_none")]
font_family: Option<String>,
font_family: Option<FontFamily>,

#[serde(skip_serializing_if = "Option::is_none")]
font_size: Option<f64>,
Expand Down Expand Up @@ -161,17 +162,17 @@ impl RadarAxisName {
self
}

pub fn font_style<S: Into<String>>(mut self, font_style: S) -> Self {
pub fn font_style<F: Into<FontStyle>>(mut self, font_style: F) -> Self {
self.font_style = Some(font_style.into());
self
}

pub fn font_weight<S: Into<String>>(mut self, font_weight: S) -> Self {
pub fn font_weight<F: Into<FontWeight>>(mut self, font_weight: F) -> Self {
self.font_weight = Some(font_weight.into());
self
}

pub fn font_family<S: Into<String>>(mut self, font_family: S) -> Self {
pub fn font_family<F: Into<FontFamily>>(mut self, font_family: F) -> Self {
self.font_family = Some(font_family.into());
self
}
Expand Down
33 changes: 32 additions & 1 deletion charming/src/element/axis_label.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use serde::Serialize;

use super::{color::Color, Formatter};
use super::{
color::Color,
font_settings::{FontFamily, FontStyle, FontWeight},
Formatter,
};

#[derive(Serialize, Debug, PartialEq, PartialOrd, Clone)]
#[serde(rename_all = "camelCase")]
Expand All @@ -11,6 +15,15 @@ pub struct AxisLabel {
#[serde(skip_serializing_if = "Option::is_none")]
distance: Option<f64>,

#[serde(skip_serializing_if = "Option::is_none")]
font_style: Option<FontStyle>,

#[serde(skip_serializing_if = "Option::is_none")]
font_weight: Option<FontWeight>,

#[serde(skip_serializing_if = "Option::is_none")]
font_family: Option<FontFamily>,

#[serde(skip_serializing_if = "Option::is_none")]
font_size: Option<f64>,

Expand Down Expand Up @@ -38,6 +51,9 @@ impl AxisLabel {
Self {
show: None,
distance: None,
font_style: None,
font_weight: None,
font_family: None,
font_size: None,
color: None,
formatter: None,
Expand All @@ -56,6 +72,21 @@ impl AxisLabel {
self
}

pub fn font_style<F: Into<FontStyle>>(mut self, font_style: F) -> Self {
self.font_style = Some(font_style.into());
self
}

pub fn font_weight<F: Into<FontWeight>>(mut self, font_weight: F) -> Self {
self.font_weight = Some(font_weight.into());
self
}

pub fn font_family<F: Into<FontFamily>>(mut self, font_family: F) -> Self {
self.font_family = Some(font_family.into());
self
}

pub fn font_size<F: Into<f64>>(mut self, font_size: F) -> Self {
self.font_size = Some(font_size.into());
self
Expand Down
139 changes: 139 additions & 0 deletions charming/src/element/font_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use serde::Serialize;

#[derive(Serialize, Debug, PartialEq, PartialOrd, Clone)]
#[serde(rename_all = "camelCase")]
pub enum FontStyle {
Normal,
Italic,
Oblique,
}

#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub enum FontWeight {
Normal,
Bold,
Bolder,
Lighter,
Number(i32),
Custom(String),
}
impl<S> From<S> for FontWeight
where
S: Into<String>,
{
fn from(s: S) -> Self {
let s = s.into();
match s.as_str() {
"normal" => FontWeight::Normal,
"bold" => FontWeight::Bold,
"bolder" => FontWeight::Bolder,
"lighter" => FontWeight::Lighter,
_ => FontWeight::Custom(s),
}
}
}

impl Serialize for FontWeight {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
FontWeight::Normal => serializer.serialize_str("normal"),
FontWeight::Bold => serializer.serialize_str("bold"),
FontWeight::Bolder => serializer.serialize_str("bolder"),
FontWeight::Lighter => serializer.serialize_str("lighter"),
FontWeight::Number(num) => serializer.serialize_i32(*num),
FontWeight::Custom(val) => serializer.serialize_str(val),
}
}
}

#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub enum FontFamily {
Serif,
SansSerif,
MonoSpace,
Cursive,
Fantasy,
Custom(String),
}

impl<S> From<S> for FontFamily
where
S: Into<String>,
{
fn from(s: S) -> Self {
let s = s.into();
match s.as_str() {
"serif" => FontFamily::Serif,
"sans-serif" => FontFamily::SansSerif,
"monospace" => FontFamily::MonoSpace,
"cursive" => FontFamily::Cursive,
"fantasy" => FontFamily::Fantasy,
_ => FontFamily::Custom(s),
}
}
}

impl Serialize for FontFamily {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
FontFamily::Serif => serializer.serialize_str("serif"),
FontFamily::SansSerif => serializer.serialize_str("sans-serif"),
FontFamily::MonoSpace => serializer.serialize_str("monospace"),
FontFamily::Cursive => serializer.serialize_str("cursive"),
FontFamily::Fantasy => serializer.serialize_str("fantasy"),
FontFamily::Custom(val) => serializer.serialize_str(val),
}
}
}

#[cfg(test)]
#[test]
fn font_style() {
let normal = serde_json::to_string(&FontStyle::Normal).unwrap();
let italic = serde_json::to_string(&FontStyle::Italic).unwrap();
let oblique = serde_json::to_string(&FontStyle::Oblique).unwrap();

assert_eq!("\"normal\"", normal);
assert_eq!("\"italic\"", italic);
assert_eq!("\"oblique\"", oblique);
}

#[test]
fn font_weight() {
let normal = serde_json::to_string(&FontWeight::Normal).unwrap();
let bold = serde_json::to_string(&FontWeight::Bold).unwrap();
let bolder = serde_json::to_string(&FontWeight::Bolder).unwrap();
let lighter = serde_json::to_string(&FontWeight::Lighter).unwrap();
let number = serde_json::to_string(&FontWeight::Number(100)).unwrap();
let custom = serde_json::to_string(&FontWeight::Custom("test".to_string())).unwrap();

assert_eq!("\"normal\"", normal);
assert_eq!("\"bold\"", bold);
assert_eq!("\"bolder\"", bolder);
assert_eq!("\"lighter\"", lighter);
assert_eq!("100", number);
assert_eq!("\"test\"", custom);
}

#[test]
fn font_family() {
let serif = serde_json::to_string(&FontFamily::Serif).unwrap();
let sans_serif = serde_json::to_string(&FontFamily::SansSerif).unwrap();
let monospace = serde_json::to_string(&FontFamily::MonoSpace).unwrap();
let cursive = serde_json::to_string(&FontFamily::Cursive).unwrap();
let fantasy = serde_json::to_string(&FontFamily::Fantasy).unwrap();
let custom = serde_json::to_string(&FontFamily::Custom("test".to_string())).unwrap();

assert_eq!("\"serif\"", serif);
assert_eq!("\"sans-serif\"", sans_serif);
assert_eq!("\"monospace\"", monospace);
assert_eq!("\"cursive\"", cursive);
assert_eq!("\"fantasy\"", fantasy);
assert_eq!("\"test\"", custom);
}
37 changes: 30 additions & 7 deletions charming/src/element/label.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use serde::Serialize;

use super::{color::Color, line_style::LineStyle, Formatter};
use super::{
color::Color,
font_settings::{FontFamily, FontStyle, FontWeight},
line_style::LineStyle,
Formatter,
};

#[derive(Serialize, Debug, PartialEq, PartialOrd, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -65,10 +70,16 @@ pub struct Label {
color: Option<Color>,

#[serde(skip_serializing_if = "Option::is_none")]
font_size: Option<f64>,
font_style: Option<FontStyle>,

#[serde(skip_serializing_if = "Option::is_none")]
font_weight: Option<FontWeight>,

#[serde(skip_serializing_if = "Option::is_none")]
font_weight: Option<String>,
font_family: Option<FontFamily>,

#[serde(skip_serializing_if = "Option::is_none")]
font_size: Option<f64>,

#[serde(skip_serializing_if = "Option::is_none")]
padding: Option<(f64, f64, f64, f64)>,
Expand Down Expand Up @@ -117,8 +128,10 @@ impl Label {
offset: None,
formatter: None,
color: None,
font_size: None,
font_style: None,
font_weight: None,
font_family: None,
font_size: None,
padding: None,
align: None,
vertical_align: None,
Expand Down Expand Up @@ -167,16 +180,26 @@ impl Label {
self
}

pub fn font_size<F: Into<f64>>(mut self, font_size: F) -> Self {
self.font_size = Some(font_size.into());
pub fn font_style<F: Into<FontStyle>>(mut self, font_style: F) -> Self {
self.font_style = Some(font_style.into());
self
}

pub fn font_weight<S: Into<String>>(mut self, font_weight: S) -> Self {
pub fn font_weight<F: Into<FontWeight>>(mut self, font_weight: F) -> Self {
self.font_weight = Some(font_weight.into());
self
}

pub fn font_family<F: Into<FontFamily>>(mut self, font_family: F) -> Self {
self.font_family = Some(font_family.into());
self
}

pub fn font_size<F: Into<f64>>(mut self, font_size: F) -> Self {
self.font_size = Some(font_size.into());
self
}

pub fn padding<F: Into<f64>>(mut self, padding: (F, F, F, F)) -> Self {
self.padding = Some((
padding.0.into(),
Expand Down
1 change: 1 addition & 0 deletions charming/src/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod coordinate_tooltip;
pub mod cursor;
pub mod dimension_encode;
pub mod emphasis;
pub mod font_settings;
pub mod formatter;
pub mod icon;
pub mod item_style;
Expand Down
17 changes: 10 additions & 7 deletions charming/src/element/text_style.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use serde::Serialize;

use super::color::Color;
use super::{
color::Color,
font_settings::{FontFamily, FontStyle, FontWeight},
};

#[derive(Serialize, Debug, PartialEq, PartialOrd, Clone)]
#[serde(rename_all = "camelCase")]
Expand All @@ -9,13 +12,13 @@ pub struct TextStyle {
color: Option<Color>,

#[serde(skip_serializing_if = "Option::is_none")]
font_style: Option<String>,
font_style: Option<FontStyle>,

#[serde(skip_serializing_if = "Option::is_none")]
font_weight: Option<String>,
font_weight: Option<FontWeight>,

#[serde(skip_serializing_if = "Option::is_none")]
font_family: Option<String>,
font_family: Option<FontFamily>,

#[serde(skip_serializing_if = "Option::is_none")]
font_size: Option<f64>,
Expand Down Expand Up @@ -55,17 +58,17 @@ impl TextStyle {
self
}

pub fn font_style<S: Into<String>>(mut self, font_style: S) -> Self {
pub fn font_style<F: Into<FontStyle>>(mut self, font_style: F) -> Self {
self.font_style = Some(font_style.into());
self
}

pub fn font_weight<S: Into<String>>(mut self, font_weight: S) -> Self {
pub fn font_weight<F: Into<FontWeight>>(mut self, font_weight: F) -> Self {
self.font_weight = Some(font_weight.into());
self
}

pub fn font_family<S: Into<String>>(mut self, font_family: S) -> Self {
pub fn font_family<F: Into<FontFamily>>(mut self, font_family: F) -> Self {
self.font_family = Some(font_family.into());
self
}
Expand Down
Loading
Loading