pango/
language.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::str::FromStr;
4
5use glib::translate::*;
6
7use crate::{ffi, Language, Script};
8
9unsafe impl Send for Language {}
10unsafe impl Sync for Language {}
11
12impl Language {
13    /// Determines the scripts used to to write @self.
14    ///
15    /// If nothing is known about the language tag @self,
16    /// or if @self is [`None`], then [`None`] is returned.
17    /// The list of scripts returned starts with the script that the
18    /// language uses most and continues to the one it uses least.
19    ///
20    /// The value @num_script points at will be set to the number
21    /// of scripts in the returned array (or zero if [`None`] is returned).
22    ///
23    /// Most languages use only one script for writing, but there are
24    /// some that use two (Latin and Cyrillic for example), and a few
25    /// use three (Japanese for example). Applications should not make
26    /// any assumptions on the maximum number of scripts returned
27    /// though, except that it is positive if the return value is not
28    /// [`None`], and it is a small number.
29    ///
30    /// The [`includes_script()`][Self::includes_script()] function uses this
31    /// function internally.
32    ///
33    /// Note: while the return value is declared as [`Script`][crate::Script], the
34    /// returned values are from the `GUnicodeScript` enumeration, which
35    /// may have more values. Callers need to handle unknown values.
36    ///
37    /// # Returns
38    ///
39    ///
40    ///   An array of [`Script`][crate::Script] values, with the number of entries in
41    ///   the array stored in @num_scripts, or [`None`] if Pango does not have
42    ///   any information about this particular language tag (also the case
43    ///   if @self is [`None`]).
44    #[doc(alias = "get_scripts")]
45    #[doc(alias = "pango_language_get_scripts")]
46    pub fn scripts(&self) -> Vec<Script> {
47        let mut num_scripts = 0;
48        let mut ret = Vec::new();
49
50        unsafe {
51            let scripts: *const ffi::PangoScript = ffi::pango_language_get_scripts(
52                mut_override(self.to_glib_none().0),
53                &mut num_scripts,
54            );
55            if num_scripts > 0 {
56                for x in 0..num_scripts {
57                    ret.push(from_glib(
58                        *(scripts.offset(x as isize) as *const ffi::PangoScript),
59                    ));
60                }
61            }
62            ret
63        }
64    }
65}
66
67impl FromStr for Language {
68    type Err = std::convert::Infallible;
69
70    fn from_str(language: &str) -> Result<Self, Self::Err> {
71        Ok(Self::from_string(language))
72    }
73}