local kiwi = require("kiwi") local Var = kiwi.Var ---@param solver kiwi.Solver local function build_solver(solver) -- create custom strength -- local width = Var("width") local height = Var("height") local mmedium = kiwi.strength.create(0.0, 1.0, 0.0, 1.25) local smedium = kiwi.strength.create(0.0, 100, 0.0) local left = Var("left") local top = Var("top") local contents_top = Var("contents_top") local contents_bottom = Var("contents_bottom") local contents_left = Var("contents_left") local contents_right = Var("contents_right") local midline = Var("midline") local ctleft = Var("ctleft") local ctheight = Var("ctheight") local cttop = Var("cttop") local ctwidth = Var("ctwidth") local lb1left = Var("lb1left") local lb1height = Var("lb1height") local lb1top = Var("lb1top") local lb1width = Var("lb1width") local lb2left = Var("lb2left") local lb2height = Var("lb2height") local lb2top = Var("lb2top") local lb2width = Var("lb2width") local lb3left = Var("lb3left") local lb3height = Var("lb3height") local lb3top = Var("lb3top") local lb3width = Var("lb3width") local fl1left = Var("fl1left") local fl1height = Var("fl1height") local fl1top = Var("fl1top") local fl1width = Var("fl1width") local fl2left = Var("fl2left") local fl2height = Var("fl2height") local fl2top = Var("fl2top") local fl2width = Var("fl2width") local fl3left = Var("fl3left") local fl3height = Var("fl3height") local fl3top = Var("fl3top") local fl3width = Var("fl3width") solver:add_edit_var(width, kiwi.strength.STRONG) solver:add_edit_var(height, kiwi.strength.STRONG) local strong, medium, weak = kiwi.strength.STRONG, kiwi.strength.MEDIUM, kiwi.strength.WEAK local constraints = { (left + -0):ge(0), (height + 0):eq(0, medium), (top + -0):ge(0), (width + -0):ge(0), (height + -0):ge(0), (-top + contents_top + -10):eq(0), (lb3height + -16):eq(0, strong), (lb3height + -16):ge(0, strong), (ctleft + -0):ge(0), (cttop + -0):ge(0), (ctwidth + -0):ge(0), (ctheight + -0):ge(0), (fl3left + -0):ge(0), (ctheight + -24):ge(0, smedium), (ctwidth + -1.67772e+07):le(0, smedium), (ctheight + -24):le(0, smedium), (fl3top + -0):ge(0), (fl3width + -0):ge(0), (fl3height + -0):ge(0), (lb1width + -67):eq(0, weak), (lb2width + -0):ge(0), (lb2height + -0):ge(0), (fl2height + -0):ge(0), (lb3left + -0):ge(0), (fl2width + -125):ge(0, strong), (fl2height + -21):eq(0, strong), (fl2height + -21):ge(0, strong), (lb3top + -0):ge(0), (lb3width + -0):ge(0), (fl1left + -0):ge(0), (fl1width + -0):ge(0), (lb1width + -67):ge(0, strong), (fl2left + -0):ge(0), (lb2width + -66):eq(0, weak), (lb2width + -66):ge(0, strong), (lb2height + -16):eq(0, strong), (fl1height + -0):ge(0), (fl1top + -0):ge(0), (lb2top + -0):ge(0), (-lb2top + lb3top + -lb2height + -10):eq(0, mmedium), (-lb3top + -lb3height + fl3top + -10):ge(0), (-lb3top + -lb3height + fl3top + -10):eq(0, mmedium), (contents_bottom + -fl3height + -fl3top + -0):eq(0, mmedium), (fl1top + -contents_top + 0):ge(0), (fl1top + -contents_top + 0):eq(0, mmedium), (contents_bottom + -fl3height + -fl3top + -0):ge(0), (-left + -width + contents_right + 10):eq(0), (-top + -height + contents_bottom + 10):eq(0), (-left + contents_left + -10):eq(0), (lb3left + -contents_left + 0):eq(0, mmedium), (fl1left + -midline + 0):eq(0, strong), (fl2left + -midline + 0):eq(0, strong), (ctleft + -midline + 0):eq(0, strong), (fl1top + 0.5 * fl1height + -lb1top + -0.5 * lb1height + 0):eq(0, strong), (lb1left + -contents_left + 0):ge(0), (lb1left + -contents_left + 0):eq(0, mmedium), (-lb1left + fl1left + -lb1width + -10):ge(0), (-lb1left + fl1left + -lb1width + -10):eq(0, mmedium), (-fl1left + contents_right + -fl1width + -0):ge(0), (width + 0):eq(0, medium), (-fl1top + fl2top + -fl1height + -10):ge(0), (-fl1top + fl2top + -fl1height + -10):eq(0, mmedium), (cttop + -fl2top + -fl2height + -10):ge(0), (-ctheight + -cttop + fl3top + -10):ge(0), (contents_bottom + -fl3height + -fl3top + -0):ge(0), (cttop + -fl2top + -fl2height + -10):eq(0, mmedium), (-fl1left + contents_right + -fl1width + -0):eq(0, mmedium), (-lb2top + -0.5 * lb2height + fl2top + 0.5 * fl2height + 0):eq(0, strong), (-contents_left + lb2left + 0):ge(0), (-contents_left + lb2left + 0):eq(0, mmedium), (fl2left + -lb2width + -lb2left + -10):ge(0), (-ctheight + -cttop + fl3top + -10):eq(0, mmedium), (contents_bottom + -fl3height + -fl3top + -0):eq(0, mmedium), (lb1top + -0):ge(0), (lb1width + -0):ge(0), (lb1height + -0):ge(0), (fl2left + -lb2width + -lb2left + -10):eq(0, mmedium), (-fl2left + -fl2width + contents_right + -0):eq(0, mmedium), (-fl2left + -fl2width + contents_right + -0):ge(0), (lb3left + -contents_left + 0):ge(0), (lb1left + -0):ge(0), (0.5 * ctheight + cttop + -lb3top + -0.5 * lb3height + 0):eq(0, strong), (ctleft + -lb3left + -lb3width + -10):ge(0), (-ctwidth + -ctleft + contents_right + -0):ge(0), (ctleft + -lb3left + -lb3width + -10):eq(0, mmedium), (fl3left + -contents_left + 0):ge(0), (fl3left + -contents_left + 0):eq(0, mmedium), (-ctwidth + -ctleft + contents_right + -0):eq(0, mmedium), (-fl3left + contents_right + -fl3width + -0):eq(0, mmedium), (-contents_top + lb1top + 0):ge(0), (-contents_top + lb1top + 0):eq(0, mmedium), (-fl3left + contents_right + -fl3width + -0):ge(0), (lb2top + -lb1top + -lb1height + -10):ge(0), (-lb2top + lb3top + -lb2height + -10):ge(0), (lb2top + -lb1top + -lb1height + -10):eq(0, mmedium), (fl1height + -21):eq(0, strong), (fl1height + -21):ge(0, strong), (lb2left + -0):ge(0), (lb2height + -16):ge(0, strong), (fl2top + -0):ge(0), (fl2width + -0):ge(0), (lb1height + -16):ge(0, strong), (lb1height + -16):eq(0, strong), (fl3width + -125):ge(0, strong), (fl3height + -21):eq(0, strong), (fl3height + -21):ge(0, strong), (lb3height + -0):ge(0), (ctwidth + -119):ge(0, smedium), (lb3width + -24):eq(0, weak), (lb3width + -24):ge(0, strong), (fl1width + -125):ge(0, strong), } for _, c in ipairs(constraints) do print(c) solver:add_constraint(c) end return width, height end local function main() local sizes = { { w = 400, h = 600 }, { w = 600, h = 400 }, { w = 800, h = 1200 }, { w = 1200, h = 800 }, { w = 400, h = 800 }, { w = 800, h = 400 }, } for i = 1, 1 do local solver = kiwi.Solver() local width, height = build_solver(solver) for _, size in ipairs(sizes) do solver:suggest_value(width, size.w) solver:suggest_value(height, size.h) solver:update_vars() print(width:value(), height:value()) end end end main() collectgarbage("collect") collectgarbage("collect") collectgarbage("collect") collectgarbage("collect")