Skip to main content

PdfGraphics

Struct PdfGraphics 

Source
pub struct PdfGraphics { /* private fields */ }
Expand description

PDF graphics context for drawing operations

Implementations§

Source§

impl PdfGraphics

Source

pub fn new() -> Self

Create a new graphics context

Source

pub fn content(&self) -> &str

Get the content stream

Source

pub fn set_stroke_color(&mut self, color: Color) -> Result<()>

Set stroke color (for lines and borders)

Source

pub fn set_fill_color(&mut self, color: Color) -> Result<()>

Set fill color (for rectangles)

Source

pub fn set_opacity(&mut self, gs_name: &str) -> Result<()>

Set fill opacity (for fill operations)

Sets the alpha value for fill operations using the /ca operator in the graphics state dictionary. This requires ExtGState support.

§Arguments
  • opacity - Opacity value from 0.0 (transparent) to 1.0 (opaque)
  • gs_name - Name of the graphics state resource (e.g., “gs1”)
§PDF Reference

See PDF specification section 8.4 for graphics state parameters.

Source

pub fn set_stroke_opacity(&mut self, gs_name: &str) -> Result<()>

Set stroke opacity (for stroke operations)

Sets the alpha value for stroke operations using the /CA operator in the graphics state dictionary. This requires ExtGState support.

§Arguments
  • gs_name - Name of the graphics state resource (e.g., “gs1”)
§PDF Reference

See PDF specification section 8.4 for graphics state parameters.

Source

pub fn set_line_width(&mut self, width: Length) -> Result<()>

Set line width

Source

pub fn set_dash_pattern(&mut self, dash_array: &[f64], phase: f64) -> Result<()>

Set dash pattern for line drawing

§Arguments
  • dash_array - Array of on/off lengths (e.g., &[3.0, 2.0] for dashed)
  • phase - Offset into the dash pattern (usually 0)
§Examples
  • Solid: set_dash_pattern(&[], 0) or []0 d
  • Dashed: set_dash_pattern(&[6.0, 3.0], 0) or [6 3] 0 d
  • Dotted: set_dash_pattern(&[1.0, 2.0], 0) or [1 2] 0 d
Source

pub fn draw_rectangle( &mut self, x: Length, y: Length, width: Length, height: Length, ) -> Result<()>

Draw a rectangle (stroke only)

Source

pub fn fill_rectangle( &mut self, x: Length, y: Length, width: Length, height: Length, ) -> Result<()>

Fill a rectangle

Source

pub fn fill_rectangle_with_radius( &mut self, x: Length, y: Length, width: Length, height: Length, border_radius: Option<[Length; 4]>, ) -> Result<()>

Fill a rectangle with optional rounded corners

Source

pub fn draw_rounded_rectangle( &mut self, x: Length, y: Length, width: Length, height: Length, radii: [Length; 4], fill: bool, ) -> Result<()>

Draw a rounded rectangle with independent corner radii

Uses Bezier curves to approximate circular arcs for rounded corners. The magic number 0.552284749831 is used to approximate a circle with cubic Bezier curves. This is derived from 4*(sqrt(2)-1)/3, which minimizes the error between the arc and the curve.

§Arguments
  • x, y - Bottom-left corner of the rectangle
  • width, height - Dimensions of the rectangle
  • radii - Corner radii [top-left, top-right, bottom-right, bottom-left]
  • fill - If true, fill the rectangle; if false, stroke it
Source

pub fn draw_line( &mut self, x1: Length, y1: Length, x2: Length, y2: Length, ) -> Result<()>

Draw a line

Source

pub fn draw_borders( &mut self, x: Length, y: Length, width: Length, height: Length, border_widths: [Length; 4], border_colors: [Color; 4], border_styles: [BorderStyle; 4], ) -> Result<()>

Draw borders (all four sides)

Source

pub fn draw_borders_with_radius( &mut self, x: Length, y: Length, width: Length, height: Length, border_widths: [Length; 4], border_colors: [Color; 4], border_styles: [BorderStyle; 4], border_radius: Option<[Length; 4]>, ) -> Result<()>

Draw borders with optional rounded corners

Source

pub fn save_state(&mut self) -> Result<()>

Save graphics state

Source

pub fn restore_state(&mut self) -> Result<()>

Restore graphics state

Source

pub fn save_clip_state( &mut self, x: Length, y: Length, width: Length, height: Length, ) -> Result<()>

Save graphics state and set clipping path

This method saves the current graphics state and establishes a rectangular clipping path. Content drawn after this call will be clipped to the specified rectangle until restore_clip_state() is called.

PDF operators used:

  • q: Save graphics state
  • re: Rectangle path
  • W: Set clipping path (intersect with current path)
  • n: End path without stroking or filling
§Arguments
  • x, y - Bottom-left corner of clipping rectangle (PDF coordinates)
  • width, height - Dimensions of clipping rectangle
§PDF Reference

See PDF specification section 8.5 for clipping path details.

Source

pub fn restore_clip_state(&mut self) -> Result<()>

Restore graphics state after clipping

This restores the graphics state that was saved by save_clip_state(), removing the clipping path.

PDF operator used:

  • Q: Restore graphics state
Source

pub fn fill_gradient( &mut self, x: Length, y: Length, width: Length, height: Length, gradient_index: usize, ) -> Result<()>

Fill a rectangle with a gradient

Uses PDF shading pattern to fill the specified rectangle with a gradient.

§Arguments
  • x, y - Bottom-left corner of the rectangle
  • width, height - Dimensions of the rectangle
  • gradient_index - Index of the gradient shading resource (Sh0, Sh1, etc.)
§PDF Reference

See PDF specification section 8.7 for shading patterns. Uses the /sh operator to paint with shading pattern.

Source

pub fn fill_gradient_with_radius( &mut self, x: Length, y: Length, width: Length, height: Length, gradient_index: usize, border_radius: Option<[Length; 4]>, ) -> Result<()>

Fill a rectangle with a gradient and optional rounded corners

Trait Implementations§

Source§

impl Default for PdfGraphics

Source§

fn default() -> Self

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

Auto Trait Implementations§

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.