Area

Struct Area 

Source
pub struct Area<'p> {
    layer: Layer<'p>,
    origin: Position,
    size: Size,
}
Expand description

A view on an area of a PDF layer that can be drawn on.

This struct provides access to the drawing methods of a printpdf::PdfLayerReference. It is defined by the layer that is drawn on and the origin and the size of the area.

Fields§

§layer: Layer<'p>§origin: Position§size: Size

Implementations§

Source§

impl<'p> Area<'p>

Source

fn new(layer: Layer<'p>, origin: Position, size: Size) -> Area<'p>

Source

pub fn next_layer(&self) -> Self

Returns a copy of this area on the next layer of the page.

If this area is not on the last layer, the existing next layer is used. If it is on the last layer, a new layer is created and added to the page.

Source

pub fn add_margins(&mut self, margins: impl Into<Margins>)

Reduces the size of the drawable area by the given margins.

Source

pub fn size(&self) -> Size

Returns the size of this area.

Source

pub fn add_offset(&mut self, offset: impl Into<Position>)

Adds the given offset to the area, reducing the drawable area.

Source

pub fn set_size(&mut self, size: impl Into<Size>)

Sets the size of this area.

Source

pub fn set_width(&mut self, width: Mm)

Sets the width of this area.

Source

pub fn set_height(&mut self, height: Mm)

Sets the height of this area.

Source

pub fn split_horizontally(&self, weights: &[usize]) -> Vec<Area<'p>>

Splits this area horizontally using the given weights.

The returned vector has the same number of elements as the provided slice. The width of the i-th area is width * weights[i] / total_weight, where width is the width of this area, and total_weight is the sum of all given weights.

§Examples
use genpdfi_extended::render::Renderer;
use genpdfi_extended::Size;

let r = Renderer::new(Size::new(210.0, 297.0), "ex").expect("renderer");
let area = r.get_page(0).unwrap().first_layer().area();
let parts = area.split_horizontally(&[1usize, 2usize]);
assert_eq!(parts.len(), 2);
Source

pub fn add_image( &self, image: &DynamicImage, position: Position, scale: Scale, rotation: Rotation, dpi: Option<f32>, )

§Example (images)
use genpdfi_extended::render::Renderer;
use genpdfi_extended::{Size, Mm, Position, Rotation, Scale};
use image::{DynamicImage, RgbImage, Rgb};
let mut r = Renderer::new(Size::new(210.0, 297.0), "img").expect("renderer");
let area = r.first_page().first_layer().area();
let img = DynamicImage::ImageRgb8(RgbImage::from_pixel(5, 5, Rgb([128,128,128])));
area.add_image(&img, Position::new(Mm::from(10.0), Mm::from(10.0)), Scale::new(1.0, 1.0), Rotation::from_degrees(0.0), None);
let mut buf = Vec::new();
r.write(&mut buf).expect("write");
assert!(!buf.is_empty());

Inserts an image into the document.

Only available if the images feature is enabled.

The position is assumed to be relative to the upper left hand corner of the area. Your position will need to compensate for rotation/scale/dpi. Using Image’s render functionality will do this for you and is the recommended way to insert an image into an Area.

Source

pub fn add_svg( &self, svg: &ExternalXObject, position: Position, scale: Scale, rotation: Rotation, )

Inserts an SVG image into the document.

Only available if the images feature is enabled.

SVG images are embedded as vector graphics without rasterization. This method leverages printpdf’s native SVG support for optimal rendering quality.

The position is assumed to be relative to the upper left hand corner of the area. Your position will need to compensate for rotation/scale. Using Image’s render functionality will do this for you and is the recommended way to insert an SVG into an Area.

§Arguments
  • svg - A parsed SVG ExternalXObject from printpdf
  • position - The position in the area, relative to upper left corner
  • scale - Scaling factors for width and height
  • rotation - Clockwise rotation in degrees

Adds a clickable link annotation for an image area.

§Arguments
  • position - Position of the image (upper-left corner)
  • size - Size of the image in millimeters
  • rotation - Rotation of the image
  • uri - The URL to open when the image is clicked
Source

pub fn draw_line<I>(&self, points: I, line_style: LineStyle)
where I: IntoIterator<Item = Position>,

Draws a line with the given points and the given line style.

The points are relative to the upper left corner of the area.

Source

pub fn print_str<S: AsRef<str>>( &self, font_cache: &FontCache, position: Position, style: Style, s: S, ) -> Result<bool, Error>

Tries to draw the given string at the given position and returns true if the area was large enough to draw the string.

The font cache must contain the PDF font for the font set in the style. The position is relative to the upper left corner of the area.

Source

pub fn text_section<'f>( &self, font_cache: &'f FontCache, position: Position, metrics: Metrics, ) -> Option<TextSection<'f, 'p>>

Creates a new text section at the given position if the text section fits in this area.

The given style is only used to calculate the line height of the section. The position is relative to the upper left corner of the area. The font cache must contain the PDF font for all fonts printed with the text section.

§Examples
use genpdfi_extended::render::Renderer;
use genpdfi_extended::Size;
use genpdfi_extended::fonts::{FontCache, FontData, FontFamily};
use genpdfi_extended::style::Style;

// Use the bundled TTF to create a FontCache for the example
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 = FontFamily { regular: fd.clone(), bold: fd.clone(), italic: fd.clone(), bold_italic: fd.clone() };
let cache = FontCache::new(family);
let mut r = Renderer::new(Size::new(210.0, 297.0), "ex").expect("renderer");
// metrics from style
use genpdfi_extended::Position;
let style = Style::new().with_font_family(cache.default_font_family());
let metrics = style.metrics(&cache);
let area = r.get_page(0).unwrap().first_layer().area();
let sec = area.text_section(&cache, Position::default(), metrics);
assert!(sec.is_some());
Source

fn position(&self, position: Position) -> LayerPosition

Returns a position relative to the top left corner of this area.

Adds a clickable link to the document.

The font cache must contain the PDF font for the font set in the style. The position is relative to the upper left corner of the area.

Trait Implementations§

Source§

impl<'p> Clone for Area<'p>

Source§

fn clone(&self) -> Area<'p>

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

Auto Trait Implementations§

§

impl<'p> Freeze for Area<'p>

§

impl<'p> !RefUnwindSafe for Area<'p>

§

impl<'p> !Send for Area<'p>

§

impl<'p> !Sync for Area<'p>

§

impl<'p> Unpin for Area<'p>

§

impl<'p> !UnwindSafe for Area<'p>

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