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(crate) 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}
46
47struct LoadedFont {
48 font: Arc<CosmicTextFont>,
49 features: CosmicFontFeatures,
50 is_known_emoji_font: bool,
51}
52
53impl CosmicTextSystem {
54 pub(crate) fn new() -> Self {
55 // todo(linux) make font loading non-blocking
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 }))
65 }
66}
67
68impl Default for CosmicTextSystem {
69 fn default() -> Self {
70 Self::new()
71 }
72}
73
74impl PlatformTextSystem for CosmicTextSystem {
75 fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
76 self.0.write().add_fonts(fonts)
77 }
78
79 fn all_font_names(&self) -> Vec<String> {
80 let mut result = self
81 .0
82 .read()
83 .font_system
84 .db()
85 .faces()
86 .filter_map(|face| face.families.first().map(|family| family.0.clone()))
87 .collect_vec();
88 result.sort();
89 result.dedup();
90 result
91 }
92
93 fn font_id(&self, font: &Font) -> Result<FontId> {
94 // todo(linux): Do we need to use CosmicText's Font APIs? Can we consolidate this to use font_kit?
95 let mut state = self.0.write();
96 let key = FontKey::new(font.family.clone(), font.features.clone());
97 let candidates = if let Some(font_ids) = state.font_ids_by_family_cache.get(&key) {
98 font_ids.as_slice()
99 } else {
100 let font_ids = state.load_family(&font.family, &font.features)?;
101 state.font_ids_by_family_cache.insert(key.clone(), font_ids);
102 state.font_ids_by_family_cache[&key].as_ref()
103 };
104
105 // todo(linux) ideally we would make fontdb's `find_best_match` pub instead of using font-kit here
106 let candidate_properties = candidates
107 .iter()
108 .map(|font_id| {
109 let database_id = state.loaded_font(*font_id).font.id();
110 let face_info = state.font_system.db().face(database_id).expect("");
111 face_info_into_properties(face_info)
112 })
113 .collect::<SmallVec<[_; 4]>>();
114
115 let ix =
116 font_kit::matching::find_best_match(&candidate_properties, &font_into_properties(font))
117 .context("requested font family contains no font matching the other parameters")?;
118
119 Ok(candidates[ix])
120 }
121
122 fn font_metrics(&self, font_id: FontId) -> FontMetrics {
123 let metrics = self
124 .0
125 .read()
126 .loaded_font(font_id)
127 .font
128 .as_swash()
129 .metrics(&[]);
130
131 FontMetrics {
132 units_per_em: metrics.units_per_em as u32,
133 ascent: metrics.ascent,
134 descent: -metrics.descent, // todo(linux) confirm this is correct
135 line_gap: metrics.leading,
136 underline_position: metrics.underline_offset,
137 underline_thickness: metrics.stroke_size,
138 cap_height: metrics.cap_height,
139 x_height: metrics.x_height,
140 // todo(linux): Compute this correctly
141 bounding_box: Bounds {
142 origin: point(0.0, 0.0),
143 size: size(metrics.max_width, metrics.ascent + metrics.descent),
144 },
145 }
146 }
147
148 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>> {
149 let lock = self.0.read();
150 let glyph_metrics = lock.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
151 let glyph_id = glyph_id.0 as u16;
152 // todo(linux): Compute this correctly
153 // see https://github.com/servo/font-kit/blob/master/src/loaders/freetype.rs#L614-L620
154 Ok(Bounds {
155 origin: point(0.0, 0.0),
156 size: size(
157 glyph_metrics.advance_width(glyph_id),
158 glyph_metrics.advance_height(glyph_id),
159 ),
160 })
161 }
162
163 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
164 self.0.read().advance(font_id, glyph_id)
165 }
166
167 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
168 self.0.read().glyph_for_char(font_id, ch)
169 }
170
171 fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
172 self.0.write().raster_bounds(params)
173 }
174
175 fn rasterize_glyph(
176 &self,
177 params: &RenderGlyphParams,
178 raster_bounds: Bounds<DevicePixels>,
179 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
180 self.0.write().rasterize_glyph(params, raster_bounds)
181 }
182
183 fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout {
184 self.0.write().layout_line(text, font_size, runs)
185 }
186
187 fn recommended_rendering_mode(
188 &self,
189 _font_id: FontId,
190 _font_size: Pixels,
191 ) -> TextRenderingMode {
192 // Ideally, we'd use fontconfig to read the user preference.
193 TextRenderingMode::Subpixel
194 }
195}
196
197impl CosmicTextSystemState {
198 fn loaded_font(&self, font_id: FontId) -> &LoadedFont {
199 &self.loaded_fonts[font_id.0]
200 }
201
202 #[profiling::function]
203 fn add_fonts(&mut self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
204 let db = self.font_system.db_mut();
205 for bytes in fonts {
206 match bytes {
207 Cow::Borrowed(embedded_font) => {
208 db.load_font_data(embedded_font.to_vec());
209 }
210 Cow::Owned(bytes) => {
211 db.load_font_data(bytes);
212 }
213 }
214 }
215 Ok(())
216 }
217
218 #[profiling::function]
219 fn load_family(
220 &mut self,
221 name: &str,
222 features: &FontFeatures,
223 ) -> Result<SmallVec<[FontId; 4]>> {
224 // TODO: Determine the proper system UI font.
225 let name = gpui::font_name_with_fallbacks(name, "IBM Plex Sans");
226
227 let families = self
228 .font_system
229 .db()
230 .faces()
231 .filter(|face| face.families.iter().any(|family| *name == family.0))
232 .map(|face| (face.id, face.post_script_name.clone()))
233 .collect::<SmallVec<[_; 4]>>();
234
235 let mut loaded_font_ids = SmallVec::new();
236 for (font_id, postscript_name) in families {
237 let font = self
238 .font_system
239 .get_font(font_id, cosmic_text::Weight::NORMAL)
240 .context("Could not load font")?;
241
242 // HACK: To let the storybook run and render Windows caption icons. We should actually do better font fallback.
243 let allowed_bad_font_names = [
244 "SegoeFluentIcons", // NOTE: Segoe fluent icons postscript name is inconsistent
245 "Segoe Fluent Icons",
246 ];
247
248 if font.as_swash().charmap().map('m') == 0
249 && !allowed_bad_font_names.contains(&postscript_name.as_str())
250 {
251 self.font_system.db_mut().remove_face(font.id());
252 continue;
253 };
254
255 let font_id = FontId(self.loaded_fonts.len());
256 loaded_font_ids.push(font_id);
257 self.loaded_fonts.push(LoadedFont {
258 font,
259 features: cosmic_font_features(features)?,
260 is_known_emoji_font: check_is_known_emoji_font(&postscript_name),
261 });
262 }
263
264 Ok(loaded_font_ids)
265 }
266
267 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
268 let glyph_metrics = self.loaded_font(font_id).font.as_swash().glyph_metrics(&[]);
269 Ok(Size {
270 width: glyph_metrics.advance_width(glyph_id.0 as u16),
271 height: glyph_metrics.advance_height(glyph_id.0 as u16),
272 })
273 }
274
275 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
276 let glyph_id = self.loaded_font(font_id).font.as_swash().charmap().map(ch);
277 if glyph_id == 0 {
278 None
279 } else {
280 Some(GlyphId(glyph_id.into()))
281 }
282 }
283
284 fn raster_bounds(&mut self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
285 let image = self.render_glyph_image(params)?;
286 Ok(Bounds {
287 origin: point(image.placement.left.into(), (-image.placement.top).into()),
288 size: size(image.placement.width.into(), image.placement.height.into()),
289 })
290 }
291
292 #[profiling::function]
293 fn rasterize_glyph(
294 &mut self,
295 params: &RenderGlyphParams,
296 glyph_bounds: Bounds<DevicePixels>,
297 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
298 if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
299 anyhow::bail!("glyph bounds are empty");
300 }
301
302 let mut image = self.render_glyph_image(params)?;
303 let bitmap_size = glyph_bounds.size;
304 match image.content {
305 swash::scale::image::Content::Color | swash::scale::image::Content::SubpixelMask => {
306 // Convert from RGBA to BGRA.
307 for pixel in image.data.chunks_exact_mut(4) {
308 pixel.swap(0, 2);
309 }
310 Ok((bitmap_size, image.data))
311 }
312 swash::scale::image::Content::Mask => Ok((bitmap_size, image.data)),
313 }
314 }
315
316 fn render_glyph_image(
317 &mut self,
318 params: &RenderGlyphParams,
319 ) -> Result<swash::scale::image::Image> {
320 let loaded_font = &self.loaded_fonts[params.font_id.0];
321 let font_ref = loaded_font.font.as_swash();
322 let pixel_size = f32::from(params.font_size);
323
324 let subpixel_offset = Vector::new(
325 params.subpixel_variant.x as f32 / SUBPIXEL_VARIANTS_X as f32 / params.scale_factor,
326 params.subpixel_variant.y as f32 / SUBPIXEL_VARIANTS_Y as f32 / params.scale_factor,
327 );
328
329 let mut scaler = self
330 .swash_scale_context
331 .builder(font_ref)
332 .size(pixel_size * params.scale_factor)
333 .hint(true)
334 .build();
335
336 let sources: &[Source] = if params.is_emoji {
337 &[
338 Source::ColorOutline(0),
339 Source::ColorBitmap(StrikeWith::BestFit),
340 Source::Outline,
341 ]
342 } else {
343 &[Source::Outline]
344 };
345
346 let mut renderer = Render::new(sources);
347 if params.subpixel_rendering {
348 // There seems to be a bug in Swash where the B and R values are swapped.
349 renderer
350 .format(Format::subpixel_bgra())
351 .offset(subpixel_offset);
352 } else {
353 renderer.format(Format::Alpha).offset(subpixel_offset);
354 }
355
356 let glyph_id: u16 = params.glyph_id.0.try_into()?;
357 renderer
358 .render(&mut scaler, glyph_id)
359 .with_context(|| format!("unable to render glyph via swash for {params:?}"))
360 }
361
362 /// This is used when cosmic_text has chosen a fallback font instead of using the requested
363 /// font, typically to handle some unicode characters. When this happens, `loaded_fonts` may not
364 /// yet have an entry for this fallback font, and so one is added.
365 ///
366 /// Note that callers shouldn't use this `FontId` somewhere that will retrieve the corresponding
367 /// `LoadedFont.features`, as it will have an arbitrarily chosen or empty value. The only
368 /// current use of this field is for the *input* of `layout_line`, and so it's fine to use
369 /// `font_id_for_cosmic_id` when computing the *output* of `layout_line`.
370 fn font_id_for_cosmic_id(&mut self, id: cosmic_text::fontdb::ID) -> FontId {
371 if let Some(ix) = self
372 .loaded_fonts
373 .iter()
374 .position(|loaded_font| loaded_font.font.id() == id)
375 {
376 FontId(ix)
377 } else {
378 let font = self
379 .font_system
380 .get_font(id, cosmic_text::Weight::NORMAL)
381 .unwrap();
382 let face = self.font_system.db().face(id).unwrap();
383
384 let font_id = FontId(self.loaded_fonts.len());
385 self.loaded_fonts.push(LoadedFont {
386 font,
387 features: CosmicFontFeatures::new(),
388 is_known_emoji_font: check_is_known_emoji_font(&face.post_script_name),
389 });
390
391 font_id
392 }
393 }
394
395 #[profiling::function]
396 fn layout_line(&mut self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
397 let mut attrs_list = AttrsList::new(&Attrs::new());
398 let mut offs = 0;
399 for run in font_runs {
400 let loaded_font = self.loaded_font(run.font_id);
401 let font = self.font_system.db().face(loaded_font.font.id()).unwrap();
402
403 attrs_list.add_span(
404 offs..(offs + run.len),
405 &Attrs::new()
406 .metadata(run.font_id.0)
407 .family(Family::Name(&font.families.first().unwrap().0))
408 .stretch(font.stretch)
409 .style(font.style)
410 .weight(font.weight)
411 .font_features(loaded_font.features.clone()),
412 );
413 offs += run.len;
414 }
415
416 let line = ShapeLine::new(
417 &mut self.font_system,
418 text,
419 &attrs_list,
420 cosmic_text::Shaping::Advanced,
421 4,
422 );
423 let mut layout_lines = Vec::with_capacity(1);
424 line.layout_to_buffer(
425 &mut self.scratch,
426 f32::from(font_size),
427 None, // We do our own wrapping
428 cosmic_text::Wrap::None,
429 None,
430 &mut layout_lines,
431 None,
432 cosmic_text::Hinting::Disabled,
433 );
434 let layout = layout_lines.first().unwrap();
435
436 let mut runs: Vec<ShapedRun> = Vec::new();
437 for glyph in &layout.glyphs {
438 let mut font_id = FontId(glyph.metadata);
439 let mut loaded_font = self.loaded_font(font_id);
440 if loaded_font.font.id() != glyph.font_id {
441 font_id = self.font_id_for_cosmic_id(glyph.font_id);
442 loaded_font = self.loaded_font(font_id);
443 }
444 let is_emoji = loaded_font.is_known_emoji_font;
445
446 // HACK: Prevent crash caused by variation selectors.
447 if glyph.glyph_id == 3 && is_emoji {
448 continue;
449 }
450
451 let shaped_glyph = ShapedGlyph {
452 id: GlyphId(glyph.glyph_id as u32),
453 position: point(glyph.x.into(), glyph.y.into()),
454 index: glyph.start,
455 is_emoji,
456 };
457
458 if let Some(last_run) = runs
459 .last_mut()
460 .filter(|last_run| last_run.font_id == font_id)
461 {
462 last_run.glyphs.push(shaped_glyph);
463 } else {
464 runs.push(ShapedRun {
465 font_id,
466 glyphs: vec![shaped_glyph],
467 });
468 }
469 }
470
471 LineLayout {
472 font_size,
473 width: layout.w.into(),
474 ascent: layout.max_ascent.into(),
475 descent: layout.max_descent.into(),
476 runs,
477 len: text.len(),
478 }
479 }
480}
481
482fn cosmic_font_features(features: &FontFeatures) -> Result<CosmicFontFeatures> {
483 let mut result = CosmicFontFeatures::new();
484 for feature in features.0.iter() {
485 let name_bytes: [u8; 4] = feature
486 .0
487 .as_bytes()
488 .try_into()
489 .context("Incorrect feature flag format")?;
490
491 let tag = cosmic_text::FeatureTag::new(&name_bytes);
492
493 result.set(tag, feature.1);
494 }
495 Ok(result)
496}
497
498fn font_into_properties(font: &gpui::Font) -> font_kit::properties::Properties {
499 font_kit::properties::Properties {
500 style: match font.style {
501 gpui::FontStyle::Normal => font_kit::properties::Style::Normal,
502 gpui::FontStyle::Italic => font_kit::properties::Style::Italic,
503 gpui::FontStyle::Oblique => font_kit::properties::Style::Oblique,
504 },
505 weight: font_kit::properties::Weight(font.weight.0),
506 stretch: Default::default(),
507 }
508}
509
510fn face_info_into_properties(
511 face_info: &cosmic_text::fontdb::FaceInfo,
512) -> font_kit::properties::Properties {
513 font_kit::properties::Properties {
514 style: match face_info.style {
515 cosmic_text::Style::Normal => font_kit::properties::Style::Normal,
516 cosmic_text::Style::Italic => font_kit::properties::Style::Italic,
517 cosmic_text::Style::Oblique => font_kit::properties::Style::Oblique,
518 },
519 // both libs use the same values for weight
520 weight: font_kit::properties::Weight(face_info.weight.0.into()),
521 stretch: match face_info.stretch {
522 cosmic_text::Stretch::Condensed => font_kit::properties::Stretch::CONDENSED,
523 cosmic_text::Stretch::Expanded => font_kit::properties::Stretch::EXPANDED,
524 cosmic_text::Stretch::ExtraCondensed => font_kit::properties::Stretch::EXTRA_CONDENSED,
525 cosmic_text::Stretch::ExtraExpanded => font_kit::properties::Stretch::EXTRA_EXPANDED,
526 cosmic_text::Stretch::Normal => font_kit::properties::Stretch::NORMAL,
527 cosmic_text::Stretch::SemiCondensed => font_kit::properties::Stretch::SEMI_CONDENSED,
528 cosmic_text::Stretch::SemiExpanded => font_kit::properties::Stretch::SEMI_EXPANDED,
529 cosmic_text::Stretch::UltraCondensed => font_kit::properties::Stretch::ULTRA_CONDENSED,
530 cosmic_text::Stretch::UltraExpanded => font_kit::properties::Stretch::ULTRA_EXPANDED,
531 },
532 }
533}
534
535fn check_is_known_emoji_font(postscript_name: &str) -> bool {
536 // TODO: Include other common emoji fonts
537 postscript_name == "NotoColorEmoji"
538}