-- 保存为MPwordslink.lua
-- 依赖utf8.lua模块处理中文,见 https://gist.github.com/markandgo/5776124
-- 注意改名,以免跟系统模块冲突
local utf8 = require "m-utf8" --或用unicode.utf8.gmatch
-- c: 字;s: "词语;词语;...";r:半径
function MP.wordslink(char,words,r) --lua.MP是给用户预留的命名空间,lua.mp则是系统自用的
local function split (c, inputstr, sep)
if sep == nil then
sep = "%s*;%s*"
end
-- {{str:word, len:wordlength, match_num:c_num_in_the_word, head_or_tail:last_c's_position},..}
local t={}
for str in utf8.gmatch(inputstr, "[^"..sep.."]+") do
local tbl = {}
local match_num = 0
for cc in utf8.gmatch(str,".") do
table.insert(tbl, cc)
if c == cc then match_num = match_num +1 end
end
local len = utf8.len(str)
local head_or_tail = nil
if tbl[1] == c then head_or_tail = 1 end
if tbl[#tbl] == c then head_or_tail = 2 end
--print(str, len, match_num, head_or_tail)
-- 删除生字
if match_num == 1 and head_or_tail then
str = utf8.gsub(str, c, nil)
end
table.insert(t, {["str"]=str, ["len"]=len, ["match_num"]=match_num, ["head_or_tail"]=head_or_tail})
end
return t
end
local t = split(char, words)
local rt = 360 / #t
local code1 = [[
input boxes;
sp:=%s;
circleit.n0("%s");
n0.c = origin;
drawboxed(n0);
circleit.a("");
]]
mp.fprint(code1, r, char)
mp.fprint([[a.c = n0.c + (0, %s cm);]], r) --定位圆
for i,data in ipairs(t) do
local v = data["str"]
v = utf8.gsub(v, "r", [[\low{儿}]])
local len = data["len"]
local ri = len/2*r --实际半径cm
mp.fprint([[circleit.n%s(btex %s etex);]], i, v)
mp.fprint([[n%s.c = (a.c + (0, %s cm)) rotated %s;]], i, ri, (-rt*(i-1)))
mp.fprint([[drawboxed(n%s);]], i)
if data["match_num"] >= 2 or data["head_or_tail"] == nil then --含两个以上生字,或生字不在头尾
mp.fprint([[draw n0.c .. n%s.c cutbefore bpath.n0 cutafter bpath.n%s;]], i, i)
elseif data["head_or_tail"] == 2 then --生字在尾
mp.fprint([[drawarrow n%s.c .. n0.c cutafter bpath.n0 cutbefore bpath.n%s;]], i, i)
elseif data["head_or_tail"] == 1 then --生字在头
mp.fprint([[drawarrow n0.c .. n%s.c cutbefore bpath.n0 cutafter bpath.n%s;]], i, i)
end
end
end
return MP.wordslink