Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

2.9 KB
Raw Blame
use crate::parser::ParsedFont;
use config::FontRasterizerSelection;
use image::{ImageBuffer, Rgba};

/// The amount, as a number in [0,1], to horizontally skew a glyph when rendering synthetic
/// italics
pub(crate) const FAKE_ITALIC_SKEW: f64 = 0.2;

pub mod colr;
pub mod freetype;
pub mod harfbuzz;

pub use thinkterm_font_core::{FontRasterizer, RasterizedGlyph};

pub fn new_rasterizer(
    rasterizer: FontRasterizerSelection,
    handle: &ParsedFont,
    pixel_geometry: config::DisplayPixelGeometry,
) -> anyhow::Result<Box<dyn FontRasterizer>> {
    match rasterizer {
        FontRasterizerSelection::FreeType => Ok(Box::new(
            freetype::FreeTypeRasterizer::from_locator(handle, pixel_geometry)?,
        )),
        FontRasterizerSelection::Harfbuzz => Ok(Box::new(
            harfbuzz::HarfbuzzRasterizer::from_locator(handle)?,
        )),
    }
}

pub(crate) fn swap_red_and_blue<Container: std::ops::Deref<Target = [u8]> + std::ops::DerefMut>(
    image: &mut ImageBuffer<Rgba<u8>, Container>,
) {
    for pixel in image.pixels_mut() {
        let red = pixel[0];
        pixel[0] = pixel[2];
        pixel[2] = red;
    }
}

pub(crate) fn crop_to_non_transparent<'a, Container>(
    image: &'a mut image::ImageBuffer<Rgba<u8>, Container>,
) -> image::SubImage<&'a mut ImageBuffer<Rgba<u8>, Container>>
where
    Container: std::ops::Deref<Target = [u8]>,
{
    let width = image.width();
    let height = image.height();

    let mut first_line = None;
    let mut first_col = None;
    let mut last_col = None;
    let mut last_line = None;

    for (y, row) in image.rows().enumerate() {
        for (x, pixel) in row.enumerate() {
            let alpha = pixel[3];
            if alpha != 0 {
                if first_line.is_none() {
                    first_line = Some(y);
                }
                first_col = match first_col.take() {
                    Some(other) if x < other => Some(x),
                    Some(other) => Some(other),
                    None => Some(x),
                };
            }
        }
    }
    for (y, row) in image.rows().enumerate().rev() {
        for (x, pixel) in row.enumerate().rev() {
            let alpha = pixel[3];
            if alpha != 0 {
                if last_line.is_none() {
                    last_line = Some(y);
                }
                last_col = match last_col.take() {
                    Some(other) if x > other => Some(x),
                    Some(other) => Some(other),
                    None => Some(x),
                };
            }
        }
    }

    let first_col = first_col.unwrap_or(0) as u32;
    let first_line = first_line.unwrap_or(0) as u32;
    let last_col = last_col.unwrap_or(width as usize) as u32;
    let last_line = last_line.unwrap_or(height as usize) as u32;

    image::imageops::crop(
        image,
        first_col,
        first_line,
        last_col - first_col,
        last_line - first_line,
    )
}