dymokomi/luce-svg
Source of release 0.9.2
0.9.2 / src / svg / text_tests_full.lucb
#==============================================================================================
#
# svg/text_tests_full - Tests of SVG text layout with a synthetic font
#
#==============================================================================================
## A font of boxes: every scalar but a space is a 600 by 700 unit box on the baseline,
## advancing 600 units of a 1000-unit em; "AV" kerns by -100.
struct BoxFont: GlyphSource:
var selected: u32 = 0
func select_face(query: FaceQuery) -> u32?:
self.selected += 1
return 1
func fallback_face(face: u32, scalar: u32) -> u32?:
return none
func face_metrics(face: u32) -> FaceMetrics:
return FaceMetrics()
func glyph_index(face: u32, scalar: u32) -> u32:
return scalar
func glyph_advance(face: u32, glyph: u32) -> f64:
return 600.0
func glyph_kerning(face: u32, left: u32, right: u32) -> f64:
return -100.0 if left == 65 and right == 86 else 0.0
func shape(face: u32, scalars: const u32[], rtl: bool, kerning: bool, small_caps: bool, out: GlyphRecord[]) -> usize?!:
return none
func glyph_outline(face: u32, glyph: u32, path: Path*) -> bool!:
if glyph == 32:
return false
try path.rect(0.0, 0.0, 600.0, 700.0)
return true
## Draw `source` with the box font.
func draw_text_test(source: str, width: u32, height: u32) -> u8[]!:
var font = BoxFont()
let picture = try load(source, Options(glyphs = &font))
defer picture.release()
let pixels = try new u8[(usize)width * (usize)height * 4] --- in memory.heap
errdefer free(pixels) in memory.heap
try render(picture.value(), fit_transform(picture.value(), width, height), width, height, pixels)
return pixels
test "text sets glyphs on the baseline, anchored, spaced and shifted":
# 10 px to the em: boxes 6 wide and 7 tall above the baseline at y 20.
let pixels = try draw_text_test("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"60\" height=\"30\"><text x=\"10\" y=\"20\" font-size=\"10\">AB<tspan dx=\"5\" fill=\"blue\">C</tspan></text></svg>", 60, 30)
defer free(pixels) in memory.heap
assert(rgba_at(pixels, 60, 12, 16)[3] == 255 and rgba_at(pixels, 60, 12, 11)[3] == 0 and rgba_at(pixels, 60, 12, 21)[3] == 0)
# B at 16..22, then C shifted 5 to 27..33 in blue.
assert(rgba_at(pixels, 60, 18, 16)[3] == 255 and rgba_at(pixels, 60, 24, 16)[3] == 0)
let c = rgba_at(pixels, 60, 30, 16)
assert(c[2] == 255 and c[3] == 255)
test "text-anchor, letter-spacing, kerning and decorations":
let pixels = try draw_text_test("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"60\" height=\"40\"><text x=\"30\" y=\"10\" font-size=\"10\" text-anchor=\"middle\">AA</text><text x=\"0\" y=\"25\" font-size=\"10\">AV</text><text x=\"0\" y=\"38\" font-size=\"10\" text-decoration=\"underline\">B</text></svg>", 60, 40)
defer free(pixels) in memory.heap
# Two boxes 12 wide centred on 30: 24..36.
assert(rgba_at(pixels, 60, 25, 6)[3] == 255 and rgba_at(pixels, 60, 35, 6)[3] == 255 and rgba_at(pixels, 60, 37, 6)[3] == 0)
# V kerned 1 px left: 5..11 rather than 6..12.
assert(rgba_at(pixels, 60, 11, 22)[3] == 0)
# The underline sits 1 px under the baseline (position -100 units).
assert(rgba_at(pixels, 60, 3, 39)[3] > 0 or rgba_at(pixels, 60, 3, 38)[3] > 0)
test "text follows a text path":
let pixels = try draw_text_test("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"40\" height=\"40\"><path id=\"p\" d=\"M5 30 H35\"/><text font-size=\"10\"><textPath href=\"#p\" startOffset=\"10\">A</textPath></text></svg>", 40, 40)
defer free(pixels) in memory.heap
assert(rgba_at(pixels, 40, 17, 27)[3] == 255 and rgba_at(pixels, 40, 13, 27)[3] == 0)
test "textLength stretches spacing, or spacing and glyphs":
let pixels = try draw_text_test("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"40\" height=\"30\"><text x=\"0\" y=\"10\" font-size=\"10\" textLength=\"24\" lengthAdjust=\"spacingAndGlyphs\">AA</text><text x=\"0\" y=\"25\" font-size=\"10\" textLength=\"24\">AA</text></svg>", 40, 30)
defer free(pixels) in memory.heap
# Stretched glyphs: 0..12 and 12..24 solid.
assert(rgba_at(pixels, 40, 8, 6)[3] == 255 and rgba_at(pixels, 40, 22, 6)[3] == 255 and rgba_at(pixels, 40, 25, 6)[3] == 0)
# Spaced: boxes at 0..6 and, the gap spread, 18..24.
assert(rgba_at(pixels, 40, 8, 21)[3] == 0 and rgba_at(pixels, 40, 20, 21)[3] == 255)
test "path lengths and positions along a path, as kurbo measures them":
# A straight cubic 100 long, and a cubic quarter circle of radius 100.
let line = straight(Point(0.0, 0.0), Point(60.0, 80.0))
assert(math.abs(line.length(0.5) - 100.0) < 1e-9)
let point = line.at(line.parameter(50.0, 1e-6))
assert(math.abs(point.x - 30.0) < 1e-4 and math.abs(point.y - 40.0) < 1e-4)
let k = 0.5522847498
let arc = Bezier(p0 = Point(100.0, 0.0), p1 = Point(100.0, 100.0 * k), p2 = Point(100.0 * k, 100.0), p3 = Point(0.0, 100.0))
assert(math.abs(arc.length(0.01) - 157.08) < 0.05)
# Half way along lies on the diagonal.
let middle = arc.at(arc.parameter(arc.length(1e-6) * 0.5, 1e-6))
assert(math.abs(middle.x - middle.y) < 1e-3)
test "a filter on text measures the text box, and tref needs an SVG element":
# Box font: ascent 800, descent -200 units at 10 px: the box spans y 2..12 for a
# baseline at 10, x 5..17 for two glyphs; the flood fills exactly that.
let pixels = try draw_text_test("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"30\" height=\"30\"><filter id=\"f\" x=\"0\" y=\"0\" width=\"1\" height=\"1\"><feFlood flood-color=\"blue\"/></filter><text x=\"5\" y=\"10\" font-size=\"10\" filter=\"url(#f)\">AB</text><x id=\"t\" xmlns=\"urn:x\">AB</x><text x=\"5\" y=\"25\" font-size=\"10\"><tref xlink:href=\"#t\"/></text></svg>", 30, 30)
defer free(pixels) in memory.heap
assert(rgba_at(pixels, 30, 6, 3)[2] == 255 and rgba_at(pixels, 30, 16, 11)[2] == 255)
assert(rgba_at(pixels, 30, 6, 1)[3] == 0 and rgba_at(pixels, 30, 18, 6)[3] == 0)
# The tref links to a foreign element: nothing is drawn.
assert(rgba_at(pixels, 30, 8, 22)[3] == 0)
## The box font with a shaper that sets "fi" as one glyph 900 units wide.
struct LigatureFont: GlyphSource:
var boxes: BoxFont = BoxFont()
func select_face(query: FaceQuery) -> u32?:
return self.boxes.select_face(query)
func fallback_face(face: u32, scalar: u32) -> u32?:
return none
func face_metrics(face: u32) -> FaceMetrics:
return FaceMetrics()
func glyph_index(face: u32, scalar: u32) -> u32:
return scalar
func glyph_advance(face: u32, glyph: u32) -> f64:
return 900.0 if glyph == 0xFB01 else 600.0
func glyph_kerning(face: u32, left: u32, right: u32) -> f64:
return 0.0
func shape(face: u32, scalars: const u32[], rtl: bool, kerning: bool, small_caps: bool, out: GlyphRecord[]) -> usize?!:
var count: usize = 0
var at: usize = 0
while at < scalars.length:
let ligature = scalars[at] == 0x66 and at + 1 < scalars.length and scalars[at + 1] == 0x69
if count < out.length:
out[count] = GlyphRecord(glyph = 0xFB01 if ligature else scalars[at], cluster = at, advance = 900.0 if ligature else 600.0)
count += 1
at += 2 if ligature else 1
return count
func glyph_outline(face: u32, glyph: u32, path: Path*) -> bool!:
try path.rect(0.0, 0.0, 900.0 if glyph == 0xFB01 else 600.0, 700.0)
return true
test "bidi runs come in visual order: numbers inside right-to-left text read left to right":
# "ab " Hebrew alef bet " 12 cd": runs [0,3) L, [6,8) numbers, [3,6) Hebrew, [8,11) L.
let scalars: u32[11] = [0x61, 0x62, 0x20, 0x05D0, 0x05D1, 0x20, 0x31, 0x32, 0x20, 0x63, 0x64]
var runs = List[BidiRun]()
defer runs.release()
try bidi_runs(scalars[0..<11], &runs)
assert(runs.used == 4)
assert(runs.items[0].start == 0 and runs.items[0].end == 3 and not runs.items[0].rtl())
assert(runs.items[1].start == 6 and runs.items[1].end == 8 and runs.items[1].level == 2)
assert(runs.items[2].start == 3 and runs.items[2].end == 6 and runs.items[2].rtl())
assert(runs.items[3].start == 8 and runs.items[3].end == 11)
test "a shaping source's ligature is one cluster":
var font = LigatureFont()
let picture = try load("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"30\" height=\"20\"><text x=\"0\" y=\"10\" font-size=\"10\">fix</text></svg>", Options(glyphs = &font))
defer picture.release()
let pixels = try new u8[30 * 20 * 4] --- in memory.heap
defer free(pixels) in memory.heap
try render(picture.value(), fit_transform(picture.value(), 30, 20), 30, 20, pixels)
# "fi" 9 px then "x" 6 px: ink ends at 15, not 18.
assert(rgba_at(pixels, 30, 14, 6)[3] == 255 and rgba_at(pixels, 30, 16, 6)[3] == 0)