From 4cb47493b0f7362b06f87a3c49419087ee18ab54 Mon Sep 17 00:00:00 2001 From: James Campos Date: Sat, 18 Jul 2020 02:44:34 -0700 Subject: unicode --- src/main.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6bee84e..90a60cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -262,20 +262,34 @@ impl View for Page { } let c = bk.chap(); let (start, end) = c.lines[bk.line + row as usize]; - // FIXME unicode width - let byte = start + (col - bk.pad()) as usize; - if byte > end { + let line_col = (col - bk.pad()) as usize; + + let mut cols = 0; + let mut found = false; + let mut byte = start; + for (i, c) in c.text[start..end].char_indices() { + cols += c.width().unwrap(); + if cols > line_col { + byte += i; + found = true; + break; + } + } + + if !found { return; } + let r = c.links.binary_search_by(|&(start, end, _)| { if start > byte { Ordering::Greater - } else if end < byte { + } else if end <= byte { Ordering::Less } else { Ordering::Equal } }); + if let Ok(i) = r { let url = &c.links[i].2; let &(chapter, byte) = bk.links.get(url).unwrap(); -- cgit v1.2.3