Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

411
vendor/swash/src/scale/bitmap/mod.rs vendored Normal file
View File

@@ -0,0 +1,411 @@
//! Bitmap scaling.
#![allow(dead_code)]
use alloc::vec::Vec;
#[cfg(feature = "libm")]
use core_maths::CoreFloat;
mod png;
/// Decodes a PNG image.
pub fn decode_png(data: &[u8], scratch: &mut Vec<u8>, target: &mut [u8]) -> Option<(u32, u32)> {
png::decode(data, scratch, target)
.map(|(w, h, _)| (w, h))
.ok()
}
pub fn blit(
mask: &[u8],
mask_width: u32,
mask_height: u32,
x: i32,
y: i32,
color: [u8; 4],
target: &mut [u8],
target_width: u32,
target_height: u32,
) {
if mask_width == 0 || mask_height == 0 || target_width == 0 || target_height == 0 {
return;
}
let source_width = mask_width as usize;
let source_height = mask_height as usize;
let dest_width = target_width as usize;
let dest_height = target_height as usize;
let source_x = if x < 0 { -x as usize } else { 0 };
let source_y = if y < 0 { -y as usize } else { 0 };
if source_x >= source_width || source_y >= source_height {
return;
}
let dest_x = if x < 0 { 0 } else { x as usize };
let dest_y = if y < 0 { 0 } else { y as usize };
if dest_x >= dest_width || dest_y >= dest_height {
return;
}
let source_end_x = (source_width).min(dest_width - dest_x + source_x);
let source_end_y = (source_height).min(dest_height - dest_y + source_y);
let source_columns = source_y..source_end_y;
let source_rows = source_x..source_end_x;
let dest_pitch = target_width as usize * 4;
let color_a = color[3] as u32;
let mut dy = dest_y;
for sy in source_columns {
let src_row = &mask[sy * mask_width as usize..];
let dst_row = &mut target[dy * dest_pitch..];
dy += 1;
let mut dx = dest_x * 4;
for sx in source_rows.clone() {
let a = (src_row[sx] as u32 * color_a) >> 8;
if a >= 255 {
dst_row[dx + 3] = 255;
dst_row[dx..(dx + 3)].copy_from_slice(&color[..3]);
dst_row[dx + 3] = 255;
} else if a != 0 {
let inverse_a = 255 - a;
for i in 0..3 {
let d = dst_row[dx + i] as u32;
let c = ((inverse_a * d) + (a * color[i] as u32)) >> 8;
dst_row[dx + i] = c as u8;
}
let d = dst_row[dx + 3] as u32;
let c = ((inverse_a * d) + a * 255) >> 8;
dst_row[dx + 3] = c as u8;
}
dx += 4;
}
}
}
#[derive(Copy, Clone, PartialEq)]
pub enum Filter {
Nearest,
Bilinear,
Bicubic,
Mitchell,
Lanczos3,
Gaussian,
}
pub fn resize(
image: &[u8],
width: u32,
height: u32,
channels: u32,
target: &mut [u8],
target_width: u32,
target_height: u32,
filter: Filter,
scratch: Option<&mut Vec<u8>>,
) -> bool {
if target_width == 0 || target_height == 0 {
return true;
}
let mut tmp = Vec::new();
let scratch = if let Some(scratch) = scratch {
scratch
} else {
&mut tmp
};
let image_size = (width * height * channels) as usize;
if image.len() < image_size {
return false;
}
let target_size = (target_width * target_height * channels) as usize;
if target.len() < target_size {
return false;
}
let scratch_size = (target_width * height * channels) as usize;
scratch.resize(scratch_size, 0);
use Filter::*;
match filter {
Nearest => resample(
image,
width,
height,
channels,
target,
target_width,
target_height,
scratch,
0.,
&nearest,
),
Bilinear => resample(
image,
width,
height,
channels,
target,
target_width,
target_height,
scratch,
1.,
&bilinear,
),
Bicubic => resample(
image,
width,
height,
channels,
target,
target_width,
target_height,
scratch,
2.,
&bicubic,
),
Mitchell => resample(
image,
width,
height,
channels,
target,
target_width,
target_height,
scratch,
2.,
&mitchell,
),
Lanczos3 => resample(
image,
width,
height,
channels,
target,
target_width,
target_height,
scratch,
3.,
&lanczos3,
),
Gaussian => resample(
image,
width,
height,
channels,
target,
target_width,
target_height,
scratch,
3.,
&|x| gaussian(x, 0.5),
),
}
}
fn resample<Filter>(
image: &[u8],
width: u32,
height: u32,
channels: u32,
target: &mut [u8],
target_width: u32,
target_height: u32,
scratch: &mut [u8],
support: f32,
filter: &Filter,
) -> bool
where
Filter: Fn(f32) -> f32,
{
let tmp_width = target_width;
let tmp_height = height;
let s = 1. / 255.;
if channels == 1 {
sample_dir(
&|x, y| [0., 0., 0., image[(y * width + x) as usize] as f32 * s],
width,
height,
target_width,
filter,
support,
&mut |x, y, p| scratch[(y * tmp_width + x) as usize] = (p[3] * 255.) as u8,
);
sample_dir(
&|y, x| [0., 0., 0., scratch[(y * tmp_width + x) as usize] as f32 * s],
tmp_height,
tmp_width,
target_height,
filter,
support,
&mut |y, x, p| target[(y * target_width + x) as usize] = (p[3] * 255.) as u8,
);
true
} else if channels == 4 {
sample_dir(
&|x, y| {
let row = (y * width * channels + x * channels) as usize;
[
image[row] as f32 * s,
image[row + 1] as f32 * s,
image[row + 2] as f32 * s,
image[row + 3] as f32 * s,
]
},
width,
height,
target_width,
filter,
support,
&mut |x, y, p| {
let row = (y * target_width * channels + x * channels) as usize;
scratch[row] = (p[0] * 255.) as u8;
scratch[row + 1] = (p[1] * 255.) as u8;
scratch[row + 2] = (p[2] * 255.) as u8;
scratch[row + 3] = (p[3] * 255.) as u8;
},
);
sample_dir(
&|y, x| {
let row = (y * tmp_width * channels + x * channels) as usize;
[
scratch[row] as f32 * s,
scratch[row + 1] as f32 * s,
scratch[row + 2] as f32 * s,
scratch[row + 3] as f32 * s,
]
},
tmp_height,
tmp_width,
target_height,
filter,
support,
&mut |y, x, p| {
let row = (y * target_width * channels + x * channels) as usize;
target[row] = (p[0] * 255.) as u8;
target[row + 1] = (p[1] * 255.) as u8;
target[row + 2] = (p[2] * 255.) as u8;
target[row + 3] = (p[3] * 255.) as u8;
},
);
true
} else {
false
}
}
fn sample_dir<Input, Output, Filter>(
input: &Input,
width: u32,
height: u32,
new_width: u32,
filter: &Filter,
support: f32,
output: &mut Output,
) where
Input: Fn(u32, u32) -> [f32; 4],
Output: FnMut(u32, u32, &[f32; 4]),
Filter: Fn(f32) -> f32,
{
const MAX_WEIGHTS: usize = 64;
let mut weights = [0f32; MAX_WEIGHTS];
let mut num_weights;
let ratio = width as f32 / new_width as f32;
let sratio = ratio.max(1.);
let src_support = support * sratio;
let isratio = 1. / sratio;
for outx in 0..new_width {
let inx = (outx as f32 + 0.5) * ratio;
let left = (inx - src_support).floor() as i32;
let mut left = left.max(0).min(width as i32 - 1) as usize;
let right = (inx + src_support).ceil() as i32;
let mut right = right.max(left as i32 + 1).min(width as i32) as usize;
let inx = inx - 0.5;
while right - left > MAX_WEIGHTS {
right -= 1;
left += 1;
}
num_weights = 0;
let mut sum = 0.;
for i in left..right {
let w = filter((i as f32 - inx) * isratio);
weights[num_weights] = w;
num_weights += 1;
sum += w;
}
let isum = 1. / sum;
let weights = &weights[..num_weights];
for y in 0..height {
let mut accum = [0f32; 4];
for (i, w) in weights.iter().enumerate() {
let p = input((left + i) as u32, y);
let a = p[3];
accum[0] += p[0] * w * a;
accum[1] += p[1] * w * a;
accum[2] += p[2] * w * a;
accum[3] += p[3] * w;
}
if accum[3] != 0. {
let a = 1. / accum[3];
accum[0] *= a;
accum[1] *= a;
accum[2] *= a;
accum[3] *= isum;
}
output(outx, y, &accum);
}
}
}
fn sinc(t: f32) -> f32 {
let a = t * core::f32::consts::PI;
if t == 0. {
1.
} else {
a.sin() / a
}
}
fn lanczos3(x: f32) -> f32 {
if x.abs() < 3. {
(sinc(x) * sinc(x / 3.)).abs()
} else {
0.
}
}
fn bilinear(x: f32) -> f32 {
let x = x.abs();
if x < 1. {
1. - x
} else {
0.
}
}
fn bicubic(x: f32) -> f32 {
let a = x.abs();
let b = 0.;
let c = 0.5;
let k = if a < 1. {
(12. - 9. * b - 6. * c) * a.powi(3) + (-18. + 12. * b + 6. * c) * a.powi(2) + (6. - 2. * b)
} else if a < 2. {
(-b - 6. * c) * a.powi(3)
+ (6. * b + 30. * c) * a.powi(2)
+ (-12. * b - 48. * c) * a
+ (8. * b + 24. * c)
} else {
0.
};
(k / 6.).abs()
}
fn mitchell(x: f32) -> f32 {
let x = x.abs();
if x < 1. {
((16. + x * x * (21. * x - 36.)) / 18.).abs()
} else if x < 2. {
((32. + x * (-60. + x * (36. - 7. * x))) / 18.).abs()
} else {
0.
}
}
fn nearest(_x: f32) -> f32 {
1.
}
fn gaussian(x: f32, r: f32) -> f32 {
((2. * core::f32::consts::PI).sqrt() * r).recip() * (-x.powi(2) / (2. * r.powi(2))).exp()
}

589
vendor/swash/src/scale/bitmap/png.rs vendored Normal file
View File

@@ -0,0 +1,589 @@
//! PNG decoder.
use alloc::vec::Vec;
/// PNG magic bytes.
pub const SIGNATURE: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
/// Errors that can occur during decoding.
#[derive(Debug)]
pub enum DecodeError {
/// The file format was not supported.
UnsupportedFileFormat,
/// Conversion into the requested format failed.
ConversionFailed,
/// Invalid signature in an image.
InvalidSignature,
/// The file contains a pixel format this is not supported.
UnsupportedPixelFormat,
/// The file enables a feature that is not supported.
UnsupportedFeature,
/// Some size limit was exceeded.
LimitExceeded,
/// Some index into the image was out of bounds.
IndexOutOfBounds,
/// Some portion of the file was corrupt.
CorruptData,
/// An "end of file" was reached prematurely.
UnexpectedEof,
}
impl From<yazi::Error> for DecodeError {
fn from(_: yazi::Error) -> Self {
Self::CorruptData
}
}
/// The possible color types for a PNG image.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ColorType {
Greyscale = 0,
GreyscaleAlpha = 4,
Indexed = 3,
TrueColor = 2,
TrueColorAlpha = 6,
}
/// The PNG header.
#[derive(Copy, Clone, Debug)]
pub struct Header {
pub width: u32,
pub height: u32,
pub color_type: ColorType,
pub depth: u8,
pub interlaced: bool,
}
impl Header {
/// Attempts to decode a PNG header from the specified buffer.
pub fn decode(png: &[u8]) -> Option<Self> {
const IHDR: u32 = 73 << 24 | 72 << 16 | 68 << 8 | 82;
if png.len() < 33 || !check_signature(png) {
return None;
}
let mut o = 8;
let len = get_u32be(png, o);
if len != 13 || get_u32be(png, o + 4) != IHDR {
return None;
}
o += 4;
let width = get_u32be(png, o + 4);
let height = get_u32be(png, o + 8);
let depth = png[o + 12];
let color_type = png[o + 13];
let compression_method = png[o + 14];
let filter_method = png[o + 15];
let interlace_method = png[o + 16];
if compression_method != 0
|| filter_method != 0
|| (interlace_method != 0 && interlace_method != 1)
{
return None;
}
let _crc = get_u32be(png, o + 17);
let d = depth;
use ColorType::*;
let color_type = match color_type {
0 => Greyscale,
2 => TrueColor,
3 => Indexed,
4 => GreyscaleAlpha,
6 => TrueColorAlpha,
_ => return None,
};
match color_type {
Greyscale | Indexed => {
if d != 1 && d != 2 && d != 4 && d != 8 && d != 16 {
return None;
}
if color_type == Indexed && d == 16 {
return None;
}
}
TrueColor | TrueColorAlpha | GreyscaleAlpha => {
if d != 8 && d != 16 {
return None;
}
}
};
Some(Self {
width,
height,
color_type,
depth,
interlaced: interlace_method != 0,
})
}
}
/// Returns true if the specified buffer might represent a PNG image.
pub fn check_signature(png: &[u8]) -> bool {
if png.len() >= 8 {
for i in 0..8 {
if png[i] != SIGNATURE[i] {
return false;
}
}
return true;
}
false
}
pub fn decode(
data: &[u8],
scratch: &mut Vec<u8>,
target: &mut [u8],
) -> Result<(u32, u32, bool), DecodeError> {
let mut state = State::new(data, scratch)?;
let w = state.header.width;
let h = state.header.height;
if w == 0 || h == 0 {
return Ok((w, h, false));
}
let decomp_len = scratch.len();
scratch.resize(decomp_len + state.extra_bytes, 0);
let (decomp, extra) = scratch.split_at_mut(decomp_len);
if target.len() < (w * h * 4) as usize {
return Err(DecodeError::LimitExceeded);
}
state.trunc_16 = true;
state.expand_alpha = true;
decode_data::<EmitRgba8>(&mut state, decomp, extra, target).ok_or(DecodeError::CorruptData)?;
Ok((w, h, state.has_alpha))
}
struct State<'a> {
header: Header,
data: &'a [u8],
palette: &'a [u8],
trans: &'a [u8],
gamma: Option<f32>,
effective_depth: u8,
bpp: usize,
channels: usize,
pitch: usize,
extra_bytes: usize,
bwidth: usize,
has_alpha: bool,
trunc_16: bool,
expand_alpha: bool,
}
impl<'a> State<'a> {
fn new(data: &'a [u8], decomp: &mut Vec<u8>) -> Result<Self, DecodeError> {
let header = Header::decode(data).ok_or(DecodeError::CorruptData)?;
let mut this = Self {
header,
data,
palette: &[],
trans: &[],
gamma: None,
effective_depth: header.depth,
bpp: 0,
channels: 0,
pitch: 0,
extra_bytes: 0,
bwidth: 0,
has_alpha: false,
trunc_16: false,
expand_alpha: false,
};
let w = header.width as usize;
let h = header.height as usize;
if w == 0 || h == 0 {
return Ok(this);
}
use ColorType::*;
let (channels, has_alpha) = match header.color_type {
TrueColor => (3, false),
TrueColorAlpha => (4, true),
GreyscaleAlpha => (2, true),
Greyscale => (1, false),
Indexed => (1, false),
};
this.has_alpha = has_alpha;
this.bpp = this.header.depth as usize * channels;
this.pitch = (w * this.bpp + 7) / 8;
this.bwidth = (this.bpp + 7) / 8;
this.extra_bytes = this.pitch * 2 + w * 8;
decomp.clear();
decomp.reserve(this.extra_bytes + (this.pitch + 1) * h);
let limit = data.len();
let mut offset = 33;
let mut dec = yazi::Decoder::new();
dec.set_format(yazi::Format::Zlib);
let mut stream = dec.stream_into_vec(decomp);
loop {
if offset + 8 > limit {
return Err(DecodeError::CorruptData);
}
let len = get_u32be(data, offset) as usize;
offset += 4;
let ty = get_u32be(data, offset);
offset += 4;
if offset + len > limit {
return Err(DecodeError::CorruptData);
}
let bytes = data
.get(offset..offset + len)
.ok_or(DecodeError::CorruptData)?;
const PLTE: u32 = chunk_name(b"PLTE");
const TRNS: u32 = chunk_name(b"tRNS");
const IDAT: u32 = chunk_name(b"IDAT");
const GAMA: u32 = chunk_name(b"gAMA");
const IEND: u32 = chunk_name(b"IEND");
match ty {
PLTE => this.palette = bytes,
TRNS => this.trans = bytes,
IDAT => {
stream.write(bytes)?;
}
GAMA => {
if bytes.len() > 4 && this.gamma.is_none() {
this.gamma = Some(get_u32be(bytes, 0) as f32 / 100000.);
}
}
IEND => break,
_ => {}
}
offset += len + 4;
}
stream.finish()?;
if header.color_type == Indexed {
if this.palette.is_empty() {
return Err(DecodeError::CorruptData);
}
if !this.trans.is_empty() {
this.has_alpha = true;
}
}
Ok(this)
}
}
fn decode_data<E: Emit>(
state: &mut State,
decomp: &mut [u8],
extra: &mut [u8],
target: &mut [u8],
) -> Option<()> {
let has_palette = !state.palette.is_empty();
let w = state.header.width as usize;
let h = state.header.height as usize;
let (mut line, extra) = extra.split_at_mut(state.pitch);
let (mut prev_line, out_line) = extra.split_at_mut(state.pitch);
let depth = state.header.depth;
let bpp = state.bpp;
let pitch = state.pitch;
let bwidth = state.bwidth;
let trunc_16 = state.trunc_16;
if state.header.interlaced {
const ROW_START: [u8; 7] = [0, 0, 4, 0, 2, 0, 1];
const ROW_INCREMENT: [u8; 7] = [8, 8, 8, 4, 4, 2, 2];
const COL_START: [u8; 7] = [0, 4, 0, 2, 0, 1, 0];
const COL_INCREMENT: [u8; 7] = [8, 8, 4, 4, 2, 2, 1];
let mut pass = 0;
let mut y = 0;
let mut offset = 0;
loop {
let cols = match pass {
0 => (w + 7) / 8,
1 => (w + 3) / 8,
2 => (w + 3) / 4,
3 => (w + 1) / 4,
4 => (w + 1) / 2,
5 => w / 2,
6 => w,
_ => return None,
};
if cols == 0 {
pass += 1;
continue;
}
let start = COL_START[pass] as usize;
let inc = COL_INCREMENT[pass] as usize;
let row_inc = ROW_INCREMENT[pass] as usize;
while y < h {
let pitch = (cols * bpp + 7) / 8;
let end = offset + pitch + 1;
let source = decomp.get(offset..end)?;
offset = end;
let ty = source[0];
defilter(
ty,
source.get(1..)?,
line.get_mut(..pitch)?,
prev_line.get(..pitch)?,
bwidth,
)?;
if depth == 8 {
E::emit(state, line, target, start, y, w, inc, cols)?;
} else {
normalize(line, out_line, depth, has_palette, cols, trunc_16)?;
E::emit(state, out_line, target, start, y, w, inc, cols)?;
}
core::mem::swap(&mut prev_line, &mut line);
y += row_inc;
}
if pass == 6 {
break;
}
pass += 1;
y = ROW_START[pass] as usize;
for byte in prev_line.iter_mut() {
*byte = 0;
}
}
} else if depth == 8 {
for y in 0..h {
let offset = y * (pitch + 1);
let end = offset + pitch + 1;
let source = decomp.get(offset..end)?;
let ty = *source.get(0)?;
defilter(ty, source.get(1..)?, line, prev_line, bwidth)?;
E::emit(state, line, target, 0, y, w, 1, w)?;
core::mem::swap(&mut prev_line, &mut line);
}
} else {
for y in 0..h {
let offset = y * (pitch + 1);
let end = offset + pitch + 1;
let source = decomp.get(offset..end)?;
let ty = *source.get(0)?;
defilter(ty, source.get(1..)?, line, prev_line, bwidth)?;
normalize(line, out_line, depth, has_palette, w, trunc_16)?;
E::emit(state, out_line, target, 0, y, w, 1, w)?;
core::mem::swap(&mut prev_line, &mut line);
}
}
Some(())
}
#[cfg(target_endian = "little")]
const IS_LITTLE_ENDIAN: bool = true;
#[cfg(not(target_endian = "little"))]
const IS_LITTLE_ENDIAN: bool = false;
fn defilter(ty: u8, source: &[u8], dest: &mut [u8], last: &[u8], bwidth: usize) -> Option<()> {
let len = source.len();
match ty {
0 => {
dest.copy_from_slice(source);
}
1 => {
dest.get_mut(..bwidth)?
.copy_from_slice(source.get(..bwidth)?);
for i in bwidth..len {
dest[i] = source[i].wrapping_add(dest[i - bwidth]);
}
}
2 => {
for ((dest, source), last) in dest.iter_mut().zip(source.iter()).zip(last.iter()) {
*dest = source.wrapping_add(*last);
}
}
3 => {
for i in 0..bwidth {
dest[i] = source[i].wrapping_add((last[i] as u32 / 2) as u8);
}
for i in bwidth..len {
dest[i] = source[i].wrapping_add(
((dest[i - bwidth] as u32).wrapping_add(last[i] as u32) / 2) as u8,
);
}
}
4 => {
for i in 0..bwidth {
dest[i] = source[i].wrapping_add(last[i]);
}
for i in bwidth..len {
dest[i] =
source[i].wrapping_add(paeth(dest[i - bwidth], last[i], last[i - bwidth]));
}
}
_ => return None,
}
Some(())
}
fn normalize(
source: &[u8],
dest: &mut [u8],
depth: u8,
palette: bool,
width: usize,
trunc_16: bool,
) -> Option<()> {
match depth {
16 => {
if trunc_16 {
for (i, d) in dest.iter_mut().enumerate() {
*d = source[i * 2];
}
} else if IS_LITTLE_ENDIAN {
for (s, d) in source.chunks(2).zip(dest.chunks_mut(2)) {
d[1] = s[0];
d[0] = s[1];
}
} else {
dest.copy_from_slice(source);
}
}
8 => {
dest.copy_from_slice(source);
}
4 => {
let conv = if !palette { 17 } else { 1 };
for (i, d) in dest.get_mut(..width)?.iter_mut().enumerate() {
*d = (source[i / 2] >> (4 - i % 2 * 4) & 15) * conv;
}
}
2 => {
let conv = if !palette { 85 } else { 1 };
for (i, d) in dest.get_mut(..width)?.iter_mut().enumerate() {
*d = (source[i / 4] >> (6 - i % 4 * 2) & 3) * conv;
}
}
1 => {
let conv = if !palette { 255 } else { 1 };
for (i, d) in dest.get_mut(..width)?.iter_mut().enumerate() {
*d = (source[i / 8] >> (7 - i % 8) & 1) * conv;
}
}
_ => {}
}
Some(())
}
trait Emit {
fn emit(
state: &State,
source: &[u8],
image: &mut [u8],
x: usize,
y: usize,
width: usize,
inc: usize,
len: usize,
) -> Option<()>;
}
struct EmitRgba8;
impl Emit for EmitRgba8 {
fn emit(
state: &State,
source: &[u8],
image: &mut [u8],
x: usize,
y: usize,
width: usize,
inc: usize,
len: usize,
) -> Option<()> {
use ColorType::*;
let src = source;
let mut out = y * width * 4 + x * 4;
let mut i = 0;
match state.header.color_type {
Indexed => {
let palette = state.palette;
let trans = state.trans;
let palette_len = palette.len();
let trans_len = trans.len();
for _ in 0..len {
let t = src[i] as usize;
let p = t * 3;
if p + 2 >= palette_len {
image[out] = 0;
image[out + 1] = 0;
image[out + 2] = 0;
} else {
image[out] = palette[p];
image[out + 1] = palette[p + 1];
image[out + 2] = palette[p + 2];
}
if t >= trans_len {
image[out + 3] = 255;
} else {
image[out + 3] = trans[t];
}
i += 1;
out += 4 * inc;
}
}
TrueColor => {
for _ in 0..len {
image[out] = src[i];
image[out + 1] = src[i + 1];
image[out + 2] = src[i + 2];
image[out + 3] = 255;
i += 3;
out += 4 * inc;
}
}
TrueColorAlpha => {
for _ in 0..len {
image[out] = src[i];
image[out + 1] = src[i + 1];
image[out + 2] = src[i + 2];
image[out + 3] = src[i + 3];
i += 4;
out += 4 * inc;
}
}
Greyscale => {
for c in src[..len].iter().copied() {
image[out] = c;
image[out + 1] = c;
image[out + 2] = c;
image[out + 3] = 255;
out += 4 * inc;
}
}
GreyscaleAlpha => {
let mut i = 0;
for _ in 0..len {
let c = src[i];
image[out] = c;
image[out + 1] = c;
image[out + 2] = c;
image[out + 3] = src[i + 1];
i += 2;
out += 4 * inc;
}
}
}
Some(())
}
}
#[inline(always)]
fn paeth(a: u8, b: u8, c: u8) -> u8 {
let pa = ((b as i32).wrapping_sub(c as i32)).abs();
let pb = ((a as i32).wrapping_sub(c as i32)).abs();
let pc = ((a as i32)
.wrapping_add(b as i32)
.wrapping_sub(c as i32)
.wrapping_sub(c as i32))
.abs();
if pc < pa && pc < pb {
c
} else if pb < pa {
b
} else {
a
}
}
fn get_u32be(buf: &[u8], offset: usize) -> u32 {
(buf[offset] as u32) << 24
| (buf[offset + 1] as u32) << 16
| (buf[offset + 2] as u32) << 8
| buf[offset + 3] as u32
}
const fn chunk_name(bytes: &[u8; 4]) -> u32 {
(bytes[0] as u32) << 24 | (bytes[1] as u32) << 16 | (bytes[2] as u32) << 8 | bytes[3] as u32
}

96
vendor/swash/src/scale/color.rs vendored Normal file
View File

@@ -0,0 +1,96 @@
use super::super::{
palette::{ColorPalette, ColorPalettes},
FontRef, GlyphId,
};
use super::internal::{raw_tag, Bytes, RawFont, RawTag};
const COLR: RawTag = raw_tag(b"COLR");
const CPAL: RawTag = raw_tag(b"CPAL");
#[derive(Copy, Clone, Default)]
pub struct ColorProxy {
pub colr: u32,
pub cpal: u32,
}
impl ColorProxy {
pub fn from_font(font: &FontRef) -> Self {
Self {
colr: font.table_offset(COLR),
cpal: font.table_offset(CPAL),
}
}
pub fn layers<'a>(&self, data: &'a [u8], glyph_id: GlyphId) -> Option<Layers<'a>> {
let b = Bytes::with_offset(data, self.colr as usize)?;
let count = b.read::<u16>(2)? as usize;
let base_offset = b.read::<u32>(4)? as usize;
let mut l = 0;
let mut h = count;
while l < h {
use core::cmp::Ordering::*;
let i = l + (h - l) / 2;
let rec = base_offset + i * 6;
let id = b.read::<u16>(rec)?;
match glyph_id.cmp(&id) {
Less => h = i,
Greater => l = i + 1,
Equal => {
let first = b.read::<u16>(rec + 2)? as usize;
let offset = b.read::<u32>(8)? as usize + first * 4;
let len = b.read::<u16>(rec + 4)?;
return Some(Layers {
data: b,
offset,
len,
});
}
}
}
None
}
// Unused when render feature is disabled.
#[allow(dead_code)]
pub fn palette<'a>(&self, font: &FontRef<'a>, index: u16) -> Option<ColorPalette<'a>> {
if self.cpal != 0 {
ColorPalettes::from_font_and_offset(font, self.cpal).nth(index as usize)
} else {
None
}
}
}
#[derive(Copy, Clone)]
pub struct Layers<'a> {
data: Bytes<'a>,
offset: usize,
len: u16,
}
impl<'a> Layers<'a> {
pub fn len(&self) -> u16 {
self.len
}
pub fn get(&self, index: u16) -> Option<Layer> {
let b = &self.data;
let base = self.offset + index as usize * 4;
let glyph_id = b.read::<u16>(base)?;
let color_index = b.read::<u16>(base + 2)?;
Some(Layer {
glyph_id,
color_index: if color_index != 0xFFFF {
Some(color_index)
} else {
None
},
})
}
}
#[derive(Copy, Clone)]
pub struct Layer {
pub glyph_id: GlyphId,
pub color_index: Option<u16>,
}

95
vendor/swash/src/scale/hinting_cache.rs vendored Normal file
View File

@@ -0,0 +1,95 @@
use alloc::vec::Vec;
use skrifa::{
instance::{NormalizedCoord, Size},
outline::{
HintingInstance, HintingMode, LcdLayout, OutlineGlyphCollection, OutlineGlyphFormat,
},
};
/// We keep this small to enable a simple LRU cache with a linear
/// search. Regenerating hinting data is low to medium cost so it's fine
/// to redo it occasionally.
const MAX_CACHED_HINT_INSTANCES: usize = 8;
pub(crate) struct HintingKey<'a> {
pub id: [u64; 2],
pub outlines: &'a OutlineGlyphCollection<'a>,
pub size: Size,
pub coords: &'a [NormalizedCoord],
}
impl<'a> HintingKey<'a> {
fn new_instance(&self) -> Option<HintingInstance> {
HintingInstance::new(self.outlines, self.size, self.coords, HINTING_MODE).ok()
}
}
const HINTING_MODE: HintingMode = HintingMode::Smooth {
lcd_subpixel: Some(LcdLayout::Horizontal),
preserve_linear_metrics: true,
};
#[derive(Default)]
pub(super) struct HintingCache {
// Split caches for glyf/cff because the instance type can reuse
// internal memory when reconfigured for the same format.
glyf_entries: Vec<HintingEntry>,
cff_entries: Vec<HintingEntry>,
serial: u64,
}
impl HintingCache {
pub(super) fn get(&mut self, key: &HintingKey) -> Option<&HintingInstance> {
let entries = match key.outlines.format()? {
OutlineGlyphFormat::Glyf => &mut self.glyf_entries,
OutlineGlyphFormat::Cff | OutlineGlyphFormat::Cff2 => &mut self.cff_entries,
};
let (entry_ix, is_current) = find_hinting_entry(entries, key)?;
let entry = entries.get_mut(entry_ix)?;
self.serial += 1;
entry.serial = self.serial;
if !is_current {
entry.id = key.id;
entry
.instance
.reconfigure(key.outlines, key.size, key.coords, HINTING_MODE)
.ok()?;
}
Some(&entry.instance)
}
}
struct HintingEntry {
id: [u64; 2],
instance: HintingInstance,
serial: u64,
}
fn find_hinting_entry(entries: &mut Vec<HintingEntry>, key: &HintingKey) -> Option<(usize, bool)> {
let mut found_serial = u64::MAX;
let mut found_index = 0;
for (ix, entry) in entries.iter().enumerate() {
if entry.id == key.id
&& entry.instance.size() == key.size
&& entry.instance.location().coords() == key.coords
{
return Some((ix, true));
}
if entry.serial < found_serial {
found_serial = entry.serial;
found_index = ix;
}
}
if entries.len() < MAX_CACHED_HINT_INSTANCES {
let instance = key.new_instance()?;
let ix = entries.len();
entries.push(HintingEntry {
id: key.id,
instance,
serial: 0,
});
Some((ix, true))
} else {
Some((found_index, false))
}
}

52
vendor/swash/src/scale/image.rs vendored Normal file
View File

@@ -0,0 +1,52 @@
/*!
Rendered glyph image.
*/
use super::Source;
use alloc::vec::Vec;
use zeno::Placement;
/// Content of a scaled glyph image.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Content {
/// 8-bit alpha mask.
Mask,
/// 32-bit RGBA subpixel mask.
SubpixelMask,
/// 32-bit RGBA bitmap.
Color,
}
impl Default for Content {
fn default() -> Self {
Self::Mask
}
}
/// Scaled glyph image.
#[derive(Clone, Default)]
pub struct Image {
/// Source of the image.
pub source: Source,
/// Content of the image.
pub content: Content,
/// Offset and size of the image.
pub placement: Placement,
/// Raw image data.
pub data: Vec<u8>,
}
impl Image {
/// Creates a new empty scaled image.
pub fn new() -> Self {
Self::default()
}
/// Resets the image to a default state.
pub fn clear(&mut self) {
self.source = Source::default();
self.content = Content::default();
self.placement = Placement::default();
self.data.clear();
}
}

993
vendor/swash/src/scale/mod.rs vendored Normal file
View File

@@ -0,0 +1,993 @@
/*!
Scaling, hinting and rasterization of visual glyph representations.
Scaling is the process of generating an appropriately sized visual
representation of a glyph. The scaler can produce rendered glyph
[images](Image) from outlines, layered color outlines and embedded
bitmaps. Alternatively, you can request raw, optionally hinted
[outlines](Outline) that can then be further processed by [zeno] or
fed into other crates like [lyon](https://github.com/nical/lyon) or
[pathfinder](https://github.com/servo/pathfinder) for tessellation and
GPU rendering.
# Building the scaler
All scaling in this crate takes place within the purview of a
[`ScaleContext`]. This opaque struct manages internal LRU caches and scratch
buffers that are necessary for the scaling process. Generally, you'll
want to keep an instance with your glyph cache, or if doing multithreaded
glyph rasterization, one instance per thread.
The only method available on the context is [`builder`](ScaleContext::builder)
which takes a type that can be converted into a [`FontRef`] as an argument
and produces a [`ScalerBuilder`] that provides options for configuring and
building a [`Scaler`].
Here, we'll create a context and build a scaler for a size of 14px with
hinting enabled:
```
# use swash::{FontRef, CacheKey, scale::*};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ScaleContext::new();
let mut scaler = context.builder(font)
.size(14.)
.hint(true)
.build();
```
You can specify variation settings by calling the [`variations`](ScalerBuilder::variations)
method with an iterator that yields a sequence of values that are convertible
to [`Setting<f32>`]. Tuples of (&str, f32) will work in a pinch. For example,
you can request a variation of the weight axis like this:
```
# use swash::{FontRef, CacheKey, scale::*};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ScaleContext::new();
let mut scaler = context.builder(font)
.size(14.)
.hint(true)
.variations(&[("wght", 520.5)])
.build();
```
Alternatively, you can specify variations using the
[`normalized_coords`](ScalerBuilder::normalized_coords) method which takes an iterator
that yields [`NormalizedCoord`]s (a type alias for `i16` which is a fixed point value
in 2.14 format). This method is faster than specifying variations by tag and value, but
the difference is likely negligible outside of microbenchmarks. The real advantage
is that a sequence of `i16` is more compact and easier to fold into a key in a glyph
cache. You can compute these normalized coordinates by using the
[`Variation::normalize`](crate::Variation::normalize) method for each available axis in
the font. The best strategy, however, is to simply capture these during shaping with
the [`Shaper::normalized_coords`](crate::shape::Shaper::normalized_coords) method which
will have already computed them for you.
See [`ScalerBuilder`] for available options and default values.
# Outlines and bitmaps
The [`Scaler`] struct essentially provides direct access to the outlines and embedded
bitmaps that are available in the font. In the case of outlines, it can produce the
raw outline in font units or an optionally hinted, scaled outline. For example, to
extract the raw outline for the letter 'Q':
```
# use swash::{FontRef, CacheKey, scale::*};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ScaleContext::new();
let mut scaler = context.builder(font).build();
let glyph_id = font.charmap().map('Q');
let outline = scaler.scale_outline(glyph_id);
```
For the same, but hinted at 12px:
```
# use swash::{FontRef, CacheKey, scale::*};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ScaleContext::new();
let mut scaler = context.builder(font)
.hint(true)
.size(12.)
.build();
let glyph_id = font.charmap().map('Q');
let outline = scaler.scale_outline(glyph_id);
```
The [`scale_outline`](Scaler::scale_outline) method returns an [`Outline`] wrapped
in an option. It will return `None` if an outline was not available or if there was
an error during the scaling process. Note that
[`scale_color_outline`](Scaler::scale_color_outline) can be used to access layered
color outlines such as those included in the Microsoft _Segoe UI Emoji_ font. Finally,
the `_into` variants of these methods ([`scale_outline_into`](Scaler::scale_outline_into)
and [`scale_color_outline_into`](Scaler::scale_color_outline_into)) will return
their results in a previously allocated outline avoiding the extra allocations.
Similar to outlines, bitmaps can be retrieved with the [`scale_bitmap`](Scaler::scale_bitmap)
and [`scale_color_bitmap`](Scaler::scale_color_bitmap) for alpha and color bitmaps,
respectively. These methods return an [`Image`] wrapped in an option. The associated
`_into` variants are also available.
Unlike outlines, bitmaps are available in [`strike`](crate::BitmapStrike)s of various sizes.
When requesting a bitmap, you specify the strategy for strike selection using the
[`StrikeWith`] enum.
For example, if we want the largest available unscaled image for the fire emoji:
```
# use swash::{FontRef, CacheKey, scale::*};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ScaleContext::new();
let mut scaler = context.builder(font).build();
let glyph_id = font.charmap().map('🔥');
let image = scaler.scale_color_bitmap(glyph_id, StrikeWith::LargestSize);
```
Or, to produce a scaled image for a size of 18px:
```
# use swash::{FontRef, CacheKey, scale::*};
# let font: FontRef = FontRef { data: &[], offset: 0, key: CacheKey::new() };
// let font = ...;
let mut context = ScaleContext::new();
let mut scaler = context.builder(font)
.size(18.)
.build();
let glyph_id = font.charmap().map('🔥');
let image = scaler.scale_color_bitmap(glyph_id, StrikeWith::BestFit);
```
This will select the best strike for the requested size and return
a bitmap that is scaled appropriately for an 18px run of text.
Alpha bitmaps should generally be avoided unless you're rendering small East
Asian text where these are sometimes still preferred over scalable outlines. In
this case, you should only use [`StrikeWith::ExactSize`] to select the strike,
falling back to an outline if a bitmap is unavailable.
# Rendering
In the general case of text rendering, you'll likely not care about the specific
details of outlines or bitmaps and will simply want an appropriately sized
image that represents your glyph. For this purpose, you'll want to use the
[`Render`] struct which is a builder that provides options for rendering an image.
This struct is constructed with a slice of [`Source`]s in priority order and
will iterate through them until it finds one that satisfies the request. Typically,
you'll want to use the following order:
```
# use swash::scale::*;
Render::new(&[
// Color outline with the first palette
Source::ColorOutline(0),
// Color bitmap with best fit selection mode
Source::ColorBitmap(StrikeWith::BestFit),
// Standard scalable outline
Source::Outline,
]);
```
The [`Render`] struct offers several options that control rasterization of
outlines such as [`format`](Render::format) for selecting a subpixel rendering mode,
[`offset`](Render::offset) for applying fractional positioning, and others. See the
struct documentation for detail.
After selecting your options, call the [`render`](Render::render) method, passing your
configured [`Scaler`] and the requested glyph identifier to produce an [`Image`].
Let's put it all together by writing a simple function that will render subpixel glyphs
with fractional positioning:
```
# use swash::{scale::{*, image::Image}, FontRef, GlyphId};
fn render_glyph(
context: &mut ScaleContext,
font: &FontRef,
size: f32,
hint: bool,
glyph_id: GlyphId,
x: f32,
y: f32,
) -> Option<Image> {
use zeno::{Format, Vector};
// Build the scaler
let mut scaler = context.builder(*font).size(size).hint(hint).build();
// Compute the fractional offset-- you'll likely want to quantize this
// in a real renderer
let offset = Vector::new(x.fract(), y.fract());
// Select our source order
Render::new(&[
Source::ColorOutline(0),
Source::ColorBitmap(StrikeWith::BestFit),
Source::Outline,
])
// Select a subpixel format
.format(Format::Subpixel)
// Apply the fractional offset
.offset(offset)
// Render the image
.render(&mut scaler, glyph_id)
}
```
Note that rendering also takes care of correctly scaling, rasterizing and
compositing layered color outlines for us.
There are other options available for emboldening, transforming with an
affine matrix, and applying path effects. See the methods on [`Render`] for
more detail.
*/
pub mod image;
pub mod outline;
mod bitmap;
mod color;
mod hinting_cache;
mod proxy;
use hinting_cache::HintingCache;
use image::*;
use outline::*;
use skrifa::{
instance::{NormalizedCoord as SkrifaNormalizedCoord, Size as SkrifaSize},
outline::OutlineGlyphCollection,
GlyphId as SkrifaGlyphId, MetadataProvider,
};
use super::internal;
use super::{cache::FontCache, setting::Setting, FontRef, GlyphId, NormalizedCoord};
use alloc::vec::Vec;
use core::borrow::Borrow;
#[cfg(all(feature = "libm", feature = "render"))]
use core_maths::CoreFloat;
use proxy::*;
use zeno::Placement;
#[cfg(feature = "render")]
use zeno::{Format, Mask, Origin, Point, Scratch, Style, Transform, Vector};
pub(crate) use bitmap::decode_png;
/// Index of a color palette.
pub type PaletteIndex = u16;
/// Index of a bitmap strike.
pub type StrikeIndex = u32;
/// Bitmap strike selection mode.
#[derive(Copy, Clone, Debug)]
pub enum StrikeWith {
/// Load a bitmap only if the exact size is available.
ExactSize,
/// Load a bitmap of the best available size.
BestFit,
/// Loads a bitmap of the largest size available.
LargestSize,
/// Load a bitmap from the specified strike.
Index(StrikeIndex),
}
/// Glyph sources for the renderer.
#[derive(Copy, Clone, Debug)]
pub enum Source {
/// Scalable outlines.
Outline,
/// Layered color scalable outlines.
ColorOutline(PaletteIndex),
/// Embedded alpha bitmaps.
Bitmap(StrikeWith),
/// Embedded color bitmaps.
ColorBitmap(StrikeWith),
}
impl Default for Source {
fn default() -> Self {
Self::Outline
}
}
/// Context that manages caches and scratch buffers for scaling.
///
/// See the module level [documentation](index.html#building-the-scaler) for detail.
pub struct ScaleContext {
fonts: FontCache<ScalerProxy>,
state: State,
hinting_cache: HintingCache,
coords: Vec<SkrifaNormalizedCoord>,
}
struct State {
scratch0: Vec<u8>,
scratch1: Vec<u8>,
outline: Outline,
#[cfg(feature = "render")]
rcx: Scratch,
}
impl ScaleContext {
/// Creates a new scaling context.
pub fn new() -> Self {
Self::with_max_entries(8)
}
/// Creates a new scaling context with the specified maximum number of
/// cache entries.
pub fn with_max_entries(max_entries: usize) -> Self {
let max_entries = max_entries.clamp(1, 64);
Self {
fonts: FontCache::new(max_entries),
state: State {
scratch0: Vec::new(),
scratch1: Vec::new(),
outline: Outline::new(),
#[cfg(feature = "render")]
rcx: Scratch::new(),
},
hinting_cache: HintingCache::default(),
coords: Vec::new(),
}
}
/// Creates a new builder for constructing a scaler with this context
/// and the specified font.
pub fn builder<'a>(&'a mut self, font: impl Into<FontRef<'a>>) -> ScalerBuilder<'a> {
ScalerBuilder::new(self, font, None)
}
/// Creates a new builder for constructing a scaler with this context,
/// specified font and a custom unique identifier.
pub fn builder_with_id<'a>(
&'a mut self,
font: impl Into<FontRef<'a>>,
id: [u64; 2],
) -> ScalerBuilder<'a> {
ScalerBuilder::new(self, font, Some(id))
}
}
impl Default for ScaleContext {
fn default() -> Self {
Self::new()
}
}
/// Builder for configuring a scaler.
pub struct ScalerBuilder<'a> {
state: &'a mut State,
hinting_cache: &'a mut HintingCache,
font: FontRef<'a>,
outlines: Option<OutlineGlyphCollection<'a>>,
proxy: &'a ScalerProxy,
id: [u64; 2],
coords: &'a mut Vec<SkrifaNormalizedCoord>,
size: f32,
hint: bool,
}
impl<'a> ScalerBuilder<'a> {
fn new(
context: &'a mut ScaleContext,
font: impl Into<FontRef<'a>>,
id: Option<[u64; 2]>,
) -> Self {
let font = font.into();
let (id, proxy) = context.fonts.get(&font, id, ScalerProxy::from_font);
let skrifa_font = if font.offset == 0 {
skrifa::FontRef::new(font.data).ok()
} else {
// TODO: make this faster
let index = crate::FontDataRef::new(font.data)
.and_then(|font_data| font_data.fonts().position(|f| f.offset == font.offset));
index.and_then(|index| skrifa::FontRef::from_index(font.data, index as u32).ok())
};
let outlines = skrifa_font.map(|font_ref| font_ref.outline_glyphs());
Self {
state: &mut context.state,
hinting_cache: &mut context.hinting_cache,
font,
outlines,
proxy,
id,
coords: &mut context.coords,
size: 0.,
hint: false,
}
}
/// Specifies the font size in pixels per em. The default value is `0` which will produce
/// unscaled glyphs in original font units.
pub fn size(mut self, ppem: f32) -> Self {
self.size = ppem.max(0.);
self
}
/// Specifies whether to apply hinting to outlines. The default value is `false`.
pub fn hint(mut self, yes: bool) -> Self {
self.hint = yes;
self
}
/// Adds variation settings to the scaler.
pub fn variations<I>(self, settings: I) -> Self
where
I: IntoIterator,
I::Item: Into<Setting<f32>>,
{
if self.proxy.coord_count != 0 {
let vars = self.font.variations();
self.coords.resize(vars.len(), Default::default());
for setting in settings {
let setting = setting.into();
for var in vars {
if var.tag() == setting.tag {
let value = var.normalize(setting.value);
if let Some(c) = self.coords.get_mut(var.index()) {
*c = SkrifaNormalizedCoord::from_bits(value);
}
}
}
}
}
self
}
/// Specifies the variation settings in terms of normalized coordinates. This will replace
/// any previous variation settings.
pub fn normalized_coords<I>(self, coords: I) -> Self
where
I: IntoIterator,
I::Item: Borrow<NormalizedCoord>,
{
self.coords.clear();
self.coords.extend(
coords
.into_iter()
.map(|c| SkrifaNormalizedCoord::from_bits(*c.borrow())),
);
self
}
/// Builds a scaler for the current configuration.
pub fn build(self) -> Scaler<'a> {
let upem = self.proxy.metrics.units_per_em();
let skrifa_size = if self.size != 0.0 && upem != 0 {
SkrifaSize::new(self.size)
} else {
SkrifaSize::unscaled()
};
let hinting_instance = match (self.hint, &self.outlines) {
(true, Some(outlines)) => {
let key = hinting_cache::HintingKey {
id: self.id,
outlines,
size: skrifa_size,
coords: self.coords,
};
self.hinting_cache.get(&key)
}
_ => None,
};
Scaler {
state: self.state,
font: self.font,
outlines: self.outlines,
hinting_instance,
proxy: self.proxy,
coords: &self.coords[..],
size: self.size,
skrifa_size,
}
}
}
/// Scales outline and bitmap glyphs.
///
/// See the module level [documentation](index.html#outlines-and-bitmaps) for detail.
pub struct Scaler<'a> {
state: &'a mut State,
font: FontRef<'a>,
outlines: Option<OutlineGlyphCollection<'a>>,
hinting_instance: Option<&'a skrifa::outline::HintingInstance>,
proxy: &'a ScalerProxy,
coords: &'a [SkrifaNormalizedCoord],
size: f32,
skrifa_size: SkrifaSize,
}
impl<'a> Scaler<'a> {
/// Returns true if scalable glyph outlines are available.
pub fn has_outlines(&self) -> bool {
self.outlines
.as_ref()
.map(|outlines| outlines.format().is_some())
.unwrap_or_default()
}
/// Scales an outline for the specified glyph into the provided outline.
pub fn scale_outline_into(&mut self, glyph_id: GlyphId, outline: &mut Outline) -> bool {
outline.clear();
self.scale_outline_impl(glyph_id, None, Some(outline))
}
/// Scales an outline for the specified glyph.
pub fn scale_outline(&mut self, glyph_id: GlyphId) -> Option<Outline> {
let mut outline = Outline::new();
if self.scale_outline_into(glyph_id, &mut outline) {
Some(outline)
} else {
None
}
}
/// Returns true if scalable color glyph outlines are available.
pub fn has_color_outlines(&self) -> bool {
self.proxy.color.colr != 0 && self.proxy.color.cpal != 0
}
/// Scales a color outline for the specified glyph into the provided outline.
pub fn scale_color_outline_into(&mut self, glyph_id: GlyphId, outline: &mut Outline) -> bool {
outline.clear();
if !self.has_color_outlines() {
return false;
}
let layers = match self.proxy.color.layers(self.font.data, glyph_id) {
Some(layers) => layers,
_ => return false,
};
for i in 0..layers.len() {
let layer = match layers.get(i) {
Some(layer) => layer,
_ => return false,
};
if !self.scale_outline_impl(layer.glyph_id, layer.color_index, Some(outline)) {
return false;
}
}
outline.set_color(true);
true
}
/// Scales a color outline for the specified glyph.
pub fn scale_color_outline(&mut self, glyph_id: GlyphId) -> Option<Outline> {
let mut outline = Outline::new();
if self.scale_color_outline_into(glyph_id, &mut outline) {
Some(outline)
} else {
None
}
}
fn scale_outline_impl(
&mut self,
glyph_id: GlyphId,
color_index: Option<u16>,
outline: Option<&mut Outline>,
) -> bool {
let mut outline = match outline {
Some(x) => x,
_ => &mut self.state.outline,
};
if let Some(outlines) = &self.outlines {
if let Some(glyph) = outlines.get(SkrifaGlyphId::from(glyph_id)) {
outline.begin_layer(color_index);
let settings: skrifa::outline::DrawSettings =
if let Some(hinting_instance) = &self.hinting_instance {
(*hinting_instance).into()
} else {
(
self.skrifa_size,
skrifa::instance::LocationRef::new(self.coords),
)
.into()
};
if glyph
.draw(settings, &mut OutlineWriter(&mut outline))
.is_ok()
{
outline.maybe_close();
outline.finish();
return true;
}
}
}
false
}
// Unused when render feature is disabled.
#[allow(dead_code)]
fn scale_color_outline_impl(&mut self, glyph_id: GlyphId) -> bool {
if !self.has_color_outlines() {
return false;
}
let layers = match self.proxy.color.layers(self.font.data, glyph_id) {
Some(layers) => layers,
_ => return false,
};
self.state.outline.clear();
for i in 0..layers.len() {
let layer = match layers.get(i) {
Some(layer) => layer,
_ => return false,
};
if !self.scale_outline_impl(layer.glyph_id, layer.color_index, None) {
return false;
}
}
true
}
/// Returns true if alpha bitmaps are available.
pub fn has_bitmaps(&self) -> bool {
self.proxy.bitmaps.has_alpha()
}
/// Scales a bitmap for the specified glyph and mode into the provided image.
pub fn scale_bitmap_into(
&mut self,
glyph_id: u16,
strike: StrikeWith,
image: &mut Image,
) -> bool {
self.scale_bitmap_impl(glyph_id, false, strike, image) == Some(true)
}
/// Scales a bitmap for the specified glyph and mode.
pub fn scale_bitmap(&mut self, glyph_id: u16, strike: StrikeWith) -> Option<Image> {
let mut image = Image::new();
if self.scale_bitmap_into(glyph_id, strike, &mut image) {
Some(image)
} else {
None
}
}
/// Returns true if color bitmaps are available.
pub fn has_color_bitmaps(&self) -> bool {
self.proxy.bitmaps.has_color()
}
/// Scales a color bitmap for the specified glyph and mode into the provided image.
pub fn scale_color_bitmap_into(
&mut self,
glyph_id: u16,
strike: StrikeWith,
image: &mut Image,
) -> bool {
self.scale_bitmap_impl(glyph_id, true, strike, image) == Some(true)
}
/// Scales a color bitmap for the specified glyph and mode.
pub fn scale_color_bitmap(&mut self, glyph_id: u16, strike: StrikeWith) -> Option<Image> {
let mut image = Image::new();
if self.scale_color_bitmap_into(glyph_id, strike, &mut image) {
Some(image)
} else {
None
}
}
fn scale_bitmap_impl(
&mut self,
glyph_id: GlyphId,
color: bool,
strike: StrikeWith,
image: &mut Image,
) -> Option<bool> {
image.clear();
let size = self.size;
let mut strikes = if color {
self.proxy.bitmaps.materialize_color(&self.font)
} else {
self.proxy.bitmaps.materialize_alpha(&self.font)
};
let bitmap = match strike {
StrikeWith::ExactSize => {
if self.size == 0. {
None
} else {
strikes
.find_by_exact_ppem(size as u16, glyph_id)?
.get(glyph_id)
}
}
StrikeWith::BestFit => {
if self.size == 0. {
None
} else {
strikes
.find_by_nearest_ppem(size as u16, glyph_id)?
.get(glyph_id)
}
}
StrikeWith::LargestSize => strikes.find_by_largest_ppem(glyph_id)?.get(glyph_id),
StrikeWith::Index(i) => strikes
.nth(i as usize)
.and_then(|strike| strike.get(glyph_id)),
}?;
if bitmap.ppem == 0 {
return None;
}
let (_, _, bufsize) = bitmap.scaled_size(size);
image.data.resize(bufsize, 0);
self.state.scratch0.clear();
self.state.scratch1.clear();
let mut w = bitmap.width;
let mut h = bitmap.height;
let scale = size / bitmap.ppem as f32;
image.placement = if size != 0. && scale != 1. {
self.state
.scratch0
.resize(bitmap.format.buffer_size(w, h), 0);
w = (w as f32 * scale) as u32;
h = (h as f32 * scale) as u32;
image.data.resize(bitmap.format.buffer_size(w, h), 0);
if !bitmap.decode(Some(&mut self.state.scratch1), &mut self.state.scratch0) {
return None;
}
if !bitmap::resize(
&self.state.scratch0,
bitmap.width,
bitmap.height,
bitmap.format.channels(),
&mut image.data,
w,
h,
bitmap::Filter::Mitchell,
Some(&mut self.state.scratch1),
) {
return None;
}
let left = (bitmap.left as f32 * scale) as i32;
let top = (bitmap.top as f32 * scale) as i32;
Placement {
left,
top,
width: w,
height: h,
}
} else {
image.data.resize(bitmap.format.buffer_size(w, h), 0);
if !bitmap.decode(Some(&mut self.state.scratch1), &mut image.data) {
return None;
}
Placement {
left: bitmap.left,
top: bitmap.top,
width: w,
height: h,
}
};
image.source = match color {
true => Source::ColorBitmap(strike),
false => Source::Bitmap(strike),
};
image.content = match bitmap.format.channels() {
1 => Content::Mask,
_ => Content::Color,
};
// let mut advance = bitmap.advance() as f32;
// if options.size != 0. && options.size as u16 != bitmap.ppem() {
// advance *= options.size / bitmap.ppem() as f32;
// }
Some(true)
}
}
/// Builder type for rendering a glyph into an image.
///
/// See the module level [documentation](index.html#rendering) for detail.
#[cfg(feature = "render")]
pub struct Render<'a> {
sources: &'a [Source],
format: Format,
offset: Point,
transform: Option<Transform>,
embolden: f32,
foreground: [u8; 4],
style: Style<'a>,
}
#[cfg(feature = "render")]
impl<'a> Render<'a> {
/// Creates a new builder for configuring rendering using the specified
/// prioritized list of sources.
pub fn new(sources: &'a [Source]) -> Self {
Self {
sources,
format: Format::Alpha,
offset: Point::new(0., 0.),
transform: None,
embolden: 0.,
foreground: [128, 128, 128, 255],
style: Style::default(),
}
}
/// Specifies the target format for rasterizing an outline. Default is
/// [`Format::Alpha`].
pub fn format(&mut self, format: Format) -> &mut Self {
self.format = format;
self
}
/// Specifies the path style to use when rasterizing an outline. Default is
/// [`Fill::NonZero`](zeno::Fill::NonZero).
pub fn style(&mut self, style: impl Into<Style<'a>>) -> &mut Self {
self.style = style.into();
self
}
/// Specifies an additional offset to apply when rasterizing an outline.
/// Default is `(0, 0)`.
pub fn offset(&mut self, offset: Vector) -> &mut Self {
self.offset = offset;
self
}
/// Specifies a transformation matrix to apply when rasterizing an
/// outline. Default is `None`.
pub fn transform(&mut self, transform: Option<Transform>) -> &mut Self {
self.transform = transform;
self
}
/// Specifies the strength of a faux bold transform to apply when
/// rasterizing an outline. Default is `0`.
pub fn embolden(&mut self, strength: f32) -> &mut Self {
self.embolden = strength;
self
}
/// Specifies an RGBA color to use when rasterizing layers of a color
/// outline that do not directly reference a palette color. Default is
/// `[128, 128, 128, 255]`.
pub fn default_color(&mut self, color: [u8; 4]) -> &mut Self {
self.foreground = color;
self
}
/// Renders the specified glyph using the current configuration into the
/// provided image.
pub fn render_into(&self, scaler: &mut Scaler, glyph_id: GlyphId, image: &mut Image) -> bool {
for source in self.sources {
match source {
Source::Outline => {
if !scaler.has_outlines() {
continue;
}
scaler.state.outline.clear();
if scaler.scale_outline_impl(glyph_id, None, None) {
let state = &mut scaler.state;
let rcx = &mut state.rcx;
let outline = &mut state.outline;
if self.embolden != 0. {
outline.embolden(self.embolden, self.embolden);
}
if let Some(transform) = &self.transform {
outline.transform(transform);
}
let placement = Mask::with_scratch(outline.path(), rcx)
.format(self.format)
.origin(Origin::BottomLeft)
.style(self.style)
.offset(self.offset)
.render_offset(self.offset)
.inspect(|fmt, w, h| {
image.data.resize(fmt.buffer_size(w, h), 0);
})
.render_into(&mut image.data[..], None);
image.placement = placement;
image.content = if self.format == Format::Alpha {
Content::Mask
} else {
Content::SubpixelMask
};
image.source = Source::Outline;
return true;
}
}
Source::ColorOutline(palette_index) => {
if !scaler.has_color_outlines() {
continue;
}
scaler.state.outline.clear();
if scaler.scale_color_outline_impl(glyph_id) {
let font = &scaler.font;
let proxy = &scaler.proxy;
let state = &mut scaler.state;
let scratch = &mut state.scratch0;
let rcx = &mut state.rcx;
let outline = &mut state.outline;
// Cool effect, but probably not generally desirable.
// Maybe expose a separate option?
// if self.embolden != 0. {
// outline.embolden(self.embolden, self.embolden);
// }
if let Some(transform) = &self.transform {
outline.transform(transform);
}
let palette = proxy.color.palette(font, *palette_index);
let total_bounds = outline.bounds();
// need to take offset into account when placing glyph
let base_x = (total_bounds.min.x + self.offset.x).floor() as i32;
let base_y = (total_bounds.min.y + self.offset.y).ceil() as i32;
let base_w = total_bounds.width().ceil() as u32;
let base_h = total_bounds.height().ceil() as u32;
image.data.resize((base_w * base_h * 4) as usize, 0);
image.placement.left = base_x;
image.placement.top = base_h as i32 + base_y;
image.placement.width = total_bounds.width().ceil() as u32;
image.placement.height = total_bounds.height().ceil() as u32;
let mut ok = true;
for i in 0..outline.len() {
let layer = match outline.get(i) {
Some(layer) => layer,
_ => {
ok = false;
break;
}
};
scratch.clear();
let placement = Mask::with_scratch(layer.path(), rcx)
.origin(Origin::BottomLeft)
.style(self.style)
.offset(self.offset)
.render_offset(self.offset)
.inspect(|fmt, w, h| {
scratch.resize(fmt.buffer_size(w, h), 0);
})
.render_into(&mut scratch[..], None);
let color = layer
.color_index()
.and_then(|i| palette.map(|p| p.get(i)))
.unwrap_or(self.foreground);
bitmap::blit(
&scratch[..],
placement.width,
placement.height,
placement.left.wrapping_sub(base_x),
(base_h as i32 + base_y).wrapping_sub(placement.top),
color,
&mut image.data,
base_w,
base_h,
);
}
if ok {
image.source = Source::ColorOutline(*palette_index);
image.content = Content::Color;
return true;
}
}
}
Source::Bitmap(mode) => {
if !scaler.has_bitmaps() {
continue;
}
if scaler.scale_bitmap_into(glyph_id, *mode, image) {
return true;
}
}
Source::ColorBitmap(mode) => {
if !scaler.has_color_bitmaps() {
continue;
}
if scaler.scale_color_bitmap_into(glyph_id, *mode, image) {
return true;
}
}
}
}
false
}
/// Renders the specified glyph using the current configuration.
pub fn render(&self, scaler: &mut Scaler, glyph_id: GlyphId) -> Option<Image> {
let mut image = Image::new();
if self.render_into(scaler, glyph_id, &mut image) {
Some(image)
} else {
None
}
}
}

427
vendor/swash/src/scale/outline.rs vendored Normal file
View File

@@ -0,0 +1,427 @@
/*!
Glyph outline.
*/
use alloc::vec::Vec;
use zeno::{Bounds, PathData, Point, Transform, Verb};
/// Scaled glyph outline represented as a collection of layers and a sequence
/// of points and verbs.
#[derive(Clone, Default)]
pub struct Outline {
layers: Vec<LayerData>,
points: Vec<Point>,
verbs: Vec<Verb>,
is_color: bool,
}
impl Outline {
/// Creates a new empty outline.
pub fn new() -> Self {
Self::default()
}
/// Returns true if the outline has color layers.
pub fn is_color(&self) -> bool {
self.is_color
}
/// Returns the number of layers in the outline.
pub fn len(&self) -> usize {
self.layers.len()
}
/// Returns true if there are no layers in the outline.
pub fn is_empty(&self) -> bool {
self.layers.is_empty()
}
/// Returns a reference to the layer at the specified index.
pub fn get<'a>(&'a self, index: usize) -> Option<Layer<'a>> {
let data = self.layers.get(index)?;
let points = self.points.get(data.points.0..data.points.1)?;
let verbs = self.verbs.get(data.verbs.0..data.verbs.1)?;
let color_index = data.color_index;
Some(Layer {
points,
verbs,
color_index,
})
}
/// Returns a mutable reference to the layer at the specified index.
pub fn get_mut<'a>(&'a mut self, index: usize) -> Option<LayerMut<'a>> {
let data = self.layers.get(index)?;
let points = self.points.get_mut(data.points.0..data.points.1)?;
let verbs = self.verbs.get(data.verbs.0..data.verbs.1)?;
let color_index = data.color_index;
Some(LayerMut {
points,
verbs,
color_index,
})
}
/// Returns a reference to the sequence of points in the outline.
pub fn points(&self) -> &[Point] {
&self.points
}
/// Returns a mutable reference to the sequence of points in the outline.
pub fn points_mut(&mut self) -> &mut [Point] {
&mut self.points
}
/// Returns a reference to the sequence of verbs in the outline.
pub fn verbs(&self) -> &[Verb] {
&self.verbs
}
/// Returns path data for the outline.
pub fn path(&self) -> impl PathData + '_ {
(&self.points[..], &self.verbs[..])
}
/// Computes the bounding box of the outline.
pub fn bounds(&self) -> Bounds {
Bounds::from_points(&self.points)
}
/// Transforms the outline by the specified matrix.
pub fn transform(&mut self, transform: &Transform) {
for p in &mut self.points {
*p = transform.transform_point(*p);
}
}
/// Applies a faux bold to the outline with the specified strengths in the
/// x and y directions.
pub fn embolden(&mut self, x_strength: f32, y_strength: f32) {
for i in 0..self.len() {
if let Some(mut layer) = self.get_mut(i) {
layer.embolden(x_strength, y_strength);
}
}
}
/// Clears the outline.
pub fn clear(&mut self) {
self.points.clear();
self.verbs.clear();
self.layers.clear();
self.is_color = false;
}
}
/// Reference to a layer in a scaled outline.
#[derive(Copy, Clone)]
pub struct Layer<'a> {
points: &'a [Point],
verbs: &'a [Verb],
color_index: Option<u16>,
}
impl<'a> Layer<'a> {
/// Returns the sequence of points for the layer.
pub fn points(&self) -> &'a [Point] {
self.points
}
/// Returns the sequence of verbs for the layer.
pub fn verbs(&self) -> &'a [Verb] {
self.verbs
}
/// Returns path data for the layer.
pub fn path(&self) -> impl PathData + 'a {
(self.points(), self.verbs())
}
/// Computes the bounding box of the layer.
pub fn bounds(&self) -> Bounds {
Bounds::from_points(self.points())
}
/// Returns the color index for the layer.
pub fn color_index(&self) -> Option<u16> {
self.color_index
}
}
/// Mutable reference to a layer in a scaled outline.
pub struct LayerMut<'a> {
points: &'a mut [Point],
verbs: &'a [Verb],
color_index: Option<u16>,
}
impl<'a> LayerMut<'a> {
/// Returns the sequence of points for the layer.
pub fn points(&'a self) -> &'a [Point] {
self.points
}
/// Returns a mutable reference the sequence of points for the layer.
pub fn points_mut(&'a mut self) -> &'a mut [Point] {
&mut *self.points
}
/// Returns the sequence of verbs for the layer.
pub fn verbs(&self) -> &'a [Verb] {
self.verbs
}
/// Returns path data for the layer.
pub fn path(&'a self) -> impl PathData + 'a {
(self.points(), self.verbs())
}
/// Computes the bounding box of the layer.
pub fn bounds(&self) -> Bounds {
Bounds::from_points(self.points())
}
/// Returns the color index for the layer.
pub fn color_index(&self) -> Option<u16> {
self.color_index
}
/// Transforms this layer by the specified matrix.
pub fn transform(&'a mut self, transform: &Transform) {
for p in self.points.iter_mut() {
*p = transform.transform_point(*p);
}
}
/// Applies a faux bold to this layer with the specified strengths in the
/// x and y directions.
pub fn embolden(&mut self, x_strength: f32, y_strength: f32) {
let mut point_start = 0;
let mut pos = 0;
let winding = compute_winding(self.points);
for verb in self.verbs {
match verb {
Verb::MoveTo | Verb::Close => {
if let Some(points) = self.points.get_mut(point_start..pos) {
if !points.is_empty() {
embolden(points, winding, x_strength, y_strength);
}
point_start = pos;
if *verb == Verb::MoveTo {
pos += 1;
}
} else {
return;
}
}
Verb::LineTo => pos += 1,
Verb::QuadTo => pos += 2,
Verb::CurveTo => pos += 3,
}
}
if pos > point_start {
if let Some(points) = self.points.get_mut(point_start..pos) {
embolden(points, winding, x_strength, y_strength);
}
}
}
}
#[derive(Copy, Clone, Default)]
struct LayerData {
points: (usize, usize),
verbs: (usize, usize),
color_index: Option<u16>,
}
impl Outline {
pub(super) fn set_color(&mut self, color: bool) {
self.is_color = color;
}
pub(super) fn move_to(&mut self, p: Point) {
self.maybe_close();
self.points.push(p);
self.verbs.push(Verb::MoveTo);
}
pub(super) fn line_to(&mut self, p: Point) {
self.points.push(p);
self.verbs.push(Verb::LineTo);
}
pub(super) fn quad_to(&mut self, p0: Point, p1: Point) {
self.points.push(p0);
self.points.push(p1);
self.verbs.push(Verb::QuadTo);
}
pub(super) fn curve_to(&mut self, p0: Point, p1: Point, p2: Point) {
self.points.push(p0);
self.points.push(p1);
self.points.push(p2);
self.verbs.push(Verb::CurveTo);
}
pub(super) fn close(&mut self) {
self.verbs.push(Verb::Close);
}
pub(super) fn maybe_close(&mut self) {
if !self.verbs.is_empty() && self.verbs.last() != Some(&Verb::Close) {
self.close();
}
}
pub(super) fn begin_layer(&mut self, color_index: Option<u16>) {
let points_end = self.points.len();
let verbs_end = self.verbs.len();
if let Some(last) = self.layers.last_mut() {
last.points.1 = points_end;
last.verbs.1 = verbs_end;
}
self.layers.push(LayerData {
points: (points_end, points_end),
verbs: (verbs_end, verbs_end),
color_index,
});
}
pub(super) fn finish(&mut self) {
let points_end = self.points.len();
let verbs_end = self.verbs.len();
if let Some(last) = self.layers.last_mut() {
last.points.1 = points_end;
last.verbs.1 = verbs_end;
} else {
self.layers.push(LayerData {
points: (0, points_end),
verbs: (0, verbs_end),
color_index: None,
});
}
}
}
fn embolden(points: &mut [Point], winding: u8, x_strength: f32, y_strength: f32) {
if points.is_empty() {
return;
}
let last = points.len() - 1;
let mut i = last;
let mut j = 0;
let mut k = !0;
let mut out_len;
let mut in_len = 0.;
let mut anchor_len = 0.;
let mut anchor = Point::ZERO;
let mut out;
let mut in_ = Point::ZERO;
while j != i && i != k {
if j != k {
out = points[j] - points[i];
out_len = out.length();
if out_len == 0. {
j = if j < last { j + 1 } else { 0 };
continue;
} else {
let s = 1. / out_len;
out.x *= s;
out.y *= s;
}
} else {
out = anchor;
out_len = anchor_len;
}
if in_len != 0. {
if k == !0 {
k = i;
anchor = in_;
anchor_len = in_len;
}
let mut d = (in_.x * out.x) + (in_.y * out.y);
let shift = if d > -0.9396 {
d += 1.;
let mut sx = in_.y + out.y;
let mut sy = in_.x + out.x;
if winding == 0 {
sx = -sx;
} else {
sy = -sy;
}
let mut q = (out.x * in_.y) - (out.y * in_.x);
if winding == 0 {
q = -q;
}
let l = in_len.min(out_len);
if x_strength * q <= l * d {
sx = sx * x_strength / d;
} else {
sx = sx * l / q;
}
if y_strength * q <= l * d {
sy = sy * y_strength / d;
} else {
sy = sy * l / q;
}
Point::new(sx, sy)
} else {
Point::ZERO
};
while i != j {
points[i].x += x_strength + shift.x;
points[i].y += y_strength + shift.y;
i = if i < last { i + 1 } else { 0 };
}
} else {
i = j;
}
in_ = out;
in_len = out_len;
j = if j < last { j + 1 } else { 0 };
}
}
fn compute_winding(points: &[Point]) -> u8 {
if points.is_empty() {
return 0;
}
let mut area = 0.;
let last = points.len() - 1;
let mut prev = points[last];
for cur in points[0..=last].iter() {
area += (cur.y - prev.y) * (cur.x + prev.x);
prev = *cur;
}
if area > 0. {
1
} else {
0
}
}
// The OutlineWriter wrapper allows us to make the trait implementation of OutlinePen
// private which allows us to keep skrifa as a private dependency of swash
pub(crate) struct OutlineWriter<'a>(pub &'a mut Outline);
impl skrifa::outline::OutlinePen for OutlineWriter<'_> {
fn move_to(&mut self, x: f32, y: f32) {
self.0.move_to((x, y).into());
}
fn line_to(&mut self, x: f32, y: f32) {
self.0.line_to((x, y).into());
}
fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {
self.0.quad_to((cx0, cy0).into(), (x, y).into());
}
fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
self.0
.curve_to((cx0, cy0).into(), (cx1, cy1).into(), (x, y).into());
}
fn close(&mut self) {
self.0.close();
}
}

23
vendor/swash/src/scale/proxy.rs vendored Normal file
View File

@@ -0,0 +1,23 @@
use super::{
super::{metrics::MetricsProxy, strike::BitmapStrikesProxy, FontRef},
color::ColorProxy,
};
#[derive(Copy, Clone)]
pub struct ScalerProxy {
pub metrics: MetricsProxy,
pub color: ColorProxy,
pub bitmaps: BitmapStrikesProxy,
pub coord_count: u16,
}
impl ScalerProxy {
pub fn from_font(font: &FontRef) -> Self {
Self {
metrics: MetricsProxy::from_font(font),
color: ColorProxy::from_font(font),
bitmaps: BitmapStrikesProxy::from_font(font),
coord_count: font.variations().len() as u16,
}
}
}