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,24 +79,35 @@ 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)
if character >= 32 {
destinationRectangle, mask, maskPoint, _, ok := backend.font.face.Glyph ( destinationRectangle, mask, maskPoint, _, ok := backend.font.face.Glyph (
fixed.Point26_6 { fixed.Point26_6 {
X: fixed.I(origin.X), X: fixed.I(origin.X),
@ -101,9 +117,7 @@ func (backend *Backend) drawRune (
if !ok { if !ok {
strokeRectangle ( strokeRectangle (
&image.Uniform { &image.Uniform { C: foreground },
C: backend.config.Color(stone.ColorForeground),
},
backend.canvas, backend.canvas,
backend.boundsOfCell(x, y)) backend.boundsOfCell(x, y))
return return
@ -111,9 +125,7 @@ func (backend *Backend) drawRune (
if backend.drawCellBounds { if backend.drawCellBounds {
strokeRectangle ( strokeRectangle (
&image.Uniform { &image.Uniform { C: foreground },
C: backend.config.Color(stone.ColorForeground),
},
backend.canvas, backend.canvas,
backend.boundsOfCell(x, y)) backend.boundsOfCell(x, y))
} }
@ -123,11 +135,23 @@ func (backend *Backend) drawRune (
if isAlpha { if isAlpha {
backend.sprayRuneMaskAlpha ( backend.sprayRuneMaskAlpha (
alphaMask, destinationRectangle, alphaMask, destinationRectangle,
maskPoint, backend.colors[runeColor]) maskPoint, foreground, background)
} else { } else {
backend.sprayRuneMask ( backend.sprayRuneMask (
mask, destinationRectangle, mask, destinationRectangle,
maskPoint, backend.colors[runeColor]) maskPoint, foreground, background)
}
}
// underline
if runeStyle & stone.StyleUnderline > 0 {
maxX := origin.X + backend.metrics.cellWidth
y :=
origin.Y -
backend.metrics.descent
for x := origin.X; x < maxX; x ++ {
backend.canvas.SetBGRA(x, y, foreground)
}
} }
} }
@ -136,6 +160,7 @@ func (backend *Backend) sprayRuneMask (
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,
@ -163,6 +188,7 @@ func (backend *Backend) sprayRuneMaskAlpha (
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
) )