We load fonts but it's a bit broken
This commit is contained in:
@@ -1,20 +1,44 @@
|
||||
package tss
|
||||
|
||||
import "os"
|
||||
import "fmt"
|
||||
import "image"
|
||||
import "errors"
|
||||
import "image/color"
|
||||
import "golang.org/x/image/font"
|
||||
import "github.com/flopp/go-findfont"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "golang.org/x/image/font/opentype"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
|
||||
type styleBuilder struct {
|
||||
sheet Sheet
|
||||
fonts map[string] *opentype.Font
|
||||
faces map[faceKey] font.Face
|
||||
}
|
||||
|
||||
type faceKey struct {
|
||||
name string
|
||||
size int
|
||||
}
|
||||
|
||||
// BuildStyle builds a Tomo style from the specified sheet. Resources associated
|
||||
// with it (such as textures) can be freed by closing the returned cookie.
|
||||
func BuildStyle (sheet Sheet) (*tomo.Style, event.Cookie, error) {
|
||||
err := sheet.Flatten()
|
||||
builder := &styleBuilder {
|
||||
sheet: sheet,
|
||||
fonts: make(map[string] *opentype.Font),
|
||||
faces: make(map[faceKey] font.Face),
|
||||
}
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) {
|
||||
err := this.sheet.Flatten()
|
||||
if err != nil { return nil, nil, err }
|
||||
|
||||
getColor := func (name string) color.Color {
|
||||
if list, ok := sheet.Variables[name]; ok {
|
||||
if list, ok := this.sheet.Variables[name]; ok {
|
||||
if len(list) > 0 {
|
||||
if col, ok := list[0].(ValueColor); ok {
|
||||
return col
|
||||
@@ -26,7 +50,7 @@ func BuildStyle (sheet Sheet) (*tomo.Style, event.Cookie, error) {
|
||||
|
||||
cookies := []event.Cookie { }
|
||||
style := &tomo.Style {
|
||||
Rules: make([]tomo.Rule, len(sheet.Rules)),
|
||||
Rules: make([]tomo.Rule, len(this.sheet.Rules)),
|
||||
Colors: map[tomo.Color] color.Color {
|
||||
tomo.ColorBackground: getColor("ColorBackground"),
|
||||
tomo.ColorForeground: getColor("ColorForeground"),
|
||||
@@ -36,7 +60,7 @@ func BuildStyle (sheet Sheet) (*tomo.Style, event.Cookie, error) {
|
||||
},
|
||||
}
|
||||
|
||||
for index, rule := range sheet.Rules {
|
||||
for index, rule := range this.sheet.Rules {
|
||||
styleRule := tomo.Rule {
|
||||
Role: tomo.Role {
|
||||
Package: rule.Selector.Package,
|
||||
@@ -46,7 +70,7 @@ func BuildStyle (sheet Sheet) (*tomo.Style, event.Cookie, error) {
|
||||
Set: make(tomo.AttrSet),
|
||||
}
|
||||
for name, attr := range rule.Attrs {
|
||||
styleAttr, cookie, err := buildAttr(name, attr)
|
||||
styleAttr, cookie, err := this.buildAttr(name, attr)
|
||||
if err != nil { return nil, nil, err }
|
||||
styleRule.Set.Add(styleAttr)
|
||||
if cookie != nil {
|
||||
@@ -56,10 +80,16 @@ func BuildStyle (sheet Sheet) (*tomo.Style, event.Cookie, error) {
|
||||
style.Rules[index] = styleRule
|
||||
}
|
||||
|
||||
// TODO include all the faces in this.faces (ONCE EACH) in the
|
||||
// multicookie
|
||||
|
||||
return style, event.MultiCookie(cookies...), nil
|
||||
}
|
||||
|
||||
func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error) {
|
||||
func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error) {
|
||||
errWrongType := func () error {
|
||||
return errors.New(fmt.Sprintf("wrong type for %s attribute", name))
|
||||
}
|
||||
expectSingle := func () error {
|
||||
if len(attr) != 1 {
|
||||
return errors.New(fmt.Sprintf (
|
||||
@@ -81,9 +111,7 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
expectNumbers := func (list ValueList) error {
|
||||
for _, value := range list {
|
||||
if _, ok := value.(ValueNumber); ok { continue }
|
||||
return errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return errWrongType()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -94,9 +122,7 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
nums[index] = int(value)
|
||||
continue
|
||||
}
|
||||
return nil, errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return nil, errWrongType()
|
||||
}
|
||||
return nums, nil
|
||||
}
|
||||
@@ -113,9 +139,7 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
continue
|
||||
}
|
||||
}
|
||||
return nil, errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return nil, errWrongType()
|
||||
}
|
||||
return bools, nil
|
||||
}
|
||||
@@ -146,9 +170,7 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
if col, ok := attr[0][0].(ValueColor); ok {
|
||||
return tomo.AColor(col), nil, nil
|
||||
}
|
||||
return nil, nil, errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return nil, nil, errWrongType()
|
||||
|
||||
case "Texture":
|
||||
// TODO load image from file
|
||||
@@ -165,9 +187,7 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
"unknown texture mode: %s",
|
||||
keyword))
|
||||
}
|
||||
return nil, nil, errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return nil, nil, errWrongType()
|
||||
|
||||
case "Border":
|
||||
attrBorder, err := buildAttrBorder(attr)
|
||||
@@ -207,9 +227,7 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
if col, ok := attr[0][0].(ValueColor); ok {
|
||||
return tomo.ATextColor(col), nil, nil
|
||||
}
|
||||
return nil, nil, errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return nil, nil, errWrongType()
|
||||
|
||||
case "DotColor":
|
||||
err := expectSingleSingle()
|
||||
@@ -217,12 +235,24 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
if col, ok := attr[0][0].(ValueColor); ok {
|
||||
return tomo.ADotColor(col), nil, nil
|
||||
}
|
||||
return nil, nil, errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return nil, nil, errWrongType()
|
||||
|
||||
case "Face":
|
||||
// TODO: load font from file with some parameters
|
||||
err := expectSingle()
|
||||
if err != nil { return nil, nil, err }
|
||||
list := attr[0]
|
||||
if len(list) != 2 {
|
||||
return nil, nil, errors.New(fmt.Sprintf (
|
||||
"%s attribute requires exactly two values",
|
||||
name))
|
||||
}
|
||||
name, ok := list[0].(ValueString)
|
||||
if !ok { return nil, nil, errWrongType() }
|
||||
size, ok := list[1].(ValueNumber)
|
||||
if !ok { return nil, nil, errWrongType() }
|
||||
face, err := this.face(string(name), int(size))
|
||||
if err != nil { return nil, nil, err }
|
||||
return tomo.AFace(face), nil, nil
|
||||
|
||||
case "Wrap":
|
||||
err := expectSingleSingle()
|
||||
@@ -233,9 +263,7 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
case "false": return tomo.AWrap(false), nil, nil
|
||||
}
|
||||
}
|
||||
return nil, nil, errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return nil, nil, errWrongType()
|
||||
|
||||
case "Align":
|
||||
err := expectSingle()
|
||||
@@ -250,18 +278,16 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
for index, value := range list {
|
||||
if keyword, ok := value.(ValueKeyword); ok {
|
||||
switch keyword {
|
||||
case "start": aligns[index] = tomo.AlignStart
|
||||
case "middle": aligns[index] = tomo.AlignMiddle
|
||||
case "end": aligns[index] = tomo.AlignEnd
|
||||
case "even": aligns[index] = tomo.AlignEven
|
||||
case "start": aligns[index] = tomo.AlignStart; continue
|
||||
case "middle": aligns[index] = tomo.AlignMiddle; continue
|
||||
case "end": aligns[index] = tomo.AlignEnd; continue
|
||||
case "even": aligns[index] = tomo.AlignEven; continue
|
||||
default: return nil, nil, errors.New(fmt.Sprintf (
|
||||
"unknown texture mode: %s",
|
||||
keyword))
|
||||
}
|
||||
}
|
||||
return nil, nil, errors.New(fmt.Sprintf (
|
||||
"wrong type for %s attribute",
|
||||
name))
|
||||
return nil, nil, errWrongType()
|
||||
}
|
||||
return tomo.AAlign(aligns[0], aligns[1]), nil, nil
|
||||
|
||||
@@ -285,6 +311,36 @@ func buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error)
|
||||
return nil, nil, errors.New(fmt.Sprintf("unimplemented attribute name %s", name))
|
||||
}
|
||||
|
||||
func (this *styleBuilder) face (name string, size int) (font.Face, error) {
|
||||
key := faceKey { name: name, size: size }
|
||||
face, ok := this.faces[key]
|
||||
if ok { return face, nil }
|
||||
|
||||
font, ok := this.fonts[name]
|
||||
if !ok {
|
||||
path, err := findfont.Find(name)
|
||||
if err != nil { return nil, err }
|
||||
file, err := os.Open(path)
|
||||
if err != nil { return nil, err }
|
||||
defer file.Close()
|
||||
fnt, err := opentype.ParseReaderAt(file)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf (
|
||||
"could not load font %s: %v", path, err))
|
||||
}
|
||||
font = fnt
|
||||
this.fonts[name] = font
|
||||
}
|
||||
|
||||
face, err := opentype.NewFace(font, &opentype.FaceOptions {
|
||||
Size: float64(size),
|
||||
DPI: 96, // TODO: replace this with the actual DPI
|
||||
})
|
||||
if err != nil { return nil, err }
|
||||
this.faces[key] = face
|
||||
return face, nil
|
||||
}
|
||||
|
||||
func buildAttrBorder (attr []ValueList) (tomo.Attr, error) {
|
||||
borders := make([]tomo.Border, len(attr))
|
||||
for index, list := range attr {
|
||||
@@ -349,3 +405,4 @@ func copyBorderValue[T any, U ~[]T] (destination, source U) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -135,13 +135,15 @@ func (this *parser) parseSelector () (Selector, error) {
|
||||
if err == nil {
|
||||
this.Next()
|
||||
for {
|
||||
err := this.Expect(Ident, String, RBrace)
|
||||
err := this.Expect(Ident, String, RBracket)
|
||||
if err != nil { return Selector { }, err }
|
||||
if this.Is(RBrace) { break }
|
||||
if this.Is(RBracket) { break }
|
||||
if this.Is(Comma) { this.Next() }
|
||||
selector.Tags = append(selector.Tags, this.Value())
|
||||
err = this.ExpectNext(Comma, RBracket)
|
||||
if err != nil { return Selector { }, err }
|
||||
}
|
||||
this.Next()
|
||||
}
|
||||
|
||||
return selector, nil
|
||||
@@ -163,7 +165,8 @@ func (this *parser) parseAttr () (string, []ValueList, error) {
|
||||
Number, Color, String, Ident, Dollar, Slash,
|
||||
Comma, Semicolon)
|
||||
if err != nil { return "", nil, err }
|
||||
if this.Is(Semicolon) { break }
|
||||
if this.Is(Semicolon) { break }
|
||||
if this.Is(Comma) { this.Next() }
|
||||
valueList, err := this.parseValueList()
|
||||
if err != nil { return "", nil, err }
|
||||
attr = append(attr, valueList)
|
||||
|
||||
Reference in New Issue
Block a user