mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-08 02:45:38 +00:00
ui/text: add LayoutOptions
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+8
-10
@@ -37,8 +37,7 @@ type layoutKey struct {
|
|||||||
f *sfnt.Font
|
f *sfnt.Font
|
||||||
ppem fixed.Int26_6
|
ppem fixed.Int26_6
|
||||||
str string
|
str string
|
||||||
singleLine bool
|
opts text.LayoutOptions
|
||||||
maxWidth int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type pathKey struct {
|
type pathKey struct {
|
||||||
@@ -102,21 +101,20 @@ func (f *Faces) init() {
|
|||||||
f.layoutCache = make(map[layoutKey]cachedLayout)
|
f.layoutCache = make(map[layoutKey]cachedLayout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *textFace) Layout(str string, singleLine bool, maxWidth int) *text.Layout {
|
func (f *textFace) Layout(str string, opts text.LayoutOptions) *text.Layout {
|
||||||
ppem := fixed.Int26_6(f.faces.Config.Val(f.size)*64 + .5)
|
ppem := fixed.Int26_6(f.faces.Config.Val(f.size)*64 + .5)
|
||||||
lk := layoutKey{
|
lk := layoutKey{
|
||||||
f: f.font.Font,
|
f: f.font.Font,
|
||||||
ppem: ppem,
|
ppem: ppem,
|
||||||
str: str,
|
str: str,
|
||||||
singleLine: singleLine,
|
opts: opts,
|
||||||
maxWidth: maxWidth,
|
|
||||||
}
|
}
|
||||||
if l, ok := f.faces.layoutCache[lk]; ok {
|
if l, ok := f.faces.layoutCache[lk]; ok {
|
||||||
l.active = true
|
l.active = true
|
||||||
f.faces.layoutCache[lk] = l
|
f.faces.layoutCache[lk] = l
|
||||||
return l.layout
|
return l.layout
|
||||||
}
|
}
|
||||||
l := layoutText(ppem, str, f.font, singleLine, maxWidth)
|
l := layoutText(ppem, str, f.font, opts)
|
||||||
f.faces.layoutCache[lk] = cachedLayout{active: true, layout: l}
|
f.faces.layoutCache[lk] = cachedLayout{active: true, layout: l}
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
@@ -138,7 +136,7 @@ func (f *textFace) Path(str text.String) ui.BlockOp {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func layoutText(ppem fixed.Int26_6, str string, f *opentype, singleLine bool, maxWidth int) *text.Layout {
|
func layoutText(ppem fixed.Int26_6, str string, f *opentype, opts text.LayoutOptions) *text.Layout {
|
||||||
m := f.Metrics(ppem)
|
m := f.Metrics(ppem)
|
||||||
lineTmpl := text.Line{
|
lineTmpl := text.Line{
|
||||||
Ascent: m.Ascent,
|
Ascent: m.Ascent,
|
||||||
@@ -149,8 +147,8 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, singleLine bool, ma
|
|||||||
}
|
}
|
||||||
var lines []text.Line
|
var lines []text.Line
|
||||||
maxDotX := fixed.Int26_6(math.MaxInt32)
|
maxDotX := fixed.Int26_6(math.MaxInt32)
|
||||||
if maxWidth != ui.Inf {
|
if opts.MaxWidth != ui.Inf {
|
||||||
maxDotX = fixed.I(maxWidth)
|
maxDotX = fixed.I(opts.MaxWidth)
|
||||||
}
|
}
|
||||||
type state struct {
|
type state struct {
|
||||||
r rune
|
r rune
|
||||||
@@ -175,7 +173,7 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, singleLine bool, ma
|
|||||||
for prev.idx < len(str) {
|
for prev.idx < len(str) {
|
||||||
c, s := utf8.DecodeRuneInString(str[prev.idx:])
|
c, s := utf8.DecodeRuneInString(str[prev.idx:])
|
||||||
nl := text.IsNewline(c)
|
nl := text.IsNewline(c)
|
||||||
if singleLine && nl {
|
if opts.SingleLine && nl {
|
||||||
nl = false
|
nl = false
|
||||||
c = ' '
|
c = ' '
|
||||||
s = 1
|
s = 1
|
||||||
|
|||||||
+1
-1
@@ -302,7 +302,7 @@ func (e *Editor) moveCoord(pos image.Point) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) layoutText() {
|
func (e *Editor) layoutText() {
|
||||||
textLayout := e.Face.Layout(e.rr.String(), e.SingleLine, e.maxWidth)
|
textLayout := e.Face.Layout(e.rr.String(), LayoutOptions{SingleLine: e.SingleLine, MaxWidth: e.maxWidth})
|
||||||
lines := textLayout.Lines
|
lines := textLayout.Lines
|
||||||
dims := linesDimens(lines)
|
dims := linesDimens(lines)
|
||||||
for i := 0; i < len(lines)-1; i++ {
|
for i := 0; i < len(lines)-1; i++ {
|
||||||
|
|||||||
+1
-1
@@ -81,7 +81,7 @@ func (l *lineIterator) Next() (String, f32.Point, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l Label) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
func (l Label) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
||||||
textLayout := l.Face.Layout(l.Text, false, cs.Width.Max)
|
textLayout := l.Face.Layout(l.Text, LayoutOptions{MaxWidth: cs.Width.Max})
|
||||||
lines := textLayout.Lines
|
lines := textLayout.Lines
|
||||||
if max := l.MaxLines; max > 0 && len(lines) > max {
|
if max := l.MaxLines; max > 0 && len(lines) > max {
|
||||||
lines = lines[:max]
|
lines = lines[:max]
|
||||||
|
|||||||
+6
-1
@@ -33,8 +33,13 @@ type Layout struct {
|
|||||||
Lines []Line
|
Lines []Line
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LayoutOptions struct {
|
||||||
|
MaxWidth int
|
||||||
|
SingleLine bool
|
||||||
|
}
|
||||||
|
|
||||||
type Face interface {
|
type Face interface {
|
||||||
Layout(str string, singleLine bool, maxWidth int) *Layout
|
Layout(str string, opts LayoutOptions) *Layout
|
||||||
Path(str String) ui.BlockOp
|
Path(str String) ui.BlockOp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user