1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
///! The contents of this module imitate the internal BLVariant structure
use bitflags::bitflags;

use crate::{error::expect_mem_err, image::Image, path::Path, pattern::Pattern, region::Region};

use ffi::BLImplType::*;
bl_enum! {
    pub enum ImplType {
        Null                 = BL_IMPL_TYPE_NULL,
        BitArray             = BL_IMPL_TYPE_BIT_ARRAY,
        String               = BL_IMPL_TYPE_STRING,
        ArrayVar             = BL_IMPL_TYPE_ARRAY_VAR,
        ArrayI8              = BL_IMPL_TYPE_ARRAY_I8,
        ArrayU8              = BL_IMPL_TYPE_ARRAY_U8,
        ArrayI16             = BL_IMPL_TYPE_ARRAY_I16,
        ArrayU16             = BL_IMPL_TYPE_ARRAY_U16,
        ArrayI32             = BL_IMPL_TYPE_ARRAY_I32,
        ArrayU32             = BL_IMPL_TYPE_ARRAY_U32,
        ArrayI64             = BL_IMPL_TYPE_ARRAY_I64,
        ArrayU64             = BL_IMPL_TYPE_ARRAY_U64,
        ArrayF32             = BL_IMPL_TYPE_ARRAY_F32,
        ArrayF64             = BL_IMPL_TYPE_ARRAY_F64,
        ArrayStruct1         = BL_IMPL_TYPE_ARRAY_STRUCT_1,
        ArrayStruct2         = BL_IMPL_TYPE_ARRAY_STRUCT_2,
        ArrayStruct3         = BL_IMPL_TYPE_ARRAY_STRUCT_3,
        ArrayStruct4         = BL_IMPL_TYPE_ARRAY_STRUCT_4,
        ArrayStruct6         = BL_IMPL_TYPE_ARRAY_STRUCT_6,
        ArrayStruct8         = BL_IMPL_TYPE_ARRAY_STRUCT_8,
        ArrayStruct10        = BL_IMPL_TYPE_ARRAY_STRUCT_10,
        ArrayStruct12        = BL_IMPL_TYPE_ARRAY_STRUCT_12,
        ArrayStruct16        = BL_IMPL_TYPE_ARRAY_STRUCT_16,
        ArrayStruct20        = BL_IMPL_TYPE_ARRAY_STRUCT_20,
        ArrayStruct24        = BL_IMPL_TYPE_ARRAY_STRUCT_24,
        ArrayStruct32        = BL_IMPL_TYPE_ARRAY_STRUCT_32,
        Path                 = BL_IMPL_TYPE_PATH,
        Region               = BL_IMPL_TYPE_REGION,
        Image                = BL_IMPL_TYPE_IMAGE,
        ImageCodec           = BL_IMPL_TYPE_IMAGE_CODEC,
        ImageDecoder         = BL_IMPL_TYPE_IMAGE_DECODER,
        ImageEncoder         = BL_IMPL_TYPE_IMAGE_ENCODER,
        Gradient             = BL_IMPL_TYPE_GRADIENT,
        Pattern              = BL_IMPL_TYPE_PATTERN,
        Context              = BL_IMPL_TYPE_CONTEXT,
        Font                 = BL_IMPL_TYPE_FONT,
        FontFace             = BL_IMPL_TYPE_FONT_FACE,
        FontData             = BL_IMPL_TYPE_FONT_DATA,
        FontLoader           = BL_IMPL_TYPE_FONT_LOADER,
        FontFeatureOptions   = BL_IMPL_TYPE_FONT_FEATURE_OPTIONS,
        FontVariationOptions = BL_IMPL_TYPE_FONT_VARIATION_OPTIONS,
    }
    Default => Null
}

bitflags! {
    pub struct ImplTraits: u8 {
        const NULL =      ffi::BLImplTraits::BL_IMPL_TRAIT_NULL as u8;
        const VIRTUAL =   ffi::BLImplTraits::BL_IMPL_TRAIT_VIRT as u8;
        const MUTABLE =   ffi::BLImplTraits::BL_IMPL_TRAIT_MUTABLE as u8;
        const IMMUTABLE = ffi::BLImplTraits::BL_IMPL_TRAIT_IMMUTABLE as u8;
        const EXTERNAL =  ffi::BLImplTraits::BL_IMPL_TRAIT_EXTERNAL as u8;
        const FOREIGN =   ffi::BLImplTraits::BL_IMPL_TRAIT_FOREIGN as u8;
    }
}

/// Marker trait for virtual function table struct/
pub trait VTable {}

impl VTable for ffi::BLContextVirt {}
impl VTable for ffi::BLFontDataVirt {}
impl VTable for ffi::BLFontLoaderVirt {}
impl VTable for ffi::BLImageCodecVirt {}
impl VTable for ffi::BLImageDecoderVirt {}
impl VTable for ffi::BLImageEncoderVirt {}

pub unsafe trait BlVariantImpl: Sized {
    type VTable;

    #[inline]
    fn virt(&self) -> &Self::VTable
    where
        Self::VTable: VTable,
    {
        unsafe { &*(self.as_variant_impl().__bindgen_anon_1.virt as *const _ as *const _) }
    }

    #[inline]
    fn ref_count(&self) -> usize {
        self.as_variant_impl().refCount
    }

    #[inline]
    fn impl_type(&self) -> ImplType {
        (self.as_variant_impl().implType as u32).into()
    }

    #[inline]
    fn impl_traits(&self) -> ImplTraits {
        ImplTraits::from_bits_truncate(self.as_variant_impl().implTraits)
    }

    #[inline]
    fn as_variant_impl(&self) -> &ffi::BLVariantImpl {
        unsafe { &*(self as *const _ as *const _) }
    }
}

unsafe impl BlVariantImpl for ffi::BLArrayImpl {
    type VTable = ();
}
unsafe impl BlVariantImpl for ffi::BLContextImpl {
    type VTable = ffi::BLContextVirt;
}
unsafe impl BlVariantImpl for ffi::BLGradientImpl {
    type VTable = ();
}
unsafe impl BlVariantImpl for ffi::BLFontImpl {
    type VTable = ();
}
unsafe impl BlVariantImpl for ffi::BLFontDataImpl {
    type VTable = ffi::BLFontDataVirt;
}
unsafe impl BlVariantImpl for ffi::BLFontFaceImpl {
    type VTable = ffi::BLFontFaceVirt;
}
unsafe impl BlVariantImpl for ffi::BLFontLoaderImpl {
    type VTable = ffi::BLFontLoaderVirt;
}
unsafe impl BlVariantImpl for ffi::BLImageImpl {
    type VTable = ();
}
unsafe impl BlVariantImpl for ffi::BLImageCodecImpl {
    type VTable = ffi::BLImageCodecVirt;
}
unsafe impl BlVariantImpl for ffi::BLImageDecoderImpl {
    type VTable = ffi::BLImageDecoderVirt;
}
unsafe impl BlVariantImpl for ffi::BLImageEncoderImpl {
    type VTable = ffi::BLImageDecoderVirt;
}
unsafe impl BlVariantImpl for ffi::BLPathImpl {
    type VTable = ();
}
unsafe impl BlVariantImpl for ffi::BLPatternImpl {
    type VTable = ();
}
unsafe impl BlVariantImpl for ffi::BLRegionImpl {
    type VTable = ();
}
unsafe impl BlVariantImpl for ffi::BLStringImpl {
    type VTable = ();
}
unsafe impl BlVariantImpl for ffi::BLVariantImpl {
    type VTable = ();
}

pub unsafe trait BlVariantCore: Sized {
    type Impl: BlVariantImpl;

    #[inline]
    fn as_variant_core(&self) -> &ffi::BLVariantCore {
        unsafe { &*(self as *const _ as *const _) }
    }

    #[inline]
    fn impl_(&self) -> &Self::Impl {
        unsafe { &*(self.as_variant_core().impl_ as *const _) }
    }

    #[inline]
    fn impl_mut(&mut self) -> &mut Self::Impl {
        unsafe { &mut *(self.as_variant_core().impl_ as *mut _) }
    }

    #[inline]
    fn init_weak(&self, other: &mut Self) {
        unsafe { ffi::blVariantInitWeak(other as *mut _ as *mut _, self as *const _ as *const _) };
    }
}

unsafe impl BlVariantCore for ffi::BLArrayCore {
    type Impl = ffi::BLArrayImpl;
}
unsafe impl BlVariantCore for ffi::BLContextCore {
    type Impl = ffi::BLContextImpl;
}
unsafe impl BlVariantCore for ffi::BLGradientCore {
    type Impl = ffi::BLGradientImpl;
}
unsafe impl BlVariantCore for ffi::BLFontCore {
    type Impl = ffi::BLFontImpl;
}
unsafe impl BlVariantCore for ffi::BLFontDataCore {
    type Impl = ffi::BLFontDataImpl;
}
unsafe impl BlVariantCore for ffi::BLFontFaceCore {
    type Impl = ffi::BLFontFaceImpl;
}
unsafe impl BlVariantCore for ffi::BLFontLoaderCore {
    type Impl = ffi::BLFontLoaderImpl;
}
unsafe impl BlVariantCore for ffi::BLImageCore {
    type Impl = ffi::BLImageImpl;
}
unsafe impl BlVariantCore for ffi::BLImageCodecCore {
    type Impl = ffi::BLImageCodecImpl;
}
unsafe impl BlVariantCore for ffi::BLImageDecoderCore {
    type Impl = ffi::BLImageDecoderImpl;
}
unsafe impl BlVariantCore for ffi::BLImageEncoderCore {
    type Impl = ffi::BLImageEncoderImpl;
}
unsafe impl BlVariantCore for ffi::BLPathCore {
    type Impl = ffi::BLPathImpl;
}
unsafe impl BlVariantCore for ffi::BLPatternCore {
    type Impl = ffi::BLPatternImpl;
}
unsafe impl BlVariantCore for ffi::BLRegionCore {
    type Impl = ffi::BLRegionImpl;
}
unsafe impl BlVariantCore for ffi::BLStringCore {
    type Impl = ffi::BLStringImpl;
}
unsafe impl BlVariantCore for ffi::BLVariantCore {
    type Impl = ffi::BLVariantImpl;
}

/// Implementing type must be either:
///     #[repr(transparent)] and its only field may be a struct that contains a
///     pointer to a BlxxxxImpl
///
///     #[repr(C)] and its first field must be a pointer to its core's
///     [`Impl`] type
pub unsafe trait WrappedBlCore: Sized {
    type Core: BlVariantCore;
    const IMPL_TYPE_INDEX: usize;

    fn from_core(core: Self::Core) -> Self;

    /// The default implementation reinterprets &self as &Self::Core.
    #[inline]
    fn core(&self) -> &Self::Core {
        unsafe { &*(self as *const _ as *const _) }
    }

    /// The default implementation reinterprets &mut self as &mut Self::Core.
    #[inline]
    fn core_mut(&mut self) -> &mut Self::Core {
        unsafe { &mut *(self as *mut _ as *mut _) }
    }

    #[inline]
    fn impl_(&self) -> &<Self::Core as BlVariantCore>::Impl {
        self.core().impl_()
    }

    #[inline]
    fn impl_mut(&mut self) -> &mut <Self::Core as BlVariantCore>::Impl {
        self.core_mut().impl_mut()
    }

    /// Checks whether the wrapped implementation is a none object.
    #[inline]
    fn is_none(&self) -> bool {
        self.impl_().impl_traits().contains(ImplTraits::NULL)
    }

    /// Retrieves the none version of Self::Core
    #[inline]
    fn none() -> &'static Self::Core {
        unsafe { &*(&ffi::blNone[Self::IMPL_TYPE_INDEX] as *const _ as *const _) }
    }

    /// Checks equality of the objects implementations by comparing the pointer.
    #[inline]
    fn impl_equals(&self, other: &Self) -> bool {
        self.impl_() as *const _ == other.impl_() as *const _
    }

    /// Creates a weak refcount copy.
    #[inline]
    fn init_weak(&self) -> Self::Core {
        let mut other = unsafe { core::mem::zeroed() };
        self.core().init_weak(&mut other);
        other
    }
}

type BlAssignDeep<C> = unsafe extern "C" fn(*mut C, *const C) -> ffi::BLResult;

/// A trait for deep cloning the object. This is different from [Clone] for
/// blObjects in the regard that normal cloning only creates a weak
/// reference-counted clone.
pub trait DeepClone
where
    Self: WrappedBlCore,
    <Self as WrappedBlCore>::Core: Copy + 'static,
{
    #[doc(hidden)]
    const ASSIGN_DEEP: BlAssignDeep<<Self as WrappedBlCore>::Core>;
    /// Returns a deeply cloned copy of the value.
    fn clone_deep(&self) -> Self {
        let mut new = Self::from_core(*Self::none());
        unsafe { expect_mem_err(Self::ASSIGN_DEEP(new.core_mut(), self.core())) };
        new
    }
}

impl DeepClone for Image {
    const ASSIGN_DEEP: BlAssignDeep<Self::Core> = ffi::blImageAssignDeep;
}
impl DeepClone for Path {
    const ASSIGN_DEEP: BlAssignDeep<Self::Core> = ffi::blPathAssignDeep;
}

// Fails to compile on rust stable but works fine on nightly(E0277). I believe
// this might be related to https://github.com/rust-lang/rust/issues/24159,
// but im unsure as to how one could circumvent it
/*impl<T> DeepClone for Array<T>
where
    T: ArrayType,
    Array<T>: WrappedBlCore,
    <Array<T> as WrappedBlCore>::Core: Copy + 'static,
{
    const ASSIGN_DEEP: BlAssignDeep<Self::Core> = ffi::blArrayAssignDeep;
}*/

impl DeepClone for Region {
    const ASSIGN_DEEP: BlAssignDeep<Self::Core> = ffi::blRegionAssignDeep;
}
impl DeepClone for Pattern {
    const ASSIGN_DEEP: BlAssignDeep<Self::Core> = ffi::blPatternAssignDeep;
}