| 1 | //! Display-list backend for `anyrender`: the blitz-paint scene recorded into a |
| 2 | //! compact binary stream that a platform executor replays with its own 2D API — |
| 3 | //! Android `Canvas` today (`CanvasScene.kt` in android/renderer-blitz), QPainter |
| 4 | //! would be the tdesktop twin. Layout, cascade and shaping stay in Rust; the |
| 5 | //! platform draws paths, gradients, images and — through its own font engine, |
| 6 | //! from the very bytes parley shaped with — the glyphs. Platform-neutral, so it |
| 7 | //! is unit-tested on the host. Selected with `TWB_BACKEND=canvas` or |
| 8 | //! `twb_set_backend(h, "canvas")` / `BlitzNative.setBackend(h, "canvas")`. |
| 9 | //! |
| 10 | //! Stream (little-endian, f32/u32/u64; the Kotlin reader mirrors this): |
| 11 | //! ```text |
| 12 | //! u32 magic 'TWDL' (0x4C445754), u32 version (1), u32 op count, then ops: |
| 13 | //! 1 PushLayer: u8 rule, u8 mix, u8 compose, f32 alpha, path (clip path in base coordinates) |
| 14 | //! 2 PushClip: u8 rule, path |
| 15 | //! 3 Pop |
| 16 | //! 4 Fill: u8 rule, xf, paint, path |
| 17 | //! 5 Stroke: xf, paint, f32 width, u8 join, u8 cap, f32 miter, u32 ndash, f32×ndash, f32 dash offset, path |
| 18 | //! 6 Glyphs: u64 font id, u32 face, f32 size, u32 naxes, (u32 tag, f32 value)×naxes, u32 ARGB, |
| 19 | //! xf, f32 skew (x shear of the glyph transform), u32 n, (u32 id, f32 x, f32 y)×n |
| 20 | //! 7 Shadow: xf, f32 x0 y0 x1 y1, u32 ARGB, f32 corner radius, f32 sigma |
| 21 | //! xf = 6 × f32 (a b c d tx ty), x' = a·x + c·y + tx, y' = b·x + d·y + ty |
| 22 | //! path = u32 nverbs, then u8 verb: 0 M(x y) 1 L(x y) 2 Q(cx cy x y) 3 C(c1x c1y c2x c2y x y) 4 Z |
| 23 | //! paint = u8 kind: 0 solid(u32 ARGB) | 1 linear(f32 x0 y0 x1 y1, u8 extend, stops) | 2 radial(f32 cx0 cy0 r0 cx1 cy1 r1, u8 extend, stops) |
| 24 | //! | 3 sweep(f32 cx cy a0 a1, u8 extend, stops) | 4 image(u64 id, u32 w, u32 h, u8 xext, u8 yext, u8 quality, f32 alpha) | 5 none, |
| 25 | //! then u8 has brush transform (+ xf). stops = u32 n, (f32 offset, u32 ARGB)×n. rule: 0 non-zero, 1 even-odd. extend: 0 pad 1 repeat 2 reflect. |
| 26 | //! ``` |
| 27 | //! Fonts and images are referenced by blob id; the bytes are fetched once per |
| 28 | //! id through `font_blob` / `image` (JNI `fontBytes` / `imagePixels`). |
| 29 | |
| 30 | use anyrender::{Filter, Glyph, NormalizedCoord, Paint, PaintRef, PaintScene, RenderContext}; |
| 31 | use blitz_dom::BaseDocument; |
| 32 | use blitz_paint::paint_scene; |
| 33 | use kurbo::{Affine, Cap, Join, PathEl, Point, Rect, Shape, Stroke}; |
| 34 | use peniko::color::Srgb; |
| 35 | use peniko::{BlendMode, Blob, Color, ColorStops, Extend, Fill, FontData, GradientKind, ImageAlphaType, ImageData, ImageFormat, ImageQuality, StyleRef |
| 36 | use skrifa::MetadataProvider; |
| 37 | use std::collections::HashMap; |
| 38 | use std::sync::{Arc, Mutex, OnceLock}; |
| 39 | |
| 40 | pub const MAGIC: u32 = 0x4C44_5754; |
| 41 | pub const VERSION: u32 = 1; |
| 42 | const TOLERANCE: f64 = 0.1; |
| 43 | |
| 44 | static FONTS: OnceLock<Mutex<HashMap<u64, Blob<u8>>>> = OnceLock::new(); |
| 45 | static IMAGES: OnceLock<Mutex<HashMap<u64, ImageData>>> = OnceLock::new(); |
| 46 | |
| 47 | /// The font file behind a glyph op's font id (kept alive for the process once referenced). |
| 48 | pub fn font_blob(id: u64) -> Option<Blob<u8>> { |
| 49 | FONTS.get_or_init(Default::default).lock().unwrap().get(&id).cloned() |
| 50 | } |
| 51 | |
| 52 | /// The decoded image behind an image paint's id. |
| 53 | pub fn image(id: u64) -> Option<ImageData> { |
| 54 | IMAGES.get_or_init(Default::default).lock().unwrap().get(&id).cloned() |
| 55 | } |
| 56 | |
| 57 | /// `image` as premultiplied RGBA8 rows (what a Bitmap wants), whatever the decoder produced. |
| 58 | pub fn image_rgba_premultiplied(img: &ImageData) -> Vec<u8> { |
| 59 | let src = img.data.data(); |
| 60 | let n = (img.width as usize) * (img.height as usize) * 4; |
| 61 | let mut out = vec![0u8; n]; |
| 62 | for (o, px) in out.chunks_exact_mut(4).zip(src.chunks_exact(4)) { |
| 63 | let (r, g, b, a) = match img.format { |
| 64 | ImageFormat::Rgba8 => (px[0], px[1], px[2], px[3]), |
| 65 | ImageFormat::Bgra8 => (px[2], px[1], px[0], px[3]), |
| 66 | _ => (px[0], px[1], px[2], px[3]), |
| 67 | }; |
| 68 | let (r, g, b) = match img.alpha_type { |
| 69 | ImageAlphaType::AlphaPremultiplied => (r, g, b), |
| 70 | _ => { |
| 71 | let m = |c: u8| ((c as u32 * a as u32 + 127) / 255) as |
| 72 | (m(r), m(g), m(b)) |
| 73 | } |
| 74 | }; |
| 75 | o.copy_from_slice(&[r, g, b, a]); |
| 76 | } |
| 77 | out |
| 78 | } |
| 79 | |
| 80 | fn argb(c: Color) -> u32 { |
| 81 | let [r, g, b, a] = c.components; |
| 82 | let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u32; |
| 83 | (q(a) << 24) | (q(r) << 16) | (q(g) << 8) | q(b) |
| 84 | } |
| 85 | |
| 86 | fn extend_code(e: Extend) -> u8 { |
| 87 | match e { |
| 88 | Extend::Pad => 0, |
| 89 | Extend::Repeat => 1, |
| 90 | Extend::Reflect => 2, |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /// User-space values of the non-default variation axes (normalized F2Dot14 → axis units), as (tag, value). |
| 95 | pub fn axis_values(font: &FontData, coords: &[NormalizedCoord]) -> Vec<(u32, f32)> { |
| 96 | let Ok(fr) = skrifa::FontRef::from_index(font.data.data(), font.index) else { return Vec::new() }; |
| 97 | let mut out = Vec::new(); |
| 98 | for (i, axis) in fr.axes().iter().enumerate() { |
| 99 | let n = coords.get(i).copied().unwrap_or(0) as f32 / 16384.0; |
| 100 | if n == 0.0 { |
| 101 | continue; |
| 102 | } |
| 103 | let (min, def, max) = (axis.min_value(), axis.default_value(), axis.max_value()); |
| 104 | let v = if n < 0.0 { def + n * (def - min) } else { def + n * (max - def) }; |
| 105 | out.push((u32::from_be_bytes(axis.tag().to_be_bytes()), v)); |
| 106 | } |
| 107 | out |
| 108 | } |
| 109 | |
| 110 | /// Records one frame. |
| 111 | pub struct DisplayListScene { |
| 112 | buf: Vec<u8>, |
| 113 | count: u32, |
| 114 | } |
| 115 | |
| 116 | impl Default for DisplayListScene { |
| 117 | fn default() -> Self { |
| 118 | Self::new() |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl DisplayListScene { |
| 123 | pub fn new() -> Self { |
| 124 | let mut s = Self { buf: Vec::with_capacity(16 * 1024), count: 0 }; |
| 125 | s.u32(MAGIC); |
| 126 | s.u32(VERSION); |
| 127 | s.u32(0); |
| 128 | s |
| 129 | } |
| 130 | |
| 131 | /// The finished stream (op count patched in). |
| 132 | pub fn finish(mut self) -> Vec<u8> { |
| 133 | self.buf[8..12].copy_from_slice(&self.count.to_le_bytes()); |
| 134 | self.buf |
| 135 | } |
| 136 | |
| 137 | pub fn op_count(&self) -> u32 { |
| 138 | self.count |
| 139 | } |
| 140 | |
| 141 | fn u8(&mut self, v: u8) { |
| 142 | self.buf.push(v); |
| 143 | } |
| 144 | fn u32(&mut self, v: u32) { |
| 145 | self.buf.extend_from_slice(&v.to_le_bytes()); |
| 146 | } |
| 147 | fn u64(&mut self, v: u64) { |
| 148 | self.buf.extend_from_slice(&v.to_le_bytes()); |
| 149 | } |
| 150 | fn f32(&mut self, v: f64) { |
| 151 | self.buf.extend_from_slice(&(v as f32).to_le_bytes()); |
| 152 | } |
| 153 | fn op(&mut self, code: u8) { |
| 154 | self.u8(code); |
| 155 | self.count += 1; |
| 156 | } |
| 157 | fn xf(&mut self, a: Affine) { |
| 158 | for c in a.as_coeffs() { |
| 159 | self.f32(c); |
| 160 | } |
| 161 | } |
| 162 | fn rule(&mut self, f: Fill) { |
| 163 | self.u8(match f { |
| 164 | Fill::NonZero => 0, |
| 165 | Fill::EvenOdd => 1, |
| 166 | }); |
| 167 | } |
| 168 | |
| 169 | fn path(&mut self, shape: &impl Shape, transform: Option<Affine>) { |
| 170 | let at = self.buf.len(); |
| 171 | self.u32(0); |
| 172 | let mut n = 0u32; |
| 173 | let t = |p: Point| transform.map_or(p, |a| a * p); |
| 174 | for el in shape.path_elements(TOLERANCE) { |
| 175 | n += 1; |
| 176 | match el { |
| 177 | PathEl::MoveTo(p) => { |
| 178 | let p = t(p); |
| 179 | self.u8(0); |
| 180 | self.f32(p.x); |
| 181 | self.f32(p.y); |
| 182 | } |
| 183 | PathEl::LineTo(p) => { |
| 184 | let p = t(p); |
| 185 | self.u8(1); |
| 186 | self.f32(p.x); |
| 187 | self.f32(p.y); |
| 188 | } |
| 189 | PathEl::QuadTo(c, p) => { |
| 190 | let (c, p) = (t(c), t(p)); |
| 191 | self.u8(2); |
| 192 | for v in [c.x, c.y, p.x, p.y] { |
| 193 | self.f32(v); |
| 194 | } |
| 195 | } |
| 196 | PathEl::CurveTo(c1, c2, p) => { |
| 197 | let (c1, c2, p) = (t(c1), t(c2), t(p)); |
| 198 | self.u8(3); |
| 199 | for v in [c1.x, c1.y, c2.x, c2.y, p.x, p.y] { |
| 200 | self.f32(v); |
| 201 | } |
| 202 | } |
| 203 | PathEl::ClosePath => self.u8(4), |
| 204 | } |
| 205 | } |
| 206 | self.buf[at..at + 4].copy_from_slice(&n.to_le_bytes()); |
| 207 | } |
| 208 | |
| 209 | fn stops(&mut self, stops: &ColorStops) { |
| 210 | self.u32(stops.len() as u32); |
| 211 | for s in stops.iter() { |
| 212 | self.f32(s.offset as f64); |
| 213 | self.u32(argb(s.color.to_alpha_color::<Srgb>())); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | fn paint(&mut self, paint: PaintRef<'_>, brush_transform: Option<Affine>) { |
| 218 | match paint { |
| 219 | Paint::Solid(c) => { |
| 220 | self.u8(0); |
| 221 | self.u32(argb(c)); |
| 222 | } |
| 223 | Paint::Gradient(g) => { |
| 224 | match &g.kind { |
| 225 | GradientKind::Linear(l) => { |
| 226 | self.u8(1); |
| 227 | for v in [l.start.x, l.start.y, l.end.x, l.end.y] { |
| 228 | self.f32(v); |
| 229 | } |
| 230 | } |
| 231 | GradientKind::Radial(r) => { |
| 232 | self.u8(2); |
| 233 | for v in [r.start_center.x, r.start_center.y, r.start_radius as f64, r.end_center.x, r.end_center.y, r.end_radius as |
| 234 | self.f32(v); |
| 235 | } |
| 236 | } |
| 237 | GradientKind::Sweep(s) => { |
| 238 | self.u8(3); |
| 239 | for v in [s.center.x, s.center.y, s.start_angle as f64, s.end_angle as f64] { |
| 240 | self.f32(v); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | self.u8(extend_code(g.extend)); |
| 245 | self.stops(&g.stops); |
| 246 | } |
| 247 | Paint::Image(img) => { |
| 248 | let id = img.image.data.id(); |
| 249 | IMAGES.get_or_init(Default::default).lock().unwrap().entry(id).or_insert_with(|| img.image.clone()); |
| 250 | self.u8(4); |
| 251 | self.u64(id); |
| 252 | self.u32(img.image.width); |
| 253 | self.u32(img.image.height); |
| 254 | self.u8(extend_code(img.sampler.x_extend)); |
| 255 | self.u8(extend_code(img.sampler.y_extend)); |
| 256 | self.u8(match img.sampler.quality { |
| 257 | ImageQuality::Low => 0, |
| 258 | ImageQuality::Medium => 1, |
| 259 | ImageQuality::High => 2, |
| 260 | }); |
| 261 | self.f32(img.sampler.alpha as f64); |
| 262 | } |
| 263 | Paint::Resource(_) | Paint::Custom(_) => self.u8(5), |
| 264 | } |
| 265 | match brush_transform { |
| 266 | Some(t) => { |
| 267 | self.u8(1); |
| 268 | self.xf(t); |
| 269 | } |
| 270 | None => self.u8(0), |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | impl RenderContext for DisplayListScene {} |
| 276 | |
| 277 | impl PaintScene for DisplayListScene { |
| 278 | fn reset(&mut self) { |
| 279 | *self = Self::new(); |
| 280 | } |
| 281 | |
| 282 | fn push_layer( |
| 283 | &mut self, |
| 284 | fill: Fill, |
| 285 | blend: impl Into<BlendMode>, |
| 286 | alpha: f32, |
| 287 | transform: Affine, |
| 288 | clip: &impl Shape, |
| 289 | _filter: Option<Arc<Filter>>, |
| 290 | _backdrop_filter: Option<Arc<Filter>>, |
| 291 | ) { |
| 292 | let blend = blend.into(); |
| 293 | self.op(1); |
| 294 | self.rule(fill); |
| 295 | self.u8(blend.mix as u8); |
| 296 | self.u8(blend.compose as u8); |
| 297 | self.f32(alpha as f64); |
| 298 | self.path(clip, Some(transform)); |
| 299 | } |
| 300 | |
| 301 | fn push_clip_layer(&mut self, fill: Fill, transform: Affine, clip: &impl Shape) { |
| 302 | self.op(2); |
| 303 | self.rule(fill); |
| 304 | self.path(clip, Some(transform)); |
| 305 | } |
| 306 | |
| 307 | fn pop_layer(&mut self) { |
| 308 | self.op(3); |
| 309 | } |
| 310 | |
| 311 | fn stroke<'a>(&mut self, style: &Stroke, transform: Affine, brush: impl Into<PaintRef<'a>>, brush_transform: |
| 312 | self.op(5); |
| 313 | self.xf(transform); |
| 314 | self.paint(brush.into(), brush_transform); |
| 315 | self.f32(style.width); |
| 316 | self.u8(match style.join { |
| 317 | Join::Miter => 0, |
| 318 | Join::Round => 1, |
| 319 | Join::Bevel => 2, |
| 320 | }); |
| 321 | self.u8(match style.start_cap { |
| 322 | Cap::Butt => 0, |
| 323 | Cap::Round => 1, |
| 324 | Cap::Square => 2, |
| 325 | }); |
| 326 | self.f32(style.miter_limit); |
| 327 | self.u32(style.dash_pattern.len() as u32); |
| 328 | for d in style.dash_pattern.iter() { |
| 329 | self.f32(*d); |
| 330 | } |
| 331 | self.f32(style.dash_offset); |
| 332 | self.path(shape, None); |
| 333 | } |
| 334 | |
| 335 | fn fill<'a>(&mut self, style: Fill, transform: Affine, brush: impl Into<PaintRef<'a>>, brush_transform: |
| 336 | self.op(4); |
| 337 | self.rule(style); |
| 338 | self.xf(transform); |
| 339 | self.paint(brush.into(), brush_transform); |
| 340 | self.path(shape, None); |
| 341 | } |
| 342 | |
| 343 | fn draw_glyphs<'a, 's: 'a>( |
| 344 | &'s mut self, |
| 345 | font: &'a FontData, |
| 346 | font_size: f32, |
| 347 | _hint: bool, |
| 348 | normalized_coords: &'a [NormalizedCoord], |
| 349 | _embolden: kurbo::Vec2, |
| 350 | _style: impl Into<StyleRef<'a>>, |
| 351 | brush: impl Into<PaintRef<'a>>, |
| 352 | brush_alpha: f32, |
| 353 | transform: Affine, |
| 354 | glyph_transform: Option<Affine>, |
| 355 | glyphs: impl Iterator<Item = Glyph> + Clone, |
| 356 | ) { |
| 357 | let paint: PaintRef<'a> = brush.into(); |
| 358 | let mut color = match paint { |
| 359 | Paint::Solid(c) => c, |
| 360 | Paint::Gradient(g) => g.stops.first().map(|s| s.color.to_alpha_color::<Srgb>()).unwrap_or(Color |
| 361 | _ => return, |
| 362 | }; |
| 363 | color.components[3] *= brush_alpha.clamp(0.0, 1.0); |
| 364 | let glyphs: Vec<Glyph> = glyphs.collect(); |
| 365 | if glyphs.is_empty() { |
| 366 | return; |
| 367 | } |
| 368 | let id = font.data.id(); |
| 369 | FONTS.get_or_init(Default::default).lock().unwrap().entry(id).or_insert_with(|| font.data.clone()); |
| 370 | self.op(6); |
| 371 | self.u64(id); |
| 372 | self.u32(font.index); |
| 373 | self.f32(font_size as f64); |
| 374 | let axes = axis_values(font, normalized_coords); |
| 375 | self.u32(axes.len() as u32); |
| 376 | for (tag, v) in axes { |
| 377 | self.u32(tag); |
| 378 | self.f32(v as f64); |
| 379 | } |
| 380 | self.u32(argb(color)); |
| 381 | self.xf(transform); |
| 382 | self.f32(glyph_transform.map_or(0.0, |g| g.as_coeffs()[2])); |
| 383 | self.u32(glyphs.len() as u32); |
| 384 | for g in glyphs { |
| 385 | self.u32(g.id); |
| 386 | self.f32(g.x as f64); |
| 387 | self.f32(g.y as f64); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | fn draw_box_shadow(&mut self, transform: Affine, rect: Rect, brush: Color, radius: f64, std_dev: f64) { |
| 392 | self.op(7); |
| 393 | self.xf(transform); |
| 394 | for v in [rect.x0, rect.y0, rect.x1, rect.y1] { |
| 395 | self.f32(v); |
| 396 | } |
| 397 | self.u32(argb(brush)); |
| 398 | self.f32(radius); |
| 399 | self.f32(std_dev); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /// One frame of `doc` as a display list, `scale` = device pixel ratio, `w`×`h` device px. |
| 404 | pub fn record(doc: &mut BaseDocument, scale: f64, w: u32, h: u32) -> Vec<u8> { |
| 405 | let mut scene = DisplayListScene::new(); |
| 406 | paint_scene(&mut scene, doc, scale, w, h, 0, 0); |
| 407 | scene.finish() |
| 408 | } |
| 409 | |
| 410 | #[cfg(test)] |
| 411 | mod tests { |
| 412 | use super::*; |
| 413 | use peniko::color::palette::css::{BLUE, RED}; |
| 414 | |
| 415 | fn u32_at(b: &[u8], i: usize) -> u32 { |
| 416 | u32::from_le_bytes(b[i..i + 4].try_into().unwrap()) |
| 417 | } |
| 418 | fn f32_at(b: &[u8], i: usize) -> f32 { |
| 419 | f32::from_le_bytes(b[i..i + 4].try_into().unwrap()) |
| 420 | } |
| 421 | |
| 422 | #[test] |
| 423 | fn fill_records_rule_transform_paint_and_path() { |
| 424 | let mut s = DisplayListScene::new(); |
| 425 | s.fill(Fill::EvenOdd, Affine::translate((5.0, 7.0)), RED, None, &Rect::new(0.0, 0.0, 10.0, |
| 426 | let b = s.finish(); |
| 427 | assert_eq!(u32_at(&b, 0), MAGIC); |
| 428 | assert_eq!(u32_at(&b, 8), 1, "one op"); |
| 429 | let mut i = 12; |
| 430 | assert_eq!(b[i], 4, "fill"); |
| 431 | i += 1; |
| 432 | assert_eq!(b[i], 1, "even-odd"); |
| 433 | i += 1; |
| 434 | let xf: Vec<f32> = (0..6).map(|k| f32_at(&b, i + k * 4 |
| 435 | assert_eq!(xf, vec![1.0, 0.0, 0.0, 1.0, 5.0, 7.0]); |
| 436 | i += 24; |
| 437 | assert_eq!(b[i], 0, "solid"); |
| 438 | assert_eq!(u32_at(&b, i + 1), 0xFFFF_0000, "opaque red ARGB"); |
| 439 | i += 5; |
| 440 | assert_eq!(b[i], 0, "no brush transform"); |
| 441 | i += 1; |
| 442 | assert_eq!(u32_at(&b, i), 5, "M L L L Z"); |
| 443 | i += 4; |
| 444 | assert_eq!(b[i], 0, "move"); |
| 445 | assert_eq!((f32_at(&b, i + 1), f32_at(&b, i + 5)), (0.0, 0.0)); |
| 446 | } |
| 447 | |
| 448 | #[test] |
| 449 | fn clip_paths_are_pre_transformed_and_layers_carry_alpha() { |
| 450 | let mut s = DisplayListScene::new(); |
| 451 | s.push_layer(Fill::NonZero, peniko::Mix::Multiply, 0.5, Affine::translate((100.0, 0.0)), &Rect::new(0.0, |
| 452 | s.pop_layer(); |
| 453 | let b = s.finish(); |
| 454 | assert_eq!(u32_at(&b, 8), 2); |
| 455 | let i = 12; |
| 456 | assert_eq!(b[i], 1); |
| 457 | assert_eq!((b[i + 1], b[i + 2], b[i + 3]), (0, 1, 3), "non-zero, multiply, src-over"); |
| 458 | assert_eq!(f32_at(&b, i + 4), 0.5); |
| 459 | assert_eq!(u32_at(&b, i + 8), 5); |
| 460 | assert_eq!(b[i + 12], 0); |
| 461 | assert_eq!(f32_at(&b, i + 13), 100.0, "clip x translated into base space"); |
| 462 | assert_eq!(b[b.len() - 1], 3, "pop"); |
| 463 | } |
| 464 | |
| 465 | #[test] |
| 466 | fn gradients_keep_their_stops() { |
| 467 | let g = peniko::Gradient::new_linear((0.0, 0.0), (10.0, 0.0)).with_stops([RED, BLUE]); |
| 468 | let mut s = DisplayListScene::new(); |
| 469 | s.fill(Fill::NonZero, Affine::IDENTITY, &g, Some(Affine::scale(2.0)), &Rect::new(0.0, 0.0, |
| 470 | let b = s.finish(); |
| 471 | let i = 12 + 1 + 1 + 24; |
| 472 | assert_eq!(b[i], 1, "linear"); |
| 473 | assert_eq!(f32_at(&b, i + 9), 10.0, "end x"); |
| 474 | assert_eq!(b[i + 17], 0, "pad"); |
| 475 | assert_eq!(u32_at(&b, i + 18), 2, "two stops"); |
| 476 | assert_eq!(u32_at(&b, i + 22 + 4), 0xFFFF_0000); |
| 477 | assert_eq!(u32_at(&b, i + 22 + 8 + 4), 0xFF00_00FF); |
| 478 | assert_eq!(b[i + 22 + 16], 1, "brush transform follows"); |
| 479 | } |
| 480 | |
| 481 | #[test] |
| 482 | fn glyph_runs_register_the_font_and_carry_axes() { |
| 483 | let Ok(bytes) = std::fs::read("/System/Library/Fonts/SFNS.ttf") else { return }; |
| 484 | let font = FontData { data: Blob::new(Arc::new(bytes)), index: 0 }; |
| 485 | let mut s = DisplayListScene::new(); |
| 486 | // wght is the first axis of SF: +0.5 of the way to the maximum. |
| 487 | s.draw_glyphs(&font, 24.0, true, &[8192], kurbo::Vec2::ZERO, Fill::NonZero, RED, 1.0, Affine::IDENTITY |
| 488 | let b = s.finish(); |
| 489 | let i = 12; |
| 490 | assert_eq!(b[i], 6); |
| 491 | assert_eq!(u64::from_le_bytes(b[i + 1..i + 9].try_into().unwrap()), font.data.id()); |
| 492 | assert!(font_blob(font.data.id()).is_some(), "font registered for the executor"); |
| 493 | assert_eq!(f32_at(&b, i + 13), 24.0); |
| 494 | let naxes = u32_at(&b, i + 17) as usize; |
| 495 | assert!(naxes >= 1, "a variation axis is carried"); |
| 496 | let axes: Vec<(u32, f32)> = (0..naxes).map(|k| (u32_at(&b, i + 21 |
| 497 | let (tag, v) = axes[0]; |
| 498 | let fr = skrifa::FontRef::from_index(font.data.data(), 0).unwrap(); |
| 499 | let axis = fr.axes().get(0).unwrap(); |
| 500 | assert_eq!(tag, u32::from_be_bytes(axis.tag().to_be_bytes()), "first axis of the font"); |
| 501 | assert!(v > axis.default_value() && v <= axis.max_value(), "halfway to the maximum in axis units: {v}"); |
| 502 | } |
| 503 | } |
| 504 | |