2022-10-24 08:56:48 -06:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2022-10-18 12:42:37 -06:00
|
|
|
use std::{
|
2022-10-25 10:55:24 -06:00
|
|
|
cmp,
|
2022-10-18 17:04:22 -06:00
|
|
|
fmt,
|
2022-10-18 12:42:37 -06:00
|
|
|
time::Instant,
|
|
|
|
|
};
|
2022-10-24 14:18:40 -06:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2022-10-18 12:07:22 -06:00
|
|
|
|
2022-10-25 12:52:46 -06:00
|
|
|
use crate::{LayoutGlyph, LayoutLine, FontMatches, FontShapeLine};
|
2022-10-18 12:07:22 -06:00
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// An action to perform on a [TextBuffer]
|
2022-10-18 17:04:22 -06:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
2022-10-18 12:07:22 -06:00
|
|
|
pub enum TextAction {
|
2022-10-21 12:08:49 -06:00
|
|
|
/// Move cursor to previous character ([Left] in LTR, [Right] in RTL)
|
|
|
|
|
Previous,
|
|
|
|
|
/// Move cursor to next character ([Right] in LTR, [Left] in RTL)
|
|
|
|
|
Next,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Move cursor left
|
2022-10-18 12:07:22 -06:00
|
|
|
Left,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Move cursor right
|
2022-10-18 12:07:22 -06:00
|
|
|
Right,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Move cursor up
|
2022-10-18 12:07:22 -06:00
|
|
|
Up,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Move cursor down
|
2022-10-18 12:07:22 -06:00
|
|
|
Down,
|
2022-10-19 11:08:15 -06:00
|
|
|
/// Move cursor to start of line
|
|
|
|
|
Home,
|
|
|
|
|
/// Move cursor to end of line
|
|
|
|
|
End,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Scroll up one page
|
2022-10-18 12:42:37 -06:00
|
|
|
PageUp,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Scroll down one page
|
2022-10-18 12:42:37 -06:00
|
|
|
PageDown,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Insert character at cursor
|
2022-10-18 12:07:22 -06:00
|
|
|
Insert(char),
|
2022-10-19 11:33:35 -06:00
|
|
|
/// Create new line
|
|
|
|
|
Enter,
|
|
|
|
|
/// Delete text behind cursor
|
|
|
|
|
Backspace,
|
|
|
|
|
/// Delete text in front of cursor
|
|
|
|
|
Delete,
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Mouse click at specified position
|
2022-10-18 17:04:22 -06:00
|
|
|
Click { x: i32, y: i32 },
|
2022-10-19 10:12:52 -06:00
|
|
|
/// Mouse drag to specified position
|
|
|
|
|
Drag { x: i32, y: i32 },
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Scroll specified number of lines
|
|
|
|
|
Scroll { lines: i32 },
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Current cursor location
|
2022-10-18 17:04:22 -06:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
2022-10-18 12:07:22 -06:00
|
|
|
pub struct TextCursor {
|
2022-10-20 19:34:56 -06:00
|
|
|
/// Text line the cursor is on
|
|
|
|
|
pub line: TextLineIndex,
|
2022-10-21 11:44:11 -06:00
|
|
|
/// Index of glyph at cursor (will insert behind this glyph)
|
|
|
|
|
pub index: usize,
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TextCursor {
|
2022-10-21 11:44:11 -06:00
|
|
|
pub const fn new(line: TextLineIndex, index: usize) -> Self {
|
|
|
|
|
Self { line, index }
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
struct TextLayoutCursor {
|
2022-10-22 11:42:08 -06:00
|
|
|
line: TextLineIndex,
|
|
|
|
|
layout: usize,
|
|
|
|
|
glyph: usize,
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
impl TextLayoutCursor {
|
2022-10-22 11:42:08 -06:00
|
|
|
fn new(line: TextLineIndex, layout: usize, glyph: usize) -> Self {
|
|
|
|
|
Self { line, layout, glyph }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
pub struct TextLayoutRun<'a> {
|
2022-10-25 12:52:46 -06:00
|
|
|
/// The index of the original text line
|
|
|
|
|
pub line_i: TextLineIndex,
|
|
|
|
|
/// The original text line
|
|
|
|
|
pub text: &'a str,
|
2022-10-25 13:15:52 -06:00
|
|
|
/// True if the original paragraph direction is RTL
|
2022-10-25 12:52:46 -06:00
|
|
|
pub rtl: bool,
|
|
|
|
|
/// The array of layout glyphs to draw
|
|
|
|
|
pub glyphs: &'a [LayoutGlyph],
|
|
|
|
|
/// Y offset of line
|
|
|
|
|
pub line_y: i32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct TextLayoutRunIter<'a> {
|
|
|
|
|
buffer: &'a TextBuffer<'a>,
|
|
|
|
|
line_i: usize,
|
|
|
|
|
layout_i: usize,
|
2022-10-25 11:40:10 -06:00
|
|
|
line_y: i32,
|
2022-10-25 12:52:46 -06:00
|
|
|
total_layout: i32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> TextLayoutRunIter<'a> {
|
|
|
|
|
pub fn new(buffer: &'a TextBuffer<'a>) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
buffer,
|
|
|
|
|
line_i: 0,
|
|
|
|
|
layout_i: 0,
|
|
|
|
|
line_y: buffer.metrics.font_size - buffer.metrics.line_height,
|
|
|
|
|
total_layout: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for TextLayoutRunIter<'a> {
|
|
|
|
|
type Item = TextLayoutRun<'a>;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
while let Some(line) = self.buffer.lines.get(self.line_i) {
|
|
|
|
|
let shape = line.shape_opt.as_ref()?;
|
|
|
|
|
let layout = line.layout_opt.as_ref()?;
|
|
|
|
|
while let Some(layout_line) = layout.get(self.layout_i) {
|
|
|
|
|
self.layout_i += 1;
|
|
|
|
|
|
|
|
|
|
let scrolled = self.total_layout < self.buffer.scroll;
|
|
|
|
|
self.total_layout += 1;
|
|
|
|
|
if scrolled {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.line_y += self.buffer.metrics.line_height;
|
|
|
|
|
if self.line_y > self.buffer.height {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Some(TextLayoutRun {
|
|
|
|
|
line_i: TextLineIndex::new(self.line_i),
|
|
|
|
|
text: line.text.as_str(),
|
|
|
|
|
rtl: shape.rtl,
|
|
|
|
|
glyphs: &layout_line.glyphs,
|
|
|
|
|
line_y: self.line_y,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
self.line_i += 1;
|
|
|
|
|
self.layout_i = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Index of a text line
|
2022-10-20 19:34:56 -06:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
|
2022-10-18 17:13:48 -06:00
|
|
|
pub struct TextLineIndex(usize);
|
|
|
|
|
|
|
|
|
|
impl TextLineIndex {
|
|
|
|
|
pub fn new(index: usize) -> Self {
|
|
|
|
|
Self(index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get(&self) -> usize {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Metrics of text
|
2022-10-18 17:04:22 -06:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
|
|
|
|
pub struct TextMetrics {
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Font size in pixels
|
2022-10-18 17:04:22 -06:00
|
|
|
pub font_size: i32,
|
2022-10-18 17:13:48 -06:00
|
|
|
/// Line height in pixels
|
2022-10-18 17:04:22 -06:00
|
|
|
pub line_height: i32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TextMetrics {
|
|
|
|
|
pub const fn new(font_size: i32, line_height: i32) -> Self {
|
|
|
|
|
Self { font_size, line_height }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub const fn scale(self, scale: i32) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
font_size: self.font_size * scale,
|
|
|
|
|
line_height: self.line_height * scale,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for TextMetrics {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(f, "{}px / {}px", self.font_size, self.line_height)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 10:13:44 -06:00
|
|
|
pub struct TextBufferLine {
|
2022-10-19 17:48:11 -06:00
|
|
|
text: String,
|
2022-10-25 10:13:44 -06:00
|
|
|
shape_opt: Option<FontShapeLine>,
|
2022-10-25 12:52:46 -06:00
|
|
|
layout_opt: Option<Vec<LayoutLine>>,
|
2022-10-19 17:48:11 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 10:13:44 -06:00
|
|
|
impl TextBufferLine {
|
2022-10-19 17:48:11 -06:00
|
|
|
pub fn new(text: String) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
text,
|
|
|
|
|
shape_opt: None,
|
|
|
|
|
layout_opt: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn text(&self) -> &str {
|
|
|
|
|
&self.text
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
|
self.shape_opt = None;
|
|
|
|
|
self.layout_opt = None;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 10:13:44 -06:00
|
|
|
pub fn shape(&mut self, font_matches: &FontMatches<'_>) -> &FontShapeLine {
|
2022-10-19 17:48:11 -06:00
|
|
|
if self.shape_opt.is_none() {
|
2022-10-20 19:34:56 -06:00
|
|
|
self.shape_opt = Some(font_matches.shape_line(&self.text));
|
2022-10-19 17:48:11 -06:00
|
|
|
self.layout_opt = None;
|
|
|
|
|
}
|
|
|
|
|
self.shape_opt.as_ref().unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 12:52:46 -06:00
|
|
|
pub fn layout(&mut self, font_matches: &FontMatches<'_>, font_size: i32, width: i32) -> &[LayoutLine] {
|
2022-10-19 17:48:11 -06:00
|
|
|
if self.layout_opt.is_none() {
|
|
|
|
|
let mut layout = Vec::new();
|
2022-10-20 19:34:56 -06:00
|
|
|
let shape = self.shape(font_matches);
|
2022-10-19 17:48:11 -06:00
|
|
|
shape.layout(
|
|
|
|
|
font_size,
|
|
|
|
|
width,
|
|
|
|
|
&mut layout,
|
|
|
|
|
0,
|
|
|
|
|
);
|
|
|
|
|
self.layout_opt = Some(layout);
|
|
|
|
|
}
|
|
|
|
|
self.layout_opt.as_ref().unwrap()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:13:48 -06:00
|
|
|
/// A buffer of text that is shaped and laid out
|
2022-10-18 12:07:22 -06:00
|
|
|
pub struct TextBuffer<'a> {
|
2022-10-25 11:10:44 -06:00
|
|
|
font_matches: FontMatches<'a>,
|
2022-10-25 10:13:44 -06:00
|
|
|
lines: Vec<TextBufferLine>,
|
2022-10-18 17:04:22 -06:00
|
|
|
metrics: TextMetrics,
|
2022-10-18 12:42:37 -06:00
|
|
|
width: i32,
|
|
|
|
|
height: i32,
|
2022-10-18 13:20:13 -06:00
|
|
|
scroll: i32,
|
2022-10-19 08:34:34 -06:00
|
|
|
cursor: TextCursor,
|
2022-10-19 10:12:52 -06:00
|
|
|
select_opt: Option<TextCursor>,
|
2022-10-18 12:07:22 -06:00
|
|
|
pub redraw: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> TextBuffer<'a> {
|
2022-10-18 12:42:37 -06:00
|
|
|
pub fn new(
|
2022-10-25 11:10:44 -06:00
|
|
|
font_matches: FontMatches<'a>,
|
2022-10-18 17:04:22 -06:00
|
|
|
metrics: TextMetrics,
|
2022-10-18 12:42:37 -06:00
|
|
|
) -> Self {
|
2022-10-19 17:48:11 -06:00
|
|
|
let mut buffer = Self {
|
2022-10-18 12:07:22 -06:00
|
|
|
font_matches,
|
2022-10-19 17:48:11 -06:00
|
|
|
lines: Vec::new(),
|
2022-10-18 17:04:22 -06:00
|
|
|
metrics,
|
|
|
|
|
width: 0,
|
|
|
|
|
height: 0,
|
|
|
|
|
scroll: 0,
|
2022-10-18 12:07:22 -06:00
|
|
|
cursor: TextCursor::default(),
|
2022-10-19 10:12:52 -06:00
|
|
|
select_opt: None,
|
2022-10-18 12:07:22 -06:00
|
|
|
redraw: false,
|
2022-10-19 17:48:11 -06:00
|
|
|
};
|
|
|
|
|
buffer.set_text("");
|
|
|
|
|
buffer
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
/// Pre-shape lines in the buffer, up to `lines`, return actual number of layout lines
|
|
|
|
|
pub fn shape_until(&mut self, lines: i32) -> i32 {
|
2022-10-18 12:07:22 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let mut reshaped = 0;
|
2022-10-20 19:34:56 -06:00
|
|
|
let mut total_layout = 0;
|
|
|
|
|
for line in self.lines.iter_mut() {
|
|
|
|
|
if total_layout >= lines {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if line.shape_opt.is_none() {
|
|
|
|
|
reshaped += 1;
|
|
|
|
|
}
|
|
|
|
|
let layout = line.layout(
|
2022-10-25 11:10:44 -06:00
|
|
|
&self.font_matches,
|
2022-10-20 19:34:56 -06:00
|
|
|
self.metrics.font_size,
|
|
|
|
|
self.width
|
|
|
|
|
);
|
|
|
|
|
total_layout += layout.len() as i32;
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
if reshaped > 0 {
|
|
|
|
|
log::debug!("shape_until {}: {:?}", reshaped, duration);
|
2022-10-20 19:34:56 -06:00
|
|
|
self.redraw = true;
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
2022-10-20 19:34:56 -06:00
|
|
|
|
|
|
|
|
total_layout
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 11:13:22 -06:00
|
|
|
/// Shape lines until cursor, also scrolling to include cursor in view
|
2022-10-21 12:31:02 -06:00
|
|
|
pub fn shape_until_cursor(&mut self) {
|
2022-10-21 09:02:15 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let mut reshaped = 0;
|
|
|
|
|
let mut layout_i = 0;
|
|
|
|
|
for (line_i, line) in self.lines.iter_mut().enumerate() {
|
|
|
|
|
if line_i > self.cursor.line.get() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if line.shape_opt.is_none() {
|
|
|
|
|
reshaped += 1;
|
|
|
|
|
}
|
|
|
|
|
let layout = line.layout(
|
2022-10-25 11:10:44 -06:00
|
|
|
&self.font_matches,
|
2022-10-21 09:02:15 -06:00
|
|
|
self.metrics.font_size,
|
|
|
|
|
self.width
|
|
|
|
|
);
|
|
|
|
|
if line_i == self.cursor.line.get() {
|
|
|
|
|
for layout_line in layout {
|
|
|
|
|
let mut found = false;
|
|
|
|
|
for glyph in layout_line.glyphs.iter() {
|
2022-10-21 11:44:11 -06:00
|
|
|
if glyph.start <= self.cursor.index {
|
2022-10-21 09:02:15 -06:00
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if found {
|
|
|
|
|
layout_i += 1;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
layout_i += layout.len() as i32;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
if reshaped > 0 {
|
|
|
|
|
log::debug!("shape_until_cursor {}: {:?}", reshaped, duration);
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let lines = self.lines();
|
2022-10-21 12:31:02 -06:00
|
|
|
if layout_i < self.scroll {
|
|
|
|
|
self.scroll = layout_i;
|
|
|
|
|
} else if layout_i >= self.scroll + lines {
|
|
|
|
|
self.scroll = layout_i - (lines - 1);
|
2022-10-21 09:02:15 -06:00
|
|
|
}
|
2022-10-21 12:31:02 -06:00
|
|
|
|
|
|
|
|
self.shape_until_scroll();
|
2022-10-21 09:02:15 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:33:48 -06:00
|
|
|
pub fn shape_until_scroll(&mut self) {
|
2022-10-18 13:05:36 -06:00
|
|
|
let lines = self.lines();
|
2022-10-18 12:42:37 -06:00
|
|
|
|
|
|
|
|
let scroll_end = self.scroll + lines;
|
2022-10-20 19:34:56 -06:00
|
|
|
let total_layout = self.shape_until(scroll_end);
|
2022-10-18 12:42:37 -06:00
|
|
|
|
|
|
|
|
self.scroll = cmp::max(
|
|
|
|
|
0,
|
|
|
|
|
cmp::min(
|
2022-10-20 19:34:56 -06:00
|
|
|
total_layout - (lines - 1),
|
2022-10-18 12:42:37 -06:00
|
|
|
self.scroll,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
fn relayout(&mut self) {
|
2022-10-18 12:07:22 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
for line in self.lines.iter_mut() {
|
|
|
|
|
if line.shape_opt.is_some() {
|
|
|
|
|
line.layout_opt = None;
|
|
|
|
|
line.layout(
|
2022-10-25 11:10:44 -06:00
|
|
|
&self.font_matches,
|
2022-10-20 19:34:56 -06:00
|
|
|
self.metrics.font_size,
|
|
|
|
|
self.width
|
|
|
|
|
);
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
2022-10-20 19:34:56 -06:00
|
|
|
log::debug!("relayout: {:?}", duration);
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
fn layout_cursor(&self, cursor: &TextCursor) -> TextLayoutCursor {
|
2022-10-22 11:42:08 -06:00
|
|
|
let line = &self.lines[cursor.line.get()];
|
|
|
|
|
|
|
|
|
|
let layout = line.layout_opt.as_ref().unwrap(); //TODO: ensure layout is done?
|
|
|
|
|
for (layout_i, layout_line) in layout.iter().enumerate() {
|
|
|
|
|
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
|
|
|
|
|
if cursor.index == glyph.start {
|
2022-10-25 11:40:10 -06:00
|
|
|
return TextLayoutCursor::new(
|
2022-10-22 11:42:08 -06:00
|
|
|
cursor.line,
|
|
|
|
|
layout_i,
|
|
|
|
|
glyph_i
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
match layout_line.glyphs.last() {
|
|
|
|
|
Some(glyph) => {
|
|
|
|
|
if cursor.index == glyph.end {
|
2022-10-25 11:40:10 -06:00
|
|
|
return TextLayoutCursor::new(
|
2022-10-22 11:42:08 -06:00
|
|
|
cursor.line,
|
|
|
|
|
layout_i,
|
|
|
|
|
layout_line.glyphs.len()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => {
|
2022-10-25 11:40:10 -06:00
|
|
|
return TextLayoutCursor::new(
|
2022-10-22 11:42:08 -06:00
|
|
|
cursor.line,
|
|
|
|
|
layout_i,
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fall back to start of line
|
|
|
|
|
//TODO: should this be the end of the line?
|
2022-10-25 11:40:10 -06:00
|
|
|
TextLayoutCursor::new(
|
2022-10-22 11:42:08 -06:00
|
|
|
cursor.line,
|
|
|
|
|
0,
|
|
|
|
|
0
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
fn set_layout_cursor(&mut self, cursor: TextLayoutCursor) {
|
2022-10-22 11:42:08 -06:00
|
|
|
let line = &mut self.lines[cursor.line.get()];
|
|
|
|
|
let layout = line.layout(
|
2022-10-25 11:10:44 -06:00
|
|
|
&self.font_matches,
|
2022-10-22 11:42:08 -06:00
|
|
|
self.metrics.font_size,
|
|
|
|
|
self.width
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let layout_line = match layout.get(cursor.layout) {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => match layout.last() {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => todo!("layout cursor in line with no layouts"),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let new_index = match layout_line.glyphs.get(cursor.glyph) {
|
|
|
|
|
Some(glyph) => glyph.start,
|
|
|
|
|
None => match layout_line.glyphs.last() {
|
|
|
|
|
Some(glyph) => glyph.end,
|
|
|
|
|
//TODO: is this correct?
|
|
|
|
|
None => 0,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if self.cursor.line != cursor.line || self.cursor.index != new_index {
|
|
|
|
|
self.cursor.line = cursor.line;
|
|
|
|
|
self.cursor.index = new_index;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 08:34:34 -06:00
|
|
|
/// Get the current cursor position
|
|
|
|
|
pub fn cursor(&self) -> TextCursor {
|
|
|
|
|
self.cursor
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the current [TextMetrics]
|
|
|
|
|
pub fn metrics(&self) -> TextMetrics {
|
|
|
|
|
self.metrics
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Set the current [TextMetrics]
|
|
|
|
|
pub fn set_metrics(&mut self, metrics: TextMetrics) {
|
|
|
|
|
if metrics != self.metrics {
|
|
|
|
|
self.metrics = metrics;
|
2022-10-18 12:42:37 -06:00
|
|
|
self.relayout();
|
2022-10-18 13:20:13 -06:00
|
|
|
self.shape_until_scroll();
|
2022-10-18 12:42:37 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the current buffer dimensions (width, height)
|
|
|
|
|
pub fn size(&self) -> (i32, i32) {
|
|
|
|
|
(self.width, self.height)
|
2022-10-18 13:05:36 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Set the current buffer dimensions
|
2022-10-18 12:42:37 -06:00
|
|
|
pub fn set_size(&mut self, width: i32, height: i32) {
|
|
|
|
|
if width != self.width {
|
|
|
|
|
self.width = width;
|
|
|
|
|
self.relayout();
|
2022-10-18 13:20:13 -06:00
|
|
|
self.shape_until_scroll();
|
2022-10-18 12:42:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if height != self.height {
|
|
|
|
|
self.height = height;
|
|
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the current scroll location
|
|
|
|
|
pub fn scroll(&self) -> i32 {
|
|
|
|
|
self.scroll
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the number of lines that can be viewed in the buffer
|
|
|
|
|
pub fn lines(&self) -> i32 {
|
|
|
|
|
self.height / self.metrics.line_height
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 13:15:07 -06:00
|
|
|
/// Set text of buffer
|
|
|
|
|
pub fn set_text(&mut self, text: &str) {
|
2022-10-19 17:48:11 -06:00
|
|
|
self.lines.clear();
|
|
|
|
|
for line in text.lines() {
|
|
|
|
|
self.lines.push(TextBufferLine::new(line.to_string()));
|
|
|
|
|
}
|
|
|
|
|
// Make sure there is always one line
|
|
|
|
|
if self.lines.is_empty() {
|
|
|
|
|
self.lines.push(TextBufferLine::new(String::new()));
|
2022-10-19 13:15:07 -06:00
|
|
|
}
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-19 13:15:07 -06:00
|
|
|
self.scroll = 0;
|
|
|
|
|
self.cursor = TextCursor::default();
|
|
|
|
|
self.select_opt = None;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-19 13:15:07 -06:00
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Get the lines of the original text
|
2022-10-25 10:13:44 -06:00
|
|
|
pub fn text_lines(&self) -> &[TextBufferLine] {
|
2022-10-19 17:48:11 -06:00
|
|
|
&self.lines
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 17:04:22 -06:00
|
|
|
/// Perform a [TextAction] on the buffer
|
2022-10-18 12:07:22 -06:00
|
|
|
pub fn action(&mut self, action: TextAction) {
|
|
|
|
|
match action {
|
2022-10-21 12:08:49 -06:00
|
|
|
TextAction::Previous => {
|
|
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
|
|
|
|
|
if self.cursor.index > 0 {
|
|
|
|
|
// Find previous character index
|
|
|
|
|
let mut prev_index = 0;
|
2022-10-24 21:30:36 +00:00
|
|
|
for (i, _) in line.text.grapheme_indices(true) {
|
2022-10-21 12:08:49 -06:00
|
|
|
if i < self.cursor.index {
|
|
|
|
|
prev_index = i;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.cursor.index = prev_index;
|
2022-10-22 09:37:05 -06:00
|
|
|
self.redraw = true;
|
2022-10-21 12:08:49 -06:00
|
|
|
} else if self.cursor.line.get() > 0 {
|
|
|
|
|
self.cursor.line = TextLineIndex::new(self.cursor.line.get() - 1);
|
|
|
|
|
self.cursor.index = self.lines[self.cursor.line.get()].text.len();
|
2022-10-22 09:37:05 -06:00
|
|
|
self.redraw = true;
|
2022-10-21 12:08:49 -06:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
TextAction::Next => {
|
|
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
|
|
|
|
|
if self.cursor.index < line.text.len() {
|
2022-10-24 21:30:36 +00:00
|
|
|
for (i, c) in line.text.grapheme_indices(true) {
|
2022-10-21 12:08:49 -06:00
|
|
|
if i == self.cursor.index {
|
2022-10-24 21:30:36 +00:00
|
|
|
self.cursor.index += c.len();
|
2022-10-22 09:37:05 -06:00
|
|
|
self.redraw = true;
|
2022-10-21 12:08:49 -06:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if self.cursor.line.get() + 1 < self.lines.len() {
|
|
|
|
|
self.cursor.line = TextLineIndex::new(self.cursor.line.get() + 1);
|
|
|
|
|
self.cursor.index = 0;
|
2022-10-22 09:37:05 -06:00
|
|
|
self.redraw = true;
|
2022-10-21 12:08:49 -06:00
|
|
|
}
|
|
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Left => {
|
2022-10-21 13:50:20 -06:00
|
|
|
let rtl_opt = self.lines[self.cursor.line.get()].shape_opt.as_ref().map(|shape| shape.rtl);
|
|
|
|
|
if let Some(rtl) = rtl_opt {
|
|
|
|
|
if rtl {
|
|
|
|
|
self.action(TextAction::Next);
|
|
|
|
|
} else {
|
|
|
|
|
self.action(TextAction::Previous);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Right => {
|
2022-10-21 13:50:20 -06:00
|
|
|
let rtl_opt = self.lines[self.cursor.line.get()].shape_opt.as_ref().map(|shape| shape.rtl);
|
|
|
|
|
if let Some(rtl) = rtl_opt {
|
|
|
|
|
if rtl {
|
|
|
|
|
self.action(TextAction::Previous);
|
|
|
|
|
} else {
|
|
|
|
|
self.action(TextAction::Next);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Up => {
|
2022-10-22 11:42:08 -06:00
|
|
|
//TODO: make this preserve X as best as possible!
|
|
|
|
|
let mut cursor = self.layout_cursor(&self.cursor);
|
|
|
|
|
if cursor.layout > 0 {
|
|
|
|
|
cursor.layout -= 1;
|
|
|
|
|
} else if cursor.line.get() > 0 {
|
|
|
|
|
cursor.line = TextLineIndex::new(cursor.line.get() - 1);
|
|
|
|
|
cursor.layout = usize::max_value();
|
2022-10-22 09:41:17 -06:00
|
|
|
}
|
2022-10-22 11:42:08 -06:00
|
|
|
self.set_layout_cursor(cursor);
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-18 12:07:22 -06:00
|
|
|
TextAction::Down => {
|
2022-10-22 11:42:08 -06:00
|
|
|
//TODO: make this preserve X as best as possible!
|
|
|
|
|
let mut cursor = self.layout_cursor(&self.cursor);
|
|
|
|
|
let layout_len = {
|
|
|
|
|
let line = &mut self.lines[cursor.line.get()];
|
|
|
|
|
let layout = line.layout(
|
2022-10-25 11:10:44 -06:00
|
|
|
&self.font_matches,
|
2022-10-22 11:42:08 -06:00
|
|
|
self.metrics.font_size,
|
|
|
|
|
self.width
|
|
|
|
|
);
|
|
|
|
|
layout.len()
|
|
|
|
|
};
|
|
|
|
|
if cursor.layout + 1 < layout_len {
|
|
|
|
|
cursor.layout += 1;
|
|
|
|
|
} else if cursor.line.get() + 1 < self.lines.len() {
|
|
|
|
|
cursor.line = TextLineIndex::new(cursor.line.get() + 1);
|
|
|
|
|
cursor.layout = 0;
|
2022-10-22 09:41:17 -06:00
|
|
|
}
|
2022-10-22 11:42:08 -06:00
|
|
|
self.set_layout_cursor(cursor);
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-19 11:08:15 -06:00
|
|
|
TextAction::Home => {
|
2022-10-22 11:42:08 -06:00
|
|
|
let mut cursor = self.layout_cursor(&self.cursor);
|
|
|
|
|
cursor.glyph = 0;
|
|
|
|
|
self.set_layout_cursor(cursor);
|
2022-10-19 11:08:15 -06:00
|
|
|
},
|
|
|
|
|
TextAction::End => {
|
2022-10-22 11:42:08 -06:00
|
|
|
let mut cursor = self.layout_cursor(&self.cursor);
|
|
|
|
|
cursor.glyph = usize::max_value();
|
|
|
|
|
self.set_layout_cursor(cursor);
|
2022-10-19 11:08:15 -06:00
|
|
|
}
|
2022-10-18 12:42:37 -06:00
|
|
|
TextAction::PageUp => {
|
2022-10-22 09:41:17 -06:00
|
|
|
//TODO: move cursor
|
2022-10-18 13:05:36 -06:00
|
|
|
self.scroll -= self.lines();
|
2022-10-18 12:42:37 -06:00
|
|
|
self.redraw = true;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-18 12:42:37 -06:00
|
|
|
self.shape_until_scroll();
|
|
|
|
|
},
|
|
|
|
|
TextAction::PageDown => {
|
2022-10-22 09:41:17 -06:00
|
|
|
//TODO: move cursor
|
2022-10-18 13:05:36 -06:00
|
|
|
self.scroll += self.lines();
|
2022-10-18 12:42:37 -06:00
|
|
|
self.redraw = true;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-18 12:42:37 -06:00
|
|
|
self.shape_until_scroll();
|
|
|
|
|
},
|
2022-10-21 14:58:15 -06:00
|
|
|
TextAction::Insert(character) => {
|
|
|
|
|
if character.is_control()
|
|
|
|
|
&& !['\t', '\u{92}'].contains(&character)
|
|
|
|
|
{
|
|
|
|
|
// Filter out special chars (except for tab), use TextAction instead
|
|
|
|
|
log::debug!("Refusing to insert control character {:?}", character);
|
|
|
|
|
} else {
|
|
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
line.reset();
|
|
|
|
|
line.text.insert(self.cursor.index, character);
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-21 14:58:15 -06:00
|
|
|
self.cursor.index += character.len_utf8();
|
|
|
|
|
}
|
2022-10-19 11:33:35 -06:00
|
|
|
},
|
|
|
|
|
TextAction::Enter => {
|
2022-10-21 11:51:04 -06:00
|
|
|
let new_line = {
|
|
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
line.reset();
|
|
|
|
|
line.text.split_off(self.cursor.index)
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
let next_line = self.cursor.line.get() + 1;
|
2022-10-21 11:51:04 -06:00
|
|
|
self.lines.insert(next_line, TextBufferLine::new(new_line));
|
2022-10-19 11:33:35 -06:00
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
self.cursor.line = TextLineIndex::new(next_line);
|
2022-10-21 11:44:11 -06:00
|
|
|
self.cursor.index = 0;
|
2022-10-19 11:33:35 -06:00
|
|
|
},
|
|
|
|
|
TextAction::Backspace => {
|
2022-10-21 11:44:11 -06:00
|
|
|
if self.cursor.index > 0 {
|
2022-10-20 19:34:56 -06:00
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
line.reset();
|
|
|
|
|
|
2022-10-21 11:44:11 -06:00
|
|
|
// Find previous character index
|
|
|
|
|
let mut prev_index = 0;
|
2022-10-20 19:34:56 -06:00
|
|
|
for (i, _) in line.text.char_indices() {
|
2022-10-21 11:44:11 -06:00
|
|
|
if i < self.cursor.index {
|
|
|
|
|
prev_index = i;
|
2022-10-19 16:09:22 -06:00
|
|
|
} else {
|
2022-10-21 11:44:11 -06:00
|
|
|
break;
|
2022-10-19 16:09:22 -06:00
|
|
|
}
|
2022-10-20 19:34:56 -06:00
|
|
|
}
|
2022-10-19 11:51:17 -06:00
|
|
|
|
2022-10-21 11:44:11 -06:00
|
|
|
self.cursor.index = prev_index;
|
|
|
|
|
|
|
|
|
|
line.text.remove(self.cursor.index);
|
2022-10-20 19:34:56 -06:00
|
|
|
} else if self.cursor.line.get() > 0 {
|
|
|
|
|
let mut line_index = self.cursor.line.get();
|
|
|
|
|
let old_line = self.lines.remove(line_index);
|
|
|
|
|
line_index -= 1;
|
|
|
|
|
|
|
|
|
|
let line = &mut self.lines[line_index];
|
|
|
|
|
line.reset();
|
|
|
|
|
|
|
|
|
|
self.cursor.line = TextLineIndex::new(line_index);
|
2022-10-21 11:44:11 -06:00
|
|
|
self.cursor.index = line.text.len();
|
2022-10-19 11:51:17 -06:00
|
|
|
|
2022-10-20 19:34:56 -06:00
|
|
|
line.text.push_str(&old_line.text);
|
2022-10-19 11:33:35 -06:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
TextAction::Delete => {
|
2022-10-21 12:08:49 -06:00
|
|
|
if self.cursor.index < self.lines[self.cursor.line.get()].text.len() {
|
|
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
line.reset();
|
|
|
|
|
|
2022-10-24 23:24:12 +00:00
|
|
|
if let Some((i, c)) = line
|
|
|
|
|
.text
|
|
|
|
|
.grapheme_indices(true)
|
2022-10-24 19:11:28 -06:00
|
|
|
.take_while(|(i, _)| *i <= self.cursor.index)
|
2022-10-24 23:24:12 +00:00
|
|
|
.last()
|
|
|
|
|
{
|
|
|
|
|
line.text.replace_range(i..(i + c.len()), "");
|
|
|
|
|
self.cursor.index = i;
|
2022-10-24 22:10:02 +00:00
|
|
|
}
|
2022-10-21 12:08:49 -06:00
|
|
|
} else if self.cursor.line.get() + 1 < self.lines.len() {
|
|
|
|
|
let old_line = self.lines.remove(self.cursor.line.get() + 1);
|
|
|
|
|
|
|
|
|
|
let line = &mut self.lines[self.cursor.line.get()];
|
|
|
|
|
line.reset();
|
|
|
|
|
|
|
|
|
|
line.text.push_str(&old_line.text);
|
|
|
|
|
}
|
2022-10-18 17:04:22 -06:00
|
|
|
},
|
|
|
|
|
TextAction::Click { x, y } => {
|
2022-10-19 10:12:52 -06:00
|
|
|
self.select_opt = None;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-19 10:12:52 -06:00
|
|
|
self.click(x, y);
|
|
|
|
|
},
|
|
|
|
|
TextAction::Drag { x, y } => {
|
|
|
|
|
if self.select_opt.is_none() {
|
|
|
|
|
self.select_opt = Some(self.cursor);
|
2022-10-19 10:21:29 -06:00
|
|
|
self.redraw = true;
|
2022-10-19 10:12:52 -06:00
|
|
|
}
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-19 07:36:27 -06:00
|
|
|
self.click(x, y);
|
2022-10-18 12:42:37 -06:00
|
|
|
},
|
2022-10-19 08:34:34 -06:00
|
|
|
TextAction::Scroll { lines } => {
|
2022-10-18 13:20:13 -06:00
|
|
|
self.scroll += lines;
|
|
|
|
|
self.redraw = true;
|
2022-10-20 19:34:56 -06:00
|
|
|
|
2022-10-18 13:20:13 -06:00
|
|
|
self.shape_until_scroll();
|
|
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
|
|
|
|
|
fn click(&mut self, mouse_x: i32, mouse_y: i32) {
|
|
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let font_size = self.metrics.font_size;
|
|
|
|
|
let line_height = self.metrics.line_height;
|
|
|
|
|
|
2022-10-25 13:13:13 -06:00
|
|
|
let mut new_cursor = self.cursor;
|
|
|
|
|
|
|
|
|
|
for run in self.layout_runs() {
|
|
|
|
|
let line_y = run.line_y;
|
|
|
|
|
|
|
|
|
|
if mouse_y >= line_y - font_size
|
|
|
|
|
&& mouse_y < line_y - font_size + line_height
|
|
|
|
|
{
|
|
|
|
|
let mut new_cursor_glyph = run.glyphs.len();
|
|
|
|
|
let mut new_cursor_char = 0;
|
|
|
|
|
'hit: for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
|
|
|
|
|
if mouse_x >= glyph.x as i32
|
|
|
|
|
&& mouse_x <= (glyph.x + glyph.w) as i32
|
|
|
|
|
{
|
|
|
|
|
new_cursor_glyph = glyph_i;
|
|
|
|
|
|
|
|
|
|
let cluster = &run.text[glyph.start..glyph.end];
|
|
|
|
|
let total = cluster.grapheme_indices(true).count();
|
|
|
|
|
let mut egc_x = glyph.x;
|
|
|
|
|
let egc_w = glyph.w / (total as f32);
|
|
|
|
|
for (egc_i, egc) in cluster.grapheme_indices(true) {
|
|
|
|
|
if mouse_x >= egc_x as i32
|
|
|
|
|
&& mouse_x <= (egc_x + egc_w) as i32
|
|
|
|
|
{
|
|
|
|
|
new_cursor_char = egc_i;
|
|
|
|
|
|
|
|
|
|
let right_half = mouse_x >= (egc_x + egc_w / 2.0) as i32;
|
|
|
|
|
if right_half != glyph.rtl {
|
|
|
|
|
// If clicking on last half of glyph, move cursor past glyph
|
|
|
|
|
new_cursor_char += egc.len();
|
2022-10-24 14:18:40 -06:00
|
|
|
}
|
2022-10-25 13:13:13 -06:00
|
|
|
break 'hit;
|
2022-10-24 14:18:40 -06:00
|
|
|
}
|
2022-10-25 13:13:13 -06:00
|
|
|
egc_x += egc_w;
|
|
|
|
|
}
|
2022-10-24 14:18:40 -06:00
|
|
|
|
2022-10-25 13:13:13 -06:00
|
|
|
let right_half = mouse_x >= (glyph.x + glyph.w / 2.0) as i32;
|
|
|
|
|
if right_half != glyph.rtl {
|
|
|
|
|
// If clicking on last half of glyph, move cursor past glyph
|
|
|
|
|
new_cursor_char = cluster.len();
|
2022-10-19 09:31:01 -06:00
|
|
|
}
|
2022-10-25 13:13:13 -06:00
|
|
|
break 'hit;
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
2022-10-25 13:13:13 -06:00
|
|
|
}
|
2022-10-21 09:02:15 -06:00
|
|
|
|
2022-10-25 13:13:13 -06:00
|
|
|
new_cursor.line = run.line_i;
|
|
|
|
|
new_cursor.index = 0;
|
|
|
|
|
|
|
|
|
|
match run.glyphs.get(new_cursor_glyph) {
|
|
|
|
|
Some(glyph) => {
|
|
|
|
|
// Position at glyph
|
|
|
|
|
new_cursor.index = glyph.start + new_cursor_char;
|
|
|
|
|
},
|
|
|
|
|
None => if let Some(glyph) = run.glyphs.last() {
|
|
|
|
|
// Position at end of line
|
|
|
|
|
new_cursor.index = glyph.end;
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
|
2022-10-25 13:13:13 -06:00
|
|
|
if new_cursor != self.cursor {
|
|
|
|
|
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
|
|
|
|
|
let font_opt = self.font_matches.get_font(&glyph.cache_key.font_id);
|
|
|
|
|
let text_glyph = &run.text[glyph.start..glyph.end];
|
|
|
|
|
log::debug!(
|
|
|
|
|
"{}, {}: '{}' ('{}'): '{}' ({:?})",
|
|
|
|
|
self.cursor.line.get(),
|
|
|
|
|
self.cursor.index,
|
2022-10-25 15:47:55 -06:00
|
|
|
font_opt.as_ref().map_or("?", |font| font.info.family.as_str()),
|
|
|
|
|
font_opt.as_ref().map_or("?", |font| font.info.post_script_name.as_str()),
|
2022-10-25 13:13:13 -06:00
|
|
|
text_glyph,
|
|
|
|
|
text_glyph
|
|
|
|
|
);
|
2022-10-21 09:02:15 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 13:13:13 -06:00
|
|
|
break;
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 13:13:13 -06:00
|
|
|
if new_cursor != self.cursor {
|
|
|
|
|
self.cursor = new_cursor;
|
|
|
|
|
self.redraw = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 07:36:27 -06:00
|
|
|
let duration = instant.elapsed();
|
2022-10-19 13:29:50 -06:00
|
|
|
log::trace!("click({}, {}): {:?}", mouse_x, mouse_y, duration);
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
/// Get the visible layout runs for rendering and other tasks
|
2022-10-25 12:52:46 -06:00
|
|
|
pub fn layout_runs(&self) -> TextLayoutRunIter {
|
|
|
|
|
TextLayoutRunIter::new(self)
|
2022-10-25 11:40:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Draw the buffer
|
|
|
|
|
#[cfg(feature = "swash")]
|
2022-10-25 11:40:57 -06:00
|
|
|
pub fn draw<F>(&self, cache: &mut crate::SwashCache, color: u32, mut f: F)
|
2022-10-25 11:40:10 -06:00
|
|
|
where F: FnMut(i32, i32, u32, u32, u32)
|
|
|
|
|
{
|
|
|
|
|
let font_size = self.metrics.font_size;
|
|
|
|
|
let line_height = self.metrics.line_height;
|
|
|
|
|
|
2022-10-25 12:52:46 -06:00
|
|
|
for run in self.layout_runs() {
|
2022-10-25 11:40:10 -06:00
|
|
|
let line_i = run.line_i;
|
|
|
|
|
let line_y = run.line_y;
|
|
|
|
|
|
|
|
|
|
let cursor_glyph_opt = |cursor: &TextCursor| -> Option<(usize, f32)> {
|
|
|
|
|
if cursor.line == line_i {
|
2022-10-25 12:52:46 -06:00
|
|
|
for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
|
2022-10-25 11:40:10 -06:00
|
|
|
if cursor.index == glyph.start {
|
|
|
|
|
return Some((glyph_i, 0.0));
|
|
|
|
|
} else if cursor.index > glyph.start && cursor.index < glyph.end {
|
|
|
|
|
// Guess x offset based on characters
|
|
|
|
|
let mut before = 0;
|
|
|
|
|
let mut total = 0;
|
|
|
|
|
|
2022-10-25 12:52:46 -06:00
|
|
|
let cluster = &run.text[glyph.start..glyph.end];
|
2022-10-25 11:40:10 -06:00
|
|
|
for (i, _) in cluster.grapheme_indices(true) {
|
|
|
|
|
if glyph.start + i < cursor.index {
|
|
|
|
|
before += 1;
|
|
|
|
|
}
|
|
|
|
|
total += 1;
|
2022-10-21 11:51:04 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
|
|
|
|
|
let offset = glyph.w * (before as f32) / (total as f32);
|
|
|
|
|
return Some((glyph_i, offset));
|
2022-10-21 11:51:04 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
}
|
2022-10-25 12:52:46 -06:00
|
|
|
match run.glyphs.last() {
|
2022-10-25 11:40:10 -06:00
|
|
|
Some(glyph) => {
|
|
|
|
|
if cursor.index == glyph.end {
|
2022-10-25 12:52:46 -06:00
|
|
|
return Some((run.glyphs.len(), 0.0));
|
2022-10-21 11:51:04 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
return Some((0, 0.0));
|
2022-10-21 09:49:16 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
};
|
2022-10-21 09:49:16 -06:00
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
// Highlight selection (TODO: HIGHLIGHT COLOR!)
|
|
|
|
|
if let Some(select) = self.select_opt {
|
|
|
|
|
let (start, end) = match select.line.cmp(&self.cursor.line) {
|
|
|
|
|
cmp::Ordering::Greater => (self.cursor, select),
|
|
|
|
|
cmp::Ordering::Less => (select, self.cursor),
|
|
|
|
|
cmp::Ordering::Equal => {
|
|
|
|
|
/* select.line == self.cursor.line */
|
|
|
|
|
if select.index < self.cursor.index {
|
|
|
|
|
(select, self.cursor)
|
|
|
|
|
} else {
|
|
|
|
|
/* select.index >= self.cursor.index */
|
|
|
|
|
(self.cursor, select)
|
2022-10-20 19:34:56 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
}
|
|
|
|
|
};
|
2022-10-19 10:12:52 -06:00
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
if line_i >= start.line && line_i <= end.line {
|
|
|
|
|
let mut range_opt = None;
|
2022-10-25 12:52:46 -06:00
|
|
|
for glyph in run.glyphs.iter() {
|
2022-10-25 11:40:10 -06:00
|
|
|
// Guess x offset based on characters
|
2022-10-25 12:52:46 -06:00
|
|
|
let cluster = &run.text[glyph.start..glyph.end];
|
2022-10-25 11:40:10 -06:00
|
|
|
let total = cluster.grapheme_indices(true).count();
|
|
|
|
|
let mut c_x = glyph.x;
|
|
|
|
|
let c_w = glyph.w / total as f32;
|
|
|
|
|
for (i, c) in cluster.grapheme_indices(true) {
|
|
|
|
|
let c_start = glyph.start + i;
|
|
|
|
|
let c_end = glyph.start + i + c.len();
|
|
|
|
|
if (start.line != line_i || c_end > start.index)
|
|
|
|
|
&& (end.line != line_i || c_start < end.index) {
|
|
|
|
|
range_opt = match range_opt.take() {
|
|
|
|
|
Some((min, max)) => Some((
|
|
|
|
|
cmp::min(min, c_x as i32),
|
|
|
|
|
cmp::max(max, (c_x + c_w) as i32),
|
|
|
|
|
)),
|
|
|
|
|
None => Some((
|
|
|
|
|
c_x as i32,
|
|
|
|
|
(c_x + c_w) as i32,
|
|
|
|
|
))
|
|
|
|
|
};
|
|
|
|
|
} else if let Some((min, max)) = range_opt.take() {
|
|
|
|
|
f(
|
|
|
|
|
min,
|
|
|
|
|
line_y - font_size,
|
|
|
|
|
cmp::max(0, max - min) as u32,
|
|
|
|
|
line_height as u32,
|
|
|
|
|
0x33_00_00_00 | (color & 0xFF_FF_FF)
|
|
|
|
|
);
|
2022-10-24 18:57:13 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
c_x += c_w;
|
2022-10-20 19:34:56 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
}
|
2022-10-24 19:11:28 -06:00
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
if let Some((mut min, mut max)) = range_opt.take() {
|
|
|
|
|
if end.line > line_i {
|
|
|
|
|
// Draw to end of line
|
2022-10-25 12:52:46 -06:00
|
|
|
if run.rtl {
|
2022-10-25 11:40:10 -06:00
|
|
|
min = 0;
|
|
|
|
|
} else {
|
|
|
|
|
max = self.width;
|
2022-10-25 10:43:38 -04:00
|
|
|
}
|
2022-10-24 19:11:28 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
f(
|
|
|
|
|
min,
|
|
|
|
|
line_y - font_size,
|
|
|
|
|
cmp::max(0, max - min) as u32,
|
|
|
|
|
line_height as u32,
|
|
|
|
|
0x33_00_00_00 | (color & 0xFF_FF_FF)
|
|
|
|
|
);
|
2022-10-19 10:20:22 -06:00
|
|
|
}
|
2022-10-19 10:12:52 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
}
|
2022-10-19 10:12:52 -06:00
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
// Draw cursor
|
|
|
|
|
//TODO: draw at end of line but not start of next line
|
|
|
|
|
if let Some((cursor_glyph, cursor_glyph_offset)) = cursor_glyph_opt(&self.cursor) {
|
2022-10-25 12:52:46 -06:00
|
|
|
let x = match run.glyphs.get(cursor_glyph) {
|
2022-10-25 11:40:10 -06:00
|
|
|
Some(glyph) => {
|
|
|
|
|
// Start of detected glyph
|
|
|
|
|
if glyph.rtl {
|
|
|
|
|
(glyph.x + glyph.w - cursor_glyph_offset) as i32
|
|
|
|
|
} else {
|
|
|
|
|
(glyph.x + cursor_glyph_offset) as i32
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-10-25 12:52:46 -06:00
|
|
|
None => match run.glyphs.last() {
|
2022-10-21 11:51:04 -06:00
|
|
|
Some(glyph) => {
|
2022-10-25 11:40:10 -06:00
|
|
|
// End of last glyph
|
2022-10-24 18:57:13 -06:00
|
|
|
if glyph.rtl {
|
2022-10-25 11:40:10 -06:00
|
|
|
glyph.x as i32
|
2022-10-21 11:51:04 -06:00
|
|
|
} else {
|
2022-10-25 11:40:10 -06:00
|
|
|
(glyph.x + glyph.w) as i32
|
2022-10-21 11:51:04 -06:00
|
|
|
}
|
|
|
|
|
},
|
2022-10-25 11:40:10 -06:00
|
|
|
None => {
|
|
|
|
|
// Start of empty line
|
|
|
|
|
0
|
2022-10-21 11:51:04 -06:00
|
|
|
}
|
2022-10-25 11:40:10 -06:00
|
|
|
}
|
|
|
|
|
};
|
2022-10-25 10:55:24 -06:00
|
|
|
|
2022-10-25 11:40:10 -06:00
|
|
|
f(
|
|
|
|
|
x,
|
|
|
|
|
line_y - font_size,
|
|
|
|
|
1,
|
|
|
|
|
line_height as u32,
|
|
|
|
|
color,
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
|
2022-10-25 12:52:46 -06:00
|
|
|
for glyph in run.glyphs.iter() {
|
2022-10-25 11:40:10 -06:00
|
|
|
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
|
|
|
|
|
cache.with_pixels(&self.font_matches, cache_key, color, |x, y, color| {
|
|
|
|
|
f(x_int + x, line_y + y_int + y, 1, 1, color)
|
|
|
|
|
});
|
2022-10-20 19:34:56 -06:00
|
|
|
}
|
2022-10-25 12:52:46 -06:00
|
|
|
}
|
2022-10-19 07:36:27 -06:00
|
|
|
}
|
2022-10-18 12:07:22 -06:00
|
|
|
}
|