ltk/widget/text_edit/
tabs.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Horizontal positions in a row of a multiline field, where a tab
//! reaches the next stop.

use crate::render::Canvas;

use super::theme;
use super::TextFont;

pub( crate ) fn tab_width( canvas: &Canvas, font: &TextFont, px: f32 ) -> f32
{
	font.measure_char( canvas, '0', px ) * theme::TAB_COLUMNS
}

/// The first tab stop past `x`, measured from the start of the row.
pub( crate ) fn next_stop( x: f32, tab_w: f32 ) -> f32
{
	if tab_w <= 0.0 { return x; }
	( ( x / tab_w ).floor() + 1.0 ) * tab_w
}

/// How far into the row starting at byte `start` the byte `at` is.
pub( crate ) fn line_x( canvas: &Canvas, font: &TextFont, value: &str, start: usize, at: usize, px: f32 ) -> f32
{
	let text = &value[ start..at ];
	if !text.contains( '\t' ) { return font.measure( canvas, text, px ); }
	let tab_w = tab_width( canvas, font, px );
	let mut x = 0.0;
	for ( i, piece ) in text.split( '\t' ).enumerate()
	{
		if i > 0 { x = next_stop( x, tab_w ); }
		if !piece.is_empty() { x += font.measure( canvas, piece, px ); }
	}
	x
}