summaryrefslogtreecommitdiff
path: root/alacritty/src/cursor.rs
blob: 8c782185da5ae74846a0863d04d24fc02f4d95ca (plain)
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
//! Helpers for creating different cursor glyphs from font metrics.

use crossfont::{BitmapBuffer, Metrics, RasterizedGlyph};

use alacritty_terminal::ansi::CursorStyle;

pub fn get_cursor_glyph(
    cursor: CursorStyle,
    metrics: Metrics,
    offset_x: i8,
    offset_y: i8,
    is_wide: bool,
    cursor_thickness: f64,
) -> RasterizedGlyph {
    // Calculate the cell metrics.
    //
    // NOTE: With Rust 1.47+ `f64 as usize` is defined to clamp automatically:
    // https://github.com/rust-lang/rust/commit/14d608f1d8a0b84da5f3bccecb3efb3d35f980dc
    let height = (metrics.line_height + f64::from(offset_y)).max(1.) as usize;
    let mut width = (metrics.average_advance + f64::from(offset_x)).max(1.) as usize;
    let line_width = (cursor_thickness * width as f64).round().max(1.) as usize;

    // Double the cursor width if it's above a double-width glyph.
    if is_wide {
        width *= 2;
    }

    match cursor {
        CursorStyle::HollowBlock => get_box_cursor_glyph(height, width, line_width),
        CursorStyle::Underline => get_underline_cursor_glyph(width, line_width),
        CursorStyle::Beam => get_beam_cursor_glyph(height, line_width),
        CursorStyle::Block => get_block_cursor_glyph(height, width),
        CursorStyle::Hidden => RasterizedGlyph::default(),
    }
}

/// Return a custom underline cursor character.
pub fn get_underline_cursor_glyph(width: usize, line_width: usize) -> RasterizedGlyph {
    // Create a new rectangle, the height is relative to the font width.
    let buf = vec![255u8; width * line_width * 3];

    // Create a custom glyph with the rectangle data attached to it.
    RasterizedGlyph {
        c: ' ',
        top: line_width as i32,
        left: 0,
        height: line_width as i32,
        width: width as i32,
        buf: BitmapBuffer::RGB(buf),
    }
}

/// Return a custom beam cursor character.
pub fn get_beam_cursor_glyph(height: usize, line_width: usize) -> RasterizedGlyph {
    // Create a new rectangle that is at least one pixel wide
    let buf = vec![255u8; line_width * height * 3];

    // Create a custom glyph with the rectangle data attached to it
    RasterizedGlyph {
        c: ' ',
        top: height as i32,
        left: 0,
        height: height as i32,
        width: line_width as i32,
        buf: BitmapBuffer::RGB(buf),
    }
}

/// Returns a custom box cursor character.
pub fn get_box_cursor_glyph(height: usize, width: usize, line_width: usize) -> RasterizedGlyph {
    // Create a new box outline rectangle.
    let mut buf = Vec::with_capacity(width * height * 3);
    for y in 0..height {
        for x in 0..width {
            if y < line_width
                || y >= height - line_width
                || x < line_width
                || x >= width - line_width
            {
                buf.append(&mut vec![255u8; 3]);
            } else {
                buf.append(&mut vec![0u8; 3]);
            }
        }
    }

    // Create a custom glyph with the rectangle data attached to it.
    RasterizedGlyph {
        c: ' ',
        top: height as i32,
        left: 0,
        height: height as i32,
        width: width as i32,
        buf: BitmapBuffer::RGB(buf),
    }
}

/// Return a custom block cursor character.
pub fn get_block_cursor_glyph(height: usize, width: usize) -> RasterizedGlyph {
    // Create a completely filled glyph.
    let buf = vec![255u8; width * height * 3];

    // Create a custom glyph with the rectangle data attached to it.
    RasterizedGlyph {
        c: ' ',
        top: height as i32,
        left: 0,
        height: height as i32,
        width: width as i32,
        buf: BitmapBuffer::RGB(buf),
    }
}