Skip to main content

CGContext

Struct CGContext 

Source
pub struct CGContext { /* private fields */ }
Available on crate feature cg only.
Expand description

Reference-counted CGContextRef backed by a bitmap.

Clones retain the same mutable native context; they are not independent drawing surfaces.

Implementations§

Source§

impl CGContext

Source

pub const unsafe fn from_raw(ptr: *mut c_void) -> Self

Wrap a raw CGContextRef pointer — takes ownership without retaining.

§Safety

ptr must be a non-null CGContextRef whose ownership the caller is transferring to the returned CGContext.

Source

pub fn new_rgba8(width: usize, height: usize) -> Result<Self, CFError>

Create a premultiplied-last RGBA8 bitmap context.

§Errors

Returns CFError if Core Graphics fails to create the bitmap context.

Source

pub fn new_grayscale(width: usize, height: usize) -> Result<Self, CFError>

Create an 8-bit grayscale bitmap context.

§Errors

Returns CFError if Core Graphics fails to create the bitmap context.

Source

pub fn width(&self) -> usize

Width in pixels.

Source

pub fn height(&self) -> usize

Height in pixels.

Source

pub fn bytes_per_row(&self) -> usize

Bytes per row.

Source

pub fn bits_per_component(&self) -> usize

Bits per component.

Source

pub fn bits_per_pixel(&self) -> usize

Bits per pixel.

Source

pub fn data(&self) -> *mut u8

Raw bitmap data pointer.

Dereferencing the pointer is unsafe because retained aliases and native drawing operations can mutate the same storage.

Source

pub fn color_space(&self) -> Option<CGColorSpace>

The context color space, if one is set.

Source

pub fn alpha_info(&self) -> u32

The raw CGImageAlphaInfo value for the bitmap context.

Source

pub unsafe fn as_bytes(&self) -> &[u8] ⓘ

The bitmap storage as immutable bytes.

§Safety

Every byte in the range must already be initialized. For the returned reference’s lifetime, the bitmap allocation must remain live and immovable, and no retained alias or native call may draw into, clear, replace, or otherwise mutate the context storage.

Source

pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ

The bitmap storage as mutable bytes.

§Safety

Every byte in the range must already be initialized. For the returned reference’s lifetime, the caller must have unique access to the bitmap storage across every retained wrapper and native alias, and no drawing or snapshot operation may access the context.

Source

pub fn set_rgb_fill_color(&self, r: f64, g: f64, b: f64, a: f64)

Set the current fill color in DeviceRGB.

Source

pub fn set_rgb_stroke_color(&self, r: f64, g: f64, b: f64, a: f64)

Set the current stroke color in DeviceRGB.

Source

pub fn set_line_width(&self, w: f64)

Set the stroke line width.

Source

pub fn clear_rect(&self, x: f64, y: f64, w: f64, h: f64)

Clear a rectangle to transparent black.

Source

pub fn fill_rect(&self, x: f64, y: f64, w: f64, h: f64)

Fill a rectangle.

Source

pub fn stroke_rect(&self, x: f64, y: f64, w: f64, h: f64)

Stroke a rectangle.

Source

pub fn begin_path(&self)

Begin a new path.

Source

pub fn close_path(&self)

Close the current path.

Source

pub fn move_to(&self, x: f64, y: f64)

Move the current point.

Source

pub fn add_line_to(&self, x: f64, y: f64)

Add a line segment to the current path.

Source

pub fn add_rect(&self, x: f64, y: f64, w: f64, h: f64)

Add a rectangle to the current path.

Source

pub fn add_ellipse_in_rect(&self, x: f64, y: f64, w: f64, h: f64)

Add an ellipse inscribed in the rectangle.

Source

pub fn fill_path(&self)

Fill the current path.

Source

pub fn stroke_path(&self)

Stroke the current path.

Source

pub fn draw_image(&self, x: f64, y: f64, w: f64, h: f64, image: &CGImage)

Draw an image into the target rectangle.

Source

pub fn translate(&self, tx: f64, ty: f64)

Translate the current transformation matrix.

Source

pub fn scale(&self, sx: f64, sy: f64)

Scale the current transformation matrix.

Source

pub fn rotate(&self, radians: f64)

Rotate the current transformation matrix.

Source

pub fn save_g_state(&self)

Save the current graphics state.

Source

pub fn restore_g_state(&self)

Restore the most recently saved graphics state.

Source

pub fn snapshot_to_image(&self) -> Option<CGImage>

Snapshot the current bitmap contents to a CGImage.

Source

pub const fn as_ptr(&self) -> *mut c_void

Raw CGContextRef pointer.

Trait Implementations§

Source§

impl Clone for CGContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for CGContext

Source§

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

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

impl Drop for CGContext

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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> 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
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> 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
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.