1+ #![ allow( non_upper_case_globals) ]
2+
13//! core graphics gradient support
24
35use core_foundation:: {
@@ -16,14 +18,15 @@ use core_graphics::{
1618use piet:: kurbo:: Point ;
1719use piet:: { Color , FixedGradient , FixedLinearGradient , FixedRadialGradient , GradientStop } ;
1820
21+ //FIXME: remove all this when core-graphics 0.20.0 is released
1922// core-graphics does not provide a CGGradient type
2023pub enum CGGradientT { }
2124pub type CGGradientRef = * mut CGGradientT ;
25+ pub type CGGradientDrawingOptions = u32 ;
26+ pub const CGGradientDrawsBeforeStartLocation : CGGradientDrawingOptions = 1 ;
27+ pub const CGGradientDrawsAfterEndLocation : CGGradientDrawingOptions = 1 << 1 ;
2228
23- declare_TCFType ! {
24- CGGradient , CGGradientRef
25- }
26-
29+ declare_TCFType ! ( CGGradient , CGGradientRef ) ;
2730impl_TCFType ! ( CGGradient , CGGradientRef , CGGradientGetTypeID ) ;
2831
2932/// A wrapper around CGGradient
@@ -53,8 +56,7 @@ impl Gradient {
5356 . unwrap_or ( Color :: BLACK )
5457 }
5558
56- pub ( crate ) fn fill ( & self , ctx : & mut CGContextRef ) {
57- let context_ref: * mut u8 = ctx as * mut CGContextRef as * mut u8 ;
59+ pub ( crate ) fn fill ( & self , ctx : & mut CGContextRef , options : CGGradientDrawingOptions ) {
5860 match self . piet_grad {
5961 FixedGradient :: Radial ( FixedRadialGradient {
6062 center,
@@ -63,16 +65,16 @@ impl Gradient {
6365 ..
6466 } ) => {
6567 let start_center = to_cgpoint ( center + origin_offset) ;
66- let center = to_cgpoint ( center) ;
68+ let end_center = to_cgpoint ( center) ;
6769 unsafe {
6870 CGContextDrawRadialGradient (
69- context_ref ,
71+ ctx ,
7072 self . cg_grad . as_concrete_TypeRef ( ) ,
7173 start_center,
72- 0.0 ,
73- center ,
74+ 0.0 , // start_radius
75+ end_center ,
7476 radius as CGFloat ,
75- 0 ,
77+ options ,
7678 )
7779 }
7880 }
@@ -81,7 +83,7 @@ impl Gradient {
8183 let end = to_cgpoint ( end) ;
8284 unsafe {
8385 CGContextDrawLinearGradient (
84- context_ref ,
86+ ctx ,
8587 self . cg_grad . as_concrete_TypeRef ( ) ,
8688 start,
8789 end,
@@ -97,23 +99,19 @@ fn new_cg_gradient(stops: &[GradientStop]) -> CGGradient {
9799 unsafe {
98100 //FIXME: is this expensive enough we should be reusing it?
99101 let space = CGColorSpace :: create_with_name ( kCGColorSpaceSRGB) . unwrap ( ) ;
100- let space_ref: * const u8 = & * space as * const CGColorSpaceRef as * const u8 ;
101102 let mut colors = Vec :: < CGColor > :: new ( ) ;
102103 let mut locations = Vec :: < CGFloat > :: new ( ) ;
103104 for GradientStop { pos, color } in stops {
104105 let ( r, g, b, a) = Color :: as_rgba ( & color) ;
105- let color = CGColorCreate ( space_ref as * const u8 , [ r, g, b, a] . as_ptr ( ) ) ;
106+ let color = CGColorCreate ( & * space , [ r, g, b, a] . as_ptr ( ) ) ;
106107 let color = CGColor :: wrap_under_create_rule ( color) ;
107108 colors. push ( color) ;
108109 locations. push ( * pos as CGFloat ) ;
109110 }
110111
111112 let colors = CFArray :: from_CFTypes ( & colors) ;
112- let gradient = CGGradientCreateWithColors (
113- space_ref as * const u8 ,
114- colors. as_concrete_TypeRef ( ) ,
115- locations. as_ptr ( ) ,
116- ) ;
113+ let gradient =
114+ CGGradientCreateWithColors ( & * space, colors. as_concrete_TypeRef ( ) , locations. as_ptr ( ) ) ;
117115
118116 CGGradient :: wrap_under_create_rule ( gradient)
119117 }
@@ -126,21 +124,26 @@ fn to_cgpoint(point: Point) -> CGPoint {
126124#[ link( name = "CoreGraphics" , kind = "framework" ) ]
127125extern "C" {
128126 fn CGGradientGetTypeID ( ) -> CFTypeID ;
127+ //CGColorSpaceRef is missing repr(c).
128+ #[ allow( improper_ctypes) ]
129129 fn CGGradientCreateWithColors (
130- space : * const u8 ,
130+ space : * const CGColorSpaceRef ,
131131 colors : CFArrayRef ,
132132 locations : * const CGFloat ,
133133 ) -> CGGradientRef ;
134- fn CGColorCreate ( space : * const u8 , components : * const CGFloat ) -> SysCGColorRef ;
134+ #[ allow( improper_ctypes) ]
135+ fn CGColorCreate ( space : * const CGColorSpaceRef , components : * const CGFloat ) -> SysCGColorRef ;
136+ #[ allow( improper_ctypes) ]
135137 fn CGContextDrawLinearGradient (
136- ctx : * mut u8 ,
138+ ctx : * mut CGContextRef ,
137139 gradient : CGGradientRef ,
138140 startPoint : CGPoint ,
139141 endPoint : CGPoint ,
140142 options : u32 ,
141143 ) ;
144+ #[ allow( improper_ctypes) ]
142145 fn CGContextDrawRadialGradient (
143- ctx : * mut u8 ,
146+ ctx : * mut CGContextRef ,
144147 gradient : CGGradientRef ,
145148 startCenter : CGPoint ,
146149 startRadius : CGFloat ,
0 commit comments