Debian Codegraph @

shiny/widget/text.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package widget
6
7 import (
8 "image"
9 "image/draw"
10
11 "golang.org/x/exp/shiny/text"
12 "golang.org/x/exp/shiny/unit"
13 "golang.org/x/exp/shiny/widget/node"
14 "golang.org/x/exp/shiny/widget/theme"
15 "golang.org/x/image/font"
16 "golang.org/x/image/math/fixed"
17 )
18
19 // Text is a leaf widget that holds a text label.
20 type Text struct {
21 node.LeafEmbed
22 frame text.Frame
23 faceSet bool
24
25 // TODO: scrolling, although should that be the responsibility of this
26 // widget, the parent widget or something else?
27 }
28
29 // NewText returns a new Text widget.
30 func NewText(text string) *Text {
31 w := &Text{}
32 w.Wrapper = w
33 if text != "" {
34 c := w.frame.NewCaret()
35 c.WriteString(text)
36 c.Close()
37 }
38 return w
39 }
40
41 func (w *Text) setFace(t *theme.Theme) {
42 // TODO: can a theme change at runtime, or can it be set only once, at
43 // start-up?
44 if !w.faceSet {
45 w.faceSet = true
46 // TODO: when is face released? Should we just unconditionally call
47 // SetFace for every Measure, Layout and Paint? How do we avoid
48 // excessive re-calculation of soft returns when re-using the same
49 // logical face (as in "Times New Roman 12pt") even if using different
50 // physical font.Face values (as each Face may have its own caches)?
51 face := t.AcquireFontFace(theme.FontFaceOptions{})
52 w.frame.SetFace(face)
53 }
54 }
55
56 // TODO: should padding (and/or margin and border) be a universal concept and
57 // part of the node.Embed type instead of having each widget implement its own?
58
59 func (w *Text) padding(t *theme.Theme) int {
60 return t.Pixels(unit.Ems(0.5)).Ceil()
61 }
62
63 func (w *Text) Measure(t *theme.Theme, widthHint, heightHint int) {
64 w.setFace(t)
65 padding := w.padding(t)
66
67 if widthHint < 0 {
68 w.frame.SetMaxWidth(0)
69 w.MeasuredSize = image.Point{
70 0, // TODO: this isn't right.
71 w.frame.Height() + 2*padding,
72 }
73 return
74 }
75
76 maxWidth := fixed.I(widthHint - 2*padding)
77 if maxWidth <= 1 {
78 maxWidth = 1
79 }
80 w.frame.SetMaxWidth(maxWidth)
81
82 w.MeasuredSize = image.Point{
83 widthHint,
84 w.frame.Height() + 2*padding,
85 }
86 }
87
88 func (w *Text) Layout(t *theme.Theme) {
89 w.setFace(t)
90 padding := w.padding(t)
91 maxWidth := fixed.I(w.Rect.Dx() - 2*padding)
92 if maxWidth <= 1 {
93 maxWidth = 1
94 }
95 w.frame.SetMaxWidth(maxWidth)
96 }
97
98 func (w *Text) PaintBase(ctx *node.PaintBaseContext, origin image.Point) error {
99 w.Marks.UnmarkNeedsPaintBase()
100 dst := ctx.Dst.SubImage(w.Rect.Add(origin)).(*image.RGBA)
101 if dst.Bounds().Empty() {
102 return nil
103 }
104
105 face := ctx.Theme.AcquireFontFace(theme.FontFaceOptions{})
106 defer ctx.Theme.ReleaseFontFace(theme.FontFaceOptions{}, face)
107 m := face.Metrics()
108 ascent := m.Ascent.Ceil()
109 descent := m.Descent.Ceil()
110 height := m.Height.Ceil()
111
112 padding := w.padding(ctx.Theme)
113
114 draw.Draw(dst, dst.Bounds(), ctx.Theme.GetPalette().Background(), image.Point{}, draw.Src)
115
116 minDotY := fixed.I(dst.Bounds().Min.Y - descent)
117 maxDotY := fixed.I(dst.Bounds().Max.Y + ascent)
118
119 x0 := fixed.I(origin.X + w.Rect.Min.X + padding)
120 d := font.Drawer{
121 Dst: dst,
122 Src: ctx.Theme.GetPalette().Foreground(),
123 Face: face,
124 Dot: fixed.Point26_6{
125 X: x0,
126 Y: fixed.I(origin.Y + w.Rect.Min.Y + padding + ascent),
127 },
128 }
129 f := &w.frame
130 for p := f.FirstParagraph(); p != nil; p = p.Next(f) {
131 for l := p.FirstLine(f); l != nil; l = l.Next(f) {
132 if d.Dot.Y > minDotY {
133 if d.Dot.Y >= maxDotY {
134 return nil
135 }
136 for b := l.FirstBox(f); b != nil; b = b.Next(f) {
137 d.DrawBytes(b.TrimmedText(f))
138 // TODO: adjust d.Dot.X for any ligatures?
139 }
140 d.Dot.X = x0
141 }
142 d.Dot.Y += fixed.I(height)
143 }
144 }
145 return nil
146 }
147
148 func (w *Text) Paint(ctx *node.PaintContext, origin image.Point) error {
149 // TODO: draw an optional border, whose color depends on whether w has the
150 // keyboard focus.
151 return w.LeafEmbed.Paint(ctx, origin)
152 }