Use foreign-types for CGContext

This commit is contained in:
Kornel
2017-09-15 00:38:06 +01:00
parent 8c0664a542
commit c86445c76f
2 changed files with 52 additions and 94 deletions
+49 -94
View File
@@ -9,9 +9,9 @@
use base::CGFloat;
use color_space::CGColorSpace;
use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType};
use core_foundation::base::{CFRelease, CFRetain, CFTypeID};
use libc::{c_void, c_int, size_t};
use std::mem;
use std::ptr;
use std::slice;
use geometry::CGRect;
@@ -29,67 +29,22 @@ pub enum CGTextDrawingMode {
CGTextClip
}
// This is an enum due to zero-sized types warnings.
// For more details see https://github.com/rust-lang/rust/issues/27303
pub enum __CGContext {}
pub type CGContextRef = *const __CGContext;
pub struct CGContext {
obj: CGContextRef,
foreign_type! {
#[doc(hidden)]
type CType = ::sys::CGContext;
fn drop = |cs| CFRelease(cs as *mut _);
fn clone = |p| CFRetain(p as *const _) as *mut _;
pub struct CGContext;
pub struct CGContextRef;
}
impl Drop for CGContext {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
impl Clone for CGContext {
fn clone(&self) -> CGContext {
unsafe {
TCFType::wrap_under_get_rule(self.as_concrete_TypeRef())
}
}
}
impl TCFType<CGContextRef> for CGContext {
#[inline]
fn as_concrete_TypeRef(&self) -> CGContextRef {
self.obj
}
#[inline]
unsafe fn wrap_under_get_rule(reference: CGContextRef) -> CGContext {
let reference: CGContextRef = mem::transmute(CFRetain(mem::transmute(reference)));
TCFType::wrap_under_create_rule(reference)
}
#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
unsafe {
mem::transmute(self.as_concrete_TypeRef())
}
}
#[inline]
unsafe fn wrap_under_create_rule(obj: CGContextRef) -> CGContext {
CGContext {
obj: obj,
}
}
#[inline]
fn type_id() -> CFTypeID {
impl CGContext {
pub fn type_id() -> CFTypeID {
unsafe {
CGContextGetTypeID()
}
}
}
impl CGContext {
pub fn create_bitmap_context(data: Option<*mut c_void>,
width: size_t,
height: size_t,
@@ -107,116 +62,116 @@ impl CGContext {
space.as_ptr(),
bitmap_info);
assert!(!result.is_null());
TCFType::wrap_under_create_rule(result)
Self::from_ptr(result)
}
}
pub fn data(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(
CGBitmapContextGetData(self.as_concrete_TypeRef()) as *mut u8,
CGBitmapContextGetData(self.as_ptr()) as *mut u8,
(self.height() * self.bytes_per_row()) as usize)
}
}
pub fn width(&self) -> size_t {
unsafe {
CGBitmapContextGetWidth(self.as_concrete_TypeRef())
CGBitmapContextGetWidth(self.as_ptr())
}
}
pub fn height(&self) -> size_t {
unsafe {
CGBitmapContextGetHeight(self.as_concrete_TypeRef())
CGBitmapContextGetHeight(self.as_ptr())
}
}
pub fn bytes_per_row(&self) -> size_t {
unsafe {
CGBitmapContextGetBytesPerRow(self.as_concrete_TypeRef())
CGBitmapContextGetBytesPerRow(self.as_ptr())
}
}
pub fn set_rgb_fill_color(&self, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
unsafe {
CGContextSetRGBFillColor(self.as_concrete_TypeRef(), red, green, blue, alpha)
CGContextSetRGBFillColor(self.as_ptr(), red, green, blue, alpha)
}
}
pub fn set_allows_font_smoothing(&self, allows_font_smoothing: bool) {
unsafe {
CGContextSetAllowsFontSmoothing(self.as_concrete_TypeRef(), allows_font_smoothing)
CGContextSetAllowsFontSmoothing(self.as_ptr(), allows_font_smoothing)
}
}
pub fn set_font_smoothing_style(&self, style: i32) {
unsafe {
CGContextSetFontSmoothingStyle(self.as_concrete_TypeRef(), style as _);
CGContextSetFontSmoothingStyle(self.as_ptr(), style as _);
}
}
pub fn set_should_smooth_fonts(&self, should_smooth_fonts: bool) {
unsafe {
CGContextSetShouldSmoothFonts(self.as_concrete_TypeRef(), should_smooth_fonts)
CGContextSetShouldSmoothFonts(self.as_ptr(), should_smooth_fonts)
}
}
pub fn set_allows_antialiasing(&self, allows_antialiasing: bool) {
unsafe {
CGContextSetAllowsAntialiasing(self.as_concrete_TypeRef(), allows_antialiasing)
CGContextSetAllowsAntialiasing(self.as_ptr(), allows_antialiasing)
}
}
pub fn set_should_antialias(&self, should_antialias: bool) {
unsafe {
CGContextSetShouldAntialias(self.as_concrete_TypeRef(), should_antialias)
CGContextSetShouldAntialias(self.as_ptr(), should_antialias)
}
}
pub fn set_allows_font_subpixel_quantization(&self, allows_font_subpixel_quantization: bool) {
unsafe {
CGContextSetAllowsFontSubpixelQuantization(self.as_concrete_TypeRef(), allows_font_subpixel_quantization)
CGContextSetAllowsFontSubpixelQuantization(self.as_ptr(), allows_font_subpixel_quantization)
}
}
pub fn set_should_subpixel_quantize_fonts(&self, should_subpixel_quantize_fonts: bool) {
unsafe {
CGContextSetShouldSubpixelQuantizeFonts(self.as_concrete_TypeRef(), should_subpixel_quantize_fonts)
CGContextSetShouldSubpixelQuantizeFonts(self.as_ptr(), should_subpixel_quantize_fonts)
}
}
pub fn set_allows_font_subpixel_positioning(&self, allows_font_subpixel_positioning: bool) {
unsafe {
CGContextSetAllowsFontSubpixelPositioning(self.as_concrete_TypeRef(), allows_font_subpixel_positioning)
CGContextSetAllowsFontSubpixelPositioning(self.as_ptr(), allows_font_subpixel_positioning)
}
}
pub fn set_should_subpixel_position_fonts(&self, should_subpixel_position_fonts: bool) {
unsafe {
CGContextSetShouldSubpixelPositionFonts(self.as_concrete_TypeRef(), should_subpixel_position_fonts)
CGContextSetShouldSubpixelPositionFonts(self.as_ptr(), should_subpixel_position_fonts)
}
}
pub fn set_text_drawing_mode(&self, mode: CGTextDrawingMode) {
unsafe {
CGContextSetTextDrawingMode(self.as_concrete_TypeRef(), mode)
CGContextSetTextDrawingMode(self.as_ptr(), mode)
}
}
pub fn fill_rect(&self, rect: CGRect) {
unsafe {
CGContextFillRect(self.as_concrete_TypeRef(), rect)
CGContextFillRect(self.as_ptr(), rect)
}
}
pub fn draw_image(&self, rect: CGRect, image: &CGImage) {
unsafe {
CGContextDrawImage(self.as_concrete_TypeRef(), rect, image.as_ptr());
CGContextDrawImage(self.as_ptr(), rect, image.as_ptr());
}
}
pub fn create_image(&self) -> Option<CGImage> {
let image = unsafe { CGBitmapContextCreateImage(self.as_concrete_TypeRef()) };
let image = unsafe { CGBitmapContextCreateImage(self.as_ptr()) };
if !image.is_null() {
Some(unsafe { CGImage::from_ptr(image) })
} else {
@@ -258,34 +213,34 @@ extern {
bytesPerRow: size_t,
space: ::sys::CGColorSpaceRef,
bitmapInfo: u32)
-> CGContextRef;
fn CGBitmapContextGetData(context: CGContextRef) -> *mut c_void;
fn CGBitmapContextGetWidth(context: CGContextRef) -> size_t;
fn CGBitmapContextGetHeight(context: CGContextRef) -> size_t;
fn CGBitmapContextGetBytesPerRow(context: CGContextRef) -> size_t;
fn CGBitmapContextCreateImage(context: CGContextRef) -> ::sys::CGImageRef;
-> ::sys::CGContextRef;
fn CGBitmapContextGetData(context: ::sys::CGContextRef) -> *mut c_void;
fn CGBitmapContextGetWidth(context: ::sys::CGContextRef) -> size_t;
fn CGBitmapContextGetHeight(context: ::sys::CGContextRef) -> size_t;
fn CGBitmapContextGetBytesPerRow(context: ::sys::CGContextRef) -> size_t;
fn CGBitmapContextCreateImage(context: ::sys::CGContextRef) -> ::sys::CGImageRef;
fn CGContextGetTypeID() -> CFTypeID;
fn CGContextSetAllowsFontSmoothing(c: CGContextRef, allowsFontSmoothing: bool);
fn CGContextSetShouldSmoothFonts(c: CGContextRef, shouldSmoothFonts: bool);
fn CGContextSetFontSmoothingStyle(c: CGContextRef, style: c_int);
fn CGContextSetAllowsAntialiasing(c: CGContextRef, allowsAntialiasing: bool);
fn CGContextSetShouldAntialias(c: CGContextRef, shouldAntialias: bool);
fn CGContextSetAllowsFontSubpixelQuantization(c: CGContextRef,
fn CGContextSetAllowsFontSmoothing(c: ::sys::CGContextRef, allowsFontSmoothing: bool);
fn CGContextSetShouldSmoothFonts(c: ::sys::CGContextRef, shouldSmoothFonts: bool);
fn CGContextSetFontSmoothingStyle(c: ::sys::CGContextRef, style: c_int);
fn CGContextSetAllowsAntialiasing(c: ::sys::CGContextRef, allowsAntialiasing: bool);
fn CGContextSetShouldAntialias(c: ::sys::CGContextRef, shouldAntialias: bool);
fn CGContextSetAllowsFontSubpixelQuantization(c: ::sys::CGContextRef,
allowsFontSubpixelQuantization: bool);
fn CGContextSetShouldSubpixelQuantizeFonts(c: CGContextRef,
fn CGContextSetShouldSubpixelQuantizeFonts(c: ::sys::CGContextRef,
shouldSubpixelQuantizeFonts: bool);
fn CGContextSetAllowsFontSubpixelPositioning(c: CGContextRef,
fn CGContextSetAllowsFontSubpixelPositioning(c: ::sys::CGContextRef,
allowsFontSubpixelPositioning: bool);
fn CGContextSetShouldSubpixelPositionFonts(c: CGContextRef,
fn CGContextSetShouldSubpixelPositionFonts(c: ::sys::CGContextRef,
shouldSubpixelPositionFonts: bool);
fn CGContextSetTextDrawingMode(c: CGContextRef, mode: CGTextDrawingMode);
fn CGContextSetRGBFillColor(context: CGContextRef,
fn CGContextSetTextDrawingMode(c: ::sys::CGContextRef, mode: CGTextDrawingMode);
fn CGContextSetRGBFillColor(context: ::sys::CGContextRef,
red: CGFloat,
green: CGFloat,
blue: CGFloat,
alpha: CGFloat);
fn CGContextFillRect(context: CGContextRef,
fn CGContextFillRect(context: ::sys::CGContextRef,
rect: CGRect);
fn CGContextDrawImage(c: CGContextRef, rect: CGRect, image: ::sys::CGImageRef);
fn CGContextDrawImage(c: ::sys::CGContextRef, rect: CGRect, image: ::sys::CGImageRef);
}
+3
View File
@@ -13,4 +13,7 @@ pub type CGFontRef = *mut CGFont;
pub enum CGEvent {}
pub type CGEventRef = *mut CGEvent;
pub enum CGContext {}
pub type CGContextRef = *mut CGContext;