Compare commits

...

8 Commits
v0.4.0 ... main

7 changed files with 336 additions and 113 deletions

View File

@ -49,11 +49,25 @@ func (backend *Backend) drawCells (forceRedraw bool) (areas []image.Rectangle) {
cell := backend.application.GetForRendering(x, y) cell := backend.application.GetForRendering(x, y)
content := cell.Rune() 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)) 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,115 +88,205 @@ 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 { 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 ( 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 := 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))
}
// 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 { // 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,
italic bool,
bold bool,
) { ) {
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
for y := 0; y < maxY; y ++ { for y := 0; y < maxY; y ++ {
for x := 0; x < maxX; x ++ { var previousAlpha uint32
_, _, _, offset := 0
alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA() if italic {
backend.canvas.SetBGRA ( offset = (maxY - y) / 4
x + bounds.Min.X, }
y + bounds.Min.Y - backend.metrics.descent, for x := 0; x < maxX; x ++ {
xgraphics.BlendBGRA ( _, _, _,
backend.colors[stone.ColorBackground], alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA()
xgraphics.BGRA { currentAlpha := alpha
R: fill.R, if bold && previousAlpha > alpha {
G: fill.G, alpha = previousAlpha
B: fill.B, }
A: uint8(alpha >> 8), 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 ( // 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 // ) {
maxY := bounds.Max.Y - bounds.Min.Y // 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 ++ { // for y := 0; y < maxY; y ++ {
alpha := mask.AlphaAt(x + maskPoint.X, y + maskPoint.Y).A // for x := 0; x < maxX; x ++ {
backend.canvas.SetBGRA ( // alpha := mask.AlphaAt(x + maskPoint.X, y + maskPoint.Y).A
x + bounds.Min.X, // backend.canvas.SetBGRA (
y + bounds.Min.Y - backend.metrics.descent, // x + bounds.Min.X,
xgraphics.BlendBGRA ( // y + bounds.Min.Y - backend.metrics.descent,
backend.colors[stone.ColorBackground], // xgraphics.BlendBGRA (
xgraphics.BGRA { // background,
R: fill.R, // xgraphics.BGRA {
G: fill.G, // R: fill.R,
B: fill.B, // G: fill.G,
A: alpha, // B: fill.B,
})) // A: alpha,
}} // }))
} // }}
// }
func fillRectangle ( func fillRectangle (
source image.Image, source image.Image,

View File

@ -32,11 +32,20 @@ func factory (
} }
// load font // load font
backend.font.face = findAndLoadFont ( backend.font.normal = findAndLoadFont (
backend.config.FontName(), backend.config.FontNameNormal(),
float64(backend.config.FontSize())) float64(backend.config.FontSize()))
if backend.font.face == nil { backend.font.bold = findAndLoadFont (
backend.font.face = basicfont.Face7x13 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 // pre-calculate colors
@ -56,8 +65,8 @@ func factory (
} }
// calculate metrics // calculate metrics
metrics := backend.font.face.Metrics() metrics := backend.font.normal.Metrics()
glyphAdvance, _ := backend.font.face.GlyphAdvance('M') glyphAdvance, _ := backend.font.normal.GlyphAdvance('M')
backend.metrics.cellWidth = glyphAdvance.Round() backend.metrics.cellWidth = glyphAdvance.Round()
backend.metrics.cellHeight = metrics.Height.Round() backend.metrics.cellHeight = metrics.Height.Round()
backend.metrics.descent = metrics.Descent.Round() backend.metrics.descent = metrics.Descent.Round()

View File

@ -27,7 +27,10 @@ type Backend struct {
lock sync.Mutex lock sync.Mutex
font struct { font struct {
face font.Face normal font.Face
bold font.Face
italic font.Face
boldItalic font.Face
} }
colors [8]xgraphics.BGRA colors [8]xgraphics.BGRA

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
) )
@ -44,7 +44,7 @@ func (cell Cell) Color () (color Color) {
} }
// Style returns the styling information associated with the cell // Style returns the styling information associated with the cell
func (cell Cell) Style (style Style) { func (cell Cell) Style () (style Style) {
style = cell.style style = cell.style
return return
} }

View File

@ -12,7 +12,10 @@ type Config struct {
padding int padding int
center bool center bool
fontSize int fontSize int
fontName string fontNameNormal string
fontNameBold string
fontNameItalic string
fontNameBoldItalic string
} }
// Color returns the color value at the specified index. // Color returns the color value at the specified index.
@ -41,9 +44,27 @@ func (public *Config) FontSize () (fontSize int) {
return return
} }
// FontName specifies the name of the font to use. // FontNameNormal specifies the name of the font to use for normal text.
func (public *Config) FontName () (fontName string) { func (public *Config) FontNameNormal () (fontName string) {
fontName = public.fontName 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 return
} }
@ -51,6 +72,9 @@ func (public *Config) load () {
public.private = config.Config { public.private = config.Config {
LegalParameters: map[string] config.Type { LegalParameters: map[string] config.Type {
"fontNormal": config.TypeString, "fontNormal": config.TypeString,
"fontBold": config.TypeString,
"fontItalic": config.TypeString,
"fontBoldItalic": config.TypeString,
"fontSize": config.TypeInteger, "fontSize": config.TypeInteger,
"padding": config.TypeInteger, "padding": config.TypeInteger,
"center": config.TypeBoolean, "center": config.TypeBoolean,
@ -65,10 +89,13 @@ func (public *Config) load () {
}, },
Parameters: map[string] any { Parameters: map[string] any {
"fontNormal": "", "fontNormal": "",
"fontSize": 11, "fontBold": "",
"padding": 2, "fontItalic": "",
"center": false, "fontBoldItalic": "",
"fontSize": 11,
"padding": 2,
"center": false,
"colorBackground": "colorBackground":
color.RGBA { R: 0, G: 0, B: 0, A: 0 }, color.RGBA { R: 0, G: 0, B: 0, A: 0 },
"colorForeground": "colorForeground":
@ -91,10 +118,13 @@ func (public *Config) load () {
public.private.Load("stone") public.private.Load("stone")
params := public.private.Parameters params := public.private.Parameters
public.fontName = params["fontNormal"].(string) public.fontNameNormal = params["fontNormal"].(string)
public.fontSize = params["fontSize"].(int) public.fontNameBold = params["fontBold"].(string)
public.padding = params["padding"].(int) public.fontNameItalic = params["fontItalic"].(string)
public.center = params["center"].(bool) 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[ColorBackground] = params["colorBackground"].(color.RGBA)
public.colors[ColorForeground] = params["colorForeground"].(color.RGBA) public.colors[ColorForeground] = params["colorForeground"].(color.RGBA)

View File

@ -29,14 +29,14 @@ func init () {
configDirs = append(strings.Split(configDirsString, ":"), configHome) configDirs = append(strings.Split(configDirsString, ":"), configHome)
configHome = os.Getenv("XDG_DATA_HOME") dataHome = os.Getenv("XDG_DATA_HOME")
if configHome == "" { if dataHome == "" {
configHome = filepath.Join(homeDirectory, "/.local/share/") dataHome = filepath.Join(homeDirectory, "/.local/share/")
} }
cacheHome = os.Getenv("XDG_CACHE_HOME") cacheHome = os.Getenv("XDG_CACHE_HOME")
if cacheHome == "" { if cacheHome == "" {
configHome = filepath.Join(homeDirectory, "/.cache/") cacheHome = filepath.Join(homeDirectory, "/.cache/")
} }
} }

77
examples/style/main.go Normal file
View File

@ -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))
}
}