Remove font logic from TSS

This commit is contained in:
Sasha Koshka 2024-08-10 22:20:34 -04:00
parent b0672ec8ee
commit a8bc074aad
2 changed files with 16 additions and 51 deletions

View File

@ -1,20 +1,15 @@
package tss package tss
import "os"
import "fmt" import "fmt"
import "image" import "image"
import "errors" import "errors"
import "image/color" import "image/color"
import "golang.org/x/image/font"
import "github.com/flopp/go-findfont"
import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo"
import "golang.org/x/image/font/opentype"
import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/backend/style"
type styleBuilder struct { type styleBuilder struct {
sheet Sheet sheet Sheet
fonts map[string] *opentype.Font
faces map[faceKey] font.Face
} }
type faceKey struct { type faceKey struct {
@ -24,16 +19,14 @@ type faceKey struct {
// BuildStyle builds a Tomo style from the specified sheet. Resources associated // BuildStyle builds a Tomo style from the specified sheet. Resources associated
// with it (such as textures) can be freed by closing the returned cookie. // with it (such as textures) can be freed by closing the returned cookie.
func BuildStyle (sheet Sheet) (*tomo.Style, event.Cookie, error) { func BuildStyle (sheet Sheet) (*style.Style, event.Cookie, error) {
builder := &styleBuilder { builder := &styleBuilder {
sheet: sheet, sheet: sheet,
fonts: make(map[string] *opentype.Font),
faces: make(map[faceKey] font.Face),
} }
return builder.build() return builder.build()
} }
func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) { func (this *styleBuilder) build () (*style.Style, event.Cookie, error) {
err := this.sheet.Flatten() err := this.sheet.Flatten()
if err != nil { return nil, nil, err } if err != nil { return nil, nil, err }
@ -49,8 +42,8 @@ func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) {
} }
cookies := []event.Cookie { } cookies := []event.Cookie { }
style := &tomo.Style { sty := &style.Style {
Rules: make([]tomo.Rule, len(this.sheet.Rules)), Rules: make([]style.Rule, len(this.sheet.Rules)),
Colors: map[tomo.Color] color.Color { Colors: map[tomo.Color] color.Color {
tomo.ColorBackground: getColor("ColorBackground"), tomo.ColorBackground: getColor("ColorBackground"),
tomo.ColorForeground: getColor("ColorForeground"), tomo.ColorForeground: getColor("ColorForeground"),
@ -61,13 +54,13 @@ func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) {
} }
for index, rule := range this.sheet.Rules { for index, rule := range this.sheet.Rules {
styleRule := tomo.Rule { styleRule := style.Rule {
Role: tomo.Role { Role: tomo.Role {
Package: rule.Selector.Package, Package: rule.Selector.Package,
Object: rule.Selector.Object, Object: rule.Selector.Object,
}, },
Tags: rule.Selector.Tags, Tags: rule.Selector.Tags,
Set: make(tomo.AttrSet), Set: make(style.AttrSet),
} }
for name, attr := range rule.Attrs { for name, attr := range rule.Attrs {
styleAttr, cookie, err := this.buildAttr(name, attr) styleAttr, cookie, err := this.buildAttr(name, attr)
@ -77,13 +70,13 @@ func (this *styleBuilder) build () (*tomo.Style, event.Cookie, error) {
cookies = append(cookies, cookie) cookies = append(cookies, cookie)
} }
} }
style.Rules[index] = styleRule sty.Rules[index] = styleRule
} }
// TODO include all the faces in this.faces (ONCE EACH) in the // TODO include all the faces in this.faces (ONCE EACH) in the
// multicookie // multicookie
return style, event.MultiCookie(cookies...), nil return sty, event.MultiCookie(cookies...), nil
} }
func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error) { func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr, event.Cookie, error) {
@ -238,6 +231,7 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
return nil, nil, errWrongType() return nil, nil, errWrongType()
case "Face": case "Face":
// TODO support weight, italic, slant
err := expectSingle() err := expectSingle()
if err != nil { return nil, nil, err } if err != nil { return nil, nil, err }
list := attr[0] list := attr[0]
@ -250,9 +244,10 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
if !ok { return nil, nil, errWrongType() } if !ok { return nil, nil, errWrongType() }
size, ok := list[1].(ValueNumber) size, ok := list[1].(ValueNumber)
if !ok { return nil, nil, errWrongType() } if !ok { return nil, nil, errWrongType() }
face, err := this.face(string(name), int(size)) return tomo.AFace(tomo.Face {
if err != nil { return nil, nil, err } Font: string(name),
return tomo.AFace(face), nil, nil Size: float64(size),
}), nil, nil
case "Wrap": case "Wrap":
err := expectSingleSingle() err := expectSingleSingle()
@ -311,36 +306,6 @@ func (this *styleBuilder) buildAttr (name string, attr []ValueList) (tomo.Attr,
return nil, nil, errors.New(fmt.Sprintf("unimplemented attribute name %s", name)) 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) { func buildAttrBorder (attr []ValueList) (tomo.Attr, error) {
borders := make([]tomo.Border, len(attr)) borders := make([]tomo.Border, len(attr))
for index, list := range attr { for index, list := range attr {

View File

@ -1,8 +1,8 @@
package tss package tss
import "os" import "os"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/backend/style"
type Sheet struct { type Sheet struct {
Variables map[string] ValueList Variables map[string] ValueList
@ -65,7 +65,7 @@ func (ValueCut) value () { }
// LoadFile loads the stylesheet from the specified file. This may return a // LoadFile loads the stylesheet from the specified file. This may return a
// parse.Error, so use parse.Format to print it. // parse.Error, so use parse.Format to print it.
func LoadFile (name string) (*tomo.Style, event.Cookie, error) { func LoadFile (name string) (*style.Style, event.Cookie, error) {
// TODO check cache for gobbed sheet. if the cache is nonexistent or // TODO check cache for gobbed sheet. if the cache is nonexistent or
// invalid, then open/load/cache. // invalid, then open/load/cache.