text_style/
genpdf.rs

1// SPDX-FileCopyrightText: 2020 Robin Krahl <robin.krahl@ireas.org>
2// SPDX-License-Identifier: Apache-2.0 or MIT
3
4//! Conversion methods for [`genpdf`][]’s text style types.
5//!
6//! *Requires the `genpdf` feature.*
7//!
8//! This module implements these conversions:
9//! - [`Color`][] to [`genpdf::style::Color`][]
10//! - [`Style`][] to [`genpdf::style::Style`][]
11//! - [`StyledStr`][] and [`StyledString`][] to [`genpdf::style::StyledStr`][] and
12//!   [`genpdf::style::StyledString`][]
13//!
14//! # Example
15//!
16//! Adding a string to a paragraph:
17//!
18//! ```
19//! let text = text_style::StyledStr::plain("test").bold();
20//! let p = genpdf::elements::Paragraph::new(text);
21//! ```
22//!
23//! [`genpdf`]: https://docs.rs/genpdf
24//! [`genpdf::style::Color`]: https://docs.rs/genpdf/latest/genpdf/style/enum.Color.html
25//! [`genpdf::style::Style`]: https://docs.rs/genpdf/latest/genpdf/style/struct.Style.html
26//! [`genpdf::style::StyledStr`]: https://docs.rs/genpdf/latest/genpdf/style/struct.StyledStr.html
27//! [`genpdf::style::StyledString`]: https://docs.rs/genpdf/latest/genpdf/style/struct.StyledString.html
28//! [`Color`]: ../enum.Color.html
29//! [`Style`]: ../struct.Style.html
30//! [`StyledStr`]: ../struct.StyledStr.html
31//! [`StyledString`]: ../struct.StyledString.html
32
33use genpdf::style;
34
35use crate::{AnsiColor, AnsiMode, Color, Style, StyledStr, StyledString};
36
37impl From<Color> for style::Color {
38    fn from(c: Color) -> style::Color {
39        match c {
40            Color::Ansi { color, mode } => get_rgb_color(color, mode),
41            Color::Rgb { r, g, b } => style::Color::Rgb(r, g, b),
42        }
43    }
44}
45
46fn get_rgb_color(color: AnsiColor, mode: AnsiMode) -> style::Color {
47    use AnsiColor::*;
48    use AnsiMode::*;
49
50    let (r, g, b) = match (mode, color) {
51        (Dark, Black) => (0, 0, 0),
52        (Dark, Red) => (170, 0, 0),
53        (Dark, Green) => (0, 170, 0),
54        (Dark, Yellow) => (170, 85, 0),
55        (Dark, Blue) => (0, 0, 170),
56        (Dark, Magenta) => (170, 0, 170),
57        (Dark, Cyan) => (0, 170, 170),
58        (Dark, White) => (170, 170, 170),
59        (Light, Black) => (85, 85, 85),
60        (Light, Red) => (255, 85, 85),
61        (Light, Green) => (85, 255, 85),
62        (Light, Yellow) => (255, 255, 85),
63        (Light, Blue) => (85, 85, 255),
64        (Light, Magenta) => (255, 85, 255),
65        (Light, Cyan) => (85, 255, 255),
66        (Light, White) => (255, 255, 255),
67    };
68    style::Color::Rgb(r, g, b)
69}
70
71impl From<Style> for style::Style {
72    fn from(s: Style) -> style::Style {
73        let mut style = style::Style::new();
74        if let Some(color) = s.fg {
75            style.set_color(color.into());
76        }
77        if s.effects.is_bold {
78            style.set_bold();
79        }
80        if s.effects.is_italic {
81            style.set_italic();
82        }
83        style
84    }
85}
86
87impl<'a, 's> From<&'a StyledStr<'s>> for style::StyledStr<'s> {
88    fn from(s: &'a StyledStr<'s>) -> style::StyledStr<'s> {
89        style::StyledStr::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
90    }
91}
92
93impl<'s> From<StyledStr<'s>> for style::StyledStr<'s> {
94    fn from(s: StyledStr<'s>) -> style::StyledStr<'s> {
95        style::StyledStr::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
96    }
97}
98
99impl<'s> From<&'s StyledString> for style::StyledStr<'s> {
100    fn from(s: &'s StyledString) -> style::StyledStr<'s> {
101        style::StyledStr::new(&s.s, s.style.map(style::Style::from).unwrap_or_default())
102    }
103}
104
105impl<'a, 's> From<&'a StyledStr<'s>> for style::StyledString {
106    fn from(s: &'a StyledStr<'s>) -> style::StyledString {
107        style::StyledString::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
108    }
109}
110
111impl<'s> From<StyledStr<'s>> for style::StyledString {
112    fn from(s: StyledStr<'s>) -> style::StyledString {
113        style::StyledString::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
114    }
115}
116
117impl<'s> From<&'s StyledString> for style::StyledString {
118    fn from(s: &'s StyledString) -> style::StyledString {
119        style::StyledString::new(&s.s, s.style.map(style::Style::from).unwrap_or_default())
120    }
121}
122
123impl From<StyledString> for style::StyledString {
124    fn from(s: StyledString) -> style::StyledString {
125        style::StyledString::new(s.s, s.style.map(style::Style::from).unwrap_or_default())
126    }
127}