1package text
2
3import (
4 "errors"
5 "math"
6 "slices"
7
8 "github.com/bnema/nefergui/internal/bidi"
9 "github.com/go-text/typesetting/di"
10 "github.com/go-text/typesetting/font"
11 "github.com/go-text/typesetting/segmenter"
12 "github.com/go-text/typesetting/shaping"
13 "golang.org/x/image/math/fixed"
14)
15
16// Glyph contains a positioned glyph in logical pixels, with its original shaping metrics.
17type Glyph struct {
18 ID font.GID
19 X, Y float64
20 Advance float64
21 // Cluster is the rune index, in the original Measure input, of the first
22 // rune of the glyph's cluster. It is not a UTF-8 byte offset.
23 Cluster int
24 Missing bool
25}
26
27// Run is a shaped span with one face, direction and embedding level.
28type Run struct {
29 Face *Face
30 Direction di.Direction
31 Level uint8 // resolved embedding level after L1
32 // Start and End are rune indices in the original Measure input; End is
33 // exclusive. A CRLF separator counts as two runes.
34 Start, End int
35 X, Y, Advance float64
36 Glyphs []Glyph
37}
38type Line struct {
39 Runs []Run
40 Direction di.Direction // resolved paragraph base direction
41 Width, Baseline, Height float64
42}
43type Layout struct {
44 Lines []Line
45 Direction di.Direction // paragraph basis, auto resolved by bidi analysis
46 Width, Height, Baseline, LineHeight float64
47}
48
49// Engine is single-goroutine only: Measure and EndFrame are not synchronized.
50// Its catalog is fixed at NewEngine and must not change afterwards, because
51// cached measurements depend on its faces.
52type Engine struct {
53 catalog *Catalog
54 seg shaping.Segmenter
55 shaper shaping.HarfbuzzShaper
56 wrapper shaping.LineWrapper
57 cache measureCache
58 scratch measureScratch
59}
60
61// measureScratch holds per-call buffers reused across measure calls. Nothing
62// in it may be referenced by a returned Layout.
63type measureScratch struct {
64 runes []rune
65 faces []*Face
66 reverse map[*font.Face]*Face
67 faceFor []*font.Face
68 items []shaping.Input
69 levelled []shaping.Input
70 outs []shaping.Output
71 runs shaping.RunIterator
72 graphemes segmenter.Segmenter
73 missing []rune // lazy coverage: runes no named face covers
74 choices []*Face // lazy coverage: faces selected for this measure
75 levels []uint8
76 breaks [1]int
77 visual visualScratch
78}
79
80// fullBreak returns the single paragraph-end line break (nil for empty text),
81// backed by scratch storage.
82func (s *measureScratch) fullBreak(text []rune) []int {
83 if len(text) == 0 {
84 return nil
85 }
86 s.breaks[0] = len(text)
87 return s.breaks[:]
88}
89
90// iterate returns the reusable run iterator positioned at the start of outs.
91func (s *measureScratch) iterate(outs []shaping.Output) shaping.RunIterator {
92 if r, ok := s.runs.(interface{ Reset([]shaping.Output) }); ok {
93 r.Reset(outs)
94 return s.runs
95 }
96 s.runs = shaping.NewSliceIterator(outs)
97 return s.runs
98}
99
100func NewEngine(c *Catalog) *Engine { return &Engine{catalog: c} }
101
102type constantFace struct{ face *font.Face }
103
104func (m constantFace) ResolveFace(r rune) *font.Face { return m.face }
105
106// clusterFaces selects a single face for every UAX#29 extended grapheme. Format
107// characters (ZWJ and variation selectors) do not need outline coverage; they
108// remain in the cluster to let HarfBuzz apply substitutions.
109// seg is reused scratch storage.
110func clusterFaces(seg *segmenter.Segmenter, selected []*font.Face, text []rune, choices []*Face) []*font.Face {
111 selected = slices.Grow(selected[:0], len(text))[:len(text)]
112 seg.Init(text)
113 it := seg.GraphemeIterator()
114 for it.Next() {
115 g := it.Grapheme()
116 f := choices[0]
117 for _, candidate := range choices {
118 all := true
119 for _, r := range g.Text {
120 if r == 0x200d || r >= 0xfe00 && r <= 0xfe0f {
121 continue
122 }
123 if _, ok := candidate.Shape.NominalGlyph(r); !ok {
124 all = false
125 break
126 }
127 }
128 if all {
129 f = candidate
130 break
131 }
132 }
133 for i := range g.Text {
134 selected[g.Offset+i] = f.Shape
135 }
136 }
137 return selected
138}
139
140// faceCovers checks an entire grapheme, ignoring format selectors.
141func faceCovers(f *Face, cluster []rune) bool {
142 for _, ch := range cluster {
143 if ch == 0x200d || ch >= 0xfe00 && ch <= 0xfe0f {
144 continue
145 }
146 if _, ok := f.Shape.NominalGlyph(ch); !ok {
147 return false
148 }
149 }
150 return true
151}
152
153// split cluster-aligned face runs after go-text's bidi/script itemization.
154func splitFaces(out, items []shaping.Input, faceFor []*font.Face) []shaping.Input {
155 out = out[:0]
156 for _, item := range items {
157 if item.RunStart >= item.RunEnd {
158 continue
159 }
160 start := item.RunStart
161 face := faceFor[start]
162 for i := start + 1; i < item.RunEnd; i++ {
163 if faceFor[i] != face {
164 part := item
165 part.RunStart = start
166 part.RunEnd = i
167 part.Face = face
168 out = append(out, part)
169 start = i
170 face = faceFor[i]
171 }
172 }
173 item.RunStart = start
174 item.Face = face
175 out = append(out, item)
176 }
177 return out
178}
179func (e *Engine) shape(text []rune, r Request, direction di.Direction, levels []uint8) ([]shaping.Output, map[*font.Face]*Face, error) {
180 if e == nil || e.catalog == nil || len(e.catalog.Faces) == 0 {
181 return nil, nil, errors.New("no font catalog")
182 }
183 if r.Size <= 0 || math.IsNaN(r.Size) || math.IsInf(r.Size, 0) || r.Size > 4096 {
184 return nil, nil, errors.New("invalid font size")
185 }
186 choices := e.catalog.candidates(r)
187 if e.catalog.source != nil {
188 // Load named faces only as required for coverage. A failing face is
189 // excluded permanently and ranking is recomputed to admit its runner-up.
190 // The scratch segmenter is free here: clusterFaces re-inits it later.
191 var named []*Face
192 seg := &e.scratch.graphemes
193 missing := e.scratch.missing[:0]
194 for {
195 seg.Init(text)
196 it := seg.GraphemeIterator()
197 missing = missing[:0]
198 for it.Next() {
199 cluster := it.Grapheme().Text
200 covered := false
201 for {
202 named = e.catalog.candidates(r)
203 failed := false
204 for _, f := range named {
205 if !f.ready() {
206 failed = true
207 break
208 }
209 if faceCovers(f, cluster) {
210 covered = true
211 break
212 }
213 }
214 if covered || !failed {
215 break
216 }
217 }
218 if !covered {
219 missing = append(missing, cluster...)
220 }
221 }
222 // A partial lazy index may lack faces of the named families:
223 // index every font before falling back, and check coverage again.
224 if len(missing) == 0 || !e.catalog.indexAll() {
225 break
226 }
227 }
228 // Rebuild after failed loads. The fallback window excludes named
229 // families by key, so it never depends on load or failure state.
230 e.scratch.missing = missing
231 named = e.catalog.candidates(r)
232 choices = e.scratch.choices[:0]
233 for _, f := range named {
234 if f.Shape != nil && !f.bad {
235 choices = append(choices, f)
236 }
237 }
238 if len(missing) != 0 {
239 for _, f := range e.catalog.fallbackWindow(missing, r) {
240 if !f.ready() {
241 continue
242 }
243 choices = append(choices, f)
244 complete := true
245 seg.Init(missing)
246 it := seg.GraphemeIterator()
247 for it.Next() {
248 covered := false
249 for _, candidate := range choices {
250 if faceCovers(candidate, it.Grapheme().Text) {
251 covered = true
252 break
253 }
254 }
255 if !covered {
256 complete = false
257 break
258 }
259 }
260 if complete {
261 break
262 }
263 }
264 }
265 // Nothing selected (empty text, or only uncovered runes with a failed
266 // window): the primary face shapes the text and supplies deterministic tofu.
267 if len(choices) == 0 {
268 if f := e.catalog.primary(r); f != nil {
269 choices = append(choices, f)
270 }
271 }
272 e.scratch.choices = choices
273 }
274 if len(choices) == 0 {
275 return nil, nil, errors.New("no usable fonts")
276 }
277 sc := &e.scratch
278 if sc.reverse == nil {
279 sc.reverse = map[*font.Face]*Face{}
280 }
281 clear(sc.reverse)
282 faces := sc.faces[:0]
283 // Variations belong to the engine invocation; do not mutate the catalog's shared faces.
284 for _, f := range choices {
285 if len(r.Variations) > 0 {
286 f = f.WithVariations(r.Variations)
287 }
288 faces = append(faces, f)
289 sc.reverse[f.Shape] = f
290 }
291 sc.faces = faces
292 sc.faceFor = clusterFaces(&sc.graphemes, sc.faceFor, text, faces)
293 input := shaping.Input{Text: text, RunEnd: len(text), Direction: direction, Size: fixed.Int26_6(math.Round(r.Size * 64))}
294 sc.items = splitFaces(sc.items, e.seg.Split(input, constantFace{face: faces[0].Shape}), sc.faceFor)
295 sc.levelled = splitLevels(sc.levelled, sc.items, levels)
296 out := sc.outs[:0]
297 for _, item := range sc.levelled {
298 if item.RunEnd > item.RunStart {
299 out = append(out, e.shaper.Shape(item))
300 }
301 }
302 clear(out[len(out):cap(out)]) // drop glyph references from longer earlier calls
303 sc.outs = out
304 return out, sc.reverse, nil
305}
306
307// Measure shapes, wraps (UAX#14) and positions runs. Width <= 0 means unbounded.
308// Every UAX#9 paragraph resolves its own direction; line baselines stack in logical px.
309//
310// Results are cached per (text, request, width) until EndFrame evicts them.
311// The returned Lines, Runs and Glyphs are shared with the cache: treat them as
312// immutable and copy before changing positions (see Layout.Clone).
313func (e *Engine) Measure(s string, r Request, width float64) (Layout, error) {
314 if l, ok := e.cache.get(s, r, width); ok {
315 return l, nil
316 }
317 l, err := e.measure(s, r, width)
318 if err == nil {
319 e.cache.put(s, r, width, l)
320 }
321 return l, err
322}
323
324// EndFrame evicts measurements not used during the last two frames. Call it
325// once after each frame that may have measured text.
326func (e *Engine) EndFrame() { e.cache.endFrame() }
327
328func appendRunes(dst []rune, s string) []rune {
329 for _, c := range s {
330 dst = append(dst, c)
331 }
332 return dst
333}
334
335func (e *Engine) measure(s string, r Request, width float64) (Layout, error) {
336 var result Layout
337 if math.IsNaN(width) || math.IsInf(width, 0) || width > 1<<24 {
338 return result, errors.New("invalid width")
339 }
340 if math.IsNaN(r.LetterSpacing) || math.IsInf(r.LetterSpacing, 0) || math.Abs(r.LetterSpacing) > 4096 {
341 return result, errors.New("invalid letter spacing")
342 }
343 e.scratch.runes = appendRunes(e.scratch.runes[:0], s)
344 text := e.scratch.runes
345 if paragraphs, split := paragraphRanges(text); split {
346 // Each paragraph measure reuses the scratch runes; keep a copy.
347 return e.measureParagraphs(slices.Clone(text), paragraphs, r, width)
348 }
349 if r.Direction > bidi.RTL {
350 return result, errors.New("invalid paragraph direction")
351 }
352 paraLevels, baseLevel, err := bidi.ResolveInto(e.scratch.levels, text, r.Direction, e.scratch.fullBreak(text))
353 if err != nil {
354 return result, err
355 }
356 e.scratch.levels = paraLevels
357 direction := di.DirectionLTR
358 if baseLevel%2 == 1 {
359 direction = di.DirectionRTL
360 }
361 result.Direction = direction
362 outs, reverse, err := e.shape(text, r, direction, paraLevels)
363 if err != nil {
364 return result, err
365 }
366 if r.LetterSpacing != 0 {
367 shaping.AddSpacing(outs, text, 0, fixed.Int26_6(math.Round(r.LetterSpacing*64)))
368 }
369 baseline := r.Size * 0.8
370 height := r.Size * 1.2
371 if ext, ok := e.catalog.primary(r).Shape.FontHExtents(); ok {
372 unit := r.Size / float64(e.catalog.primary(r).Shape.Upem())
373 baseline = float64(ext.Ascender) * unit
374 height = float64(ext.Ascender-ext.Descender+ext.LineGap) * unit
375 }
376 if height <= 0 {
377 height = r.Size * 1.2
378 }
379 result.Baseline = baseline
380 result.LineHeight = height
381 if len(text) == 0 {
382 return result, nil
383 }
384 limit := fixed.Int26_6(1 << 30)
385 if width > 0 {
386 limit = fixed.Int26_6(math.Round(width * 64))
387 }
388 wrapped, _ := e.wrapper.WrapParagraphF(shaping.WrapConfig{Direction: direction, DisableTrailingWhitespaceTrim: true}, limit, text, e.scratch.iterate(outs))
389 if len(wrapped) > 0 {
390 result.Lines = make([]Line, 0, len(wrapped))
391 }
392 for idx, line := range wrapped {
393 ordered, err := e.scratch.visual.order(text, line, r.Direction)
394 if err != nil {
395 return result, err
396 }
397 dst := Line{Baseline: baseline + float64(idx)*height, Height: height, Direction: direction}
398 glyphCount := 0
399 for _, o := range ordered {
400 glyphCount += len(o.output.Glyphs)
401 }
402 var glyphs []Glyph
403 if len(ordered) > 0 {
404 dst.Runs = make([]Run, 0, len(ordered))
405 glyphs = make([]Glyph, 0, glyphCount)
406 }
407 for _, item := range ordered {
408 o := item.output
409 f := reverse[o.Face]
410 run := Run{Face: f, Direction: o.Direction, Level: item.level, Start: o.Runes.Offset, End: o.Runes.Offset + o.Runes.Count, X: dst.Width, Y: dst.Baseline, Advance: float64(o.Advance) / 64}
411 cursor := 0.0
412 first := len(glyphs)
413 for _, g := range o.Glyphs {
414 glyphs = append(glyphs, Glyph{ID: g.GlyphID, X: run.X + cursor + float64(g.XOffset)/64, Y: run.Y - float64(g.YOffset)/64, Advance: float64(g.Advance) / 64, Cluster: g.TextIndex(), Missing: g.GlyphID == 0})
415 cursor += float64(g.Advance) / 64
416 }
417 if len(glyphs) > first {
418 run.Glyphs = glyphs[first:len(glyphs):len(glyphs)]
419 }
420 dst.Runs = append(dst.Runs, run)
421 dst.Width += run.Advance
422 }
423 if dst.Width > result.Width {
424 result.Width = dst.Width
425 }
426 result.Lines = append(result.Lines, dst)
427 }
428 result.Height = float64(len(result.Lines)) * height
429 return result, nil
430}