pub struct Style {
font_family: Option<FontFamily<Font>>,
font_size: Option<u8>,
line_spacing: Option<f32>,
color: Option<Color>,
is_bold: bool,
is_italic: bool,
font_override: Option<FontFamily<Font>>,
}Expand description
A style annotation for a string.
The annotation consists of:
- a font family, see
FontFamily(defaults to theFontCachedefault) - 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.
Fields§
§font_family: Option<FontFamily<Font>>§font_size: Option<u8>§line_spacing: Option<f32>§color: Option<Color>§is_bold: bool§is_italic: bool§font_override: Option<FontFamily<Font>>Optional font override for special rendering contexts (e.g., code blocks in monospace). This is used to render specific text with a different font than the document default.
Implementations§
Source§impl Style
impl Style
Sourcepub fn new() -> Style
pub fn new() -> Style
Creates a new style without settings.
§Examples
use genpdfi_extended::style::Style;
let s = Style::new();
assert_eq!(s.font_size(), 12);
assert!(!s.is_bold());Sourcepub fn and(self, style: impl Into<Style>) -> Style
pub fn and(self, style: impl Into<Style>) -> Style
Combines this style and the given style and returns the result.
§Examples
use genpdfi_extended::style::Style;
let a = Style::new().bold();
let b = Style::new().with_font_size(18);
let c = a.and(b);
assert!(c.is_bold());
assert_eq!(c.font_size(), 18);Sourcepub fn combine(left: impl Into<Style>, right: impl Into<Style>) -> Style
pub fn combine(left: impl Into<Style>, right: impl Into<Style>) -> Style
Creates a new style by combining the given styles.
Sourcepub fn font_size(&self) -> u8
pub fn font_size(&self) -> u8
Returns the font size for this style in points, or 12 if no font size is set.
Sourcepub fn line_spacing(&self) -> f32
pub fn line_spacing(&self) -> f32
Returns the line spacing factor for this style, or 1 if no line spacing factor is set.
Sourcepub fn bold(self) -> Style
pub fn bold(self) -> Style
Sets the bold effect for this style and returns it.
§Examples
use genpdfi_extended::style::Style;
let s = Style::new().bold();
assert!(s.is_bold());
let s2 = s.italic();
assert!(s2.is_italic());Sourcepub fn set_italic(&mut self)
pub fn set_italic(&mut self)
Sets the italic effect for this style.
Sourcepub fn set_font_family(&mut self, font_family: FontFamily<Font>)
pub fn set_font_family(&mut self, font_family: FontFamily<Font>)
Sets the font family for this style.
Sourcepub fn with_font_family(self, font_family: FontFamily<Font>) -> Style
pub fn with_font_family(self, font_family: FontFamily<Font>) -> Style
Sets the font family for this style and returns it.
§Examples
use genpdfi_extended::style::Style;
use genpdfi_extended::fonts::{FontData, FontFamily, FontCache};
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts/NotoSans-Regular.ttf")).to_vec();
let fd = FontData::new(data.clone(), None).expect("font data");
let family_data = FontFamily { regular: fd.clone(), bold: fd.clone(), italic: fd.clone(), bold_italic: fd.clone() };
let mut cache = FontCache::new(family_data);
let family = cache.default_font_family();
let s = Style::new().with_font_family(family);Sourcepub fn set_line_spacing(&mut self, line_spacing: f32)
pub fn set_line_spacing(&mut self, line_spacing: f32)
Sets the line spacing factor for this style.
Sourcepub fn with_line_spacing(self, line_spacing: f32) -> Style
pub fn with_line_spacing(self, line_spacing: f32) -> Style
Sets the line spacing factor for this style and returns it.
§Examples
use genpdfi_extended::style::Style;
let s = Style::new().with_line_spacing(1.5);
assert!((s.line_spacing() - 1.5).abs() < f32::EPSILON);Sourcepub fn set_font_size(&mut self, font_size: u8)
pub fn set_font_size(&mut self, font_size: u8)
Sets the font size in points for this style.
Sourcepub fn with_font_size(self, font_size: u8) -> Style
pub fn with_font_size(self, font_size: u8) -> Style
Sets the font size in points for this style and returns it.
§Examples
use genpdfi_extended::style::Style;
let s = Style::new().with_font_size(18);
assert_eq!(s.font_size(), 18);Sourcepub fn with_color(self, color: Color) -> Self
pub fn with_color(self, color: Color) -> Self
Sets the outline color for this style and returns it.
Sourcepub fn set_font_override(&mut self, font_override: FontFamily<Font>)
pub fn set_font_override(&mut self, font_override: FontFamily<Font>)
Sets the font override for this style.
Sourcepub fn with_font_override(self, font_override: FontFamily<Font>) -> Style
pub fn with_font_override(self, font_override: FontFamily<Font>) -> Style
Sets the font override for this style and returns it.
Sourcepub fn font_override(&self) -> Option<FontFamily<Font>>
pub fn font_override(&self) -> Option<FontFamily<Font>>
Returns the font override for this style, if set.
Sourcepub fn char_width(&self, font_cache: &FontCache, c: char) -> Mm
pub fn char_width(&self, font_cache: &FontCache, c: char) -> Mm
Calculates the width of the given character with this style using the data in the given font cache.
If the font family is set, it must have been created by the given FontCache.
Sourcepub fn char_left_side_bearing(&self, font_cache: &FontCache, c: char) -> Mm
pub fn char_left_side_bearing(&self, font_cache: &FontCache, c: char) -> Mm
Returns the width of the empty space between the origin of the glyph bounding box and the leftmost edge of the character, for this style using the given font cache.
If the font family is set, it must have been created by the given FontCache.
Sourcepub fn str_width(&self, font_cache: &FontCache, s: &str) -> Mm
pub fn str_width(&self, font_cache: &FontCache, s: &str) -> Mm
Calculates the width of the given string with this style using the data in the given font cache.
If the font family is set, it must have been created by the given FontCache.
Sourcepub fn font_family(&self, font_cache: &FontCache) -> FontFamily<Font>
pub fn font_family(&self, font_cache: &FontCache) -> FontFamily<Font>
Returns the font family for this style or the default font family using the given font cache.
If the font family is set, it must have been created by the given FontCache.
Sourcepub fn font(&self, font_cache: &FontCache) -> Font
pub fn font(&self, font_cache: &FontCache) -> Font
Returns the font for this style using the given font cache.
If the font family is set, it must have been created by the given FontCache.
Sourcepub fn line_height(&self, font_cache: &FontCache) -> Mm
pub fn line_height(&self, font_cache: &FontCache) -> Mm
Calculates the line height for strings with this style using the data in the given font cache.
If the font family is set, it must have been created by the given FontCache.
Sourcepub fn metrics(&self, font_cache: &FontCache) -> Metrics
pub fn metrics(&self, font_cache: &FontCache) -> Metrics
Calculate the metrics of the font for this style using the data in the given font cache.
If the font family is set, it must have been created by the given FontCache.
Sourcepub fn text_width(&self, font_cache: &FontCache, s: &str) -> Mm
pub fn text_width(&self, font_cache: &FontCache, s: &str) -> Mm
Calculate the width of the given string with this style using the data in the given font cache.
If the font family is set, it must have been created by the given FontCache.
§Example
use genpdfi_extended::style::Style;
use genpdfi_extended::fonts::{FontData, FontFamily, FontCache};
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts/NotoSans-Regular.ttf")).to_vec();
let fd = FontData::new(data, None).expect("font data");
let family = FontFamily { regular: fd.clone(), bold: fd.clone(), italic: fd.clone(), bold_italic: fd.clone() };
let cache = FontCache::new(family);
let style = Style::new().with_font_family(cache.default_font_family());
let w = style.text_width(&cache, "abc");
use genpdfi_extended::Mm;
assert!(w > Mm::from(0.0));Trait Implementations§
Source§impl<T: Into<Style>> Extend<T> for Style
impl<T: Into<Style>> Extend<T> for Style
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)impl Copy for Style
impl StructuralPartialEq for Style
Auto Trait Implementations§
impl Freeze for Style
impl RefUnwindSafe for Style
impl Send for Style
impl Sync for Style
impl Unpin for Style
impl UnwindSafe for Style
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.