Implemented highlight and underline styles

This commit is contained in:
Sasha Koshka 2022-11-29 01:28:54 -05:00
parent 0462afdf11
commit f51f9ae5c5
2 changed files with 80 additions and 54 deletions

View File

@ -53,7 +53,12 @@ func (backend *Backend) drawCells (forceRedraw bool) (areas []image.Rectangle) {
if forceRedraw && content < 32 { continue } if forceRedraw && content < 32 { continue }
areas = append(areas, backend.boundsOfCell(x, y)) areas = append(areas, backend.boundsOfCell(x, y))
backend.drawRune(x, y, content, cell.Color(), !forceRedraw) backend.drawRune (
x, y,
content,
cell.Color(),
cell.Style(),
!forceRedraw)
}} }}
if backend.drawBufferBounds && forceRedraw { if backend.drawBufferBounds && forceRedraw {
@ -74,68 +79,88 @@ func (backend *Backend) drawRune (
x, y int, x, y int,
character rune, character rune,
runeColor stone.Color, runeColor stone.Color,
runeStyle stone.Style,
drawBackground bool, drawBackground bool,
) { ) {
// TODO: cache these draws as non-transparent buffers with the // TODO: cache these draws as non-transparent buffers with the
// application background color as the background. that way, we won't // application background color as the background. that way, we won't
// need to redraw the characters *or* composite them. // need to redraw the characters *or* composite them.
if drawBackground { var background xgraphics.BGRA
var foreground xgraphics.BGRA
highlight := runeStyle & stone.StyleHighlight > 0
if highlight {
background = backend.colors[runeColor]
foreground = backend.colors[stone.ColorBackground]
} else {
background = backend.colors[stone.ColorBackground]
foreground = backend.colors[runeColor]
}
if drawBackground || highlight {
fillRectangle ( fillRectangle (
&image.Uniform { &image.Uniform { C: background },
C: backend.config.Color(stone.ColorBackground),
},
backend.canvas, backend.canvas,
backend.boundsOfCell(x, y)) backend.boundsOfCell(x, y))
} }
if character < 32 { return }
origin := backend.originOfCell(x, y + 1) origin := backend.originOfCell(x, y + 1)
destinationRectangle, mask, maskPoint, _, ok := backend.font.face.Glyph (
fixed.Point26_6 {
X: fixed.I(origin.X),
Y: fixed.I(origin.Y),
},
character)
if !ok { if character >= 32 {
strokeRectangle ( destinationRectangle, mask, maskPoint, _, ok := backend.font.face.Glyph (
&image.Uniform { fixed.Point26_6 {
C: backend.config.Color(stone.ColorForeground), X: fixed.I(origin.X),
Y: fixed.I(origin.Y),
}, },
backend.canvas, character)
backend.boundsOfCell(x, y))
return if !ok {
strokeRectangle (
&image.Uniform { C: foreground },
backend.canvas,
backend.boundsOfCell(x, y))
return
}
if backend.drawCellBounds {
strokeRectangle (
&image.Uniform { C: foreground },
backend.canvas,
backend.boundsOfCell(x, y))
}
// cue a series of pointless optimizations
alphaMask, isAlpha := mask.(*image.Alpha)
if isAlpha {
backend.sprayRuneMaskAlpha (
alphaMask, destinationRectangle,
maskPoint, foreground, background)
} else {
backend.sprayRuneMask (
mask, destinationRectangle,
maskPoint, foreground, background)
}
} }
if backend.drawCellBounds { // underline
strokeRectangle ( if runeStyle & stone.StyleUnderline > 0 {
&image.Uniform { maxX := origin.X + backend.metrics.cellWidth
C: backend.config.Color(stone.ColorForeground), y :=
}, origin.Y -
backend.canvas, backend.metrics.descent
backend.boundsOfCell(x, y)) for x := origin.X; x < maxX; x ++ {
} backend.canvas.SetBGRA(x, y, foreground)
}
// cue a series of pointless optimizations
alphaMask, isAlpha := mask.(*image.Alpha)
if isAlpha {
backend.sprayRuneMaskAlpha (
alphaMask, destinationRectangle,
maskPoint, backend.colors[runeColor])
} else {
backend.sprayRuneMask (
mask, destinationRectangle,
maskPoint, backend.colors[runeColor])
} }
} }
func (backend *Backend) sprayRuneMask ( func (backend *Backend) sprayRuneMask (
mask image.Image, mask image.Image,
bounds image.Rectangle, bounds image.Rectangle,
maskPoint image.Point, maskPoint image.Point,
fill xgraphics.BGRA, fill xgraphics.BGRA,
background xgraphics.BGRA,
) { ) {
maxX := bounds.Max.X - bounds.Min.X maxX := bounds.Max.X - bounds.Min.X
maxY := bounds.Max.Y - bounds.Min.Y maxY := bounds.Max.Y - bounds.Min.Y
@ -148,7 +173,7 @@ func (backend *Backend) sprayRuneMask (
x + bounds.Min.X, x + bounds.Min.X,
y + bounds.Min.Y - backend.metrics.descent, y + bounds.Min.Y - backend.metrics.descent,
xgraphics.BlendBGRA ( xgraphics.BlendBGRA (
backend.colors[stone.ColorBackground], background,
xgraphics.BGRA { xgraphics.BGRA {
R: fill.R, R: fill.R,
G: fill.G, G: fill.G,
@ -159,10 +184,11 @@ func (backend *Backend) sprayRuneMask (
} }
func (backend *Backend) sprayRuneMaskAlpha ( func (backend *Backend) sprayRuneMaskAlpha (
mask *image.Alpha, mask *image.Alpha,
bounds image.Rectangle, bounds image.Rectangle,
maskPoint image.Point, maskPoint image.Point,
fill xgraphics.BGRA, fill xgraphics.BGRA,
background xgraphics.BGRA,
) { ) {
maxX := bounds.Max.X - bounds.Min.X maxX := bounds.Max.X - bounds.Min.X
maxY := bounds.Max.Y - bounds.Min.Y maxY := bounds.Max.Y - bounds.Min.Y
@ -174,7 +200,7 @@ func (backend *Backend) sprayRuneMaskAlpha (
x + bounds.Min.X, x + bounds.Min.X,
y + bounds.Min.Y - backend.metrics.descent, y + bounds.Min.Y - backend.metrics.descent,
xgraphics.BlendBGRA ( xgraphics.BlendBGRA (
backend.colors[stone.ColorBackground], background,
xgraphics.BGRA { xgraphics.BGRA {
R: fill.R, R: fill.R,
G: fill.G, G: fill.G,

View File

@ -21,11 +21,11 @@ const (
type Style uint8 type Style uint8
const ( const (
StyleNormal Style = iota StyleNormal Style = 0
StyleBold Style = iota >> 1 StyleBold Style = 1
StyleItalic StyleItalic Style = 2
StyleUnderline StyleUnderline Style = 4
StyleHighlight StyleHighlight Style = 8
StyleBoldItalic Style = StyleBold | StyleItalic StyleBoldItalic Style = StyleBold | StyleItalic
) )