oficina / slides Public

Fonts: read faces by their own hash; hand the PDF only faces a run uses

The object store is content-addressed (Put keys bytes by SHA-256, Get
refuses any other key), so fontObjectKey(ref) is the ref itself; a
face stored with Put loads by its ref through a real dir store. The
loader says the per-deck ownership check arrives with DeckFont. The
export hands the PDF module only the uploaded faces some painted run
is set in, so an uploaded style the deck never draws costs nothing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1281cff558ec63fa24a23a4a5b4c7e9452af0c3a parent b5eb27c
5 files changed, +110 −12
  • internal/slides/export.go +1 −1
  • internal/slides/export_fonts_test.go +39 −3
  • internal/slides/fontstore.go +10 −2
  • internal/slides/layout/layout.go +25 −6
  • internal/slides/layout/uploaded_test.go +35 −0
diff --git a/internal/slides/export.go b/internal/slides/export.go
index 18a5144..3b36b4c 100644
--- a/internal/slides/export.go
+++ b/internal/slides/export.go
@@ -184,7 +184,7 @@ func Export(ctx context.Context, d *deck.Deck, o ExportOptions) ([]byte, []layou
for _, f := range fonts.All() {
faces = append(faces, fonts.Data(f))
}
- // The uploaded faces the pages were set in, each renamed to the
+ // The uploaded faces some run was drawn in, each renamed to the
// family the pages' SVG names it by: the module matches a
// font-family against the name inside the font (fonts.Renamed).
for _, u := range lay.Uploaded() {
diff --git a/internal/slides/export_fonts_test.go b/internal/slides/export_fonts_test.go
index cbb1cb2..7d024ef 100644
--- a/internal/slides/export_fonts_test.go
+++ b/internal/slides/export_fonts_test.go
@@ -3,7 +3,9 @@ package slides
import (
"bytes"
"context"
+ "crypto/sha256"
"encoding/binary"
+ "encoding/hex"
"errors"
"io"
"os"
@@ -171,8 +173,10 @@ func (s *fakeFontStore) Delete(context.Context, string) error { return nil }
func TestTheFontLoaderReadsTheObjectStore(t *testing.T) {
ctx := context.Background()
- if got := fontObjectKey(uploadedRef); got != "fonts/"+uploadedRef {
- t.Errorf("fontObjectKey = %q", got)
+ // The store is content-addressed: a face is kept under its own
+ // SHA-256, which is its ref.
+ if got := fontObjectKey(uploadedRef); got != uploadedRef {
+ t.Errorf("fontObjectKey = %q, want the ref itself", got)
}
// No store: every read is an error, and so everything falls back.
@@ -194,7 +198,7 @@ func TestTheFontLoaderReadsTheObjectStore(t *testing.T) {
if _, err := load(strings.Repeat("d", 64)); err == nil {
t.Error("a face over the size limit loaded")
}
- for _, bad := range []string{"", "../x", "fonts/" + uploadedRef, strings.ToUpper(uploadedRef)} {
+ for _, bad := range []string{"", "../x", "fonts/" + uploadedRef, strings.ToUpper(uploadedRef), uploadedRef[:63]} {
if _, err := load(bad); err == nil {
t.Errorf("ref %q loaded", bad)
}
@@ -240,3 +244,35 @@ func addTable(t *testing.T, data []byte, tag string, body []byte) []byte {
}
return append(head, out...)
}
+
+// A face stored the way uploads store one — Put into the object store,
+// which keys it by its SHA-256 — is what the loader reads back by ref,
+// through a real store rather than a fake that takes any key.
+func TestAFontStoredWithPutIsLoaded(t *testing.T) {
+ ctx := context.Background()
+ store, err := blobs.Dir(t.TempDir())
+ if err != nil {
+ t.Fatal(err)
+ }
+ serif := embeddedFont(t, "LiberationSerif-Regular.ttf")
+ ref, err := store.Put(ctx, bytes.NewReader(serif), "font/ttf")
+ if err != nil {
+ t.Fatal(err)
+ }
+ sum := sha256.Sum256(serif)
+ if ref.Hash != hex.EncodeToString(sum[:]) {
+ t.Fatalf("Put keyed the face %q, not its SHA-256", ref.Hash)
+ }
+ a := &app{}
+ a.cfg.Blobs = store
+ got, err := a.fontLoader(ctx)(ref.Hash)
+ if err != nil || !bytes.Equal(got, serif) {
+ t.Fatalf("load = %d bytes, %v; want the face Put stored", len(got), err)
+ }
+
+ // And an export through that store draws with it.
+ pdf, _ := exportWith(t, uploadedDeck(deck.FontFace{Ref: ref.Hash, Format: "ttf", Weight: [2]int{400, 400}, Pictures: true}), a.fontLoader(ctx))
+ if !serifSubset.Match(pdf) {
+ t.Error("the export did not draw with the stored face")
+ }
+}
diff --git a/internal/slides/fontstore.go b/internal/slides/fontstore.go
index 5364aeb..981b6f8 100644
--- a/internal/slides/fontstore.go
+++ b/internal/slides/fontstore.go
@@ -24,12 +24,20 @@ var fontRefPattern = regexp.MustCompile(`^[0-9a-f]{64}$`)
// store; the text falls back to the rest of its stack.
var errNoFontStore = errors.New("slides: no object store to read a font from")
-// fontObjectKey is the object-store key an uploaded font file lives at.
-func fontObjectKey(ref string) string { return "fonts/" + ref }
+// fontObjectKey is the object-store key an uploaded font file lives at:
+// its ref itself. The store is content-addressed — Put keeps bytes under
+// their own SHA-256 and Get takes nothing else — and a face's ref is
+// that SHA-256, so an upload stored with Put is found by its ref. One
+// function all the same, so storing and reading cannot disagree.
+func fontObjectKey(ref string) string { return ref }
// fontLoader reads uploaded font files for one render (a thumbnail, an
// export): layout.Options.Fonts and ExportOptions.Fonts. It reads, and
// keeps nothing; the render parses each face once and drops it after.
+//
+// It reads any well-formed ref the deck lists. Limiting that to the
+// refs the deck recorded from its owner's library is the DeckFont
+// record's job, and arrives with it.
func (a *app) fontLoader(ctx context.Context) func(ref string) ([]byte, error) {
store := a.cfg.Blobs
return func(ref string) ([]byte, error) {
diff --git a/internal/slides/layout/layout.go b/internal/slides/layout/layout.go
index bc11d9c..31353e2 100644
--- a/internal/slides/layout/layout.go
+++ b/internal/slides/layout/layout.go
@@ -51,14 +51,17 @@ type Renderer struct {
// folded to lower case: one of its faces (fonts.UploadedFamily), or
// nil for a family with none to draw with.
families map[string]*fonts.Uploaded
- // used is every uploaded face of those families, in the order they
- // were resolved — what the PDF has to be handed.
+ // used is every uploaded face some run of this render is drawn
+ // in, in the order first drawn — what the PDF has to be handed, and
+ // no more: a style uploaded and never set costs an export nothing.
used []*fonts.Uploaded
+ seen map[*fonts.Uploaded]bool
}
// New is a render of d.
func New(d *deck.Deck, o Options) *Renderer {
- return &Renderer{d: d, opts: o, parsed: map[string]*fonts.Font{}, families: map[string]*fonts.Uploaded{}}
+ return &Renderer{d: d, opts: o, parsed: map[string]*fonts.Font{},
+ families: map[string]*fonts.Uploaded{}, seen: map[*fonts.Uploaded]bool{}}
}
// Layout draws the deck's slide at index; see the package's Layout.
@@ -72,11 +75,28 @@ func (r *Renderer) Layout(index int) (Paint, []Warning, error) {
}
e := newEngine(r, index)
e.slide(d.Slides[index])
+ r.note(e.ops)
return Paint{W: FrameW, H: FrameH, Background: e.bg, Ops: e.ops}, e.warnings, nil
}
-// Uploaded is every uploaded face this render has drawn with, or may
-// have: every face of each uploaded family a stack it laid out named.
+// note records the uploaded faces ops draw runs in.
+func (r *Renderer) note(ops []Op) {
+ for _, op := range ops {
+ t, ok := op.(Text)
+ if !ok {
+ continue
+ }
+ for _, run := range t.Runs {
+ if u := run.Face.Upload(); u != nil && !r.seen[u] {
+ r.seen[u] = true
+ r.used = append(r.used, u)
+ }
+ }
+ }
+}
+
+// Uploaded is every uploaded face a run this render laid out is drawn
+// in, in the order first drawn.
func (r *Renderer) Uploaded() []*fonts.Uploaded { return r.used }
// uploadedCSS is a CSS family name a picture can name an upload by:
@@ -124,7 +144,6 @@ func (r *Renderer) family(fam deck.FontFamily) *fonts.Uploaded {
}
u := fonts.UploadedFamily(faces...)
r.families[key] = u
- r.used = append(r.used, faces...)
return u
}
diff --git a/internal/slides/layout/uploaded_test.go b/internal/slides/layout/uploaded_test.go
index 154d8d4..f7f5c7b 100644
--- a/internal/slides/layout/uploaded_test.go
+++ b/internal/slides/layout/uploaded_test.go
@@ -456,3 +456,38 @@ func TestAnUploadedBoldFaceSetsTheHeading(t *testing.T) {
t.Errorf("the heading is drawn in %q", got)
}
}
+
+// Only the uploaded faces some run is drawn in go to the PDF: a style
+// the deck uploaded and never sets costs the export nothing.
+func TestOnlyTheFacesARunUsesAreHandedOn(t *testing.T) {
+ italic := regularFace(refItalic)
+ italic.Italic = true
+ bold := regularFace(strings.Repeat("e", 64))
+ bold.Weight = [2]int{700, 700}
+ d := uploadDeck(regularFace(refRegular), italic, bold)
+ d.Slides[0].Blocks = []deck.Block{{ID: "t1", Type: "text", Region: "body", Text: txt("Only upright words here")}}
+ l := newLoader(map[string][]byte{
+ refRegular: libFile(t, "LiberationSerif-Regular.ttf"),
+ refItalic: libFile(t, "LiberationSerif-Italic.ttf"),
+ strings.Repeat("e", 64): libFile(t, "LiberationSerif-Bold.ttf"),
+ })
+ r := layout.New(d, layout.Options{Fonts: l.load})
+ if _, _, err := r.Layout(0); err != nil {
+ t.Fatal(err)
+ }
+ used := r.Uploaded()
+ if len(used) != 1 || used[0].Weight != 400 || used[0].Italic {
+ var got []string
+ for _, u := range used {
+ got = append(got, u.CSS+" "+strconv.Itoa(u.Weight)+" "+strconv.FormatBool(u.Italic))
+ }
+ t.Errorf("handed on %q, want only the regular face", got)
+ }
+ // Laying the slide out again hands on nothing twice.
+ if _, _, err := r.Layout(0); err != nil {
+ t.Fatal(err)
+ }
+ if n := len(r.Uploaded()); n != 1 {
+ t.Errorf("after a second layout, %d faces", n)
+ }
+}