1use crate::{
2 point, px, size, Bounds, DevicePixels, Font, FontFeatures, FontId, FontMetrics, FontRun,
3 FontStyle, FontWeight, GlyphId, LineLayout, Pixels, PlatformTextSystem, Point,
4 RenderGlyphParams, Result, ShapedGlyph, ShapedRun, SharedString, Size, SUBPIXEL_VARIANTS,
5};
6use anyhow::anyhow;
7use cocoa::appkit::{CGFloat, CGPoint};
8use collections::{BTreeSet, HashMap};
9use core_foundation::{
10 attributed_string::CFMutableAttributedString,
11 base::{CFRange, TCFType},
12 number::CFNumber,
13 string::CFString,
14};
15use core_graphics::{
16 base::{kCGImageAlphaPremultipliedLast, CGGlyph},
17 color_space::CGColorSpace,
18 context::CGContext,
19};
20use core_text::{
21 font::CTFont,
22 font_descriptor::{
23 kCTFontSlantTrait, kCTFontSymbolicTrait, kCTFontWeightTrait, kCTFontWidthTrait,
24 },
25 line::CTLine,
26 string_attributes::kCTFontAttributeName,
27};
28use font_kit::{
29 font::Font as FontKitFont,
30 handle::Handle,
31 hinting::HintingOptions,
32 metrics::Metrics,
33 properties::{Style as FontkitStyle, Weight as FontkitWeight},
34 source::SystemSource,
35 sources::mem::MemSource,
36};
37use parking_lot::{RwLock, RwLockUpgradableReadGuard};
38use pathfinder_geometry::{
39 rect::{RectF, RectI},
40 transform2d::Transform2F,
41 vector::{Vector2F, Vector2I},
42};
43use smallvec::SmallVec;
44use std::{borrow::Cow, char, cmp, convert::TryFrom, sync::Arc};
45
46use super::open_type;
47
48#[allow(non_upper_case_globals)]
49const kCGImageAlphaOnly: u32 = 7;
50
51pub(crate) struct MacTextSystem(RwLock<MacTextSystemState>);
52
53#[derive(Clone, PartialEq, Eq, Hash)]
54struct FontKey {
55 font_family: SharedString,
56 font_features: FontFeatures,
57}
58
59struct MacTextSystemState {
60 memory_source: MemSource,
61 system_source: SystemSource,
62 fonts: Vec<FontKitFont>,
63 font_selections: HashMap<Font, FontId>,
64 font_ids_by_postscript_name: HashMap<String, FontId>,
65 font_ids_by_font_key: HashMap<FontKey, SmallVec<[FontId; 4]>>,
66 postscript_names_by_font_id: HashMap<FontId, String>,
67}
68
69impl MacTextSystem {
70 pub(crate) fn new() -> Self {
71 Self(RwLock::new(MacTextSystemState {
72 memory_source: MemSource::empty(),
73 system_source: SystemSource::new(),
74 fonts: Vec::new(),
75 font_selections: HashMap::default(),
76 font_ids_by_postscript_name: HashMap::default(),
77 font_ids_by_font_key: HashMap::default(),
78 postscript_names_by_font_id: HashMap::default(),
79 }))
80 }
81}
82
83impl Default for MacTextSystem {
84 fn default() -> Self {
85 Self::new()
86 }
87}
88
89impl PlatformTextSystem for MacTextSystem {
90 fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
91 self.0.write().add_fonts(fonts)
92 }
93
94 fn all_font_names(&self) -> Vec<String> {
95 let collection = core_text::font_collection::create_for_all_families();
96 let Some(descriptors) = collection.get_descriptors() else {
97 return Vec::new();
98 };
99 let mut names = BTreeSet::new();
100 for descriptor in descriptors.into_iter() {
101 names.extend(lenient_font_attributes::family_name(&descriptor));
102 }
103 if let Ok(fonts_in_memory) = self.0.read().memory_source.all_families() {
104 names.extend(fonts_in_memory);
105 }
106 names.into_iter().collect()
107 }
108
109 fn all_font_families(&self) -> Vec<String> {
110 self.0
111 .read()
112 .system_source
113 .all_families()
114 .expect("core text should never return an error")
115 }
116
117 fn font_id(&self, font: &Font) -> Result<FontId> {
118 let lock = self.0.upgradable_read();
119 if let Some(font_id) = lock.font_selections.get(font) {
120 Ok(*font_id)
121 } else {
122 let mut lock = RwLockUpgradableReadGuard::upgrade(lock);
123 let font_key = FontKey {
124 font_family: font.family.clone(),
125 font_features: font.features.clone(),
126 };
127 let candidates = if let Some(font_ids) = lock.font_ids_by_font_key.get(&font_key) {
128 font_ids.as_slice()
129 } else {
130 let font_ids = lock.load_family(&font.family, &font.features)?;
131 lock.font_ids_by_font_key.insert(font_key.clone(), font_ids);
132 lock.font_ids_by_font_key[&font_key].as_ref()
133 };
134
135 let candidate_properties = candidates
136 .iter()
137 .map(|font_id| lock.fonts[font_id.0].properties())
138 .collect::<SmallVec<[_; 4]>>();
139
140 let ix = font_kit::matching::find_best_match(
141 &candidate_properties,
142 &font_kit::properties::Properties {
143 style: font.style.into(),
144 weight: font.weight.into(),
145 stretch: Default::default(),
146 },
147 )?;
148
149 let font_id = candidates[ix];
150 lock.font_selections.insert(font.clone(), font_id);
151 Ok(font_id)
152 }
153 }
154
155 fn font_metrics(&self, font_id: FontId) -> FontMetrics {
156 self.0.read().fonts[font_id.0].metrics().into()
157 }
158
159 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>> {
160 Ok(self.0.read().fonts[font_id.0]
161 .typographic_bounds(glyph_id.0)?
162 .into())
163 }
164
165 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
166 self.0.read().advance(font_id, glyph_id)
167 }
168
169 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
170 self.0.read().glyph_for_char(font_id, ch)
171 }
172
173 fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
174 self.0.read().raster_bounds(params)
175 }
176
177 fn rasterize_glyph(
178 &self,
179 glyph_id: &RenderGlyphParams,
180 raster_bounds: Bounds<DevicePixels>,
181 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
182 self.0.read().rasterize_glyph(glyph_id, raster_bounds)
183 }
184
185 fn layout_line(&self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
186 self.0.write().layout_line(text, font_size, font_runs)
187 }
188}
189
190impl MacTextSystemState {
191 fn add_fonts(&mut self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
192 let fonts = fonts
193 .into_iter()
194 .map(|bytes| match bytes {
195 Cow::Borrowed(embedded_font) => {
196 let data_provider = unsafe {
197 core_graphics::data_provider::CGDataProvider::from_slice(embedded_font)
198 };
199 let font = core_graphics::font::CGFont::from_data_provider(data_provider)
200 .map_err(|_| anyhow!("Could not load an embedded font."))?;
201 let font = font_kit::loaders::core_text::Font::from_core_graphics_font(font);
202 Ok(Handle::from_native(&font))
203 }
204 Cow::Owned(bytes) => Ok(Handle::from_memory(Arc::new(bytes), 0)),
205 })
206 .collect::<Result<Vec<_>>>()?;
207 self.memory_source.add_fonts(fonts.into_iter())?;
208 Ok(())
209 }
210
211 fn load_family(
212 &mut self,
213 name: &str,
214 features: &FontFeatures,
215 ) -> Result<SmallVec<[FontId; 4]>> {
216 let name = if name == ".SystemUIFont" {
217 ".AppleSystemUIFont"
218 } else {
219 name
220 };
221
222 let mut font_ids = SmallVec::new();
223 let family = self
224 .memory_source
225 .select_family_by_name(name)
226 .or_else(|_| self.system_source.select_family_by_name(name))?;
227 for font in family.fonts() {
228 let mut font = font.load()?;
229
230 open_type::apply_features(&mut font, features);
231
232 // This block contains a precautionary fix to guard against loading fonts
233 // that might cause panics due to `.unwrap()`s up the chain.
234 {
235 // We use the 'm' character for text measurements in various spots
236 // (e.g., the editor). However, at time of writing some of those usages
237 // will panic if the font has no 'm' glyph.
238 //
239 // Therefore, we check up front that the font has the necessary glyph.
240 let has_m_glyph = font.glyph_for_char('m').is_some();
241
242 // HACK: The 'Segoe Fluent Icons' font does not have an 'm' glyph,
243 // but we need to be able to load it for rendering Windows icons in
244 // the Storybook (on macOS).
245 let is_segoe_fluent_icons = font.full_name() == "Segoe Fluent Icons";
246
247 if !has_m_glyph && !is_segoe_fluent_icons {
248 // I spent far too long trying to track down why a font missing the 'm'
249 // character wasn't loading. This log statement will hopefully save
250 // someone else from suffering the same fate.
251 log::warn!(
252 "font '{}' has no 'm' character and was not loaded",
253 font.full_name()
254 );
255 continue;
256 }
257 }
258
259 // We've seen a number of panics in production caused by calling font.properties()
260 // which unwraps a downcast to CFNumber. This is an attempt to avoid the panic,
261 // and to try and identify the incalcitrant font.
262 let traits = font.native_font().all_traits();
263 if unsafe {
264 !(traits
265 .get(kCTFontSymbolicTrait)
266 .downcast::<CFNumber>()
267 .is_some()
268 && traits
269 .get(kCTFontWidthTrait)
270 .downcast::<CFNumber>()
271 .is_some()
272 && traits
273 .get(kCTFontWeightTrait)
274 .downcast::<CFNumber>()
275 .is_some()
276 && traits
277 .get(kCTFontSlantTrait)
278 .downcast::<CFNumber>()
279 .is_some())
280 } {
281 log::error!(
282 "Failed to read traits for font {:?}",
283 font.postscript_name().unwrap()
284 );
285 continue;
286 }
287
288 let font_id = FontId(self.fonts.len());
289 font_ids.push(font_id);
290 let postscript_name = font.postscript_name().unwrap();
291 self.font_ids_by_postscript_name
292 .insert(postscript_name.clone(), font_id);
293 self.postscript_names_by_font_id
294 .insert(font_id, postscript_name);
295 self.fonts.push(font);
296 }
297 Ok(font_ids)
298 }
299
300 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
301 Ok(self.fonts[font_id.0].advance(glyph_id.0)?.into())
302 }
303
304 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId> {
305 self.fonts[font_id.0].glyph_for_char(ch).map(GlyphId)
306 }
307
308 fn id_for_native_font(&mut self, requested_font: CTFont) -> FontId {
309 let postscript_name = requested_font.postscript_name();
310 if let Some(font_id) = self.font_ids_by_postscript_name.get(&postscript_name) {
311 *font_id
312 } else {
313 let font_id = FontId(self.fonts.len());
314 self.font_ids_by_postscript_name
315 .insert(postscript_name.clone(), font_id);
316 self.postscript_names_by_font_id
317 .insert(font_id, postscript_name);
318 self.fonts
319 .push(font_kit::font::Font::from_core_graphics_font(
320 requested_font.copy_to_CGFont(),
321 ));
322 font_id
323 }
324 }
325
326 fn is_emoji(&self, font_id: FontId) -> bool {
327 self.postscript_names_by_font_id
328 .get(&font_id)
329 .map_or(false, |postscript_name| {
330 postscript_name == "AppleColorEmoji" || postscript_name == ".AppleColorEmojiUI"
331 })
332 }
333
334 fn raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
335 let font = &self.fonts[params.font_id.0];
336 let scale = Transform2F::from_scale(params.scale_factor);
337 Ok(font
338 .raster_bounds(
339 params.glyph_id.0,
340 params.font_size.into(),
341 scale,
342 HintingOptions::None,
343 font_kit::canvas::RasterizationOptions::GrayscaleAa,
344 )?
345 .into())
346 }
347
348 fn rasterize_glyph(
349 &self,
350 params: &RenderGlyphParams,
351 glyph_bounds: Bounds<DevicePixels>,
352 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
353 if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
354 Err(anyhow!("glyph bounds are empty"))
355 } else {
356 // Add an extra pixel when the subpixel variant isn't zero to make room for anti-aliasing.
357 let mut bitmap_size = glyph_bounds.size;
358 if params.subpixel_variant.x > 0 {
359 bitmap_size.width += DevicePixels(1);
360 }
361 if params.subpixel_variant.y > 0 {
362 bitmap_size.height += DevicePixels(1);
363 }
364 let bitmap_size = bitmap_size;
365
366 let mut bytes;
367 let cx;
368 if params.is_emoji {
369 bytes = vec![0; bitmap_size.width.0 as usize * 4 * bitmap_size.height.0 as usize];
370 cx = CGContext::create_bitmap_context(
371 Some(bytes.as_mut_ptr() as *mut _),
372 bitmap_size.width.0 as usize,
373 bitmap_size.height.0 as usize,
374 8,
375 bitmap_size.width.0 as usize * 4,
376 &CGColorSpace::create_device_rgb(),
377 kCGImageAlphaPremultipliedLast,
378 );
379 } else {
380 bytes = vec![0; bitmap_size.width.0 as usize * bitmap_size.height.0 as usize];
381 cx = CGContext::create_bitmap_context(
382 Some(bytes.as_mut_ptr() as *mut _),
383 bitmap_size.width.0 as usize,
384 bitmap_size.height.0 as usize,
385 8,
386 bitmap_size.width.0 as usize,
387 &CGColorSpace::create_device_gray(),
388 kCGImageAlphaOnly,
389 );
390 }
391
392 // Move the origin to bottom left and account for scaling, this
393 // makes drawing text consistent with the font-kit's raster_bounds.
394 cx.translate(
395 -glyph_bounds.origin.x.0 as CGFloat,
396 (glyph_bounds.origin.y.0 + glyph_bounds.size.height.0) as CGFloat,
397 );
398 cx.scale(
399 params.scale_factor as CGFloat,
400 params.scale_factor as CGFloat,
401 );
402
403 let subpixel_shift = params
404 .subpixel_variant
405 .map(|v| v as f32 / SUBPIXEL_VARIANTS as f32);
406 cx.set_allows_font_subpixel_positioning(true);
407 cx.set_should_subpixel_position_fonts(true);
408 cx.set_allows_font_subpixel_quantization(false);
409 cx.set_should_subpixel_quantize_fonts(false);
410 self.fonts[params.font_id.0]
411 .native_font()
412 .clone_with_font_size(f32::from(params.font_size) as CGFloat)
413 .draw_glyphs(
414 &[params.glyph_id.0 as CGGlyph],
415 &[CGPoint::new(
416 (subpixel_shift.x / params.scale_factor) as CGFloat,
417 (subpixel_shift.y / params.scale_factor) as CGFloat,
418 )],
419 cx,
420 );
421
422 if params.is_emoji {
423 // Convert from RGBA with premultiplied alpha to BGRA with straight alpha.
424 for pixel in bytes.chunks_exact_mut(4) {
425 pixel.swap(0, 2);
426 let a = pixel[3] as f32 / 255.;
427 pixel[0] = (pixel[0] as f32 / a) as u8;
428 pixel[1] = (pixel[1] as f32 / a) as u8;
429 pixel[2] = (pixel[2] as f32 / a) as u8;
430 }
431 }
432
433 Ok((bitmap_size, bytes))
434 }
435 }
436
437 fn layout_line(&mut self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
438 // Construct the attributed string, converting UTF8 ranges to UTF16 ranges.
439 let mut string = CFMutableAttributedString::new();
440 {
441 string.replace_str(&CFString::new(text), CFRange::init(0, 0));
442 let utf16_line_len = string.char_len() as usize;
443
444 let mut ix_converter = StringIndexConverter::new(text);
445 for run in font_runs {
446 let utf8_end = ix_converter.utf8_ix + run.len;
447 let utf16_start = ix_converter.utf16_ix;
448
449 if utf16_start >= utf16_line_len {
450 break;
451 }
452
453 ix_converter.advance_to_utf8_ix(utf8_end);
454 let utf16_end = cmp::min(ix_converter.utf16_ix, utf16_line_len);
455
456 let cf_range =
457 CFRange::init(utf16_start as isize, (utf16_end - utf16_start) as isize);
458
459 let font: &FontKitFont = &self.fonts[run.font_id.0];
460 unsafe {
461 string.set_attribute(
462 cf_range,
463 kCTFontAttributeName,
464 &font.native_font().clone_with_font_size(font_size.into()),
465 );
466 }
467
468 if utf16_end == utf16_line_len {
469 break;
470 }
471 }
472 }
473
474 // Retrieve the glyphs from the shaped line, converting UTF16 offsets to UTF8 offsets.
475 let line = CTLine::new_with_attributed_string(string.as_concrete_TypeRef());
476
477 let mut runs = Vec::new();
478 for run in line.glyph_runs().into_iter() {
479 let attributes = run.attributes().unwrap();
480 let font = unsafe {
481 attributes
482 .get(kCTFontAttributeName)
483 .downcast::<CTFont>()
484 .unwrap()
485 };
486 let font_id = self.id_for_native_font(font);
487
488 let mut ix_converter = StringIndexConverter::new(text);
489 let mut glyphs = SmallVec::new();
490 for ((glyph_id, position), glyph_utf16_ix) in run
491 .glyphs()
492 .iter()
493 .zip(run.positions().iter())
494 .zip(run.string_indices().iter())
495 {
496 let glyph_utf16_ix = usize::try_from(*glyph_utf16_ix).unwrap();
497 ix_converter.advance_to_utf16_ix(glyph_utf16_ix);
498 glyphs.push(ShapedGlyph {
499 id: GlyphId(*glyph_id as u32),
500 position: point(position.x as f32, position.y as f32).map(px),
501 index: ix_converter.utf8_ix,
502 is_emoji: self.is_emoji(font_id),
503 });
504 }
505
506 runs.push(ShapedRun { font_id, glyphs })
507 }
508
509 let typographic_bounds = line.get_typographic_bounds();
510 LineLayout {
511 runs,
512 font_size,
513 width: typographic_bounds.width.into(),
514 ascent: typographic_bounds.ascent.into(),
515 descent: typographic_bounds.descent.into(),
516 len: text.len(),
517 }
518 }
519}
520
521#[derive(Clone)]
522struct StringIndexConverter<'a> {
523 text: &'a str,
524 utf8_ix: usize,
525 utf16_ix: usize,
526}
527
528impl<'a> StringIndexConverter<'a> {
529 fn new(text: &'a str) -> Self {
530 Self {
531 text,
532 utf8_ix: 0,
533 utf16_ix: 0,
534 }
535 }
536
537 fn advance_to_utf8_ix(&mut self, utf8_target: usize) {
538 for (ix, c) in self.text[self.utf8_ix..].char_indices() {
539 if self.utf8_ix + ix >= utf8_target {
540 self.utf8_ix += ix;
541 return;
542 }
543 self.utf16_ix += c.len_utf16();
544 }
545 self.utf8_ix = self.text.len();
546 }
547
548 fn advance_to_utf16_ix(&mut self, utf16_target: usize) {
549 for (ix, c) in self.text[self.utf8_ix..].char_indices() {
550 if self.utf16_ix >= utf16_target {
551 self.utf8_ix += ix;
552 return;
553 }
554 self.utf16_ix += c.len_utf16();
555 }
556 self.utf8_ix = self.text.len();
557 }
558}
559
560impl From<Metrics> for FontMetrics {
561 fn from(metrics: Metrics) -> Self {
562 FontMetrics {
563 units_per_em: metrics.units_per_em,
564 ascent: metrics.ascent,
565 descent: metrics.descent,
566 line_gap: metrics.line_gap,
567 underline_position: metrics.underline_position,
568 underline_thickness: metrics.underline_thickness,
569 cap_height: metrics.cap_height,
570 x_height: metrics.x_height,
571 bounding_box: metrics.bounding_box.into(),
572 }
573 }
574}
575
576impl From<RectF> for Bounds<f32> {
577 fn from(rect: RectF) -> Self {
578 Bounds {
579 origin: point(rect.origin_x(), rect.origin_y()),
580 size: size(rect.width(), rect.height()),
581 }
582 }
583}
584
585impl From<RectI> for Bounds<DevicePixels> {
586 fn from(rect: RectI) -> Self {
587 Bounds {
588 origin: point(DevicePixels(rect.origin_x()), DevicePixels(rect.origin_y())),
589 size: size(DevicePixels(rect.width()), DevicePixels(rect.height())),
590 }
591 }
592}
593
594impl From<Vector2I> for Size<DevicePixels> {
595 fn from(value: Vector2I) -> Self {
596 size(value.x().into(), value.y().into())
597 }
598}
599
600impl From<RectI> for Bounds<i32> {
601 fn from(rect: RectI) -> Self {
602 Bounds {
603 origin: point(rect.origin_x(), rect.origin_y()),
604 size: size(rect.width(), rect.height()),
605 }
606 }
607}
608
609impl From<Point<u32>> for Vector2I {
610 fn from(size: Point<u32>) -> Self {
611 Vector2I::new(size.x as i32, size.y as i32)
612 }
613}
614
615impl From<Vector2F> for Size<f32> {
616 fn from(vec: Vector2F) -> Self {
617 size(vec.x(), vec.y())
618 }
619}
620
621impl From<FontWeight> for FontkitWeight {
622 fn from(value: FontWeight) -> Self {
623 FontkitWeight(value.0)
624 }
625}
626
627impl From<FontStyle> for FontkitStyle {
628 fn from(style: FontStyle) -> Self {
629 match style {
630 FontStyle::Normal => FontkitStyle::Normal,
631 FontStyle::Italic => FontkitStyle::Italic,
632 FontStyle::Oblique => FontkitStyle::Oblique,
633 }
634 }
635}
636
637// Some fonts may have no attributest despite `core_text` requiring them (and panicking).
638// This is the same version as `core_text` has without `expect` calls.
639mod lenient_font_attributes {
640 use core_foundation::{
641 base::{CFRetain, CFType, TCFType},
642 string::{CFString, CFStringRef},
643 };
644 use core_text::font_descriptor::{
645 kCTFontFamilyNameAttribute, CTFontDescriptor, CTFontDescriptorCopyAttribute,
646 };
647
648 pub fn family_name(descriptor: &CTFontDescriptor) -> Option<String> {
649 unsafe { get_string_attribute(descriptor, kCTFontFamilyNameAttribute) }
650 }
651
652 fn get_string_attribute(
653 descriptor: &CTFontDescriptor,
654 attribute: CFStringRef,
655 ) -> Option<String> {
656 unsafe {
657 let value = CTFontDescriptorCopyAttribute(descriptor.as_concrete_TypeRef(), attribute);
658 if value.is_null() {
659 return None;
660 }
661
662 let value = CFType::wrap_under_create_rule(value);
663 assert!(value.instance_of::<CFString>());
664 let s = wrap_under_get_rule(value.as_CFTypeRef() as CFStringRef);
665 Some(s.to_string())
666 }
667 }
668
669 unsafe fn wrap_under_get_rule(reference: CFStringRef) -> CFString {
670 assert!(!reference.is_null(), "Attempted to create a NULL object.");
671 let reference = CFRetain(reference as *const ::std::os::raw::c_void) as CFStringRef;
672 TCFType::wrap_under_create_rule(reference)
673 }
674}
675
676#[cfg(test)]
677mod tests {
678 use crate::{font, px, FontRun, GlyphId, MacTextSystem, PlatformTextSystem};
679
680 #[test]
681 fn test_layout_line_bom_char() {
682 let fonts = MacTextSystem::new();
683 let font_id = fonts.font_id(&font("Helvetica")).unwrap();
684 let line = "\u{feff}";
685 let mut style = FontRun {
686 font_id,
687 len: line.len(),
688 };
689
690 let layout = fonts.layout_line(line, px(16.), &[style]);
691 assert_eq!(layout.len, line.len());
692 assert!(layout.runs.is_empty());
693
694 let line = "a\u{feff}b";
695 style.len = line.len();
696 let layout = fonts.layout_line(line, px(16.), &[style]);
697 assert_eq!(layout.len, line.len());
698 assert_eq!(layout.runs.len(), 1);
699 assert_eq!(layout.runs[0].glyphs.len(), 2);
700 assert_eq!(layout.runs[0].glyphs[0].id, GlyphId(68u32)); // a
701 // There's no glyph for \u{feff}
702 assert_eq!(layout.runs[0].glyphs[1].id, GlyphId(69u32)); // b
703 }
704}