Skip to content

Commit

Permalink
Add penalty node
Browse files Browse the repository at this point in the history
  • Loading branch information
pgundlach committed Nov 11, 2021
1 parent 6892c6e commit 14499af
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 24 deletions.
97 changes: 89 additions & 8 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

const (
luaNodeTypeName = "node"
luaHlistNodeTypeName = "hlistnode"
luaVlistNodeTypeName = "vlistnode"
luaGlyphNodeTypeName = "glyphnode"
luaImageNodeTypeName = "imagenode"
luaLangNodeTypeName = "langnode"
luaGlueNodeTypeName = "gluenode"
luaDiscNodeTypeName = "discnode"
luaNodeTypeName = "node"
luaDiscNodeTypeName = "discnode"
luaGlueNodeTypeName = "gluenode"
luaGlyphNodeTypeName = "glyphnode"
luaHlistNodeTypeName = "hlistnode"
luaImageNodeTypeName = "imagenode"
luaLangNodeTypeName = "langnode"
luaPenaltyNodeTypeName = "penaltynode"
luaVlistNodeTypeName = "vlistnode"
)

/*
Expand Down Expand Up @@ -130,6 +131,9 @@ func newNode(l *lua.LState) int {
case "lang":
l.Push(newUserDataFromNode(l, bagnode.NewLang()))
return 1
case "penalty":
l.Push(newUserDataFromNode(l, bagnode.NewPenalty()))
return 1
case "vlist":
l.Push(newUserDataFromNode(l, bagnode.NewVList()))
return 1
Expand Down Expand Up @@ -170,6 +174,10 @@ func newUserDataFromNode(l *lua.LState, n bagnode.Node) *lua.LUserData {
mt = l.NewTypeMetatable(luaLangNodeTypeName)
l.SetField(mt, "__index", l.NewFunction(langNodeIndex))
l.SetField(mt, "__newindex", l.NewFunction(langNodeNewIndex))
case *bagnode.Penalty:
mt = l.NewTypeMetatable(luaPenaltyNodeTypeName)
l.SetField(mt, "__index", l.NewFunction(penaltyNodeIndex))
l.SetField(mt, "__newindex", l.NewFunction(penaltyNodeNewIndex))
case *bagnode.VList:
mt = l.NewTypeMetatable(luaVlistNodeTypeName)
l.SetField(mt, "__index", l.NewFunction(vlistIndex))
Expand Down Expand Up @@ -586,6 +594,79 @@ func langNodeIndex(l *lua.LState) int {
return 0
}

/*
Penalty nodes
*/

func checkPenaltyNode(l *lua.LState, argpos int) *bagnode.Penalty {
ud := l.CheckUserData(argpos)
if v, ok := ud.Value.(*bagnode.Penalty); ok {
return v
}
l.ArgError(argpos, "penalty node expected")
return nil
}

func penaltyNodeNewIndex(l *lua.LState) int {
n := checkPenaltyNode(l, 1)
switch arg := l.ToString(2); arg {
case "next":
if l.Get(3) == lua.LNil {
n.SetNext(nil)
} else {
n.SetNext(checkNode(l, 3))
}
return 0
case "prev":
if l.Get(3) == lua.LNil {
n.SetNext(nil)
} else {
n.SetNext(checkNode(l, 3))
}
return 0
case "penalty":
n.Penalty = l.CheckInt(3)
case "flagged":
n.Flagged = l.CheckBool(3)
case "width":
wd := l.CheckNumber(3)
n.Width = bag.ScaledPoint(wd)
}
return 0
}

func penaltyNodeIndex(l *lua.LState) int {
n := checkPenaltyNode(l, 1)
switch arg := l.ToString(2); arg {
case "next":
var other bagnode.Node
if other = n.Next(); other == nil {
return 0
}
l.Push(newUserDataFromNode(l, other))
return 1
case "prev":
var other bagnode.Node
if other = n.Next(); other == nil {
return 0
}
l.Push(newUserDataFromNode(l, other))
return 1
case "penalty":
l.Push(lua.LNumber(n.Penalty))
return 1
case "flagged":
l.Push(lua.LBool(n.Flagged))
return 1
case "width":
l.Push(lua.LNumber(n.Width))
return 1
}
return 0
}

/*
VList nodes
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ module github.com/speedata/ets
go 1.17

require (
github.com/speedata/boxesandglue v0.0.0-20211102102252-92eec49ab5a8
github.com/speedata/boxesandglue v0.0.0-20211111193940-f1fa41337424
github.com/speedata/optionparser v1.0.0
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9
go.uber.org/zap v1.19.1
)

require (
github.com/antonfisher/nested-logrus-formatter v1.3.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/speedata/gofpdi v1.0.15 // indirect
github.com/speedata/gootf v0.0.0-20211102101716-062a89e3221e // indirect
github.com/speedata/hyphenation v1.0.1 // indirect
Expand Down
15 changes: 2 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ=
github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand All @@ -8,13 +6,11 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
Expand All @@ -25,23 +21,17 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/speedata/boxesandglue v0.0.0-20211029075238-e1c07b278f7b h1:4Jy2Xlgu8r1yLFQs4aOH5txtEpZHXQTP88zD7/L7SYY=
github.com/speedata/boxesandglue v0.0.0-20211029075238-e1c07b278f7b/go.mod h1:v+UKsgo7MTFOvMYuU7xR2iuxoBekwVvT+s91ynM9wkM=
github.com/speedata/boxesandglue v0.0.0-20211102102252-92eec49ab5a8 h1:Ar28XsmuTAWESJOQzQcsBa2IkstqxnJFzN82kzX8p+w=
github.com/speedata/boxesandglue v0.0.0-20211102102252-92eec49ab5a8/go.mod h1:9BV4WsjEDggvn0iSppPMrFV//NB0wlOqX+XC9RImjh8=
github.com/speedata/boxesandglue v0.0.0-20211111193940-f1fa41337424 h1:PldJT1hdoYf4akvjCw8aqeAk6OeNxyL3j6uxP0D99RM=
github.com/speedata/boxesandglue v0.0.0-20211111193940-f1fa41337424/go.mod h1:9BV4WsjEDggvn0iSppPMrFV//NB0wlOqX+XC9RImjh8=
github.com/speedata/gofpdi v1.0.15 h1:PLFmzHTAdhgvCDMsWgvL/njaDFzTmvsYewqgw3UUEg4=
github.com/speedata/gofpdi v1.0.15/go.mod h1:aRZ6VjhFbzJWwCoHeKOdbUjImuf4GMVCJpwS9+bCYuM=
github.com/speedata/gootf v0.0.0-20210922121111-803211d11afa/go.mod h1:bXdvybIvXGiKH0Yvm3KeIINmXldBdxe8RgHL7nkk25U=
github.com/speedata/gootf v0.0.0-20211102101716-062a89e3221e h1:NHpP15eopboLD2vKal/0qQ3IuXYa+ohenSH9+2YjDKs=
github.com/speedata/gootf v0.0.0-20211102101716-062a89e3221e/go.mod h1:W9eH+4fle8N24D52HYisQ4hJcp050D2lkx6QZ0e4xUs=
github.com/speedata/hyphenation v1.0.1 h1:O9gKtVw2JvyiHybCYJwpB3zzHaeXEvDtXjiExZdm1TU=
github.com/speedata/hyphenation v1.0.1/go.mod h1:vwrKKvBvJWFll0sVZw99hyWS/+r4YlMI7MAYjnje0nM=
github.com/speedata/optionparser v1.0.0 h1:O9Bvoq20BQWMPq0b/id0kBT9oI0njNlHzY/wtQ3JA1o=
github.com/speedata/optionparser v1.0.0/go.mod h1:AetA6qHxho+CGuukPCm44MsGu+Vvu/fATB/Er9PKmGk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -69,7 +59,6 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down

0 comments on commit 14499af

Please sign in to comment.