Files
openchamber-android-native/third_party/mygo/ui/glyphs.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

256 lines
8.5 KiB
Go

package ui
import (
"math"
"github.com/egoist/mygo/internal/scene"
"github.com/egoist/mygo/internal/text"
)
// Font is a font for text a Painter draws itself (Shape): a family list
// as Element.Font takes it, a size in DIPs (14 when 0), a weight from 100
// to 900 (400 when 0), italics, and OpenType features as
// Element.FontFeatures takes them, comma separated.
type Font struct {
Family string
Size float32
Weight int
Italic bool
Features string
// Lang is the language of the text, as for Element.Lang; "" is the
// language the app shows.
Lang string
// LineHeight is the height of a line of text ShapeText and
// ShapeRichText lay out, as a multiple of Size, as Element.LineHeight
// takes it: 1.8 gives CJK prose its usual leading. 0 takes the font's
// own. Metrics and Shape ignore it.
LineHeight float32
// Thicken draws the glyphs with a thicker stroke, as Ghostty's
// font-thicken: on macOS, with Core Text's font smoothing at its
// strongest, whatever the color and the user's setting. It changes
// nothing elsewhere.
Thicken bool
// Antialiased draws the glyphs without font smoothing, thinner, as
// browsers draw text styled -webkit-font-smoothing: antialiased on
// macOS, for drawings that match a web page's. It changes nothing
// elsewhere.
Antialiased bool
}
func (f Font) style() text.Style {
return text.Style{Family: f.Family, Size: f.Size, Weight: f.Weight, Italic: f.Italic, Features: f.Features, Lang: f.Lang, LineHeight: f.LineHeight}
}
// FontMetrics are the vertical metrics of a font, in DIPs: how far its
// glyphs reach above and below the baseline, and the gap it leaves
// between lines.
type FontMetrics struct {
Ascent, Descent, LineGap float32
}
// Metrics returns the font's metrics.
func (f Font) Metrics() FontMetrics {
style := f.style()
style.LineHeight = 0
a, d, h := text.Shared().Metrics(style)
return FontMetrics{Ascent: a, Descent: d, LineGap: max(h-a-d, 0)}
}
// Glyph is a glyph of text that Shape placed, which Painter.Glyphs draws.
type Glyph struct {
// Cluster is the rune of the text the glyph's cluster starts at, and
// Runes how many runes the cluster holds: a ligature holds several,
// and so does a character with combining marks, whose glyphs share
// the cluster.
Cluster, Runes int
// X is the left of the glyph's advance from the start of the text,
// and Advance its width. Move X to draw the glyph elsewhere, as in the
// cell of a grid.
X, Advance float32
// RTL marks a glyph of a right-to-left run, as of Hebrew or Arabic,
// whose clusters go from right to left: its cluster's first rune is
// at the right of its advance.
RTL bool
// After RTL, so that a Glyph takes no more room with it.
thick, flat bool
id uint32
font *text.Font
// y is how far below the baseline shaping put the glyph, as a mark
// above or below its base: negative above.
y float32
}
// Shape shapes s in font f, on one line, for Painter.Glyphs: the
// system's text engine finds the font, falls back to others for what it
// lacks, and applies ligatures and kerning. Unlike the text of elements,
// which ui caches by content, it caches nothing: keep the glyphs of text
// drawn in several frames.
//
// The glyphs are in visual order, on every platform, as they show from
// left to right: X ascends, and through a right-to-left run Cluster
// descends, with RTL set. Find the glyphs of a range of runes by their
// Cluster and Runes, not by their place in the slice; the glyphs of a
// cluster are next to each other.
func Shape(s string, f Font) []Glyph {
l := text.Shared().Shape(text.Params{Text: s, Style: f.style(), NoWrap: true, KeepSpaces: true})
var out []Glyph
for _, line := range l.Lines {
for _, g := range line.Glyphs {
out = append(out, Glyph{Cluster: g.Cluster, Runes: g.Runes, X: line.X + g.X, Advance: g.Advance, RTL: g.RTL, font: g.Font, id: g.ID, thick: f.Thicken, flat: f.Antialiased, y: g.Y - line.Baseline})
}
}
return out
}
// Glyphs draws glyphs that Shape made, from x, on the baseline at y, in
// color c.
func (p *Painter) Glyphs(glyphs []Glyph, x, y float32, c Color) {
sys := p.rt.text
var gs glyphSpace
if p.transform.Set && len(glyphs) > 0 {
var w, size float32
for _, g := range glyphs {
w = max(w, g.X+g.Advance)
if g.font != nil {
size = max(size, g.font.Size)
}
}
gs = p.glyphSpaceAt(x+w/2, y, size, false)
} else {
gs = glyphSpace{scale: p.scale, q: 1}
}
s := gs.scale
shade := text.ShadeOf(c.R, c.G, c.B)
c = c.Alpha(p.opacity)
color := c.scene()
baseline := sys.Baseline(y*s + gs.dy)
start := int32(len(p.s.Glyphs))
left, right := p.clip.X*s, (p.clip.X+p.clip.W)*s
var run *glyphRun
if sys.JoinsGlyphs() {
run = &glyphRun{scale: s}
}
for _, g := range glyphs {
if g.font == nil {
continue
}
pen := (x+g.X)*s + gs.dx
if !p.transform.Set && (pen > right || pen+(g.Advance+g.font.Size)*s < left) {
continue
}
ix := float32(math.Floor(float64(pen)))
shade := shade
if g.thick {
shade = text.Thick
} else if g.flat {
shade = text.Flat
}
gi := sys.Glyph(g.font, g.id, s, pen, shade, p.opaque)
if !gi.OK {
continue
}
gb := baseline
if g.y != 0 {
gb = sys.Baseline((y+g.y)*s + gs.dy)
}
sg := scene.Glyph{
X: ix + gi.Left, Y: gb + gi.Top, W: float32(gi.W), H: float32(gi.H),
U: gi.X, V: gi.Y, UW: gi.W, VH: gi.H,
Color: color, Wide: p.glyphWide(c), Colored: gi.Colored, Subpixel: gi.Subpixel, Thin: gi.Thin,
}
if run != nil {
run.add(p, text.Glyph{Font: g.font, ID: g.id}, gi, pen, sg, shade, gb)
continue
}
p.s.Glyphs = append(p.s.Glyphs, sg)
}
if run != nil {
run.flush(p)
}
if end := int32(len(p.s.Glyphs)); end > start {
gs.add(p, scene.Op{Kind: scene.OpGlyphs, Start: start, End: end})
}
}
// TextLine paints the glyphs of the first line of l, a layout of this
// module's text system that only its packages make, as package ui/editor
// does, from x, on the baseline at y, each in the color color returns for
// the rune its cluster starts at. Glyphs outside the painter's clip are
// left out.
func (p *Painter) TextLine(l *text.Layout, x, y float32, color func(cluster int) Color) {
if l == nil || len(l.Lines) == 0 {
return
}
sys := p.rt.text
gs := glyphSpace{scale: p.scale, q: 1}
if p.transform.Set {
gs = p.glyphSpaceAt(x+l.Lines[0].Width/2, y, l.Params.Style.FontSize(), false)
}
s := gs.scale
baseline := sys.Baseline(y*s + gs.dy)
left, right := p.clip.X*s, (p.clip.X+p.clip.W)*s
start := int32(len(p.s.Glyphs))
var run *glyphRun
if sys.JoinsGlyphs() {
run = &p.rt.glyphRun
run.ids, run.pens, run.glyphs = run.ids[:0], run.pens[:0], run.glyphs[:0]
run.scale = s
}
line := &l.Lines[0]
for _, g := range line.Glyphs {
pen := (x+g.X)*s + gs.dx
// An inline object's box draws no glyph (text.Span.ObjectWidth).
if g.Object || !p.transform.Set && (pen > right || pen+(g.Advance+g.Size)*s < left) {
continue
}
c := color(g.Cluster)
shade := text.ShadeOf(c.R, c.G, c.B)
gi := sys.Glyph(g.Font, g.ID, s, pen, shade, p.opaque)
if !gi.OK {
continue
}
c = c.Alpha(p.opacity)
// y is the line's baseline: the layout's lies Baseline below it.
gb := glyphBaseline(sys, &g, line, y-line.Baseline, baseline, gs)
sg := scene.Glyph{
X: float32(math.Floor(float64(pen))) + gi.Left, Y: gb + gi.Top, W: float32(gi.W), H: float32(gi.H),
U: gi.X, V: gi.Y, UW: gi.W, VH: gi.H,
Color: c.scene(), Wide: p.glyphWide(c), Colored: gi.Colored, Subpixel: gi.Subpixel, Thin: gi.Thin,
}
if run != nil {
run.add(p, text.Glyph{Font: g.Font, ID: g.ID}, gi, pen, sg, shade, gb)
continue
}
p.s.Glyphs = append(p.s.Glyphs, sg)
}
if run != nil {
run.flush(p)
}
if end := int32(len(p.s.Glyphs)); end > start {
gs.add(p, scene.Op{Kind: scene.OpGlyphs, Start: start, End: end})
}
}
// Visible returns what the painter's clip leaves of the window, in DIPs:
// the part of an element's box that shows inside the scroll containers
// and clips around it, as an element taller than the window, showing a
// long document, paints only what falls in it. It is in the coordinates
// the painter draws in: under a transform, an element's or the
// painter's, the clip taken back through it, so an element scaled up
// sees as much less of itself as shows.
func (p *Painter) Visible() Rect {
if !p.transform.Set {
return p.clip
}
inv, ok := p.transform.Inverse()
if !ok {
return p.clip
}
return transformRect(inv, p.clip)
}
// Scale returns how many device pixels a DIP is, for drawing that lines
// up with the pixels of the display.
func (p *Painter) Scale() float32 { return p.scale }