1//! This file contains implementations for types that are
2//! re-exported in skia-safe.
3//!
4//! We could provide trait implementations in skia-safe, but then users of the library would have to
5//! import the implementation type _and_ the trait.
6//!
7//! See also: <https://github.com/rust-lang/rfcs/issues/1880>
8
9use crate::{SkAlphaType, SkBlendMode, SkBlendModeCoeff, SkPathFillType, SkPathVerb, SkPath_Verb};
10use std::ffi::CStr;
11
12impl SkBlendMode {
13 pub fn as_coeff(self) -> Option<(SkBlendModeCoeff, SkBlendModeCoeff)> {
14 let mut src: SkBlendModeCoeff = SkBlendModeCoeff::Zero;
15 let mut dst: SkBlendModeCoeff = SkBlendModeCoeff::Zero;
16 if unsafe { crate::SkBlendMode_AsCoeff(self, &mut src, &mut dst) } {
17 Some((src, dst))
18 } else {
19 None
20 }
21 }
22
23 pub fn name(self) -> &'static str {
24 unsafe {
25 let name_ptr: *const i8 = crate::SkBlendMode_Name(self);
26 CStr::from_ptr(name_ptr).to_str().unwrap()
27 }
28 }
29}
30
31//
32// m84 introduced two different variants of the Path verb types.
33// One with Done and one without.
34//
35
36impl SkPathVerb {
37 /// The maximum number of points an iterator will return for the verb.
38 pub const MAX_POINTS: usize = SkPath_Verb::MAX_POINTS;
39 /// The number of points an iterator will return for the verb.
40 pub fn points(self) -> usize {
41 SkPath_Verb::from(self).points()
42 }
43}
44
45impl SkPath_Verb {
46 /// The maximum number of points an iterator will return for the verb.
47 pub const MAX_POINTS: usize = 4;
48 /// The number of points an iterator will return for the verb.
49 pub fn points(self) -> usize {
50 match self {
51 SkPath_Verb::Move => 1,
52 SkPath_Verb::Line => 2,
53 SkPath_Verb::Quad => 3,
54 SkPath_Verb::Conic => 3,
55 SkPath_Verb::Cubic => 4,
56 SkPath_Verb::Close => 0,
57 SkPath_Verb::Done => 0,
58 }
59 }
60}
61
62impl From<SkPathVerb> for SkPath_Verb {
63 fn from(v: SkPathVerb) -> Self {
64 match v {
65 SkPathVerb::Move => SkPath_Verb::Move,
66 SkPathVerb::Line => SkPath_Verb::Line,
67 SkPathVerb::Quad => SkPath_Verb::Quad,
68 SkPathVerb::Conic => SkPath_Verb::Conic,
69 SkPathVerb::Cubic => SkPath_Verb::Cubic,
70 SkPathVerb::Close => SkPath_Verb::Close,
71 }
72 }
73}
74
75impl SkPathFillType {
76 pub fn is_even_odd(self) -> bool {
77 (self as i32 & 1) != 0
78 }
79
80 pub fn is_inverse(self) -> bool {
81 (self as i32 & 2) != 0
82 }
83
84 #[must_use]
85 pub fn to_non_inverse(self) -> Self {
86 use SkPathFillType::*;
87 match self {
88 Winding => self,
89 EvenOdd => self,
90 InverseWinding => Winding,
91 InverseEvenOdd => EvenOdd,
92 }
93 }
94}
95
96impl SkAlphaType {
97 pub fn is_opaque(self) -> bool {
98 self == SkAlphaType::Opaque
99 }
100}
101
102#[cfg(feature = "gl")]
103impl From<crate::GrGLenum> for crate::GrGLFormat {
104 fn from(e: crate::GrGLenum) -> Self {
105 unsafe { crate::C_GrGLFormatFromGLEnum(glFormat:e) }
106 }
107}
108
109#[cfg(feature = "gl")]
110impl From<crate::GrGLFormat> for crate::GrGLenum {
111 fn from(format: crate::GrGLFormat) -> Self {
112 unsafe { crate::C_GrGLFormatToEnum(format) }
113 }
114}
115
116#[cfg(feature = "d3d")]
117mod d3d {
118 use std::marker::PhantomData;
119
120 impl<T> Default for crate::gr_cp<T> {
121 fn default() -> Self {
122 Self {
123 fObject: std::ptr::null_mut(),
124 _phantom_0: PhantomData,
125 }
126 }
127 }
128
129 impl Default for crate::GrD3DTextureResourceInfo {
130 fn default() -> Self {
131 let mut instance: MaybeUninit<{unknown}> = std::mem::MaybeUninit::uninit();
132 unsafe {
133 crate::C_GrD3DTextureResourceInfo_Construct(instance.as_mut_ptr());
134 instance.assume_init()
135 }
136 }
137 }
138}
139