( )
| 2455 | } |
| 2456 | |
| 2457 | pub fn create_measure_text_function( |
| 2458 | ) -> impl Fn(&str, &crate::TextConfig) -> crate::Dimensions + 'static { |
| 2459 | move |text: &str, config: &crate::TextConfig| { |
| 2460 | #[cfg(feature = "text-styling")] |
| 2461 | let cleaned_text = { |
| 2462 | // Remove macroquad_text_styling tags, handling escapes |
| 2463 | let mut result = String::new(); |
| 2464 | let mut in_style_def = false; |
| 2465 | let mut escaped = false; |
| 2466 | for c in text.chars() { |
| 2467 | if escaped { |
| 2468 | result.push(c); |
| 2469 | escaped = false; |
| 2470 | continue; |
| 2471 | } |
| 2472 | match c { |
| 2473 | '\\' => { |
| 2474 | escaped = true; |
| 2475 | } |
| 2476 | '{' => { |
| 2477 | in_style_def = true; |
| 2478 | } |
| 2479 | '|' => { |
| 2480 | if in_style_def { |
| 2481 | in_style_def = false; |
| 2482 | } else { |
| 2483 | result.push(c); |
| 2484 | } |
| 2485 | } |
| 2486 | '}' => { |
| 2487 | // Nothing |
| 2488 | } |
| 2489 | _ => { |
| 2490 | if !in_style_def { |
| 2491 | result.push(c); |
| 2492 | } |
| 2493 | } |
| 2494 | } |
| 2495 | } |
| 2496 | if in_style_def { |
| 2497 | warn!("Ended inside a style definition while cleaning text for measurement! Make sure to escape curly braces with \\. Here is what we tried to measure: {}", text); |
| 2498 | } |
| 2499 | result |
| 2500 | }; |
| 2501 | #[cfg(not(feature = "text-styling"))] |
| 2502 | let cleaned_text = text.to_string(); |
| 2503 | let mut fm = FONT_MANAGER.lock().unwrap(); |
| 2504 | // Resolve font: use asset font if available, otherwise default |
| 2505 | let font = if let Some(asset) = config.font_asset { |
| 2506 | fm.get(asset) |
| 2507 | } else { |
| 2508 | fm.get_default() |
| 2509 | }; |
| 2510 | let measured = macroquad::text::measure_text( |
| 2511 | &cleaned_text, |
| 2512 | font, |
| 2513 | config.font_size, |
| 2514 | 1.0, |
no test coverage detected