mirror of
https://github.com/wez/wezterm.git
synced 2024-11-24 07:46:59 +03:00
fonts: cut raster and shaper over to new stuff
This commit is contained in:
parent
d4f0eb40f7
commit
6fdb693f2c
@ -44,6 +44,31 @@ use term::CellAttributes;
|
||||
pub struct LoadedFont {
|
||||
rasterizers: Vec<Box<dyn FontRasterizer>>,
|
||||
shaper: Box<dyn FontShaper>,
|
||||
metrics: FontMetrics,
|
||||
font_size: f64,
|
||||
dpi: u32,
|
||||
}
|
||||
|
||||
impl LoadedFont {
|
||||
pub fn metrics(&self) -> FontMetrics {
|
||||
self.metrics
|
||||
}
|
||||
|
||||
pub fn shape(&self, text: &str) -> Fallible<Vec<GlyphInfo>> {
|
||||
self.shaper.shape(text, self.font_size, self.dpi)
|
||||
}
|
||||
|
||||
pub fn rasterize_glyph(
|
||||
&self,
|
||||
glyph_pos: u32,
|
||||
fallback: FallbackIdx,
|
||||
) -> Fallible<RasterizedGlyph> {
|
||||
let rasterizer = self
|
||||
.rasterizers
|
||||
.get(fallback)
|
||||
.ok_or_else(|| format_err!("no such fallback index: {}", fallback))?;
|
||||
rasterizer.rasterize_glyph(glyph_pos, self.font_size, self.dpi)
|
||||
}
|
||||
}
|
||||
|
||||
type FontPtr = Rc<RefCell<Box<dyn NamedFont>>>;
|
||||
@ -192,9 +217,17 @@ impl FontConfiguration {
|
||||
}
|
||||
let shaper = FontShaperSelection::get_default().new_shaper(&handles)?;
|
||||
|
||||
let config = configuration();
|
||||
let font_size = config.font_size * *self.font_scale.borrow();
|
||||
let dpi = config.dpi as u32;
|
||||
let metrics = shaper.metrics(font_size, dpi)?;
|
||||
|
||||
let loaded = Rc::new(LoadedFont {
|
||||
rasterizers,
|
||||
shaper,
|
||||
metrics,
|
||||
font_size,
|
||||
dpi,
|
||||
});
|
||||
|
||||
fonts.insert(style.clone(), Rc::clone(&loaded));
|
||||
|
@ -8,9 +8,9 @@ use std::mem;
|
||||
use std::slice;
|
||||
|
||||
pub struct FreeTypeRasterizer {
|
||||
face: RefCell<ftwrap::Face>,
|
||||
lib: ftwrap::Library,
|
||||
has_color: bool,
|
||||
face: RefCell<ftwrap::Face>,
|
||||
_lib: ftwrap::Library,
|
||||
}
|
||||
|
||||
impl FontRasterizer for FreeTypeRasterizer {
|
||||
@ -272,7 +272,7 @@ impl FreeTypeRasterizer {
|
||||
(((*face.face).face_flags as u32) & (ftwrap::FT_FACE_FLAG_COLOR as u32)) != 0
|
||||
};
|
||||
Ok(Self {
|
||||
lib,
|
||||
_lib: lib,
|
||||
face: RefCell::new(face),
|
||||
has_color,
|
||||
})
|
||||
|
@ -14,7 +14,7 @@ struct FontPair {
|
||||
|
||||
pub struct HarfbuzzShaper {
|
||||
fonts: Vec<RefCell<FontPair>>,
|
||||
lib: ftwrap::Library,
|
||||
_lib: ftwrap::Library,
|
||||
}
|
||||
|
||||
impl HarfbuzzShaper {
|
||||
@ -29,7 +29,7 @@ impl HarfbuzzShaper {
|
||||
font.set_load_flags(load_flags);
|
||||
fonts.push(RefCell::new(FontPair { face, font }));
|
||||
}
|
||||
Ok(Self { fonts, lib })
|
||||
Ok(Self { fonts, _lib: lib })
|
||||
}
|
||||
|
||||
fn do_shape(
|
||||
|
@ -103,22 +103,13 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
fn load_glyph(&mut self, info: &GlyphInfo, style: &TextStyle) -> Fallible<Rc<CachedGlyph<T>>> {
|
||||
let metrics;
|
||||
let glyph;
|
||||
let has_color;
|
||||
|
||||
let (cell_width, cell_height) = {
|
||||
let font = self.fonts.cached_font(style)?;
|
||||
let mut font = font.borrow_mut();
|
||||
metrics = font
|
||||
.get_fallback(0)
|
||||
.map_err(|e| e.context(format!("glyph {:?}", info)))?
|
||||
.metrics();
|
||||
let active_font = font
|
||||
.get_fallback(info.font_idx)
|
||||
.map_err(|e| e.context(format!("glyph {:?}", info)))?;
|
||||
has_color = active_font.has_color();
|
||||
glyph = active_font.rasterize_glyph(info.glyph_pos)?;
|
||||
(metrics.cell_width, metrics.cell_height)
|
||||
};
|
||||
{
|
||||
let font = self.fonts.resolve_font(style)?;
|
||||
metrics = font.metrics();
|
||||
glyph = font.rasterize_glyph(info.glyph_pos, info.font_idx)?;
|
||||
}
|
||||
let (cell_width, cell_height) = (metrics.cell_width, metrics.cell_height);
|
||||
|
||||
let scale = if (info.x_advance / f64::from(info.num_cells)).floor() > cell_width {
|
||||
f64::from(info.num_cells) * (cell_width / info.x_advance)
|
||||
@ -130,7 +121,7 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
let glyph = if glyph.width == 0 || glyph.height == 0 {
|
||||
// a whitespace glyph
|
||||
CachedGlyph {
|
||||
has_color,
|
||||
has_color: glyph.has_color,
|
||||
texture: None,
|
||||
x_offset: info.x_offset * scale,
|
||||
y_offset: info.y_offset * scale,
|
||||
@ -160,7 +151,7 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
let tex = self.atlas.allocate(&raw_im)?;
|
||||
|
||||
CachedGlyph {
|
||||
has_color,
|
||||
has_color: glyph.has_color,
|
||||
texture: Some(tex),
|
||||
x_offset,
|
||||
y_offset,
|
||||
|
@ -1250,8 +1250,7 @@ impl TermWindow {
|
||||
|
||||
// Shape the printable text from this cluster
|
||||
let glyph_info = {
|
||||
let font = self.fonts.cached_font(style)?;
|
||||
let mut font = font.borrow_mut();
|
||||
let font = self.fonts.resolve_font(style)?;
|
||||
font.shape(&cluster.text)?
|
||||
};
|
||||
|
||||
@ -1478,8 +1477,7 @@ impl TermWindow {
|
||||
|
||||
// Shape the printable text from this cluster
|
||||
let glyph_info = {
|
||||
let font = self.fonts.cached_font(style)?;
|
||||
let mut font = font.borrow_mut();
|
||||
let font = self.fonts.resolve_font(style)?;
|
||||
font.shape(&cluster.text)?
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user