diff options
author | Christian Duerr <chrisduerr@users.noreply.github.com> | 2019-04-19 18:00:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-19 18:00:24 +0000 |
commit | cfc20d4f34dca535654cc32df18e785296af4cc5 (patch) | |
tree | b9697a365d1ec4b3161fd67937497e90a63f5c0d /src/cursor.rs | |
parent | a47d716daace7c197392854754406a06454ec380 (diff) | |
download | r-alacritty-cfc20d4f34dca535654cc32df18e785296af4cc5.tar.gz r-alacritty-cfc20d4f34dca535654cc32df18e785296af4cc5.tar.bz2 r-alacritty-cfc20d4f34dca535654cc32df18e785296af4cc5.zip |
Fix cursor dimensions with font offset
Previously cursor dimensions were not calculated correctly when a font
offset was specified, since the font offset was completely ignored.
This has been fixed by moving all the cursor logic from the font into
the Alacritty crate, applying the config's offsets before rasterizing
the cursors.
This has also fixed an issue with some cursors not being rendered as
double-width correctly when over double-width glyphs.
This fixes #2209.
Diffstat (limited to 'src/cursor.rs')
-rw-r--r-- | src/cursor.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/cursor.rs b/src/cursor.rs new file mode 100644 index 00000000..268f35fa --- /dev/null +++ b/src/cursor.rs @@ -0,0 +1,98 @@ +// Copyright 2016 Joe Wilm, The Alacritty Project Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Helpers for creating different cursor glyphs from font metrics + +use std::cmp; + +use font::{Metrics, RasterizedGlyph}; + +use crate::ansi::CursorStyle; + +/// Width/Height of the cursor relative to the font width +pub const CURSOR_WIDTH_PERCENTAGE: i32 = 15; + +pub fn get_cursor_glyph( + cursor: CursorStyle, + metrics: Metrics, + offset_x: i8, + offset_y: i8, + is_wide: bool, +) -> RasterizedGlyph { + // Calculate the cell metrics + let height = metrics.line_height as i32 + i32::from(offset_y); + let mut width = metrics.average_advance as i32 + i32::from(offset_x); + let line_width = cmp::max(width * CURSOR_WIDTH_PERCENTAGE / 100, 1); + + // 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), + } +} + +// Returns a custom underline cursor character +pub fn get_underline_cursor_glyph(width: i32, line_width: i32) -> RasterizedGlyph { + // Create a new rectangle, the height is relative to the font width + let buf = vec![255u8; (width * line_width * 3) as usize]; + + // Create a custom glyph with the rectangle data attached to it + RasterizedGlyph { c: ' ', top: line_width, left: 0, height: line_width, width, buf } +} + +// Returns a custom beam cursor character +pub fn get_beam_cursor_glyph(height: i32, line_width: i32) -> RasterizedGlyph { + // Create a new rectangle that is at least one pixel wide + let buf = vec![255u8; (line_width * height * 3) as usize]; + + // Create a custom glyph with the rectangle data attached to it + RasterizedGlyph { c: ' ', top: height, left: 0, height, width: line_width, buf } +} + +// Returns a custom box cursor character +pub fn get_box_cursor_glyph(height: i32, width: i32, line_width: i32) -> RasterizedGlyph { + // Create a new box outline rectangle + let mut buf = Vec::with_capacity((width * height * 3) as usize); + 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, left: 0, height, width, buf } +} + +// Returns a custom block cursor character +pub fn get_block_cursor_glyph(height: i32, width: i32) -> RasterizedGlyph { + // Create a completely filled glyph + let buf = vec![255u8; (width * height * 3) as usize]; + + // Create a custom glyph with the rectangle data attached to it + RasterizedGlyph { c: ' ', top: height, left: 0, height, width, buf } +} |