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
38
39
40
41
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
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
//! Terminal layout geometry, scroll state, and viewport math.
use crate::frame::VisualLine;
// ---------------------------------------------------------------------------
// Layout / ScrollState
// ---------------------------------------------------------------------------
#[derive(Clone, Copy)]
pub(super) struct Layout {
pub sidebar_cols: u16,
pub image_col: u16, // 画像領域の開始列 (= sidebar_cols)
pub image_cols: u16, // 画像領域の幅 (= term_cols - sidebar_cols)
pub image_rows: u16, // 画像領域の高さ (= term_rows - 1)
pub status_row: u16, // ステータスバーの行 (= term_rows - 1)
pub cell_w: u16, // ピクセル/セル(幅)
pub cell_h: u16, // ピクセル/セル(高さ)
}
/// Scroll position and viewport/document pixel dimensions.
///
/// Groups the data needed for scroll-bound calculations: current position
/// (`y_offset`), document height (`img_h`), and viewport dimensions
/// (`vp_w`, `vp_h`).
pub(super) struct ScrollState {
pub y_offset: u32, // スクロールオフセット(ピクセル)
pub img_h: u32, // ドキュメント高さ(ピクセル)
pub vp_w: u32, // ビューポート幅(ピクセル)
pub vp_h: u32, // ビューポート高さ(ピクセル)
}
impl Layout {
/// Viewport width in typst points.
pub(super) fn viewport_width_pt(&self, ppi: f64) -> f64 {
self.image_cols as f64 * self.cell_w as f64 * 72.0 / ppi
}
/// Viewport height in typst points.
pub(super) fn viewport_height_pt(&self, ppi: f64) -> f64 {
self.image_rows as f64 * self.cell_h as f64 * 72.0 / ppi
}
/// Sidebar width in typst points.
pub(super) fn sidebar_width_pt(&self, ppi: f64) -> f64 {
self.sidebar_cols as f64 * self.cell_w as f64 * 72.0 / ppi
}
/// Align tile height (pt) to cell_h pixel boundary, ensuring exact 1:1 scaling.
pub(super) fn align_tile_height_pt(&self, tile_height_pt: f64, ppi: f64) -> f64 {
let raw_px = (tile_height_pt * ppi / 72.0).round() as u32;
let cell_h = self.cell_h as u32;
let aligned_px = raw_px.div_ceil(cell_h) * cell_h;
aligned_px as f64 * 72.0 / ppi
}
}
pub(super) fn compute_layout(
term_cols: u16,
term_rows: u16,
pixel_w: u16,
pixel_h: u16,
sidebar_cols: u16,
) -> Layout {
let image_col = sidebar_cols;
let image_cols = term_cols.saturating_sub(sidebar_cols);
let image_rows = term_rows.saturating_sub(1);
let status_row = term_rows.saturating_sub(1);
let cell_w = pixel_w.checked_div(term_cols).unwrap_or(1);
let cell_h = pixel_h.checked_div(term_rows).unwrap_or(1);
Layout {
sidebar_cols,
image_col,
image_cols,
image_rows,
status_row,
cell_w,
cell_h,
}
}
pub(super) fn vp_dims(layout: &Layout, img_w: u32, img_h: u32) -> (u32, u32) {
let vp_w = (layout.image_cols as u32 * layout.cell_w as u32).min(img_w);
let vp_h = (layout.image_rows as u32 * layout.cell_h as u32).min(img_h);
(vp_w, vp_h)
}
/// Compute the y_offset for a 1-based visual line number (pure function, no mutation).
pub(super) fn visual_line_offset(
visual_lines: &[VisualLine],
max_scroll: u32,
line_num: u32,
) -> u32 {
let idx = (line_num as usize).saturating_sub(1); // 1-based to 0-based
if idx < visual_lines.len() {
// Use the previous line's baseline as the scroll target so that line N
// appears fully visible at the top (y_px is the baseline, not the ascender).
visual_lines[idx.saturating_sub(1)].y_px.min(max_scroll)
} else {
0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compute_layout_basic() {
let l = compute_layout(80, 24, 1280, 576, 6);
assert_eq!(l.sidebar_cols, 6);
assert_eq!(l.image_col, 6);
assert_eq!(l.image_cols, 74);
assert_eq!(l.image_rows, 23);
assert_eq!(l.status_row, 23);
assert_eq!(l.cell_w, 16); // 1280/80
assert_eq!(l.cell_h, 24); // 576/24
}
#[test]
fn compute_layout_zero_cols_no_panic() {
let l = compute_layout(0, 0, 0, 0, 0);
assert_eq!(l.image_cols, 0);
assert_eq!(l.cell_w, 1); // fallback
assert_eq!(l.cell_h, 1);
}
#[test]
fn visual_line_offset_first_line() {
let vls = vec![
VisualLine {
y_pt: 0.0,
y_px: 0,
md_block_range: None,
md_offset: None,
diff_status: None,
},
VisualLine {
y_pt: 0.0,
y_px: 100,
md_block_range: None,
md_offset: None,
diff_status: None,
},
];
// Line 1 → idx 0, saturating_sub(1) → idx 0 → y_px 0
assert_eq!(visual_line_offset(&vls, 1000, 1), 0);
}
#[test]
fn visual_line_offset_middle_line() {
let vls = vec![
VisualLine {
y_pt: 0.0,
y_px: 0,
md_block_range: None,
md_offset: None,
diff_status: None,
},
VisualLine {
y_pt: 0.0,
y_px: 100,
md_block_range: None,
md_offset: None,
diff_status: None,
},
VisualLine {
y_pt: 0.0,
y_px: 200,
md_block_range: None,
md_offset: None,
diff_status: None,
},
];
// Line 3 → idx 2, previous line idx 1 → y_px 100
assert_eq!(visual_line_offset(&vls, 1000, 3), 100);
}
#[test]
fn visual_line_offset_out_of_range() {
let vls = vec![VisualLine {
y_pt: 0.0,
y_px: 0,
md_block_range: None,
md_offset: None,
diff_status: None,
}];
assert_eq!(visual_line_offset(&vls, 1000, 99), 0);
}
#[test]
fn visual_line_offset_clamps_to_max() {
let vls = vec![
VisualLine {
y_pt: 0.0,
y_px: 0,
md_block_range: None,
md_offset: None,
diff_status: None,
},
VisualLine {
y_pt: 0.0,
y_px: 500,
md_block_range: None,
md_offset: None,
diff_status: None,
},
];
// Line 2 → idx 1, previous idx 0 → y_px 0, min(100) → 0
assert_eq!(visual_line_offset(&vls, 100, 2), 0);
// With high y_px that exceeds max_scroll
let vls2 = vec![
VisualLine {
y_pt: 0.0,
y_px: 0,
md_block_range: None,
md_offset: None,
diff_status: None,
},
VisualLine {
y_pt: 0.0,
y_px: 9999,
md_block_range: None,
md_offset: None,
diff_status: None,
},
VisualLine {
y_pt: 0.0,
y_px: 10000,
md_block_range: None,
md_offset: None,
diff_status: None,
},
];
// Line 3 → idx 2, previous idx 1 → y_px 9999, min(500) → 500
assert_eq!(visual_line_offset(&vls2, 500, 3), 500);
}
#[test]
fn vp_dims_viewport_smaller_than_image() {
let l = compute_layout(80, 24, 1280, 576, 6);
let (vp_w, vp_h) = vp_dims(&l, 2000, 5000);
// image_cols=74, cell_w=16 → 74*16=1184 < 2000
assert_eq!(vp_w, 1184);
// image_rows=23, cell_h=24 → 23*24=552 < 5000
assert_eq!(vp_h, 552);
}
#[test]
fn vp_dims_viewport_larger_than_image() {
let l = compute_layout(80, 24, 1280, 576, 6);
let (vp_w, vp_h) = vp_dims(&l, 100, 200);
assert_eq!(vp_w, 100);
assert_eq!(vp_h, 200);
}
#[test]
fn layout_pt_conversions() {
// 80x24 terminal, 1280x576 pixels → cell_w=16, cell_h=24
// image_cols=74, image_rows=23, sidebar_cols=6
let l = compute_layout(80, 24, 1280, 576, 6);
let ppi = 144.0;
// viewport_width_pt: 74 * 16 * 72 / 144 = 1184 * 0.5 = 592.0
assert_eq!(l.viewport_width_pt(ppi), 592.0);
// viewport_height_pt: 23 * 24 * 72 / 144 = 552 * 0.5 = 276.0
assert_eq!(l.viewport_height_pt(ppi), 276.0);
// sidebar_width_pt: 6 * 16 * 72 / 144 = 96 * 0.5 = 48.0
assert_eq!(l.sidebar_width_pt(ppi), 48.0);
}
#[test]
fn align_tile_height_rounds_up_to_cell_boundary() {
let l = compute_layout(80, 24, 1280, 576, 6);
let ppi = 144.0;
// cell_h = 24px
// 276pt → 276 * 144 / 72 = 552px → 552 / 24 = 23.0 → already aligned → 552px → 276pt
assert_eq!(l.align_tile_height_pt(276.0, ppi), 276.0);
// 277pt → 277 * 144 / 72 = 554px → div_ceil(554, 24) = 24 → 24*24 = 576px → 288pt
assert_eq!(l.align_tile_height_pt(277.0, ppi), 288.0);
}
}