nasin/internal/styles/tss/tss.go

82 lines
1.7 KiB
Go

package tss
import "os"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/backend/style"
type Sheet struct {
Path string
Variables map[string] ValueList
Rules []Rule
flat bool
}
type Rule struct {
Selector Selector
Attrs map[string] []ValueList
}
type Selector struct {
Package string
Object string
Tags []string
}
type ValueList []Value
type Value interface {
value ()
}
type ValueNumber int
func (ValueNumber) value () { }
type ValueColor uint32
func (ValueColor) value () { }
func (value ValueColor) RGBA () (r, g, b, a uint32) {
// extract components
bits := uint32(value)
r = (bits & 0xFF000000) >> 24
g = (bits & 0x00FF0000) >> 16
b = (bits & 0x0000FF00) >> 8
a = (bits & 0x000000FF)
// extend to 16 bits per channel
r = r << 8 | r
g = g << 8 | g
b = b << 8 | b
a = a << 8 | a
// alpha premultiply
r = (r * a) / 0xFFFF
g = (g * a) / 0xFFFF
b = (b * a) / 0xFFFF
return
}
type ValueString string
func (ValueString) value () { }
type ValueKeyword string
func (ValueKeyword) value () { }
type ValueVariable string
func (ValueVariable) value () { }
type ValueCut struct { }
func (ValueCut) value () { }
// LoadFile loads the stylesheet from the specified file. This may return a
// parse.Error, so use parse.Format to print it.
func LoadFile (name string) (*style.Style, event.Cookie, error) {
// TODO check cache for gobbed sheet. if the cache is nonexistent or
// invalid, then open/load/cache.
file, err := os.Open(name)
if err != nil { return nil, nil, err }
defer file.Close()
sheet, err := Parse(Lex(name, file))
if err != nil { return nil, nil, err }
sheet.Path = name
return BuildStyle(sheet)
}