vanilladocs
vanillapunch.com
API / core/colorglyph

core/colorglyph

색 글리프 래스터. (이모지 글꼴의 색 글리프를 프리멀티플라이드 RGBA 로)

  • 원본은 브라우저 Canvas2D 가 색 글리프를 제 색으로 그린다. fillStyle 의 색은 먹지 않고 알파만 먹으며, strokeText 도 색 글리프는 테를 긋지 않고 색 그대로 칠한다. 러스트 판은 글꼴의 색 글리프 자료를 직접 읽어 그린다. 크롬처럼 OT-SVG 글리프는 그리지 않는다.
  • 비트맵: sbix (애플 컬러 이모지), CBDT/CBLC (노토 컬러 이모지 비트맵 판)의 PNG 와 프리멀티플라이드 BGRA 를 꺼내 풀고 글자 크기에 맞춘다. 판은 그 글리프를 가진 판 가운데 글자 크기 이상인 가장 작은 것, 없으면 가장 큰 것이다. (애플 컬러 이모지는 판마다 빠진 글리프가 있다) 줄일 때는 넓이 평균, 키울 때는 두 점 사이 섞기다. 자리는 판의 픽셀 단위 자리 값(그림의 왼쪽 아래가 펜 원점에서 떨어진 거리)을 글자 크기 비로 옮긴 것이다. 비트맵만 있는 CBDT 글꼴의 전진폭과 픽셀 상자는 크롬 리눅스(FreeType 과 스키아)처럼 판의 글리프 수치(정수 전진폭)와 그림 상자를 글자 크기 비로 옮긴다. (read_bitmap_glyph_advance, read_bitmap_glyph_bounds)
  • 윤곽: COLR/CPAL v0 (층마다 윤곽 하나를 한 색으로)과 v1 (그라데이션, 변환, 자르기, 합성). ttf-parser 의 Painter 로 받아 직접 칠한다. 윤곽은 곡선을 선분으로 펴서 넓이 누적 방식(font-rs 와 같은 방식, 0 이 아닌 감김)으로 덮인 정도를 셈한다. 칠은 한 색, 선형(세 점), 두 원 사이 방사, 스윕 그라데이션을 픽셀 가운데에서 셈한다. 색 줄의 끝 밖은 Pad, Repeat, Reflect 를 따르고 색은 곧은 알파로 섞는다. 합성은 COLR 의 28 가지(Porter-Duff 와 W3C 섞기)를 프리멀티플라이드 색으로 한다. 팔레트는 0 번, 글자 색(팔레트 번호 0xFFFF)은 흰색이다. (글자는 흰색으로 굽고 그릴 때 색을 곱한다) v1 의 칠(paint)은 지금 자르기 영역을 칠한다. 자르기가 없으면(v0) 마지막으로 받은 윤곽을 칠한다.
  • ttf-parser 는 rustybuzz 가 다시 내보내는 것을 쓴다. (모양 짓기 얼굴과 같은 얼굴)

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네모 윤곽의 덮인 정도 셈하기 (글꼴 단위 100 이 10 픽셀)Compute the coverage of a square outline (100 font units are 10 pixels)
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));

ColorGlyphRasterstruct

소스source

색 글리프 래스터 하나. (프리멀티플라이드 RGBA, 왼쪽 위 자리는 펜 원점에서 오른쪽 left_x, 기준선에서 위로 top_y)

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
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)
}
필드fieldpub pixels: Vec<u8>#0.4.0 부터since 0.4.0소스source

프리멀티플라이드 RGBA 픽셀. (위 줄부터 width x height x 4 바이트)

Premultiplied RGBA pixels. (from the top row, width x height x 4 bytes)

반환ReturnsVec<u8>

필드fieldpub width: i32#0.4.0 부터since 0.4.0소스source

너비. (픽셀)

Width. (pixels)

반환Returnsi32

필드fieldpub height: i32#0.4.0 부터since 0.4.0소스source

높이. (픽셀)

Height. (pixels)

반환Returnsi32

필드fieldpub left_x: i32#0.4.0 부터since 0.4.0소스source

펜 원점에서 그림 왼쪽까지. (픽셀, 오른쪽이 +)

From the pen origin to the left of the image. (pixels, right is +)

반환Returnsi32

필드fieldpub top_y: i32#0.4.0 부터since 0.4.0소스source

기준선에서 그림 위쪽까지. (픽셀, 위가 +)

From the baseline to the top of the image. (pixels, up is +)

반환Returnsi32

함수Functions

함수functionpub fn has_color_tables(face: &Face) -> bool#0.4.0 부터since 0.4.0소스source

글꼴에 색 글리프 표가 있는지. (sbix, CBDT, COLR)

Whether the font has color glyph tables. (sbix, CBDT, COLR)

face&Face
볼 글꼴 얼굴. (rustybuzz 가 다시 내보내는 ttf-parser 의 Face)Font face to look at. (ttf-parser's Face re-exported by rustybuzz)

반환Returnsbool sbix, CBDT, COLR 표 가운데 하나라도 있으면 true.true if any of the sbix, CBDT, COLR tables exists.

함수functionpub fn has_color_glyph(face: &Face, glyph_index: u16) -> bool#0.4.0 부터since 0.4.0소스source

글리프가 색 글리프인지. (COLR 에 있거나, PNG / BGRA 비트맵이 있다)

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_indexu16
글리프 번호.Glyph ID.

반환Returnsbool COLR 색 글리프이거나, 글자 크기 64 로 고른 판의 비트맵이 PNG 나 프리멀티플라이드 BGRA 면 true.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.

함수functionpub fn rasterize_color_glyph(face: &Face<'_>, glyph_index: u16, pixel_size: f32) -> Option<ColorGlyphRaster>#0.4.0 부터since 0.4.0소스source

색 글리프 래스터. (COLR 이 먼저, 없으면 비트맵, 색 글리프가 아니면 None)

Color glyph raster. (COLR first, otherwise a bitmap, None if not a color glyph)

face&Face<'_>
그릴 글꼴 얼굴.Font face to draw.
glyph_indexu16
글리프 번호.Glyph ID.
pixel_sizef32
글자 크기. (픽셀, em 크기, 0 이하면 None)Font size. (pixels, em size, None if 0 or less)

반환ReturnsOption<ColorGlyphRaster> 그린 래스터. 색 글리프가 아니거나, 그림을 풀지 못하거나, 크기가 0 이거나 가로세로 4096 픽셀을 넘으면 None.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.

함수functionpub fn read_bitmap_glyph_bounds(face: &Face<'_>, glyph_index: u16, pixel_size: f32) -> Option<(i32, i32, i32, i32)>#0.4.0 부터since 0.4.0소스source

비트맵 색 글리프의 픽셀 상자. (sbix, CBDT 그림의 자리와 크기를 글자 크기 비로 옮겨 바깥 정수로 넓힌 것, 스키아 FreeType 의 비트맵 글리프 상자)

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_indexu16
글리프 번호.Glyph ID.
pixel_sizef32
글자 크기. (픽셀)Font size. (pixels)

반환ReturnsOption<(i32, i32, i32, i32)> (왼쪽, 아래, 너비, 높이). (픽셀, 위가 양수) 그 글리프의 그림이 없으면 None.(left, bottom, width, height). (pixels, up is positive) None if the glyph has no image.

함수functionpub fn read_bitmap_glyph_advance(face: &Face<'_>, glyph_index: u16, pixel_size: f32) -> Option<f32>#0.4.0 부터since 0.4.0소스source

CBDT 비트맵 글리프의 전진폭. (CBLC 판의 글리프 수치에 적힌 정수 전진폭을 글자 크기 비로 옮긴 값)

  • 크롬 리눅스는 비트맵만 있는 글꼴을 FreeType 으로 연다. FreeType 은 판 크기의 글리프 수치(smallGlyphMetrics, bigGlyphMetrics 의 advance)를 전진폭으로 주고, 스키아(SkFontHost_FreeType)가 글자 크기 / 판 크기를 곱한다. 판은 글자 크기 이상인 가장 작은 것, 없으면 가장 큰 것이다. (chooseBitmapStrike)
  • CBDT 그림 꼴 17(작은 수치), 18(큰 수치), 19(CBLC 색인 꼴 2, 5 의 큰 수치)을 읽는다.

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_indexu16
글리프 번호.Glyph ID.
pixel_sizef32
글자 크기. (픽셀)Font size. (pixels)

반환ReturnsOption<f32> 전진폭. (픽셀) CBLC, CBDT 표가 없거나, 그 글리프가 어느 판에도 없거나, 표가 깨졌으면 None.Advance. (pixels) None if there is no CBLC or CBDT table, the glyph is in no strike, or the tables are broken.

함수functionpub fn read_outline_bounds(draw_outline: &dyn Fn(&mut dyn OutlineBuilder) -> bool, scale: f32) -> Option<(i32, i32, i32, i32)>#0.4.0 부터since 0.4.0소스source

윤곽이 덮는 픽셀 상자. (윤곽을 그리는 함수와 글꼴 단위 → 픽셀 배율. 펜 원점에서 왼쪽, 기준선에서 위, 너비, 높이. 비었으면 None)

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
받은 OutlineBuilder 에 윤곽을 그리는 함수. (글꼴 단위, 위가 +, 그렸으면 true)Function that draws the outline into the given OutlineBuilder. (font units, up is +, true if drawn)
scalef32
글꼴 단위에서 픽셀로의 배율.Scale from font units to pixels.

반환ReturnsOption<(i32, i32, i32, i32)> (펜 원점에서 왼쪽, 기준선에서 위, 너비, 높이). 픽셀 단위로 내림, 올림한 상자다. 함수가 false 를 돌려주거나, 선분이 없거나, 너비나 높이가 0 이하거나 4096 을 넘으면 None.(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.

함수functionpub fn rasterize_outline(draw_outline: &dyn Fn(&mut dyn OutlineBuilder) -> bool, scale: f32) -> Option<(Vec<u8>, i32, i32, i32, i32)>#0.4.0 부터since 0.4.0소스source

윤곽의 덮인 정도. (가변 글꼴의 글리프, fontdue 가 축 값을 읽지 못하고 맥 SFNS 같은 가변 글꼴의 윤곽을 그리지 못해 여기서 그린다. 비었으면 None)

  • 돌려주는 것: (덮인 정도, 너비, 높이, 펜 원점에서 왼쪽, 기준선에서 위). 색 글리프와 같은 넓이 누적 래스터다.

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
받은 OutlineBuilder 에 윤곽을 그리는 함수. (글꼴 단위, 위가 +, 그렸으면 true, 경계를 잴 때와 칠할 때 두 번 부른다)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)
scalef32
글꼴 단위에서 픽셀로의 배율.Scale from font units to pixels.

반환ReturnsOption<(Vec<u8>, i32, i32, i32, i32)> (덮인 정도, 너비, 높이, 펜 원점에서 왼쪽, 기준선에서 위). 덮인 정도는 위 줄부터 픽셀마다 0 ~ 255 한 바이트다. read_outline_bounds 가 None 이면 None.(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.