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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
use std::collections::HashMap;
use std::mem;
use crossfont::Metrics;
use alacritty_terminal::grid::Dimensions;
use alacritty_terminal::index::{Column, Point};
use alacritty_terminal::term::cell::Flags;
use alacritty_terminal::term::color::Rgb;
use alacritty_terminal::term::SizeInfo;
use crate::display::content::RenderableCell;
use crate::gl;
use crate::gl::types::*;
use crate::renderer;
#[derive(Debug, Copy, Clone)]
pub struct RenderRect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
pub color: Rgb,
pub alpha: f32,
}
impl RenderRect {
pub fn new(x: f32, y: f32, width: f32, height: f32, color: Rgb, alpha: f32) -> Self {
RenderRect { x, y, width, height, color, alpha }
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct RenderLine {
pub start: Point<usize>,
pub end: Point<usize>,
pub color: Rgb,
}
impl RenderLine {
pub fn rects(&self, flag: Flags, metrics: &Metrics, size: &SizeInfo) -> Vec<RenderRect> {
let mut rects = Vec::new();
let mut start = self.start;
while start.line < self.end.line {
let end = Point::new(start.line, size.last_column());
Self::push_rects(&mut rects, metrics, size, flag, start, end, self.color);
start = Point::new(start.line + 1, Column(0));
}
Self::push_rects(&mut rects, metrics, size, flag, start, self.end, self.color);
rects
}
/// Push all rects required to draw the cell's line.
fn push_rects(
rects: &mut Vec<RenderRect>,
metrics: &Metrics,
size: &SizeInfo,
flag: Flags,
start: Point<usize>,
end: Point<usize>,
color: Rgb,
) {
match flag {
Flags::UNDERCURL => {
Self::push_undercurl_rects(
rects,
size,
metrics.descent,
start,
end,
metrics.underline_position + 1.,
metrics.underline_thickness,
color)
}
Flags::DOUBLE_UNDERLINE => {
// Position underlines so each one has 50% of descent available.
let top_pos = 0.25 * metrics.descent;
let bottom_pos = 0.75 * metrics.descent;
rects.push(Self::create_rect(
size,
metrics.descent,
start,
end,
top_pos,
metrics.underline_thickness,
color,
));
rects.push(Self::create_rect(
size,
metrics.descent,
start,
end,
bottom_pos,
metrics.underline_thickness,
color,
));
},
Flags::UNDERLINE => {
rects.push(Self::create_rect(
size,
metrics.descent,
start,
end,
metrics.underline_position + 1.,
metrics.underline_thickness,
color,
));
},
Flags::OVERLINE => {
let start_x = start.column.0 as f32 * size.cell_width();
let end_x = (end.column.0 + 1) as f32 * size.cell_width();
rects.push(RenderRect::new(
start_x + size.padding_x(),
(start.line as f32 * size.cell_height()) + (size.cell_height() / 8.),
end_x - start_x,
metrics.underline_thickness,
color,
1.,
));
},
Flags::STRIKEOUT => {
rects.push(Self::create_rect(
size,
metrics.descent,
start,
end,
metrics.strikeout_position,
metrics.strikeout_thickness,
color,
));
},
Flags::DOTTED_UNDERLINE => {
Self::push_dotted_underline_rects(
rects,
size,
metrics.descent,
start,
end,
metrics.underline_position + 1.,
metrics.underline_thickness,
color)
}
_ => unimplemented!("Invalid flag for cell line drawing specified"),
};
}
fn push_undercurl_rects(
rects: &mut Vec<RenderRect>,
size: &SizeInfo,
descent: f32,
start: Point<usize>,
end: Point<usize>,
position: f32,
mut thickness: f32,
color: Rgb,
) {
let start_x = start.column.0 as f32 * size.cell_width();
let end_x = (end.column.0 + 1) as f32 * size.cell_width();
// Make sure lines are always visible.
thickness = thickness.max(1.);
let line_bottom = (start.line as f32 + 1.) * size.cell_height();
let baseline = line_bottom + descent;
let mut y = (baseline - position - thickness / 2.).ceil();
let max_y = line_bottom - thickness;
if y > max_y {
y = max_y;
}
let period_div = 1.5 * (2. * std::f32::consts::PI) / size.cell_width();
let amplitude_mul = size.cell_height() / 10.;
let mut x = start_x;
let mut idx = 0;
while x < end_x {
let altr = [1., 0., -1., 0.][idx % 4];
let fl = (x * period_div).sin() * amplitude_mul;
let al = (fl.round() - fl).abs();
rects.push(RenderRect::new(
x + size.padding_x(),
y + size.padding_y() + fl,
1.,
thickness,
color,
(1. - al).powf(2.),
));
let fl = (x * period_div).sin() * amplitude_mul - 0.5;
let al = (fl.round() - fl).abs();
rects.push(RenderRect::new(
x + size.padding_x(),
y + size.padding_y() + fl,
1.,
thickness,
color,
(1. - al).powf(2.),
));
let fl = (x * period_div).sin() * amplitude_mul + 0.5;
let al = (fl.round() - fl).abs();
rects.push(RenderRect::new(
x + size.padding_x(),
y + size.padding_y() + fl,
1.,
thickness,
color,
(1. - al).powf(2.),
));
x += 1.;
idx += 1;
}
}
fn push_dotted_underline_rects(
rects: &mut Vec<RenderRect>,
size: &SizeInfo,
descent: f32,
start: Point<usize>,
end: Point<usize>,
position: f32,
mut thickness: f32,
color: Rgb,
) {
let start_x = start.column.0 as f32 * size.cell_width();
let end_x = (end.column.0 + 1) as f32 * size.cell_width();
// Make sure lines are always visible.
thickness = thickness.max(1.);
let line_bottom = (start.line as f32 + 1.) * size.cell_height();
let baseline = line_bottom + descent;
let mut y = (baseline - position - thickness / 2.).ceil();
let max_y = line_bottom - thickness;
if y > max_y {
y = max_y;
}
let mut x = start_x;
let mut idx = 0;
while x < end_x {
if idx % 4 == 0 {
let rect = RenderRect::new(
x + size.padding_x(),
y + size.padding_y(),
thickness + 1.,
thickness + 1.,
color,
1.,
);
rects.push(rect);
}
x += 1.;
idx += 1;
}
}
/// Create a line's rect at a position relative to the baseline.
fn create_rect(
size: &SizeInfo,
descent: f32,
start: Point<usize>,
end: Point<usize>,
position: f32,
mut thickness: f32,
color: Rgb,
) -> RenderRect {
let start_x = start.column.0 as f32 * size.cell_width();
let end_x = (end.column.0 + 1) as f32 * size.cell_width();
let width = end_x - start_x;
// Make sure lines are always visible.
thickness = thickness.max(1.);
let line_bottom = (start.line as f32 + 1.) * size.cell_height();
let baseline = line_bottom + descent;
let mut y = (baseline - position - thickness / 2.).ceil();
let max_y = line_bottom - thickness;
if y > max_y {
y = max_y;
}
RenderRect::new(
start_x + size.padding_x(),
y + size.padding_y(),
width,
thickness,
color,
1.,
)
}
}
/// Lines for underline and strikeout.
#[derive(Default)]
pub struct RenderLines {
inner: HashMap<Flags, Vec<RenderLine>>,
}
impl RenderLines {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn rects(&self, metrics: &Metrics, size: &SizeInfo) -> Vec<RenderRect> {
self.inner
.iter()
.flat_map(|(flag, lines)| {
lines.iter().flat_map(move |line| line.rects(*flag, metrics, size))
})
.collect()
}
/// Update the stored lines with the next cell info.
#[inline]
pub fn update(&mut self, cell: &RenderableCell) {
self.update_flag(cell, Flags::UNDERLINE);
self.update_flag(cell, Flags::DOUBLE_UNDERLINE);
self.update_flag(cell, Flags::STRIKEOUT);
self.update_flag(cell, Flags::UNDERCURL);
self.update_flag(cell, Flags::OVERLINE);
self.update_flag(cell, Flags::DOTTED_UNDERLINE);
}
/// Update the lines for a specific flag.
fn update_flag(&mut self, cell: &RenderableCell, flag: Flags) {
if !cell.flags.contains(flag) {
return;
}
// Include wide char spacer if the current cell is a wide char.
let mut end = cell.point;
if cell.flags.contains(Flags::WIDE_CHAR) {
end.column += 1;
}
// Check if there's an active line.
if let Some(line) = self.inner.get_mut(&flag).and_then(|lines| lines.last_mut()) {
if cell.fg == line.color
&& cell.point.column == line.end.column + 1
&& cell.point.line == line.end.line
{
// Update the length of the line.
line.end = end;
return;
}
}
// Start new line if there currently is none.
let line = RenderLine { start: cell.point, end, color: cell.sp };
match self.inner.get_mut(&flag) {
Some(lines) => lines.push(line),
None => {
self.inner.insert(flag, vec![line]);
},
}
}
}
/// Shader sources for rect rendering program.
static RECT_SHADER_F: &str = include_str!("../../res/rect.f.glsl");
static RECT_SHADER_V: &str = include_str!("../../res/rect.v.glsl");
#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct Vertex {
// Normalized screen coordinates.
x: f32,
y: f32,
// Color.
r: u8,
g: u8,
b: u8,
a: u8,
}
#[derive(Debug)]
pub struct RectRenderer {
// GL buffer objects.
vao: GLuint,
vbo: GLuint,
program: RectShaderProgram,
vertices: Vec<Vertex>,
}
impl RectRenderer {
pub fn new() -> Result<Self, renderer::Error> {
let mut vao: GLuint = 0;
let mut vbo: GLuint = 0;
let program = RectShaderProgram::new()?;
unsafe {
// Allocate buffers.
gl::GenVertexArrays(1, &mut vao);
gl::GenBuffers(1, &mut vbo);
gl::BindVertexArray(vao);
// VBO binding is not part of VAO itself, but VBO binding is stored in attributes.
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
let mut attribute_offset = 0;
// Position.
gl::VertexAttribPointer(
0,
2,
gl::FLOAT,
gl::FALSE,
mem::size_of::<Vertex>() as i32,
attribute_offset as *const _,
);
gl::EnableVertexAttribArray(0);
attribute_offset += mem::size_of::<f32>() * 2;
// Color.
gl::VertexAttribPointer(
1,
4,
gl::UNSIGNED_BYTE,
gl::TRUE,
mem::size_of::<Vertex>() as i32,
attribute_offset as *const _,
);
gl::EnableVertexAttribArray(1);
// Reset buffer bindings.
gl::BindVertexArray(0);
gl::BindBuffer(gl::ARRAY_BUFFER, 0);
}
Ok(Self { vao, vbo, program, vertices: Vec::new() })
}
pub fn draw(&mut self, size_info: &SizeInfo, rects: Vec<RenderRect>) {
unsafe {
// Bind VAO to enable vertex attribute slots.
gl::BindVertexArray(self.vao);
// Bind VBO only once for buffer data upload only.
gl::BindBuffer(gl::ARRAY_BUFFER, self.vbo);
gl::UseProgram(self.program.id);
}
let half_width = size_info.width() / 2.;
let half_height = size_info.height() / 2.;
// Build rect vertices vector.
self.vertices.clear();
for rect in &rects {
self.add_rect(half_width, half_height, rect);
}
unsafe {
// Upload accumulated vertices.
gl::BufferData(
gl::ARRAY_BUFFER,
(self.vertices.len() * mem::size_of::<Vertex>()) as isize,
self.vertices.as_ptr() as *const _,
gl::STREAM_DRAW,
);
// Draw all vertices as list of triangles.
gl::DrawArrays(gl::TRIANGLES, 0, self.vertices.len() as i32);
// Disable program.
gl::UseProgram(0);
// Reset buffer bindings to nothing.
gl::BindBuffer(gl::ARRAY_BUFFER, 0);
gl::BindVertexArray(0);
}
}
fn add_rect(&mut self, half_width: f32, half_height: f32, rect: &RenderRect) {
// Calculate rectangle vertices positions in normalized device coordinates.
// NDC range from -1 to +1, with Y pointing up.
let x = rect.x / half_width - 1.0;
let y = -rect.y / half_height + 1.0;
let width = rect.width / half_width;
let height = rect.height / half_height;
let Rgb { r, g, b } = rect.color;
let a = (rect.alpha * 255.) as u8;
// Make quad vertices.
let quad = [
Vertex { x, y, r, g, b, a },
Vertex { x, y: y - height, r, g, b, a },
Vertex { x: x + width, y, r, g, b, a },
Vertex { x: x + width, y: y - height, r, g, b, a },
];
// Append the vertices to form two triangles.
self.vertices.push(quad[0]);
self.vertices.push(quad[1]);
self.vertices.push(quad[2]);
self.vertices.push(quad[2]);
self.vertices.push(quad[3]);
self.vertices.push(quad[1]);
}
}
impl Drop for RectRenderer {
fn drop(&mut self) {
unsafe {
gl::DeleteBuffers(1, &self.vbo);
gl::DeleteVertexArrays(1, &self.vao);
}
}
}
/// Rectangle drawing program.
#[derive(Debug)]
pub struct RectShaderProgram {
/// Program id.
id: GLuint,
}
impl RectShaderProgram {
pub fn new() -> Result<Self, renderer::ShaderCreationError> {
let vertex_shader = renderer::create_shader(gl::VERTEX_SHADER, RECT_SHADER_V)?;
let fragment_shader = renderer::create_shader(gl::FRAGMENT_SHADER, RECT_SHADER_F)?;
let program = renderer::create_program(vertex_shader, fragment_shader)?;
unsafe {
gl::DeleteShader(fragment_shader);
gl::DeleteShader(vertex_shader);
gl::UseProgram(program);
}
let shader = Self { id: program };
unsafe { gl::UseProgram(0) }
Ok(shader)
}
}
impl Drop for RectShaderProgram {
fn drop(&mut self) {
unsafe {
gl::DeleteProgram(self.id);
}
}
}
|