1mod open_type;
2
3use crate::{
4 fonts::{Features, FontId, GlyphId, Metrics, Properties},
5 geometry::{
6 rect::{RectF, RectI},
7 transform2d::Transform2F,
8 vector::{vec2f, Vector2F},
9 },
10 platform::{self, RasterizationOptions},
11 text_layout::{Glyph, LineLayout, Run, RunStyle},
12};
13use cocoa::appkit::{CGFloat, CGPoint};
14use collections::HashMap;
15use core_foundation::{
16 array::CFIndex,
17 attributed_string::{CFAttributedStringRef, CFMutableAttributedString},
18 base::{CFRange, TCFType},
19 string::CFString,
20};
21use core_graphics::{
22 base::{kCGImageAlphaPremultipliedLast, CGGlyph},
23 color_space::CGColorSpace,
24 context::CGContext,
25};
26use core_text::{font::CTFont, line::CTLine, string_attributes::kCTFontAttributeName};
27use font_kit::{
28 handle::Handle, hinting::HintingOptions, source::SystemSource, sources::mem::MemSource,
29};
30use parking_lot::RwLock;
31use std::{cell::RefCell, char, cmp, convert::TryFrom, ffi::c_void, sync::Arc};
32
33#[allow(non_upper_case_globals)]
34const kCGImageAlphaOnly: u32 = 7;
35
36pub struct FontSystem(RwLock<FontSystemState>);
37
38struct FontSystemState {
39 memory_source: MemSource,
40 system_source: SystemSource,
41 fonts: Vec<font_kit::font::Font>,
42 font_ids_by_postscript_name: HashMap<String, FontId>,
43 postscript_names_by_font_id: HashMap<FontId, String>,
44}
45
46impl FontSystem {
47 pub fn new() -> Self {
48 Self(RwLock::new(FontSystemState {
49 memory_source: MemSource::empty(),
50 system_source: SystemSource::new(),
51 fonts: Vec::new(),
52 font_ids_by_postscript_name: Default::default(),
53 postscript_names_by_font_id: Default::default(),
54 }))
55 }
56}
57
58impl Default for FontSystem {
59 fn default() -> Self {
60 Self::new()
61 }
62}
63
64impl platform::FontSystem for FontSystem {
65 fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()> {
66 self.0.write().add_fonts(fonts)
67 }
68
69 fn all_families(&self) -> Vec<String> {
70 self.0
71 .read()
72 .system_source
73 .all_families()
74 .expect("core text should never return an error")
75 }
76
77 fn load_family(&self, name: &str, features: &Features) -> anyhow::Result<Vec<FontId>> {
78 self.0.write().load_family(name, features)
79 }
80
81 fn select_font(&self, font_ids: &[FontId], properties: &Properties) -> anyhow::Result<FontId> {
82 self.0.read().select_font(font_ids, properties)
83 }
84
85 fn font_metrics(&self, font_id: FontId) -> Metrics {
86 self.0.read().font_metrics(font_id)
87 }
88
89 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF> {
90 self.0.read().typographic_bounds(font_id, glyph_id)
91 }
92
93 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F> {
94 self.0.read().advance(font_id, glyph_id)
95 }
96
97 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
98 self.0.read().glyph_for_char(font_id, ch)
99 }
100
101 fn rasterize_glyph(
102 &self,
103 font_id: FontId,
104 font_size: f32,
105 glyph_id: GlyphId,
106 subpixel_shift: Vector2F,
107 scale_factor: f32,
108 options: RasterizationOptions,
109 ) -> Option<(RectI, Vec<u8>)> {
110 self.0.read().rasterize_glyph(
111 font_id,
112 font_size,
113 glyph_id,
114 subpixel_shift,
115 scale_factor,
116 options,
117 )
118 }
119
120 fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout {
121 self.0.write().layout_line(text, font_size, runs)
122 }
123
124 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize> {
125 self.0.read().wrap_line(text, font_id, font_size, width)
126 }
127}
128
129impl FontSystemState {
130 fn add_fonts(&mut self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()> {
131 self.memory_source.add_fonts(
132 fonts
133 .iter()
134 .map(|bytes| Handle::from_memory(bytes.clone(), 0)),
135 )?;
136 Ok(())
137 }
138
139 fn load_family(&mut self, name: &str, features: &Features) -> anyhow::Result<Vec<FontId>> {
140 let mut font_ids = Vec::new();
141
142 let family = self
143 .memory_source
144 .select_family_by_name(name)
145 .or_else(|_| self.system_source.select_family_by_name(name))?;
146 for font in family.fonts() {
147 let mut font = font.load()?;
148 open_type::apply_features(&mut font, features);
149 let font_id = FontId(self.fonts.len());
150 font_ids.push(font_id);
151 let postscript_name = font.postscript_name().unwrap();
152 self.font_ids_by_postscript_name
153 .insert(postscript_name.clone(), font_id);
154 self.postscript_names_by_font_id
155 .insert(font_id, postscript_name);
156 self.fonts.push(font);
157 }
158 Ok(font_ids)
159 }
160
161 fn select_font(&self, font_ids: &[FontId], properties: &Properties) -> anyhow::Result<FontId> {
162 let candidates = font_ids
163 .iter()
164 .map(|font_id| self.fonts[font_id.0].properties())
165 .collect::<Vec<_>>();
166 let idx = font_kit::matching::find_best_match(&candidates, properties)?;
167 Ok(font_ids[idx])
168 }
169
170 fn font_metrics(&self, font_id: FontId) -> Metrics {
171 self.fonts[font_id.0].metrics()
172 }
173
174 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF> {
175 Ok(self.fonts[font_id.0].typographic_bounds(glyph_id)?)
176 }
177
178 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F> {
179 Ok(self.fonts[font_id.0].advance(glyph_id)?)
180 }
181
182 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
183 self.fonts[font_id.0].glyph_for_char(ch)
184 }
185
186 fn id_for_native_font(&mut self, requested_font: CTFont) -> FontId {
187 let postscript_name = requested_font.postscript_name();
188 if let Some(font_id) = self.font_ids_by_postscript_name.get(&postscript_name) {
189 *font_id
190 } else {
191 let font_id = FontId(self.fonts.len());
192 self.font_ids_by_postscript_name
193 .insert(postscript_name.clone(), font_id);
194 self.postscript_names_by_font_id
195 .insert(font_id, postscript_name);
196 self.fonts
197 .push(font_kit::font::Font::from_core_graphics_font(
198 requested_font.copy_to_CGFont(),
199 ));
200 font_id
201 }
202 }
203
204 fn is_emoji(&self, font_id: FontId) -> bool {
205 self.postscript_names_by_font_id
206 .get(&font_id)
207 .map_or(false, |postscript_name| {
208 postscript_name == "AppleColorEmoji"
209 })
210 }
211
212 fn rasterize_glyph(
213 &self,
214 font_id: FontId,
215 font_size: f32,
216 glyph_id: GlyphId,
217 subpixel_shift: Vector2F,
218 scale_factor: f32,
219 options: RasterizationOptions,
220 ) -> Option<(RectI, Vec<u8>)> {
221 let font = &self.fonts[font_id.0];
222 let scale = Transform2F::from_scale(scale_factor);
223 let glyph_bounds = font
224 .raster_bounds(
225 glyph_id,
226 font_size,
227 scale,
228 HintingOptions::None,
229 font_kit::canvas::RasterizationOptions::GrayscaleAa,
230 )
231 .ok()?;
232
233 if glyph_bounds.width() == 0 || glyph_bounds.height() == 0 {
234 None
235 } else {
236 // Make room for subpixel variants.
237 let subpixel_padding = subpixel_shift.ceil().to_i32();
238 let cx_bounds = RectI::new(
239 glyph_bounds.origin(),
240 glyph_bounds.size() + subpixel_padding,
241 );
242
243 let mut bytes;
244 let cx;
245 match options {
246 RasterizationOptions::Alpha => {
247 bytes = vec![0; cx_bounds.width() as usize * cx_bounds.height() as usize];
248 cx = CGContext::create_bitmap_context(
249 Some(bytes.as_mut_ptr() as *mut _),
250 cx_bounds.width() as usize,
251 cx_bounds.height() as usize,
252 8,
253 cx_bounds.width() as usize,
254 &CGColorSpace::create_device_gray(),
255 kCGImageAlphaOnly,
256 );
257 }
258 RasterizationOptions::Bgra => {
259 bytes = vec![0; cx_bounds.width() as usize * 4 * cx_bounds.height() as usize];
260 cx = CGContext::create_bitmap_context(
261 Some(bytes.as_mut_ptr() as *mut _),
262 cx_bounds.width() as usize,
263 cx_bounds.height() as usize,
264 8,
265 cx_bounds.width() as usize * 4,
266 &CGColorSpace::create_device_rgb(),
267 kCGImageAlphaPremultipliedLast,
268 );
269 }
270 }
271
272 // Move the origin to bottom left and account for scaling, this
273 // makes drawing text consistent with the font-kit's raster_bounds.
274 cx.translate(
275 -glyph_bounds.origin_x() as CGFloat,
276 (glyph_bounds.origin_y() + glyph_bounds.height()) as CGFloat,
277 );
278 cx.scale(scale_factor as CGFloat, scale_factor as CGFloat);
279
280 cx.set_allows_font_subpixel_positioning(true);
281 cx.set_should_subpixel_position_fonts(true);
282 cx.set_allows_font_subpixel_quantization(false);
283 cx.set_should_subpixel_quantize_fonts(false);
284 font.native_font()
285 .clone_with_font_size(font_size as CGFloat)
286 .draw_glyphs(
287 &[glyph_id as CGGlyph],
288 &[CGPoint::new(
289 (subpixel_shift.x() / scale_factor) as CGFloat,
290 (subpixel_shift.y() / scale_factor) as CGFloat,
291 )],
292 cx,
293 );
294
295 if let RasterizationOptions::Bgra = options {
296 // Convert from RGBA with premultiplied alpha to BGRA with straight alpha.
297 for pixel in bytes.chunks_exact_mut(4) {
298 pixel.swap(0, 2);
299 let a = pixel[3] as f32 / 255.;
300 pixel[0] = (pixel[0] as f32 / a) as u8;
301 pixel[1] = (pixel[1] as f32 / a) as u8;
302 pixel[2] = (pixel[2] as f32 / a) as u8;
303 }
304 }
305
306 Some((cx_bounds, bytes))
307 }
308 }
309
310 fn layout_line(
311 &mut self,
312 text: &str,
313 font_size: f32,
314 runs: &[(usize, RunStyle)],
315 ) -> LineLayout {
316 // Construct the attributed string, converting UTF8 ranges to UTF16 ranges.
317 let mut string = CFMutableAttributedString::new();
318 {
319 string.replace_str(&CFString::new(text), CFRange::init(0, 0));
320 let utf16_line_len = string.char_len() as usize;
321
322 let last_run: RefCell<Option<(usize, FontId)>> = Default::default();
323 let font_runs = runs
324 .iter()
325 .filter_map(|(len, style)| {
326 let mut last_run = last_run.borrow_mut();
327 if let Some((last_len, last_font_id)) = last_run.as_mut() {
328 if style.font_id == *last_font_id {
329 *last_len += *len;
330 None
331 } else {
332 let result = (*last_len, *last_font_id);
333 *last_len = *len;
334 *last_font_id = style.font_id;
335 Some(result)
336 }
337 } else {
338 *last_run = Some((*len, style.font_id));
339 None
340 }
341 })
342 .chain(std::iter::from_fn(|| last_run.borrow_mut().take()));
343
344 let mut ix_converter = StringIndexConverter::new(text);
345 for (run_len, font_id) in font_runs {
346 let utf8_end = ix_converter.utf8_ix + run_len;
347 let utf16_start = ix_converter.utf16_ix;
348
349 if utf16_start >= utf16_line_len {
350 break;
351 }
352
353 ix_converter.advance_to_utf8_ix(utf8_end);
354 let utf16_end = cmp::min(ix_converter.utf16_ix, utf16_line_len);
355
356 let cf_range =
357 CFRange::init(utf16_start as isize, (utf16_end - utf16_start) as isize);
358 let font = &self.fonts[font_id.0];
359 unsafe {
360 string.set_attribute(
361 cf_range,
362 kCTFontAttributeName,
363 &font.native_font().clone_with_font_size(font_size as f64),
364 );
365 }
366
367 if utf16_end == utf16_line_len {
368 break;
369 }
370 }
371 }
372
373 // Retrieve the glyphs from the shaped line, converting UTF16 offsets to UTF8 offsets.
374 let line = CTLine::new_with_attributed_string(string.as_concrete_TypeRef());
375
376 let mut runs = Vec::new();
377 for run in line.glyph_runs().into_iter() {
378 let attributes = run.attributes().unwrap();
379 let font = unsafe {
380 attributes
381 .get(kCTFontAttributeName)
382 .downcast::<CTFont>()
383 .unwrap()
384 };
385 let font_id = self.id_for_native_font(font);
386
387 let mut ix_converter = StringIndexConverter::new(text);
388 let mut glyphs = Vec::new();
389 for ((glyph_id, position), glyph_utf16_ix) in run
390 .glyphs()
391 .iter()
392 .zip(run.positions().iter())
393 .zip(run.string_indices().iter())
394 {
395 let glyph_utf16_ix = usize::try_from(*glyph_utf16_ix).unwrap();
396 ix_converter.advance_to_utf16_ix(glyph_utf16_ix);
397 glyphs.push(Glyph {
398 id: *glyph_id as GlyphId,
399 position: vec2f(position.x as f32, position.y as f32),
400 index: ix_converter.utf8_ix,
401 is_emoji: self.is_emoji(font_id),
402 });
403 }
404
405 runs.push(Run { font_id, glyphs })
406 }
407
408 let typographic_bounds = line.get_typographic_bounds();
409 LineLayout {
410 width: typographic_bounds.width as f32,
411 ascent: typographic_bounds.ascent as f32,
412 descent: typographic_bounds.descent as f32,
413 runs,
414 font_size,
415 len: text.len(),
416 }
417 }
418
419 fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize> {
420 let mut string = CFMutableAttributedString::new();
421 string.replace_str(&CFString::new(text), CFRange::init(0, 0));
422 let cf_range = CFRange::init(0, text.encode_utf16().count() as isize);
423 let font = &self.fonts[font_id.0];
424 unsafe {
425 string.set_attribute(
426 cf_range,
427 kCTFontAttributeName,
428 &font.native_font().clone_with_font_size(font_size as f64),
429 );
430
431 let typesetter = CTTypesetterCreateWithAttributedString(string.as_concrete_TypeRef());
432 let mut ix_converter = StringIndexConverter::new(text);
433 let mut break_indices = Vec::new();
434 while ix_converter.utf8_ix < text.len() {
435 let utf16_len = CTTypesetterSuggestLineBreak(
436 typesetter,
437 ix_converter.utf16_ix as isize,
438 width as f64,
439 ) as usize;
440 ix_converter.advance_to_utf16_ix(ix_converter.utf16_ix + utf16_len);
441 if ix_converter.utf8_ix >= text.len() {
442 break;
443 }
444 break_indices.push(ix_converter.utf8_ix as usize);
445 }
446 break_indices
447 }
448 }
449}
450
451#[derive(Clone)]
452struct StringIndexConverter<'a> {
453 text: &'a str,
454 utf8_ix: usize,
455 utf16_ix: usize,
456}
457
458impl<'a> StringIndexConverter<'a> {
459 fn new(text: &'a str) -> Self {
460 Self {
461 text,
462 utf8_ix: 0,
463 utf16_ix: 0,
464 }
465 }
466
467 fn advance_to_utf8_ix(&mut self, utf8_target: usize) {
468 for (ix, c) in self.text[self.utf8_ix..].char_indices() {
469 if self.utf8_ix + ix >= utf8_target {
470 self.utf8_ix += ix;
471 return;
472 }
473 self.utf16_ix += c.len_utf16();
474 }
475 self.utf8_ix = self.text.len();
476 }
477
478 fn advance_to_utf16_ix(&mut self, utf16_target: usize) {
479 for (ix, c) in self.text[self.utf8_ix..].char_indices() {
480 if self.utf16_ix >= utf16_target {
481 self.utf8_ix += ix;
482 return;
483 }
484 self.utf16_ix += c.len_utf16();
485 }
486 self.utf8_ix = self.text.len();
487 }
488}
489
490#[repr(C)]
491pub struct __CFTypesetter(c_void);
492
493pub type CTTypesetterRef = *const __CFTypesetter;
494
495#[link(name = "CoreText", kind = "framework")]
496extern "C" {
497 fn CTTypesetterCreateWithAttributedString(string: CFAttributedStringRef) -> CTTypesetterRef;
498
499 fn CTTypesetterSuggestLineBreak(
500 typesetter: CTTypesetterRef,
501 start_index: CFIndex,
502 width: f64,
503 ) -> CFIndex;
504}
505
506#[cfg(test)]
507mod tests {
508 use super::*;
509 use crate::AppContext;
510 use font_kit::properties::{Style, Weight};
511 use platform::FontSystem as _;
512
513 #[crate::test(self, retries = 5)]
514 fn test_layout_str(_: &mut AppContext) {
515 // This is failing intermittently on CI and we don't have time to figure it out
516 let fonts = FontSystem::new();
517 let menlo = fonts.load_family("Menlo", &Default::default()).unwrap();
518 let menlo_regular = RunStyle {
519 font_id: fonts.select_font(&menlo, &Properties::new()).unwrap(),
520 color: Default::default(),
521 underline: Default::default(),
522 };
523 let menlo_italic = RunStyle {
524 font_id: fonts
525 .select_font(&menlo, Properties::new().style(Style::Italic))
526 .unwrap(),
527 color: Default::default(),
528 underline: Default::default(),
529 };
530 let menlo_bold = RunStyle {
531 font_id: fonts
532 .select_font(&menlo, Properties::new().weight(Weight::BOLD))
533 .unwrap(),
534 color: Default::default(),
535 underline: Default::default(),
536 };
537 assert_ne!(menlo_regular, menlo_italic);
538 assert_ne!(menlo_regular, menlo_bold);
539 assert_ne!(menlo_italic, menlo_bold);
540
541 let line = fonts.layout_line(
542 "hello world",
543 16.0,
544 &[(2, menlo_bold), (4, menlo_italic), (5, menlo_regular)],
545 );
546 assert_eq!(line.runs.len(), 3);
547 assert_eq!(line.runs[0].font_id, menlo_bold.font_id);
548 assert_eq!(line.runs[0].glyphs.len(), 2);
549 assert_eq!(line.runs[1].font_id, menlo_italic.font_id);
550 assert_eq!(line.runs[1].glyphs.len(), 4);
551 assert_eq!(line.runs[2].font_id, menlo_regular.font_id);
552 assert_eq!(line.runs[2].glyphs.len(), 5);
553 }
554
555 #[test]
556 fn test_glyph_offsets() -> anyhow::Result<()> {
557 let fonts = FontSystem::new();
558 let zapfino = fonts.load_family("Zapfino", &Default::default())?;
559 let zapfino_regular = RunStyle {
560 font_id: fonts.select_font(&zapfino, &Properties::new())?,
561 color: Default::default(),
562 underline: Default::default(),
563 };
564 let menlo = fonts.load_family("Menlo", &Default::default())?;
565 let menlo_regular = RunStyle {
566 font_id: fonts.select_font(&menlo, &Properties::new())?,
567 color: Default::default(),
568 underline: Default::default(),
569 };
570
571 let text = "This is, mπre πr less, Zapfino!π";
572 let line = fonts.layout_line(
573 text,
574 16.0,
575 &[
576 (9, zapfino_regular),
577 (13, menlo_regular),
578 (text.len() - 22, zapfino_regular),
579 ],
580 );
581 assert_eq!(
582 line.runs
583 .iter()
584 .flat_map(|r| r.glyphs.iter())
585 .map(|g| g.index)
586 .collect::<Vec<_>>(),
587 vec![0, 2, 4, 5, 7, 8, 9, 10, 14, 15, 16, 17, 21, 22, 23, 24, 26, 27, 28, 29, 36, 37],
588 );
589 Ok(())
590 }
591
592 #[test]
593 #[ignore]
594 fn test_rasterize_glyph() {
595 use std::{fs::File, io::BufWriter, path::Path};
596
597 let fonts = FontSystem::new();
598 let font_ids = fonts.load_family("Fira Code", &Default::default()).unwrap();
599 let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
600 let glyph_id = fonts.glyph_for_char(font_id, 'G').unwrap();
601
602 const VARIANTS: usize = 1;
603 for i in 0..VARIANTS {
604 let variant = i as f32 / VARIANTS as f32;
605 let (bounds, bytes) = fonts
606 .rasterize_glyph(
607 font_id,
608 16.0,
609 glyph_id,
610 vec2f(variant, variant),
611 2.,
612 RasterizationOptions::Alpha,
613 )
614 .unwrap();
615
616 let name = format!("/Users/as-cii/Desktop/twog-{}.png", i);
617 let path = Path::new(&name);
618 let file = File::create(path).unwrap();
619 let w = &mut BufWriter::new(file);
620
621 let mut encoder = png::Encoder::new(w, bounds.width() as u32, bounds.height() as u32);
622 encoder.set_color(png::ColorType::Grayscale);
623 encoder.set_depth(png::BitDepth::Eight);
624 let mut writer = encoder.write_header().unwrap();
625 writer.write_image_data(&bytes).unwrap();
626 }
627 }
628
629 #[test]
630 fn test_wrap_line() {
631 let fonts = FontSystem::new();
632 let font_ids = fonts.load_family("Helvetica", &Default::default()).unwrap();
633 let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
634
635 let line = "one two three four five\n";
636 let wrap_boundaries = fonts.wrap_line(line, font_id, 16., 64.0);
637 assert_eq!(wrap_boundaries, &["one two ".len(), "one two three ".len()]);
638
639 let line = "aaa Ξ±Ξ±Ξ± βββ πππ\n";
640 let wrap_boundaries = fonts.wrap_line(line, font_id, 16., 64.0);
641 assert_eq!(
642 wrap_boundaries,
643 &["aaa Ξ±Ξ±Ξ± ".len(), "aaa Ξ±Ξ±Ξ± βββ ".len(),]
644 );
645 }
646
647 #[test]
648 fn test_layout_line_bom_char() {
649 let fonts = FontSystem::new();
650 let font_ids = fonts.load_family("Helvetica", &Default::default()).unwrap();
651 let style = RunStyle {
652 font_id: fonts.select_font(&font_ids, &Default::default()).unwrap(),
653 color: Default::default(),
654 underline: Default::default(),
655 };
656
657 let line = "\u{feff}";
658 let layout = fonts.layout_line(line, 16., &[(line.len(), style)]);
659 assert_eq!(layout.len, line.len());
660 assert!(layout.runs.is_empty());
661
662 let line = "a\u{feff}b";
663 let layout = fonts.layout_line(line, 16., &[(line.len(), style)]);
664 assert_eq!(layout.len, line.len());
665 assert_eq!(layout.runs.len(), 1);
666 assert_eq!(layout.runs[0].glyphs.len(), 2);
667 assert_eq!(layout.runs[0].glyphs[0].id, 68); // a
668 // There's no glyph for \u{feff}
669 assert_eq!(layout.runs[0].glyphs[1].id, 69); // b
670 }
671}