2 -- return a table of names associated with decorator symbols
3 function decorator_names(model)
4 local sheets = model.doc:sheets()
5 local symbols = sheets:allNames("symbol")
7 for _, name in pairs(symbols) do
8 if name:find("deco/") == 1 then
15 -- Decorate something given by its bounding box with a given deco
16 -- object, which needs to be a path.
17 function decorate(model, bbox, deco)
18 if (deco:type() ~= "path") then
19 model.ui:explain("The decoration needs to be a path.")
23 local shape = deco:shape()
24 for _,path in pairs(shape) do
25 for _,subpath in ipairs(path) do
27 for i,point in ipairs(subpath) do
28 subpath[i] = translation(bbox, point) * point
31 -- for acs, the center must be translated separately
32 if (subpath["type"] == "arc") then
33 local arc = subpath["arc"]
34 local arc_pos = arc:matrix():translation()
35 subpath["arc"] = translation(bbox, arc_pos) * arc
41 model:creation("create", deco)
44 -- The translation matrix that should be applied to a given point when
45 -- doing the decoration.
46 function translation(bbox, point)
50 dx = dx + bbox:width()
53 dy = dy + bbox:height()
56 dy = dy + bbox:bottom()
57 return ipe.Translation(dx, dy)
60 function mainWindow(model)
61 if model.ui.win == nil then
68 function run_decorator(model)
69 -- get bbox of primary selection
70 local p = model:page()
71 local prim = p:primarySelection()
73 model.ui:explain("An object must be selected.")
76 local bbox = p:bbox(prim)
78 -- create decorator object
79 local dialog = ipeui.Dialog(mainWindow(model), "Select a decorator.")
80 local decorators = decorator_names(model)
81 dialog:add("deco", "combo", decorators, 1, 1, 1, 2)
82 dialog:add("ok", "button", { label="&Ok", action="accept" }, 2, 2)
83 dialog:add("cancel", "button", { label="&Cancel", action="reject" }, 2, 1)
84 local r = dialog:execute()
85 if not r then return end
86 local deco_name = decorators[dialog:get("deco")]
87 local symbol = model.doc:sheets():find("symbol", deco_name)
88 local deco = symbol:clone()
91 decorate(model, bbox, deco)
96 { label = "Decorate", run=run_decorator},