remove the use of bitmap scaler

This commit is contained in:
Junkui Zhang 2025-08-01 16:48:36 +08:00
parent c3f210eb5e
commit e599352fc2

View file

@ -775,12 +775,6 @@ impl DirectWriteState {
)?; )?;
} }
let antialias_mode = if params.is_emoji {
DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE
} else {
DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE
};
let glyph_analysis = unsafe { let glyph_analysis = unsafe {
self.components.factory.CreateGlyphRunAnalysis( self.components.factory.CreateGlyphRunAnalysis(
&glyph_run, &glyph_run,
@ -788,7 +782,7 @@ impl DirectWriteState {
rendering_mode, rendering_mode,
DWRITE_MEASURING_MODE_NATURAL, DWRITE_MEASURING_MODE_NATURAL,
grid_fit_mode, grid_fit_mode,
antialias_mode, DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE,
baseline_origin_x, baseline_origin_x,
baseline_origin_y, baseline_origin_y,
) )
@ -854,48 +848,41 @@ impl DirectWriteState {
anyhow::bail!("glyph bounds are empty"); anyhow::bail!("glyph bounds are empty");
} }
let bitmap_size = glyph_bounds.size; let bitmap_data = if params.is_emoji {
let mut bitmap_data: Vec<u8>;
if params.is_emoji {
let glyph_analysis = self.create_glyph_run_analysis(params)?;
if let Ok(color) = self.rasterize_color(&params, glyph_bounds) { if let Ok(color) = self.rasterize_color(&params, glyph_bounds) {
bitmap_data = color; color
} else { } else {
let monochrome = self.rasterize_monochrome(&glyph_analysis, glyph_bounds)?; let monochrome = self.rasterize_monochrome(params, glyph_bounds)?;
bitmap_data = monochrome monochrome
.into_iter() .into_iter()
.flat_map(|pixel| [0, 0, 0, pixel]) .flat_map(|pixel| [0, 0, 0, pixel])
.collect::<Vec<_>>(); .collect::<Vec<_>>()
} }
} else { } else {
let glyph_analysis = self.create_glyph_run_analysis(params)?; self.rasterize_monochrome(params, glyph_bounds)?
bitmap_data = self.rasterize_monochrome(&glyph_analysis, glyph_bounds)?; };
}
Ok((bitmap_size, bitmap_data)) Ok((glyph_bounds.size, bitmap_data))
} }
fn rasterize_monochrome( fn rasterize_monochrome(
&self, &self,
glyph_analysis: &IDWriteGlyphRunAnalysis, params: &RenderGlyphParams,
glyph_bounds: Bounds<DevicePixels>, glyph_bounds: Bounds<DevicePixels>,
) -> Result<Vec<u8>> { ) -> Result<Vec<u8>> {
let multisampled_bounds =
unsafe { glyph_analysis.GetAlphaTextureBounds(DWRITE_TEXTURE_CLEARTYPE_3x1)? };
let multisampled_size = size(
multisampled_bounds.right - multisampled_bounds.left,
multisampled_bounds.bottom - multisampled_bounds.top,
);
let mut bitmap_data = let mut bitmap_data =
vec![0u8; multisampled_size.width as usize * multisampled_size.height as usize * 3]; vec![0u8; glyph_bounds.size.width.0 as usize * glyph_bounds.size.height.0 as usize * 3];
let glyph_analysis = self.create_glyph_run_analysis(params)?;
unsafe { unsafe {
glyph_analysis.CreateAlphaTexture( glyph_analysis.CreateAlphaTexture(
DWRITE_TEXTURE_CLEARTYPE_3x1, DWRITE_TEXTURE_CLEARTYPE_3x1,
&multisampled_bounds, &RECT {
left: glyph_bounds.origin.x.0,
top: glyph_bounds.origin.y.0,
right: glyph_bounds.size.width.0 + glyph_bounds.origin.x.0,
bottom: glyph_bounds.size.height.0 + glyph_bounds.origin.y.0,
},
&mut bitmap_data, &mut bitmap_data,
)?; )?;
} }
@ -903,10 +890,10 @@ impl DirectWriteState {
let bitmap_factory = self.components.bitmap_factory.resolve()?; let bitmap_factory = self.components.bitmap_factory.resolve()?;
let bitmap = unsafe { let bitmap = unsafe {
bitmap_factory.CreateBitmapFromMemory( bitmap_factory.CreateBitmapFromMemory(
multisampled_size.width as u32, glyph_bounds.size.width.0 as u32,
multisampled_size.height as u32, glyph_bounds.size.height.0 as u32,
&GUID_WICPixelFormat24bppRGB, &GUID_WICPixelFormat24bppRGB,
multisampled_size.width as u32 * 3, glyph_bounds.size.width.0 as u32 * 3,
&bitmap_data, &bitmap_data,
) )
}?; }?;
@ -914,20 +901,10 @@ impl DirectWriteState {
let grayscale_bitmap = let grayscale_bitmap =
unsafe { WICConvertBitmapSource(&GUID_WICPixelFormat8bppGray, &bitmap) }?; unsafe { WICConvertBitmapSource(&GUID_WICPixelFormat8bppGray, &bitmap) }?;
let scaler = unsafe { bitmap_factory.CreateBitmapScaler() }?;
unsafe {
scaler.Initialize(
&grayscale_bitmap,
glyph_bounds.size.width.0 as u32,
glyph_bounds.size.height.0 as u32,
WICBitmapInterpolationModeHighQualityCubic,
)
}?;
let mut bitmap_data = let mut bitmap_data =
vec![0u8; glyph_bounds.size.width.0 as usize * glyph_bounds.size.height.0 as usize]; vec![0u8; glyph_bounds.size.width.0 as usize * glyph_bounds.size.height.0 as usize];
unsafe { unsafe {
scaler.CopyPixels( grayscale_bitmap.CopyPixels(
std::ptr::null() as _, std::ptr::null() as _,
glyph_bounds.size.width.0 as u32, glyph_bounds.size.width.0 as u32,
&mut bitmap_data, &mut bitmap_data,