Repository navigation
Description
What you want to do / 想做什么
Context
A sheet music app draws its pages on Canvas. Its tests compare what a page draws against golden images, per backend, and want to run those without opening a window: on a developer's machine while they work, and on CI runners with no display session. The app also exports a page as a PNG and makes thumbnails of scores in a library list, which is the same operation at run time.
Today
- A
Drawingbecomes pixels only inside a platform view.App.Capture()andScreenshotneednsgo test -target, which on macOS opens windows (and macOS: nsgo test screenshots (cacheDisplayInRect:) leave out shadows, per-corner radii, antialiasing, animations, backdrops and the caret #311 lists whatcacheDisplayInRect:leaves out of them). - The players already exist and take a context, not a view:
apple.Play(ctx uintptr, ops []driver.DrawOp, host CanvasHost)(internal/platform/apple/canvas.go:234) draws into any Core Graphics context, andcanvas2d.Player(internal/platform/canvas2d/canvas2d.go:24) into any 2D context. Both are underinternal/. nsgotest.TextEngine(e driver.TextEngine)(nsgotest/nsgotest.go:113) lets a headless test measure "with e instead of core.ApproxTextEngine". The real engines (apple.TextEngine,internal/platform/apple/text.go:636, and the others) are underinternal/too, so a test outside this module has nothing to pass: itsDrawing.TextMetricsand text frames are always the approximation's.- There is no API that turns a
Drawinginto an image at run time, so an app cannot export or thumbnail what it draws.
To measure NSGo's canvas for our evaluation we injected a 65-line package into the module at build time with go build -overlay, forwarding to apple.Play, apple.TextEngine and apple.RegisterFont, and played drawings into a CGBitmapContext. It worked first time (a 2,181-op page in about 4 ms on an M5 Pro), which says the pieces are there. It is not something an app or its tests should do.
Proposal
For tests, in nsgotest:
// PlatformTextEngine is the text engine of the machine the test runs on:
// CoreText on macOS, DirectWrite on Windows, Pango on Linux. It is nil
// where there is none, and a headless test then keeps the approximation.
//
// app := nsgotest.Start(t, root, nsgotest.TextEngine(nsgotest.PlatformTextEngine()))
func PlatformTextEngine() core.TextEngine
// RegisterFont registers font data with that engine for the process, as
// Scope.RegisterFont does on a platform.
func RegisterFont(data []byte) error
// Render plays d with this machine's canvas player into a bitmap of size
// points at scale pixels per point, with no window and no view. It
// reports ErrNoPlayer where the machine has none.
func Render(d *core.Drawing, size core.Size, scale float64) (*image.RGBA, error)For apps, a scope service, where a backend can do it:
// RenderDrawing plays d into an image of size points at scale pixels per
// point, off the screen, and calls done with it on the engine's goroutine.
// Env.CanRenderDrawings says whether the platform can.
func (cx *Scope) RenderDrawing(d *Drawing, size Size, scale float64, done func(image.Image, error))The first three are what our tests need. The service is the same function behind the protocol and can be a later step, or a separate issue if you prefer.
On the web, -target web already runs headless and Capture works there, so Render matters most on macOS, then Windows and Linux.
Acceptance
- On macOS with no window opened and no
-target: a plaingo testrenders eachinternal/platform/canvastestsample throughnsgotest.Renderto the pixels the Apple player's own tests expect. - A headless test started with
nsgotest.TextEngine(nsgotest.PlatformTextEngine())gets fromDrawing.TextMetricsand from aText's frame the same numbers the macOS target gives for the same font and string. -
nsgotest.RegisterFontmakes a font's family available to both, and a Private Use Area code point of it draws its glyph, not a fallback box. -
Renderat scale 3 of a drawing equals, pixel for pixel,Renderat scale 1 of the same drawing recorded 3 times as large. -
layering_test.gostill passes: the public functions live where the dependency graph allows. - If the service is taken:
RenderDrawingon macOS and iOS returns an image an app can encode withimage/png, for a drawing with text, paths, gradients and an image.
References
- NSGo's own
apple.Play,apple.DrawTopDownandapple.TextEngine: the request is to give them a public face, not to write them. - tdewolff/canvas,
renderers/rasterizer/(MIT; path checked to exist on 2026-11-16): a Go canvas API with a window-less rasterizer beside its other renderers, as an example of the shape (one drawing, several outputs).
Target platforms / 目标平台
macOS first; Windows, Linux; iOS and Android for the service.
App language / 应用语言
Go
Latest version / 最新版本
- I have checked that the latest NSGo release still lacks it.
NSGo version / NSGo 版本
Read at main 0665282e (2026-11-16); still so at 0150811c (2026-11-17).
Development machine / 开发环境
macOS 26.6 (Apple M5 Pro), Go 1.27.3, Command Line Tools without Xcode.
Target devices / 运行设备
The machine above, headless.
Activity
houko commentedon Nov 20, 2026
I audited #565 against the current architecture. Canvas players and platform text engines remain internal, while
nsgotestintentionally exposes only the approximate model headlessly. A complete implementation needs new public boundaries for platform engines, per-target bitmap contexts, font registration, image ownership and dependency-graph changes, plus macOS/Windows/Linux pixel tests. I left the issue open and did not expose an internal player through an incomplete public API.houko commentedon Nov 22, 2026
Audited the current implementation against this issue and opened PR #1871.
The PR adds macOS-only headless canvas rendering and native text helpers to
nsgotest:PlatformTextEngine,RegisterFont, andRender. It reuses the existing Apple CoreText/Core Graphics players without opening a window, returns owned transparent RGBA pixels, supports nested offscreen drawings and synchronous image loading, and covers scale, alpha, concurrency, invalid sources, cycles, font registration and native metrics with tests.The remaining Windows/Linux native helpers and a runtime
RenderDrawingservice on device targets remain roadmap work, so this issue stays open for those slices.Verification: 163
nsgotesttests passed with one existing skip; five new windowless tests and three existing Apple canvas/font regression groups passed. All dependency-layer variants and skill checks passed. The completego tool nsgo check -target all -jobs 2passed using a task-owned cache. The new helpers remain macOS-only; the matrix also verifies that other targets compile with the explicit unsupported fallback.