Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
Sasha Koshka | 45278fbb8b | |
Sasha Koshka | ef5a811140 | |
Sasha Koshka | e753fc11ca | |
Sasha Koshka | e4c7dcb2e1 | |
Sasha Koshka | f51f9ae5c5 | |
Sasha Koshka | 0462afdf11 | |
Sasha Koshka | 2fa4cc8da4 |
|
@ -49,11 +49,25 @@ func (backend *Backend) drawCells (forceRedraw bool) (areas []image.Rectangle) {
|
|||
|
||||
cell := backend.application.GetForRendering(x, y)
|
||||
content := cell.Rune()
|
||||
style := cell.Style()
|
||||
|
||||
if forceRedraw && content < 32 { continue }
|
||||
if
|
||||
forceRedraw &&
|
||||
content < 32 &&
|
||||
style & (
|
||||
stone.StyleHighlight |
|
||||
stone.StyleUnderline) == 0 {
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -74,115 +88,205 @@ func (backend *Backend) drawRune (
|
|||
x, y int,
|
||||
character rune,
|
||||
runeColor stone.Color,
|
||||
runeStyle stone.Style,
|
||||
drawBackground bool,
|
||||
) {
|
||||
// TODO: cache these draws as non-transparent buffers with the
|
||||
// application background color as the background. that way, we won't
|
||||
// need to redraw the characters *or* composite them.
|
||||
|
||||
if drawBackground {
|
||||
face := backend.font.normal
|
||||
|
||||
highlight := runeStyle & stone.StyleHighlight > 0
|
||||
bold := runeStyle & stone.StyleBold > 0
|
||||
italic := runeStyle & stone.StyleItalic > 0
|
||||
|
||||
boldTransform := false
|
||||
italicTransform := false
|
||||
|
||||
switch {
|
||||
case bold && italic:
|
||||
if backend.font.boldItalic == nil {
|
||||
switch {
|
||||
case
|
||||
backend.font.bold == nil && backend.font.italic != nil,
|
||||
backend.font.bold != nil && backend.font.italic != nil:
|
||||
|
||||
boldTransform = true
|
||||
face = backend.font.italic
|
||||
case backend.font.italic == nil && backend.font.bold != nil:
|
||||
italicTransform = true
|
||||
face = backend.font.bold
|
||||
default:
|
||||
boldTransform = true
|
||||
italicTransform = true
|
||||
}
|
||||
} else {
|
||||
face = backend.font.boldItalic
|
||||
}
|
||||
case bold:
|
||||
if backend.font.bold == nil {
|
||||
boldTransform = true
|
||||
} else {
|
||||
face = backend.font.bold
|
||||
}
|
||||
case italic:
|
||||
if backend.font.italic == nil {
|
||||
italicTransform = true
|
||||
} else {
|
||||
face = backend.font.italic
|
||||
}
|
||||
}
|
||||
|
||||
var background xgraphics.BGRA
|
||||
var foreground xgraphics.BGRA
|
||||
|
||||
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 (
|
||||
&image.Uniform {
|
||||
C: backend.config.Color(stone.ColorBackground),
|
||||
},
|
||||
&image.Uniform { C: background },
|
||||
backend.canvas,
|
||||
backend.boundsOfCell(x, y))
|
||||
}
|
||||
|
||||
if character < 32 { return }
|
||||
|
||||
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 {
|
||||
strokeRectangle (
|
||||
&image.Uniform {
|
||||
C: backend.config.Color(stone.ColorForeground),
|
||||
if character >= 32 {
|
||||
destinationRectangle, mask, maskPoint, _, ok := face.Glyph (
|
||||
fixed.Point26_6 {
|
||||
X: fixed.I(origin.X),
|
||||
Y: fixed.I(origin.Y),
|
||||
},
|
||||
backend.canvas,
|
||||
backend.boundsOfCell(x, y))
|
||||
return
|
||||
character)
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
// alphaMask, isAlpha := mask.(*image.Alpha)
|
||||
// if isAlpha {
|
||||
// backend.sprayRuneMaskAlpha (
|
||||
// alphaMask, destinationRectangle,
|
||||
// maskPoint, foreground, background)
|
||||
// } else {
|
||||
backend.sprayRuneMask (
|
||||
mask, destinationRectangle,
|
||||
maskPoint, foreground, background,
|
||||
italicTransform, boldTransform)
|
||||
// }
|
||||
}
|
||||
|
||||
if backend.drawCellBounds {
|
||||
strokeRectangle (
|
||||
&image.Uniform {
|
||||
C: backend.config.Color(stone.ColorForeground),
|
||||
},
|
||||
backend.canvas,
|
||||
backend.boundsOfCell(x, y))
|
||||
}
|
||||
|
||||
// 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])
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (backend *Backend) sprayRuneMask (
|
||||
mask image.Image,
|
||||
bounds image.Rectangle,
|
||||
maskPoint image.Point,
|
||||
fill xgraphics.BGRA,
|
||||
mask image.Image,
|
||||
bounds image.Rectangle,
|
||||
maskPoint image.Point,
|
||||
fill xgraphics.BGRA,
|
||||
background xgraphics.BGRA,
|
||||
italic bool,
|
||||
bold bool,
|
||||
) {
|
||||
maxX := bounds.Max.X - bounds.Min.X
|
||||
maxY := bounds.Max.Y - bounds.Min.Y
|
||||
|
||||
for y := 0; y < maxY; y ++ {
|
||||
for x := 0; x < maxX; x ++ {
|
||||
_, _, _,
|
||||
alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA()
|
||||
backend.canvas.SetBGRA (
|
||||
x + bounds.Min.X,
|
||||
y + bounds.Min.Y - backend.metrics.descent,
|
||||
xgraphics.BlendBGRA (
|
||||
backend.colors[stone.ColorBackground],
|
||||
xgraphics.BGRA {
|
||||
R: fill.R,
|
||||
G: fill.G,
|
||||
B: fill.B,
|
||||
A: uint8(alpha >> 8),
|
||||
}))
|
||||
}}
|
||||
var previousAlpha uint32
|
||||
offset := 0
|
||||
if italic {
|
||||
offset = (maxY - y) / 4
|
||||
}
|
||||
for x := 0; x < maxX; x ++ {
|
||||
_, _, _,
|
||||
alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA()
|
||||
currentAlpha := alpha
|
||||
if bold && previousAlpha > alpha {
|
||||
alpha = previousAlpha
|
||||
}
|
||||
backend.canvas.SetBGRA (
|
||||
x + bounds.Min.X + offset,
|
||||
y + bounds.Min.Y - backend.metrics.descent,
|
||||
xgraphics.BlendBGRA (
|
||||
background,
|
||||
xgraphics.BGRA {
|
||||
R: fill.R,
|
||||
G: fill.G,
|
||||
B: fill.B,
|
||||
A: uint8(alpha >> 8),
|
||||
}))
|
||||
previousAlpha = currentAlpha
|
||||
}
|
||||
|
||||
if bold {
|
||||
backend.canvas.SetBGRA (
|
||||
bounds.Max.X + offset,
|
||||
y + bounds.Min.Y - backend.metrics.descent,
|
||||
xgraphics.BlendBGRA (
|
||||
background,
|
||||
xgraphics.BGRA {
|
||||
R: fill.R,
|
||||
G: fill.G,
|
||||
B: fill.B,
|
||||
A: uint8(previousAlpha >> 8),
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (backend *Backend) sprayRuneMaskAlpha (
|
||||
mask *image.Alpha,
|
||||
bounds image.Rectangle,
|
||||
maskPoint image.Point,
|
||||
fill xgraphics.BGRA,
|
||||
) {
|
||||
maxX := bounds.Max.X - bounds.Min.X
|
||||
maxY := bounds.Max.Y - bounds.Min.Y
|
||||
|
||||
for y := 0; y < maxY; y ++ {
|
||||
for x := 0; x < maxX; x ++ {
|
||||
alpha := mask.AlphaAt(x + maskPoint.X, y + maskPoint.Y).A
|
||||
backend.canvas.SetBGRA (
|
||||
x + bounds.Min.X,
|
||||
y + bounds.Min.Y - backend.metrics.descent,
|
||||
xgraphics.BlendBGRA (
|
||||
backend.colors[stone.ColorBackground],
|
||||
xgraphics.BGRA {
|
||||
R: fill.R,
|
||||
G: fill.G,
|
||||
B: fill.B,
|
||||
A: alpha,
|
||||
}))
|
||||
}}
|
||||
}
|
||||
// func (backend *Backend) sprayRuneMaskAlpha (
|
||||
// mask *image.Alpha,
|
||||
// bounds image.Rectangle,
|
||||
// maskPoint image.Point,
|
||||
// fill xgraphics.BGRA,
|
||||
// background xgraphics.BGRA,
|
||||
// ) {
|
||||
// maxX := bounds.Max.X - bounds.Min.X
|
||||
// maxY := bounds.Max.Y - bounds.Min.Y
|
||||
//
|
||||
// for y := 0; y < maxY; y ++ {
|
||||
// for x := 0; x < maxX; x ++ {
|
||||
// alpha := mask.AlphaAt(x + maskPoint.X, y + maskPoint.Y).A
|
||||
// backend.canvas.SetBGRA (
|
||||
// x + bounds.Min.X,
|
||||
// y + bounds.Min.Y - backend.metrics.descent,
|
||||
// xgraphics.BlendBGRA (
|
||||
// background,
|
||||
// xgraphics.BGRA {
|
||||
// R: fill.R,
|
||||
// G: fill.G,
|
||||
// B: fill.B,
|
||||
// A: alpha,
|
||||
// }))
|
||||
// }}
|
||||
// }
|
||||
|
||||
func fillRectangle (
|
||||
source image.Image,
|
||||
|
|
|
@ -32,11 +32,20 @@ func factory (
|
|||
}
|
||||
|
||||
// load font
|
||||
backend.font.face = findAndLoadFont (
|
||||
backend.config.FontName(),
|
||||
backend.font.normal = findAndLoadFont (
|
||||
backend.config.FontNameNormal(),
|
||||
float64(backend.config.FontSize()))
|
||||
if backend.font.face == nil {
|
||||
backend.font.face = basicfont.Face7x13
|
||||
backend.font.bold = findAndLoadFont (
|
||||
backend.config.FontNameBold(),
|
||||
float64(backend.config.FontSize()))
|
||||
backend.font.italic = findAndLoadFont (
|
||||
backend.config.FontNameItalic(),
|
||||
float64(backend.config.FontSize()))
|
||||
backend.font.boldItalic = findAndLoadFont (
|
||||
backend.config.FontNameBoldItalic(),
|
||||
float64(backend.config.FontSize()))
|
||||
if backend.font.normal == nil {
|
||||
backend.font.normal = basicfont.Face7x13
|
||||
}
|
||||
|
||||
// pre-calculate colors
|
||||
|
@ -56,8 +65,8 @@ func factory (
|
|||
}
|
||||
|
||||
// calculate metrics
|
||||
metrics := backend.font.face.Metrics()
|
||||
glyphAdvance, _ := backend.font.face.GlyphAdvance('M')
|
||||
metrics := backend.font.normal.Metrics()
|
||||
glyphAdvance, _ := backend.font.normal.GlyphAdvance('M')
|
||||
backend.metrics.cellWidth = glyphAdvance.Round()
|
||||
backend.metrics.cellHeight = metrics.Height.Round()
|
||||
backend.metrics.descent = metrics.Descent.Round()
|
||||
|
|
|
@ -27,7 +27,10 @@ type Backend struct {
|
|||
lock sync.Mutex
|
||||
|
||||
font struct {
|
||||
face font.Face
|
||||
normal font.Face
|
||||
bold font.Face
|
||||
italic font.Face
|
||||
boldItalic font.Face
|
||||
}
|
||||
|
||||
colors [8]xgraphics.BGRA
|
||||
|
|
12
buffer.go
12
buffer.go
|
@ -21,11 +21,11 @@ const (
|
|||
type Style uint8
|
||||
|
||||
const (
|
||||
StyleNormal Style = iota
|
||||
StyleBold Style = iota >> 1
|
||||
StyleItalic
|
||||
StyleUnderline
|
||||
StyleHighlight
|
||||
StyleNormal Style = 0
|
||||
StyleBold Style = 1
|
||||
StyleItalic Style = 2
|
||||
StyleUnderline Style = 4
|
||||
StyleHighlight Style = 8
|
||||
StyleBoldItalic Style = StyleBold | StyleItalic
|
||||
)
|
||||
|
||||
|
@ -44,7 +44,7 @@ func (cell Cell) Color () (color Color) {
|
|||
}
|
||||
|
||||
// Style returns the styling information associated with the cell
|
||||
func (cell Cell) Style (style Style) {
|
||||
func (cell Cell) Style () (style Style) {
|
||||
style = cell.style
|
||||
return
|
||||
}
|
||||
|
|
54
config.go
54
config.go
|
@ -12,7 +12,10 @@ type Config struct {
|
|||
padding int
|
||||
center bool
|
||||
fontSize int
|
||||
fontName string
|
||||
fontNameNormal string
|
||||
fontNameBold string
|
||||
fontNameItalic string
|
||||
fontNameBoldItalic string
|
||||
}
|
||||
|
||||
// Color returns the color value at the specified index.
|
||||
|
@ -41,9 +44,27 @@ func (public *Config) FontSize () (fontSize int) {
|
|||
return
|
||||
}
|
||||
|
||||
// FontName specifies the name of the font to use.
|
||||
func (public *Config) FontName () (fontName string) {
|
||||
fontName = public.fontName
|
||||
// FontNameNormal specifies the name of the font to use for normal text.
|
||||
func (public *Config) FontNameNormal () (fontName string) {
|
||||
fontName = public.fontNameNormal
|
||||
return
|
||||
}
|
||||
|
||||
// FontNameBold specifies the name of the font to use for bold text.
|
||||
func (public *Config) FontNameBold () (fontName string) {
|
||||
fontName = public.fontNameBold
|
||||
return
|
||||
}
|
||||
|
||||
// FontName specifies the name of the font to use for text.
|
||||
func (public *Config) FontNameItalic () (fontName string) {
|
||||
fontName = public.fontNameItalic
|
||||
return
|
||||
}
|
||||
|
||||
// FontName specifies the name of the font to use for text.
|
||||
func (public *Config) FontNameBoldItalic () (fontName string) {
|
||||
fontName = public.fontNameBoldItalic
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -51,6 +72,9 @@ func (public *Config) load () {
|
|||
public.private = config.Config {
|
||||
LegalParameters: map[string] config.Type {
|
||||
"fontNormal": config.TypeString,
|
||||
"fontBold": config.TypeString,
|
||||
"fontItalic": config.TypeString,
|
||||
"fontBoldItalic": config.TypeString,
|
||||
"fontSize": config.TypeInteger,
|
||||
"padding": config.TypeInteger,
|
||||
"center": config.TypeBoolean,
|
||||
|
@ -65,10 +89,13 @@ func (public *Config) load () {
|
|||
},
|
||||
|
||||
Parameters: map[string] any {
|
||||
"fontNormal": "",
|
||||
"fontSize": 11,
|
||||
"padding": 2,
|
||||
"center": false,
|
||||
"fontNormal": "",
|
||||
"fontBold": "",
|
||||
"fontItalic": "",
|
||||
"fontBoldItalic": "",
|
||||
"fontSize": 11,
|
||||
"padding": 2,
|
||||
"center": false,
|
||||
"colorBackground":
|
||||
color.RGBA { R: 0, G: 0, B: 0, A: 0 },
|
||||
"colorForeground":
|
||||
|
@ -91,10 +118,13 @@ func (public *Config) load () {
|
|||
public.private.Load("stone")
|
||||
params := public.private.Parameters
|
||||
|
||||
public.fontName = params["fontNormal"].(string)
|
||||
public.fontSize = params["fontSize"].(int)
|
||||
public.padding = params["padding"].(int)
|
||||
public.center = params["center"].(bool)
|
||||
public.fontNameNormal = params["fontNormal"].(string)
|
||||
public.fontNameBold = params["fontBold"].(string)
|
||||
public.fontNameItalic = params["fontItalic"].(string)
|
||||
public.fontNameBoldItalic = params["fontBoldItalic"].(string)
|
||||
public.fontSize = params["fontSize"].(int)
|
||||
public.padding = params["padding"].(int)
|
||||
public.center = params["center"].(bool)
|
||||
|
||||
public.colors[ColorBackground] = params["colorBackground"].(color.RGBA)
|
||||
public.colors[ColorForeground] = params["colorForeground"].(color.RGBA)
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
package main
|
||||
|
||||
import "os"
|
||||
import "fmt"
|
||||
import "image"
|
||||
import "math/rand"
|
||||
import _ "image/png"
|
||||
import "git.tebibyte.media/sashakoshka/stone"
|
||||
import _ "git.tebibyte.media/sashakoshka/stone/backends/x"
|
||||
|
||||
var application = &stone.Application { }
|
||||
|
||||
func main () {
|
||||
application.SetTitle("style demo")
|
||||
application.SetSize(11, 8)
|
||||
|
||||
iconFile16, err := os.Open("assets/scaffold16.png")
|
||||
if err != nil { panic(err) }
|
||||
icon16, _, err := image.Decode(iconFile16)
|
||||
if err != nil { panic(err) }
|
||||
iconFile16.Close()
|
||||
iconFile32, err := os.Open("assets/scaffold32.png")
|
||||
if err != nil { panic(err) }
|
||||
icon32, _, err := image.Decode(iconFile32)
|
||||
if err != nil { panic(err) }
|
||||
iconFile16.Close()
|
||||
|
||||
application.SetIcon([]image.Image { icon16, icon32 })
|
||||
|
||||
application.OnStart(redraw)
|
||||
application.OnResize(redraw)
|
||||
application.OnPress(onPress)
|
||||
|
||||
err = application.Run()
|
||||
if err != nil { panic(err) }
|
||||
}
|
||||
|
||||
func onPress (button stone.Button, modifiers stone.Modifiers) {
|
||||
redraw()
|
||||
application.Draw()
|
||||
}
|
||||
|
||||
func redraw () {
|
||||
width, _ := application.Size()
|
||||
application.SetDot(0, 0)
|
||||
fmt.Fprint (
|
||||
application,
|
||||
"normal\n",
|
||||
"bold\n",
|
||||
"italic\n",
|
||||
"underline\n",
|
||||
"all 3\n",
|
||||
"highlighted\n",
|
||||
"all 4\n",
|
||||
"highlight?")
|
||||
fillStyle(0, width, stone.StyleNormal)
|
||||
fillStyle(1, width, stone.StyleBold)
|
||||
fillStyle(2, width, stone.StyleItalic)
|
||||
fillStyle(3, width, stone.StyleUnderline)
|
||||
fillStyle(4, width, stone.StyleBoldItalic | stone.StyleUnderline)
|
||||
fillStyle(5, width, stone.StyleHighlight)
|
||||
fillStyle(6, width, stone.StyleBoldItalic | stone.StyleUnderline |
|
||||
stone.StyleHighlight)
|
||||
|
||||
if rand.Int() % 2 == 0 {
|
||||
fillStyle(7, width, stone.StyleNormal)
|
||||
} else {
|
||||
fillStyle(7, width, stone.StyleHighlight)
|
||||
}
|
||||
}
|
||||
|
||||
func fillStyle (yOffset, width int, style stone.Style) {
|
||||
for x := 0; x < width; x ++ {
|
||||
application.SetStyle(x, yOffset, style)
|
||||
application.SetColor(x, yOffset, stone.Color(x % 7 + 1))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue