1use anyhow::{Context as _, Ok, Result};
2use collections::HashMap;
3use cosmic_text::{
4 Attrs, AttrsList, Ellipsize, Family, Font as CosmicTextFont,
5 FontFeatures as CosmicFontFeatures, FontSystem, ShapeBuffer, ShapeLine,
6};
7use gpui::{
8 Bounds, DevicePixels, Font, FontFallbacks, FontFeatures, FontId, FontMetrics, FontRun, GlyphId,
9 LineLayout, Pixels, PlatformTextSystem, RenderGlyphParams, SUBPIXEL_VARIANTS_X,
10 SUBPIXEL_VARIANTS_Y, ShapedGlyph, ShapedRun, SharedString, Size, TextRenderingMode, point,
11 size,
12};
13
14use itertools::Itertools;
15use parking_lot::RwLock;
16use smallvec::SmallVec;
17use std::{borrow::Cow, ops::Range, sync::Arc};
18use swash::{
19 scale::{Render, ScaleContext, Source, StrikeWith},
20 zeno::{Format, Vector},
21};
22use unicode_segmentation::UnicodeSegmentation;
23
24pub struct CosmicTextSystem(RwLock<CosmicTextSystemState>);
25
26#[derive(Debug, Clone, PartialEq, Eq, Hash)]
27struct FontKey {
28 family: SharedString,
29 features: FontFeatures,
30 fallbacks: Option<FontFallbacks>,
31}
32
33impl FontKey {
34 fn new(family: SharedString, features: FontFeatures, fallbacks: Option<FontFallbacks>) -> Self {
35 Self {
36 family,
37 features,
38 fallbacks,
39 }
40 }
41}
42
43struct CosmicTextSystemState {
44 font_system: FontSystem,
45 scratch: ShapeBuffer,
46 swash_scale_context: ScaleContext,
47 loaded_fonts: Vec<LoadedFont>,
49 font_ids_by_family_cache: HashMap<FontKey, SmallVec<[FontId; 4]>>,
52 system_font_fallback: String,
53}
54
55struct LoadedFont {
56 font: Arc<CosmicTextFont>,
57 features: CosmicFontFeatures,
58 is_known_emoji_font: bool,
59 user_fallback_chain: Arc<[(FontId, SharedString)]>,
62}
63
64impl CosmicTextSystem {
65 pub fn new(system_font_fallback: &str) -> Self {
66 let font_system = FontSystem::new();
67
68 Self(RwLock::new(CosmicTextSystemState {
69 font_system,
70 scratch: ShapeBuffer::default(),
71 swash_scale_context: ScaleContext::new(),
72 loaded_fonts: Vec::new(),
73 font_ids_by_family_cache: HashMap::default(),
74 system_font_fallback: system_font_fallback.to_string(),
75 }))
76 }
77
78 pub fn new_without_system_fonts(system_font_fallback: &str) -> Self {
79 let font_system = FontSystem::new_with_locale_and_db(
80 "en-US".to_string(),
81 cosmic_text::fontdb::Database::new(),
82 );
83
84 Self(RwLock::new(CosmicTextSystemState {
85 font_system,
86 scratch: ShapeBuffer::default(),
87 swash_scale_context: ScaleContext::new(),
88 loaded_fonts: Vec::new(),
89 font_ids_by_family_cache: HashMap::default(),
90 system_font_fallback: system_font_fallback.to_string(),
91 }))
92 }
93}
94
95impl PlatformTextSystem for CosmicTextSystem {
96 fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
97 self.0.write().add_fonts(fonts)
98 }
99
100 fn all_font_names(&self) -> Vec<String> {
101 let mut result = self
102 .0
103 .read()
104 .font_system
105 .db()
106 .faces()
107 .filter_map(|face| face.families.first().map(|family| family.0.clone()))
108 .collect_vec();
109 result.sort_unstable();
110 result.dedup();
111 result
112 }
113
114 fn font_id(&self, font: &Font) -> Result<FontId> {
115 let mut state = self.0.write();
116 let key = FontKey::new(
117 font.family.clone(),
118 font.features.clone(),
119 font.fallbacks.clone(),
120 );
121 let candidates = if let Some(font_ids) = state.font_ids_by_family_cache.get(&key) {
122 font_ids.as_slice()
123 } else {
124 let font_ids =
125 state.load_family(&font.family, &font.features, font.fallbacks.as_ref())?;
126 state.font_ids_by_family_cache.insert(key.clone(), font_ids);
127 state.font_ids_by_family_cache[&key].as_ref()
128 };
129
130 let ix = find_best_match(font, candidates, &state)?;
131
132 Ok(candidates[ix])
133 }
134
135 fn font_metrics(&self, font_id: FontId) -> FontMetrics {
136 let metrics = self
137 .0
138 .read()
139 .loaded_font(font_id)
140 .font
141 .as_swash()
142 .metrics(&[]);
143
144 FontMetrics {
145 units_per_em: metrics.units_per_em as u32,
146 ascent: metrics.ascent,
147 descent: -metrics.descent,
148 line_gap: metrics.leading,
149 underline_position: metrics.underline_offset,
150 underline_thickness: metrics.stroke_size,
151 cap_height: metrics.cap_height,
152 x_height: metrics.x_height,
153 bounding_box: Bounds {
154 origin: point(0.0, 0.0),
155 size: size(metrics.max_width, metrics.ascent + metrics.descent),
156 },
157 }
158 }
159
160 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>> {
161 let lock = self.0.read();
162 let glyph_metrics = lock.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
163 let glyph_id = glyph_id.0 as u16;
164 Ok(Bounds {
165 origin: point(0.0, 0.0),
166 size: size(
167 glyph_metrics.advance_width(glyph_id),
168 glyph_metrics.advance_height(glyph_id),
169 ),
170 })
171 }
172
173 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
174 self.0.read().advance(font_id, glyph_id)
175 }
176
177 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
178 self.0.read().glyph_for_char(font_id, ch)
179 }
180
181 fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
182 self.0.write().raster_bounds(params)
183 }
184
185 fn rasterize_glyph(
186 &self,
187 params: &RenderGlyphParams,
188 raster_bounds: Bounds<DevicePixels>,
189 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
190 self.0.write().rasterize_glyph(params, raster_bounds)
191 }
192
193 fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout {
194 self.0.write().layout_line(text, font_size, runs)
195 }
196
197 fn recommended_rendering_mode(
198 &self,
199 _font_id: FontId,
200 _font_size: Pixels,
201 ) -> TextRenderingMode {
202 TextRenderingMode::Subpixel
203 }
204}
205
206impl CosmicTextSystemState {
207 fn loaded_font(&self, font_id: FontId) -> &LoadedFont {
208 &self.loaded_fonts[font_id.0]
209 }
210
211 #[profiling::function]
212 fn add_fonts(&mut self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
213 let db = self.font_system.db_mut();
214 for bytes in fonts {
215 match bytes {
216 Cow::Borrowed(embedded_font) => {
217 db.load_font_data(embedded_font.to_vec());
218 }
219 Cow::Owned(bytes) => {
220 db.load_font_data(bytes);
221 }
222 }
223 }
224 Ok(())
225 }
226
227 #[profiling::function]
228 fn load_family(
229 &mut self,
230 name: &str,
231 features: &FontFeatures,
232 fallbacks: Option<&FontFallbacks>,
233 ) -> Result<SmallVec<[FontId; 4]>> {
234 let user_fallback_chain: Arc<[(FontId, SharedString)]> = match fallbacks {
238 Some(fallbacks) if !fallbacks.fallback_list().is_empty() => {
239 let mut chain: Vec<(FontId, SharedString)> = Vec::new();
240 for fallback_name in fallbacks.fallback_list() {
241 let fb_key = FontKey::new(
242 SharedString::from(fallback_name.clone()),
243 features.clone(),
244 None,
245 );
246 let fb_ids = if let Some(cached) = self.font_ids_by_family_cache.get(&fb_key) {
247 cached.clone()
248 } else {
249 let loaded = self.load_family(fallback_name, features, None)?;
250 self.font_ids_by_family_cache
251 .insert(fb_key.clone(), loaded.clone());
252 loaded
253 };
254 let Some(&fb_id) = fb_ids.first() else {
255 continue;
256 };
257 let db_id = self.loaded_fonts[fb_id.0].font.id();
258 if let Some(face) = self.font_system.db().face(db_id)
259 && let Some(family) = face.families.first()
260 {
261 chain.push((fb_id, SharedString::from(family.0.clone())));
262 }
263 }
264 Arc::from(chain)
265 }
266 _ => Arc::from(Vec::new()),
267 };
268
269 let name = gpui::font_name_with_fallbacks(name, &self.system_font_fallback);
270
271 let families = self
272 .font_system
273 .db()
274 .faces()
275 .filter(|face| face.families.iter().any(|family| *name == family.0))
276 .map(|face| (face.id, face.post_script_name.clone()))
277 .collect::<SmallVec<[_; 4]>>();
278
279 let cosmic_features = cosmic_font_features(features)?;
280
281 let mut loaded_font_ids = SmallVec::new();
282 for (font_id, postscript_name) in families {
283 let font = self
284 .font_system
285 .get_font(font_id, cosmic_text::Weight::NORMAL)
286 .context("Could not load font")?;
287
288 let allowed_bad_font_names = [
290 "SegoeFluentIcons", "Segoe Fluent Icons",
292 ];
293
294 if font.as_swash().charmap().map('m') == 0
303 && !allowed_bad_font_names.contains(&postscript_name.as_str())
304 {
305 continue;
306 };
307
308 let font_id = FontId(self.loaded_fonts.len());
309 loaded_font_ids.push(font_id);
310 self.loaded_fonts.push(LoadedFont {
311 font,
312 features: cosmic_features.clone(),
313 is_known_emoji_font: check_is_known_emoji_font(&postscript_name),
314 user_fallback_chain: Arc::clone(&user_fallback_chain),
315 });
316 }
317
318 Ok(loaded_font_ids)
319 }
320
321 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
322 let glyph_metrics = self.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
323 Ok(Size {
324 width: glyph_metrics.advance_width(glyph_id.0 as u16),
325 height: glyph_metrics.advance_height(glyph_id.0 as u16),
326 })
327 }
328
329 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
330 let glyph_id = self.loaded_font(font_id).font.as_swash().charmap().map(ch);
331 if glyph_id == 0 {
332 None
333 } else {
334 Some(GlyphId(glyph_id.into()))
335 }
336 }
337
338 fn raster_bounds(&mut self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
339 let image = self.render_glyph_image(params)?;
340 Ok(Bounds {
341 origin: point(image.placement.left.into(), (-image.placement.top).into()),
342 size: size(image.placement.width.into(), image.placement.height.into()),
343 })
344 }
345
346 #[profiling::function]
347 fn rasterize_glyph(
348 &mut self,
349 params: &RenderGlyphParams,
350 glyph_bounds: Bounds<DevicePixels>,
351 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
352 if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
353 anyhow::bail!("glyph bounds are empty");
354 }
355
356 let mut image = self.render_glyph_image(params)?;
357 let bitmap_size = glyph_bounds.size;
358 match image.content {
359 swash::scale::image::Content::Color | swash::scale::image::Content::SubpixelMask => {
360 for pixel in image.data.chunks_exact_mut(4) {
362 pixel.swap(0, 2);
363 }
364 Ok((bitmap_size, image.data))
365 }
366 swash::scale::image::Content::Mask => {
367 if params.subpixel_rendering {
368 let expanded = image.data.iter().flat_map(|&a| [a, a, a, a]).collect();
370 Ok((bitmap_size, expanded))
371 } else {
372 Ok((bitmap_size, image.data))
373 }
374 }
375 }
376 }
377
378 fn render_glyph_image(
379 &mut self,
380 params: &RenderGlyphParams,
381 ) -> Result<swash::scale::image::Image> {
382 let loaded_font = &self.loaded_fonts[params.font_id.0];
383 let font_ref = loaded_font.font.as_swash();
384 let pixel_size = f32::from(params.font_size);
385
386 let subpixel_offset = Vector::new(
387 params.subpixel_variant.x as f32 / SUBPIXEL_VARIANTS_X as f32 / params.scale_factor,
388 params.subpixel_variant.y as f32 / SUBPIXEL_VARIANTS_Y as f32 / params.scale_factor,
389 );
390
391 let mut scaler = self
392 .swash_scale_context
393 .builder(font_ref)
394 .size(pixel_size * params.scale_factor)
395 .hint(true)
396 .build();
397
398 let sources: &[Source] = if params.is_emoji {
399 &[
400 Source::ColorOutline(0),
401 Source::ColorBitmap(StrikeWith::BestFit),
402 Source::Outline,
403 ]
404 } else {
405 &[Source::Bitmap(StrikeWith::ExactSize), Source::Outline]
406 };
407
408 let mut renderer = Render::new(sources);
409 if params.subpixel_rendering {
410 renderer
412 .format(Format::subpixel_bgra())
413 .offset(subpixel_offset);
414 } else {
415 renderer.format(Format::Alpha).offset(subpixel_offset);
416 }
417
418 let glyph_id: u16 = params.glyph_id.0.try_into()?;
419 renderer
420 .render(&mut scaler, glyph_id)
421 .with_context(|| format!("unable to render glyph via swash for {params:?}"))
422 }
423
424 fn font_id_for_cosmic_id(&mut self, id: cosmic_text::fontdb::ID) -> Result<FontId> {
433 if let Some(ix) = self
434 .loaded_fonts
435 .iter()
436 .position(|loaded_font| loaded_font.font.id() == id)
437 {
438 Ok(FontId(ix))
439 } else {
440 let font = self
441 .font_system
442 .get_font(id, cosmic_text::Weight::NORMAL)
443 .context("failed to get fallback font from cosmic-text font system")?;
444 let face = self
445 .font_system
446 .db()
447 .face(id)
448 .context("fallback font face not found in cosmic-text database")?;
449
450 let font_id = FontId(self.loaded_fonts.len());
451 self.loaded_fonts.push(LoadedFont {
452 font,
453 features: CosmicFontFeatures::new(),
454 is_known_emoji_font: check_is_known_emoji_font(&face.post_script_name),
455 user_fallback_chain: Arc::from(Vec::new()),
456 });
457
458 Ok(font_id)
459 }
460 }
461
462 #[profiling::function]
463 fn layout_line(&mut self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
464 if contains_paragraph_separator(text) {
465 self.layout_line_with_separators(text, font_size, font_runs)
466 } else {
467 self.layout_line_no_separators(text, font_size, font_runs)
468 }
469 }
470
471 fn layout_line_with_separators(
472 &mut self,
473 text: &str,
474 font_size: Pixels,
475 font_runs: &[FontRun],
476 ) -> LineLayout {
477 let mut layout = LineLayout {
478 font_size,
479 len: text.len(),
480 ..Default::default()
481 };
482 let mut paragraph_start = 0;
483
484 for (separator_start, separator) in text
485 .char_indices()
486 .filter(|(_, character)| is_paragraph_separator(*character))
487 {
488 let separator_end = separator_start + separator.len_utf8();
489 self.shape_segment(
490 text,
491 paragraph_start..separator_start,
492 font_size,
493 font_runs,
494 &mut layout,
495 );
496 self.shape_segment(
497 text,
498 separator_start..separator_end,
499 font_size,
500 font_runs,
501 &mut layout,
502 );
503 paragraph_start = separator_end;
504 }
505
506 self.shape_segment(
507 text,
508 paragraph_start..text.len(),
509 font_size,
510 font_runs,
511 &mut layout,
512 );
513
514 layout
515 }
516
517 fn shape_segment(
518 &mut self,
519 text: &str,
520 range: Range<usize>,
521 font_size: Pixels,
522 font_runs: &[FontRun],
523 layout: &mut LineLayout,
524 ) {
525 if range.is_empty() {
526 return;
527 }
528
529 let segment_font_runs = clip_font_runs(font_runs, range.clone());
530 let segment =
531 self.layout_line_no_separators(&text[range.clone()], font_size, &segment_font_runs);
532
533 let mut segment_runs = segment.runs;
534 for run in &mut segment_runs {
535 for glyph in &mut run.glyphs {
536 glyph.index += range.start;
537 glyph.position.x += layout.width;
538 }
539 }
540
541 for mut run in segment_runs {
542 if let Some(same_run) = layout
543 .runs
544 .last_mut()
545 .filter(|last| last.font_id == run.font_id)
546 {
547 same_run.glyphs.append(&mut run.glyphs);
548 } else {
549 layout.runs.push(run);
550 }
551 }
552
553 layout.width += segment.width;
554 layout.ascent = layout.ascent.max(segment.ascent);
555 layout.descent = layout.descent.max(segment.descent);
556 }
557
558 fn layout_line_no_separators(
559 &mut self,
560 text: &str,
561 font_size: Pixels,
562 font_runs: &[FontRun],
563 ) -> LineLayout {
564 let mut attrs_list = AttrsList::new(&Attrs::new());
565 let mut offs = 0;
566 for run in font_runs {
567 let run_end = offs + run.len;
568
569 let loaded_font = self.loaded_font(run.font_id);
570 let Some(face) = self.font_system.db().face(loaded_font.font.id()) else {
571 log::warn!(
572 "font face not found in database for font_id {:?}",
573 run.font_id
574 );
575 offs = run_end;
576 continue;
577 };
578 let Some(first_family) = face.families.first() else {
579 log::warn!(
580 "font face has no family names for font_id {:?}",
581 run.font_id
582 );
583 offs = run_end;
584 continue;
585 };
586
587 let primary_family_name: SharedString = first_family.0.clone().into();
588 let primary_stretch = face.stretch;
589 let primary_style = face.style;
590 let primary_weight = face.weight;
591 let primary_features = loaded_font.features.clone();
592 let fallback_chain = Arc::clone(&loaded_font.user_fallback_chain);
593
594 let primary_attrs = Attrs::new()
597 .metadata(run.font_id.0)
598 .family(Family::Name(&primary_family_name))
599 .stretch(primary_stretch)
600 .style(primary_style)
601 .weight(primary_weight)
602 .font_features(primary_features.clone());
603 let fallback_attrs: SmallVec<[Attrs<'_>; 4]> = fallback_chain
604 .iter()
605 .map(|(fb_id, fb_name)| {
606 Attrs::new()
607 .metadata(fb_id.0)
608 .family(Family::Name(fb_name))
609 .stretch(primary_stretch)
610 .style(primary_style)
611 .weight(primary_weight)
612 .font_features(primary_features.clone())
613 })
614 .collect();
615
616 let spans = if fallback_chain.is_empty() {
617 let mut spans = SmallVec::<[RunSpan; 4]>::new();
618 spans.push(RunSpan {
619 start: offs,
620 end: run_end,
621 slot: None,
622 font_id: run.font_id,
623 });
624 spans
625 } else {
626 let loaded_fonts = &self.loaded_fonts;
627 let covers = |id: FontId, ch: char| charmap_covers(loaded_fonts, id, ch);
628 compute_run_spans(text, offs, run.len, run.font_id, &fallback_chain, &covers)
629 };
630
631 for span in spans {
632 let attrs = match span.slot {
633 None => &primary_attrs,
634 Some(ix) => &fallback_attrs[ix],
635 };
636 attrs_list.add_span(span.start..span.end, attrs);
637 }
638 offs = run_end;
639 }
640
641 let line = ShapeLine::new(
642 &mut self.font_system,
643 text,
644 &attrs_list,
645 cosmic_text::Shaping::Advanced,
646 4,
647 );
648 let mut layout_lines = Vec::with_capacity(1);
649 line.layout_to_buffer(
650 &mut self.scratch,
651 f32::from(font_size),
652 None, cosmic_text::Wrap::None,
654 Ellipsize::None,
655 None,
656 &mut layout_lines,
657 None,
658 cosmic_text::Hinting::Disabled,
659 );
660
661 let Some(layout) = layout_lines.first() else {
662 return LineLayout {
663 font_size,
664 width: Pixels::ZERO,
665 ascent: Pixels::ZERO,
666 descent: Pixels::ZERO,
667 runs: Vec::new(),
668 len: text.len(),
669 };
670 };
671
672 let mut runs: Vec<ShapedRun> = Vec::new();
673 for glyph in &layout.glyphs {
674 let mut font_id = FontId(glyph.metadata);
675 let mut loaded_font = self.loaded_font(font_id);
676 if loaded_font.font.id() != glyph.font_id {
677 match self.font_id_for_cosmic_id(glyph.font_id) {
678 std::result::Result::Ok(resolved_id) => {
679 font_id = resolved_id;
680 loaded_font = self.loaded_font(font_id);
681 }
682 Err(error) => {
683 log::warn!(
684 "failed to resolve cosmic font id {:?}: {error:#}",
685 glyph.font_id
686 );
687 continue;
688 }
689 }
690 }
691 let is_emoji = loaded_font.is_known_emoji_font;
692
693 if glyph.glyph_id == 3 && is_emoji {
695 continue;
696 }
697
698 let shaped_glyph = ShapedGlyph {
699 id: GlyphId(glyph.glyph_id as u32),
700 position: point(glyph.x.into(), glyph.y.into()),
701 index: glyph.start,
702 is_emoji,
703 };
704
705 if let Some(last_run) = runs
706 .last_mut()
707 .filter(|last_run| last_run.font_id == font_id)
708 {
709 last_run.glyphs.push(shaped_glyph);
710 } else {
711 runs.push(ShapedRun {
712 font_id,
713 glyphs: vec![shaped_glyph],
714 });
715 }
716 }
717
718 LineLayout {
719 font_size,
720 width: layout.w.into(),
721 ascent: layout.max_ascent.into(),
722 descent: layout.max_descent.into(),
723 runs,
724 len: text.len(),
725 }
726 }
727}
728
729#[inline(always)]
730fn is_paragraph_separator(character: char) -> bool {
731 unicode_bidi::bidi_class(character) == unicode_bidi::BidiClass::B
732}
733
734fn contains_paragraph_separator(text: &str) -> bool {
735 if text
736 .bytes()
737 .any(|byte| matches!(byte, b'\n' | b'\r' | 0x1c | 0x1d | 0x1e))
738 {
739 return true;
740 }
741
742 !text.is_ascii() && text.chars().any(is_paragraph_separator)
743}
744
745fn clip_font_runs(font_runs: &[FontRun], range: Range<usize>) -> SmallVec<[FontRun; 4]> {
746 let mut clipped = SmallVec::new();
747 let mut offs = 0;
748 for run in font_runs {
749 let run_start = offs;
750 offs += run.len;
751 if offs <= range.start {
752 continue;
753 }
754 if run_start >= range.end {
755 break;
756 }
757 let start = run_start.max(range.start);
758 let end = offs.min(range.end);
759 if start < end {
760 clipped.push(FontRun {
761 len: end - start,
762 font_id: run.font_id,
763 });
764 }
765 }
766 clipped
767}
768
769#[cfg(feature = "font-kit")]
770fn find_best_match(
771 font: &Font,
772 candidates: &[FontId],
773 state: &CosmicTextSystemState,
774) -> Result<usize> {
775 let candidate_properties = candidates
776 .iter()
777 .map(|font_id| {
778 let database_id = state.loaded_font(*font_id).font.id();
779 let face_info = state
780 .font_system
781 .db()
782 .face(database_id)
783 .context("font face not found in database")?;
784 Ok(face_info_into_properties(face_info))
785 })
786 .collect::<Result<SmallVec<[_; 4]>>>()?;
787
788 let ix =
789 font_kit::matching::find_best_match(&candidate_properties, &font_into_properties(font))
790 .context("requested font family contains no font matching the other parameters")?;
791
792 Ok(ix)
793}
794
795#[cfg(not(feature = "font-kit"))]
796fn find_best_match(
797 font: &Font,
798 candidates: &[FontId],
799 state: &CosmicTextSystemState,
800) -> Result<usize> {
801 if candidates.is_empty() {
802 anyhow::bail!("requested font family contains no font matching the other parameters");
803 }
804 if candidates.len() == 1 {
805 return Ok(0);
806 }
807
808 let target_weight = font.weight.0;
809 let target_italic = matches!(
810 font.style,
811 gpui::FontStyle::Italic | gpui::FontStyle::Oblique
812 );
813
814 let mut best_index = 0;
815 let mut best_score = u32::MAX;
816
817 for (index, font_id) in candidates.iter().enumerate() {
818 let database_id = state.loaded_font(*font_id).font.id();
819 let face_info = state
820 .font_system
821 .db()
822 .face(database_id)
823 .context("font face not found in database")?;
824
825 let is_italic = matches!(
826 face_info.style,
827 cosmic_text::Style::Italic | cosmic_text::Style::Oblique
828 );
829 let style_penalty: u32 = if is_italic == target_italic { 0 } else { 1000 };
830 let weight_diff = (face_info.weight.0 as i32 - target_weight as i32).unsigned_abs();
831 let score = style_penalty + weight_diff;
832
833 if score < best_score {
834 best_score = score;
835 best_index = index;
836 }
837 }
838
839 Ok(best_index)
840}
841
842#[derive(Debug, Clone, Copy, PartialEq, Eq)]
845struct RunSpan {
846 start: usize,
847 end: usize,
848 slot: Option<usize>,
849 font_id: FontId,
850}
851
852fn compute_run_spans(
856 text: &str,
857 run_offset: usize,
858 run_len: usize,
859 primary: FontId,
860 fallback_chain: &[(FontId, SharedString)],
861 covers: &impl Fn(FontId, char) -> bool,
862) -> SmallVec<[RunSpan; 4]> {
863 let mut spans = SmallVec::new();
864 let run_end = run_offset + run_len;
865 if run_end <= run_offset {
866 return spans;
867 }
868 if fallback_chain.is_empty() {
869 spans.push(RunSpan {
870 start: run_offset,
871 end: run_end,
872 slot: None,
873 font_id: primary,
874 });
875 return spans;
876 }
877 let run_text = &text[run_offset..run_end];
878 let mut span_start = run_offset;
879 let mut span_slot: Option<usize> = None;
880 let mut span_font_id = primary;
881 for (grapheme_idx, grapheme) in run_text.grapheme_indices(true) {
882 let abs = run_offset + grapheme_idx;
883 let ch = grapheme.chars().next().unwrap_or('\0');
884 let next_slot = pick_covering_slot(ch, span_slot, primary, fallback_chain, covers);
885 if next_slot == span_slot {
886 continue;
887 }
888 if abs > span_start {
889 spans.push(RunSpan {
890 start: span_start,
891 end: abs,
892 slot: span_slot,
893 font_id: span_font_id,
894 });
895 }
896 span_start = abs;
897 span_slot = next_slot;
898 span_font_id = slot_font_id(next_slot, primary, fallback_chain);
899 }
900 if span_start < run_end {
901 spans.push(RunSpan {
902 start: span_start,
903 end: run_end,
904 slot: span_slot,
905 font_id: span_font_id,
906 });
907 }
908 spans
909}
910
911fn slot_font_id(
912 slot: Option<usize>,
913 primary: FontId,
914 fallback_chain: &[(FontId, SharedString)],
915) -> FontId {
916 match slot {
917 None => primary,
918 Some(ix) => fallback_chain[ix].0,
919 }
920}
921
922fn pick_covering_slot(
923 ch: char,
924 current: Option<usize>,
925 primary: FontId,
926 fallback_chain: &[(FontId, SharedString)],
927 covers: &impl Fn(FontId, char) -> bool,
928) -> Option<usize> {
929 if (ch as u32) <= 0x7F {
930 return None;
931 }
932 if covers(primary, ch) {
933 return None;
934 }
935 let current_id = slot_font_id(current, primary, fallback_chain);
936 if covers(current_id, ch) {
937 return current;
938 }
939 for (ix, (fb_id, _)) in fallback_chain.iter().enumerate() {
940 if covers(*fb_id, ch) {
941 return Some(ix);
942 }
943 }
944 None
945}
946
947fn charmap_covers(loaded_fonts: &[LoadedFont], id: FontId, ch: char) -> bool {
948 loaded_fonts
949 .get(id.0)
950 .is_some_and(|loaded| loaded.font.as_swash().charmap().map(ch) != 0)
951}
952
953fn cosmic_font_features(features: &FontFeatures) -> Result<CosmicFontFeatures> {
954 let mut result = CosmicFontFeatures::new();
955 for feature in features.0.iter() {
956 let name_bytes: [u8; 4] = feature
957 .0
958 .as_bytes()
959 .try_into()
960 .context("Incorrect feature flag format")?;
961
962 let tag = cosmic_text::FeatureTag::new(&name_bytes);
963
964 result.set(tag, feature.1);
965 }
966 Ok(result)
967}
968
969#[cfg(feature = "font-kit")]
970fn font_into_properties(font: &gpui::Font) -> font_kit::properties::Properties {
971 font_kit::properties::Properties {
972 style: match font.style {
973 gpui::FontStyle::Normal => font_kit::properties::Style::Normal,
974 gpui::FontStyle::Italic => font_kit::properties::Style::Italic,
975 gpui::FontStyle::Oblique => font_kit::properties::Style::Oblique,
976 },
977 weight: font_kit::properties::Weight(font.weight.0),
978 stretch: Default::default(),
979 }
980}
981
982#[cfg(feature = "font-kit")]
983fn face_info_into_properties(
984 face_info: &cosmic_text::fontdb::FaceInfo,
985) -> font_kit::properties::Properties {
986 font_kit::properties::Properties {
987 style: match face_info.style {
988 cosmic_text::Style::Normal => font_kit::properties::Style::Normal,
989 cosmic_text::Style::Italic => font_kit::properties::Style::Italic,
990 cosmic_text::Style::Oblique => font_kit::properties::Style::Oblique,
991 },
992 weight: font_kit::properties::Weight(face_info.weight.0.into()),
993 stretch: match face_info.stretch {
994 cosmic_text::Stretch::Condensed => font_kit::properties::Stretch::CONDENSED,
995 cosmic_text::Stretch::Expanded => font_kit::properties::Stretch::EXPANDED,
996 cosmic_text::Stretch::ExtraCondensed => font_kit::properties::Stretch::EXTRA_CONDENSED,
997 cosmic_text::Stretch::ExtraExpanded => font_kit::properties::Stretch::EXTRA_EXPANDED,
998 cosmic_text::Stretch::Normal => font_kit::properties::Stretch::NORMAL,
999 cosmic_text::Stretch::SemiCondensed => font_kit::properties::Stretch::SEMI_CONDENSED,
1000 cosmic_text::Stretch::SemiExpanded => font_kit::properties::Stretch::SEMI_EXPANDED,
1001 cosmic_text::Stretch::UltraCondensed => font_kit::properties::Stretch::ULTRA_CONDENSED,
1002 cosmic_text::Stretch::UltraExpanded => font_kit::properties::Stretch::ULTRA_EXPANDED,
1003 },
1004 }
1005}
1006
1007fn check_is_known_emoji_font(postscript_name: &str) -> bool {
1008 postscript_name == "NotoColorEmoji"
1010}
1011
1012#[cfg(test)]
1013mod tests {
1014 use super::*;
1015
1016 fn fid(i: usize) -> FontId {
1017 FontId(i)
1018 }
1019
1020 fn chain(ids: &[usize]) -> SmallVec<[(FontId, SharedString); 4]> {
1021 ids.iter()
1022 .map(|&i| (fid(i), SharedString::from(format!("fb{i}"))))
1023 .collect()
1024 }
1025
1026 fn span(start: usize, end: usize, slot: Option<usize>, font_id: FontId) -> RunSpan {
1027 RunSpan {
1028 start,
1029 end,
1030 slot,
1031 font_id,
1032 }
1033 }
1034
1035 const IBM_PLEX: &[u8] = gpui_assets::IBM_PLEX_SANS_REGULAR;
1036
1037 const SEPARATORS: &[char] = &[
1040 '\u{000a}', '\u{000d}', '\u{001c}', '\u{001d}', '\u{001e}', '\u{0085}', '\u{2029}',
1041 ];
1042
1043 fn text_system() -> Result<CosmicTextSystem> {
1044 let text_system = CosmicTextSystem::new_without_system_fonts("IBM Plex Sans");
1045 text_system.add_fonts(vec![Cow::Borrowed(IBM_PLEX)])?;
1046 Ok(text_system)
1047 }
1048
1049 fn layout_text(text_system: &CosmicTextSystem, text: &str) -> Result<LineLayout> {
1050 let font_id = text_system.font_id(&gpui::font("IBM Plex Sans"))?;
1051 let runs = [FontRun {
1052 len: text.len(),
1053 font_id,
1054 }];
1055 Ok(text_system.layout_line(text, gpui::px(14.0), &runs))
1056 }
1057
1058 #[test]
1059 fn background_layout_matches_foreground_with_shared_native_fonts() {
1060 let platform = std::sync::Arc::new(text_system().expect("fixture fonts"));
1061 let shared = std::sync::Arc::new(gpui::TextSystem::new(platform));
1062 let shape = |system: &gpui::WindowTextSystem, index| {
1063 let text: gpui::SharedString =
1064 format!("let value_{index} = \"日本🙂 e\u{301}\";").into();
1065 system.shape_line(
1066 text.clone(),
1067 gpui::px(13.0),
1068 &[gpui::TextRun {
1069 len: text.len(),
1070 font: gpui::font("IBM Plex Sans"),
1071 ..Default::default()
1072 }],
1073 None,
1074 )
1075 };
1076 let baseline_system = gpui::WindowTextSystem::new(shared.clone());
1077 let baseline = (0..32)
1078 .map(|index| {
1079 let line = shape(&baseline_system, index);
1080 (line.len(), line.width())
1081 })
1082 .collect::<Vec<_>>();
1083 let check = || {
1084 let system = gpui::WindowTextSystem::new(shared.clone());
1085 for (index, expected) in baseline.iter().enumerate() {
1086 let line = shape(&system, index);
1087 assert_eq!((line.len(), line.width()), *expected);
1088 assert!(line.width() > gpui::px(0.0));
1089 }
1090 };
1091 std::thread::scope(|scope| {
1092 let worker = scope.spawn(&check);
1093 check();
1094 worker.join().expect("background shaping succeeds");
1095 });
1096 }
1097
1098 #[test]
1101 fn shape_text_with_mixed_direction_paragraphs() -> Result<()> {
1102 let platform_text_system = Arc::new(text_system()?);
1103 let text_system = Arc::new(gpui::TextSystem::new(platform_text_system));
1104 let window_text_system = gpui::WindowTextSystem::new(text_system);
1105
1106 let text: SharedString = "first line\n\u{05d0}\u{001c}A".into();
1107 let runs = [gpui::TextRun {
1108 len: text.len(),
1109 font: gpui::font("IBM Plex Sans"),
1110 ..Default::default()
1111 }];
1112
1113 let lines = window_text_system.shape_text(text, gpui::px(14.0), &runs, None, None)?;
1114
1115 assert_eq!(lines.len(), 2);
1116 assert_eq!(lines[1].len(), "\u{05d0}\u{001c}A".len());
1117 assert!(lines[1].width() > Pixels::ZERO);
1118 Ok(())
1119 }
1120
1121 #[test]
1122 fn layout_line_with_mixed_direction_paragraphs() -> Result<()> {
1123 let text_system = text_system()?;
1124
1125 for separator in SEPARATORS {
1126 for text in [
1127 format!("\u{05d0}{separator}A"),
1128 format!("A{separator}\u{05d0}"),
1129 ] {
1130 let layout = layout_text(&text_system, &text)?;
1131
1132 assert_eq!(layout.len, text.len(), "{text:?}");
1133 assert!(layout.width > Pixels::ZERO, "{text:?}");
1134 assert!(
1135 layout.runs.iter().any(|run| !run.glyphs.is_empty()),
1136 "{text:?}"
1137 );
1138 }
1139 }
1140
1141 Ok(())
1142 }
1143
1144 #[test]
1145 fn layout_line_with_separators_at_line_edges() -> Result<()> {
1146 let text_system = text_system()?;
1147
1148 for text in [
1149 "\u{001c}",
1150 "\u{001c}\u{001c}",
1151 "\u{001c}\u{05d0}",
1152 "\u{05d0}\u{001c}",
1153 "\u{05d0}\u{001c}\u{001c}A",
1154 "\u{001c}\u{05d0}\u{001c}A\u{001c}",
1155 ] {
1156 let layout = layout_text(&text_system, text)?;
1157 assert_eq!(layout.len, text.len(), "{text:?}");
1158 }
1159
1160 Ok(())
1161 }
1162
1163 #[test]
1167 fn layout_line_keeps_indices_and_positions_ordered_across_paragraphs() -> Result<()> {
1168 let text_system = text_system()?;
1169 let text = "ab\u{001c}cd\u{2029}ef";
1170 let layout = layout_text(&text_system, text)?;
1171
1172 let glyphs: Vec<_> = layout.runs.iter().flat_map(|run| &run.glyphs).collect();
1173 assert!(!glyphs.is_empty());
1174
1175 for glyph in &glyphs {
1176 assert!(glyph.index < text.len(), "{:?}", glyph.index);
1177 assert!(text.is_char_boundary(glyph.index), "{:?}", glyph.index);
1178 }
1179 for pair in glyphs.windows(2) {
1180 assert!(pair[0].index < pair[1].index);
1181 assert!(pair[0].position.x <= pair[1].position.x);
1182 }
1183
1184 assert!(layout.width > layout_text(&text_system, "ab")?.width);
1187 Ok(())
1188 }
1189
1190 #[test]
1193 fn layout_line_with_font_run_straddling_a_separator() -> Result<()> {
1194 let text_system = text_system()?;
1195 let font_id = text_system.font_id(&gpui::font("IBM Plex Sans"))?;
1196 let text = "ab\u{001c}\u{05d0}\u{05d1}";
1197
1198 let runs = [
1200 FontRun {
1201 len: "ab\u{001c}\u{05d0}".len(),
1202 font_id,
1203 },
1204 FontRun {
1205 len: "\u{05d1}".len(),
1206 font_id,
1207 },
1208 ];
1209 let layout = text_system.layout_line(text, gpui::px(14.0), &runs);
1210
1211 assert_eq!(layout.len, text.len());
1212 assert!(layout.width > Pixels::ZERO);
1213 Ok(())
1214 }
1215
1216 #[test]
1219 fn layout_line_without_separators_takes_fast_path() -> Result<()> {
1220 let text_system = text_system()?;
1221
1222 for text in [
1223 "hello world",
1224 "\u{05d0}\u{05d1}\u{05d2}",
1225 "mixed \u{05d0}\u{05d1}",
1226 ] {
1227 assert!(!contains_paragraph_separator(text), "{text:?}");
1228 let layout = layout_text(&text_system, text)?;
1229 assert_eq!(layout.len, text.len(), "{text:?}");
1230 assert!(layout.width > Pixels::ZERO, "{text:?}");
1231 }
1232
1233 Ok(())
1234 }
1235
1236 #[test]
1242 fn index_for_position_past_line_width() -> Result<()> {
1243 let text_system = Arc::new(gpui::TextSystem::new(Arc::new(text_system()?)));
1244 let window_text_system = gpui::WindowTextSystem::new(text_system);
1245 let text: SharedString = "Warning: this will delete files\u{200b}".into();
1246 let runs = [gpui::TextRun {
1247 len: text.len(),
1248 font: gpui::font("IBM Plex Sans"),
1249 ..Default::default()
1250 }];
1251 let lines =
1252 window_text_system.shape_text(text, gpui::px(14.), &runs, Some(gpui::px(4.)), None)?;
1253 let line = &lines[0];
1254 let width = line.unwrapped_layout.width;
1255 let boundary_glyph = |row: usize| {
1256 let boundary = line.wrap_boundaries[row];
1257 &line.unwrapped_layout.runs[boundary.run_ix].glyphs[boundary.glyph_ix]
1258 };
1259 let row = (1..line.wrap_boundaries.len())
1260 .find(|row| boundary_glyph(*row).position.x > width)
1261 .expect("trailing zero-width space should be a wrap boundary past the line width");
1262 let row_start_x = f32::from(boundary_glyph(row - 1).position.x);
1263 let row_end = boundary_glyph(row);
1264
1265 let mut x = f32::from(width) - row_start_x;
1266 while x + row_start_x < f32::from(width) {
1267 x = x.next_up();
1268 }
1269 assert!(gpui::px(x + row_start_x) < row_end.position.x);
1270
1271 let line_height = gpui::px(20.);
1272 let position = gpui::point(gpui::px(x), line_height * row as f32 + gpui::px(1.));
1273 assert_eq!(
1274 line.index_for_position(position, line_height),
1275 Err(row_end.index)
1276 );
1277 Ok(())
1278 }
1279
1280 #[test]
1281 fn paragraph_separator_detection() {
1282 for separator in SEPARATORS {
1283 assert!(is_paragraph_separator(*separator), "{separator:?}");
1284 assert!(contains_paragraph_separator(&format!("a{separator}b")));
1285 }
1286
1287 for text in [
1288 "",
1289 "plain ascii",
1290 "\u{05d0}",
1291 "tab\there",
1292 "emoji \u{1f600}",
1293 ] {
1294 assert!(!contains_paragraph_separator(text), "{text:?}");
1295 }
1296 }
1297
1298 #[test]
1299 fn font_runs_are_clipped_to_segment() {
1300 let runs = [
1301 FontRun {
1302 len: 3,
1303 font_id: fid(1),
1304 },
1305 FontRun {
1306 len: 4,
1307 font_id: fid(2),
1308 },
1309 ];
1310
1311 assert_eq!(clip_font_runs(&runs, 0..7).as_slice(), &runs);
1312 assert_eq!(
1313 clip_font_runs(&runs, 2..5).as_slice(),
1314 &[
1315 FontRun {
1316 len: 1,
1317 font_id: fid(1)
1318 },
1319 FontRun {
1320 len: 2,
1321 font_id: fid(2)
1322 },
1323 ]
1324 );
1325 assert_eq!(
1326 clip_font_runs(&runs, 3..7).as_slice(),
1327 &[FontRun {
1328 len: 4,
1329 font_id: fid(2)
1330 }]
1331 );
1332 assert!(clip_font_runs(&runs, 5..5).is_empty());
1333 }
1334
1335 #[test]
1336 fn primary_wins_over_current_fallback_when_primary_covers() {
1337 let primary = fid(0);
1338 let fb = chain(&[1, 2]);
1339 let covers = |id: FontId, _: char| id == fid(0) || id == fid(1);
1340 assert_eq!(
1341 pick_covering_slot('a', Some(0), primary, &fb, &covers),
1342 None
1343 );
1344 }
1345
1346 #[test]
1347 fn primary_preferred_over_fallback_when_both_cover() {
1348 let primary = fid(0);
1349 let fb = chain(&[1]);
1350 let covers = |_: FontId, _: char| true;
1351 assert_eq!(pick_covering_slot('a', None, primary, &fb, &covers), None);
1352 }
1353
1354 #[test]
1355 fn falls_through_chain_in_order() {
1356 let primary = fid(0);
1357 let fb = chain(&[1, 2, 3]);
1358 let covers = |id: FontId, _: char| id == fid(2);
1360 assert_eq!(
1361 pick_covering_slot('字', None, primary, &fb, &covers),
1362 Some(1)
1363 );
1364 }
1365
1366 #[test]
1367 fn no_coverage_returns_primary() {
1368 let primary = fid(0);
1369 let fb = chain(&[1, 2]);
1370 let covers = |_: FontId, _: char| false;
1371 assert_eq!(
1374 pick_covering_slot('\u{1F600}', Some(1), primary, &fb, &covers),
1375 None
1376 );
1377 }
1378
1379 #[test]
1380 fn empty_chain_always_returns_primary() {
1381 let primary = fid(0);
1382 let fb: SmallVec<[(FontId, SharedString); 4]> = SmallVec::new();
1383 let covers = |_: FontId, _: char| false;
1384 assert_eq!(pick_covering_slot('a', None, primary, &fb, &covers), None);
1385 }
1386
1387 #[test]
1388 fn slot_font_id_resolution() {
1389 let primary = fid(7);
1390 let fb = chain(&[10, 20]);
1391 assert_eq!(slot_font_id(None, primary, &fb), fid(7));
1392 assert_eq!(slot_font_id(Some(0), primary, &fb), fid(10));
1393 assert_eq!(slot_font_id(Some(1), primary, &fb), fid(20));
1394 }
1395
1396 #[test]
1397 fn run_spans_with_no_chain_emit_one_primary_span() {
1398 let primary = fid(0);
1399 let fb: SmallVec<[(FontId, SharedString); 4]> = SmallVec::new();
1400 let covers = |_: FontId, _: char| false;
1401 let text = "hello";
1402 let spans = compute_run_spans(text, 0, text.len(), primary, &fb, &covers);
1403 assert_eq!(spans.as_slice(), &[span(0, text.len(), None, primary)]);
1404 }
1405
1406 #[test]
1407 fn run_spans_use_byte_offsets_for_multibyte_chars() {
1408 let primary = fid(0);
1409 let fb = chain(&[1]);
1410 let covers = |id: FontId, ch: char| {
1412 if id == primary {
1413 ch.is_ascii()
1414 } else {
1415 !ch.is_ascii()
1416 }
1417 };
1418 let text = "a字b";
1419 let spans = compute_run_spans(text, 0, text.len(), primary, &fb, &covers);
1420 assert_eq!(
1422 spans.as_slice(),
1423 &[
1424 span(0, 1, None, primary),
1425 span(1, 4, Some(0), fid(1)),
1426 span(4, 5, None, primary),
1427 ]
1428 );
1429 }
1430
1431 #[test]
1432 fn run_spans_respect_run_offset() {
1433 let primary = fid(0);
1434 let fb = chain(&[1]);
1435 let covers = |id: FontId, ch: char| {
1436 if id == primary {
1437 ch.is_ascii()
1438 } else {
1439 !ch.is_ascii()
1440 }
1441 };
1442 let text = "xx字y";
1444 let run_offset = 2;
1445 let run_len = text.len() - run_offset;
1446 let spans = compute_run_spans(text, run_offset, run_len, primary, &fb, &covers);
1447 assert_eq!(
1448 spans.as_slice(),
1449 &[span(2, 5, Some(0), fid(1)), span(5, 6, None, primary)]
1450 );
1451 }
1452
1453 #[test]
1454 fn run_spans_keep_combining_marks_with_base_in_fallback() {
1455 let primary = fid(0);
1456 let fb = chain(&[1]);
1457 let covers = |id: FontId, ch: char| {
1461 if id == primary {
1462 ch.is_ascii()
1463 } else {
1464 ch == '\u{0905}'
1465 }
1466 };
1467 let text = "\u{0905}\u{0902}";
1469 let spans = compute_run_spans(text, 0, text.len(), primary, &fb, &covers);
1470 assert_eq!(spans.as_slice(), &[span(0, text.len(), Some(0), fid(1))]);
1471 }
1472
1473 #[test]
1474 fn run_spans_keep_zwj_inside_emoji_cluster() {
1475 let primary = fid(0);
1476 let fb = chain(&[1]);
1477 let covers = |id: FontId, ch: char| id == fid(1) && ch != '\u{200D}';
1479 let text = "\u{1F469}\u{200D}\u{1F467}";
1481 let spans = compute_run_spans(text, 0, text.len(), primary, &fb, &covers);
1482 assert_eq!(spans.as_slice(), &[span(0, text.len(), Some(0), fid(1))]);
1483 }
1484
1485 #[test]
1486 fn run_spans_collapse_adjacent_same_slot() {
1487 let primary = fid(0);
1488 let fb = chain(&[1]);
1489 let covers = |id: FontId, ch: char| {
1490 if id == primary {
1491 ch.is_ascii()
1492 } else {
1493 !ch.is_ascii()
1494 }
1495 };
1496 let text = "字字字";
1497 let spans = compute_run_spans(text, 0, text.len(), primary, &fb, &covers);
1498 assert_eq!(spans.as_slice(), &[span(0, text.len(), Some(0), fid(1))]);
1499 }
1500
1501 #[test]
1502 fn run_spans_empty_run_returns_no_spans() {
1503 let primary = fid(0);
1504 let fb = chain(&[1]);
1505 let covers = |_: FontId, _: char| true;
1506 let spans = compute_run_spans("anything", 3, 0, primary, &fb, &covers);
1507 assert!(spans.is_empty());
1508 }
1509}