geolith 0.2.0

Shared library of the geolith builders: source readers, conflation, admin areas, tile encoding and the PMTiles and MBTiles writers
Documentation
//! Unicode script of a name, as the protomaps basemap tiles record it.
//!
//! Port of `Script.java` from protomaps/basemaps (BSD-3-Clause, see
//! `NOTICE.md`). The style compares the `script` attribute with the script of
//! the language it draws, so the names here must match the Java ones
//! (`Character.UnicodeScript` names, first letter upper case, the rest lower
//! case).

use unicode_script::{Script, UnicodeScript};

/// The script of one character, `None` for the characters that carry no
/// script of their own (digits, punctuation, marks, unassigned code points).
#[must_use]
pub(crate) fn char_script(ch: char) -> Option<Script> {
    match ch.script() {
        Script::Common | Script::Inherited | Script::Unknown => None,
        script => Some(script),
    }
}

/// The Java name of a script: `HAN` is `Han`, `OLD_ITALIC` is `Old_italic`.
fn java_name(script: Script) -> String {
    let mut name = script.full_name().replace(' ', "_");
    name.make_ascii_lowercase();
    if let Some(first) = name.get_mut(0..1) {
        first.make_ascii_uppercase();
    }
    name
}

/// The script of `text`.
///
/// `Generic` when no character has a script of its own (empty text, digits,
/// punctuation), `Mixed-Japanese` when several scripts occur and one of them
/// is Hiragana or Katakana, `Mixed` when several scripts occur, else the one
/// script (`Latin`, `Arabic`, `Han`, `Devanagari`, ...).
#[must_use]
pub fn script_of(text: &str) -> String {
    let mut overall: Option<Script> = None;
    for ch in text.chars() {
        let Some(script) = char_script(ch) else {
            continue;
        };
        match overall {
            None => overall = Some(script),
            Some(first) if first != script => {
                let japanese = text
                    .chars()
                    .any(|c| matches!(c.script(), Script::Hiragana | Script::Katakana));
                return if japanese {
                    "Mixed-Japanese".to_string()
                } else {
                    "Mixed".to_string()
                };
            }
            Some(_) => {}
        }
    }
    overall.map_or_else(|| "Generic".to_string(), java_name)
}

#[cfg(test)]
mod tests {
    use super::*;

    // Expected values: protomaps/basemaps `ScriptTest.java`, which run on the
    // Java implementation this module ports.
    #[test]
    fn single_script_names() {
        for (text, script) in [
            ("Berlin", "Latin"),
            ("دبي", "Arabic"),
            ("Скопје", "Cyrillic"),
            ("北京", "Han"),
            ("አዲስ አበባ", "Ethiopic"),
            ("Երևան", "Armenian"),
            ("서울", "Hangul"),
            ("თბილისი", "Georgian"),
            ("Αθήνα", "Greek"),
            ("ᠤᠯᠠᠭᠠᠨᠪᠠᠭᠠᠲᠤᠷ", "Mongolian"),
            ("काठमाडौँ", "Devanagari"),
            ("תל אביב", "Hebrew"),
            ("ಬೆಂಗಳೂರು", "Kannada"),
            ("ঢাকা", "Bengali"),
            ("ရန်ကုန်", "Myanmar"),
            ("ភ្នំពេញ", "Khmer"),
            ("ປາກເຊ", "Lao"),
            ("കൊല്ലം", "Malayalam"),
            ("ਜਲੰਧਰ", "Gurmukhi"),
            ("කොළඹ", "Sinhala"),
            ("கொழும்பு", "Tamil"),
            ("హైదరాబాద్", "Telugu"),
            ("กรุงเทพมหานคร", "Thai"),
            ("パリス", "Katakana"),
            ("さいたま", "Hiragana"),
        ] {
            assert_eq!(script_of(text), script, "{text}");
        }
    }

    #[test]
    fn text_without_a_script_is_generic() {
        for text in ["", " ", "123", "!?-.,"] {
            assert_eq!(script_of(text), "Generic", "{text:?}");
        }
    }

    #[test]
    fn digits_and_punctuation_do_not_change_the_script() {
        assert_eq!(script_of("Αθήνα 123"), "Greek");
        assert_eq!(script_of("台北101"), "Han");
        assert_eq!(script_of("Berlin!"), "Latin");
        assert_eq!(script_of("Zürich"), "Latin");
    }

    #[test]
    fn several_scripts_are_mixed() {
        assert_eq!(script_of("Berlin Αθήνα"), "Mixed");
        assert_eq!(script_of("काठमाडौँ கொழும்பு"), "Mixed");
        assert_eq!(script_of("つつじケ丘五丁目"), "Mixed-Japanese");
    }

    #[test]
    fn a_script_name_with_an_underscore_follows_the_java_spelling() {
        // Java: UnicodeScript.OLD_ITALIC.toString() is "OLD_ITALIC".
        assert_eq!(script_of("\u{10300}"), "Old_italic");
    }
}