1use anyhow::{Context as _, Ok, Result};
2use collections::HashMap;
3use cosmic_text::{
4 Attrs, AttrsList, Family, Font as CosmicTextFont, FontFeatures as CosmicFontFeatures,
5 FontSystem, ShapeBuffer, ShapeLine,
6};
7use gpui::{
8 Bounds, DevicePixels, Font, FontFeatures, FontId, FontMetrics, FontRun, GlyphId, LineLayout,
9 Pixels, PlatformTextSystem, RenderGlyphParams, SUBPIXEL_VARIANTS_X, SUBPIXEL_VARIANTS_Y,
10 ShapedGlyph, ShapedRun, SharedString, Size, TextRenderingMode, point, size,
11};
12
13use itertools::Itertools;
14use parking_lot::RwLock;
15use smallvec::SmallVec;
16use std::{borrow::Cow, sync::Arc};
17use swash::{
18 scale::{Render, ScaleContext, Source, StrikeWith},
19 zeno::{Format, Vector},
20};
21
22pub struct CosmicTextSystem(RwLock<CosmicTextSystemState>);
23
24#[derive(Debug, Clone, PartialEq, Eq, Hash)]
25struct FontKey {
26 family: SharedString,
27 features: FontFeatures,
28}
29
30impl FontKey {
31 fn new(family: SharedString, features: FontFeatures) -> Self {
32 Self { family, features }
33 }
34}
35
36struct CosmicTextSystemState {
37 font_system: FontSystem,
38 scratch: ShapeBuffer,
39 swash_scale_context: ScaleContext,
40 /// Contains all already loaded fonts, including all faces. Indexed by `FontId`.
41 loaded_fonts: Vec<LoadedFont>,
42 /// Caches the `FontId`s associated with a specific family to avoid iterating the font database
43 /// for every font face in a family.
44 font_ids_by_family_cache: HashMap<FontKey, SmallVec<[FontId; 4]>>,
45 system_font_fallback: String,
46}
47
48struct LoadedFont {
49 font: Arc<CosmicTextFont>,
50 features: CosmicFontFeatures,
51 is_known_emoji_font: bool,
52}
53
54impl CosmicTextSystem {
55 pub fn new(system_font_fallback: &str) -> Self {
56 let font_system = FontSystem::new();
57
58 Self(RwLock::new(CosmicTextSystemState {
59 font_system,
60 scratch: ShapeBuffer::default(),
61 swash_scale_context: ScaleContext::new(),
62 loaded_fonts: Vec::new(),
63 font_ids_by_family_cache: HashMap::default(),
64 system_font_fallback: system_font_fallback.to_string(),
65 }))
66 }
67
68 pub fn new_without_system_fonts(system_font_fallback: &str) -> Self {
69 let font_system = FontSystem::new_with_locale_and_db(
70 "en-US".to_string(),
71 cosmic_text::fontdb::Database::new(),
72 );
73
74 Self(RwLock::new(CosmicTextSystemState {
75 font_system,
76 scratch: ShapeBuffer::default(),
77 swash_scale_context: ScaleContext::new(),
78 loaded_fonts: Vec::new(),
79 font_ids_by_family_cache: HashMap::default(),
80 system_font_fallback: system_font_fallback.to_string(),
81 }))
82 }
83}
84
85impl PlatformTextSystem for CosmicTextSystem {
86 fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
87 self.0.write().add_fonts(fonts)
88 }
89
90 fn all_font_names(&self) -> Vec<String> {
91 let mut result = self
92 .0
93 .read()
94 .font_system
95 .db()
96 .faces()
97 .filter_map(|face| face.families.first().map(|family| family.0.clone()))
98 .collect_vec();
99 result.sort();
100 result.dedup();
101 result
102 }
103
104 fn font_id(&self, font: &Font) -> Result<FontId> {
105 let mut state = self.0.write();
106 let key = FontKey::new(font.family.clone(), font.features.clone());
107 let candidates = if let Some(font_ids) = state.font_ids_by_family_cache.get(&key) {
108 font_ids.as_slice()
109 } else {
110 let font_ids = state.load_family(&font.family, &font.features)?;
111 state.font_ids_by_family_cache.insert(key.clone(), font_ids);
112 state.font_ids_by_family_cache[&key].as_ref()
113 };
114
115 let ix = find_best_match(font, candidates, &state)?;
116
117 Ok(candidates[ix])
118 }
119
120 fn font_metrics(&self, font_id: FontId) -> FontMetrics {
121 let metrics = self
122 .0
123 .read()
124 .loaded_font(font_id)
125 .font
126 .as_swash()
127 .metrics(&[]);
128
129 FontMetrics {
130 units_per_em: metrics.units_per_em as u32,
131 ascent: metrics.ascent,
132 descent: -metrics.descent,
133 line_gap: metrics.leading,
134 underline_position: metrics.underline_offset,
135 underline_thickness: metrics.stroke_size,
136 cap_height: metrics.cap_height,
137 x_height: metrics.x_height,
138 bounding_box: Bounds {
139 origin: point(0.0, 0.0),
140 size: size(metrics.max_width, metrics.ascent + metrics.descent),
141 },
142 }
143 }
144
145 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>> {
146 let lock = self.0.read();
147 let glyph_metrics = lock.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
148 let glyph_id = glyph_id.0 as u16;
149 Ok(Bounds {
150 origin: point(0.0, 0.0),
151 size: size(
152 glyph_metrics.advance_width(glyph_id),
153 glyph_metrics.advance_height(glyph_id),
154 ),
155 })
156 }
157
158 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
159 self.0.read().advance(font_id, glyph_id)
160 }
161
162 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
163 self.0.read().glyph_for_char(font_id, ch)
164 }
165
166 fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
167 self.0.write().raster_bounds(params)
168 }
169
170 fn rasterize_glyph(
171 &self,
172 params: &RenderGlyphParams,
173 raster_bounds: Bounds<DevicePixels>,
174 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
175 self.0.write().rasterize_glyph(params, raster_bounds)
176 }
177
178 fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout {
179 self.0.write().layout_line(text, font_size, runs)
180 }
181
182 fn recommended_rendering_mode(
183 &self,
184 _font_id: FontId,
185 _font_size: Pixels,
186 ) -> TextRenderingMode {
187 TextRenderingMode::Subpixel
188 }
189}
190
191impl CosmicTextSystemState {
192 fn loaded_font(&self, font_id: FontId) -> &LoadedFont {
193 &self.loaded_fonts[font_id.0]
194 }
195
196 #[profiling::function]
197 fn add_fonts(&mut self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
198 let db = self.font_system.db_mut();
199 for bytes in fonts {
200 match bytes {
201 Cow::Borrowed(embedded_font) => {
202 db.load_font_data(embedded_font.to_vec());
203 }
204 Cow::Owned(bytes) => {
205 db.load_font_data(bytes);
206 }
207 }
208 }
209 Ok(())
210 }
211
212 #[profiling::function]
213 fn load_family(
214 &mut self,
215 name: &str,
216 features: &FontFeatures,
217 ) -> Result<SmallVec<[FontId; 4]>> {
218 let name = gpui::font_name_with_fallbacks(name, &self.system_font_fallback);
219
220 let families = self
221 .font_system
222 .db()
223 .faces()
224 .filter(|face| face.families.iter().any(|family| *name == family.0))
225 .map(|face| (face.id, face.post_script_name.clone()))
226 .collect::<SmallVec<[_; 4]>>();
227
228 let mut loaded_font_ids = SmallVec::new();
229 for (font_id, postscript_name) in families {
230 let font = self
231 .font_system
232 .get_font(font_id, cosmic_text::Weight::NORMAL)
233 .context("Could not load font")?;
234
235 // HACK: To let the storybook run and render Windows caption icons. We should actually do better font fallback.
236 let allowed_bad_font_names = [
237 "SegoeFluentIcons", // NOTE: Segoe fluent icons postscript name is inconsistent
238 "Segoe Fluent Icons",
239 ];
240
241 if font.as_swash().charmap().map('m') == 0
242 && !allowed_bad_font_names.contains(&postscript_name.as_str())
243 {
244 self.font_system.db_mut().remove_face(font.id());
245 continue;
246 };
247
248 let font_id = FontId(self.loaded_fonts.len());
249 loaded_font_ids.push(font_id);
250 self.loaded_fonts.push(LoadedFont {
251 font,
252 features: cosmic_font_features(features)?,
253 is_known_emoji_font: check_is_known_emoji_font(&postscript_name),
254 });
255 }
256
257 Ok(loaded_font_ids)
258 }
259
260 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
261 let glyph_metrics = self.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
262 Ok(Size {
263 width: glyph_metrics.advance_width(glyph_id.0 as u16),
264 height: glyph_metrics.advance_height(glyph_id.0 as u16),
265 })
266 }
267
268 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
269 let glyph_id = self.loaded_font(font_id).font.as_swash().charmap().map(ch);
270 if glyph_id == 0 {
271 None
272 } else {
273 Some(GlyphId(glyph_id.into()))
274 }
275 }
276
277 fn raster_bounds(&mut self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
278 let image = self.render_glyph_image(params)?;
279 Ok(Bounds {
280 origin: point(image.placement.left.into(), (-image.placement.top).into()),
281 size: size(image.placement.width.into(), image.placement.height.into()),
282 })
283 }
284
285 #[profiling::function]
286 fn rasterize_glyph(
287 &mut self,
288 params: &RenderGlyphParams,
289 glyph_bounds: Bounds<DevicePixels>,
290 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
291 if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
292 anyhow::bail!("glyph bounds are empty");
293 }
294
295 let mut image = self.render_glyph_image(params)?;
296 let bitmap_size = glyph_bounds.size;
297 match image.content {
298 swash::scale::image::Content::Color | swash::scale::image::Content::SubpixelMask => {
299 // Convert from RGBA to BGRA.
300 for pixel in image.data.chunks_exact_mut(4) {
301 pixel.swap(0, 2);
302 }
303 Ok((bitmap_size, image.data))
304 }
305 swash::scale::image::Content::Mask => Ok((bitmap_size, image.data)),
306 }
307 }
308
309 fn render_glyph_image(
310 &mut self,
311 params: &RenderGlyphParams,
312 ) -> Result<swash::scale::image::Image> {
313 let loaded_font = &self.loaded_fonts[params.font_id.0];
314 let font_ref = loaded_font.font.as_swash();
315 let pixel_size = f32::from(params.font_size);
316
317 let subpixel_offset = Vector::new(
318 params.subpixel_variant.x as f32 / SUBPIXEL_VARIANTS_X as f32 / params.scale_factor,
319 params.subpixel_variant.y as f32 / SUBPIXEL_VARIANTS_Y as f32 / params.scale_factor,
320 );
321
322 let mut scaler = self
323 .swash_scale_context
324 .builder(font_ref)
325 .size(pixel_size * params.scale_factor)
326 .hint(true)
327 .build();
328
329 let sources: &[Source] = if params.is_emoji {
330 &[
331 Source::ColorOutline(0),
332 Source::ColorBitmap(StrikeWith::BestFit),
333 Source::Outline,
334 ]
335 } else {
336 &[Source::Outline]
337 };
338
339 let mut renderer = Render::new(sources);
340 if params.subpixel_rendering {
341 // There seems to be a bug in Swash where the B and R values are swapped.
342 renderer
343 .format(Format::subpixel_bgra())
344 .offset(subpixel_offset);
345 } else {
346 renderer.format(Format::Alpha).offset(subpixel_offset);
347 }
348
349 let glyph_id: u16 = params.glyph_id.0.try_into()?;
350 renderer
351 .render(&mut scaler, glyph_id)
352 .with_context(|| format!("unable to render glyph via swash for {params:?}"))
353 }
354
355 /// This is used when cosmic_text has chosen a fallback font instead of using the requested
356 /// font, typically to handle some unicode characters. When this happens, `loaded_fonts` may not
357 /// yet have an entry for this fallback font, and so one is added.
358 ///
359 /// Note that callers shouldn't use this `FontId` somewhere that will retrieve the corresponding
360 /// `LoadedFont.features`, as it will have an arbitrarily chosen or empty value. The only
361 /// current use of this field is for the *input* of `layout_line`, and so it's fine to use
362 /// `font_id_for_cosmic_id` when computing the *output* of `layout_line`.
363 fn font_id_for_cosmic_id(&mut self, id: cosmic_text::fontdb::ID) -> Result<FontId> {
364 if let Some(ix) = self
365 .loaded_fonts
366 .iter()
367 .position(|loaded_font| loaded_font.font.id() == id)
368 {
369 Ok(FontId(ix))
370 } else {
371 let font = self
372 .font_system
373 .get_font(id, cosmic_text::Weight::NORMAL)
374 .context("failed to get fallback font from cosmic-text font system")?;
375 let face = self
376 .font_system
377 .db()
378 .face(id)
379 .context("fallback font face not found in cosmic-text database")?;
380
381 let font_id = FontId(self.loaded_fonts.len());
382 self.loaded_fonts.push(LoadedFont {
383 font,
384 features: CosmicFontFeatures::new(),
385 is_known_emoji_font: check_is_known_emoji_font(&face.post_script_name),
386 });
387
388 Ok(font_id)
389 }
390 }
391
392 #[profiling::function]
393 fn layout_line(&mut self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
394 let mut attrs_list = AttrsList::new(&Attrs::new());
395 let mut offs = 0;
396 for run in font_runs {
397 let loaded_font = self.loaded_font(run.font_id);
398 let Some(face) = self.font_system.db().face(loaded_font.font.id()) else {
399 log::warn!(
400 "font face not found in database for font_id {:?}",
401 run.font_id
402 );
403 offs += run.len;
404 continue;
405 };
406 let Some(first_family) = face.families.first() else {
407 log::warn!(
408 "font face has no family names for font_id {:?}",
409 run.font_id
410 );
411 offs += run.len;
412 continue;
413 };
414
415 attrs_list.add_span(
416 offs..(offs + run.len),
417 &Attrs::new()
418 .metadata(run.font_id.0)
419 .family(Family::Name(&first_family.0))
420 .stretch(face.stretch)
421 .style(face.style)
422 .weight(face.weight)
423 .font_features(loaded_font.features.clone()),
424 );
425 offs += run.len;
426 }
427
428 let line = ShapeLine::new(
429 &mut self.font_system,
430 text,
431 &attrs_list,
432 cosmic_text::Shaping::Advanced,
433 4,
434 );
435 let mut layout_lines = Vec::with_capacity(1);
436 line.layout_to_buffer(
437 &mut self.scratch,
438 f32::from(font_size),
439 None, // We do our own wrapping
440 cosmic_text::Wrap::None,
441 None,
442 &mut layout_lines,
443 None,
444 cosmic_text::Hinting::Disabled,
445 );
446
447 let Some(layout) = layout_lines.first() else {
448 return LineLayout {
449 font_size,
450 width: Pixels::ZERO,
451 ascent: Pixels::ZERO,
452 descent: Pixels::ZERO,
453 runs: Vec::new(),
454 len: text.len(),
455 };
456 };
457
458 let mut runs: Vec<ShapedRun> = Vec::new();
459 for glyph in &layout.glyphs {
460 let mut font_id = FontId(glyph.metadata);
461 let mut loaded_font = self.loaded_font(font_id);
462 if loaded_font.font.id() != glyph.font_id {
463 match self.font_id_for_cosmic_id(glyph.font_id) {
464 std::result::Result::Ok(resolved_id) => {
465 font_id = resolved_id;
466 loaded_font = self.loaded_font(font_id);
467 }
468 Err(error) => {
469 log::warn!(
470 "failed to resolve cosmic font id {:?}: {error:#}",
471 glyph.font_id
472 );
473 continue;
474 }
475 }
476 }
477 let is_emoji = loaded_font.is_known_emoji_font;
478
479 // HACK: Prevent crash caused by variation selectors.
480 if glyph.glyph_id == 3 && is_emoji {
481 continue;
482 }
483
484 let shaped_glyph = ShapedGlyph {
485 id: GlyphId(glyph.glyph_id as u32),
486 position: point(glyph.x.into(), glyph.y.into()),
487 index: glyph.start,
488 is_emoji,
489 };
490
491 if let Some(last_run) = runs
492 .last_mut()
493 .filter(|last_run| last_run.font_id == font_id)
494 {
495 last_run.glyphs.push(shaped_glyph);
496 } else {
497 runs.push(ShapedRun {
498 font_id,
499 glyphs: vec![shaped_glyph],
500 });
501 }
502 }
503
504 LineLayout {
505 font_size,
506 width: layout.w.into(),
507 ascent: layout.max_ascent.into(),
508 descent: layout.max_descent.into(),
509 runs,
510 len: text.len(),
511 }
512 }
513}
514
515#[cfg(feature = "font-kit")]
516fn find_best_match(
517 font: &Font,
518 candidates: &[FontId],
519 state: &CosmicTextSystemState,
520) -> Result<usize> {
521 let candidate_properties = candidates
522 .iter()
523 .map(|font_id| {
524 let database_id = state.loaded_font(*font_id).font.id();
525 let face_info = state
526 .font_system
527 .db()
528 .face(database_id)
529 .context("font face not found in database")?;
530 Ok(face_info_into_properties(face_info))
531 })
532 .collect::<Result<SmallVec<[_; 4]>>>()?;
533
534 let ix =
535 font_kit::matching::find_best_match(&candidate_properties, &font_into_properties(font))
536 .context("requested font family contains no font matching the other parameters")?;
537
538 Ok(ix)
539}
540
541#[cfg(not(feature = "font-kit"))]
542fn find_best_match(
543 font: &Font,
544 candidates: &[FontId],
545 state: &CosmicTextSystemState,
546) -> Result<usize> {
547 if candidates.is_empty() {
548 anyhow::bail!("requested font family contains no font matching the other parameters");
549 }
550 if candidates.len() == 1 {
551 return Ok(0);
552 }
553
554 let target_weight = font.weight.0;
555 let target_italic = matches!(
556 font.style,
557 gpui::FontStyle::Italic | gpui::FontStyle::Oblique
558 );
559
560 let mut best_index = 0;
561 let mut best_score = u32::MAX;
562
563 for (index, font_id) in candidates.iter().enumerate() {
564 let database_id = state.loaded_font(*font_id).font.id();
565 let face_info = state
566 .font_system
567 .db()
568 .face(database_id)
569 .context("font face not found in database")?;
570
571 let is_italic = matches!(
572 face_info.style,
573 cosmic_text::Style::Italic | cosmic_text::Style::Oblique
574 );
575 let style_penalty: u32 = if is_italic == target_italic { 0 } else { 1000 };
576 let weight_diff = (face_info.weight.0 as i32 - target_weight as i32).unsigned_abs();
577 let score = style_penalty + weight_diff;
578
579 if score < best_score {
580 best_score = score;
581 best_index = index;
582 }
583 }
584
585 Ok(best_index)
586}
587
588fn cosmic_font_features(features: &FontFeatures) -> Result<CosmicFontFeatures> {
589 let mut result = CosmicFontFeatures::new();
590 for feature in features.0.iter() {
591 let name_bytes: [u8; 4] = feature
592 .0
593 .as_bytes()
594 .try_into()
595 .context("Incorrect feature flag format")?;
596
597 let tag = cosmic_text::FeatureTag::new(&name_bytes);
598
599 result.set(tag, feature.1);
600 }
601 Ok(result)
602}
603
604#[cfg(feature = "font-kit")]
605fn font_into_properties(font: &gpui::Font) -> font_kit::properties::Properties {
606 font_kit::properties::Properties {
607 style: match font.style {
608 gpui::FontStyle::Normal => font_kit::properties::Style::Normal,
609 gpui::FontStyle::Italic => font_kit::properties::Style::Italic,
610 gpui::FontStyle::Oblique => font_kit::properties::Style::Oblique,
611 },
612 weight: font_kit::properties::Weight(font.weight.0),
613 stretch: Default::default(),
614 }
615}
616
617#[cfg(feature = "font-kit")]
618fn face_info_into_properties(
619 face_info: &cosmic_text::fontdb::FaceInfo,
620) -> font_kit::properties::Properties {
621 font_kit::properties::Properties {
622 style: match face_info.style {
623 cosmic_text::Style::Normal => font_kit::properties::Style::Normal,
624 cosmic_text::Style::Italic => font_kit::properties::Style::Italic,
625 cosmic_text::Style::Oblique => font_kit::properties::Style::Oblique,
626 },
627 weight: font_kit::properties::Weight(face_info.weight.0.into()),
628 stretch: match face_info.stretch {
629 cosmic_text::Stretch::Condensed => font_kit::properties::Stretch::CONDENSED,
630 cosmic_text::Stretch::Expanded => font_kit::properties::Stretch::EXPANDED,
631 cosmic_text::Stretch::ExtraCondensed => font_kit::properties::Stretch::EXTRA_CONDENSED,
632 cosmic_text::Stretch::ExtraExpanded => font_kit::properties::Stretch::EXTRA_EXPANDED,
633 cosmic_text::Stretch::Normal => font_kit::properties::Stretch::NORMAL,
634 cosmic_text::Stretch::SemiCondensed => font_kit::properties::Stretch::SEMI_CONDENSED,
635 cosmic_text::Stretch::SemiExpanded => font_kit::properties::Stretch::SEMI_EXPANDED,
636 cosmic_text::Stretch::UltraCondensed => font_kit::properties::Stretch::ULTRA_CONDENSED,
637 cosmic_text::Stretch::UltraExpanded => font_kit::properties::Stretch::ULTRA_EXPANDED,
638 },
639 }
640}
641
642fn check_is_known_emoji_font(postscript_name: &str) -> bool {
643 // TODO: Include other common emoji fonts
644 postscript_name == "NotoColorEmoji"
645}