1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-24 07:46:59 +03:00

font: adjust interfaces to pass size information down

This commit is contained in:
Wez Furlong 2019-12-09 08:33:11 -08:00
parent a05612960e
commit accfc9b09c
4 changed files with 18 additions and 59 deletions

View File

@ -8,15 +8,13 @@ use std::slice;
pub struct FreeTypeRasterizer {
face: RefCell<ftwrap::Face>,
/// nominal monospace cell height
cell_height: f64,
/// nominal monospace cell width
cell_width: f64,
has_color: bool,
}
impl FontRasterizer for FreeTypeRasterizer {
fn rasterize_glyph(&self, glyph_pos: u32) -> Fallible<RasterizedGlyph> {
fn rasterize_glyph(&self, glyph_pos: u32, size: f64, dpi: u32) -> Fallible<RasterizedGlyph> {
self.face.borrow_mut().set_font_size(size, dpi)?;
let render_mode = //ftwrap::FT_Render_Mode::FT_RENDER_MODE_NORMAL;
// ftwrap::FT_Render_Mode::FT_RENDER_MODE_LCD;
ftwrap::FT_Render_Mode::FT_RENDER_MODE_LIGHT;
@ -271,46 +269,7 @@ impl FreeTypeRasterizer {
}
pub fn with_face_size_and_dpi(mut face: ftwrap::Face, size: f64, dpi: u32) -> Fallible<Self> {
log::debug!("set_char_size {} dpi={}", size, dpi);
// Scaling before truncating to integer minimizes the chances of hitting
// the fallback code for set_pixel_sizes below.
let size = (size * 64.0) as ftwrap::FT_F26Dot6;
let (cell_width, cell_height) = match face.set_char_size(size, size, dpi, dpi) {
Ok(_) => {
// Compute metrics for the nominal monospace cell
face.cell_metrics()
}
Err(err) => {
let sizes = unsafe {
let rec = &(*face.face);
slice::from_raw_parts(rec.available_sizes, rec.num_fixed_sizes as usize)
};
if sizes.is_empty() {
return Err(err);
}
// Find the best matching size.
// We just take the biggest.
let mut best = 0;
let mut best_size = 0;
let mut cell_width = 0;
let mut cell_height = 0;
for (idx, info) in sizes.iter().enumerate() {
let size = best_size.max(info.height);
if size > best_size {
best = idx;
best_size = size;
cell_width = info.width;
cell_height = info.height;
}
}
face.select_size(best)?;
(f64::from(cell_width), f64::from(cell_height))
}
};
log::debug!("metrics: width={} height={}", cell_width, cell_height);
face.set_font_size(size, dpi)?;
let has_color = unsafe {
(((*face.face).face_flags as u32) & (ftwrap::FT_FACE_FLAG_COLOR as u32)) != 0
@ -318,8 +277,6 @@ impl FreeTypeRasterizer {
Ok(Self {
face: RefCell::new(face),
cell_height,
cell_width,
has_color,
})
}

View File

@ -6,5 +6,5 @@ pub mod freetype;
/// Rasterizes the specified glyph index in the associated font
/// and returns the generated bitmap
pub trait FontRasterizer {
fn rasterize_glyph(&self, glyph_pos: u32) -> Fallible<RasterizedGlyph>;
fn rasterize_glyph(&self, glyph_pos: u32, size: f64, dpi: u32) -> Fallible<RasterizedGlyph>;
}

View File

@ -20,6 +20,8 @@ impl HarfbuzzShaper {
&self,
font_idx: crate::font::system::FallbackIdx,
s: &str,
font_size: f64,
dpi: u32,
) -> Fallible<Vec<GlyphInfo>> {
let features = vec![
// kerning
@ -39,9 +41,9 @@ impl HarfbuzzShaper {
{
match self.fonts.get(font_idx) {
Some(pair) => {
pair.borrow_mut()
.font
.shape(&mut buf, Some(features.as_slice()));
let mut pair = pair.borrow_mut();
pair.face.set_font_size(font_size, dpi)?;
pair.font.shape(&mut buf, Some(features.as_slice()));
}
None => {
let chars: Vec<u32> = s.chars().map(|c| c as u32).collect();
@ -109,14 +111,14 @@ impl HarfbuzzShaper {
//debug!("range: {:?}-{:?} needs fallback", start, pos);
let substr = &s[start_pos..pos];
let mut shape = match self.do_shape(font_idx + 1, substr) {
let mut shape = match self.do_shape(font_idx + 1, substr, font_size, dpi) {
Ok(shape) => Ok(shape),
Err(e) => {
error!("{:?} for {:?}", e, substr);
if font_idx == 0 && s == "?" {
bail!("unable to find any usable glyphs for `?` in font_idx 0");
}
self.do_shape(0, "?")
self.do_shape(0, "?", font_size, dpi)
}
}?;
@ -134,7 +136,7 @@ impl HarfbuzzShaper {
//debug!("glyph from `{}`", text);
cluster.push(GlyphInfo::new(text, font_idx, info, &positions[i]));
} else {
cluster.append(&mut self.do_shape(0, "?")?);
cluster.append(&mut self.do_shape(0, "?", font_size, dpi)?);
}
}
}
@ -151,14 +153,14 @@ impl HarfbuzzShaper {
substr,
);
}
let mut shape = match self.do_shape(font_idx + 1, substr) {
let mut shape = match self.do_shape(font_idx + 1, substr, font_size, dpi) {
Ok(shape) => Ok(shape),
Err(e) => {
error!("{:?} for {:?}", e, substr);
if font_idx == 0 && s == "?" {
bail!("unable to find any usable glyphs for `?` in font_idx 0");
}
self.do_shape(0, "?")
self.do_shape(0, "?", font_size, dpi)
}
}?;
// Fixup the cluster member to match our current offset
@ -175,7 +177,7 @@ impl HarfbuzzShaper {
}
impl FontShaper for HarfbuzzShaper {
fn shape(&self, text: &str) -> Fallible<Vec<GlyphInfo>> {
self.do_shape(0, text)
fn shape(&self, text: &str, size: f64, dpi: u32) -> Fallible<Vec<GlyphInfo>> {
self.do_shape(0, text, size, dpi)
}
}

View File

@ -5,5 +5,5 @@ pub mod harfbuzz;
pub trait FontShaper {
/// Shape text and return a vector of GlyphInfo
fn shape(&self, text: &str) -> Fallible<Vec<GlyphInfo>>;
fn shape(&self, text: &str, size: f64, dpi: u32) -> Fallible<Vec<GlyphInfo>>;
}