Fixed double-skewing behavior
This commit is contained in:
1 parent
de9d9bc588
commit
2f0bcb3c20
2 files changed
+11
-74
No files matched your search
@@ -1,8 +1,8 @@
|
||||
use crate::parser::ParsedFont;
|
||||
use crate::rasterizer::FontRasterizer;
|
||||
use crate::rasterizer::{FontRasterizer, FAKE_ITALIC_SKEW};
|
||||
use crate::units::*;
|
||||
use crate::{ftwrap, RasterizedGlyph};
|
||||
use ::freetype::{FT_GlyphSlotRec_, FT_Glyph_Format, FT_Matrix};
|
||||
use ::freetype::{FT_GlyphSlotRec_, FT_Matrix};
|
||||
use anyhow::bail;
|
||||
use config::{DisplayPixelGeometry, FreeTypeLoadFlags, FreeTypeLoadTarget};
|
||||
use std::cell::RefCell;
|
||||
@@ -14,7 +14,6 @@ pub struct FreeTypeRasterizer {
|
||||
face: RefCell<ftwrap::Face>,
|
||||
_lib: ftwrap::Library,
|
||||
synthesize_bold: bool,
|
||||
synthesize_italic: bool,
|
||||
freetype_load_target: Option<FreeTypeLoadTarget>,
|
||||
freetype_render_target: Option<FreeTypeLoadTarget>,
|
||||
freetype_load_flags: Option<FreeTypeLoadFlags>,
|
||||
@@ -63,13 +62,7 @@ impl FontRasterizer for FreeTypeRasterizer {
|
||||
mode => bail!("unhandled pixel mode: {:?}", mode),
|
||||
};
|
||||
|
||||
let slot = unsafe { &mut *(*face.face).glyph };
|
||||
if self.synthesize_italic && slot.format == FT_Glyph_Format::FT_GLYPH_FORMAT_BITMAP {
|
||||
// The source was and thus the italic transform did nothing
|
||||
Ok(glyph.skew())
|
||||
} else {
|
||||
Ok(glyph)
|
||||
}
|
||||
Ok(glyph)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,10 +307,10 @@ impl FreeTypeRasterizer {
|
||||
|
||||
if parsed.synthesize_italic {
|
||||
face.set_transform(Some(FT_Matrix {
|
||||
xx: 1 * 65536, // scale x
|
||||
yy: 1 * 65536, // scale y
|
||||
xy: (0.2 * 65536.0) as _, // skew x
|
||||
yx: 0 * 65536, // skey y
|
||||
xx: 1 * 65536, // scale x
|
||||
yy: 1 * 65536, // scale y
|
||||
xy: (FAKE_ITALIC_SKEW * 65536.0) as _, // skew x
|
||||
yx: 0 * 65536, // skew y
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -326,7 +319,6 @@ impl FreeTypeRasterizer {
|
||||
face: RefCell::new(face),
|
||||
has_color,
|
||||
synthesize_bold: parsed.synthesize_bold,
|
||||
synthesize_italic: parsed.synthesize_italic,
|
||||
freetype_load_flags: parsed.freetype_load_flags,
|
||||
freetype_load_target: parsed.freetype_load_target,
|
||||
freetype_render_target: parsed.freetype_render_target,
|
||||
|
||||
@@ -2,6 +2,10 @@ use crate::parser::ParsedFont;
|
||||
use crate::units::*;
|
||||
use config::FontRasterizerSelection;
|
||||
|
||||
/// 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 freetype;
|
||||
|
||||
/// A bitmap representation of a glyph.
|
||||
@@ -16,65 +20,6 @@ pub struct RasterizedGlyph {
|
||||
pub has_color: bool,
|
||||
}
|
||||
|
||||
impl RasterizedGlyph {
|
||||
/// Computes a skewed version of this glyph to produce a synthesized oblique variant
|
||||
pub fn skew(&self) -> Self {
|
||||
// This function is derived from code which is subject to the terms of the
|
||||
// Mozilla Public License, v. 2.0. http://mozilla.org/MPL/2.0/
|
||||
|
||||
let factor = 0.2f64; // Skew factor
|
||||
let stride = self.width * 4;
|
||||
|
||||
// Calculate the skewed horizontal offsets of the bottom and top of the glyph.
|
||||
let bottom = self.bearing_y.get().round() as f64 - self.height as f64;
|
||||
let skew_min = ((bottom + 0.5) * factor).floor();
|
||||
let skew_max = ((self.bearing_y.get() as f64 - 0.5) * factor).ceil();
|
||||
|
||||
// Allocate enough extra width for the min/max skew offsets.
|
||||
let skew_width = self.width + (skew_max - skew_min) as usize;
|
||||
let mut skew_buffer = vec![0u8; skew_width * self.height * 4];
|
||||
for y in 0..self.height {
|
||||
// Calculate a skew offset at the vertical center of the current row.
|
||||
let offset = (self.bearing_y.get() - y as f64 - 0.5) * factor - skew_min;
|
||||
// Get a blend factor in 0..256 constant across all pixels in the row.
|
||||
let blend = (offset.fract() * 256.0) as u32;
|
||||
let src_row = y * stride;
|
||||
let dest_row = (y * skew_width + offset.floor() as usize) * 4;
|
||||
let mut prev_px = [0u32; 4];
|
||||
for (src, dest) in self.data[src_row..src_row + stride]
|
||||
.chunks(4)
|
||||
.zip(skew_buffer[dest_row..dest_row + stride].chunks_mut(4))
|
||||
{
|
||||
let px = [src[0] as u32, src[1] as u32, src[2] as u32, src[3] as u32];
|
||||
// Blend current pixel with previous pixel based on blend factor.
|
||||
let next_px = [px[0] * blend, px[1] * blend, px[2] * blend, px[3] * blend];
|
||||
dest[0] = ((((px[0] << 8) - next_px[0]) + prev_px[0] + 128) >> 8) as u8;
|
||||
dest[1] = ((((px[1] << 8) - next_px[1]) + prev_px[1] + 128) >> 8) as u8;
|
||||
dest[2] = ((((px[2] << 8) - next_px[2]) + prev_px[2] + 128) >> 8) as u8;
|
||||
dest[3] = ((((px[3] << 8) - next_px[3]) + prev_px[3] + 128) >> 8) as u8;
|
||||
// Save the remainder for blending onto the next pixel.
|
||||
prev_px = next_px;
|
||||
}
|
||||
// If the skew misaligns the final pixel, write out the remainder.
|
||||
if blend > 0 {
|
||||
let dest = &mut skew_buffer[dest_row + stride..dest_row + stride + 4];
|
||||
dest[0] = ((prev_px[0] + 128) >> 8) as u8;
|
||||
dest[1] = ((prev_px[1] + 128) >> 8) as u8;
|
||||
dest[2] = ((prev_px[2] + 128) >> 8) as u8;
|
||||
dest[3] = ((prev_px[3] + 128) >> 8) as u8;
|
||||
}
|
||||
}
|
||||
Self {
|
||||
data: skew_buffer,
|
||||
height: self.height,
|
||||
width: skew_width,
|
||||
bearing_x: self.bearing_x + PixelLength::new(skew_min),
|
||||
bearing_y: self.bearing_y,
|
||||
has_color: self.has_color,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Rasterizes the specified glyph index in the associated font
|
||||
/// and returns the generated bitmap
|
||||
pub trait FontRasterizer {
|
||||
|
||||
Reference in new issue
Block a user