| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "amadan.net/oficina/slides/internal/slides/deck" |
| 7 | "amadan.net/oficina/slides/internal/slides/fonts" |
| 8 | ) |
| 9 | |
| 10 | // families.go is which of the three font families a deck's slides can |
| 11 | // be drawn in — the list the font gate waits for before it shows any |
| 12 | // slide surface (../fontgate.go; Paul, 2026-10-26: "No flashing, |
| 13 | // ever!"). |
| 14 | // |
| 15 | // Families, not faces. Docs learned that over four review rounds of |
| 16 | // finding faces a face list had missed (a space-only run, a block's |
| 17 | // strut, a bold run's wrapper): the gate waits for every style of every |
| 18 | // family the deck references, so no wrapper, mark or block can be in a |
| 19 | // face that was not waited for. A family is referenced when: |
| 20 | // |
| 21 | // - the deck's heading or body stack names it — the theme's own, or |
| 22 | // the deck's meta.fonts override, judged exactly as sectionStyle |
| 23 | // judges it (safeFontStack). Both are referenced whether or not a |
| 24 | // heading is on any slide: the section's own font-family is the |
| 25 | // body stack, and a person can add a heading at any moment; |
| 26 | // - any slide carries code: a code block, or a run with the code |
| 27 | // mark in any block's text or any table cell. The code stack is |
| 28 | // slides.css's --slides-font-code, the Mono stack on every theme. |
| 29 | // |
| 30 | // A stack names a family wherever it appears in it |
| 31 | // (fonts.FamiliesInStack), because a browser falls through a stack. |
| 32 | // Speaker notes are not here: no surface draws them in a deck face. |
| 33 | // |
| 34 | // A deck's own uploaded families (meta.fontFiles) are UploadedUsed's: |
| 35 | // one is used when the heading or body stack leads with it. |
| 36 | // |
| 37 | // fixtures/font-families.mjs is the browser's copy of this rule, for |
| 38 | // a deck the editor or a collaborator has just changed, and |
| 39 | // fixtures/vectors/font-families.json holds the two to the same |
| 40 | // answers. internal/slidestest's TestFontTheGateCoversEveryFaceADeckUses |
| 41 | // holds both to what the browser actually draws, over generated decks. |
| 42 | |
| 43 | // DeckStacks is the heading and body font stacks d's slides are set |
| 44 | // in: the theme's, with the deck's own meta.fonts override where it |
| 45 | // passes the allowlist. Every slide of a deck has the same two. |
| 46 | func DeckStacks(d *deck.Deck) (heading, body string) { |
| 47 | vars := slideVars(d, deck.Slide{}) |
| 48 | return vars["--slides-font-heading"], vars["--slides-font-body"] |
| 49 | } |
| 50 | |
| 51 | // CodeStack is the stack code is set in, on every theme |
| 52 | // (slides.css's --slides-font-code). |
| 53 | func CodeStack() string { return fonts.Mono.Stack() } |
| 54 | |
| 55 | // ThemeStacks is every theme's heading and body stack, keyed by theme |
| 56 | // id — what the font gate's manifest carries so the editor can work |
| 57 | // out the families of a deck whose theme it has just changed. |
| 58 | func ThemeStacks() map[string][2]string { |
| 59 | out := make(map[string][2]string, len(themeVars)) |
| 60 | for id, v := range themeVars { |
| 61 | out[id] = [2]string{v["--slides-font-heading"], v["--slides-font-body"]} |
| 62 | } |
| 63 | return out |
| 64 | } |
| 65 | |
| 66 | // FamiliesUsed is every family d's slides can be drawn in, in |
| 67 | // fonts.Families order. A deck with no slides draws in nothing. |
| 68 | func FamiliesUsed(d *deck.Deck) []fonts.Family { |
| 69 | if d == nil || len(d.Slides) == 0 { |
| 70 | return nil |
| 71 | } |
| 72 | heading, body := DeckStacks(d) |
| 73 | stacks := []string{heading, body} |
| 74 | if HasCode(d) { |
| 75 | stacks = append(stacks, CodeStack()) |
| 76 | } |
| 77 | named := map[fonts.Family]bool{} |
| 78 | for _, s := range stacks { |
| 79 | for _, f := range fonts.FamiliesInStack(s) { |
| 80 | named[f] = true |
| 81 | } |
| 82 | } |
| 83 | var out []fonts.Family |
| 84 | for _, f := range fonts.Families() { |
| 85 | if named[f] { |
| 86 | out = append(out, f) |
| 87 | } |
| 88 | } |
| 89 | return out |
| 90 | } |
| 91 | |
| 92 | // UploadedUsed is every family of d's meta.fontFiles that its slides |
| 93 | // can be drawn in, in fontFiles order: a family is used when the |
| 94 | // deck's heading or body stack leads with its css name (compared as a |
| 95 | // browser compares family names, without regard to case). An uploaded |
| 96 | // family is put in front of the theme's stack (fixtures/deck-fonts.mjs), |
| 97 | // so leading is the rule; one named later in a stack, or only listed, |
| 98 | // is not waited for and stays out of document.fonts, so the stack falls |
| 99 | // through it. A stack leading with a name fontFiles does not list falls |
| 100 | // back the same way. The font gate keys these "u:" + css. |
| 101 | func UploadedUsed(d *deck.Deck) []deck.FontFamily { |
| 102 | if d == nil || len(d.Slides) == 0 || len(d.Meta.FontFiles) == 0 { |
| 103 | return nil |
| 104 | } |
| 105 | heading, body := DeckStacks(d) |
| 106 | leads := []string{LeadFamily(heading), LeadFamily(body)} |
| 107 | var out []deck.FontFamily |
| 108 | seen := map[string]bool{} |
| 109 | for _, f := range d.Meta.FontFiles { |
| 110 | key := strings.ToLower(f.CSS) |
| 111 | if f.CSS == "" || seen[key] { |
| 112 | continue |
| 113 | } |
| 114 | for _, lead := range leads { |
| 115 | if lead != "" && strings.ToLower(lead) == key { |
| 116 | seen[key] = true |
| 117 | out = append(out, f) |
| 118 | break |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | return out |
| 123 | } |
| 124 | |
| 125 | // LeadFamily is the first family a stack names, trimmed and unquoted |
| 126 | // ("" for none): fixtures/deck-fonts.mjs's leadFamily. |
| 127 | func LeadFamily(stack string) string { |
| 128 | first, _, _ := strings.Cut(stack, ",") |
| 129 | return strings.Trim(strings.TrimSpace(first), `"'`) |
| 130 | } |
| 131 | |
| 132 | // HasCode reports whether any slide of d carries text in the code |
| 133 | // face: a code block, or a code-marked run in a block's text or in a |
| 134 | // table cell. |
| 135 | func HasCode(d *deck.Deck) bool { |
| 136 | for _, s := range d.Slides { |
| 137 | for _, b := range s.Blocks { |
| 138 | if b.Type == "code" || textHasCode(b.Text) { |
| 139 | return true |
| 140 | } |
| 141 | if b.Type == "table" { |
| 142 | for _, cell := range cellsProp(b.Props) { |
| 143 | if textHasCode(cell) { |
| 144 | return true |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | return false |
| 151 | } |
| 152 | |
| 153 | func textHasCode(t deck.Text) bool { |
| 154 | for _, p := range t { |
| 155 | for _, r := range p { |
| 156 | if r.C { |
| 157 | return true |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | return false |
| 162 | } |
| 163 | |