// SPDX-FileCopyrightText: 2020 Robin Krahl // SPDX-License-Identifier: Apache-2.0 or MIT //! Types for styled strings. //! //! A [`StyledString`][] is a [`String`][] with a [`Style`][] annotation. Accordingly, a //! [`StyledStr`][] is a [`&str`][] with a [`Style`][] annotation, and a [`StyledCow`][] is either //! a [`Cow<'_, str>`][] with a [`Style`][] annotation. //! //! A [`Style`][] is a combination of a [`FontFamily`][], a font size, a line spacing factor, a //! [`Color`][] and a combination of [`Effect`][]s (bold or italic). //! //! # Example //! //! ``` //! use genpdf::style; //! let style = style::Style::new().bold(); //! let ss1 = style::StyledStr::new("bold", style); //! let ss2 = style::StyledStr::new("red", style::Color::Rgb(255, 0, 0)); //! ``` //! //! [`Color`]: enum.Color.html //! [`Effect`]: enum.Effect.html //! [`FontFamily`]: ../fonts/struct.FontFamily.html //! [`Style`]: struct.Style.html //! [`StyledCow`]: struct.StyledCow.html //! [`StyledStr`]: struct.StyledStr.html //! [`StyledString`]: struct.StyledString.html //! [`String`]: https://doc.rust-lang.org/std/string/struct.String.html //! [`&str`]: https://doc.rust-lang.org/std/primitive.str.html //! [`Cow<'_, str>`]: https://doc.rust-lang.org/std/borrow/enum.Cow.html use std::borrow; use std::iter; use crate::fonts; use crate::Mm; /// A color, represented by RGB, CMYK or Greyscale values. /// /// For all variants, the possible values range from 0 to 255. /// /// # Examples /// /// ``` /// let red = genpdf::style::Color::Rgb(255, 0, 0); /// let cyan = genpdf::style::Color::Cmyk(255, 0, 0, 0); /// let grey = genpdf::style::Color::Greyscale(127); /// ``` #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Color { /// An RGB color with red, green and blue values between 0 and 255. Rgb(u8, u8, u8), /// An CMYK color with cyan, magenta, yellow and key values between 0 and 255. Cmyk(u8, u8, u8, u8), /// A greyscale color with a value between 0 and 255. Greyscale(u8), } impl From for printpdf::Color { fn from(color: Color) -> printpdf::Color { match color { Color::Rgb(r, g, b) => printpdf::Color::Rgb(printpdf::Rgb::new( f64::from(r) / 255.0, f64::from(g) / 255.0, f64::from(b) / 255.0, None, )), Color::Cmyk(c, m, y, k) => printpdf::Color::Cmyk(printpdf::Cmyk::new( f64::from(c) / 255.0, f64::from(m) / 255.0, f64::from(y) / 255.0, f64::from(k) / 255.0, None, )), Color::Greyscale(val) => { printpdf::Color::Greyscale(printpdf::Greyscale::new(f64::from(val) / 255.0, None)) } } } } /// A text effect (bold or italic). #[derive(Clone, Copy, Debug, PartialEq)] pub enum Effect { /// Bold text. Bold, /// Italic text. Italic, } /// A style annotation for a string. /// /// The annotation consists of: /// - a font family, see [`FontFamily`][] (defaults to the [`FontCache`][] default) /// - a font size in points (defaults to 12) /// - a line spacing factor, with 1 meaning single line spacing (defaults to 1) /// - an outline color, see [`Color`][] (defaults to black) /// - a combination of text effects, see [`Effect`][] (defaults to none) /// /// All properties are optional. If they are not set, they can be inferred from parent styles or /// from the defaults. /// /// [`Color`]: enum.Color.html /// [`Effect`]: enum.Effect.html /// [`FontFamily`]: ../fonts/struct.FontFamily.html /// [`FontCache`]: ../fonts/struct.FontCache.html #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct Style { font_family: Option>, font_size: Option, line_spacing: Option, color: Option, is_bold: bool, is_italic: bool, } impl Style { /// Creates a new style without settings. pub fn new() -> Style { Style::default() } /// Merges the given style into this style. pub fn merge(&mut self, style: impl Into