dymokomi/luce-browser-render
Source of release 0.6.2
0.6.2 / src / display_list / tests_cpu_glyph_run.lucb
#==============================================================================================
#
# tests_cpu_glyph_run - glyph runs drawn as Skia's CPU text draws them
#
# DESCRIPTION:
# The player's glyph masks against DisplayListPlayerSkia's pixels
# (tests_skia_reference_data_3.lucb): positions at every quarter-pixel phase and
# rounded baselines, the mask pre-blend for several text colors on colored
# backgrounds, vertical text (a rotated device matrix, whose glyphs keep their
# subpixel phase along y), a text blob at scale 1.5, and .notdef boxes. SerenitySans'
# glyphs carry no TrueType instructions but its .notdef's, which tt_hinting.lucb runs as
# FreeType's v40 interpreter does, so the pixels match exactly. The pre-blend tables come from powf, which differs in the last
# bit between C libraries: off macOS a table entry, and so a pixel, may be one level
# away.
#
#==============================================================================================
## The largest difference allowed in a pre-blend table entry or pixel: none on macOS, where
## the reference was recorded, one level elsewhere (powf).
let test_glyph_level: u32 = 0 if os.macos else 1
## The number of pixels allowed to differ by that level off macOS.
let test_glyph_pixels: usize = 0 if os.macos else 40
## SerenitySans at 12pt (16px) or 9pt (12px), from the repository root.
func test_serenity(point_size: f32) -> const web_fonts.Font*:
return test_font("tests/web_fonts/fonts/SerenitySans-Regular.ttf", point_size)
## Draws `run` at `baseline` in `color` as DisplayListRecorder::draw_glyph_run records it.
func test_draw_run(scene: const TestScene*, run: const web_fonts.GlyphRun*, x: f32, y: f32, color: gfx.Color, rect: gfx.DevicePixelRect, orientation: gfx.Orientation):
display_list_player_draw_glyph_run(test_scene_player(scene), DrawGlyphRun(glyph_run = run, rect = rect, translation = test_float_point(x, y), color = color, orientation = orientation))
test "cpu glyph run: the mask pre-blend tables are Skia's":
let text = skia_mask_gamma.bytes
var offset: usize = 0
for index in 0..<8u32:
# A gray whose canonical luminance has this index.
let base = index << 5
let gray = (u8)(base | (base >> 3) | (base >> 6))
var table: u8[256]
web_fonts.sk_mask_gamma_table(table, gray, gray, gray)
for i in 0..<256usize:
while text[offset] == ' ' or text[offset] == '\n':
offset += 1
var value: u32 = 0
for k in 0..<8usize:
value = (value << 4) | test_hex_digit(text[offset + k])
offset += 8
let actual = (u32)table[i]
let d = actual - value if actual > value else value - actual
if d > test_glyph_level:
print(f"table {index} entry {i}: {actual}, Skia's {value}")
trap("a mask pre-blend table differs from Skia's")
test "cpu glyph run: every quarter-pixel phase, baselines rounded":
with memory.heap:
let scene = test_scene_new(48, 40)
defer test_scene_release(&scene)
let run = test_glyph_run("fox", test_serenity(12.0), 1.0)
let black = test_color(0, 0, 0, 255)
let rect = test_rect(0, 0, 48, 40)
test_draw_run(&scene, run, 1.1, 12.4, black, rect, gfx.Orientation.horizontal)
test_draw_run(&scene, run, 24.3, 12.6, black, rect, gfx.Orientation.horizontal)
test_draw_run(&scene, run, 1.55, 30.5, black, rect, gfx.Orientation.horizontal)
test_draw_run(&scene, run, 24.8, 31.45, black, rect, gfx.Orientation.horizontal)
expect_like_skia(&scene, "glyph_run_phases", skia_glyph_run_phases, test_glyph_level, test_glyph_pixels)
test "cpu glyph run: text colors on colored backgrounds":
with memory.heap:
let scene = test_scene_new(48, 24)
defer test_scene_release(&scene)
let player = test_scene_player(&scene)
display_list_player_fill_rect(player, FillRect(rect = test_rect(0, 0, 24, 24), color = test_color(255, 165, 0, 255)))
display_list_player_fill_rect(player, FillRect(rect = test_rect(24, 0, 24, 24), color = test_color(255, 0, 255, 255)))
let run = test_glyph_run("ab", test_serenity(12.0), 1.0)
let rect = test_rect(0, 0, 48, 24)
test_draw_run(&scene, run, 2.25, 4.0, test_color(0, 0, 0, 255), rect, gfx.Orientation.horizontal)
test_draw_run(&scene, run, 26.5, 4.0, test_color(255, 255, 255, 255), rect, gfx.Orientation.horizontal)
test_draw_run(&scene, run, 2.75, 14.0, test_color(30, 140, 60, 255), rect, gfx.Orientation.horizontal)
test_draw_run(&scene, run, 26.0, 14.0, test_color(200, 200, 40, 128), rect, gfx.Orientation.horizontal)
expect_like_skia(&scene, "glyph_run_colors", skia_glyph_run_colors, test_glyph_level, test_glyph_pixels)
test "cpu glyph run: vertical text":
with memory.heap:
let scene = test_scene_new(24, 40)
defer test_scene_release(&scene)
let run = test_glyph_run("Hey", test_serenity(9.0), 1.0)
test_draw_run(&scene, run, 2.3, 4.6, test_color(0, 0, 128, 255), test_rect(3, 2, 18, 36), gfx.Orientation.vertical)
expect_like_skia(&scene, "glyph_run_vertical", skia_glyph_run_vertical, test_glyph_level, test_glyph_pixels)
test "cpu glyph run: a text blob at scale 1.5":
with memory.heap:
let scene = test_scene_new(64, 32)
defer test_scene_release(&scene)
let run = test_glyph_run("Hi, fox", test_serenity(9.0), 1.5)
test_draw_run(&scene, run, 1.5, 20.25, test_color(0, 0, 0, 255), test_rect(0, 0, 64, 32), gfx.Orientation.horizontal)
expect_like_skia(&scene, "glyph_run_serenity_scaled", skia_glyph_run_serenity_scaled, test_glyph_level, test_glyph_pixels)
test "cpu glyph run: .notdef boxes, hinted by their glyph program":
with memory.heap:
let scene = test_scene_new(48, 40)
defer test_scene_release(&scene)
# Two Hebrew letters SerenitySans does not have: its .notdef box, whose instructions
# snap the box's edges along y.
let run = test_glyph_run("אבa", test_serenity(12.0), 1.0)
test_draw_run(&scene, run, 1.3, 12.6, test_color(0, 0, 0, 255), test_rect(0, 0, 48, 40), gfx.Orientation.horizontal)
let run12 = test_glyph_run("אבa", test_serenity(9.0), 1.5)
test_draw_run(&scene, run12, 2.7, 31.4, test_color(0, 0, 0, 255), test_rect(0, 0, 48, 40), gfx.Orientation.horizontal)
expect_like_skia(&scene, "glyph_run_notdef", skia_glyph_run_notdef, test_glyph_level, test_glyph_pixels)