Skip to content

Commit

Permalink
Account for font.offset and glyph.offset in built-in font
Browse files Browse the repository at this point in the history
This commit takes into account `font.offset` and `font.glyph_offset`
when generating built-in font.
  • Loading branch information
kchibisov authored Jan 29, 2022
1 parent 094c2c9 commit f6651a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- OSC 4 not handling `?`
- `?` in OSC strings reporting default colors instead of modified ones
- OSC 104 not clearing colors when second parameter is empty
- Builtin font lines not contiguous when `font.offset` is used
- `font.glyph_offset` is no longer applied on builtin font

## 0.10.0

Expand Down
37 changes: 27 additions & 10 deletions alacritty/src/renderer/builtin_font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::{cmp, mem};

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

use crate::config::ui_config::Delta;

// Colors which are used for filling shade variants.
const COLOR_FILL_ALPHA_STEP_1: Pixel = Pixel { _r: 192, _g: 192, _b: 192 };
const COLOR_FILL_ALPHA_STEP_2: Pixel = Pixel { _r: 128, _g: 128, _b: 128 };
Expand All @@ -14,17 +16,29 @@ const COLOR_FILL_ALPHA_STEP_3: Pixel = Pixel { _r: 64, _g: 64, _b: 64 };
const COLOR_FILL: Pixel = Pixel { _r: 255, _g: 255, _b: 255 };

/// Returns the rasterized glyph if the character is part of the built-in font.
pub fn builtin_glyph(character: char, metrics: &Metrics) -> Option<RasterizedGlyph> {
match character {
pub fn builtin_glyph(
character: char,
metrics: &Metrics,
offset: &Delta<i8>,
glyph_offset: &Delta<i8>,
) -> Option<RasterizedGlyph> {
let mut glyph = match character {
// Box drawing characters and block elements.
'\u{2500}'..='\u{259f}' => Some(box_drawing(character, metrics)),
_ => None,
}
'\u{2500}'..='\u{259f}' => box_drawing(character, metrics, offset),
_ => return None,
};

// Since we want to ignore `glyph_offset` for the built-in font, subtract it to compensate its
// addition when loading glyphs in the renderer.
glyph.left -= glyph_offset.x as i32;
glyph.top -= glyph_offset.y as i32;

Some(glyph)
}

fn box_drawing(character: char, metrics: &Metrics) -> RasterizedGlyph {
let height = metrics.line_height as usize;
let width = metrics.average_advance as usize;
fn box_drawing(character: char, metrics: &Metrics, offset: &Delta<i8>) -> RasterizedGlyph {
let height = (metrics.line_height as i32 + offset.y as i32) as usize;
let width = (metrics.average_advance as i32 + offset.x as i32) as usize;
let stroke_size = cmp::max(metrics.underline_thickness as usize, 1);
let heavy_stroke_size = stroke_size * 3;
// Certain symbols require larger canvas than the cell itself, since for proper contiguous
Expand Down Expand Up @@ -741,13 +755,16 @@ mod test {
strikeout_thickness: 2.,
};

let offset = Default::default();
let glyph_offset = Default::default();

// Test coverage of box drawing characters.
for character in '\u{2500}'..='\u{259f}' {
assert!(builtin_glyph(character, &metrics).is_some());
assert!(builtin_glyph(character, &metrics, &offset, &glyph_offset).is_some());
}

for character in ('\u{2450}'..'\u{2500}').chain('\u{25a0}'..'\u{2600}') {
assert!(builtin_glyph(character, &metrics).is_none());
assert!(builtin_glyph(character, &metrics, &offset, &glyph_offset).is_none());
}
}
}
14 changes: 13 additions & 1 deletion alacritty/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ pub struct GlyphCache {
/// Font size.
font_size: crossfont::Size,

/// Font offset.
font_offset: Delta<i8>,

/// Glyph offset.
glyph_offset: Delta<i8>,

Expand Down Expand Up @@ -168,6 +171,7 @@ impl GlyphCache {
bold_key: bold,
italic_key: italic,
bold_italic_key: bold_italic,
font_offset: font.offset,
glyph_offset: font.glyph_offset,
metrics,
builtin_box_drawing: font.builtin_box_drawing,
Expand Down Expand Up @@ -274,7 +278,14 @@ impl GlyphCache {
// for everything else.
let rasterized = self
.builtin_box_drawing
.then(|| builtin_font::builtin_glyph(glyph_key.character, &self.metrics))
.then(|| {
builtin_font::builtin_glyph(
glyph_key.character,
&self.metrics,
&self.font_offset,
&self.glyph_offset,
)
})
.flatten()
.map_or_else(|| self.rasterizer.get_glyph(glyph_key), Ok);

Expand Down Expand Up @@ -342,6 +353,7 @@ impl GlyphCache {
) -> Result<(), crossfont::Error> {
// Update dpi scaling.
self.rasterizer.update_dpr(dpr as f32);
self.font_offset = font.offset;

// Recompute font keys.
let (regular, bold, italic, bold_italic) =
Expand Down

0 comments on commit f6651a9

Please sign in to comment.