From 071d2c60252c0e6acc2c1c347a1b88dff02c4b41 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 20 Aug 2018 16:30:52 -0700 Subject: [PATCH 1/2] Move methods on CGContext taking &self from to CGContextRef --- core-graphics/src/context.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core-graphics/src/context.rs b/core-graphics/src/context.rs index 7b659cf..58784ea 100644 --- a/core-graphics/src/context.rs +++ b/core-graphics/src/context.rs @@ -20,7 +20,7 @@ use std::ptr; use std::slice; use geometry::{CGAffineTransform, CGRect}; use image::CGImage; -use foreign_types::ForeignType; +use foreign_types::{ForeignType, ForeignTypeRef}; #[repr(C)] pub enum CGTextDrawingMode { @@ -77,7 +77,9 @@ impl CGContext { (self.height() * self.bytes_per_row()) as usize) } } +} +impl CGContextRef { pub fn flush(&self) { unsafe { CGContextFlush(self.as_ptr()) From 8052ba29e194810f6c2c9c3545146897013ecc38 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 20 Aug 2018 16:31:46 -0700 Subject: [PATCH 2/2] Add save, restore, translate, scale, set_fill_color, add_path, close_path, and fill_path to CGContext --- core-graphics/src/context.rs | 61 +++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/core-graphics/src/context.rs b/core-graphics/src/context.rs index 58784ea..3cca450 100644 --- a/core-graphics/src/context.rs +++ b/core-graphics/src/context.rs @@ -9,9 +9,11 @@ use base::CGFloat; use color_space::CGColorSpace; -use core_foundation::base::{CFRelease, CFRetain, CFTypeID}; +use core_foundation::base::{ToVoid, CFRelease, CFRetain, CFTypeID}; use font::{CGFont, CGGlyph}; use geometry::CGPoint; +use color::CGColor; +use path::CGPathRef; use libc::{c_int, size_t}; use std::os::raw::c_void; @@ -104,6 +106,12 @@ impl CGContextRef { } } + pub fn set_fill_color(&self, color: &CGColor) { + unsafe { + CGContextSetFillColorWithColor(self.as_ptr(), color.to_void()); + } + } + pub fn set_rgb_fill_color(&self, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { unsafe { CGContextSetRGBFillColor(self.as_ptr(), red, green, blue, alpha) @@ -176,6 +184,24 @@ impl CGContextRef { } } + pub fn add_path(&self, path: &CGPathRef) { + unsafe { + CGContextAddPath(self.as_ptr(), path.as_ptr()); + } + } + + pub fn close_path(&self) { + unsafe { + CGContextClosePath(self.as_ptr()); + } + } + + pub fn fill_path(&self) { + unsafe { + CGContextFillPath(self.as_ptr()); + } + } + pub fn fill_rect(&self, rect: CGRect) { unsafe { CGContextFillRect(self.as_ptr(), rect) @@ -224,6 +250,30 @@ impl CGContextRef { count) } } + + pub fn save(&self) { + unsafe { + CGContextSaveGState(self.as_ptr()); + } + } + + pub fn restore(&self) { + unsafe { + CGContextRestoreGState(self.as_ptr()); + } + } + + pub fn translate(&self, tx: CGFloat, ty: CGFloat) { + unsafe { + CGContextTranslateCTM(self.as_ptr(), tx, ty); + } + } + + pub fn scale(&self, sx: CGFloat, sy: CGFloat) { + unsafe { + CGContextScaleCTM(self.as_ptr(), sx, sy); + } + } } #[test] @@ -281,6 +331,10 @@ extern { fn CGContextSetShouldSubpixelPositionFonts(c: ::sys::CGContextRef, shouldSubpixelPositionFonts: bool); fn CGContextSetTextDrawingMode(c: ::sys::CGContextRef, mode: CGTextDrawingMode); + fn CGContextSetFillColorWithColor(c: ::sys::CGContextRef, color: *const c_void); + fn CGContextAddPath(c: ::sys::CGContextRef, path: ::sys::CGPathRef); + fn CGContextClosePath(c: ::sys::CGContextRef); + fn CGContextFillPath(c: ::sys::CGContextRef); fn CGContextSetRGBFillColor(context: ::sys::CGContextRef, red: CGFloat, green: CGFloat, @@ -297,5 +351,10 @@ extern { glyphs: *const CGGlyph, positions: *const CGPoint, count: size_t); + + fn CGContextSaveGState(c: ::sys::CGContextRef); + fn CGContextRestoreGState(c: ::sys::CGContextRef); + fn CGContextTranslateCTM(c: ::sys::CGContextRef, tx: CGFloat, ty: CGFloat); + fn CGContextScaleCTM(c: ::sys::CGContextRef, sx: CGFloat, sy: CGFloat); }