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
// 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;

/// The [`Language`][crate::Language] structure is used to
/// represent a language.
///
/// [`Language`][crate::Language] pointers can be efficiently
/// copied and compared with each other.
#[doc(alias = "PangoLanguage")]
pub struct Language(*mut ffi::PangoLanguage);

#[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 {
    /// Take a RFC-3066 format language tag as a string and convert it to a
    /// [`Language`][crate::Language] pointer that can be efficiently copied (copy the
    /// pointer) and compared with other language tags (compare the
    /// pointer.)
    ///
    /// This function first canonicalizes the string by converting it to
    /// lowercase, mapping '_' to '-', and stripping all characters other
    /// than letters and '-'.
    ///
    /// Use [`default()`][Self::default()] if you want to get the [`Language`][crate::Language] for
    /// the current locale of the process.
    /// ## `language`
    /// a string representing a language tag, or [`None`]
    ///
    /// # Returns
    ///
    /// an opaque pointer to a
    ///  [`Language`][crate::Language] structure, or [`None`] if `language` was
    ///  [`None`]. The returned pointer will be valid forever
    ///  after, and should not be freed.
    #[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)) }
    }

    /// Gets the RFC-3066 format string representing the given language tag.
    ///
    /// # Returns
    ///
    /// a string representing the language tag. This is owned by
    ///  Pango and should not be freed.
    #[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)) }
    }

    /// Checks if a language tag matches one of the elements in a list of
    /// language ranges. A language tag is considered to match a range
    /// in the list if the range is '*', the range is exactly the tag,
    /// or the range is a prefix of the tag, and the character after it
    /// in the tag is '-'.
    /// ## `range_list`
    /// a list of language ranges, separated by ';', ':',
    ///  ',', or space characters.
    ///  Each element must either be '*', or a RFC 3066 language range
    ///  canonicalized as by [`from_string()`][Self::from_string()]
    ///
    /// # Returns
    ///
    /// [`true`] if a match was found.
    #[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,
            ))
        }
    }

    /// Determines if `script` is one of the scripts used to
    /// write `self`. The returned value is conservative;
    /// if nothing is known about the language tag `self`,
    /// [`true`] will be returned, since, as far as Pango knows,
    /// `script` might be used to write `self`.
    ///
    /// This routine is used in Pango's itemization process when
    /// determining if a supplied language tag is relevant to
    /// a particular section of text. It probably is not useful for
    /// applications in most circumstances.
    ///
    /// This function uses [`scripts()`][Self::scripts()] internally.
    /// ## `script`
    /// a [`Script`][crate::Script]
    ///
    /// # Returns
    ///
    /// [`true`] if `script` is one of the scripts used
    /// to write `self` or if nothing is known about `self`
    /// (including the case that `self` is [`None`]),
    /// [`false`] otherwise.
    #[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(),
            ))
        }
    }

    /// Determines the scripts used to to write `self`.
    /// If nothing is known about the language tag `self`,
    /// or if `self` is [`None`], then [`None`] is returned.
    /// The list of scripts returned starts with the script that the
    /// language uses most and continues to the one it uses least.
    ///
    /// The value `num_script` points at will be set to the number
    /// of scripts in the returned array (or zero if [`None`] is returned).
    ///
    /// Most languages use only one script for writing, but there are
    /// some that use two (Latin and Cyrillic for example), and a few
    /// use three (Japanese for example). Applications should not make
    /// any assumptions on the maximum number of scripts returned
    /// though, except that it is positive if the return value is not
    /// [`None`], and it is a small number.
    ///
    /// The [`includes_script()`][Self::includes_script()] function uses this function
    /// internally.
    ///
    /// Note: while the return value is declared as PangoScript, the
    /// returned values are from the GUnicodeScript enumeration, which
    /// may have more values. Callers need to handle unknown values.
    ///
    /// # Returns
    ///
    /// An array of
    /// [`Script`][crate::Script] values, with the number of entries in the array stored
    /// in `num_scripts`, or [`None`] if Pango does not have any information
    /// about this particular language tag (also the case if `self` is
    /// [`None`]). The returned array is owned by Pango and should not be
    /// modified or freed.
    #[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
        }
    }

    /// Get a string that is representative of the characters needed to
    /// render a particular language.
    ///
    /// The sample text may be a pangram, but is not necessarily. It is chosen to
    /// be demonstrative of normal text in the language, as well as exposing font
    /// feature requirements unique to the language. It is suitable for use
    /// as sample text in a font selection dialog.
    ///
    /// If `self` is [`None`], the default language as found by
    /// [`default()`][Self::default()] is used.
    ///
    /// If Pango does not have a sample string for `self`, the classic
    /// "The quick brown fox..." is returned. This can be detected by
    /// comparing the returned pointer value to that returned for (non-existent)
    /// language code "xx". That is, compare to:
    /// `<informalexample>``<programlisting>`
    /// pango_language_get_sample_string (pango_language_from_string ("xx"))
    /// `</programlisting>``</informalexample>`
    ///
    /// # Returns
    ///
    /// the sample string. This value is owned by Pango
    ///  and should not be freed.
    #[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)) }
    }
}