mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
font/opentype,text,widget{,/material}: [API] support bitmap glyph rendering
This commit supports rendering opentype glyphs containing bitmap data instead of
color data. In order to support returning the shaped bitmap glyphs from the Shaper's
Shape() method, it has gained a second return parameter, an op.CallOp. Adding
that CallOp immediately after or immediately before painting the returned path
will display the bitmap glyphs.
The consequences of supporting colored glyphs forced changes upon the widget APIs
for widgets that display text. Previously text always had a fixed paint material,
so we could rely upon the caller setting the material (e.g. adding a paint.ColorOp)
before painting the glyphs and everything would work. Now that we display image-
based glyphs, we end up changing the painting material to an image midway through
displaying text. This is an awkward consequence of how we currently manage the
painting material, and to work around it widgets now accept an op.CallOp that
is expected to set the proper paint material. Text widgets will use that op.CallOp
before painting text (or other paint operations) to ensure that they are painting
with the proper materials.
This, in turn, changed the APIs for laying out widget.Editor, widget.Label, and
widget.Selectable, and eliminated the need for them to accept a callback (the
callback was only really to set the colors). Dropping that callback function
allowed me to consolidate widget.Label to only need one exported Layout method,
and allowed me to unexport the PaintText, PaintCaret, and PaintSelection methods
from widget.Editor and widget.Selectable. Those methods are useless in the public
API now that they don't need to be invoked after applying a color operation.
Callers of the raw text shaper API will need to make the following changes:
- Where before you used:
var ops *op.Ops // Assume we have an operation list.
var shaper *text.Shaper // Assume we have a shaper.
var col color.NRGBA // Assume we have a text color.
var glyphs []text.Glyph // Assume we have already filled a slice of glyphs.
shape := shaper.Shape(glyphs)
paint.FillShape(ops, col, clip.Outline{Path:shape}.Op())
- Now you should do:
shape, call := shaper.Shape(glyphs)
paint.FillShape(ops, col, clip.Outline{Path:shape}.Op())
call.Add(ops)
Callers of the widget.{Label,Selectable,Editor} APIs will need to make the
following changes:
- Where before you used:
var gtx layout.Context // Assume we have an operation list.
var shaper *text.Shaper // Assume we have a shaper.
var textCol color.NRGBA // Assume we have a text color.
var selectCol color.NRGBA // Assume we have a selection color.
var ed widget.Editor // Assume we have an editor.
var sel widget.Selectable // Assume we have a selectable.
// Lay out an editor.
ed.Layout(gtx, shaper, text.Font{}, unit.Sp(30), func(layout.Context) layout.Dimensions {
// Paint the editor.
})
// Lay out a selectable.
sel.Layout(gtx, shaper, text.Font{}, unit.Sp(30), func(layout.Context) layout.Dimensions {
// Paint the selectable.
})
// Lay out an interactive label.
widget.Label{}.LayoutSelectable(gtx, shaper, text.Font{}, unit.Sp(30), "hello", func(layout.Context) layout.Dimensions {
// Paint the label.
})
// Lay out a non-interactive label.
widget.Label{}.Layout(gtx, shaper, text.Font{}, unit.Sp(30), "hello")
- Now you should do:
// Capture setting the text paint material in a macro.
textColMacro := op.Record(gtx.Ops)
paint.ColorOp{Color: textCol}.Add(gtx.Ops)
textMaterial := textColMacro.Stop()
// Capture setting the selection paint material in a macro.
selectColMacro := op.Record(gtx.Ops)
paint.ColorOp{Color: selectCol}.Add(gtx.Ops)
selectMaterial := selectColMacro.Stop()
// Lay out an editor.
ed.Layout(gtx, shaper, text.Font{}, unit.Sp(30), textMaterial, selectMaterial)
// Lay out a selectable.
sel.Layout(gtx, shaper, text.Font{}, unit.Sp(30), textMaterial, selectMaterial)
// Lay out a label (no difference between interactive and non-interactive)
widget.Label{}.Layout(gtx, shaper, text.Font{}, unit.Sp(30), "hello", textMaterial, selectMaterial)
Callers of the material package API do not need to make any changes.
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
+24
-12
@@ -27,22 +27,22 @@ type Label struct {
|
||||
Selectable *Selectable
|
||||
}
|
||||
|
||||
// Layout the label with the given shaper, font, size, and text. Content is a function that will be invoked
|
||||
// with the label's clip area applied, and should be used to set colors and paint the text/selection.
|
||||
// content will only be invoked for labels with a non-nil Selectable. For stateless labels, the paint color
|
||||
// should be set prior to calling Layout.
|
||||
func (l Label) LayoutSelectable(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, txt string, content layout.Widget) layout.Dimensions {
|
||||
// Layout the label with the given shaper, font, size, text, and materials. If the Selectable field is
|
||||
// populated, the label will support text selection. Otherwise, it will be non-interactive. The textMaterial
|
||||
// and selectionMaterial op.CallOps are responsible for setting the painting material for the text glyphs
|
||||
// and the text selection rectangles, respectively.
|
||||
func (l Label) Layout(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, txt string, textMaterial, selectionMaterial op.CallOp) layout.Dimensions {
|
||||
if l.Selectable == nil {
|
||||
return l.Layout(gtx, lt, font, size, txt)
|
||||
return l.layout(gtx, lt, font, size, txt, textMaterial, selectionMaterial)
|
||||
}
|
||||
l.Selectable.text.Alignment = l.Alignment
|
||||
l.Selectable.text.MaxLines = l.MaxLines
|
||||
l.Selectable.SetText(txt)
|
||||
return l.Selectable.Layout(gtx, lt, font, size, content)
|
||||
return l.Selectable.Layout(gtx, lt, font, size, textMaterial, selectionMaterial)
|
||||
}
|
||||
|
||||
// Layout the text as non-interactive.
|
||||
func (l Label) Layout(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, txt string) layout.Dimensions {
|
||||
// layout the text as non-interactive.
|
||||
func (l Label) layout(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, txt string, textMaterial, selectionMaterial op.CallOp) layout.Dimensions {
|
||||
cs := gtx.Constraints
|
||||
textSize := fixed.I(gtx.Sp(size))
|
||||
lt.LayoutString(text.Parameters{
|
||||
@@ -53,7 +53,11 @@ func (l Label) Layout(gtx layout.Context, lt *text.Shaper, font text.Font, size
|
||||
}, cs.Min.X, cs.Max.X, gtx.Locale, txt)
|
||||
m := op.Record(gtx.Ops)
|
||||
viewport := image.Rectangle{Max: cs.Max}
|
||||
it := textIterator{viewport: viewport, maxLines: l.MaxLines}
|
||||
it := textIterator{
|
||||
viewport: viewport,
|
||||
maxLines: l.MaxLines,
|
||||
material: textMaterial,
|
||||
}
|
||||
semantic.LabelOp(txt).Add(gtx.Ops)
|
||||
var glyphs [32]text.Glyph
|
||||
line := glyphs[:0]
|
||||
@@ -86,6 +90,9 @@ type textIterator struct {
|
||||
viewport image.Rectangle
|
||||
// maxLines is the maximum number of text lines that should be displayed.
|
||||
maxLines int
|
||||
// material sets the paint material for the text glyphs. If none is provided
|
||||
// the glyphs will be invisible.
|
||||
material op.CallOp
|
||||
|
||||
// linesSeen tracks the quantity of line endings this iterator has seen.
|
||||
linesSeen int
|
||||
@@ -165,9 +172,14 @@ func (it *textIterator) paintGlyph(gtx layout.Context, shaper *text.Shaper, glyp
|
||||
}
|
||||
if glyph.Flags&text.FlagLineBreak != 0 || cap(line)-len(line) == 0 || !visibleOrBefore {
|
||||
t := op.Offset(it.lineOff).Push(gtx.Ops)
|
||||
op := clip.Outline{Path: shaper.Shape(line)}.Op().Push(gtx.Ops)
|
||||
path := shaper.Shape(line)
|
||||
outline := clip.Outline{Path: path}.Op().Push(gtx.Ops)
|
||||
it.material.Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
op.Pop()
|
||||
outline.Pop()
|
||||
if call := shaper.Bitmaps(line); call != (op.CallOp{}) {
|
||||
call.Add(gtx.Ops)
|
||||
}
|
||||
t.Pop()
|
||||
line = line[:0]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user