diff options
author | Christian Duerr <contact@christianduerr.com> | 2017-12-09 17:07:07 +0100 |
---|---|---|
committer | Joe Wilm <jwilm@users.noreply.github.com> | 2017-12-24 09:46:54 -0800 |
commit | 8195d7103498043270523bd81d703cb678cd11a2 (patch) | |
tree | 8af420bc50c79156e07b1002b9ec4f740c960539 /font/src/lib.rs | |
parent | ce8bd1aaf2ba8c5a931b8667b354f90ddf50a1a6 (diff) | |
download | alacritty-8195d7103498043270523bd81d703cb678cd11a2.tar.gz alacritty-8195d7103498043270523bd81d703cb678cd11a2.zip |
Format cursor code and add documentation
As requested a few comments have been added to the darwin code. There
also was an off by one error in the ascent calculation which has been
corrected.
The beam cursor width has also been tweaked to be slightly slimmer in
general.
All code added in this PR has also been run through the default rustfmt
to make sure the formatting is okay.
Diffstat (limited to 'font/src/lib.rs')
-rw-r--r-- | font/src/lib.rs | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/font/src/lib.rs b/font/src/lib.rs index a2580fc8..f9eaaac6 100644 --- a/font/src/lib.rs +++ b/font/src/lib.rs @@ -43,7 +43,7 @@ extern crate foreign_types; extern crate log; use std::hash::{Hash, Hasher}; -use std::fmt; +use std::{fmt, cmp}; use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; // If target isn't macos, reexport everything from ft @@ -65,6 +65,8 @@ pub const UNDERLINE_CURSOR_CHAR: char = '\u{10a3e2}'; /// Character used for the beam cursor // This is part of the private use area and should not conflict with any font pub const BEAM_CURSOR_CHAR: char = '\u{10a3e3}'; +/// Width of the beam cursor relative to the font width +pub const BEAM_CURSOR_WIDTH_PERCENTAGE: i32 = 15; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct FontDesc { @@ -211,11 +213,10 @@ impl Default for RasterizedGlyph { } // Returns a custom underline cursor character -// TODO: Make sure this works with positive/0 descent -> small fonts pub fn get_underline_cursor_glyph(descent: i32, width: i32) -> Result<RasterizedGlyph, Error> { // Create a new rectangle, the height is half the distance between // bounding box bottom and the baseline - let height = i32::abs(descent / 2); + let height = cmp::max(i32::abs(descent / 2), 1); let buf = vec![255u8; (width * height * 3) as usize]; // Create a custom glyph with the rectangle data attached to it @@ -230,10 +231,13 @@ pub fn get_underline_cursor_glyph(descent: i32, width: i32) -> Result<Rasterized } // Returns a custom beam cursor character -// TODO: Make sure this works with positive/0 descent -> small fonts -pub fn get_beam_cursor_glyph(ascent: i32, height: i32, width: i32) -> Result<RasterizedGlyph, Error> { - // Create a new rectangle - let beam_width = (f64::from(width) / 5.) as i32; +pub fn get_beam_cursor_glyph( + ascent: i32, + height: i32, + width: i32, +) -> Result<RasterizedGlyph, Error> { + // Create a new rectangle that is at least one pixel wide + let beam_width = cmp::max(width * BEAM_CURSOR_WIDTH_PERCENTAGE / 100, 1); let buf = vec![255u8; (beam_width * height * 3) as usize]; // Create a custom glyph with the rectangle data attached to it |