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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::Script;
use glib::translate::*;
use glib::GString;

#[doc(alias = "PangoLanguage")]
pub struct Language(*mut ffi::PangoLanguage);

unsafe impl Send for Language {}
unsafe impl Sync for Language {}

#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *mut ffi::PangoLanguage> for &'a Language {
    type Storage = &'a Language;

    fn to_glib_none(&self) -> Stash<'a, *mut ffi::PangoLanguage, Self> {
        Stash(self.0, *self)
    }
}

#[doc(hidden)]
impl<'a> ToGlibPtrMut<'a, *mut ffi::PangoLanguage> for Language {
    type Storage = &'a mut Self;

    #[inline]
    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::PangoLanguage, Self> {
        StashMut(self.0, self)
    }
}

#[doc(hidden)]
impl FromGlibPtrNone<*mut ffi::PangoLanguage> for Language {
    unsafe fn from_glib_none(ptr: *mut ffi::PangoLanguage) -> Self {
        assert!(!ptr.is_null());
        Self(ptr)
    }
}

#[doc(hidden)]
impl FromGlibPtrFull<*mut ffi::PangoLanguage> for Language {
    unsafe fn from_glib_full(ptr: *mut ffi::PangoLanguage) -> Self {
        assert!(!ptr.is_null());
        Self(ptr)
    }
}

#[doc(hidden)]
impl FromGlibPtrNone<*const ffi::PangoLanguage> for Language {
    unsafe fn from_glib_none(ptr: *const ffi::PangoLanguage) -> Self {
        assert!(!ptr.is_null());
        Self(ptr as *mut _)
    }
}

#[doc(hidden)]
impl FromGlibPtrFull<*const ffi::PangoLanguage> for Language {
    unsafe fn from_glib_full(ptr: *const ffi::PangoLanguage) -> Self {
        assert!(!ptr.is_null());
        Self(ptr as *mut _)
    }
}

impl Default for Language {
    #[doc(alias = "pango_language_get_default")]
    fn default() -> Self {
        unsafe { from_glib_full(ffi::pango_language_get_default()) }
    }
}

impl Language {
    #[doc(alias = "pango_language_from_string")]
    pub fn from_string(language: &str) -> Self {
        unsafe { from_glib_full(ffi::pango_language_from_string(language.to_glib_none().0)) }
    }

    #[doc(alias = "pango_language_to_string")]
    pub fn to_string(&self) -> GString {
        unsafe { from_glib_none(ffi::pango_language_to_string(self.to_glib_none().0)) }
    }

    #[doc(alias = "pango_language_matches")]
    pub fn matches(&self, range_list: &str) -> bool {
        unsafe {
            from_glib(ffi::pango_language_matches(
                self.to_glib_none().0,
                range_list.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "pango_language_includes_script")]
    pub fn includes_script(&self, script: Script) -> bool {
        unsafe {
            from_glib(ffi::pango_language_includes_script(
                self.to_glib_none().0,
                script.into_glib(),
            ))
        }
    }

    #[doc(alias = "get_scripts")]
    #[doc(alias = "pango_language_get_scripts")]
    pub fn scripts(&self) -> Vec<Script> {
        let mut num_scripts = 0;
        let mut ret = Vec::new();

        unsafe {
            let scripts: *const ffi::PangoScript =
                ffi::pango_language_get_scripts(self.to_glib_none().0, &mut num_scripts);
            if num_scripts > 0 {
                for x in 0..num_scripts {
                    ret.push(from_glib(
                        *(scripts.offset(x as isize) as *const ffi::PangoScript),
                    ));
                }
            }
            ret
        }
    }

    #[doc(alias = "get_sample_string")]
    #[doc(alias = "pango_language_get_sample_string")]
    pub fn sample_string(&self) -> GString {
        unsafe { from_glib_none(ffi::pango_language_get_sample_string(self.to_glib_none().0)) }
    }

    #[cfg(any(feature = "v1_48", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_48")))]
    #[doc(alias = "get_preferred")]
    #[doc(alias = "pango_language_get_preferred")]
    pub fn preferred(&self) -> Vec<Language> {
        unsafe {
            let langs = ffi::pango_language_get_preferred();
            let mut ptr = langs;

            let mut ret = vec![];

            while !(*ptr).is_null() {
                ret.push(Language(*ptr));
                ptr = ptr.add(1);
            }

            ret
        }
    }
}