use crate::{
Bounds, DevicePixels, Font, FontFeatures, FontId, FontMetrics, FontRun, FontStyle, FontWeight,
GlyphId, LineLayout, Pixels, PlatformTextSystem, Point, RenderGlyphParams, SUBPIXEL_VARIANTS_X,
SUBPIXEL_VARIANTS_Y, ShapedGlyph, ShapedRun, SharedString, Size, point, size,
};
use anyhow::{Context as _, Ok, Result};
use collections::HashMap;
use cosmic_text::{
Attrs, AttrsList, CacheKey, Ellipsize, Family, Font as CosmicTextFont,
FontFeatures as CosmicFontFeatures, FontSystem, Hinting, ShapeBuffer, ShapeLine, SwashCache,
};
use itertools::Itertools;
use parking_lot::RwLock;
use smallvec::SmallVec;
use std::{borrow::Cow, sync::Arc};
pub(crate) struct CosmicTextSystem(RwLock<CosmicTextSystemState>);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct FontKey {
family: SharedString,
features: FontFeatures,
}
impl FontKey {
fn new(family: SharedString, features: FontFeatures) -> Self {
Self { family, features }
}
}
struct CosmicTextSystemState {
swash_cache: SwashCache,
font_system: FontSystem,
scratch: ShapeBuffer,
loaded_fonts: Vec<LoadedFont>,
font_ids_by_family_cache: HashMap<FontKey, SmallVec<[FontId; 4]>>,
}
struct LoadedFont {
font: Arc<CosmicTextFont>,
weight: cosmic_text::fontdb::Weight,
features: CosmicFontFeatures,
is_known_emoji_font: bool,
}
impl CosmicTextSystem {
pub(crate) fn new() -> Self {
let mut font_system = FontSystem::new();
Self(RwLock::new(CosmicTextSystemState {
font_system,
swash_cache: SwashCache::new(),
scratch: ShapeBuffer::default(),
loaded_fonts: Vec::new(),
font_ids_by_family_cache: HashMap::default(),
}))
}
}
impl Default for CosmicTextSystem {
fn default() -> Self {
Self::new()
}
}
impl PlatformTextSystem for CosmicTextSystem {
fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
self.0.write().add_fonts(fonts)
}
fn all_font_names(&self) -> Vec<String> {
let mut result = self
.0
.read()
.font_system
.db()
.faces()
.filter_map(|face| face.families.first().map(|family| family.0.clone()))
.collect_vec();
result.sort();
result.dedup();
result
}
fn font_id(&self, font: &Font) -> Result<FontId> {
let mut state = self.0.write();
let key = FontKey::new(font.family.clone(), font.features.clone());
let candidates = if let Some(font_ids) = state.font_ids_by_family_cache.get(&key) {
font_ids.as_slice()
} else {
let font_ids = state.load_family(&font.family, &font.features)?;
state.font_ids_by_family_cache.insert(key.clone(), font_ids);
state.font_ids_by_family_cache[&key].as_ref()
};
let candidate_properties = candidates
.iter()
.map(|font_id| {
let database_id = state.loaded_font(*font_id).font.id();
let face_info = state.font_system.db().face(database_id).expect("");
face_info_into_properties(face_info)
})
.collect::<SmallVec<[_; 4]>>();
let ix = find_best_match(&candidate_properties, &font_into_properties(font))
.context("requested font family contains no font matching the other parameters")?;
Ok(candidates[ix])
}
fn font_metrics(&self, font_id: FontId) -> FontMetrics {
let metrics = self
.0
.read()
.loaded_font(font_id)
.font
.as_swash()
.metrics(&[]);
FontMetrics {
units_per_em: metrics.units_per_em as u32,
ascent: metrics.ascent,
descent: -metrics.descent, line_gap: metrics.leading,
underline_position: metrics.underline_offset,
underline_thickness: metrics.stroke_size,
cap_height: metrics.cap_height,
x_height: metrics.x_height,
bounding_box: Bounds {
origin: point(0.0, 0.0),
size: size(metrics.max_width, metrics.ascent + metrics.descent),
},
}
}
fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>> {
let lock = self.0.read();
let glyph_metrics = lock.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
let glyph_id = glyph_id.0 as u16;
Ok(Bounds {
origin: point(0.0, 0.0),
size: size(
glyph_metrics.advance_width(glyph_id),
glyph_metrics.advance_height(glyph_id),
),
})
}
fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
self.0.read().advance(font_id, glyph_id)
}
fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
self.0.read().glyph_for_char(font_id, ch)
}
fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
self.0.write().raster_bounds(params)
}
fn rasterize_glyph(
&self,
params: &RenderGlyphParams,
raster_bounds: Bounds<DevicePixels>,
) -> Result<(Size<DevicePixels>, Vec<u8>)> {
self.0.write().rasterize_glyph(params, raster_bounds)
}
fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout {
self.0.write().layout_line(text, font_size, runs)
}
}
impl CosmicTextSystemState {
fn loaded_font(&self, font_id: FontId) -> &LoadedFont {
&self.loaded_fonts[font_id.0]
}
#[profiling::function]
fn add_fonts(&mut self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
let db = self.font_system.db_mut();
for bytes in fonts {
match bytes {
Cow::Borrowed(embedded_font) => {
db.load_font_data(embedded_font.to_vec());
}
Cow::Owned(bytes) => {
db.load_font_data(bytes);
}
}
}
Ok(())
}
#[profiling::function]
fn load_family(
&mut self,
name: &str,
features: &FontFeatures,
) -> Result<SmallVec<[FontId; 4]>> {
let name = crate::text_system::font_name_with_fallbacks(name, "IBM Plex Sans");
let families = self
.font_system
.db()
.faces()
.filter(|face| face.families.iter().any(|family| *name == family.0))
.map(|face| (face.id, face.post_script_name.clone(), face.weight))
.collect::<SmallVec<[_; 4]>>();
let mut loaded_font_ids = SmallVec::new();
for (font_id, postscript_name, weight) in families {
let font = self
.font_system
.get_font(font_id, weight)
.context("Could not load font")?;
let allowed_bad_font_names = [
"SegoeFluentIcons", "Segoe Fluent Icons",
];
if font.as_swash().charmap().map('m') == 0
&& !allowed_bad_font_names.contains(&postscript_name.as_str())
{
self.font_system.db_mut().remove_face(font.id());
continue;
};
let font_id = FontId(self.loaded_fonts.len());
loaded_font_ids.push(font_id);
self.loaded_fonts.push(LoadedFont {
font,
weight,
features: features.try_into()?,
is_known_emoji_font: check_is_known_emoji_font(&postscript_name),
});
}
Ok(loaded_font_ids)
}
fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
let glyph_metrics = self.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
Ok(Size {
width: glyph_metrics.advance_width(glyph_id.0 as u16),
height: glyph_metrics.advance_height(glyph_id.0 as u16),
})
}
fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
let glyph_id = self.loaded_font(font_id).font.as_swash().charmap().map(ch);
if glyph_id == 0 {
None
} else {
Some(GlyphId(glyph_id.into()))
}
}
fn raster_bounds(&mut self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
let loaded_font = &self.loaded_fonts[params.font_id.0];
let font = &loaded_font.font;
let weight = loaded_font.weight;
let subpixel_shift = point(
params.subpixel_variant.x as f32 / SUBPIXEL_VARIANTS_X as f32 / params.scale_factor,
params.subpixel_variant.y as f32 / SUBPIXEL_VARIANTS_Y as f32 / params.scale_factor,
);
let image = self
.swash_cache
.get_image(
&mut self.font_system,
CacheKey::new(
font.id(),
params.glyph_id.0 as u16,
(params.font_size * params.scale_factor).into(),
(subpixel_shift.x, subpixel_shift.y.trunc()),
weight,
cosmic_text::CacheKeyFlags::empty(),
)
.0,
)
.clone()
.with_context(|| format!("no image for {params:?} in font {font:?}"))?;
Ok(Bounds {
origin: point(image.placement.left.into(), (-image.placement.top).into()),
size: size(image.placement.width.into(), image.placement.height.into()),
})
}
#[profiling::function]
fn rasterize_glyph(
&mut self,
params: &RenderGlyphParams,
glyph_bounds: Bounds<DevicePixels>,
) -> Result<(Size<DevicePixels>, Vec<u8>)> {
if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
anyhow::bail!("glyph bounds are empty");
} else {
let bitmap_size = glyph_bounds.size;
let loaded_font = &self.loaded_fonts[params.font_id.0];
let font = &loaded_font.font;
let weight = loaded_font.weight;
let subpixel_shift = point(
params.subpixel_variant.x as f32 / SUBPIXEL_VARIANTS_X as f32 / params.scale_factor,
params.subpixel_variant.y as f32 / SUBPIXEL_VARIANTS_Y as f32 / params.scale_factor,
);
let mut image = self
.swash_cache
.get_image(
&mut self.font_system,
CacheKey::new(
font.id(),
params.glyph_id.0 as u16,
(params.font_size * params.scale_factor).into(),
(subpixel_shift.x, subpixel_shift.y.trunc()),
weight,
cosmic_text::CacheKeyFlags::empty(),
)
.0,
)
.clone()
.with_context(|| format!("no image for {params:?} in font {font:?}"))?;
if params.is_emoji {
for pixel in image.data.chunks_exact_mut(4) {
pixel.swap(0, 2);
}
}
Ok((bitmap_size, image.data))
}
}
fn font_id_for_cosmic_id(&mut self, id: cosmic_text::fontdb::ID) -> FontId {
if let Some(ix) = self
.loaded_fonts
.iter()
.position(|loaded_font| loaded_font.font.id() == id)
{
FontId(ix)
} else {
let face = self.font_system.db().face(id).unwrap();
let weight = face.weight;
let postscript_name = face.post_script_name.clone();
let font = self.font_system.get_font(id, weight).unwrap();
let font_id = FontId(self.loaded_fonts.len());
self.loaded_fonts.push(LoadedFont {
font,
weight,
features: CosmicFontFeatures::new(),
is_known_emoji_font: check_is_known_emoji_font(&postscript_name),
});
font_id
}
}
#[profiling::function]
fn layout_line(&mut self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
let mut attrs_list = AttrsList::new(&Attrs::new());
let mut offs = 0;
for run in font_runs {
let loaded_font = self.loaded_font(run.font_id);
let font = self.font_system.db().face(loaded_font.font.id()).unwrap();
attrs_list.add_span(
offs..(offs + run.len),
&Attrs::new()
.metadata(run.font_id.0)
.family(Family::Name(&font.families.first().unwrap().0))
.stretch(font.stretch)
.style(font.style)
.weight(font.weight)
.font_features(loaded_font.features.clone()),
);
offs += run.len;
}
let line = ShapeLine::new(
&mut self.font_system,
text,
&attrs_list,
cosmic_text::Shaping::Advanced,
4,
);
let mut layout_lines = Vec::with_capacity(1);
line.layout_to_buffer(
&mut self.scratch,
font_size.0,
None, cosmic_text::Wrap::None,
Ellipsize::None,
None,
&mut layout_lines,
None,
Hinting::default(),
);
let layout = layout_lines.first().unwrap();
let mut runs: Vec<ShapedRun> = Vec::new();
for glyph in &layout.glyphs {
let mut font_id = FontId(glyph.metadata);
let mut loaded_font = self.loaded_font(font_id);
if loaded_font.font.id() != glyph.font_id {
font_id = self.font_id_for_cosmic_id(glyph.font_id);
loaded_font = self.loaded_font(font_id);
}
let is_emoji = loaded_font.is_known_emoji_font;
if glyph.glyph_id == 3 && is_emoji {
continue;
}
let shaped_glyph = ShapedGlyph {
id: GlyphId(glyph.glyph_id as u32),
position: point(glyph.x.into(), glyph.y.into()),
index: glyph.start,
is_emoji,
};
if let Some(last_run) = runs
.last_mut()
.filter(|last_run| last_run.font_id == font_id)
{
last_run.glyphs.push(shaped_glyph);
} else {
runs.push(ShapedRun {
font_id,
glyphs: vec![shaped_glyph],
});
}
}
LineLayout {
font_size,
width: layout.w.into(),
ascent: layout.max_ascent.into(),
descent: layout.max_descent.into(),
runs,
len: text.len(),
}
}
}
impl TryFrom<&FontFeatures> for CosmicFontFeatures {
type Error = anyhow::Error;
fn try_from(features: &FontFeatures) -> Result<Self> {
let mut result = CosmicFontFeatures::new();
for feature in features.0.iter() {
let name_bytes: [u8; 4] = feature
.0
.as_bytes()
.try_into()
.context("Incorrect feature flag format")?;
let tag = cosmic_text::FeatureTag::new(&name_bytes);
result.set(tag, feature.1);
}
Ok(result)
}
}
#[derive(Clone, Copy)]
struct RectF {
origin_x: f32,
origin_y: f32,
width: f32,
height: f32,
}
impl RectF {
fn origin_x(&self) -> f32 {
self.origin_x
}
fn origin_y(&self) -> f32 {
self.origin_y
}
fn width(&self) -> f32 {
self.width
}
fn height(&self) -> f32 {
self.height
}
}
#[derive(Clone, Copy)]
struct RectI {
origin_x: i32,
origin_y: i32,
width: i32,
height: i32,
}
impl RectI {
fn origin_x(&self) -> i32 {
self.origin_x
}
fn origin_y(&self) -> i32 {
self.origin_y
}
fn width(&self) -> i32 {
self.width
}
fn height(&self) -> i32 {
self.height
}
}
#[derive(Clone, Copy)]
struct Vector2I {
x: i32,
y: i32,
}
impl Vector2I {
fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
fn x(&self) -> i32 {
self.x
}
fn y(&self) -> i32 {
self.y
}
}
#[derive(Clone, Copy)]
struct Vector2F {
x: f32,
y: f32,
}
impl Vector2F {
fn x(&self) -> f32 {
self.x
}
fn y(&self) -> f32 {
self.y
}
}
impl From<RectF> for Bounds<f32> {
fn from(rect: RectF) -> Self {
Bounds {
origin: point(rect.origin_x(), rect.origin_y()),
size: size(rect.width(), rect.height()),
}
}
}
impl From<RectI> for Bounds<DevicePixels> {
fn from(rect: RectI) -> Self {
Bounds {
origin: point(DevicePixels(rect.origin_x()), DevicePixels(rect.origin_y())),
size: size(DevicePixels(rect.width()), DevicePixels(rect.height())),
}
}
}
impl From<Vector2I> for Size<DevicePixels> {
fn from(value: Vector2I) -> Self {
size(value.x().into(), value.y().into())
}
}
impl From<RectI> for Bounds<i32> {
fn from(rect: RectI) -> Self {
Bounds {
origin: point(rect.origin_x(), rect.origin_y()),
size: size(rect.width(), rect.height()),
}
}
}
impl From<Point<u32>> for Vector2I {
fn from(size: Point<u32>) -> Self {
Vector2I::new(size.x as i32, size.y as i32)
}
}
impl From<Vector2F> for Size<f32> {
fn from(vec: Vector2F) -> Self {
size(vec.x(), vec.y())
}
}
impl From<FontWeight> for cosmic_text::Weight {
fn from(value: FontWeight) -> Self {
cosmic_text::Weight(value.0 as u16)
}
}
impl From<FontStyle> for cosmic_text::Style {
fn from(style: FontStyle) -> Self {
match style {
FontStyle::Normal => cosmic_text::Style::Normal,
FontStyle::Italic => cosmic_text::Style::Italic,
FontStyle::Oblique => cosmic_text::Style::Oblique,
}
}
}
#[derive(Clone, Copy, PartialEq)]
enum FontMatchStyle {
Normal,
Italic,
Oblique,
}
#[derive(Clone, Copy, PartialEq, PartialOrd)]
struct FontMatchWeight(f32);
#[derive(Clone, Copy, PartialEq, PartialOrd)]
struct FontMatchStretch(f32);
impl FontMatchStretch {
const ULTRA_CONDENSED: Self = Self(0.5);
const EXTRA_CONDENSED: Self = Self(0.625);
const CONDENSED: Self = Self(0.75);
const SEMI_CONDENSED: Self = Self(0.875);
const NORMAL: Self = Self(1.0);
const SEMI_EXPANDED: Self = Self(1.125);
const EXPANDED: Self = Self(1.25);
const EXTRA_EXPANDED: Self = Self(1.5);
const ULTRA_EXPANDED: Self = Self(2.0);
}
#[derive(Clone, Copy)]
struct FontMatchProperties {
style: FontMatchStyle,
weight: FontMatchWeight,
stretch: FontMatchStretch,
}
fn font_into_properties(font: &crate::Font) -> FontMatchProperties {
FontMatchProperties {
style: match font.style {
crate::FontStyle::Normal => FontMatchStyle::Normal,
crate::FontStyle::Italic => FontMatchStyle::Italic,
crate::FontStyle::Oblique => FontMatchStyle::Oblique,
},
weight: FontMatchWeight(font.weight.0),
stretch: FontMatchStretch::NORMAL,
}
}
fn find_best_match(
candidates: &[FontMatchProperties],
query: &FontMatchProperties,
) -> Result<usize> {
let mut matching_set: Vec<usize> = (0..candidates.len()).collect();
if matching_set.is_empty() {
anyhow::bail!("no candidate fonts");
}
let matching_stretch = if matching_set
.iter()
.any(|&index| candidates[index].stretch == query.stretch)
{
query.stretch
} else if query.stretch <= FontMatchStretch::NORMAL {
match matching_set
.iter()
.filter(|&&index| candidates[index].stretch < query.stretch)
.min_by(|&&a, &&b| {
(query.stretch.0 - candidates[a].stretch.0)
.total_cmp(&(query.stretch.0 - candidates[b].stretch.0))
}) {
Some(&matching_index) => candidates[matching_index].stretch,
None => {
let matching_index = *matching_set
.iter()
.min_by(|&&a, &&b| {
(candidates[a].stretch.0 - query.stretch.0)
.total_cmp(&(candidates[b].stretch.0 - query.stretch.0))
})
.expect("matching_set is non-empty");
candidates[matching_index].stretch
}
}
} else {
match matching_set
.iter()
.filter(|&&index| candidates[index].stretch > query.stretch)
.min_by(|&&a, &&b| {
(candidates[a].stretch.0 - query.stretch.0)
.total_cmp(&(candidates[b].stretch.0 - query.stretch.0))
}) {
Some(&matching_index) => candidates[matching_index].stretch,
None => {
let matching_index = *matching_set
.iter()
.min_by(|&&a, &&b| {
(query.stretch.0 - candidates[a].stretch.0)
.total_cmp(&(query.stretch.0 - candidates[b].stretch.0))
})
.expect("matching_set is non-empty");
candidates[matching_index].stretch
}
}
};
matching_set.retain(|&index| candidates[index].stretch == matching_stretch);
let style_preference = match query.style {
FontMatchStyle::Italic => [
FontMatchStyle::Italic,
FontMatchStyle::Oblique,
FontMatchStyle::Normal,
],
FontMatchStyle::Oblique => [
FontMatchStyle::Oblique,
FontMatchStyle::Italic,
FontMatchStyle::Normal,
],
FontMatchStyle::Normal => [
FontMatchStyle::Normal,
FontMatchStyle::Oblique,
FontMatchStyle::Italic,
],
};
let matching_style = *style_preference
.iter()
.find(|&query_style| {
matching_set
.iter()
.any(|&index| candidates[index].style == *query_style)
})
.expect("matching_set is non-empty");
matching_set.retain(|&index| candidates[index].style == matching_style);
let matching_weight = if matching_set
.iter()
.any(|&index| candidates[index].weight == query.weight)
{
query.weight
} else if query.weight >= FontMatchWeight(400.0)
&& query.weight < FontMatchWeight(450.0)
&& matching_set
.iter()
.any(|&index| candidates[index].weight == FontMatchWeight(500.0))
{
FontMatchWeight(500.0)
} else if query.weight >= FontMatchWeight(450.0)
&& query.weight <= FontMatchWeight(500.0)
&& matching_set
.iter()
.any(|&index| candidates[index].weight == FontMatchWeight(400.0))
{
FontMatchWeight(400.0)
} else if query.weight <= FontMatchWeight(500.0) {
match matching_set
.iter()
.filter(|&&index| candidates[index].weight <= query.weight)
.min_by(|&&a, &&b| {
(query.weight.0 - candidates[a].weight.0)
.total_cmp(&(query.weight.0 - candidates[b].weight.0))
}) {
Some(&matching_index) => candidates[matching_index].weight,
None => {
let matching_index = *matching_set
.iter()
.min_by(|&&a, &&b| {
(candidates[a].weight.0 - query.weight.0)
.total_cmp(&(candidates[b].weight.0 - query.weight.0))
})
.expect("matching_set is non-empty");
candidates[matching_index].weight
}
}
} else {
match matching_set
.iter()
.filter(|&&index| candidates[index].weight >= query.weight)
.min_by(|&&a, &&b| {
(candidates[a].weight.0 - query.weight.0)
.total_cmp(&(candidates[b].weight.0 - query.weight.0))
}) {
Some(&matching_index) => candidates[matching_index].weight,
None => {
let matching_index = *matching_set
.iter()
.min_by(|&&a, &&b| {
(query.weight.0 - candidates[a].weight.0)
.total_cmp(&(query.weight.0 - candidates[b].weight.0))
})
.expect("matching_set is non-empty");
candidates[matching_index].weight
}
}
};
matching_set.retain(|&index| candidates[index].weight == matching_weight);
matching_set
.into_iter()
.next()
.context("no matching font found")
}
fn face_info_into_properties(face_info: &cosmic_text::fontdb::FaceInfo) -> FontMatchProperties {
FontMatchProperties {
style: match face_info.style {
cosmic_text::Style::Normal => FontMatchStyle::Normal,
cosmic_text::Style::Italic => FontMatchStyle::Italic,
cosmic_text::Style::Oblique => FontMatchStyle::Oblique,
},
weight: FontMatchWeight(face_info.weight.0.into()),
stretch: match face_info.stretch {
cosmic_text::Stretch::Condensed => FontMatchStretch::CONDENSED,
cosmic_text::Stretch::Expanded => FontMatchStretch::EXPANDED,
cosmic_text::Stretch::ExtraCondensed => FontMatchStretch::EXTRA_CONDENSED,
cosmic_text::Stretch::ExtraExpanded => FontMatchStretch::EXTRA_EXPANDED,
cosmic_text::Stretch::Normal => FontMatchStretch::NORMAL,
cosmic_text::Stretch::SemiCondensed => FontMatchStretch::SEMI_CONDENSED,
cosmic_text::Stretch::SemiExpanded => FontMatchStretch::SEMI_EXPANDED,
cosmic_text::Stretch::UltraCondensed => FontMatchStretch::ULTRA_CONDENSED,
cosmic_text::Stretch::UltraExpanded => FontMatchStretch::ULTRA_EXPANDED,
},
}
}
fn check_is_known_emoji_font(postscript_name: &str) -> bool {
postscript_name == "NotoColorEmoji"
}