Skip to content

Commit 8866f9e

Browse files
committed
Make style control easier
1 parent 30456d9 commit 8866f9e

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/style/color.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<T: Color> Color for &'_ T {
6161

6262
/// The RGBA representation of the color, Plotters use RGBA as the internal representation
6363
/// of color
64-
#[derive(Copy, Clone, PartialEq, Debug)]
64+
#[derive(Copy, Clone, PartialEq, Debug, Default)]
6565
pub struct RGBAColor(pub(crate) u8, pub(crate) u8, pub(crate) u8, pub(crate) f64);
6666

6767
impl Color for RGBAColor {
@@ -95,7 +95,7 @@ impl<P: Palette> Color for PaletteColor<P> {
9595
}
9696

9797
/// The color described by its RGB value
98-
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
98+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
9999
pub struct RGBColor(pub u8, pub u8, pub u8);
100100

101101
impl BackendStyle for RGBAColor {

src/style/font/font_desc.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub struct FontDesc<'a> {
2424

2525
impl<'a> From<&'a str> for FontDesc<'a> {
2626
fn from(from: &'a str) -> FontDesc<'a> {
27-
FontDesc::new(from.into(), 1.0, FontStyle::Normal)
27+
FontDesc::new(from.into(), 12.0, FontStyle::Normal)
2828
}
2929
}
3030

3131
impl<'a> From<FontFamily<'a>> for FontDesc<'a> {
3232
fn from(family: FontFamily<'a>) -> FontDesc<'a> {
33-
FontDesc::new(family, 1.0, FontStyle::Normal)
33+
FontDesc::new(family, 12.0, FontStyle::Normal)
3434
}
3535
}
3636

@@ -40,6 +40,18 @@ impl<'a, T: Into<f64>> From<(FontFamily<'a>, T)> for FontDesc<'a> {
4040
}
4141
}
4242

43+
impl From<f64> for FontDesc<'static> {
44+
fn from(size: f64) -> FontDesc<'static> {
45+
FontDesc::new(FontFamily::SansSerif, size.into(), FontStyle::Normal)
46+
}
47+
}
48+
49+
impl From<u32> for FontDesc<'static> {
50+
fn from(size: u32) -> FontDesc<'static> {
51+
FontDesc::new(FontFamily::SansSerif, size.into(), FontStyle::Normal)
52+
}
53+
}
54+
4355
impl<'a, T: Into<f64>> From<(&'a str, T)> for FontDesc<'a> {
4456
fn from((typeface, size): (&'a str, T)) -> FontDesc<'a> {
4557
FontDesc::new(typeface.into(), size.into(), FontStyle::Normal)
@@ -91,7 +103,7 @@ impl<'a> FontDesc<'a> {
91103
///
92104
/// - `size`: The new size to set
93105
/// - **returns** The newly created font descriptor with a new size
94-
pub fn resize(&self, size: f64) -> FontDesc<'a> {
106+
pub fn resize(&self, size: f64) -> Self {
95107
Self {
96108
size,
97109
family: self.family,

src/style/font/ttf.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ mod test {
302302

303303
#[test]
304304
fn test_font_cache() -> FontResult<()> {
305-
306305
// We cannot only check the size of font cache, because
307306
// the test case may be run in parallel. Thus the font cache
308307
// may contains other fonts.

src/style/text.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,36 @@ impl<'a> IntoTextStyle<'a> for FontFamily<'a> {
3838
}
3939
}
4040

41+
impl<'a, T: Color> IntoTextStyle<'a> for &'a T {
42+
fn into_text_style<P: HasDimension>(self, _: &P) -> TextStyle<'a> {
43+
Into::<TextStyle>::into(FontFamily::SansSerif).color(self)
44+
}
45+
}
46+
4147
impl<'a, T: SizeDesc> IntoTextStyle<'a> for (&'a str, T) {
4248
fn into_text_style<P: HasDimension>(self, parent: &P) -> TextStyle<'a> {
4349
(self.0, self.1.in_pixels(parent)).into()
4450
}
4551
}
4652

53+
impl<'a, T: SizeDesc, C: Color> IntoTextStyle<'a> for (&'a str, T, &'a C) {
54+
fn into_text_style<P: HasDimension>(self, parent: &P) -> TextStyle<'a> {
55+
Into::<TextStyle>::into((self.0, self.1.in_pixels(parent))).color(self.2)
56+
}
57+
}
58+
4759
impl<'a, T: SizeDesc> IntoTextStyle<'a> for (FontFamily<'a>, T) {
4860
fn into_text_style<P: HasDimension>(self, parent: &P) -> TextStyle<'a> {
4961
(self.0, self.1.in_pixels(parent)).into()
5062
}
5163
}
5264

65+
impl<'a, T: SizeDesc, C: Color> IntoTextStyle<'a> for (FontFamily<'a>, T, &'a C) {
66+
fn into_text_style<P: HasDimension>(self, parent: &P) -> TextStyle<'a> {
67+
Into::<TextStyle>::into((self.0, self.1.in_pixels(parent))).color(self.2)
68+
}
69+
}
70+
5371
impl<'a, T: SizeDesc> IntoTextStyle<'a> for (&'a str, T, FontStyle) {
5472
fn into_text_style<P: HasDimension>(self, parent: &P) -> TextStyle<'a> {
5573
Into::<FontDesc>::into((self.0, self.1.in_pixels(parent), self.2)).into()

0 commit comments

Comments
 (0)