Fast Go implementation of the Cassowary constraint solving algorithm, ported directly from LUME Kiwi.
go get github.com/unxed/kiwi-gopackage main
import (
"fmt"
"github.com/unxed/kiwi-go"
)
func main() {
solver := kiwi.NewSolver()
left := kiwi.NewVariable("left")
width := kiwi.NewVariable("width")
right := kiwi.NewVariable("right")
_ = solver.AddEditVariable(left, kiwi.StrengthStrong)
_ = solver.AddEditVariable(width, kiwi.StrengthStrong)
_ = solver.SuggestValue(left, 100)
_ = solver.SuggestValue(width, 400)
// right == left + width
cn := kiwi.NewConstraint(right, kiwi.OpEq, left.Plus(width))
_ = solver.AddConstraint(cn)
solver.UpdateVariables()
fmt.Printf("right = %f\n", right.Value()) // 500.0
}kiwi-go includes a Discrete Cassowary Engine designed for Terminal User Interfaces (TUI), adapting concepts from font hinting (TrueType & FreeType):
- FreeType-style Autohinting (
ApportionSum): Distributes rounding remainders using Hamilton / Hare-Niemeyer apportionment so that integer component sizes sum to container totals with zero character gaps or overflows, preserving layout symmetry. - TrueType-style Rule Directives (
RuleHinter): Apply explicit layout instructions such asSnapToGrid(for double-width CJK or grid boundaries),ClampMinMax,EqualizeGroup, andAlignEdges. DiscreteSolver: Integrates continuous Cassowary constraint solving, autohinting apportionment, and rule directives in a unified TUI layout solver.
For full mathematical design and TUI layout examples, see docs/DISCRETE_CASSOWARY.md.
Run the interactive TUI console demo:
go run ./cmd/kiwi-demoRun unit tests:
go test -v ./...Run benchmarks:
go test -bench=. ./...First, ensure you have generated kiwi.wasm and wasm_exec.js using ./build.sh.
To run the NodeJS integration test, execute:
node test.jsYou can also run the internal Go bridge tests using standard Go tools:
GOOS=js GOARCH=wasm go test -v .To see the engine solving UI layouts in your browser:
- Start a local web server in this directory (browsers do not allow fetching WASM from local
file://URLs due to CORS). For example:python3 -m http.server 8080
- Open http://localhost:8080/index.html in your browser.
- Resize the gray container box to see Cassowary dynamically calculate and apply layout constraints in real-time.
## License
Modified BSD License.