oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "encoding/json"
5 "net/http"
6 "sort"
7
8 "amadan.net/rastrillo/rastrillo"
9
10 "github.com/carlosframework/docs/internal/export"
11 "github.com/carlosframework/docs/internal/fonts"
12)
13
14// The font gate: what a document page carries so that its text is
15// never painted in a face it will later leave (Paul, 2026-10-24: "never
16// a flash of unstyled text, ever"). The design is
17// polish-audit/fonts/NO-FOUT.md; in short:
18//
19// - The page preloads, in <head>, exactly the faces its text is set
20// in, found by internal/export's own styleFor, so the screen asks
21// for what the PDF would embed.
22// - A blocking script in <head> (fixtures/fonts-hold.js) hides the
23// sheet before the first paint; the gate (fixtures/fonts-gate.mjs)
24// turns the web faces on once those faces have loaded, waits for a
25// paged editor to paginate, and shows the sheet once.
26// - A face still missing at the deadline is committed to its fallback
27// for the rest of the visit, so it can never swap in later.
28//
29// A reader without JavaScript is never held and never given a web
30// face: the stylesheet's system faces are the whole visit.
31
32// maxPreloads caps the <link rel=preload>s. More would compete with the
33// page itself; the faces past the cap load through the stylesheet, and
34// the gate holds the text for them all the same.
35const maxPreloads = 6
36
37// fontGate is the font gate's part of a document page.
38type fontGate struct {
39 Stylesheet string
40 Preloads []string
41 // Need is the JSON list of faces the gate waits for, on
42 // [docs-canvas]'s data-docs-fonts-need.
43 Need string
44 Script string
45 // Hold is fixtures/fonts-hold.js, the blocking script in <head>.
46 Hold string
47
48 // What the page can say about its fonts, over the formatting bar
49 // (fixtures/font-status.mjs): substitutes in use, a font that could
50 // not be loaded, one loading, a change refused while it does, and the
51 // label of the button that dismisses a note.
52 Substitute string
53 Unavailable string
54 Loading string
55 Refused string
56 Dismiss string
57}
58
59// gateFace is one entry of Need: the CSS the gate asks
60// document.fonts.load for, and the family key it commits to fallback if
61// the face misses the deadline ("previews" for the picker's).
62type gateFace struct {
63 Key string `json:"key"`
64 Family string `json:"family"`
65 Weight int `json:"weight"`
66 Style string `json:"style"`
67}
68
69// newFontGate is the gate for a document with this structure. Every
70// document page shows the formatting bar (disabled for a reader), and its
71// font control is drawn in the picker's preview faces, so those are
72// always gated too.
73func newFontGate(structure export.Document) fontGate {
74 // Families, not faces (branch review 4): every style of every family
75 // the document references, so no wrapper, mark or block can be in a
76 // face the gate did not wait for. The faces that draw the most text
77 // are preloaded first; the rest come through the stylesheet.
78 used := export.FacesUsed(structure)
79 families := export.FamiliesUsed(structure)
80 var faces []*fonts.Face
81 for _, f := range fonts.Families {
82 if families[f] {
83 for _, s := range f.Styles() {
84 faces = append(faces, f.Face(s.Bold(), s.Italic()))
85 }
86 }
87 }
88 sort.SliceStable(faces, func(i, j int) bool { return used[faces[i]] > used[faces[j]] })
89 g := fontGate{Stylesheet: fonts.StylesheetPath(), Script: fontsGateScriptPath(), Hold: fontsHoldScriptPath()}
90 var need []gateFace
91 for _, f := range faces {
92 if len(g.Preloads) < maxPreloads {
93 g.Preloads = append(g.Preloads, f.Path)
94 }
95 need = append(need, gateFace{Key: f.Family.Key, Family: f.Family.CSSName(), Weight: f.Style.Weight(), Style: f.Style.CSSStyle()})
96 }
97 // The closed font control shows the default's name in the default's
98 // preview face from the first frame, so that one is preloaded; the
99 // others are a couple of KB each and load with it. All of them gate
100 // the control, which is hidden until then.
101 if len(g.Preloads) < maxPreloads {
102 g.Preloads = append(g.Preloads, fonts.Default.PreviewPath())
103 }
104 for _, f := range fonts.Families {
105 need = append(need, gateFace{Key: "previews", Family: f.PreviewCSSName(), Weight: 400, Style: "normal"})
106 }
107 b, err := json.Marshal(need)
108 if err != nil {
109 panic("docs: marshal font gate: " + err.Error())
110 }
111 g.Need = string(b)
112 return g
113}
114
115// withCopy fills in the notes' copy for this request's locale.
116func (g fontGate) withCopy(r *http.Request) fontGate {
117 g.Substitute = rastrillo.T(r, "docs.editor.fonts.substitute")
118 g.Unavailable = rastrillo.T(r, "docs.editor.fonts.unavailable")
119 g.Loading = rastrillo.T(r, "docs.editor.fonts.loading")
120 g.Refused = rastrillo.T(r, "docs.editor.fonts.refused")
121 g.Dismiss = rastrillo.T(r, "docs.editor.fonts.dismiss")
122 return g
123}
124