From fe9145e6cda4aa06c497b05b59fa44f40b99d169 Mon Sep 17 00:00:00 2001 From: Max Ammann Date: Tue, 20 Aug 2024 20:16:21 +0100 Subject: [PATCH] Add tagged string test --- maplibre/src/sdf/bidi.rs | 2 +- maplibre/src/sdf/font_stack.rs | 2 +- maplibre/src/sdf/glyph_atlas.rs | 2 +- maplibre/src/sdf/i18n.rs | 3 +- maplibre/src/sdf/shaping.rs | 2 +- maplibre/src/sdf/tagged_string.rs | 76 +++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 6 deletions(-) diff --git a/maplibre/src/sdf/bidi.rs b/maplibre/src/sdf/bidi.rs index 5d4625c0a..dd776766b 100644 --- a/maplibre/src/sdf/bidi.rs +++ b/maplibre/src/sdf/bidi.rs @@ -1,4 +1,4 @@ -use std::collections::{BTreeSet}; +use std::collections::BTreeSet; use widestring::U16String; pub type Char16 = u16; // was char16_t diff --git a/maplibre/src/sdf/font_stack.rs b/maplibre/src/sdf/font_stack.rs index da271070d..0a3766783 100644 --- a/maplibre/src/sdf/font_stack.rs +++ b/maplibre/src/sdf/font_stack.rs @@ -1,6 +1,6 @@ use crate::sdf::util::hash_combine; use crate::style::layer::StyleLayer; -use std::collections::{BTreeSet}; +use std::collections::BTreeSet; // An array of font names pub type FontStack = Vec; diff --git a/maplibre/src/sdf/glyph_atlas.rs b/maplibre/src/sdf/glyph_atlas.rs index 357413148..354955d72 100644 --- a/maplibre/src/sdf/glyph_atlas.rs +++ b/maplibre/src/sdf/glyph_atlas.rs @@ -2,7 +2,7 @@ use crate::euclid::Rect; use crate::sdf::font_stack::FontStackHash; use crate::sdf::glyph::{GlyphID, GlyphMap, GlyphMetrics}; use crate::sdf::GlyphSpace; -use std::collections::{BTreeMap}; +use std::collections::BTreeMap; // TODO structs pub struct AlphaImage; diff --git a/maplibre/src/sdf/i18n.rs b/maplibre/src/sdf/i18n.rs index d8a8695b6..bdfd5d7cc 100644 --- a/maplibre/src/sdf/i18n.rs +++ b/maplibre/src/sdf/i18n.rs @@ -14,8 +14,7 @@ pub fn allowsWordBreaking(chr: Char16) -> bool { || chr == 0xb7 /* middle dot */ || chr == 0x200b /* zero-width space */ || chr == 0x2010 /* hyphen */ - || chr == 0x2013 /* en dash */ - ); + || chr == 0x2013/* en dash */); } pub fn charAllowsLetterSpacing(chr: Char16) -> bool { diff --git a/maplibre/src/sdf/shaping.rs b/maplibre/src/sdf/shaping.rs index f97ca75e2..75718b067 100644 --- a/maplibre/src/sdf/shaping.rs +++ b/maplibre/src/sdf/shaping.rs @@ -10,7 +10,7 @@ use crate::sdf::style_types::{IconTextFitType, SymbolAnchorType, TextJustifyType use crate::sdf::tagged_string::{SectionOptions, TaggedString}; use crate::sdf::{i18n, GlyphSpace}; use cgmath::num_traits::Pow; -use std::collections::{BTreeSet}; +use std::collections::BTreeSet; #[derive(Clone, Copy, Default, PartialEq)] pub struct Padding { diff --git a/maplibre/src/sdf/tagged_string.rs b/maplibre/src/sdf/tagged_string.rs index 60e54a195..65e9d8c2c 100644 --- a/maplibre/src/sdf/tagged_string.rs +++ b/maplibre/src/sdf/tagged_string.rs @@ -59,6 +59,18 @@ pub struct TaggedString { pub imageSectionID: Char16, } +impl Default for TaggedString { + /// Returns an empty string + fn default() -> Self { + Self { + styledText: (U16String::new(), vec![]), // TODO is this correct? + sections: vec![], + supportsVerticalWritingMode: None, + imageSectionID: 0 as Char16, // TODO is this correct? + } + } +} + impl TaggedString { pub fn new_from_raw(text_: U16String, options: SectionOptions) -> Self { let text_len = text_.len(); @@ -228,3 +240,67 @@ impl TaggedString { return Some(self.imageSectionID); } } + +#[cfg(test)] +mod tests { + use crate::sdf::bidi::Char16; + use crate::sdf::i18n::BACKSLACK_V; + use crate::sdf::tagged_string::{SectionOptions, TaggedString}; + use widestring::U16String; + + #[test] + fn TaggedString_Trim() { + let mut basic = TaggedString::new_from_raw( + " \t\ntrim that and not this \n\t".into(), + SectionOptions::new(1.0, vec![], None), + ); + basic.trim(); + assert_eq!(basic.rawText(), &U16String::from("trim that and not this")); + + let mut twoSections = TaggedString::default(); + twoSections.addTextSection(&" \t\ntrim that".into(), 1.5, vec![], None); + twoSections.addTextSection(&" and not this \n\t".into(), 0.5, vec![], None); + + twoSections.trim(); + assert_eq!( + twoSections.rawText(), + &U16String::from("trim that and not this") + ); + + let mut empty = TaggedString::new_from_raw( + format!( + "\n\t{} \r \t\n", + char::from_u32(BACKSLACK_V as u32).unwrap() + ) + .into(), + SectionOptions::new(1.0, vec![], None), + ); + empty.trim(); + assert_eq!(empty.rawText(), &U16String::from("")); + + let mut noTrim = + TaggedString::new_from_raw("no trim!".into(), SectionOptions::new(1.0, vec![], None)); + noTrim.trim(); + assert_eq!(noTrim.rawText(), &U16String::from("no trim!")); + } + #[test] + fn TaggedString_ImageSections() { + let mut string = TaggedString::new_from_raw(U16String::new(), SectionOptions::default()); + string.addImageSection("image_name".to_string()); + assert_eq!(string.rawText(), &U16String::from("\u{E000}")); + assert!(string.getSection(0).imageID.is_some()); + assert_eq!( + string.getSection(0).imageID.as_ref().unwrap(), + &"image_name".to_string() + ); + + let mut maxSections = TaggedString::default(); + for i in 0..6401 { + maxSections.addImageSection(i.to_string()); + } + + assert_eq!(maxSections.getSections().len(), 6400); + assert_eq!(maxSections.getCharCodeAt(0), '\u{E000}' as Char16); + assert_eq!(maxSections.getCharCodeAt(6399), '\u{F8FF}' as Char16); + } +}