diff --git a/src/buffer.rs b/src/buffer.rs index 6bb8b35..747f891 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -302,9 +302,16 @@ impl<'a> TextBuffer<'a> { self.cursor.glyph = line.glyphs.len(); self.redraw = true; } - if self.cursor.glyph > 0 { - self.cursor.glyph -= 1; - self.redraw = true; + if line.rtl { + if self.cursor.glyph < line.glyphs.len() { + self.cursor.glyph += 1; + self.redraw = true; + } + } else { + if self.cursor.glyph > 0 { + self.cursor.glyph -= 1; + self.redraw = true; + } } }, TextAction::Right => { @@ -313,9 +320,16 @@ impl<'a> TextBuffer<'a> { self.cursor.glyph = line.glyphs.len(); self.redraw = true; } - if self.cursor.glyph < line.glyphs.len() { - self.cursor.glyph += 1; - self.redraw = true; + if line.rtl { + if self.cursor.glyph > 0 { + self.cursor.glyph -= 1; + self.redraw = true; + } + } else { + if self.cursor.glyph < line.glyphs.len() { + self.cursor.glyph += 1; + self.redraw = true; + } } }, TextAction::Up => { @@ -446,7 +460,8 @@ impl<'a> TextBuffer<'a> { if mouse_x >= glyph.x as i32 && mouse_x <= (glyph.x + glyph.w) as i32 { - if mouse_x >= (glyph.x + glyph.w / 2.0) as i32 { + let right_half = mouse_x >= (glyph.x + glyph.w / 2.0) as i32; + if right_half == !line.rtl { // If clicking on last half of glyph, move cursor past glyph new_cursor_glyph = glyph_i + 1; } else { @@ -469,6 +484,8 @@ impl<'a> TextBuffer<'a> { let duration = instant.elapsed(); log::debug!("click({}, {}): {:?}", mouse_x, mouse_y, duration); + + println!("cursor {:?} select {:?}", self.cursor, self.select_opt); } /// Draw the buffer @@ -516,17 +533,30 @@ impl<'a> TextBuffer<'a> { }; if end_glyph > start_glyph { - let start_x = line.glyphs.get(start_glyph).map_or(0, |glyph| { - glyph.x as i32 - }); - let end_x = line.glyphs.get(end_glyph - 1).map_or(self.width, |glyph| { - (glyph.x + glyph.w) as i32 - }); + let (left_x, right_x) = if line.rtl { + ( + line.glyphs.get(end_glyph - 1).map_or(0, |glyph| { + glyph.x as i32 + }), + line.glyphs.get(start_glyph).map_or(self.width, |glyph| { + (glyph.x + glyph.w) as i32 + }), + ) + } else { + ( + line.glyphs.get(start_glyph).map_or(0, |glyph| { + glyph.x as i32 + }), + line.glyphs.get(end_glyph - 1).map_or(self.width, |glyph| { + (glyph.x + glyph.w) as i32 + }), + ) + }; f( - start_x, + left_x, line_y - font_size, - cmp::max(0, end_x - start_x) as u32, + cmp::max(0, right_x - left_x) as u32, line_height as u32, 0x33_00_00_00 | (color & 0xFF_FF_FF) ); @@ -541,6 +571,7 @@ impl<'a> TextBuffer<'a> { Some(glyph) => glyph.x + glyph.w, None => 0.0, }; + f( x as i32, line_y - font_size, @@ -550,23 +581,19 @@ impl<'a> TextBuffer<'a> { ); } else { let glyph = &line.glyphs[self.cursor.glyph]; + let x = if line.rtl { + (glyph.x + glyph.w) as i32 + } else { + glyph.x as i32 + }; + f( - glyph.x as i32, + x, line_y - font_size, 1, line_height as u32, color, ); - - let text_line = &self.text_lines()[line.line_i.get()]; - log::info!( - "{}, {}: '{}' ('{}'): '{}'", - glyph.start, - glyph.end, - glyph.font.info.family, - glyph.font.info.post_script_name, - &text_line[glyph.start..glyph.end], - ); } } diff --git a/src/font/layout.rs b/src/font/layout.rs index 076e2fe..8594f53 100644 --- a/src/font/layout.rs +++ b/src/font/layout.rs @@ -12,6 +12,7 @@ pub struct FontLayoutGlyph<'a> { pub struct FontLayoutLine<'a> { pub line_i: TextLineIndex, + pub rtl: bool, pub glyphs: Vec>, } diff --git a/src/font/shape.rs b/src/font/shape.rs index 48ac024..fa22d8f 100644 --- a/src/font/shape.rs +++ b/src/font/shape.rs @@ -166,6 +166,7 @@ impl<'a> FontShapeLine<'a> { layout_i, FontLayoutLine { line_i: self.line_i, + rtl: self.rtl, glyphs: glyphs_swap, }, ); @@ -200,6 +201,7 @@ impl<'a> FontShapeLine<'a> { layout_i, FontLayoutLine { line_i: self.line_i, + rtl: self.rtl, glyphs: glyphs_swap, }, ); @@ -216,6 +218,7 @@ impl<'a> FontShapeLine<'a> { layout_i, FontLayoutLine { line_i: self.line_i, + rtl: self.rtl, glyphs, }, );