Make grid RTL aware

This commit is contained in:
Laurenz 2023-02-12 12:57:40 +01:00
parent 8951b1923a
commit 01a62a690b
4 changed files with 26 additions and 2 deletions

View File

@ -131,7 +131,7 @@ impl Layout for ColumnsNode {
} }
let width = frame.width(); let width = frame.width();
let x = if dir.is_positive() { let x = if dir == Dir::LTR {
cursor cursor
} else { } else {
regions.first.x - cursor - width regions.first.x - cursor - width

View File

@ -1,4 +1,5 @@
use crate::prelude::*; use crate::prelude::*;
use crate::text::TextNode;
use super::Spacing; use super::Spacing;
@ -218,6 +219,8 @@ struct GridLayouter<'a, 'v> {
vt: &'a mut Vt<'v>, vt: &'a mut Vt<'v>,
/// The grid cells. /// The grid cells.
cells: &'a [Content], cells: &'a [Content],
/// Whether this is an RTL grid.
rtl: bool,
/// The column tracks including gutter tracks. /// The column tracks including gutter tracks.
cols: Vec<TrackSizing>, cols: Vec<TrackSizing>,
/// The row tracks including gutter tracks. /// The row tracks including gutter tracks.
@ -299,6 +302,12 @@ impl<'a, 'v> GridLayouter<'a, 'v> {
cols.pop(); cols.pop();
rows.pop(); rows.pop();
// Reverse for RTL.
let rtl = styles.get(TextNode::DIR) == Dir::RTL;
if rtl {
cols.reverse();
}
let full = regions.first.y; let full = regions.first.y;
let rcols = vec![Abs::zero(); cols.len()]; let rcols = vec![Abs::zero(); cols.len()];
let lrows = vec![]; let lrows = vec![];
@ -311,6 +320,7 @@ impl<'a, 'v> GridLayouter<'a, 'v> {
Self { Self {
vt, vt,
cells, cells,
rtl,
cols, cols,
rows, rows,
regions, regions,
@ -680,10 +690,15 @@ impl<'a, 'v> GridLayouter<'a, 'v> {
/// ///
/// Returns `None` if it's a gutter cell. /// Returns `None` if it's a gutter cell.
#[track_caller] #[track_caller]
fn cell(&self, x: usize, y: usize) -> Option<&'a Content> { fn cell(&self, mut x: usize, y: usize) -> Option<&'a Content> {
assert!(x < self.cols.len()); assert!(x < self.cols.len());
assert!(y < self.rows.len()); assert!(y < self.rows.len());
// Columns are reorded, but the cell slice is not.
if self.rtl {
x = self.cols.len() - 1 - x;
}
// Even columns and rows are children, odd ones are gutter. // Even columns and rows are children, odd ones are gutter.
if x % 2 == 0 && y % 2 == 0 { if x % 2 == 0 && y % 2 == 0 {
let c = 1 + self.cols.len() / 2; let c = 1 + self.cols.len() / 2;

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -0,0 +1,9 @@
// Test RTL grid.
---
#set text(dir: rtl)
- מימין לשמאל
---
#set text(dir: rtl)
#table(columns: 2, gutter: 5pt)[A][B][C][D]