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

use std::str::FromStr;

use crate::{Language, Script};
use glib::translate::*;

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

impl Language {
    /// 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 [`Script`][crate::Script], 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`]).
    #[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(
                mut_override(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
        }
    }

    /// Returns the list of languages that the user prefers.
    ///
    /// The list is specified by the `PANGO_LANGUAGE` or `LANGUAGE`
    /// environment variables, in order of preference. Note that this
    /// list does not necessarily include the language returned by
    /// [`default()`][Self::default()].
    ///
    /// When choosing language-specific resources, such as the sample
    /// text returned by [`sample_string()`][Self::sample_string()],
    /// you should first try the default language, followed by the
    /// languages returned by this function.
    ///
    /// # Returns
    ///
    /// a [`None`]-terminated array
    ///   of [`Language`][crate::Language]*
    #[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() -> Vec<Self> {
        unsafe {
            let ptr = ffi::pango_language_get_preferred();
            Self::from_glib_full_as_vec(ptr)
        }
    }

    pub fn to_string(&self) -> glib::GString {
        self.to_str()
    }
}

impl FromStr for Language {
    type Err = std::convert::Infallible;
    fn from_str(language: &str) -> Result<Self, Self::Err> {
        Ok(Self::from_string(language))
    }
}

impl Default for Language {
    fn default() -> Self {
        Self::default()
    }
}