Struct cosmic_text::BufferLine
source · pub struct BufferLine { /* private fields */ }Expand description
A line (or paragraph) of text that is shaped and laid out
Implementations§
source§impl BufferLine
impl BufferLine
sourcepub fn new<T: Into<String>>(text: T, attrs_list: AttrsList) -> Self
pub fn new<T: Into<String>>(text: T, attrs_list: AttrsList) -> Self
Create a new line with the given text and attributes list
Cached shaping and layout can be done using the Self::shape and
Self::layout functions
Examples found in repository?
More examples
404 405 406 407 408 409 410 411 412 413 414 415 416 417
pub fn set_text(&mut self, text: &str, attrs: Attrs<'a>) {
self.lines.clear();
for line in text.lines() {
self.lines.push(BufferLine::new(line.to_string(), AttrsList::new(attrs)));
}
// Make sure there is always one line
if self.lines.is_empty() {
self.lines.push(BufferLine::new(String::new(), AttrsList::new(attrs)));
}
self.scroll = 0;
self.shape_until_scroll();
}205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
fn action(&mut self, action: Action) {
let old_cursor = self.cursor;
match action {
Action::Previous => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index > 0 {
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().grapheme_indices(true) {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
self.buffer.set_redraw(true);
} else if self.cursor.line > 0 {
self.cursor.line -= 1;
self.cursor.index = self.buffer.lines[self.cursor.line].text().len();
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Next => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index < line.text().len() {
for (i, c) in line.text().grapheme_indices(true) {
if i == self.cursor.index {
self.cursor.index += c.len();
self.buffer.set_redraw(true);
break;
}
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Left => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Next);
} else {
self.action(Action::Previous);
}
}
},
Action::Right => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Previous);
} else {
self.action(Action::Next);
}
}
},
Action::Up => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout > 0 {
cursor.layout -= 1;
} else if cursor.line > 0 {
cursor.line -= 1;
cursor.layout = usize::max_value();
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Down => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
let layout_len = self.buffer.line_layout(cursor.line).expect("layout not found").len();
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout + 1 < layout_len {
cursor.layout += 1;
} else if cursor.line + 1 < self.buffer.lines.len() {
cursor.line += 1;
cursor.layout = 0;
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Home => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = 0;
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
},
Action::End => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = usize::max_value();
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
}
Action::PageUp => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll -= self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::PageDown => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll += self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::Escape => {
if self.select_opt.take().is_some() {
self.buffer.set_redraw(true);
}
},
Action::Insert(character) => {
if character.is_control()
&& !['\t', '\u{92}'].contains(&character)
{
// Filter out special chars (except for tab), use Action instead
log::debug!("Refusing to insert control character {:?}", character);
} else {
self.delete_selection();
let line = &mut self.buffer.lines[self.cursor.line];
// Collect text after insertion as a line
let after = line.split_off(self.cursor.index);
// Append the inserted text
line.append(BufferLine::new(
character.to_string(),
AttrsList::new(line.attrs_list().defaults() /*TODO: provide attrs?*/)
));
// Append the text after insertion
line.append(after);
self.cursor.index += character.len_utf8();
}
},
Action::Enter => {
self.delete_selection();
let new_line = self.buffer.lines[self.cursor.line].split_off(self.cursor.index);
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.lines.insert(self.cursor.line, new_line);
},
Action::Backspace => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index > 0 {
let line = &mut self.buffer.lines[self.cursor.line];
// Get text line after cursor
let after = line.split_off(self.cursor.index);
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().char_indices() {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
// Remove character
line.split_off(self.cursor.index);
// Add text after cursor
line.append(after);
} else if self.cursor.line > 0 {
let mut line_index = self.cursor.line;
let old_line = self.buffer.lines.remove(line_index);
line_index -= 1;
let line = &mut self.buffer.lines[line_index];
self.cursor.line = line_index;
self.cursor.index = line.text().len();
line.append(old_line);
}
},
Action::Delete => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index < self.buffer.lines[self.cursor.line].text().len() {
let line = &mut self.buffer.lines[self.cursor.line];
let range_opt = line
.text()
.grapheme_indices(true)
.take_while(|(i, _)| *i <= self.cursor.index)
.last()
.map(|(i, c)| {
i..(i + c.len())
});
if let Some(range) = range_opt {
self.cursor.index = range.start;
// Get text after deleted EGC
let after = line.split_off(range.end);
// Delete EGC
line.split_off(range.start);
// Add text after deleted EGC
line.append(after);
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
let old_line = self.buffer.lines.remove(self.cursor.line + 1);
self.buffer.lines[self.cursor.line].append(old_line);
}
},
Action::Click { x, y } => {
self.select_opt = None;
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Drag { x, y } => {
if self.select_opt.is_none() {
self.select_opt = Some(self.cursor);
self.buffer.set_redraw(true);
}
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Scroll { lines } => {
let mut scroll = self.buffer.scroll();
scroll += lines;
self.buffer.set_scroll(scroll);
}
}
if old_cursor != self.cursor {
self.cursor_moved = true;
/*TODO
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
let font_opt = self.buffer.font_system().get_font(glyph.cache_key.font_id);
let text_glyph = &run.text[glyph.start..glyph.end];
log::debug!(
"{}, {}: '{}' ('{}'): '{}' ({:?})",
self.cursor.line,
self.cursor.index,
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()),
text_glyph,
text_glyph
);
}
*/
}
}sourcepub fn text(&self) -> &str
pub fn text(&self) -> &str
Get current text
Examples found in repository?
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
pub fn append(&mut self, other: Self) {
let len = self.text.len();
self.text.push_str(other.text());
if other.attrs_list.defaults() != self.attrs_list.defaults() {
// If default formatting does not match, make a new span for it
self.attrs_list.add_span(len..len + other.text().len(), other.attrs_list.defaults());
}
for (other_range, attrs) in other.attrs_list.spans() {
// Add previous attrs spans
let range = other_range.start + len..other_range.end + len;
self.attrs_list.add_span(range, attrs.as_attrs());
}
self.reset();
}More examples
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
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(LayoutRun {
line_i: self.line_i,
text: line.text(),
rtl: shape.rtl,
glyphs: &layout_line.glyphs,
line_y: self.line_y,
line_w: layout_line.w,
});
}
self.line_i += 1;
self.layout_i = 0;
}
None
}94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
fn copy_selection(&mut self) -> Option<String> {
let 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)
}
}
};
let mut selection = String::new();
// Take the selection from the first line
{
// Add selected part of line to string
if start.line == end.line {
selection.push_str(&self.buffer.lines[start.line].text()[start.index..end.index]);
} else {
selection.push_str(&self.buffer.lines[start.line].text()[start.index..]);
selection.push('\n');
}
}
// Take the selection from all interior lines (if they exist)
for line_i in start.line + 1..end.line {
selection.push_str(self.buffer.lines[line_i].text());
selection.push('\n');
}
// Take the selection from the last line
if end.line > start.line {
// Add selected part of line to string
selection.push_str(&self.buffer.lines[end.line].text()[..end.index]);
}
Some(selection)
}
fn delete_selection(&mut self) -> bool {
let select = match self.select_opt.take() {
Some(some) => some,
None => return false,
};
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)
}
}
};
// Reset cursor to start of selection
self.cursor = start;
// Delete the selection from the last line
let end_line_opt = if end.line > start.line {
// Get part of line after selection
let after = self.buffer.lines[end.line].split_off(end.index);
// Remove end line
self.buffer.lines.remove(end.line);
Some(after)
} else {
None
};
// Delete interior lines (in reverse for safety)
for line_i in (start.line + 1..end.line).rev() {
self.buffer.lines.remove(line_i);
}
// Delete the selection from the first line
{
// Get part after selection if start line is also end line
let after_opt = if start.line == end.line {
Some(self.buffer.lines[start.line].split_off(end.index))
} else {
None
};
// Delete selected part of line
self.buffer.lines[start.line].split_off(start.index);
// Re-add part of line after selection
if let Some(after) = after_opt {
self.buffer.lines[start.line].append(after);
}
// Re-add valid parts of end line
if let Some(end_line) = end_line_opt {
self.buffer.lines[start.line].append(end_line);
}
}
true
}
fn action(&mut self, action: Action) {
let old_cursor = self.cursor;
match action {
Action::Previous => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index > 0 {
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().grapheme_indices(true) {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
self.buffer.set_redraw(true);
} else if self.cursor.line > 0 {
self.cursor.line -= 1;
self.cursor.index = self.buffer.lines[self.cursor.line].text().len();
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Next => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index < line.text().len() {
for (i, c) in line.text().grapheme_indices(true) {
if i == self.cursor.index {
self.cursor.index += c.len();
self.buffer.set_redraw(true);
break;
}
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Left => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Next);
} else {
self.action(Action::Previous);
}
}
},
Action::Right => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Previous);
} else {
self.action(Action::Next);
}
}
},
Action::Up => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout > 0 {
cursor.layout -= 1;
} else if cursor.line > 0 {
cursor.line -= 1;
cursor.layout = usize::max_value();
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Down => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
let layout_len = self.buffer.line_layout(cursor.line).expect("layout not found").len();
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout + 1 < layout_len {
cursor.layout += 1;
} else if cursor.line + 1 < self.buffer.lines.len() {
cursor.line += 1;
cursor.layout = 0;
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Home => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = 0;
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
},
Action::End => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = usize::max_value();
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
}
Action::PageUp => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll -= self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::PageDown => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll += self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::Escape => {
if self.select_opt.take().is_some() {
self.buffer.set_redraw(true);
}
},
Action::Insert(character) => {
if character.is_control()
&& !['\t', '\u{92}'].contains(&character)
{
// Filter out special chars (except for tab), use Action instead
log::debug!("Refusing to insert control character {:?}", character);
} else {
self.delete_selection();
let line = &mut self.buffer.lines[self.cursor.line];
// Collect text after insertion as a line
let after = line.split_off(self.cursor.index);
// Append the inserted text
line.append(BufferLine::new(
character.to_string(),
AttrsList::new(line.attrs_list().defaults() /*TODO: provide attrs?*/)
));
// Append the text after insertion
line.append(after);
self.cursor.index += character.len_utf8();
}
},
Action::Enter => {
self.delete_selection();
let new_line = self.buffer.lines[self.cursor.line].split_off(self.cursor.index);
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.lines.insert(self.cursor.line, new_line);
},
Action::Backspace => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index > 0 {
let line = &mut self.buffer.lines[self.cursor.line];
// Get text line after cursor
let after = line.split_off(self.cursor.index);
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().char_indices() {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
// Remove character
line.split_off(self.cursor.index);
// Add text after cursor
line.append(after);
} else if self.cursor.line > 0 {
let mut line_index = self.cursor.line;
let old_line = self.buffer.lines.remove(line_index);
line_index -= 1;
let line = &mut self.buffer.lines[line_index];
self.cursor.line = line_index;
self.cursor.index = line.text().len();
line.append(old_line);
}
},
Action::Delete => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index < self.buffer.lines[self.cursor.line].text().len() {
let line = &mut self.buffer.lines[self.cursor.line];
let range_opt = line
.text()
.grapheme_indices(true)
.take_while(|(i, _)| *i <= self.cursor.index)
.last()
.map(|(i, c)| {
i..(i + c.len())
});
if let Some(range) = range_opt {
self.cursor.index = range.start;
// Get text after deleted EGC
let after = line.split_off(range.end);
// Delete EGC
line.split_off(range.start);
// Add text after deleted EGC
line.append(after);
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
let old_line = self.buffer.lines.remove(self.cursor.line + 1);
self.buffer.lines[self.cursor.line].append(old_line);
}
},
Action::Click { x, y } => {
self.select_opt = None;
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Drag { x, y } => {
if self.select_opt.is_none() {
self.select_opt = Some(self.cursor);
self.buffer.set_redraw(true);
}
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Scroll { lines } => {
let mut scroll = self.buffer.scroll();
scroll += lines;
self.buffer.set_scroll(scroll);
}
}
if old_cursor != self.cursor {
self.cursor_moved = true;
/*TODO
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
let font_opt = self.buffer.font_system().get_font(glyph.cache_key.font_id);
let text_glyph = &run.text[glyph.start..glyph.end];
log::debug!(
"{}, {}: '{}' ('{}'): '{}' ({:?})",
self.cursor.line,
self.cursor.index,
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()),
text_glyph,
text_glyph
);
}
*/
}
}sourcepub fn set_text<T: AsRef<str> + Into<String>>(
&mut self,
text: T,
attrs_list: AttrsList
) -> bool
pub fn set_text<T: AsRef<str> + Into<String>>(
&mut self,
text: T,
attrs_list: AttrsList
) -> bool
Set text and attributes list
Will reset shape and layout if it differs from current text and attributes list. Returns true if the line was reset
sourcepub fn attrs_list(&self) -> &AttrsList
pub fn attrs_list(&self) -> &AttrsList
Get attributes list
Examples found in repository?
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
fn action(&mut self, action: Action) {
let old_cursor = self.cursor;
match action {
Action::Previous => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index > 0 {
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().grapheme_indices(true) {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
self.buffer.set_redraw(true);
} else if self.cursor.line > 0 {
self.cursor.line -= 1;
self.cursor.index = self.buffer.lines[self.cursor.line].text().len();
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Next => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index < line.text().len() {
for (i, c) in line.text().grapheme_indices(true) {
if i == self.cursor.index {
self.cursor.index += c.len();
self.buffer.set_redraw(true);
break;
}
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Left => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Next);
} else {
self.action(Action::Previous);
}
}
},
Action::Right => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Previous);
} else {
self.action(Action::Next);
}
}
},
Action::Up => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout > 0 {
cursor.layout -= 1;
} else if cursor.line > 0 {
cursor.line -= 1;
cursor.layout = usize::max_value();
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Down => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
let layout_len = self.buffer.line_layout(cursor.line).expect("layout not found").len();
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout + 1 < layout_len {
cursor.layout += 1;
} else if cursor.line + 1 < self.buffer.lines.len() {
cursor.line += 1;
cursor.layout = 0;
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Home => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = 0;
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
},
Action::End => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = usize::max_value();
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
}
Action::PageUp => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll -= self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::PageDown => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll += self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::Escape => {
if self.select_opt.take().is_some() {
self.buffer.set_redraw(true);
}
},
Action::Insert(character) => {
if character.is_control()
&& !['\t', '\u{92}'].contains(&character)
{
// Filter out special chars (except for tab), use Action instead
log::debug!("Refusing to insert control character {:?}", character);
} else {
self.delete_selection();
let line = &mut self.buffer.lines[self.cursor.line];
// Collect text after insertion as a line
let after = line.split_off(self.cursor.index);
// Append the inserted text
line.append(BufferLine::new(
character.to_string(),
AttrsList::new(line.attrs_list().defaults() /*TODO: provide attrs?*/)
));
// Append the text after insertion
line.append(after);
self.cursor.index += character.len_utf8();
}
},
Action::Enter => {
self.delete_selection();
let new_line = self.buffer.lines[self.cursor.line].split_off(self.cursor.index);
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.lines.insert(self.cursor.line, new_line);
},
Action::Backspace => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index > 0 {
let line = &mut self.buffer.lines[self.cursor.line];
// Get text line after cursor
let after = line.split_off(self.cursor.index);
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().char_indices() {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
// Remove character
line.split_off(self.cursor.index);
// Add text after cursor
line.append(after);
} else if self.cursor.line > 0 {
let mut line_index = self.cursor.line;
let old_line = self.buffer.lines.remove(line_index);
line_index -= 1;
let line = &mut self.buffer.lines[line_index];
self.cursor.line = line_index;
self.cursor.index = line.text().len();
line.append(old_line);
}
},
Action::Delete => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index < self.buffer.lines[self.cursor.line].text().len() {
let line = &mut self.buffer.lines[self.cursor.line];
let range_opt = line
.text()
.grapheme_indices(true)
.take_while(|(i, _)| *i <= self.cursor.index)
.last()
.map(|(i, c)| {
i..(i + c.len())
});
if let Some(range) = range_opt {
self.cursor.index = range.start;
// Get text after deleted EGC
let after = line.split_off(range.end);
// Delete EGC
line.split_off(range.start);
// Add text after deleted EGC
line.append(after);
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
let old_line = self.buffer.lines.remove(self.cursor.line + 1);
self.buffer.lines[self.cursor.line].append(old_line);
}
},
Action::Click { x, y } => {
self.select_opt = None;
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Drag { x, y } => {
if self.select_opt.is_none() {
self.select_opt = Some(self.cursor);
self.buffer.set_redraw(true);
}
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Scroll { lines } => {
let mut scroll = self.buffer.scroll();
scroll += lines;
self.buffer.set_scroll(scroll);
}
}
if old_cursor != self.cursor {
self.cursor_moved = true;
/*TODO
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
let font_opt = self.buffer.font_system().get_font(glyph.cache_key.font_id);
let text_glyph = &run.text[glyph.start..glyph.end];
log::debug!(
"{}, {}: '{}' ('{}'): '{}' ({:?})",
self.cursor.line,
self.cursor.index,
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()),
text_glyph,
text_glyph
);
}
*/
}
}sourcepub fn set_attrs_list(&mut self, attrs_list: AttrsList) -> bool
pub fn set_attrs_list(&mut self, attrs_list: AttrsList) -> bool
Set attributes list
Will reset shape and layout if it differs from current attributes list. Returns true if the line was reset
sourcepub fn wrap_simple(&self) -> bool
pub fn wrap_simple(&self) -> bool
Get simple wrapping setting (wrap by characters only)
sourcepub fn set_wrap_simple(&mut self, wrap_simple: bool) -> bool
pub fn set_wrap_simple(&mut self, wrap_simple: bool) -> bool
Set simple wrapping setting (wrap by characters only)
Will reset shape and layout if it differs from current simple wrapping setting. Returns true if the line was reset
sourcepub fn append(&mut self, other: Self)
pub fn append(&mut self, other: Self)
Append line at end of this line
The wrap setting of the appended line will be lost
Examples found in repository?
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
fn delete_selection(&mut self) -> bool {
let select = match self.select_opt.take() {
Some(some) => some,
None => return false,
};
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)
}
}
};
// Reset cursor to start of selection
self.cursor = start;
// Delete the selection from the last line
let end_line_opt = if end.line > start.line {
// Get part of line after selection
let after = self.buffer.lines[end.line].split_off(end.index);
// Remove end line
self.buffer.lines.remove(end.line);
Some(after)
} else {
None
};
// Delete interior lines (in reverse for safety)
for line_i in (start.line + 1..end.line).rev() {
self.buffer.lines.remove(line_i);
}
// Delete the selection from the first line
{
// Get part after selection if start line is also end line
let after_opt = if start.line == end.line {
Some(self.buffer.lines[start.line].split_off(end.index))
} else {
None
};
// Delete selected part of line
self.buffer.lines[start.line].split_off(start.index);
// Re-add part of line after selection
if let Some(after) = after_opt {
self.buffer.lines[start.line].append(after);
}
// Re-add valid parts of end line
if let Some(end_line) = end_line_opt {
self.buffer.lines[start.line].append(end_line);
}
}
true
}
fn action(&mut self, action: Action) {
let old_cursor = self.cursor;
match action {
Action::Previous => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index > 0 {
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().grapheme_indices(true) {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
self.buffer.set_redraw(true);
} else if self.cursor.line > 0 {
self.cursor.line -= 1;
self.cursor.index = self.buffer.lines[self.cursor.line].text().len();
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Next => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index < line.text().len() {
for (i, c) in line.text().grapheme_indices(true) {
if i == self.cursor.index {
self.cursor.index += c.len();
self.buffer.set_redraw(true);
break;
}
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Left => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Next);
} else {
self.action(Action::Previous);
}
}
},
Action::Right => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Previous);
} else {
self.action(Action::Next);
}
}
},
Action::Up => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout > 0 {
cursor.layout -= 1;
} else if cursor.line > 0 {
cursor.line -= 1;
cursor.layout = usize::max_value();
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Down => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
let layout_len = self.buffer.line_layout(cursor.line).expect("layout not found").len();
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout + 1 < layout_len {
cursor.layout += 1;
} else if cursor.line + 1 < self.buffer.lines.len() {
cursor.line += 1;
cursor.layout = 0;
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Home => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = 0;
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
},
Action::End => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = usize::max_value();
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
}
Action::PageUp => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll -= self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::PageDown => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll += self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::Escape => {
if self.select_opt.take().is_some() {
self.buffer.set_redraw(true);
}
},
Action::Insert(character) => {
if character.is_control()
&& !['\t', '\u{92}'].contains(&character)
{
// Filter out special chars (except for tab), use Action instead
log::debug!("Refusing to insert control character {:?}", character);
} else {
self.delete_selection();
let line = &mut self.buffer.lines[self.cursor.line];
// Collect text after insertion as a line
let after = line.split_off(self.cursor.index);
// Append the inserted text
line.append(BufferLine::new(
character.to_string(),
AttrsList::new(line.attrs_list().defaults() /*TODO: provide attrs?*/)
));
// Append the text after insertion
line.append(after);
self.cursor.index += character.len_utf8();
}
},
Action::Enter => {
self.delete_selection();
let new_line = self.buffer.lines[self.cursor.line].split_off(self.cursor.index);
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.lines.insert(self.cursor.line, new_line);
},
Action::Backspace => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index > 0 {
let line = &mut self.buffer.lines[self.cursor.line];
// Get text line after cursor
let after = line.split_off(self.cursor.index);
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().char_indices() {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
// Remove character
line.split_off(self.cursor.index);
// Add text after cursor
line.append(after);
} else if self.cursor.line > 0 {
let mut line_index = self.cursor.line;
let old_line = self.buffer.lines.remove(line_index);
line_index -= 1;
let line = &mut self.buffer.lines[line_index];
self.cursor.line = line_index;
self.cursor.index = line.text().len();
line.append(old_line);
}
},
Action::Delete => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index < self.buffer.lines[self.cursor.line].text().len() {
let line = &mut self.buffer.lines[self.cursor.line];
let range_opt = line
.text()
.grapheme_indices(true)
.take_while(|(i, _)| *i <= self.cursor.index)
.last()
.map(|(i, c)| {
i..(i + c.len())
});
if let Some(range) = range_opt {
self.cursor.index = range.start;
// Get text after deleted EGC
let after = line.split_off(range.end);
// Delete EGC
line.split_off(range.start);
// Add text after deleted EGC
line.append(after);
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
let old_line = self.buffer.lines.remove(self.cursor.line + 1);
self.buffer.lines[self.cursor.line].append(old_line);
}
},
Action::Click { x, y } => {
self.select_opt = None;
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Drag { x, y } => {
if self.select_opt.is_none() {
self.select_opt = Some(self.cursor);
self.buffer.set_redraw(true);
}
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Scroll { lines } => {
let mut scroll = self.buffer.scroll();
scroll += lines;
self.buffer.set_scroll(scroll);
}
}
if old_cursor != self.cursor {
self.cursor_moved = true;
/*TODO
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
let font_opt = self.buffer.font_system().get_font(glyph.cache_key.font_id);
let text_glyph = &run.text[glyph.start..glyph.end];
log::debug!(
"{}, {}: '{}' ('{}'): '{}' ({:?})",
self.cursor.line,
self.cursor.index,
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()),
text_glyph,
text_glyph
);
}
*/
}
}sourcepub fn split_off(&mut self, index: usize) -> Self
pub fn split_off(&mut self, index: usize) -> Self
Split off new line at index
Examples found in repository?
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
fn delete_selection(&mut self) -> bool {
let select = match self.select_opt.take() {
Some(some) => some,
None => return false,
};
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)
}
}
};
// Reset cursor to start of selection
self.cursor = start;
// Delete the selection from the last line
let end_line_opt = if end.line > start.line {
// Get part of line after selection
let after = self.buffer.lines[end.line].split_off(end.index);
// Remove end line
self.buffer.lines.remove(end.line);
Some(after)
} else {
None
};
// Delete interior lines (in reverse for safety)
for line_i in (start.line + 1..end.line).rev() {
self.buffer.lines.remove(line_i);
}
// Delete the selection from the first line
{
// Get part after selection if start line is also end line
let after_opt = if start.line == end.line {
Some(self.buffer.lines[start.line].split_off(end.index))
} else {
None
};
// Delete selected part of line
self.buffer.lines[start.line].split_off(start.index);
// Re-add part of line after selection
if let Some(after) = after_opt {
self.buffer.lines[start.line].append(after);
}
// Re-add valid parts of end line
if let Some(end_line) = end_line_opt {
self.buffer.lines[start.line].append(end_line);
}
}
true
}
fn action(&mut self, action: Action) {
let old_cursor = self.cursor;
match action {
Action::Previous => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index > 0 {
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().grapheme_indices(true) {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
self.buffer.set_redraw(true);
} else if self.cursor.line > 0 {
self.cursor.line -= 1;
self.cursor.index = self.buffer.lines[self.cursor.line].text().len();
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Next => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index < line.text().len() {
for (i, c) in line.text().grapheme_indices(true) {
if i == self.cursor.index {
self.cursor.index += c.len();
self.buffer.set_redraw(true);
break;
}
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Left => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Next);
} else {
self.action(Action::Previous);
}
}
},
Action::Right => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Previous);
} else {
self.action(Action::Next);
}
}
},
Action::Up => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout > 0 {
cursor.layout -= 1;
} else if cursor.line > 0 {
cursor.line -= 1;
cursor.layout = usize::max_value();
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Down => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
let layout_len = self.buffer.line_layout(cursor.line).expect("layout not found").len();
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout + 1 < layout_len {
cursor.layout += 1;
} else if cursor.line + 1 < self.buffer.lines.len() {
cursor.line += 1;
cursor.layout = 0;
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Home => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = 0;
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
},
Action::End => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = usize::max_value();
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
}
Action::PageUp => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll -= self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::PageDown => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll += self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::Escape => {
if self.select_opt.take().is_some() {
self.buffer.set_redraw(true);
}
},
Action::Insert(character) => {
if character.is_control()
&& !['\t', '\u{92}'].contains(&character)
{
// Filter out special chars (except for tab), use Action instead
log::debug!("Refusing to insert control character {:?}", character);
} else {
self.delete_selection();
let line = &mut self.buffer.lines[self.cursor.line];
// Collect text after insertion as a line
let after = line.split_off(self.cursor.index);
// Append the inserted text
line.append(BufferLine::new(
character.to_string(),
AttrsList::new(line.attrs_list().defaults() /*TODO: provide attrs?*/)
));
// Append the text after insertion
line.append(after);
self.cursor.index += character.len_utf8();
}
},
Action::Enter => {
self.delete_selection();
let new_line = self.buffer.lines[self.cursor.line].split_off(self.cursor.index);
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.lines.insert(self.cursor.line, new_line);
},
Action::Backspace => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index > 0 {
let line = &mut self.buffer.lines[self.cursor.line];
// Get text line after cursor
let after = line.split_off(self.cursor.index);
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().char_indices() {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
// Remove character
line.split_off(self.cursor.index);
// Add text after cursor
line.append(after);
} else if self.cursor.line > 0 {
let mut line_index = self.cursor.line;
let old_line = self.buffer.lines.remove(line_index);
line_index -= 1;
let line = &mut self.buffer.lines[line_index];
self.cursor.line = line_index;
self.cursor.index = line.text().len();
line.append(old_line);
}
},
Action::Delete => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index < self.buffer.lines[self.cursor.line].text().len() {
let line = &mut self.buffer.lines[self.cursor.line];
let range_opt = line
.text()
.grapheme_indices(true)
.take_while(|(i, _)| *i <= self.cursor.index)
.last()
.map(|(i, c)| {
i..(i + c.len())
});
if let Some(range) = range_opt {
self.cursor.index = range.start;
// Get text after deleted EGC
let after = line.split_off(range.end);
// Delete EGC
line.split_off(range.start);
// Add text after deleted EGC
line.append(after);
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
let old_line = self.buffer.lines.remove(self.cursor.line + 1);
self.buffer.lines[self.cursor.line].append(old_line);
}
},
Action::Click { x, y } => {
self.select_opt = None;
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Drag { x, y } => {
if self.select_opt.is_none() {
self.select_opt = Some(self.cursor);
self.buffer.set_redraw(true);
}
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Scroll { lines } => {
let mut scroll = self.buffer.scroll();
scroll += lines;
self.buffer.set_scroll(scroll);
}
}
if old_cursor != self.cursor {
self.cursor_moved = true;
/*TODO
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
let font_opt = self.buffer.font_system().get_font(glyph.cache_key.font_id);
let text_glyph = &run.text[glyph.start..glyph.end];
log::debug!(
"{}, {}: '{}' ('{}'): '{}' ({:?})",
self.cursor.line,
self.cursor.index,
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()),
text_glyph,
text_glyph
);
}
*/
}
}sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset shaping and layout information
Examples found in repository?
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
pub fn set_text<T: AsRef<str> + Into<String>>(&mut self, text: T, attrs_list: AttrsList) -> bool {
if text.as_ref() != self.text || attrs_list != self.attrs_list {
self.text = text.into();
self.attrs_list = attrs_list;
self.reset();
true
} else {
false
}
}
/// Get attributes list
pub fn attrs_list(&self) -> &AttrsList {
&self.attrs_list
}
/// Set attributes list
///
/// Will reset shape and layout if it differs from current attributes list.
/// Returns true if the line was reset
pub fn set_attrs_list(&mut self, attrs_list: AttrsList) -> bool {
if attrs_list != self.attrs_list {
self.attrs_list = attrs_list;
self.reset();
true
} else {
false
}
}
/// Get simple wrapping setting (wrap by characters only)
pub fn wrap_simple(&self) -> bool {
self.wrap_simple
}
/// Set simple wrapping setting (wrap by characters only)
///
/// Will reset shape and layout if it differs from current simple wrapping setting.
/// Returns true if the line was reset
pub fn set_wrap_simple(&mut self, wrap_simple: bool) -> bool {
if wrap_simple != self.wrap_simple {
self.wrap_simple = wrap_simple;
self.reset();
true
} else {
false
}
}
/// Append line at end of this line
///
/// The wrap setting of the appended line will be lost
pub fn append(&mut self, other: Self) {
let len = self.text.len();
self.text.push_str(other.text());
if other.attrs_list.defaults() != self.attrs_list.defaults() {
// If default formatting does not match, make a new span for it
self.attrs_list.add_span(len..len + other.text().len(), other.attrs_list.defaults());
}
for (other_range, attrs) in other.attrs_list.spans() {
// Add previous attrs spans
let range = other_range.start + len..other_range.end + len;
self.attrs_list.add_span(range, attrs.as_attrs());
}
self.reset();
}
/// Split off new line at index
pub fn split_off(&mut self, index: usize) -> Self {
let text = self.text.split_off(index);
let attrs_list = self.attrs_list.split_off(index);
self.reset();
let mut new = Self::new(text, attrs_list);
new.wrap_simple = self.wrap_simple;
new
}sourcepub fn reset_layout(&mut self)
pub fn reset_layout(&mut self)
Reset only layout information
Examples found in repository?
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
fn relayout(&mut self) {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
for line in &mut self.lines {
if line.shape_opt().is_some() {
line.reset_layout();
line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
}
}
self.redraw = true;
#[cfg(feature = "std")]
log::debug!("relayout: {:?}", instant.elapsed());
}sourcepub fn shape(&mut self, font_system: &FontSystem) -> &ShapeLine
pub fn shape(&mut self, font_system: &FontSystem) -> &ShapeLine
Shape line, will cache results
Examples found in repository?
More examples
155 156 157 158 159 160 161 162 163 164 165 166 167
pub fn layout(&mut self, font_system: &FontSystem, font_size: i32, width: i32) -> &[LayoutLine] {
if self.layout_opt.is_none() {
let wrap_simple = self.wrap_simple;
let shape = self.shape(font_system);
let layout = shape.layout(
font_size,
width,
wrap_simple
);
self.layout_opt = Some(layout);
}
self.layout_opt.as_ref().expect("layout not found")
}sourcepub fn shape_opt(&self) -> &Option<ShapeLine>
pub fn shape_opt(&self) -> &Option<ShapeLine>
Get line shaping cache
Examples found in repository?
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
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(LayoutRun {
line_i: self.line_i,
text: line.text(),
rtl: shape.rtl,
glyphs: &layout_line.glyphs,
line_y: self.line_y,
line_w: layout_line.w,
});
}
self.line_i += 1;
self.layout_i = 0;
}
None
}
}
/// Metrics of text
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Metrics {
/// Font size in pixels
pub font_size: i32,
/// Line height in pixels
pub line_height: i32,
}
impl Metrics {
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 Metrics {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}px / {}px", self.font_size, self.line_height)
}
}
/// A buffer of text that is shaped and laid out
pub struct Buffer<'a> {
font_system: &'a FontSystem,
/// [BufferLine]s (or paragraphs) of text in the buffer
pub lines: Vec<BufferLine>,
metrics: Metrics,
width: i32,
height: i32,
scroll: i32,
/// True if a redraw is requires. Set to false after processing
redraw: bool,
}
impl<'a> Buffer<'a> {
/// Create a new [`Buffer`] with the provided [`FontSystem`] and [`Metrics`]
pub fn new(
font_system: &'a FontSystem,
metrics: Metrics,
) -> Self {
let mut buffer = Self {
font_system,
lines: Vec::new(),
metrics,
width: 0,
height: 0,
scroll: 0,
redraw: false,
};
buffer.set_text("", Attrs::new());
buffer
}
fn relayout(&mut self) {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
for line in &mut self.lines {
if line.shape_opt().is_some() {
line.reset_layout();
line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
}
}
self.redraw = true;
#[cfg(feature = "std")]
log::debug!("relayout: {:?}", instant.elapsed());
}
/// Pre-shape lines in the buffer, up to `lines`, return actual number of layout lines
pub fn shape_until(&mut self, lines: i32) -> i32 {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
let mut reshaped = 0;
let mut total_layout = 0;
for line in &mut self.lines {
if total_layout >= lines {
break;
}
if line.shape_opt().is_none() {
reshaped += 1;
}
let layout = line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
total_layout += layout.len() as i32;
}
if reshaped > 0 {
#[cfg(feature = "std")]
log::debug!("shape_until {}: {:?}", reshaped, instant.elapsed());
self.redraw = true;
}
total_layout
}
/// Shape lines until cursor, also scrolling to include cursor in view
pub fn shape_until_cursor(&mut self, cursor: Cursor) {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
let mut reshaped = 0;
let mut layout_i = 0;
for (line_i, line) in self.lines.iter_mut().enumerate() {
if line_i > cursor.line {
break;
}
if line.shape_opt().is_none() {
reshaped += 1;
}
let layout = line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
if line_i == cursor.line {
let layout_cursor = self.layout_cursor(&cursor);
layout_i += layout_cursor.layout as i32;
break;
} else {
layout_i += layout.len() as i32;
}
}
if reshaped > 0 {
#[cfg(feature = "std")]
log::debug!("shape_until_cursor {}: {:?}", reshaped, instant.elapsed());
self.redraw = true;
}
let lines = self.visible_lines();
if layout_i < self.scroll {
self.scroll = layout_i;
} else if layout_i >= self.scroll + lines {
self.scroll = layout_i - (lines - 1);
}
self.shape_until_scroll();
}More examples
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
fn action(&mut self, action: Action) {
let old_cursor = self.cursor;
match action {
Action::Previous => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index > 0 {
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().grapheme_indices(true) {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
self.buffer.set_redraw(true);
} else if self.cursor.line > 0 {
self.cursor.line -= 1;
self.cursor.index = self.buffer.lines[self.cursor.line].text().len();
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Next => {
let line = &mut self.buffer.lines[self.cursor.line];
if self.cursor.index < line.text().len() {
for (i, c) in line.text().grapheme_indices(true) {
if i == self.cursor.index {
self.cursor.index += c.len();
self.buffer.set_redraw(true);
break;
}
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.set_redraw(true);
}
self.cursor_x_opt = None;
},
Action::Left => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Next);
} else {
self.action(Action::Previous);
}
}
},
Action::Right => {
let rtl_opt = self.buffer.lines[self.cursor.line].shape_opt().as_ref().map(|shape| shape.rtl);
if let Some(rtl) = rtl_opt {
if rtl {
self.action(Action::Previous);
} else {
self.action(Action::Next);
}
}
},
Action::Up => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout > 0 {
cursor.layout -= 1;
} else if cursor.line > 0 {
cursor.line -= 1;
cursor.layout = usize::max_value();
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Down => {
//TODO: make this preserve X as best as possible!
let mut cursor = self.buffer.layout_cursor(&self.cursor);
let layout_len = self.buffer.line_layout(cursor.line).expect("layout not found").len();
if self.cursor_x_opt.is_none() {
self.cursor_x_opt = Some(
cursor.glyph as i32 //TODO: glyph x position
);
}
if cursor.layout + 1 < layout_len {
cursor.layout += 1;
} else if cursor.line + 1 < self.buffer.lines.len() {
cursor.line += 1;
cursor.layout = 0;
}
if let Some(cursor_x) = self.cursor_x_opt {
cursor.glyph = cursor_x as usize; //TODO: glyph x position
}
self.set_layout_cursor(cursor);
},
Action::Home => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = 0;
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
},
Action::End => {
let mut cursor = self.buffer.layout_cursor(&self.cursor);
cursor.glyph = usize::max_value();
self.set_layout_cursor(cursor);
self.cursor_x_opt = None;
}
Action::PageUp => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll -= self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::PageDown => {
//TODO: move cursor
let mut scroll = self.buffer.scroll();
scroll += self.buffer.visible_lines();
self.buffer.set_scroll(scroll);
},
Action::Escape => {
if self.select_opt.take().is_some() {
self.buffer.set_redraw(true);
}
},
Action::Insert(character) => {
if character.is_control()
&& !['\t', '\u{92}'].contains(&character)
{
// Filter out special chars (except for tab), use Action instead
log::debug!("Refusing to insert control character {:?}", character);
} else {
self.delete_selection();
let line = &mut self.buffer.lines[self.cursor.line];
// Collect text after insertion as a line
let after = line.split_off(self.cursor.index);
// Append the inserted text
line.append(BufferLine::new(
character.to_string(),
AttrsList::new(line.attrs_list().defaults() /*TODO: provide attrs?*/)
));
// Append the text after insertion
line.append(after);
self.cursor.index += character.len_utf8();
}
},
Action::Enter => {
self.delete_selection();
let new_line = self.buffer.lines[self.cursor.line].split_off(self.cursor.index);
self.cursor.line += 1;
self.cursor.index = 0;
self.buffer.lines.insert(self.cursor.line, new_line);
},
Action::Backspace => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index > 0 {
let line = &mut self.buffer.lines[self.cursor.line];
// Get text line after cursor
let after = line.split_off(self.cursor.index);
// Find previous character index
let mut prev_index = 0;
for (i, _) in line.text().char_indices() {
if i < self.cursor.index {
prev_index = i;
} else {
break;
}
}
self.cursor.index = prev_index;
// Remove character
line.split_off(self.cursor.index);
// Add text after cursor
line.append(after);
} else if self.cursor.line > 0 {
let mut line_index = self.cursor.line;
let old_line = self.buffer.lines.remove(line_index);
line_index -= 1;
let line = &mut self.buffer.lines[line_index];
self.cursor.line = line_index;
self.cursor.index = line.text().len();
line.append(old_line);
}
},
Action::Delete => {
if self.delete_selection() {
// Deleted selection
} else if self.cursor.index < self.buffer.lines[self.cursor.line].text().len() {
let line = &mut self.buffer.lines[self.cursor.line];
let range_opt = line
.text()
.grapheme_indices(true)
.take_while(|(i, _)| *i <= self.cursor.index)
.last()
.map(|(i, c)| {
i..(i + c.len())
});
if let Some(range) = range_opt {
self.cursor.index = range.start;
// Get text after deleted EGC
let after = line.split_off(range.end);
// Delete EGC
line.split_off(range.start);
// Add text after deleted EGC
line.append(after);
}
} else if self.cursor.line + 1 < self.buffer.lines.len() {
let old_line = self.buffer.lines.remove(self.cursor.line + 1);
self.buffer.lines[self.cursor.line].append(old_line);
}
},
Action::Click { x, y } => {
self.select_opt = None;
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Drag { x, y } => {
if self.select_opt.is_none() {
self.select_opt = Some(self.cursor);
self.buffer.set_redraw(true);
}
if let Some(new_cursor) = self.buffer.hit(x, y) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
}
}
},
Action::Scroll { lines } => {
let mut scroll = self.buffer.scroll();
scroll += lines;
self.buffer.set_scroll(scroll);
}
}
if old_cursor != self.cursor {
self.cursor_moved = true;
/*TODO
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
let font_opt = self.buffer.font_system().get_font(glyph.cache_key.font_id);
let text_glyph = &run.text[glyph.start..glyph.end];
log::debug!(
"{}, {}: '{}' ('{}'): '{}' ({:?})",
self.cursor.line,
self.cursor.index,
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()),
text_glyph,
text_glyph
);
}
*/
}
}sourcepub fn layout(
&mut self,
font_system: &FontSystem,
font_size: i32,
width: i32
) -> &[LayoutLine]
pub fn layout(
&mut self,
font_system: &FontSystem,
font_size: i32,
width: i32
) -> &[LayoutLine]
Layout line, will cache results
Examples found in repository?
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
fn relayout(&mut self) {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
for line in &mut self.lines {
if line.shape_opt().is_some() {
line.reset_layout();
line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
}
}
self.redraw = true;
#[cfg(feature = "std")]
log::debug!("relayout: {:?}", instant.elapsed());
}
/// Pre-shape lines in the buffer, up to `lines`, return actual number of layout lines
pub fn shape_until(&mut self, lines: i32) -> i32 {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
let mut reshaped = 0;
let mut total_layout = 0;
for line in &mut self.lines {
if total_layout >= lines {
break;
}
if line.shape_opt().is_none() {
reshaped += 1;
}
let layout = line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
total_layout += layout.len() as i32;
}
if reshaped > 0 {
#[cfg(feature = "std")]
log::debug!("shape_until {}: {:?}", reshaped, instant.elapsed());
self.redraw = true;
}
total_layout
}
/// Shape lines until cursor, also scrolling to include cursor in view
pub fn shape_until_cursor(&mut self, cursor: Cursor) {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
let mut reshaped = 0;
let mut layout_i = 0;
for (line_i, line) in self.lines.iter_mut().enumerate() {
if line_i > cursor.line {
break;
}
if line.shape_opt().is_none() {
reshaped += 1;
}
let layout = line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
if line_i == cursor.line {
let layout_cursor = self.layout_cursor(&cursor);
layout_i += layout_cursor.layout as i32;
break;
} else {
layout_i += layout.len() as i32;
}
}
if reshaped > 0 {
#[cfg(feature = "std")]
log::debug!("shape_until_cursor {}: {:?}", reshaped, instant.elapsed());
self.redraw = true;
}
let lines = self.visible_lines();
if layout_i < self.scroll {
self.scroll = layout_i;
} else if layout_i >= self.scroll + lines {
self.scroll = layout_i - (lines - 1);
}
self.shape_until_scroll();
}
/// Shape lines until scroll
pub fn shape_until_scroll(&mut self) {
let lines = self.visible_lines();
let scroll_end = self.scroll + lines;
let total_layout = self.shape_until(scroll_end);
self.scroll = cmp::max(
0,
cmp::min(
total_layout - (lines - 1),
self.scroll,
),
);
}
pub fn layout_cursor(&self, cursor: &Cursor) -> LayoutCursor {
let line = &self.lines[cursor.line];
//TODO: ensure layout is done?
let layout = line.layout_opt().as_ref().expect("layout not found");
for (layout_i, layout_line) in layout.iter().enumerate() {
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
if cursor.index == glyph.start {
return LayoutCursor::new(
cursor.line,
layout_i,
glyph_i
);
}
}
match layout_line.glyphs.last() {
Some(glyph) => {
if cursor.index == glyph.end {
return LayoutCursor::new(
cursor.line,
layout_i,
layout_line.glyphs.len()
);
}
},
None => {
return LayoutCursor::new(
cursor.line,
layout_i,
0
);
}
}
}
// Fall back to start of line
//TODO: should this be the end of the line?
LayoutCursor::new(
cursor.line,
0,
0
)
}
/// Get [`FontSystem`] used by this [`Buffer`]
pub fn font_system(&self) -> &'a FontSystem {
self.font_system
}
/// Shape the provided line index and return the result
pub fn line_shape(&mut self, line_i: usize) -> Option<&ShapeLine> {
let line = self.lines.get_mut(line_i)?;
Some(line.shape(self.font_system))
}
/// Lay out the provided line index and return the result
pub fn line_layout(&mut self, line_i: usize) -> Option<&[LayoutLine]> {
let line = self.lines.get_mut(line_i)?;
Some(line.layout(self.font_system, self.metrics.font_size, self.width))
}sourcepub fn layout_opt(&self) -> &Option<Vec<LayoutLine>>
pub fn layout_opt(&self) -> &Option<Vec<LayoutLine>>
Get line layout cache
Examples found in repository?
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
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(LayoutRun {
line_i: self.line_i,
text: line.text(),
rtl: shape.rtl,
glyphs: &layout_line.glyphs,
line_y: self.line_y,
line_w: layout_line.w,
});
}
self.line_i += 1;
self.layout_i = 0;
}
None
}
}
/// Metrics of text
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Metrics {
/// Font size in pixels
pub font_size: i32,
/// Line height in pixels
pub line_height: i32,
}
impl Metrics {
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 Metrics {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}px / {}px", self.font_size, self.line_height)
}
}
/// A buffer of text that is shaped and laid out
pub struct Buffer<'a> {
font_system: &'a FontSystem,
/// [BufferLine]s (or paragraphs) of text in the buffer
pub lines: Vec<BufferLine>,
metrics: Metrics,
width: i32,
height: i32,
scroll: i32,
/// True if a redraw is requires. Set to false after processing
redraw: bool,
}
impl<'a> Buffer<'a> {
/// Create a new [`Buffer`] with the provided [`FontSystem`] and [`Metrics`]
pub fn new(
font_system: &'a FontSystem,
metrics: Metrics,
) -> Self {
let mut buffer = Self {
font_system,
lines: Vec::new(),
metrics,
width: 0,
height: 0,
scroll: 0,
redraw: false,
};
buffer.set_text("", Attrs::new());
buffer
}
fn relayout(&mut self) {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
for line in &mut self.lines {
if line.shape_opt().is_some() {
line.reset_layout();
line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
}
}
self.redraw = true;
#[cfg(feature = "std")]
log::debug!("relayout: {:?}", instant.elapsed());
}
/// Pre-shape lines in the buffer, up to `lines`, return actual number of layout lines
pub fn shape_until(&mut self, lines: i32) -> i32 {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
let mut reshaped = 0;
let mut total_layout = 0;
for line in &mut self.lines {
if total_layout >= lines {
break;
}
if line.shape_opt().is_none() {
reshaped += 1;
}
let layout = line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
total_layout += layout.len() as i32;
}
if reshaped > 0 {
#[cfg(feature = "std")]
log::debug!("shape_until {}: {:?}", reshaped, instant.elapsed());
self.redraw = true;
}
total_layout
}
/// Shape lines until cursor, also scrolling to include cursor in view
pub fn shape_until_cursor(&mut self, cursor: Cursor) {
#[cfg(feature = "std")]
let instant = std::time::Instant::now();
let mut reshaped = 0;
let mut layout_i = 0;
for (line_i, line) in self.lines.iter_mut().enumerate() {
if line_i > cursor.line {
break;
}
if line.shape_opt().is_none() {
reshaped += 1;
}
let layout = line.layout(
self.font_system,
self.metrics.font_size,
self.width
);
if line_i == cursor.line {
let layout_cursor = self.layout_cursor(&cursor);
layout_i += layout_cursor.layout as i32;
break;
} else {
layout_i += layout.len() as i32;
}
}
if reshaped > 0 {
#[cfg(feature = "std")]
log::debug!("shape_until_cursor {}: {:?}", reshaped, instant.elapsed());
self.redraw = true;
}
let lines = self.visible_lines();
if layout_i < self.scroll {
self.scroll = layout_i;
} else if layout_i >= self.scroll + lines {
self.scroll = layout_i - (lines - 1);
}
self.shape_until_scroll();
}
/// Shape lines until scroll
pub fn shape_until_scroll(&mut self) {
let lines = self.visible_lines();
let scroll_end = self.scroll + lines;
let total_layout = self.shape_until(scroll_end);
self.scroll = cmp::max(
0,
cmp::min(
total_layout - (lines - 1),
self.scroll,
),
);
}
pub fn layout_cursor(&self, cursor: &Cursor) -> LayoutCursor {
let line = &self.lines[cursor.line];
//TODO: ensure layout is done?
let layout = line.layout_opt().as_ref().expect("layout not found");
for (layout_i, layout_line) in layout.iter().enumerate() {
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
if cursor.index == glyph.start {
return LayoutCursor::new(
cursor.line,
layout_i,
glyph_i
);
}
}
match layout_line.glyphs.last() {
Some(glyph) => {
if cursor.index == glyph.end {
return LayoutCursor::new(
cursor.line,
layout_i,
layout_line.glyphs.len()
);
}
},
None => {
return LayoutCursor::new(
cursor.line,
layout_i,
0
);
}
}
}
// Fall back to start of line
//TODO: should this be the end of the line?
LayoutCursor::new(
cursor.line,
0,
0
)
}