# core/colorglyph (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/colorglyph/

Color glyph raster. (color glyphs of emoji fonts to premultiplied RGBA)
- In the original, the browser's Canvas2D draws color glyphs in their own colors. Only the alpha of fillStyle applies, not its color, and strokeText also fills color glyphs in their own colors without stroking an outline.
  The Rust version reads the font's color glyph data directly and draws it. Like Chrome, it does not draw OT-SVG glyphs.
- Bitmaps: extracts and decodes the PNG and premultiplied BGRA of sbix (Apple Color Emoji) and CBDT/CBLC (the Noto Color Emoji bitmap version), and fits them to the font size.
  The strike used is the smallest one at or above the font size among strikes that have the glyph, otherwise the largest. (Apple Color Emoji has glyphs missing from some strikes) Shrinking uses area averaging, enlarging uses interpolation between two points.
  The position is the strike's pixel position value (the offset of the image's bottom left from the pen origin) scaled by the font size ratio.
  For bitmap-only CBDT fonts, the advance and pixel box are the strike's glyph metrics (integer advance) and image box scaled by the font size ratio, like Chrome on Linux (FreeType and Skia). (read_bitmap_glyph_advance, read_bitmap_glyph_bounds)
- Outlines: COLR/CPAL v0 (one outline per layer in one color) and v1 (gradients, transforms, clipping, compositing). Received through ttf-parser's Painter and painted directly.
  Outlines flatten curves into line segments and compute coverage with area accumulation (the same method as font-rs, nonzero winding).
  Paints (solid color, linear (three points), radial between two circles, sweep gradient) are evaluated at pixel centers. Outside the ends of the color line follows Pad, Repeat, Reflect, and colors are blended in straight alpha.
  Compositing does COLR's 28 modes (Porter-Duff and W3C blending) in premultiplied color. The palette is number 0, and the text color (palette index 0xFFFF) is white. (glyphs are baked in white and the color is multiplied when drawing)
  A v1 paint fills the current clip region. Without a clip (v0) it fills the last received outline.
- ttf-parser is the one re-exported by rustybuzz. (the same face as the shaping face)

Example: Compute the coverage of a square outline (100 font units are 10 pixels)

```rust
use vanilla::core::colorglyph::{rasterize_outline, read_outline_bounds};
use rustybuzz::ttf_parser::OutlineBuilder;

let draw_square = |outline_builder: &mut dyn OutlineBuilder| -> bool {
	outline_builder.move_to(0.0, 0.0);
	outline_builder.line_to(100.0, 0.0);
	outline_builder.line_to(100.0, 100.0);
	outline_builder.line_to(0.0, 100.0);
	outline_builder.close();
	true
};
assert_eq!(read_outline_bounds(&draw_square, 0.1), Some((0, 10, 10, 10)));
let (coverage, width, height, left_x, top_y) = rasterize_outline(&draw_square, 0.1).unwrap();
assert_eq!((width, height, left_x, top_y), (10, 10, 0, 10));
assert!(coverage.iter().all(|value| *value == 255));
```

## ColorGlyphRaster (struct)

One color glyph raster. (premultiplied RGBA, the top left is left_x to the right of the pen origin and top_y up from the baseline)

Example: Bake into a raster if it is a color glyph

```rust
use vanilla::core::colorglyph::{has_color_glyph, rasterize_color_glyph, ColorGlyphRaster};
use rustybuzz::ttf_parser::Face;

fn bake_color_glyph(face: &Face, glyph_index: u16) -> Option<ColorGlyphRaster> {
	if !has_color_glyph(face, glyph_index) {
		return None;
	}
	let color_glyph_raster = rasterize_color_glyph(face, glyph_index, 48.0)?;
	assert_eq!(color_glyph_raster.pixels.len(), (color_glyph_raster.width * color_glyph_raster.height * 4) as usize);
	Some(color_glyph_raster)
}
```

- `pub pixels: Vec<u8>` (field, since 0.4.0): Premultiplied RGBA pixels. (from the top row, width x height x 4 bytes)
  - returns Vec<u8>
- `pub width: i32` (field, since 0.4.0): Width. (pixels)
  - returns i32
- `pub height: i32` (field, since 0.4.0): Height. (pixels)
  - returns i32
- `pub left_x: i32` (field, since 0.4.0): From the pen origin to the left of the image. (pixels, right is +)
  - returns i32
- `pub top_y: i32` (field, since 0.4.0): From the baseline to the top of the image. (pixels, up is +)
  - returns i32

## Functions and constants

- `pub fn has_color_tables(face: &Face) -> bool` (method, since 0.4.0): Whether the font has color glyph tables. (sbix, CBDT, COLR)
  - `face` &Face: Font face to look at. (ttf-parser's Face re-exported by rustybuzz)
  - returns bool: true if any of the sbix, CBDT, COLR tables exists.
- `pub fn has_color_glyph(face: &Face, glyph_index: u16) -> bool` (method, since 0.4.0): Whether the glyph is a color glyph. (it is in COLR, or has a PNG / BGRA bitmap)
  - `face` &Face: Font face to look at.
  - `glyph_index` u16: Glyph ID.
  - returns bool: true if it is a COLR color glyph, or if the bitmap of the strike chosen for font size 64 is PNG or premultiplied BGRA.
- `pub fn rasterize_color_glyph(face: &Face<'_>, glyph_index: u16, pixel_size: f32) -> Option<ColorGlyphRaster>` (method, since 0.4.0): Color glyph raster. (COLR first, otherwise a bitmap, None if not a color glyph)
  - `face` &Face<'_>: Font face to draw.
  - `glyph_index` u16: Glyph ID.
  - `pixel_size` f32: Font size. (pixels, em size, None if 0 or less)
  - returns Option<ColorGlyphRaster>: The drawn raster. None if not a color glyph, if the image cannot be decoded, if the size is 0, or if it exceeds 4096 pixels in width or height.
- `pub fn read_bitmap_glyph_bounds(face: &Face<'_>, glyph_index: u16, pixel_size: f32) -> Option<(i32, i32, i32, i32)>` (method, since 0.4.0): Pixel box of a bitmap color glyph. (the position and size of the sbix or CBDT image scaled by the font size ratio and rounded outward to integers, Skia FreeType's bitmap glyph bounds)
  - `face` &Face<'_>: Font face.
  - `glyph_index` u16: Glyph ID.
  - `pixel_size` f32: Font size. (pixels)
  - returns Option<(i32, i32, i32, i32)>: (left, bottom, width, height). (pixels, up is positive) None if the glyph has no image.
- `pub fn read_bitmap_glyph_advance(face: &Face<'_>, glyph_index: u16, pixel_size: f32) -> Option<f32>` (method, since 0.4.0): Advance of a CBDT bitmap glyph. (the integer advance in the CBLC strike's glyph metrics scaled by the font size ratio)
  - Chrome on Linux opens bitmap-only fonts with FreeType. FreeType gives the strike-size glyph metrics (the advance of smallGlyphMetrics, bigGlyphMetrics) as the advance,
    and Skia (SkFontHost_FreeType) multiplies by font size / strike size. The strike is the smallest one at or above the font size, otherwise the largest. (chooseBitmapStrike)
  - Reads CBDT image formats 17 (small metrics), 18 (big metrics) and 19 (big metrics of CBLC index formats 2 and 5).
  - `face` &Face<'_>: Font face.
  - `glyph_index` u16: Glyph ID.
  - `pixel_size` f32: Font size. (pixels)
  - returns Option<f32>: Advance. (pixels) None if there is no CBLC or CBDT table, the glyph is in no strike, or the tables are broken.
- `pub fn read_outline_bounds(draw_outline: &dyn Fn(&mut dyn OutlineBuilder) -> bool, scale: f32) -> Option<(i32, i32, i32, i32)>` (method, since 0.4.0): Pixel box covered by the outline. (a function that draws the outline and the font unit to pixel scale. Left from the pen origin, up from the baseline, width, height. None if empty)
  - `draw_outline` &dyn Fn(&mut dyn OutlineBuilder) -> bool: Function that draws the outline into the given OutlineBuilder. (font units, up is +, true if drawn)
  - `scale` f32: Scale from font units to pixels.
  - returns Option<(i32, i32, i32, i32)>: (left from the pen origin, up from the baseline, width, height). A box floored and ceiled to pixel units. None if the function returns false, if there are no line segments, or if the width or height is 0 or less or exceeds 4096.
- `pub fn rasterize_outline(draw_outline: &dyn Fn(&mut dyn OutlineBuilder) -> bool, scale: f32) -> Option<(Vec<u8>, i32, i32, i32, i32)>` (method, since 0.4.0): Coverage of an outline. (glyphs of variable fonts, drawn here because fontdue cannot read axis values and cannot draw the outlines of variable fonts such as Mac SFNS. None if empty)
  - Returns: (coverage, width, height, left from the pen origin, up from the baseline). The same area accumulation raster as color glyphs.
  - `draw_outline` &dyn Fn(&mut dyn OutlineBuilder) -> bool: Function that draws the outline into the given OutlineBuilder. (font units, up is +, true if drawn, called twice, once to measure the bounds and once to fill)
  - `scale` f32: Scale from font units to pixels.
  - returns Option<(Vec<u8>, i32, i32, i32, i32)>: (coverage, width, height, left from the pen origin, up from the baseline). Coverage is one byte 0 ~ 255 per pixel, from the top row. None if read_outline_bounds is None.
