MCPcopy Create free account
hub / github.com/TheRedDeveloper/ply-engine / measure_text_cached

Method measure_text_cached

src/engine.rs:2056–2192  ·  view source on GitHub ↗
(
        &mut self,
        text: &str,
        config: &TextConfig,
    )

Source from the content-addressed store, hash-verified

2054 }
2055
2056 fn measure_text_cached(
2057 &mut self,
2058 text: &str,
2059 config: &TextConfig,
2060 ) -> MeasureTextCacheItem {
2061 match &self.measure_text_fn {
2062 Some(_) => {},
2063 None => {
2064 if !self.boolean_warnings.text_measurement_fn_not_set {
2065 self.boolean_warnings.text_measurement_fn_not_set = true;
2066 }
2067 return MeasureTextCacheItem::default();
2068 }
2069 };
2070
2071 let id = hash_string_contents_with_config(text, config);
2072
2073 // Check cache
2074 if let Some(item) = self.measure_text_cache.get_mut(&id) {
2075 item.generation = self.generation;
2076 return *item;
2077 }
2078
2079 // Not cached - measure now
2080 let text_data = text.as_bytes();
2081 let text_length = text_data.len() as i32;
2082
2083 let space_str = " ";
2084 let space_width = (self.measure_text_fn.as_ref().unwrap())(space_str, config).width;
2085
2086 let mut start: i32 = 0;
2087 let mut end: i32 = 0;
2088 let mut line_width: f32 = 0.0;
2089 let mut measured_width: f32 = 0.0;
2090 let mut measured_height: f32 = 0.0;
2091 let mut min_width: f32 = 0.0;
2092 let mut contains_newlines = false;
2093
2094 let mut temp_word_next: i32 = -1;
2095 let mut previous_word_index: i32 = -1;
2096
2097 while end < text_length {
2098 let current = text_data[end as usize];
2099 if current == b' ' || current == b'\n' {
2100 let length = end - start;
2101 let mut dimensions = Dimensions::default();
2102 if length > 0 {
2103 let substr =
2104 core::str::from_utf8(&text_data[start as usize..end as usize]).unwrap();
2105 dimensions = (self.measure_text_fn.as_ref().unwrap())(substr, config);
2106 }
2107 min_width = f32::max(dimensions.width, min_width);
2108 measured_height = f32::max(measured_height, dimensions.height);
2109
2110 if current == b' ' {
2111 dimensions.width += space_width;
2112 let word = MeasuredWord {
2113 start_offset: start,

Callers 2

open_text_elementMethod · 0.80
wrap_textMethod · 0.80

Calls 2

add_measured_wordMethod · 0.80

Tested by

no test coverage detected