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: SizeImplementations§
Source§impl<'p> Area<'p>
impl<'p> Area<'p>
fn new(layer: Layer<'p>, origin: Position, size: Size) -> Area<'p>
Sourcepub fn next_layer(&self) -> Self
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.
Sourcepub fn add_margins(&mut self, margins: impl Into<Margins>)
pub fn add_margins(&mut self, margins: impl Into<Margins>)
Reduces the size of the drawable area by the given margins.
Sourcepub fn add_offset(&mut self, offset: impl Into<Position>)
pub fn add_offset(&mut self, offset: impl Into<Position>)
Adds the given offset to the area, reducing the drawable area.
Sourcepub fn set_height(&mut self, height: Mm)
pub fn set_height(&mut self, height: Mm)
Sets the height of this area.
Sourcepub fn split_horizontally(&self, weights: &[usize]) -> Vec<Area<'p>>
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);Sourcepub fn add_image(
&self,
image: &DynamicImage,
position: Position,
scale: Scale,
rotation: Rotation,
dpi: Option<f32>,
)
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.
Sourcepub fn add_svg(
&self,
svg: &ExternalXObject,
position: Position,
scale: Scale,
rotation: Rotation,
)
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 printpdfposition- The position in the area, relative to upper left cornerscale- Scaling factors for width and heightrotation- Clockwise rotation in degrees
Sourcepub fn add_image_link(
&self,
position: Position,
size: Size,
_rotation: Rotation,
uri: &str,
)
pub fn add_image_link( &self, position: Position, size: Size, _rotation: Rotation, uri: &str, )
Adds a clickable link annotation for an image area.
§Arguments
position- Position of the image (upper-left corner)size- Size of the image in millimetersrotation- Rotation of the imageuri- The URL to open when the image is clicked
Sourcepub fn draw_line<I>(&self, points: I, line_style: LineStyle)where
I: IntoIterator<Item = Position>,
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.
Sourcepub fn print_str<S: AsRef<str>>(
&self,
font_cache: &FontCache,
position: Position,
style: Style,
s: S,
) -> Result<bool, Error>
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.
Sourcepub fn text_section<'f>(
&self,
font_cache: &'f FontCache,
position: Position,
metrics: Metrics,
) -> Option<TextSection<'f, 'p>>
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());Sourcefn position(&self, position: Position) -> LayerPosition
fn position(&self, position: Position) -> LayerPosition
Returns a position relative to the top left corner of this area.
Sourcepub fn add_link<S: AsRef<str>>(
&self,
font_cache: &FontCache,
position: Position,
style: Style,
text: S,
uri: S,
) -> Result<bool, Error>
pub fn add_link<S: AsRef<str>>( &self, font_cache: &FontCache, position: Position, style: Style, text: S, uri: S, ) -> Result<bool, Error>
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§
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> 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.