forked from joejulian/gio
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:
+27
-13
@@ -18,6 +18,7 @@ import (
|
||||
"gioui.org/io/event"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/io/semantic"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
@@ -504,12 +505,14 @@ func (e *Editor) initBuffer() {
|
||||
e.text.Mask = e.Mask
|
||||
}
|
||||
|
||||
// Layout lays out the editor. If content is not nil, it is laid out on top.
|
||||
func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, content layout.Widget) layout.Dimensions {
|
||||
// Layout lays out the editor using the provided textMaterial as the paint material
|
||||
// for the text glyphs+caret and the selectMaterial as the paint material for the
|
||||
// selection rectangle.
|
||||
func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font text.Font, size unit.Sp, textMaterial, selectMaterial op.CallOp) layout.Dimensions {
|
||||
e.initBuffer()
|
||||
e.text.Update(gtx, lt, font, size, e.processEvents)
|
||||
|
||||
dims := e.layout(gtx, content)
|
||||
dims := e.layout(gtx, textMaterial, selectMaterial)
|
||||
|
||||
if e.focused {
|
||||
// Notify IME of selection if it changed.
|
||||
@@ -586,7 +589,7 @@ func (e *Editor) updateSnippet(gtx layout.Context, start, end int) {
|
||||
}.Add(gtx.Ops)
|
||||
}
|
||||
|
||||
func (e *Editor) layout(gtx layout.Context, content layout.Widget) layout.Dimensions {
|
||||
func (e *Editor) layout(gtx layout.Context, textMaterial, selectMaterial op.CallOp) layout.Dimensions {
|
||||
// Adjust scrolling for new viewport and layout.
|
||||
e.text.ScrollRel(0, 0)
|
||||
|
||||
@@ -659,33 +662,44 @@ func (e *Editor) layout(gtx layout.Context, content layout.Widget) layout.Dimens
|
||||
}
|
||||
e.showCaret = e.focused && (!blinking || dt%timePerBlink < timePerBlink/2)
|
||||
}
|
||||
disabled := gtx.Queue == nil
|
||||
|
||||
if content != nil {
|
||||
content(gtx)
|
||||
semantic.Editor.Add(gtx.Ops)
|
||||
if e.Len() > 0 {
|
||||
e.paintSelection(gtx, selectMaterial)
|
||||
e.paintText(gtx, textMaterial)
|
||||
}
|
||||
if !disabled {
|
||||
e.paintCaret(gtx, textMaterial)
|
||||
}
|
||||
return visibleDims
|
||||
}
|
||||
|
||||
// PaintSelection paints the contrasting background for selected text.
|
||||
func (e *Editor) PaintSelection(gtx layout.Context) {
|
||||
// paintSelection paints the contrasting background for selected text using the provided
|
||||
// material to set the painting material for the selection.
|
||||
func (e *Editor) paintSelection(gtx layout.Context, material op.CallOp) {
|
||||
e.initBuffer()
|
||||
if !e.focused {
|
||||
return
|
||||
}
|
||||
e.text.PaintSelection(gtx)
|
||||
e.text.PaintSelection(gtx, material)
|
||||
}
|
||||
|
||||
func (e *Editor) PaintText(gtx layout.Context) {
|
||||
// paintText paints the text glyphs using the provided material to set the fill of the
|
||||
// glyphs.
|
||||
func (e *Editor) paintText(gtx layout.Context, material op.CallOp) {
|
||||
e.initBuffer()
|
||||
e.text.PaintText(gtx)
|
||||
e.text.PaintText(gtx, material)
|
||||
}
|
||||
|
||||
func (e *Editor) PaintCaret(gtx layout.Context) {
|
||||
// paintCaret paints the text glyphs using the provided material to set the fill material
|
||||
// of the caret rectangle.
|
||||
func (e *Editor) paintCaret(gtx layout.Context, material op.CallOp) {
|
||||
e.initBuffer()
|
||||
if !e.showCaret || e.ReadOnly {
|
||||
return
|
||||
}
|
||||
e.text.PaintCaret(gtx)
|
||||
e.text.PaintCaret(gtx, material)
|
||||
}
|
||||
|
||||
// Len is the length of the editor contents, in runes.
|
||||
|
||||
Reference in New Issue
Block a user