Style

Struct Style 

Source
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 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.

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

Source

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());
Source

pub fn merge(&mut self, style: impl Into<Style>)

Merges the given style into this style.

Source

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);
Source

pub fn combine(left: impl Into<Style>, right: impl Into<Style>) -> Style

Creates a new style by combining the given styles.

Source

pub fn color(&self) -> Option<Color>

Returns the outline color for this style, if set.

Source

pub fn is_bold(&self) -> bool

Returns whether the bold text effect is set.

Source

pub fn is_italic(&self) -> bool

Returns whether the italic text effect is set.

Source

pub fn font_size(&self) -> u8

Returns the font size for this style in points, or 12 if no font size is set.

Source

pub fn line_spacing(&self) -> f32

Returns the line spacing factor for this style, or 1 if no line spacing factor is set.

Source

pub fn set_bold(&mut self)

Sets the bold effect for this style.

Source

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());
Source

pub fn set_italic(&mut self)

Sets the italic effect for this style.

Source

pub fn italic(self) -> Style

Sets the italic effect for this style and returns it.

Source

pub fn set_font_family(&mut self, font_family: FontFamily<Font>)

Sets the font family for this style.

Source

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);
Source

pub fn set_line_spacing(&mut self, line_spacing: f32)

Sets the line spacing factor for this style.

Source

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);
Source

pub fn set_font_size(&mut self, font_size: u8)

Sets the font size in points for this style.

Source

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);
Source

pub fn set_color(&mut self, color: Color)

Sets the outline color for this style.

Source

pub fn with_color(self, color: Color) -> Self

Sets the outline color for this style and returns it.

Source

pub fn set_font_override(&mut self, font_override: FontFamily<Font>)

Sets the font override for this style.

Source

pub fn with_font_override(self, font_override: FontFamily<Font>) -> Style

Sets the font override for this style and returns it.

Source

pub fn font_override(&self) -> Option<FontFamily<Font>>

Returns the font override for this style, if set.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 Clone for Style

Source§

fn clone(&self) -> Style

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Style

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Style

Source§

fn default() -> Style

Returns the “default value” for a type. Read more
Source§

impl<T: Into<Style>> Extend<T> for Style

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<Color> for Style

Source§

fn from(color: Color) -> Style

Converts to this type from the input type.
Source§

impl From<Effect> for Style

Source§

fn from(effect: Effect) -> Style

Converts to this type from the input type.
Source§

impl From<FontFamily<Font>> for Style

Source§

fn from(font_family: FontFamily<Font>) -> Style

Converts to this type from the input type.
Source§

impl<T: Into<Style>> FromIterator<T> for Style

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Style

Creates a value from an iterator. Read more
Source§

impl PartialEq for Style

Source§

fn eq(&self, other: &Style) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Style

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Finish for T

§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

§

fn to_owned_table(&self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V