# platform/desktop/coretextglyph (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/platform/desktop/coretextglyph/

Mac CoreText glyph drawing. (macOS only)
- A file not in the original. The original Chrome on Mac draws glyphs with CoreText (CoreGraphics). On the Mac the Rust version also draws glyphs here with CoreText. (called by textshaping.rs)
  Only this file can draw color emoji and fonts the open parsers cannot read.
  - Color emoji (sbix, Apple Color Emoji): at small text sizes CoreText draws emoji enlarged and lowered. (the 20px version at 16px, the text size as is from 24px)
    The emoji advance measured with measureText of Mac Chrome Canvas2D equals the height of the emoji box CoreText draws. (8px 10, 12px 15, 16px 20, 20px 22, 24px 24, 32px 32, 64px 64)
    Chrome's advance is that box height rounded to an integer. (16.25 at 13px is 16, 12.5 at 10px is 13. Matches Chrome at 23 sizes from 8 ~ 128px, textshaping.rs does the rounding)
    CoreText's advance (CTFontGetAdvancesForGlyphs) adds the font's tracking table (trak) and differs from Chrome, so it is not used.
  - Fonts with glyph outlines in none of glyf, CFF, CFF2: PingFangUI.ttc on Mac 26 (the Chinese fallback font) has its outlines in Apple's private hvgl table.
- The font is built from that face's PostScript name. (CoreText draws emoji enlarged only for fonts registered in the system) For hidden fonts whose name starts with a dot, or when axis values are set,
  it builds from the CTFontManagerCreateFontDescriptorsFromURL descriptors of the file path, chosen by PostScript name (or by order within the collection if none). CTFonts built per text size are kept. (one thread, cleared when full)
- Color glyph pictures are drawn in white into a premultiplied RGBA (sRGB) bitmap context. Grayscale antialiasing like the Chrome canvas, with LCD font smoothing off.
- The coverage of non-color glyphs (rasterize_glyph_mask) is drawn the same way as Mac Chrome Canvas2D. Blink FontPlatformData::CreateSkFont gives canvas text
  kSubpixelAntiAlias and subpixel positioning, and since a transparent canvas does not know the pixel layout (kUnknown_SkPixelGeometry), Skia derives A8 from the LCD drawing instead of LCD.
  Skia SkScalerContext_Mac then draws black text on a white RGB bitmap (DeviceRGB, no alpha) with font smoothing (CGContextSetShouldSmoothFonts) on,
  undoes the CoreGraphics gamma 2 with a square table (gLinearCoverageFromCGLCDValue), and corrects the luminance of the inverted RGB (SkComputeLuminance) with the gamma pre-correction table.
  The glyph box is CTFontGetBoundingRectsForGlyphs plus the subpixel part, rounded outward with 1 pixel added on every side. (SkScalerContext_Mac::generateMetrics)
  Synthetic italic is drawn by CoreText with the text matrix skew (-1/4) like Skia.

Example: Measure the enlargement ratio of the color emoji font

```rust
use vanilla::platform::desktop::coretextglyph::read_emoji_scale;
use vanilla::platform::find_fallback_font_file;

if let Some(source) = find_fallback_font_file("\u{1F600}", 400, false, true) {
	let emoji_scale = read_emoji_scale(&source.path, source.face_index.unwrap_or(0), 16.0);
	assert!(emoji_scale > 0.0);
}
```

## CoreTextGlyphRaster (struct)

Glyph drawn with CoreText. (premultiplied RGBA, the top-left position is left_x to the right of the pen origin and top_y up from the baseline)

Example: Draw the A glyph of the system fallback font

```rust
use std::rc::Rc;
use vanilla::core::textshaping::LoadedFont;
use vanilla::platform::desktop::coretextglyph::{rasterize_glyph, CoreTextGlyphRaster};
use vanilla::platform::find_fallback_font_file;

fn rasterize_character(character: char, pixel_size: f32) -> Option<CoreTextGlyphRaster> {
	let source = find_fallback_font_file(&character.to_string(), 400, false, false)?;
	let face_index = source.face_index.unwrap_or(0);
	let font_bytes: Rc<[u8]> = std::fs::read(&source.path).ok()?.into();
	let loaded_font = LoadedFont::create_from_shared_bytes(font_bytes, face_index, &[]).ok()?;
	let glyph_id = loaded_font.get_shaping_face().glyph_index(character)?;
	rasterize_glyph(&source.path, face_index, glyph_id.0, pixel_size, &[])
}

if let Some(raster) = rasterize_character('A', 32.0) {
	assert_eq!(raster.pixels.len(), (raster.width * raster.height * 4) as usize);
	assert!(raster.top_y > 0);
}
```

- `pub pixels: Vec<u8>` (field, since 0.4.0): Pixels. (premultiplied RGBA, width * 4 bytes per row, top row first)
  - returns Vec<u8>
- `pub width: i32` (field, since 0.4.0): Width. (pixels, the glyph box plus 1 pixel on every side)
  - returns i32
- `pub height: i32` (field, since 0.4.0): Height. (pixels, the glyph box plus 1 pixel on every side)
  - returns i32
- `pub left_x: i32` (field, since 0.4.0): From the pen origin to the left of the image. (pixels, right is positive)
  - returns i32
- `pub top_y: i32` (field, since 0.4.0): From the baseline to the top of the image. (pixels, up is positive)
  - returns i32

## Functions and constants

- `pub fn read_glyph_bounds(path: &Path, face_index: u32, glyph_index: u16, pixel_size: f32, variation_list: &[(u32, f32)]) -> Option<CGRect>` (method, since 0.4.0): Box the glyph is drawn in. (CTFontGetBoundingRectsForGlyphs, pixels relative to the pen origin, y is positive upward. None if there is nothing to draw)
  - `path` &Path: Font file path.
  - `face_index` u32: Face index within the collection (TTC).
  - `glyph_index` u16: Glyph ID.
  - `pixel_size` f32: Font size. (pixels)
  - `variation_list` &[(u32, f32)]: Variable font axis values. (list of (4-letter tag number, value), no axes are set if empty)
  - returns Option<CGRect>: Glyph box. None if CoreText could not build the font or the box's width or height is 0 or less.
- `pub fn read_glyph_skia_bounds(path: &Path, face_index: u32, glyph_index: u16, pixel_size: f32, variation_list: &[(u32, f32)]) -> Option<CGRect>` (method, since 0.4.0): Glyph bounds of the CTFont Skia builds. (the glyph-without-path bounds calculation of Mac Chrome measureText; no rounding)
  - Skia SkScalerContext_Mac rebuilds the font with SkCTFontCreateExactCopy. The optical size attribute (NSCTFontOpticalSizeAttribute) is the base font's value or
    the text size, and the tracking attribute (NSCTFontUnscaledTrackingAttribute) is set to 0 to turn off trak and other tracking. These are CTFontGetBoundingRectsForGlyphs of that font.
  - In this font, color emoji lose the position shift from scaling up. (at 16px, base font 0.46 to 20.46, Skia font 0 to 20)
  - `path` &Path: Font file path.
  - `face_index` u32: Face index within the collection (TTC).
  - `glyph_index` u16: Glyph ID.
  - `pixel_size` f32: Font size. (pixels)
  - `variation_list` &[(u32, f32)]: List of axis values. ((the number of the 4-letter tag, value))
  - returns Option<CGRect>: Glyph bounds. (pixels from the pen origin, up is positive) None when CoreText could not build the font or the bounds width or height is 0 or less.
- `pub fn read_emoji_scale(path: &Path, face_index: u32, pixel_size: f32) -> f32` (method, since 0.4.0): Ratio by which CoreText enlarges color emoji fonts. (smiling face emoji box height / text size, 1 if it cannot be measured)
  - `path` &Path: Font file path.
  - `face_index` u32: Face index within the collection (TTC).
  - `pixel_size` f32: Font size. (pixels)
  - returns f32: Ratio of the emoji box height divided by the text size. (1.25 at 16px, 1 from 24px) 1 if the text size is 0 or less, the font has no smiling face, or it cannot be measured.
- `pub fn rasterize_glyph(path: &Path, face_index: u32, glyph_index: u16, pixel_size: f32, variation_list: &[(u32, f32)]) -> Option<CoreTextGlyphRaster>` (method, since 0.4.0): Draws one glyph. (white, color glyphs in their own colors. None if there is nothing to draw or CoreText cannot read it)
  - `path` &Path: Font file path.
  - `face_index` u32: Face index within the collection (TTC).
  - `glyph_index` u16: Glyph ID.
  - `pixel_size` f32: Font size. (pixels)
  - `variation_list` &[(u32, f32)]: Variable font axis values. (list of (4-letter tag number, value))
  - returns Option<CoreTextGlyphRaster>: The drawn glyph. None if there is nothing to draw, CoreText could not build the font, or the image exceeds 4096 pixels.
