Skip to content

Lightpanda: the headless browser designed for AI and automation

ops: add --render-font CLI option

Karl Seguin036e969258d1
diff --git a/src/App.zig b/src/App.zig
index 124e8e6bd..f5959a370 100644
--- a/src/App.zig
+++ b/src/App.zig
@@ -19,16 +19,18 @@
const std = @import("std");
const lp = @import("lightpanda");
-const Config = @import("Config.zig");
const Regex = @import("Regex.zig");
+const Config = @import("Config.zig");
+const Watchdog = @import("Watchdog.zig");
+pub const ArenaPool = @import("ArenaPool.zig");
+
const Snapshot = @import("browser/js/Snapshot.zig");
const Platform = @import("browser/js/Platform.zig");
-const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
+const screenshot = @import("browser/screenshot.zig");
+const Sanitizer = @import("browser/webapi/Sanitizer.zig");
const Network = @import("network/Network.zig");
-const Watchdog = @import("Watchdog.zig");
-const Sanitizer = @import("browser/webapi/Sanitizer.zig");
-pub const ArenaPool = @import("ArenaPool.zig");
+const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
const log = lp.log;
const Allocator = std.mem.Allocator;
@@ -47,6 +49,7 @@ app_dir_path: ?[]const u8,
regex_context: *Regex.Context,
default_sanitizer: *Sanitizer,
+render_fonts: screenshot.Fonts,
pub fn init(allocator: Allocator, config: *const Config) !*App {
const platform = try Platform.init(.{
@@ -62,6 +65,9 @@ pub fn init(allocator: Allocator, config: *const Config) !*App {
const regex_context: *Regex.Context = try .init(allocator);
errdefer regex_context.deinit();
+ var render_fonts: screenshot.Fonts = try .load(allocator, config.renderFonts());
+ errdefer render_fonts.deinit(allocator);
+
const app = try allocator.create(App);
errdefer allocator.destroy(app);
@@ -71,6 +77,7 @@ pub fn init(allocator: Allocator, config: *const Config) !*App {
.platform = platform,
.snapshot = snapshot,
.regex_context = regex_context,
+ .render_fonts = render_fonts,
.network = undefined,
.app_dir_path = undefined,
.telemetry = undefined,
@@ -113,6 +120,7 @@ pub fn deinit(self: *App) void {
self.snapshot.deinit();
self.platform.deinit();
self.default_sanitizer.deinitDefault();
+ self.render_fonts.deinit(allocator);
self.arena_pool.deinit();
allocator.destroy(self);
diff --git a/src/Config.zig b/src/Config.zig
index 21dc0c5fb..0940c5e04 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -272,6 +272,7 @@ const CommonOptions = .{
.{ .name = "user_agent_suffix", .type = ?[]const u8 },
.{ .name = "http_cache_dir", .type = ?[]const u8 },
.{ .name = "http_cache_entry_limit", .type = ?u32, .default = 1000 },
+ .{ .name = "render_font", .type = []const u8, .multiple = true },
.{ .name = "web_bot_auth_key_file", .type = ?[]const u8 },
.{ .name = "web_bot_auth_keyid", .type = ?[]const u8 },
.{ .name = "web_bot_auth_domain", .type = ?[]const u8 },
@@ -774,6 +775,13 @@ pub fn httpCacheDir(self: *const Config) ?[]const u8 {
};
}
+pub fn renderFonts(self: *const Config) []const []const u8 {
+ return switch (self.mode) {
+ inline .serve, .fetch, .mcp, .agent => |opts| opts.render_font.items,
+ else => &.{},
+ };
+}
+
pub fn httpCacheEntryLimit(self: *const Config) u32 {
return switch (self.mode) {
inline .serve, .fetch, .mcp, .agent => |opts| opts.http_cache_entry_limit.?,
diff --git a/src/browser/pdf.zig b/src/browser/pdf.zig
index 8371bd317..17b902809 100644
--- a/src/browser/pdf.zig
+++ b/src/browser/pdf.zig
@@ -203,6 +203,9 @@ const PT: f32 = 0.75;
/// Prefix marking an embedded font as a subset.
const SUBSET_TAG = "LPSUBS";
+/// The layout's first font is always the bundled DejaVu Sans.
+const TOFU_FONT = 0;
+
const Document = struct {
arena: Allocator,
layout: *const screenshot.Layout,
@@ -216,8 +219,11 @@ const Document = struct {
/// Per page: content stream body and link annotation dictionaries.
pages: []std.Io.Writer.Allocating,
annots: []std.ArrayList([]const u8),
- /// One per layout font, filled in as glyphs are drawn.
- fonts: []FontUse,
+ /// One per layout font, filled in as glyphs are drawn. Null for a
+ /// --render-font face we can't embed (CFF, collections): its runs are
+ /// drawn as TOFU_FONT's missing-glyph box, the real text kept as
+ /// ActualText.
+ fonts: []?FontUse,
/// Per block: its span texts concatenated (what the clusters index).
texts: [][]const u8,
@@ -247,9 +253,14 @@ const Document = struct {
a.* = .empty;
}
- const fonts = try arena.alloc(FontUse, layout.fonts().len);
- for (fonts, layout.fonts()) |*f, info| {
- f.* = try .init(info);
+ const fonts = try arena.alloc(?FontUse, layout.fonts().len);
+ for (fonts, layout.fonts(), 0..) |*f, info, i| {
+ f.* = FontUse.init(info) catch |err| blk: {
+ // The bundled face (always at TOFU_FONT) must parse.
+ if (i == TOFU_FONT) return err;
+ log.warn(.browser, "pdf font not embeddable", .{ .font = info.name[0..info.name_len], .err = err });
+ break :blk null;
+ };
}
const texts = try arena.alloc([]const u8, blocks.len);
@@ -330,9 +341,10 @@ const Document = struct {
const shift = self.breaks[pg];
const w = &self.pages[pg].writer;
if (li == 0 and b.marker_line != screenshot.LAYOUT_NONE) {
- try self.drawLine(w, lines[b.marker_line], shift);
+ const block = self.blocks[bi];
+ try self.drawLine(w, lines[b.marker_line], block.marker[0..block.marker_len], shift);
}
- try self.drawLine(w, line, shift);
+ try self.drawLine(w, line, self.texts[bi], shift);
if (links.len > 0) {
try self.linkAnnotations(pg, line, shift, links);
}
@@ -383,9 +395,10 @@ const Document = struct {
return links.items;
}
- fn drawLine(self: *Document, w: *std.Io.Writer, line: screenshot.LpLine, shift: f32) !void {
+ /// `text` is what the line's clusters index.
+ fn drawLine(self: *Document, w: *std.Io.Writer, line: screenshot.LpLine, text: []const u8, shift: f32) !void {
for (self.layout.runs()[line.runs..][0..line.runs_len]) |run| {
- try self.drawRun(w, run, line.x, shift);
+ try self.drawRun(w, run, line, text, shift);
}
}
@@ -393,11 +406,19 @@ const Document = struct {
/// synthetic italic skew) in the text matrix, glyph ids as 2-byte CIDs,
/// and an adjustment wherever the layout's pen differs from the font's
/// advance (kerning, marks, visual order). Same transform as the raster.
- fn drawRun(self: *Document, w: *std.Io.Writer, run: screenshot.LpRun, line_x: f32, shift: f32) !void {
- const font = &self.fonts[run.font];
+ fn drawRun(self: *Document, w: *std.Io.Writer, run: screenshot.LpRun, line: screenshot.LpLine, text: []const u8, shift: f32) !void {
+ const line_x = line.x;
const size = run.size;
const baseline = run.baseline - shift;
- try w.print("BT /F{d} 1 Tf ", .{run.font});
+ const tofu = self.fonts[run.font] == null;
+ const font_id = if (tofu) TOFU_FONT else run.font;
+ const font = &self.fonts[font_id].?;
+ if (tofu) {
+ try w.writeAll("/Span << /ActualText <FEFF");
+ try utf16Hex(w, try self.runText(run, line, text));
+ try w.writeAll("> >> BDC\n");
+ }
+ try w.print("BT /F{d} 1 Tf ", .{font_id});
try rgb(w, run.color);
try w.writeAll(" rg\n");
var pen = line_x + run.offset;
@@ -417,13 +438,14 @@ const Document = struct {
const adj = (expected - gx) * 1000 / size;
if (@abs(adj) >= 0.5) try w.print("> {d:.1} <", .{adj});
}
- const gid: u16 = @intCast(g.id);
+ const gid: u16 = if (tofu) 0 else @intCast(g.id);
const width = try font.use(self.arena, gid);
try w.print("{X:0>4}", .{gid});
expected = gx + width * size / 1000;
}
if (cur_y != null) try w.writeAll(">] TJ\n");
try w.writeAll("ET\n");
+ if (tofu) try w.writeAll("EMC\n");
const x0 = line_x + run.offset;
for ([_]screenshot.LpDecoration{ run.underline, run.strike }) |d| {
@@ -432,6 +454,28 @@ const Document = struct {
}
}
+ /// The text a run was shaped from, in logical order: the line's clusters
+ /// in that face within the run's span.
+ fn runText(self: *Document, run: screenshot.LpRun, line: screenshot.LpLine, text: []const u8) ![]const u8 {
+ var picked: std.ArrayList(screenshot.LpCluster) = .empty;
+ for (self.layout.clusters()[line.clusters..][0..line.clusters_len]) |c| {
+ if (c.font != run.font) continue;
+ if (c.x < run.offset - 0.01 or c.x >= run.offset + run.advance - 0.01) continue;
+ try picked.append(self.arena, c);
+ }
+ std.mem.sort(screenshot.LpCluster, picked.items, {}, struct {
+ fn lt(_: void, a: screenshot.LpCluster, b: screenshot.LpCluster) bool {
+ return a.text_start < b.text_start;
+ }
+ }.lt);
+ var out: std.ArrayList(u8) = .empty;
+ for (picked.items) |c| {
+ if (c.text_start + c.text_len > text.len) continue;
+ try out.appendSlice(self.arena, text[c.text_start..][0..c.text_len]);
+ }
+ return out.items;
+ }
+
/// One URI annotation per line per link: clusters in visual order,
/// contiguous runs of the same link merged into a box.
fn linkAnnotations(self: *Document, pg: usize, line: screenshot.LpLine, shift: f32, links: []const Link) !void {
@@ -484,7 +528,7 @@ const Document = struct {
continue;
}
if (ligature) |l| {
- try self.fonts[l.font].mapText(self.arena, l.gid, l.text.items);
+ if (self.fonts[l.font]) |*f| try f.mapText(self.arena, l.gid, l.text.items);
ligature = null;
}
if (c.glyph == screenshot.LAYOUT_NONE or t.len == 0) continue;
@@ -494,10 +538,12 @@ const Document = struct {
try buf.appendSlice(self.arena, t);
ligature = .{ .font = c.font, .gid = gid, .text = buf };
} else {
- try self.fonts[c.font].mapText(self.arena, gid, t);
+ if (self.fonts[c.font]) |*f| try f.mapText(self.arena, gid, t);
}
}
- if (ligature) |l| try self.fonts[l.font].mapText(self.arena, l.gid, l.text.items);
+ if (ligature) |l| {
+ if (self.fonts[l.font]) |*f| try f.mapText(self.arena, l.gid, l.text.items);
+ }
}
// File structure: header, catalog, then each page's annotations,
@@ -554,7 +600,8 @@ const Document = struct {
}
var font_refs: std.Io.Writer.Allocating = .init(self.arena);
- for (self.fonts, 0..) |*font, i| {
+ for (self.fonts, 0..) |*slot, i| {
+ const font = if (slot.*) |*f| f else continue;
if (font.used.count() == 0) continue;
const id = try font.writeObjects(self.arena, &out);
try font_refs.writer.print("/F{d} {d} 0 R ", .{ i, id });
@@ -616,6 +663,19 @@ fn rect(w: *std.Io.Writer, color: u32, x: f32, y: f32, width: f32, height: f32)
}
/// "r g b" in 0..1 for a 0xRRGGBB.
+/// UTF-8 as UTF-16BE hex digits, the body of a PDF hex string.
+fn utf16Hex(w: *std.Io.Writer, utf8: []const u8) !void {
+ var it = std.unicode.Utf8View.initUnchecked(utf8).iterator();
+ while (it.nextCodepoint()) |cp| {
+ if (cp < 0x10000) {
+ try w.print("{X:0>4}", .{@as(u16, @intCast(cp))});
+ } else {
+ const c = cp - 0x10000;
+ try w.print("{X:0>4}{X:0>4}", .{ @as(u16, @intCast(0xD800 + (c >> 10))), @as(u16, @intCast(0xDC00 + (c & 0x3FF))) });
+ }
+ }
+}
+
fn rgb(w: *std.Io.Writer, color: u32) !void {
try w.print("{d:.3} {d:.3} {d:.3}", .{
@as(f32, @floatFromInt((color >> 16) & 0xff)) / 255,
@@ -744,16 +804,7 @@ const FontUse = struct {
try w.print("{d} beginbfchar\n", .{chunk.len});
for (chunk) |gid| {
try w.print("<{X:0>4}> <", .{gid});
- // UTF-16BE.
- var it = std.unicode.Utf8View.initUnchecked(self.unicode.get(gid).?).iterator();
- while (it.nextCodepoint()) |cp| {
- if (cp < 0x10000) {
- try w.print("{X:0>4}", .{@as(u16, @intCast(cp))});
- } else {
- const c = cp - 0x10000;
- try w.print("{X:0>4}{X:0>4}", .{ @as(u16, @intCast(0xD800 + (c >> 10))), @as(u16, @intCast(0xDC00 + (c & 0x3FF))) });
- }
- }
+ try utf16Hex(w, self.unicode.get(gid).?);
try w.writeAll(">\n");
}
try w.writeAll("endbfchar\n");
@@ -1114,6 +1165,63 @@ test "browser.pdf: rejects bad options" {
try testing.expectError(error.WriteFailed, print(a, .{ .root = div.asNode() }, .{}, &fixed, frame));
}
+test "browser.pdf: --render-font faces" {
+ defer testing.test_session.closeAllPages();
+ testing.silenceLog(&.{.browser});
+ const frame = try testing.createFrame();
+ const doc = frame.window._document;
+ const div = try doc.createElement("div", null, frame);
+ try Frame.parse.htmlAsChildren(frame, div.asNode(), "<p>A日</p>");
+
+ const browser = frame._session.browser;
+ const saved = browser.renderer;
+ defer browser.renderer = saved;
+
+ const font = try screenshot.testFallbackFont(testing.arena_allocator);
+ var aw: std.Io.Writer.Allocating = .init(testing.arena_allocator);
+ {
+ // TrueType: embedded and subset next to DejaVu, under its own name.
+ const r = try screenshot.Renderer.init(&.{.{ .data = font.ptr, .data_len = font.len }});
+ defer r.deinit();
+ browser.renderer = r;
+ try print(testing.arena_allocator, .{ .root = div.asNode() }, .{}, &aw.writer, frame);
+ try testing.expectEqual(2, std.mem.count(u8, aw.written(), "/FontFile2"));
+ try testing.expectEqual(true, std.mem.find(u8, aw.written(), "/BaseFont /" ++ SUBSET_TAG ++ "+LPTest-Regular") != null);
+ }
+ {
+ // Labelled CFF: parley still draws it, but it can't be embedded, so
+ // its run is DejaVu's missing-glyph box carrying the real text.
+ const cff = try testing.arena_allocator.dupe(u8, font);
+ @memcpy(cff[0..4], "OTTO");
+ const r = try screenshot.Renderer.init(&.{.{ .data = cff.ptr, .data_len = cff.len }});
+ defer r.deinit();
+ browser.renderer = r;
+ aw.clearRetainingCapacity();
+ try print(testing.arena_allocator, .{ .root = div.asNode() }, .{}, &aw.writer, frame);
+ try testing.expectEqual(1, std.mem.count(u8, aw.written(), "/FontFile2"));
+ try testing.expectEqual(null, std.mem.find(u8, aw.written(), "LPTest"));
+ const content = try testContent(aw.written());
+ try testing.expectEqual(1, std.mem.count(u8, content, "/Span << /ActualText <FEFF65E5> >> BDC\nBT /F0 1 Tf"));
+ try testing.expectEqual(true, std.mem.find(u8, content, "[<0000>] TJ\nET\nEMC\n") != null);
+ }
+}
+
+/// Every deflated stream in `pdf`, inflated and concatenated.
+fn testContent(pdf: []const u8) ![]const u8 {
+ const arena = testing.arena_allocator;
+ var out: std.Io.Writer.Allocating = .init(arena);
+ var rest = pdf;
+ while (std.mem.find(u8, rest, "stream\n")) |start| {
+ const body = rest[start + 7 ..];
+ const end = std.mem.find(u8, body, "endstream") orelse break;
+ rest = body[end..];
+ var in: std.Io.Reader = .fixed(body[0..end]);
+ var d: flate.Decompress = .init(&in, .zlib, &.{});
+ _ = d.reader.streamRemaining(&out.writer) catch continue;
+ }
+ return out.written();
+}
+
test "browser.pdf: parsePageRanges follows the CDP grammar" {
const a = testing.arena_allocator;
const r = try parsePageRanges(a, "1-5, 8 ,11-13,,");
diff --git a/src/browser/screenshot.zig b/src/browser/screenshot.zig
index 01dff71e1..532222e53 100644
--- a/src/browser/screenshot.zig
+++ b/src/browser/screenshot.zig
@@ -58,9 +58,69 @@ pub const Opts = struct {
}
};
+// The --render-font files, mapped read-only for the life of the App.
+pub const Fonts = struct {
+ maps: []std.Io.File.MemoryMap,
+ files: []LpFontFile,
+
+ pub fn load(allocator: Allocator, paths: []const []const u8) !Fonts {
+ if (paths.len == 0) {
+ return .{ .maps = &.{}, .files = &.{} };
+ }
+ const maps = try allocator.alloc(std.Io.File.MemoryMap, paths.len);
+ errdefer allocator.free(maps);
+
+ const files = try allocator.alloc(LpFontFile, paths.len);
+ errdefer allocator.free(files);
+
+ var loaded: usize = 0;
+ errdefer for (maps[0..loaded]) |*m| unmap(m);
+
+ for (paths, maps, files) |path, *m, *f| {
+ m.* = mapFile(path) catch |err| {
+ log.fatal(.app, "failed to load font", .{ .arg = "--render-font", .path = path, .err = err });
+ return error.InvalidArgument;
+ };
+ loaded += 1;
+ f.* = .{ .data = m.memory.ptr, .data_len = m.memory.len };
+ }
+ return .{ .maps = maps, .files = files };
+ }
+
+ pub fn deinit(self: *Fonts, allocator: Allocator) void {
+ for (self.maps) |*m| unmap(m);
+ allocator.free(self.maps);
+ allocator.free(self.files);
+ }
+
+ fn mapFile(path: []const u8) !std.Io.File.MemoryMap {
+ const file = try std.Io.Dir.cwd().openFile(lp.io, path, .{});
+ errdefer file.close(lp.io);
+ const len = try file.length(lp.io);
+ if (len == 0) {
+ return error.EmptyFile;
+ }
+ return file.createMemoryMap(lp.io, .{
+ .len = @intCast(len),
+ .populate = false,
+ .protection = .{ .read = true },
+ });
+ }
+
+ fn unmap(m: *std.Io.File.MemoryMap) void {
+ const file = m.file;
+ m.destroy(lp.io);
+ file.close(lp.io);
+ }
+};
+
// Parsed fonts, shaping scratch and the glyph cache, on the Rust side. One
// per Browser, created on the first screenshot.
pub const Renderer = opaque {
+ pub fn init(fonts: []const LpFontFile) !*Renderer {
+ return lp_render_new(fonts.ptr, fonts.len) orelse error.RendererInit;
+ }
+
pub fn deinit(self: *Renderer) void {
lp_render_free(self);
}
@@ -111,7 +171,7 @@ pub fn rendererFor(frame: *Frame) !*Renderer {
if (browser.renderer) |r| {
return r;
}
- const r = lp_render_new() orelse return error.RendererInit;
+ const r = try Renderer.init(browser.app.render_fonts.files);
browser.renderer = r;
return r;
}
@@ -372,7 +432,7 @@ const LpGlyph = extern struct {
advance: f32,
};
-const LpCluster = extern struct {
+pub const LpCluster = extern struct {
font: u32,
glyph: u32,
text_start: u32,
@@ -394,6 +454,12 @@ pub const LpFont = extern struct {
mono: u8,
};
+/// A --render-font file; the bytes outlive every Renderer.
+pub const LpFontFile = extern struct {
+ data: [*]const u8,
+ data_len: usize,
+};
+
const RC_OK: i32 = 0;
const RC_WRITE_REFUSED: i32 = 1;
const RC_INVALID: i32 = 2;
@@ -545,7 +611,7 @@ const LpAbi = extern struct {
const LINK_COLOR: u32 = 0x1a0dab;
const MUTED_COLOR: u32 = 0x6b6b6b;
-extern "c" fn lp_render_new() ?*Renderer;
+extern "c" fn lp_render_new(fonts: [*]const LpFontFile, fonts_len: usize) ?*Renderer;
extern "c" fn lp_render_free(r: *Renderer) void;
extern "c" fn lp_render_png(
r: *Renderer,
@@ -1609,3 +1675,170 @@ test "browser.screenshot: deep nesting doesn't overflow the native stack" {
try testing.expectEqual(1, blocks[0].spans_len);
try testing.expectEqual("deep", blocks[0].spans[0].text[0..blocks[0].spans[0].len]);
}
+
+test "browser.screenshot: --render-font faces fill what DejaVu lacks" {
+ defer testing.test_session.closeAllPages();
+ const frame = try testing.createFrame();
+ const doc = frame.window._document;
+ const div = try doc.createElement("div", null, frame);
+ try Frame.parse.htmlAsChildren(frame, div.asNode(), "<p>A日</p>");
+
+ const font = try testFallbackFont(testing.arena_allocator);
+ const files = [_]LpFontFile{.{ .data = font.ptr, .data_len = font.len }};
+ const r = try Renderer.init(&files);
+ defer r.deinit();
+
+ const blocks = try collect(testing.arena_allocator, .{ .root = div.asNode() }, frame);
+ var l = try layout(r, blocks, 400, 0);
+ defer l.deinit();
+
+ // "A" stays DejaVu; 日 goes to the fallback, named for the PDF.
+ const clusters = l.clusters();
+ try testing.expectEqual(2, clusters.len);
+ const fonts = l.fonts();
+ const dejavu = fonts[clusters[0].font];
+ try testing.expectEqual("DejaVuSans", dejavu.name[0..dejavu.name_len]);
+ const fallback = fonts[clusters[1].font];
+ try testing.expectEqual("LPTest-Regular", fallback.name[0..fallback.name_len]);
+ try testing.expectEqual(@intFromPtr(font.ptr), @intFromPtr(fallback.data));
+ try testing.expectEqual(1, clusters[1].glyph);
+}
+
+test "browser.screenshot: Fonts maps --render-font files" {
+ testing.silenceLog(&.{.app});
+ try testing.expectError(error.InvalidArgument, Fonts.load(testing.allocator, &.{"src/rust/render/fonts/missing.ttf"}));
+
+ var fonts: Fonts = try .load(testing.allocator, &.{ "src/rust/render/fonts/DejaVuSans.ttf", "src/rust/render/fonts/DejaVuSansMono.ttf" });
+ defer fonts.deinit(testing.allocator);
+ try testing.expectEqual(2, fonts.files.len);
+ const data = fonts.files[0].data[0..fonts.files[0].data_len];
+ try testing.expectEqual(759720, data.len);
+ try testing.expectEqual(0x00010000, std.mem.readInt(u32, data[0..4], .big));
+}
+
+/// A minimal TrueType font: one square glyph, mapped from U+65E5 (日),
+/// which DejaVu doesn't cover. Family "LPTest", PostScript "LPTest-Regular".
+pub fn testFallbackFont(arena: Allocator) ![]const u8 {
+ const Table = struct { tag: *const [4]u8, data: []const u8 };
+ const B = struct {
+ fn build(a: Allocator, comptime f: fn (*std.Io.Writer) anyerror!void) ![]const u8 {
+ var aw: std.Io.Writer.Allocating = .init(a);
+ try f(&aw.writer);
+ return aw.written();
+ }
+ fn u16s(w: *std.Io.Writer, values: []const u16) !void {
+ for (values) |v| try w.writeInt(u16, v, .big);
+ }
+ };
+
+ const os2 = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ // version 4: avg width, weight 400, width 5, then zeros up to the
+ // typo/win metrics and the cap height.
+ try B.u16s(w, &.{ 4, 1000, 400, 5 });
+ try w.splatByteAll(0, 68 - 8);
+ try B.u16s(w, &.{ 800, @bitCast(@as(i16, -200)), 0, 800, 200 });
+ try w.splatByteAll(0, 88 - 78);
+ try B.u16s(w, &.{ 800, 0, 0, 0 });
+ }
+ }.f);
+ const cmap = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ // One (3, 1) format 4 subtable: 0x65E5 → gid 1, then the 0xFFFF
+ // terminator segment.
+ try B.u16s(w, &.{ 0, 1, 3, 1, 0, 12 });
+ try B.u16s(w, &.{ 4, 32, 0, 4, 4, 1, 0 });
+ try B.u16s(w, &.{ 0x65E5, 0xFFFF, 0, 0x65E5, 0xFFFF });
+ try B.u16s(w, &.{ 1 -% @as(u16, 0x65E5), 1, 0, 0 });
+ }
+ }.f);
+ const glyf = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ // gid 0 is empty; gid 1 a 800x800 square, four on-curve points
+ // as 16-bit deltas, padded to 36.
+ try B.u16s(w, &.{ 1, 100, 0, 900, 800, 3, 0 });
+ try w.splatByteAll(1, 4);
+ try B.u16s(w, &.{ 100, 0, 800, 0 });
+ try B.u16s(w, &.{ 0, 800, 0, @bitCast(@as(i16, -800)) });
+ try w.splatByteAll(0, 2);
+ }
+ }.f);
+ const head = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ try B.u16s(w, &.{ 1, 0, 1, 0, 0, 0, 0x5F0F, 0x3CF5, 0x000B, 1000 });
+ try w.splatByteAll(0, 16);
+ // bbox, macStyle, lowestRecPPEM, direction hint, long loca, format
+ try B.u16s(w, &.{ 0, 0, 1000, 800, 0, 8, 2, 1, 0 });
+ }
+ }.f);
+ const hhea = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ try B.u16s(w, &.{ 1, 0, 800, @bitCast(@as(i16, -200)), 0, 1000, 0, 0, 1000, 1, 0, 0 });
+ try w.splatByteAll(0, 10);
+ try B.u16s(w, &.{2});
+ }
+ }.f);
+ const hmtx = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ try B.u16s(w, &.{ 500, 0, 1000, 100 });
+ }
+ }.f);
+ const loca = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ for ([_]u32{ 0, 0, 36 }) |o| try w.writeInt(u32, o, .big);
+ }
+ }.f);
+ const maxp = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ try B.u16s(w, &.{ 1, 0, 2, 4, 1, 0, 0, 2 });
+ try w.splatByteAll(0, 16);
+ }
+ }.f);
+ const name = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ // Family (1) and PostScript (6) names, UTF-16BE.
+ const family = "LPTest";
+ const ps = "LPTest-Regular";
+ try B.u16s(w, &.{ 0, 2, 6 + 2 * 12 });
+ try B.u16s(w, &.{ 3, 1, 0x409, 1, family.len * 2, 0 });
+ try B.u16s(w, &.{ 3, 1, 0x409, 6, ps.len * 2, family.len * 2 });
+ for (family ++ ps) |c| try w.writeInt(u16, c, .big);
+ }
+ }.f);
+ const post = try B.build(arena, struct {
+ fn f(w: *std.Io.Writer) !void {
+ try B.u16s(w, &.{ 3, 0, 0, 0, @bitCast(@as(i16, -100)), 50 });
+ try w.splatByteAll(0, 20);
+ }
+ }.f);
+
+ // Sorted by tag, as the directory requires.
+ const tables = [_]Table{
+ .{ .tag = "OS/2", .data = os2 },
+ .{ .tag = "cmap", .data = cmap },
+ .{ .tag = "glyf", .data = glyf },
+ .{ .tag = "head", .data = head },
+ .{ .tag = "hhea", .data = hhea },
+ .{ .tag = "hmtx", .data = hmtx },
+ .{ .tag = "loca", .data = loca },
+ .{ .tag = "maxp", .data = maxp },
+ .{ .tag = "name", .data = name },
+ .{ .tag = "post", .data = post },
+ };
+ var aw: std.Io.Writer.Allocating = .init(arena);
+ const w = &aw.writer;
+ try B.u16s(w, &.{ 1, 0, tables.len, 128, 3, tables.len * 16 - 128 });
+ var offset: u32 = 12 + tables.len * 16;
+ for (tables) |t| {
+ try w.writeAll(t.tag);
+ try w.writeInt(u32, 0, .big);
+ try w.writeInt(u32, offset, .big);
+ try w.writeInt(u32, @intCast(t.data.len), .big);
+ offset += @intCast(std.mem.alignForward(usize, t.data.len, 4));
+ }
+ for (tables) |t| {
+ try w.writeAll(t.data);
+ try w.splatByteAll(0, std.mem.alignForward(usize, t.data.len, 4) - t.data.len);
+ }
+ return aw.written();
+}
diff --git a/src/help.zon b/src/help.zon
index c0d7e2d74..4cfc99881 100644
--- a/src/help.zon
+++ b/src/help.zon
@@ -520,6 +520,11 @@
\\ --proxy-bearer-token <TOKEN>
\\ Token sent for bearer authentication with the proxy:
\\ Proxy-Authorization: Bearer <token>.
+ \\ --render-font <PATH>
+ \\ Font file (TrueType, OpenType or collection) used by the png and pdf
+ \\ dumps, and by screenshots, for characters the bundled DejaVu fonts
+ \\ lack, e.g. CJK. Can be passed multiple times; earlier files win.
+ \\ e.g. --render-font NotoSansCJK-Regular.ttc
\\ --robot-store-entry-limit <INT>
\\ Maximum number of entries kept in the RobotStore. 0 means no limit.
\\ Defaults to 1000.
diff --git a/src/rust/render/lib.rs b/src/rust/render/lib.rs
index ce4eba1c3..005b10f47 100644
--- a/src/rust/render/lib.rs
+++ b/src/rust/render/lib.rs
@@ -20,8 +20,7 @@
//! `--dump png`. Lightpanda has no layout engine, so a "screenshot" is the
//! page's text content flowed into blocks — the same content the markdown
//! dump produces — set with parley (line breaking, shaping, bidi), outlined
-//! with skrifa and filled by tiny-skia. Fonts are bundled so output doesn't
-//! depend on the host.
+//! with skrifa and filled by tiny-skia.
use std::borrow::Cow;
use std::collections::HashMap;
@@ -35,6 +34,8 @@ use parley::{
};
use skrifa::instance::{LocationRef, Size};
use skrifa::outline::{DrawSettings, OutlinePen};
+use skrifa::raw::TableProvider;
+use skrifa::string::StringId;
use skrifa::{GlyphId, MetadataProvider};
use tiny_skia::{Color, FillRule, IntRect, Paint, PathBuilder, Pixmap, Rect, Transform};
@@ -88,6 +89,14 @@ pub struct LpBlock {
pub flags: u8,
}
+/// The bytes belong to the caller and must outlive the renderer and every
+/// layout it hands out.
+#[repr(C)]
+pub struct LpFontFile {
+ pub data: *const u8,
+ pub data_len: usize,
+}
+
#[repr(C)]
#[derive(Clone, Copy)]
pub struct LpRenderOpts {
@@ -129,6 +138,8 @@ pub struct LpLayout {
pub glyphs_len: usize,
pub clusters: *const LpCluster,
pub clusters_len: usize,
+ /// Always starts with DejaVu Sans, used or not: pdf.zig draws the
+ /// glyphs of a face it can't embed as that font's missing-glyph box.
pub fonts: *const LpFont,
pub fonts_len: usize,
}
@@ -227,8 +238,8 @@ pub struct LpCluster {
pub const CLUSTER_LIGATURE_START: u32 = 1 << 0;
pub const CLUSTER_LIGATURE_CONT: u32 = 1 << 1;
-/// A face the layout used: the raw font file (static, outlives the handle)
-/// and how to name it.
+/// A face the layout used: the raw font file (outlives the handle) and how to
+/// name it.
#[repr(C)]
pub struct LpFont {
pub data: *const u8,
@@ -541,10 +552,23 @@ pub unsafe extern "C" fn lp_render_abi(out: *mut LpAbi) {
/// A renderer: parsed fonts, shaping scratch and the glyph cache. Not
/// thread-safe; the caller drives each handle from one thread at a time.
-/// Null on panic (font registration is the only thing in there that could).
+/// `fonts` are fallbacks tried in order.
#[no_mangle]
-pub extern "C" fn lp_render_new() -> *mut Renderer {
- match std::panic::catch_unwind(Renderer::new) {
+pub unsafe extern "C" fn lp_render_new(
+ fonts: *const LpFontFile,
+ fonts_len: usize,
+) -> *mut Renderer {
+ let fonts = if fonts_len == 0 {
+ &[][..]
+ } else {
+ std::slice::from_raw_parts(fonts, fonts_len)
+ };
+ // 'static is the caller's promise above: the bytes outlive the renderer.
+ let files: Vec<&'static [u8]> = fonts
+ .iter()
+ .map(|f| std::slice::from_raw_parts(f.data, f.data_len))
+ .collect();
+ match std::panic::catch_unwind(|| Renderer::new(&files)) {
Ok(r) => Box::into_raw(Box::new(r)),
Err(_) => std::ptr::null_mut(),
}
@@ -636,7 +660,7 @@ pub unsafe extern "C" fn lp_layout_new(
return std::ptr::null_mut();
};
let (placed, height) = (*r).layout(&doc, width, margin, 1.0);
- let handle = Box::new(LayoutHandle::export(&placed, height));
+ let handle = Box::new(LayoutHandle::export(&placed, height, &(*r).fallbacks));
*out = handle.view();
Box::into_raw(handle)
}));
@@ -682,7 +706,7 @@ impl LayoutHandle {
}
}
- fn export(placed: &[Placed], height: f32) -> LayoutHandle {
+ fn export(placed: &[Placed], height: f32, fallbacks: &[FallbackFace]) -> LayoutHandle {
let mut h = LayoutHandle {
height,
blocks: Vec::with_capacity(placed.len()),
@@ -692,6 +716,15 @@ impl LayoutHandle {
clusters: Vec::new(),
fonts: Vec::new(),
};
+ let (data, name, mono) = FONTS[0];
+ h.fonts.push(LpFont {
+ data: data.as_ptr(),
+ data_len: data.len(),
+ index: 0,
+ name: name.as_ptr(),
+ name_len: name.len(),
+ mono: mono as u8,
+ });
for p in placed {
let lines = h.lines.len() as u32;
if p.kind != BLOCK_RULE {
@@ -717,6 +750,17 @@ impl LayoutHandle {
quote_bars: p.quote_bars,
});
}
+ // font_index only knows the bundled faces by name.
+ for f in &mut h.fonts {
+ if let Some(fb) = fallbacks
+ .iter()
+ .find(|fb| fb.data == f.data && fb.index == f.index)
+ {
+ f.name = fb.name.as_ptr();
+ f.name_len = fb.name.len();
+ f.mono = fb.mono as u8;
+ }
+ }
h
}
@@ -954,10 +998,51 @@ type GlyphKey = (usize, u32);
pub struct Renderer {
fcx: FontContext,
+ fallbacks: Vec<FallbackFace>,
lcx: LayoutContext<Rgb>,
glyph_cache: HashMap<GlyphKey, Option<tiny_skia::Path>>,
}
+/// A face from a --render-font file
+struct FallbackFace {
+ data: *const u8,
+ index: u32,
+ name: String,
+ mono: bool,
+}
+
+impl FallbackFace {
+ fn new(data: &[u8], index: u32) -> Self {
+ let font = skrifa::FontRef::from_index(data, index).ok();
+ // PostScript names are already valid PDF names; filter anyway, the
+ // file is user input.
+ let mut name: String = font
+ .as_ref()
+ .and_then(|f| {
+ f.localized_strings(StringId::POSTSCRIPT_NAME)
+ .english_or_first()
+ })
+ .map(|n| {
+ n.chars()
+ .filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
+ .collect()
+ })
+ .unwrap_or_default();
+ if name.is_empty() {
+ name = format!("Fallback{index}");
+ }
+ let mono = font
+ .and_then(|f| f.post().ok())
+ .is_some_and(|p| p.is_fixed_pitch() != 0);
+ Self {
+ data: data.as_ptr(),
+ index,
+ name,
+ mono,
+ }
+ }
+}
+
/// A laid-out block. Positions are layout px from the top-left of the
/// column; `layout` itself is in device px (layout px times the scale it
/// was built with).
@@ -1023,7 +1108,7 @@ fn block_metrics(b: &Block) -> (f32, FontWeight, f32, f32, f32) {
}
impl Renderer {
- fn new() -> Self {
+ fn new(files: &[&'static [u8]]) -> Self {
let mut collection = Collection::new(CollectionOptions {
shared: false,
system_fonts: false,
@@ -1032,6 +1117,27 @@ impl Renderer {
Self::register(&mut collection, FONTS[1].0);
let mono = Self::register(&mut collection, FONTS[2].0);
Self::register(&mut collection, FONTS[3].0);
+
+ // Families in file order, faces of a collection (.ttc) in theirs.
+ // parley picks per cluster the first family in the list that maps
+ // the character, so DejaVu keeps everything it covers.
+ let mut families: Vec<fontique::FamilyId> = Vec::new();
+ let mut fallbacks = Vec::new();
+ for &data in files {
+ let blob = Blob::new(std::sync::Arc::new(data));
+ let mut registered = collection.register_fonts(blob, None);
+ // Not returned in file order; a collection's first face leads.
+ registered.sort_by_key(|(_, faces)| faces.iter().map(|f| f.index()).min());
+ for (family, faces) in registered {
+ if !families.contains(&family) {
+ families.push(family);
+ }
+ for face in faces {
+ fallbacks.push(FallbackFace::new(data, face.index()));
+ }
+ }
+ }
+
for generic in [
GenericFamily::SansSerif,
GenericFamily::Serif,
@@ -1039,14 +1145,21 @@ impl Renderer {
GenericFamily::Cursive,
GenericFamily::Fantasy,
] {
- collection.set_generic_families(generic, [sans].into_iter());
+ collection.set_generic_families(
+ generic,
+ std::iter::once(sans).chain(families.iter().copied()),
+ );
}
- collection.set_generic_families(GenericFamily::Monospace, [mono].into_iter());
+ collection.set_generic_families(
+ GenericFamily::Monospace,
+ std::iter::once(mono).chain(families.iter().copied()),
+ );
Self {
fcx: FontContext {
collection,
source_cache: SourceCache::default(),
},
+ fallbacks,
lcx: LayoutContext::new(),
glyph_cache: HashMap::new(),
}