iced_code_editor/canvas_editor/
metrics.rs1use iced::advanced::text::{
5 Alignment, Paragraph, Renderer as TextRenderer, Text,
6};
7use std::cmp::Ordering as CmpOrdering;
8use unicode_width::UnicodeWidthChar;
9
10use crate::canvas_editor::CodeEditor;
11
12pub(crate) const FONT_SIZE: f32 = 14.0;
14pub(crate) const LINE_HEIGHT: f32 = 20.0;
15pub(crate) const CHAR_WIDTH: f32 = 8.4; pub(crate) const TAB_WIDTH: usize = 4;
17pub(crate) const GUTTER_WIDTH: f32 = 45.0;
18pub(crate) const FOLD_MARGIN_WIDTH: f32 = 14.0;
21pub(crate) const CURSOR_BLINK_INTERVAL: std::time::Duration =
22 std::time::Duration::from_millis(530);
23
24pub(crate) fn measure_char_width(
36 c: char,
37 full_char_width: f32,
38 char_width: f32,
39) -> f32 {
40 if c == '\t' {
41 return char_width * TAB_WIDTH as f32;
42 }
43 match c.width() {
44 Some(w) if w > 1 => full_char_width,
45 Some(_) => char_width,
46 None => 0.0,
47 }
48}
49
50pub(crate) fn measure_text_width(
66 text: &str,
67 full_char_width: f32,
68 char_width: f32,
69) -> f32 {
70 text.chars()
71 .map(|c| measure_char_width(c, full_char_width, char_width))
72 .sum()
73}
74
75pub(crate) const EPSILON: f32 = 0.001;
77pub(crate) const CACHE_WINDOW_MARGIN_MULTIPLIER: usize = 2;
84pub(crate) const HIGHLIGHT_LINES_PER_FRAME: usize = 2_000;
92
93pub(crate) fn compare_floats(a: f32, b: f32) -> CmpOrdering {
106 if (a - b).abs() < EPSILON {
107 CmpOrdering::Equal
108 } else if a > b {
109 CmpOrdering::Greater
110 } else {
111 CmpOrdering::Less
112 }
113}
114
115impl CodeEditor {
116 pub fn set_font(&mut self, font: iced::Font) {
122 self.font = font;
123 self.recalculate_char_dimensions(false);
124 }
125
126 pub fn set_font_size(&mut self, size: f32, auto_adjust_line_height: bool) {
136 self.font_size = size;
137 self.recalculate_char_dimensions(auto_adjust_line_height);
138 }
139
140 pub(crate) fn recalculate_char_dimensions(
142 &mut self,
143 auto_adjust_line_height: bool,
144 ) {
145 self.char_width = self.measure_single_char_width("a");
146 self.full_char_width = self.measure_single_char_width("汉");
148
149 if self.char_width.is_infinite() {
151 self.char_width = self.font_size / 2.0; }
153
154 if self.full_char_width.is_infinite() {
155 self.full_char_width = self.font_size;
156 }
157
158 if auto_adjust_line_height {
159 let line_height_ratio = LINE_HEIGHT / FONT_SIZE;
160 self.line_height = self.font_size * line_height_ratio;
161 }
162
163 self.content_cache.clear();
164 self.overlay_cache.clear();
165 *self.max_content_width_cache.borrow_mut() = None;
166 }
167
168 fn measure_single_char_width(&self, content: &str) -> f32 {
170 let text = Text {
171 content,
172 font: self.font,
173 size: iced::Pixels(self.font_size),
174 line_height: iced::advanced::text::LineHeight::default(),
175 bounds: iced::Size::new(f32::INFINITY, f32::INFINITY),
176 align_x: Alignment::Left,
177 align_y: iced::alignment::Vertical::Top,
178 shaping: iced::advanced::text::Shaping::Advanced,
179 wrapping: iced::advanced::text::Wrapping::default(),
180 };
181 let p = <iced::Renderer as TextRenderer>::Paragraph::with_text(text);
182 p.min_width()
183 }
184
185 pub fn font_size(&self) -> f32 {
191 self.font_size
192 }
193
194 pub fn char_width(&self) -> f32 {
200 self.char_width
201 }
202
203 pub fn full_char_width(&self) -> f32 {
209 self.full_char_width
210 }
211
212 pub fn measure_text_width(&self, text: &str) -> f32 {
214 measure_text_width(text, self.full_char_width, self.char_width)
215 }
216
217 pub fn set_line_height(&mut self, height: f32) {
223 self.line_height = height;
224 self.content_cache.clear();
225 self.overlay_cache.clear();
226 }
227
228 pub fn line_height(&self) -> f32 {
234 self.line_height
235 }
236
237 pub fn viewport_height(&self) -> f32 {
239 self.viewport_height
240 }
241
242 pub fn viewport_width(&self) -> f32 {
244 self.viewport_width
245 }
246
247 pub fn viewport_scroll(&self) -> f32 {
249 self.viewport_scroll
250 }
251
252 #[must_use]
274 pub fn with_viewport_height(mut self, height: f32) -> Self {
275 self.viewport_height = height;
276 self
277 }
278
279 pub(crate) fn gutter_width(&self) -> f32 {
285 self.line_number_gutter_width() + self.fold_margin_width()
286 }
287
288 pub(crate) fn line_number_gutter_width(&self) -> f32 {
290 if self.line_numbers_enabled { GUTTER_WIDTH } else { 0.0 }
291 }
292
293 pub(crate) fn fold_margin_width(&self) -> f32 {
296 if self.folding_enabled { FOLD_MARGIN_WIDTH } else { 0.0 }
297 }
298}
299
300#[cfg(test)]
301mod tests {
302 use super::*;
303
304 #[test]
305 fn test_compare_floats() {
306 assert_eq!(
308 compare_floats(1.0, 1.0),
309 CmpOrdering::Equal,
310 "Exact equality"
311 );
312 assert_eq!(
313 compare_floats(1.0, 1.0 + 0.0001),
314 CmpOrdering::Equal,
315 "Within epsilon (positive)"
316 );
317 assert_eq!(
318 compare_floats(1.0, 1.0 - 0.0001),
319 CmpOrdering::Equal,
320 "Within epsilon (negative)"
321 );
322
323 assert_eq!(
325 compare_floats(1.0 + 0.002, 1.0),
326 CmpOrdering::Greater,
327 "Definitely greater"
328 );
329 assert_eq!(
330 compare_floats(1.0011, 1.0),
331 CmpOrdering::Greater,
332 "Just above epsilon"
333 );
334
335 assert_eq!(
337 compare_floats(1.0, 1.0 + 0.002),
338 CmpOrdering::Less,
339 "Definitely less"
340 );
341 assert_eq!(
342 compare_floats(1.0, 1.0011),
343 CmpOrdering::Less,
344 "Just below negative epsilon"
345 );
346 }
347
348 #[test]
349 fn test_measure_text_width_ascii() {
350 let text = "abc";
352 let width = measure_text_width(text, FONT_SIZE, CHAR_WIDTH);
353 let expected = CHAR_WIDTH * 3.0;
354 assert_eq!(
355 compare_floats(width, expected),
356 CmpOrdering::Equal,
357 "Width mismatch for ASCII"
358 );
359 }
360
361 #[test]
362 fn test_measure_text_width_cjk() {
363 let text = "你好";
367 let width = measure_text_width(text, FONT_SIZE, CHAR_WIDTH);
368 let expected = FONT_SIZE * 2.0;
369 assert_eq!(
370 compare_floats(width, expected),
371 CmpOrdering::Equal,
372 "Width mismatch for CJK"
373 );
374 }
375
376 #[test]
377 fn test_measure_text_width_mixed() {
378 let text = "Hi你好";
381 let width = measure_text_width(text, FONT_SIZE, CHAR_WIDTH);
382 let expected = CHAR_WIDTH * 2.0 + FONT_SIZE * 2.0;
383 assert_eq!(
384 compare_floats(width, expected),
385 CmpOrdering::Equal,
386 "Width mismatch for mixed content"
387 );
388 }
389
390 #[test]
391 fn test_measure_text_width_control_chars() {
392 let text = "\t\n";
395 let width = measure_text_width(text, FONT_SIZE, CHAR_WIDTH);
396 let expected = CHAR_WIDTH * TAB_WIDTH as f32;
397 assert_eq!(
398 compare_floats(width, expected),
399 CmpOrdering::Equal,
400 "Width mismatch for control chars"
401 );
402 }
403
404 #[test]
405 fn test_measure_text_width_empty() {
406 let text = "";
407 let width = measure_text_width(text, FONT_SIZE, CHAR_WIDTH);
408 assert!(
409 (width - 0.0).abs() < f32::EPSILON,
410 "Width should be 0 for empty string"
411 );
412 }
413
414 #[test]
415 fn test_measure_text_width_emoji() {
416 let text = "👋";
418 let width = measure_text_width(text, FONT_SIZE, CHAR_WIDTH);
419 let expected = FONT_SIZE;
420 assert_eq!(
421 compare_floats(width, expected),
422 CmpOrdering::Equal,
423 "Width mismatch for emoji"
424 );
425 }
426
427 #[test]
428 fn test_measure_text_width_korean() {
429 let text = "안녕하세요";
433 let width = measure_text_width(text, FONT_SIZE, CHAR_WIDTH);
434 let expected = FONT_SIZE * 5.0;
435 assert_eq!(
436 compare_floats(width, expected),
437 CmpOrdering::Equal,
438 "Width mismatch for Korean"
439 );
440 }
441
442 #[test]
443 fn test_measure_text_width_japanese() {
444 let text_hiragana = "こんにちは";
449 let width_hiragana =
450 measure_text_width(text_hiragana, FONT_SIZE, CHAR_WIDTH);
451 let expected_hiragana = FONT_SIZE * 5.0;
452 assert_eq!(
453 compare_floats(width_hiragana, expected_hiragana),
454 CmpOrdering::Equal,
455 "Width mismatch for Hiragana"
456 );
457
458 let text_katakana = "カタカナ";
459 let width_katakana =
460 measure_text_width(text_katakana, FONT_SIZE, CHAR_WIDTH);
461 let expected_katakana = FONT_SIZE * 4.0;
462 assert_eq!(
463 compare_floats(width_katakana, expected_katakana),
464 CmpOrdering::Equal,
465 "Width mismatch for Katakana"
466 );
467
468 let text_kanji = "漢字";
469 let width_kanji = measure_text_width(text_kanji, FONT_SIZE, CHAR_WIDTH);
470 let expected_kanji = FONT_SIZE * 2.0;
471 assert_eq!(
472 compare_floats(width_kanji, expected_kanji),
473 CmpOrdering::Equal,
474 "Width mismatch for Kanji"
475 );
476 }
477
478 #[test]
479 fn test_set_font_size() {
480 let mut editor = CodeEditor::new("", "rs");
481
482 assert!((editor.font_size() - 14.0).abs() < f32::EPSILON);
484 assert!((editor.line_height() - 20.0).abs() < f32::EPSILON);
485
486 editor.set_font_size(28.0, true);
488 assert!((editor.font_size() - 28.0).abs() < f32::EPSILON);
489 assert_eq!(
491 compare_floats(editor.line_height(), 40.0),
492 CmpOrdering::Equal
493 );
494
495 editor.set_line_height(50.0);
498 editor.set_font_size(14.0, false);
500 assert!((editor.font_size() - 14.0).abs() < f32::EPSILON);
501 assert_eq!(
503 compare_floats(editor.line_height(), 50.0),
504 CmpOrdering::Equal
505 );
506 assert!(editor.char_width > 0.0);
510 assert!((editor.char_width - CHAR_WIDTH).abs() < 0.5);
511 }
512
513 #[test]
514 fn test_measure_single_char_width() {
515 let editor = CodeEditor::new("", "rs");
516
517 let width_a = editor.measure_single_char_width("a");
519 assert!(width_a > 0.0, "Width of 'a' should be positive");
520
521 let width_cjk = editor.measure_single_char_width("汉");
523 assert!(width_cjk > 0.0, "Width of '汉' should be positive");
524
525 assert!(
526 width_cjk > width_a,
527 "Width of '汉' should be greater than 'a'"
528 );
529
530 assert!(width_cjk >= width_a * 1.5);
533 }
534
535 #[test]
536 fn test_set_line_height() {
537 let mut editor = CodeEditor::new("", "rs");
538
539 assert!((editor.line_height() - LINE_HEIGHT).abs() < f32::EPSILON);
541
542 editor.set_line_height(35.0);
544 assert!((editor.line_height() - 35.0).abs() < f32::EPSILON);
545
546 assert!((editor.font_size() - FONT_SIZE).abs() < f32::EPSILON);
548 }
549}