use crate::canvas::Canvas;
use crate::color::Color;
use crate::font_file::{Bitmap, FontFile, Set};
use crate::geom::rect;
use crate::png;
use crate::text::{Font, Metrics, TextSystem, among, cluster, pictured};
use std::ops::Range;
type Full = fn(&FontFile, u16, f32, [f32; 2], Color) -> Option<crate::colr::Drawn>;
const CACHE_LIMIT: usize = 4 << 20;
const LARGEST: usize = 1 << 22;
#[rustfmt::skip]
const SHOWN: [(u32, u32); 80] = [
(0x231a, 0x231b), (0x23e9, 0x23ec), (0x23f0, 0x23f0), (0x23f3, 0x23f3), (0x25fd, 0x25fe), (0x2614, 0x2615), (0x2648, 0x2653), (0x267f, 0x267f), (0x2693, 0x2693), (0x26a1, 0x26a1),
(0x26aa, 0x26ab), (0x26bd, 0x26be), (0x26c4, 0x26c5), (0x26ce, 0x26ce), (0x26d4, 0x26d4), (0x26ea, 0x26ea), (0x26f2, 0x26f3), (0x26f5, 0x26f5), (0x26fa, 0x26fa), (0x26fd, 0x26fd),
(0x2705, 0x2705), (0x270a, 0x270b), (0x2728, 0x2728), (0x274c, 0x274c), (0x274e, 0x274e), (0x2753, 0x2755), (0x2757, 0x2757), (0x2795, 0x2797), (0x27b0, 0x27b0), (0x27bf, 0x27bf),
(0x2b1b, 0x2b1c), (0x2b50, 0x2b50), (0x2b55, 0x2b55), (0x1f004, 0x1f004), (0x1f0cf, 0x1f0cf), (0x1f18e, 0x1f18e), (0x1f191, 0x1f19a), (0x1f1e6, 0x1f1ff), (0x1f201, 0x1f201), (0x1f21a, 0x1f21a),
(0x1f22f, 0x1f22f), (0x1f232, 0x1f236), (0x1f238, 0x1f23a), (0x1f250, 0x1f251), (0x1f300, 0x1f320), (0x1f32d, 0x1f335), (0x1f337, 0x1f37c), (0x1f37e, 0x1f393), (0x1f3a0, 0x1f3ca), (0x1f3cf, 0x1f3d3),
(0x1f3e0, 0x1f3f0), (0x1f3f4, 0x1f3f4), (0x1f3f8, 0x1f43e), (0x1f440, 0x1f440), (0x1f442, 0x1f4fc), (0x1f4ff, 0x1f53d), (0x1f54b, 0x1f54e), (0x1f550, 0x1f567), (0x1f57a, 0x1f57a), (0x1f595, 0x1f596),
(0x1f5a4, 0x1f5a4), (0x1f5fb, 0x1f64f), (0x1f680, 0x1f6c5), (0x1f6cc, 0x1f6cc), (0x1f6d0, 0x1f6d2), (0x1f6d5, 0x1f6d9), (0x1f6dc, 0x1f6df), (0x1f6eb, 0x1f6ec), (0x1f6f4, 0x1f6fc), (0x1f7e0, 0x1f7eb),
(0x1f7f0, 0x1f7f0), (0x1f90c, 0x1f93a), (0x1f93c, 0x1f945), (0x1f947, 0x1f9ff), (0x1fa70, 0x1fa7c), (0x1fa80, 0x1fac6), (0x1fac8, 0x1fac8), (0x1facc, 0x1fadd), (0x1fadf, 0x1faeb), (0x1faef, 0x1fafa),
];
pub(crate) fn plain(text: &str) -> bool {
let bytes = text.as_bytes();
let (eights, rest) = bytes.as_chunks::<8>();
let high = |eight: &[u8; 8]| {
let eight = u64::from_ne_bytes(*eight);
((eight & 0x7f7f_7f7f_7f7f_7f7f) + 0x1e1e_1e1e_1e1e_1e1e) & eight & 0x8080_8080_8080_8080 != 0
};
if !eights.iter().any(high) && rest.iter().all(|byte| *byte < 0xe2) {
return true;
}
let next = |at: usize| bytes.get(at).copied().unwrap_or(0);
let pictured = |at: usize| match bytes[at] {
..0xe2 | 0xe4..=0xee => false,
0xe2 => next(at + 1) != 0x80 || next(at + 2) >= 0xbc,
0xe3 => matches!((next(at + 1), next(at + 2)), (0x80, 0xb0 | 0xbd) | (0x8a, 0x97 | 0x99)),
0xef => next(at + 1) == 0xb8,
_ => true,
};
!(0..bytes.len()).any(pictured)
}
pub(crate) fn picture(first: char, text: &str) -> usize {
let key = matches!(first, '#' | '*' | '0'..='9');
let may = match first {
'#' | '*' | '0'..='9' => text.as_bytes().get(1).is_some_and(|byte| *byte >= 0xe2),
'\u{a9}' | '\u{ae}' => true,
_ => first >= '\u{203c}' && !('\u{329a}'..'\u{1f000}').contains(&first) && pictured(first),
};
if !may {
return 0;
}
let long = cluster(text);
let shown = match text[first.len_utf8()..].chars().next() {
_ if key => text[..long].ends_with('\u{20e3}'),
Some('\u{fe0e}') => false,
Some('\u{fe0f}' | '\u{200d}' | '\u{1f3fb}'..='\u{1f3ff}') => true,
_ => among(&SHOWN, first),
};
if shown { long } else { 0 }
}
enum Source {
Unasked,
Nowhere,
Font(Box<FontFile>),
}
struct Met {
wide: f32,
drawn: Option<Picture>,
}
struct Picture {
at: [i32; 2],
width: usize,
pixels: Vec<u32>,
ink: Option<Color>,
}
enum Part {
Outline([i32; 2], usize, Vec<u8>, Color),
Ready(usize, Vec<u32>, [i32; 4]),
Laid([i32; 2], usize, Vec<u32>),
}
impl Part {
fn reach(&self) -> [i32; 4] {
match self {
Part::Outline([x, y], width, cover, _) => [*x, *y, x + *width as i32, y + (cover.len() / (*width).max(1)) as i32],
Part::Ready(.., place) => *place,
Part::Laid([x, y], width, pixels) => [*x, *y, x + *width as i32, y + (pixels.len() / (*width).max(1)) as i32],
}
}
}
pub struct Emoji {
text: Box<dyn TextSystem>,
source: Source,
full: Option<Full>,
met: Vec<(u32, Box<str>, Option<Met>)>,
cache_bytes: usize,
pieces: Vec<(Range<usize>, Option<f32>)>,
glyphs: Vec<Set>,
pale: Vec<u32>,
}
pub(crate) fn in_colour(text: Box<dyn TextSystem>) -> Box<dyn TextSystem> {
match cfg!(feature = "emoji") {
true => Box::new(Emoji::around(text)),
false => text,
}
}
impl std::fmt::Debug for Emoji {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let font = matches!(self.source, Source::Font(_));
f.debug_struct("Emoji").field("font", &font).field("in_full", &self.full.is_some()).field("measured", &self.met.len()).finish_non_exhaustive()
}
}
impl Emoji {
pub fn around(text: Box<dyn TextSystem>) -> Self {
Emoji { text, source: Source::Unasked, full: None, met: Vec::new(), cache_bytes: 0, pieces: Vec::new(), glyphs: Vec::new(), pale: Vec::new() }
}
pub fn with(self, font: FontFile) -> Self {
Emoji { source: Source::Font(Box::new(font)), ..self }.in_full()
}
pub fn in_full(self) -> Self {
let source = match self.source {
Source::Nowhere => Source::Unasked,
source => source,
};
Emoji { source, full: Some(crate::colr::drawn), met: Vec::new(), cache_bytes: 0, ..self }
}
fn wide(&mut self, sequence: &str, pixels: f32) -> Option<f32> {
let Emoji { source, met, cache_bytes, glyphs, full, .. } = self;
let at = match place(met, sequence, pixels) {
Ok(at) => return met[at].2.as_ref().map(|met| met.wide),
Err(at) => at,
};
if let Source::Unasked = source {
*source = system(full.is_some()).map_or(Source::Nowhere, |font| Source::Font(Box::new(font)));
}
let Source::Font(font) = source else { return None };
let wide = font.set(sequence, glyphs).map(|wide| (wide * font.unit(pixels)).round());
*cache_bytes += sequence.len() + size_of::<(u32, Box<str>, Option<Met>)>();
met.insert(at, (pixels.to_bits(), sequence.into(), wide.map(|wide| Met { wide, drawn: None })));
wide
}
fn cut(&mut self, text: &str, pixels: f32) -> bool {
if self.cache_bytes > CACHE_LIMIT {
self.met.clear();
self.cache_bytes = 0;
}
self.pieces.clear();
let sized = pixels > 0.0 && pixels < 4096.0;
let (mut words, mut next) = (0, 0);
for (at, letter) in text.char_indices().filter(|_| sized) {
let long = if at < next { 0 } else { picture(letter, &text[at..]) };
let wide = if long == 0 { None } else { self.wide(&text[at..at + long], pixels) };
next = next.max(at + long);
if let Some(wide) = wide {
let two = [(words..at, None), (at..next, Some(wide))];
self.pieces.extend(two.into_iter().filter(|(piece, _)| !piece.is_empty()));
words = next;
}
}
self.pieces.extend(Some((words..text.len(), None)).filter(|(piece, _)| !piece.is_empty()));
words > 0
}
fn lay(&mut self, canvas: &mut Canvas, sequence: &str, pixels: f32, [x, line]: [i32; 2], color: Color) {
let Emoji { source: Source::Font(font), met, cache_bytes, glyphs, pale, full, .. } = self else { return };
let Some((.., Some(met))) = place(met, sequence, pixels).ok().map(|at| &mut met[at]) else { return };
let ink = color.solid();
if met.drawn.as_ref().is_none_or(|drawn| drawn.ink.is_some_and(|was| was != ink)) {
let _ = font.set(sequence, glyphs);
*cache_bytes -= met.drawn.as_ref().map_or(0, |drawn| drawn.pixels.len() * 4);
let drawn = met.drawn.insert(painted(font, glyphs, pixels, ink, *full));
*cache_bytes += drawn.pixels.len() * 4;
}
let Some(drawn) = met.drawn.as_ref().filter(|drawn| drawn.width > 0) else { return };
let (at, solid) = ([x + drawn.at[0], line + drawn.at[1]], color.a() as u32);
if solid == 255 {
return canvas.pixels(&drawn.pixels, drawn.width, at, false);
}
let paler = |pixel: &u32| (0..4).fold(0, |paler, part| paler | ((((pixel >> (part * 8)) & 255) * solid + 127) / 255) << (part * 8));
pale.clear();
pale.extend(drawn.pixels.iter().map(paler));
canvas.pixels(pale, drawn.width, at, false);
}
}
fn place(met: &[(u32, Box<str>, Option<Met>)], sequence: &str, pixels: f32) -> Result<usize, usize> {
met.binary_search_by(|(size, known, _)| (*size, &**known).cmp(&(pixels.to_bits(), sequence)))
}
fn painted(font: &FontFile, glyphs: &[Set], pixels: f32, ink: Color, full: Option<Full>) -> Picture {
let (unit, mut parts, mut inked) = (font.unit(pixels), Vec::new(), false);
for set in glyphs {
let pen = [set.at[0] * unit, set.at[1] * unit];
let mut outline = |glyph: u16, colour: Option<Color>| {
let (at, width, cover) = font.cover(glyph, unit, pen);
inked |= colour.is_none();
parts.push(Part::Outline(at, width, cover, colour.unwrap_or(ink)));
};
if let Some(drawn) = full.and_then(|full| full(font, set.glyph, unit, pen, ink)) {
inked |= drawn.inked;
parts.push(Part::Laid(drawn.at, drawn.width, drawn.pixels));
continue;
}
match (font.layers(set.glyph), font.bitmap(set.glyph, pixels)) {
(Some(layers), _) => layers.for_each(|(glyph, colour)| outline(glyph, colour)),
(None, Some(ready)) => parts.extend(fitted(ready, pixels, pen)),
(None, None) => outline(set.glyph, None),
}
}
let reach = parts.iter().map(Part::reach).filter(|[left, top, right, bottom]| left < right && top < bottom);
let around = reach.reduce(|[left, top, right, bottom], [l, t, r, b]| [left.min(l), top.min(t), right.max(r), bottom.max(b)]);
let [left, top, right, bottom] = around.unwrap_or([0; 4]);
let (width, height) = ((right - left) as usize, (bottom - top) as usize);
if width * height == 0 || width * height > LARGEST {
return Picture { at: [0; 2], width: 0, pixels: Vec::new(), ink: None };
}
let mut block = vec![0u32; width * height];
let mut canvas = Canvas::new(&mut block, width, height, (left, top), 1.0);
for part in &parts {
match part {
Part::Outline([x, y], width, cover, colour) => canvas.cover(*x, *y, *width, cover, *colour),
Part::Ready(width, pixels, [l, t, r, b]) => canvas.picture(pixels, *width, rect(*l as f32, *t as f32, (r - l) as f32, (b - t) as f32)),
Part::Laid(at, width, pixels) => canvas.pixels(pixels, *width, *at, false),
}
}
Picture { at: [left, top], width, pixels: block, ink: inked.then_some(ink) }
}
fn fitted(ready: Bitmap, pixels: f32, pen: [f32; 2]) -> Option<Part> {
let (width, picture) = png::decode(ready.png)?;
let (by, height) = (pixels / ready.pixels, picture.len() / width);
let (left, top) = ((pen[0] + ready.left * by).round(), (-pen[1] - ready.top * by).round());
let place = [left, top, left + (width as f32 * by).round().max(1.0), top + (height as f32 * by).round().max(1.0)];
place.iter().all(|edge| edge.abs() < 1e6).then(|| Part::Ready(width, picture, place.map(|edge| edge as i32)))
}
impl TextSystem for Emoji {
fn metrics(&mut self, font: Font, scale: f32) -> Metrics {
self.text.metrics(font, scale)
}
fn measure(&mut self, text: &str, font: Font, scale: f32, mut stops: Option<&mut Vec<f32>>) -> f32 {
if plain(text) || !self.cut(text, font.size * scale) {
return self.text.measure(text, font, scale, stops);
}
let mut pen = 0.0;
for (piece, wide) in &self.pieces {
let (from, ends) = (stops.as_ref().map_or(0, |stops| stops.len()), pen / scale);
let wide = match wide {
Some(wide) => *wide,
None => (self.text.measure(&text[piece.clone()], font, scale, stops.as_deref_mut()) * scale).round(),
};
pen += wide;
let Some(stops) = stops.as_deref_mut() else { continue };
stops[from..].iter_mut().for_each(|stop| *stop += ends);
stops.extend(text[piece.clone()].chars().skip(stops.len() - from).map(|_| pen / scale));
stops.last_mut().into_iter().for_each(|last| *last = pen / scale);
}
pen / scale
}
fn file(&mut self, face: crate::text::Face, letter: char) -> Option<&FontFile> {
self.text.file(face, letter)
}
fn draw(&mut self, canvas: &mut Canvas, text: &str, x: f32, y: f32, font: Font, color: Color) {
let scale = canvas.scale();
if plain(text) || !self.cut(text, font.size * scale) {
return self.text.draw(canvas, text, x, y, font, color);
}
let metrics = self.text.metrics(font, scale);
let (left, line) = ((x * scale).round(), ((y * scale).round() + (metrics.ascent * scale).round()) as i32);
let (mut pen, pieces) = (0.0, std::mem::take(&mut self.pieces));
for (piece, wide) in &pieces {
let (piece, from) = (&text[piece.clone()], (left + pen) / scale);
let Some(wide) = wide else {
self.text.draw(canvas, piece, from, y, font, color);
pen += (self.text.measure(piece, font, scale, None) * scale).round();
continue;
};
let room = canvas.clip(rect(from, y, wide / scale, metrics.height()));
self.lay(canvas, piece, font.size * scale, [(left + pen) as i32, line], color);
canvas.restore_clip(room);
pen += wide;
}
self.pieces = pieces;
}
}
#[cfg(windows)]
fn system(full: bool) -> Option<FontFile> {
let windows = std::env::var_os("WINDIR")?;
FontFile::open(std::path::Path::new(&windows).join("Fonts").join("seguiemj.ttf")).filter(|font| font.pictured() || full && font.pictured_newer())
}
#[cfg(not(windows))]
fn system(full: bool) -> Option<FontFile> {
use std::path::PathBuf;
let smiles = |font: &FontFile| (font.pictured() || full && font.pictured_newer()) && font.set("\u{1f600}", &mut Vec::new()).is_some();
#[cfg(not(target_os = "macos"))]
let named = crate::x11::emoji_file().and_then(FontFile::open).filter(smiles);
#[cfg(target_os = "macos")]
let named = crate::core_text::emoji_file().filter(smiles);
if named.is_some() {
return named;
}
let home = std::env::var_os("HOME").map(PathBuf::from).unwrap_or_default();
let own = std::env::var_os("XDG_DATA_HOME").map_or(home.join(".local/share"), PathBuf::from).join("fonts");
let mut folders = vec![PathBuf::from("/usr/share/fonts"), PathBuf::from("/usr/local/share/fonts"), home.join(".fonts"), own];
for _ in 0..4096 {
let mut found: Vec<PathBuf> = std::fs::read_dir(folders.pop()?).into_iter().flatten().flatten().map(|entry| entry.path()).collect();
found.sort();
folders.extend(found.iter().rev().filter(|path| path.is_dir()).cloned());
let font = found.iter().filter(|path| coloured(path)).find_map(|path| FontFile::open(path).filter(smiles));
if font.is_some() {
return font;
}
}
None
}
#[cfg(not(windows))]
fn coloured(path: &std::path::Path) -> bool {
use std::io::Read;
let kind = path.extension().and_then(|kind| kind.to_str()).unwrap_or("").to_ascii_lowercase();
let mut start = [0; 4096];
let read = std::fs::File::open(path).ok().filter(|_| ["ttf", "otf", "ttc"].contains(&kind.as_str())).and_then(|mut file| file.read(&mut start).ok());
start[..read.unwrap_or(0)].windows(4).any(|name| [b"COLR", b"CBDT", b"sbix", b"SVG "].contains(&name.try_into().unwrap_or(&[0; 4])))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::font_file::AppFonts;
use crate::font_file::tests::{coloured, emoji_font, file, picture_font};
use crate::geom::Size;
use crate::platform::LocalClipboard;
use crate::surface::Surface;
use crate::text::BuiltInFont;
use crate::ui::Ui;
const YELLOW: u32 = 0xffff_cc00;
const BLACK: u32 = 0xff00_0000;
const RED: u32 = 0xffe0_0000;
const BLUE: u32 = 0x7f00_007f;
fn own() -> Emoji {
Emoji::around(Box::new(BuiltInFont)).with(emoji_font())
}
const FONT: Font = Font::new(16.0);
fn drawn(writer: &mut dyn TextSystem, text: &str, color: Color, ground: u32) -> Vec<u32> {
let mut pixels = vec![ground; 64 * 24];
writer.draw(&mut Canvas::new(&mut pixels, 64, 24, (0, 0), 1.0), text, 2.0, 2.0, FONT, color);
pixels
}
fn reach(pixels: &[u32], width: usize, colour: impl Fn(u32) -> bool) -> ([usize; 4], usize) {
let found: Vec<(usize, usize)> = pixels.iter().enumerate().filter(|(_, pixel)| colour(**pixel)).map(|(n, _)| (n % width, n / width)).collect();
let around = found.iter().fold([usize::MAX, usize::MAX, 0, 0], |[left, top, right, bottom], (x, y)| [left.min(*x), top.min(*y), right.max(x + 1), bottom.max(y + 1)]);
(around, found.len())
}
fn measured(writer: &mut dyn TextSystem, text: &str) -> (f32, Vec<f32>) {
let mut stops = Vec::new();
(writer.measure(text, FONT, 1.0, Some(&mut stops)), stops)
}
#[test]
fn text_without_pictures_is_the_other_writers_to_the_pixel() {
let texts = ["", "plain words", "caf\u{e9} na\u{ef}ve", "wait\u{2026} \u{2013} \u{201c}so\u{201d}", "\u{4e2d}\u{6587}", "e\u{301}", "\u{2764} 1 \u{a9} \u{2122}"];
let letters = || AppFonts::new(FontFile::read(file(&[0, 1, 0, 0], &coloured(&[]))).unwrap());
let mut pairs: [(Box<dyn TextSystem>, Emoji); 2] = [(Box::new(BuiltInFont), own()), (Box::new(letters()), Emoji::around(Box::new(letters())).with(emoji_font()))];
for (alone, around) in &mut pairs {
for text in texts {
assert_eq!(measured(around, text), measured(alone.as_mut(), text), "{text:?}");
assert!(drawn(around, text, Color::WHITE, 0) == drawn(alone.as_mut(), text, Color::WHITE, 0), "{text:?} is drawn differently");
assert_eq!(around.metrics(FONT, 1.5), alone.metrics(FONT, 1.5));
}
assert!(around.met.is_empty(), "nothing was taken for a picture");
}
let (mut alone, mut around) = (BuiltInFont, own());
for text in ["a\u{1f600}\u{fe0e}b", "\u{1f601}", "\u{1f1e9}\u{1f1ea}", "1\u{fe0f}", "\u{1f600}\u{fe0e}\u{1f601}x"] {
assert_eq!(measured(&mut around, text), measured(&mut alone, text), "{text:?}");
assert!(drawn(&mut around, text, Color::WHITE, 0) == drawn(&mut alone, text, Color::WHITE, 0), "{text:?} is drawn differently");
}
let after = ["", "\u{fe0f}", "\u{20e3}", "\u{1f3fd}", "\u{200d}\u{1f600}"];
for (first, after) in (0..0x20000).filter_map(char::from_u32).flat_map(|first| after.map(|after| (first, after))) {
let text = ["abcdefgh"[..first as usize % 8].to_string().as_str(), first.to_string().as_str(), after].concat();
assert!(picture(first, &text[first as usize % 8..]) == 0 || !plain(&text), "{text:?} is a picture, and is taken for plain text");
}
assert!(["plain", "caf\u{e9}", "wait\u{2026} \u{2013} \u{201c}so\u{201d} \u{2022}", "\u{4e2d}\u{6587}\u{3001}\u{3002}", "\u{d55c}\u{ae00}", "\u{3b1}\u{431}"].iter().all(|text| plain(text)));
let mut unasked = Emoji::around(Box::new(BuiltInFont));
texts.iter().for_each(|text| _ = (measured(&mut unasked, text), drawn(&mut unasked, text, Color::WHITE, 0)));
assert!(matches!(unasked.source, Source::Unasked));
let mut none = Emoji { source: Source::Nowhere, ..Emoji::around(Box::new(BuiltInFont)) };
assert_eq!(measured(&mut none, "a\u{1f600}\u{1f44d}\u{1f3fd}b"), measured(&mut alone, "a\u{1f600}\u{1f44d}\u{1f3fd}b"));
assert!(drawn(&mut none, "a\u{1f600}b", Color::WHITE, 0) == drawn(&mut alone, "a\u{1f600}b", Color::WHITE, 0));
}
#[test]
fn a_picture_stands_on_the_line_in_its_own_colours() {
let mut emoji = own();
assert_eq!(measured(&mut emoji, "a\u{1f600}b"), (31.0, vec![12.0, 19.0, 31.0]));
let pixels = drawn(&mut emoji, "a\u{1f600}b", Color::WHITE, 0);
assert_eq!(reach(&pixels, 64, |pixel| pixel == YELLOW), ([15, 11, 18, 18], 3 * 7 - 4));
assert_eq!(reach(&pixels, 64, |pixel| pixel == BLACK), ([15, 16, 17, 18], 4));
let letters = |x: f32| {
let mut pixels = vec![0u32; 64 * 24];
BuiltInFont.draw(&mut Canvas::new(&mut pixels, 64, 24, (0, 0), 1.0), if x == 2.0 { "a" } else { "b" }, x, 2.0, FONT, Color::WHITE);
pixels
};
let (a, b) = (letters(2.0), letters(21.0));
assert!((0..64 * 24).filter(|n| n % 64 < 14).all(|n| pixels[n] == a[n]) && (0..64 * 24).filter(|n| n % 64 >= 21).all(|n| pixels[n] == b[n]));
let green = drawn(&mut emoji, "a\u{1f600}b", Color::rgb(0x00ff00), 0);
assert_eq!((reach(&green, 64, |pixel| pixel == YELLOW), reach(&green, 64, |pixel| pixel == BLACK)), (([15, 11, 18, 18], 17), ([15, 16, 17, 18], 4)));
let pale = drawn(&mut emoji, "\u{1f600}", Color::WHITE.with_alpha(0.5), BLACK);
let [red, green, blue] = [16, 8, 0].map(|part| pale[11 * 64 + 3] >> part & 255);
assert!((127..=129).contains(&red) && (101..=103).contains(&green) && blue == 0 && pale[11 * 64 + 3] >> 24 == 255, "{:08x}", pale[11 * 64 + 3]);
let kept = (drawn(&mut emoji, "\u{1f1e6}\u{1f1e7}", Color::WHITE, 0), emoji.cache_bytes).1;
for ink in [Color::WHITE, Color::rgb(0x00ff00), Color::WHITE] {
let flag = drawn(&mut emoji, "\u{1f1e6}\u{1f1e7}", ink, 0);
assert_eq!((reach(&flag, 64, |pixel| pixel == RED), reach(&flag, 64, |pixel| pixel == ink.0)), (([2, 12, 8, 18], 36 - 4 - 4), ([3, 16, 5, 18], 4)), "{ink}");
assert_eq!(emoji.cache_bytes, kept);
}
let hand = drawn(&mut emoji, "\u{1f44d}", Color::rgb(0x8b93ff), 0);
assert_eq!((reach(&hand, 64, |pixel| pixel == 0xff8b_93ff), reach(&hand, 64, |pixel| pixel != 0).1, measured(&mut emoji, "\u{1f44d}x").0), (([3, 16, 5, 18], 4), 4, 18.0));
}
#[test]
fn characters_that_belong_together_are_one_picture_with_one_width() {
let mut emoji = own();
assert_eq!(measured(&mut emoji, "x\u{1f1e6}\u{1f1e7}y"), (34.0, vec![12.0, 22.0, 22.0, 34.0]));
for (sequence, wide) in [("\u{1f468}\u{200d}\u{1f466}", 10.0), ("1\u{fe0f}\u{20e3}", 6.0), ("1\u{20e3}", 6.0), ("\u{1f44d}\u{1f3fd}", 6.0), ("\u{2764}\u{fe0f}", 6.0)] {
assert_eq!(measured(&mut emoji, sequence), (wide, vec![wide; sequence.chars().count()]), "{sequence:?}");
assert_eq!(emoji.pieces.len(), 1, "{sequence:?}");
}
for sequence in ["1\u{fe0f}\u{20e3}", "\u{1f44d}\u{1f3fd}"] {
let pixels = drawn(&mut emoji, sequence, Color::WHITE, 0);
assert_eq!((reach(&pixels, 64, |pixel| pixel == BLUE), reach(&pixels, 64, |pixel| pixel != 0).1), (([3, 11, 6, 18], 21), 21), "{sequence:?}");
}
assert_eq!(measured(&mut emoji, "\u{1f600}\u{1f3fd}a"), (25.0, vec![13.0, 13.0, 25.0]));
let parts = drawn(&mut emoji, "\u{1f600}\u{1f3fd}", Color::WHITE, 0);
assert_eq!((reach(&parts, 64, |pixel| pixel == YELLOW).0, reach(&parts, 64, |pixel| pixel >> 24 != 0 && pixel != YELLOW && pixel != BLACK).0), ([3, 11, 6, 18], [10, 14, 12, 18]));
assert_eq!(measured(&mut emoji, "\u{1f1e6}\u{1f1e7}\u{1f1e8}"), (20.0, vec![10.0, 10.0, 20.0]));
let text = "a\u{1f600}\u{1f1e6}\u{1f1e7}b\u{1f44d}\u{1f3fd}";
assert_eq!(measured(&mut emoji, text), (47.0, vec![12.0, 19.0, 29.0, 29.0, 41.0, 47.0, 47.0]));
let pixels = drawn(&mut emoji, text, Color::WHITE, 0);
assert_eq!(
(reach(&pixels, 64, |pixel| pixel == YELLOW).0, reach(&pixels, 64, |pixel| pixel == RED).0, reach(&pixels, 64, |pixel| pixel == BLUE).0),
([15, 11, 18, 18], [21, 12, 27, 18], [44, 11, 47, 18])
);
}
#[test]
fn a_picture_is_as_large_as_the_letters_are() {
for (font, scale) in [(Font::new(32.0), 1.0), (FONT, 2.0)] {
let (mut emoji, mut pixels) = (own(), vec![0u32; 64 * 48]);
assert_eq!(emoji.measure("\u{1f600}", font, scale, None), 14.0 / scale);
emoji.draw(&mut Canvas::new(&mut pixels, 64, 48, (0, 0), scale), "\u{1f600}", 4.0 / scale, 4.0 / scale, font, Color::WHITE);
assert_eq!(reach(&pixels, 64, |pixel| pixel == YELLOW), ([6, 22, 12, 36], 6 * 14 - 16), "{font:?} at {scale}");
}
let (mut emoji, mut pixels) = (own(), vec![0u32; 64 * 48]);
assert_eq!(emoji.measure("\u{1f600}", FONT, 1.5, None), 11.0 / 1.5);
emoji.draw(&mut Canvas::new(&mut pixels, 64, 48, (0, 0), 1.5), "\u{1f600}", 2.0, 2.0, FONT, Color::WHITE);
let (around, count) = reach(&pixels, 64, |pixel| pixel != 0);
assert!(around == [4, 16, 9, 27] && count > 40 && pixels.iter().any(|pixel| pixel >> 24 != 0 && pixel >> 24 != 255), "{around:?} {count}");
for (size, place) in [(32.0, [3, 27, 11, 35]), (16.0, [3, 14, 7, 18]), (8.0, [2, 8, 4, 10])] {
let (mut emoji, mut pixels) = (Emoji::around(Box::new(BuiltInFont)).with(picture_font(7)), vec![0u32; 64 * 48]);
emoji.draw(&mut Canvas::new(&mut pixels, 64, 48, (0, 0), 1.0), "\u{1f600}", 2.0, 2.0, Font::new(size), Color::WHITE);
assert_eq!(reach(&pixels, 64, |pixel| pixel == 0xffff_0000), (place, (place[2] - place[0]) * (place[3] - place[1])), "at {size}");
assert_eq!(emoji.measure("\u{1f600}", Font::new(size), 1.0, None), (448.0 * size / 1024.0).round());
}
for size in [0.0, -16.0, 4096.0, 1e9, f32::INFINITY, f32::NAN] {
let (mut emoji, mut alone, mut pixels) = (own(), BuiltInFont, vec![0u32; 64 * 24]);
let wide = emoji.measure("a\u{1f600}", Font::new(size), 1.0, None);
assert!(wide.to_bits() == alone.measure("a\u{1f600}", Font::new(size), 1.0, None).to_bits(), "{size}");
let font = Font::new(if size > 4096.0 { 0.0 } else { size });
emoji.draw(&mut Canvas::new(&mut pixels, 64, 24, (0, 0), 1.0), "a\u{1f600}", 2.0, 2.0, font, Color::WHITE);
assert!(emoji.met.is_empty());
}
}
#[test]
fn a_picture_keeps_to_the_canvas_and_to_its_line() {
let (mut emoji, text) = (own(), "a\u{1f600}\u{1f1e6}\u{1f1e7}b\u{1f44d}\u{1f3fd}");
let whole = drawn(&mut emoji, text, Color::WHITE, 0);
let mut strips = vec![0u32; 64 * 24];
for (n, strip) in strips.chunks_mut(64 * 4).enumerate() {
emoji.draw(&mut Canvas::new(strip, 64, 4, (0, n as i32 * 4), 1.0), text, 2.0, 2.0, FONT, Color::WHITE);
}
assert!(strips == whole);
let mut held = vec![0u32; 64 * 24];
let mut canvas = Canvas::new(&mut held, 64, 24, (0, 0), 1.0);
canvas.clip(rect(16.0, 0.0, 9.0, 14.0));
emoji.draw(&mut canvas, text, 2.0, 2.0, FONT, Color::WHITE);
assert!((0..64 * 24).all(|n| held[n] == if (16..25).contains(&(n % 64)) && n / 64 < 14 { whole[n] } else { 0 }));
let mut tall = Emoji::around(Box::new(BuiltInFont)).with(picture_font(36));
assert_eq!(reach(&drawn(&mut tall, "\u{1f600}", Color::WHITE, 0), 64, |pixel| pixel != 0), ([3, 2, 7, 4], 4 * 2));
}
#[test]
fn pictures_are_drawn_once_and_kept_up_to_a_limit() {
let mut emoji = own();
assert_eq!(emoji.cache_bytes, 0);
let measured = (emoji.measure("\u{1f600}\u{1f1e6}\u{1f1e7}", FONT, 1.0, None), emoji.cache_bytes).1;
let kept = (drawn(&mut emoji, "\u{1f600}\u{1f1e6}\u{1f1e7}", Color::WHITE, 0), emoji.cache_bytes).1;
assert!(measured > 0 && kept >= measured + (3 * 7 + 6 * 6) * 4 && kept < 1000, "{measured} {kept}");
for _ in 0..3 {
let _ = (emoji.measure("a\u{1f600}\u{1f1e6}\u{1f1e7}", FONT, 1.0, None), drawn(&mut emoji, "\u{1f1e6}\u{1f1e7} \u{1f600}", Color::WHITE, 0));
}
assert_eq!((emoji.cache_bytes, emoji.met.len()), (kept, 2));
let lacking = (emoji.measure("\u{1f601}", FONT, 1.0, None), emoji.cache_bytes).1;
assert_eq!((emoji.measure("\u{1f601}\u{1f601}", FONT, 1.0, None), emoji.cache_bytes), (24.0, lacking));
let mut most = 0;
for size in 0..40 {
let mut pixels = vec![0u32; 16 * 16];
emoji.draw(&mut Canvas::new(&mut pixels, 16, 16, (0, 0), 1.0), "\u{1f600}", 0.0, 0.0, Font::new(900.0 + size as f32), Color::WHITE);
most = most.max(emoji.cache_bytes);
}
assert!(most > CACHE_LIMIT && most < CACHE_LIMIT + 500_000 && emoji.cache_bytes < most, "{most} {}", emoji.cache_bytes);
}
#[test]
fn a_font_of_the_apps_own_leaves_pictures_whole_to_the_writer_of_what_it_lacks() {
let letters = FontFile::read(file(&[0, 1, 0, 0], &coloured(&[]))).unwrap();
let mut fonts = AppFonts::new(letters).fallback(Box::new(own()));
assert_eq!(measured(&mut fonts, "AB"), (14.0, vec![7.0, 14.0]));
assert_eq!(measured(&mut fonts, "A1B"), (22.0, vec![7.0, 13.0, 22.0]));
assert_eq!(measured(&mut fonts, "A1\u{fe0f}\u{20e3}B"), (22.0, vec![7.0, 13.0, 13.0, 13.0, 22.0]));
assert_eq!(measured(&mut fonts, "A\u{1f1e6}\u{1f1e7}\u{1f600}B"), (33.0, vec![7.0, 17.0, 17.0, 24.0, 33.0]));
let pixels = drawn(&mut fonts, "A1\u{fe0f}\u{20e3}B", Color::WHITE, 0);
assert_eq!(reach(&pixels, 64, |pixel| pixel == BLUE), ([10, 8, 13, 15], 21));
}
#[test]
fn an_interface_has_picture_letters_in_colour_by_itself_only_with_the_feature() {
let colours = |pixels: &[u32]| pixels.iter().filter(|pixel| *pixel & 0xff_ffff != 0 && (*pixel >> 16 & 255 != *pixel & 255 || *pixel >> 8 & 255 != *pixel & 255)).count();
let face = drawn(in_colour(Box::new(BuiltInFont)).as_mut(), "\u{1f600}", Color::WHITE, 0);
match cfg!(feature = "emoji") && system(false).is_some() {
true => assert!(colours(&face) > 20, "the face is in colour"),
false => assert_eq!((colours(&face), face), (0, drawn(&mut BuiltInFont, "\u{1f600}", Color::WHITE, 0))),
}
}
#[test]
fn an_interface_shows_pictures_and_draws_only_what_changed_of_them() {
let mut ui = Ui::new(Box::new(BuiltInFont), Box::new(LocalClipboard::default()));
ui.resize(Size { w: 240.0, h: 80.0 }, 1.0);
ui.theme.font = FONT;
ui.set_emoji(emoji_font());
let mut screen = Surface::draw(&mut ui, 64);
let mut shown = |ui: &mut Ui, build: &mut dyn FnMut(&mut Ui)| {
let changed = ui.run(build);
screen.redraw(ui, changed);
assert!(screen.pixels == Surface::draw(ui, 64).pixels, "drawing only what changed ({changed:?}) left the window different");
(reach(&screen.pixels, 240, |pixel| pixel == YELLOW).1, reach(&screen.pixels, 240, |pixel| pixel == RED).1)
};
assert_eq!(shown(&mut ui, &mut |ui| _ = ui.label("a\u{1f600}b")), (17, 0));
assert_eq!(shown(&mut ui, &mut |ui| _ = ui.label("a\u{1f600}\u{1f1e6}\u{1f1e7}b")), (17, 28));
assert_eq!(shown(&mut ui, &mut |ui| _ = ui.label("ab")), (0, 0));
assert_eq!(shown(&mut ui, &mut |ui| _ = ui.button("\u{1f1e6}\u{1f1e7} go \u{1f600}")), (17, 28));
let mut text = String::new();
let mut focused = false;
let mut typing = |ui: &mut Ui, text: &mut String| {
let state = ui.text_box(text, "");
if !std::mem::replace(&mut focused, true) {
ui.set_focus(state.id);
}
};
for event in [None, Some(crate::Event::Text("x\u{1f468}\u{200d}\u{1f466}".into())), Some(crate::Event::Key(crate::Key::Left, crate::Mods::NONE))] {
event.into_iter().for_each(|event| _ = ui.event(event));
shown(&mut ui, &mut |ui| typing(ui, &mut text));
}
assert_eq!((text.as_str(), ui.edit.cursor), ("x\u{1f468}\u{200d}\u{1f466}", 1));
ui.event(crate::Event::Key(crate::Key::Delete, crate::Mods::NONE));
assert_eq!((shown(&mut ui, &mut |ui| typing(ui, &mut text)), text.as_str()), ((0, 0), "x"));
}
fn real(letters: impl Fn() -> Box<dyn TextSystem>) -> Vec<(String, Emoji)> {
let named = std::env::var_os("TAUT_EMOJI").map(std::path::PathBuf::from);
let named = named.map(|path| (path.file_stem().unwrap().to_string_lossy().into_owned(), Emoji::around(letters()).with(FontFile::open(&path).expect("the font is read"))));
let mut system = Emoji::around(letters());
system.measure("\u{1f600}", FONT, 1.0, None);
match system.source {
Source::Font(_) => std::iter::once(("system".to_string(), system)).chain(named).collect(),
_ => {
eprintln!("no font of emoji is found on this system: its own emoji are not tested");
named.into_iter().collect()
}
}
}
#[test]
fn the_emoji_of_a_real_font_are_drawn_in_colour() {
let (flag, family) = ("\u{1f1e9}\u{1f1ea}", "\u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467}\u{200d}\u{1f466}");
let sequences = [
"\u{1f600}",
"\u{1f44d}\u{1f3fd}",
"\u{2764}\u{fe0f}",
"1\u{fe0f}\u{20e3}",
flag,
family,
"\u{1f469}\u{200d}\u{1f680}",
"\u{1f469}\u{1f3fb}\u{200d}\u{2764}\u{fe0f}\u{200d}\u{1f468}\u{1f3ff}",
];
let text = ["ab ", &sequences.join(" "), " ok"].concat();
for (name, mut emoji) in real(|| Box::new(BuiltInFont)) {
for (font, scale) in [(FONT, 1.0), (Font::new(32.0), 1.5)] {
let pixels = font.size * scale;
for sequence in sequences {
let mut stops = Vec::new();
let wide = emoji.measure(sequence, font, scale, Some(&mut stops));
assert_eq!((emoji.pieces.len(), &stops), (1, &vec![wide; sequence.chars().count()]), "{name}: {sequence:?}");
assert!((0.8..=3.0).contains(&(wide * scale / pixels)) && (wide * scale).fract() == 0.0, "{name}: {sequence:?} is {wide} wide");
}
let (wide, high) = (emoji.measure(&text, font, scale, None), emoji.metrics(font, scale).height());
let (width, height) = (((wide + 8.0) * scale).ceil() as usize, ((high + 8.0) * scale).ceil() as usize);
let mut line = vec![0xffff_ffffu32; width * height];
emoji.draw(&mut Canvas::new(&mut line, width, height, (0, 0), scale), &text, 4.0, 4.0, font, Color::BLACK);
if let Some(folder) = std::env::var_os("TAUT_PICTURES") {
let file = format!("emoji-{}-{name}-{}-{scale}.png", std::env::consts::OS, font.size);
std::fs::write(std::path::Path::new(&folder).join(file), png::encode(width, &line).unwrap()).unwrap();
}
let coloured =
|pixel: &&u32| [16, 8, 0].map(|part| (**pixel >> part & 255) as i32).iter().max().unwrap() - [16, 8, 0].map(|part| (**pixel >> part & 255) as i32).iter().min().unwrap() > 48;
let (colours, marks) = (line.iter().filter(coloured).count(), line.iter().filter(|pixel| **pixel != 0xffff_ffff).count());
assert!(colours * 3 > marks && colours as f32 > pixels * pixels * 2.0, "{name}: {colours} pixels of {marks} are in colour");
let mut face = vec![0u32; (pixels * 3.0) as usize * height];
emoji.draw(&mut Canvas::new(&mut face, (pixels * 3.0) as usize, height, (0, 0), scale), "\u{1f600}", 4.0, 4.0, font, Color::BLACK);
let ([_, top, _, bottom], _) = reach(&face, (pixels * 3.0) as usize, |pixel| pixel >> 24 > 128);
let (line_top, line_bottom) = ((4.0 * scale) as usize, ((4.0 + high) * scale) as usize);
assert!(((bottom - top) as f32 / pixels - 1.05).abs() < 0.25, "{name}: the face is {} pixels high for letters of {pixels}", bottom - top);
assert!(
top >= line_top && bottom <= line_bottom && top < (line_top + line_bottom) / 2 && bottom > (line_top + line_bottom) / 2,
"{name}: the face is from {top} to {bottom} in a line from {line_top} to {line_bottom}"
);
}
let Source::Font(file) = &emoji.source else { unreachable!() };
let mut glyphs = Vec::new();
let wide = file.set(family, &mut glyphs).expect("the font has a family") * file.unit(1.0);
assert!((0.8..=1.6).contains(&wide), "{name}: a family is {wide} of its height wide");
let flags = file.set(flag, &mut glyphs).is_some_and(|_| glyphs.len() == 1);
println!(
"{name}: a family is {} glyphs, {wide:.2} of the letters' height wide; a flag is {}",
file.set(family, &mut glyphs).map_or(0, |_| glyphs.len()),
if flags { "one glyph" } else { "its two letters" }
);
}
}
fn of_the_system() -> Option<Box<dyn TextSystem>> {
crate::platform::SystemText::new().map(|text| Box::new(text) as Box<dyn TextSystem>)
}
#[test]
fn pictures_stand_among_the_letters_of_the_system() {
let Some(mut alone) = of_the_system() else { return eprintln!("no text of the system's own here: not tested") };
let mut around = Emoji::around(of_the_system().unwrap());
for (text, font) in [("Hello, world", FONT), ("caf\u{e9} \u{2026} \u{2013} \u{4e2d}\u{6587}", Font::new(13.0).bold()), ("let x = 1;", Font::new(13.0).mono())] {
let both = [alone.as_mut(), &mut around as &mut dyn TextSystem].map(|writer| {
let (mut stops, mut pixels) = (Vec::new(), vec![0u32; 240 * 40]);
let wide = writer.measure(text, font, 1.5, Some(&mut stops));
writer.draw(&mut Canvas::new(&mut pixels, 240, 40, (0, 0), 1.5), text, 2.0, 2.0, font, Color::WHITE);
(wide, stops, pixels)
});
assert!(both[0] == both[1], "{text:?} is not as the system writes it");
}
assert!(matches!(around.source, Source::Unasked));
let text = "Hxg \u{1f600} Hxg \u{1f44d}\u{1f3fd} 1\u{fe0f}\u{20e3} \u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467}.";
for (name, mut emoji) in real(|| of_the_system().unwrap()) {
for (font, scale) in [(Font::new(13.0), 1.0), (Font::new(13.0), 1.5), (Font::new(20.0), 2.0)] {
let (pixels, metrics) = (font.size * scale, emoji.metrics(font, scale));
let (width, height) = (((emoji.measure(text, font, scale, None) + 8.0) * scale).ceil() as usize, ((metrics.height() + 8.0) * scale).ceil() as usize);
let mut line = vec![0xffff_ffffu32; width * height];
emoji.draw(&mut Canvas::new(&mut line, width, height, (0, 0), scale), text, 4.0, 4.0, font, Color::BLACK);
if let Some(folder) = std::env::var_os("TAUT_PICTURES") {
let file = format!("text-{}-{name}-{}-{scale}.png", std::env::consts::OS, font.size);
std::fs::write(std::path::Path::new(&folder).join(file), png::encode(width, &line).unwrap()).unwrap();
}
let coloured = |pixel: u32| (pixel >> 16 & 255).abs_diff(pixel & 255) > 60 || (pixel >> 8 & 255).abs_diff(pixel & 255) > 60;
let ([_, top, _, bottom], count) = reach(&line, width, coloured);
let stands = ((4.0 + metrics.ascent) * scale).round();
assert!(count as f32 > pixels * pixels, "{name}: {count} pixels are in colour at {pixels}");
assert!(((bottom - top) as f32 / pixels - 1.1).abs() < 0.3, "{name}: the pictures are from {top} to {bottom} for letters of {pixels}");
assert!((bottom as f32 - stands) / pixels > -0.1 && (bottom as f32 - stands) / pixels < 0.4, "{name}: the pictures' foot is at {bottom}, and the letters stand on {stands}");
}
}
}
#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn the_font_the_desktop_names_for_emoji_is_asked_first() {
let _letters = crate::x11::SystemText::new();
let Some(named) = crate::x11::emoji_file() else { return };
assert!(named.is_file(), "{named:?} is named and is not there");
let pictured = FontFile::open(&named).is_some_and(|font| font.pictured());
assert!(!pictured || system(false).is_some(), "{named:?} has pictures, and no font of emoji was found");
}
}