2024-07-28 23:50:51 -06:00
|
|
|
package tss
|
|
|
|
|
|
|
|
import "os"
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/event"
|
|
|
|
|
2024-07-29 13:13:02 -06:00
|
|
|
type Sheet struct {
|
|
|
|
Variables map[string] ValueList
|
|
|
|
Rules []Rule
|
|
|
|
flat bool
|
2024-07-28 23:50:51 -06:00
|
|
|
}
|
|
|
|
|
2024-07-29 13:13:02 -06:00
|
|
|
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)
|
2024-07-29 13:45:17 -06:00
|
|
|
r = (bits & 0xFF000000) >> 24
|
|
|
|
g = (bits & 0x00FF0000) >> 16
|
|
|
|
b = (bits & 0x0000FF00) >> 8
|
|
|
|
a = (bits & 0x000000FF)
|
2024-07-29 13:13:02 -06:00
|
|
|
// extend to 16 bits per channel
|
|
|
|
r = r << 8 | r
|
|
|
|
g = g << 8 | g
|
|
|
|
b = b << 8 | b
|
|
|
|
a = a << 8 | a
|
|
|
|
// alpha premultiply
|
2024-07-29 13:45:17 -06:00
|
|
|
r = (r * a) / 0xFFFF
|
|
|
|
g = (g * a) / 0xFFFF
|
|
|
|
b = (b * a) / 0xFFFF
|
2024-07-29 13:13:02 -06:00
|
|
|
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.
|
2024-07-28 23:50:51 -06:00
|
|
|
func LoadFile (name string) (*tomo.Style, event.Cookie, error) {
|
|
|
|
// TODO check cache for gobbed sheet. if the cache is nonexistent or
|
|
|
|
// invalid, then open/load/cache.
|
2024-07-29 13:13:02 -06:00
|
|
|
|
2024-07-28 23:50:51 -06:00
|
|
|
file, err := os.Open(name)
|
|
|
|
if err != nil { return nil, nil, err }
|
|
|
|
defer file.Close()
|
2024-07-29 13:13:02 -06:00
|
|
|
|
|
|
|
sheet, err := Parse(Lex(name, file))
|
|
|
|
if err != nil { return nil, nil, err }
|
|
|
|
return BuildStyle(sheet)
|
2024-07-28 23:50:51 -06:00
|
|
|
}
|