cleanup
This commit is contained in:
parent
a8d3e5530b
commit
d50b3e172e
1 changed files with 43 additions and 49 deletions
|
@ -836,51 +836,13 @@ impl DirectWriteState {
|
||||||
anyhow::bail!("glyph bounds are empty");
|
anyhow::bail!("glyph bounds are empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add an extra pixel when the subpixel variant isn't zero to make room for anti-aliasing.
|
|
||||||
let bitmap_size = glyph_bounds.size;
|
let bitmap_size = glyph_bounds.size;
|
||||||
|
|
||||||
let subpixel_shift = params
|
|
||||||
.subpixel_variant
|
|
||||||
.map(|v| v as f32 / SUBPIXEL_VARIANTS as f32);
|
|
||||||
let baseline_origin_x = subpixel_shift.x / params.scale_factor;
|
|
||||||
let baseline_origin_y = subpixel_shift.y / params.scale_factor;
|
|
||||||
|
|
||||||
let glyph_analysis = self.create_glyph_run_analysis(params)?;
|
let glyph_analysis = self.create_glyph_run_analysis(params)?;
|
||||||
|
|
||||||
let font = &self.fonts[params.font_id.0];
|
|
||||||
let glyph_id = [params.glyph_id.0 as u16];
|
|
||||||
let advance = [glyph_bounds.size.width.0 as f32];
|
|
||||||
let offset = [DWRITE_GLYPH_OFFSET {
|
|
||||||
advanceOffset: -glyph_bounds.origin.x.0 as f32 / params.scale_factor,
|
|
||||||
ascenderOffset: glyph_bounds.origin.y.0 as f32 / params.scale_factor,
|
|
||||||
}];
|
|
||||||
let glyph_run = DWRITE_GLYPH_RUN {
|
|
||||||
fontFace: unsafe { std::mem::transmute_copy(&font.font_face) },
|
|
||||||
fontEmSize: params.font_size.0,
|
|
||||||
glyphCount: 1,
|
|
||||||
glyphIndices: glyph_id.as_ptr(),
|
|
||||||
glyphAdvances: advance.as_ptr(),
|
|
||||||
glyphOffsets: offset.as_ptr(),
|
|
||||||
isSideways: BOOL(0),
|
|
||||||
bidiLevel: 0,
|
|
||||||
};
|
|
||||||
let transform = DWRITE_MATRIX {
|
|
||||||
m11: params.scale_factor,
|
|
||||||
m12: 0.0,
|
|
||||||
m21: 0.0,
|
|
||||||
m22: params.scale_factor,
|
|
||||||
dx: 0.0,
|
|
||||||
dy: 0.0,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut bitmap_data: Vec<u8>;
|
let mut bitmap_data: Vec<u8>;
|
||||||
if params.is_emoji {
|
if params.is_emoji {
|
||||||
if let Ok(color) = self.rasterize_color(
|
if let Ok(color) = self.rasterize_color(¶ms, glyph_bounds) {
|
||||||
&glyph_run,
|
|
||||||
&transform,
|
|
||||||
point(baseline_origin_x, baseline_origin_y),
|
|
||||||
bitmap_size,
|
|
||||||
) {
|
|
||||||
bitmap_data = color;
|
bitmap_data = color;
|
||||||
} else {
|
} else {
|
||||||
let monochrome = Self::rasterize_monochrome(&glyph_analysis, glyph_bounds)?;
|
let monochrome = Self::rasterize_monochrome(&glyph_analysis, glyph_bounds)?;
|
||||||
|
@ -921,20 +883,52 @@ impl DirectWriteState {
|
||||||
|
|
||||||
fn rasterize_color(
|
fn rasterize_color(
|
||||||
&self,
|
&self,
|
||||||
glyph_run: &DWRITE_GLYPH_RUN,
|
params: &RenderGlyphParams,
|
||||||
transform: &DWRITE_MATRIX,
|
glyph_bounds: Bounds<DevicePixels>,
|
||||||
baseline_origin: Point<f32>,
|
|
||||||
bitmap_size: Size<DevicePixels>,
|
|
||||||
) -> Result<Vec<u8>> {
|
) -> Result<Vec<u8>> {
|
||||||
|
let bitmap_size = glyph_bounds.size;
|
||||||
|
let subpixel_shift = params
|
||||||
|
.subpixel_variant
|
||||||
|
.map(|v| v as f32 / SUBPIXEL_VARIANTS as f32);
|
||||||
|
let baseline_origin_x = subpixel_shift.x / params.scale_factor;
|
||||||
|
let baseline_origin_y = subpixel_shift.y / params.scale_factor;
|
||||||
|
|
||||||
|
let transform = DWRITE_MATRIX {
|
||||||
|
m11: params.scale_factor,
|
||||||
|
m12: 0.0,
|
||||||
|
m21: 0.0,
|
||||||
|
m22: params.scale_factor,
|
||||||
|
dx: 0.0,
|
||||||
|
dy: 0.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let font = &self.fonts[params.font_id.0];
|
||||||
|
let glyph_id = [params.glyph_id.0 as u16];
|
||||||
|
let advance = [glyph_bounds.size.width.0 as f32];
|
||||||
|
let offset = [DWRITE_GLYPH_OFFSET {
|
||||||
|
advanceOffset: -glyph_bounds.origin.x.0 as f32 / params.scale_factor,
|
||||||
|
ascenderOffset: glyph_bounds.origin.y.0 as f32 / params.scale_factor,
|
||||||
|
}];
|
||||||
|
let glyph_run = DWRITE_GLYPH_RUN {
|
||||||
|
fontFace: unsafe { std::mem::transmute_copy(&font.font_face) },
|
||||||
|
fontEmSize: params.font_size.0,
|
||||||
|
glyphCount: 1,
|
||||||
|
glyphIndices: glyph_id.as_ptr(),
|
||||||
|
glyphAdvances: advance.as_ptr(),
|
||||||
|
glyphOffsets: offset.as_ptr(),
|
||||||
|
isSideways: BOOL(0),
|
||||||
|
bidiLevel: 0,
|
||||||
|
};
|
||||||
|
|
||||||
// todo: support formats other than COLR
|
// todo: support formats other than COLR
|
||||||
let color_enumerator = unsafe {
|
let color_enumerator = unsafe {
|
||||||
self.components.factory.TranslateColorGlyphRun(
|
self.components.factory.TranslateColorGlyphRun(
|
||||||
Vector2::new(baseline_origin.x, baseline_origin.y),
|
Vector2::new(baseline_origin_x, baseline_origin_y),
|
||||||
glyph_run,
|
&glyph_run,
|
||||||
None,
|
None,
|
||||||
DWRITE_GLYPH_IMAGE_FORMATS_COLR,
|
DWRITE_GLYPH_IMAGE_FORMATS_COLR,
|
||||||
DWRITE_MEASURING_MODE_NATURAL,
|
DWRITE_MEASURING_MODE_NATURAL,
|
||||||
Some(transform),
|
Some(&transform),
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
}?;
|
}?;
|
||||||
|
@ -948,13 +942,13 @@ impl DirectWriteState {
|
||||||
let color_analysis = unsafe {
|
let color_analysis = unsafe {
|
||||||
self.components.factory.CreateGlyphRunAnalysis(
|
self.components.factory.CreateGlyphRunAnalysis(
|
||||||
&color_run.Base.glyphRun as *const _,
|
&color_run.Base.glyphRun as *const _,
|
||||||
Some(transform),
|
Some(&transform),
|
||||||
DWRITE_RENDERING_MODE1_NATURAL_SYMMETRIC,
|
DWRITE_RENDERING_MODE1_NATURAL_SYMMETRIC,
|
||||||
DWRITE_MEASURING_MODE_NATURAL,
|
DWRITE_MEASURING_MODE_NATURAL,
|
||||||
DWRITE_GRID_FIT_MODE_DEFAULT,
|
DWRITE_GRID_FIT_MODE_DEFAULT,
|
||||||
DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE,
|
DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE,
|
||||||
baseline_origin.x,
|
baseline_origin_x,
|
||||||
baseline_origin.y,
|
baseline_origin_y,
|
||||||
)
|
)
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue