Skip to content

Commit

Permalink
Add tagged string test
Browse files Browse the repository at this point in the history
  • Loading branch information
maxammann committed Aug 20, 2024
1 parent aae185d commit fe9145e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
2 changes: 1 addition & 1 deletion maplibre/src/sdf/bidi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeSet};
use std::collections::BTreeSet;
use widestring::U16String;

pub type Char16 = u16; // was char16_t
Expand Down
2 changes: 1 addition & 1 deletion maplibre/src/sdf/font_stack.rs
Original file line number Diff line number Diff line change
@@ -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<String>;
Expand Down
2 changes: 1 addition & 1 deletion maplibre/src/sdf/glyph_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions maplibre/src/sdf/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion maplibre/src/sdf/shaping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
76 changes: 76 additions & 0 deletions maplibre/src/sdf/tagged_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

0 comments on commit fe9145e

Please sign in to comment.