模組:組字

萌娘百科,萬物皆可萌的百科全書!轉載請標註來源頁面的網頁連結,並聲明引自萌娘百科。內容不可商用。
貢獻者:
Template-info.svg 模塊文檔  [] [刷新]
  1. local p={}
  2. p.parse=function(frame)
  3. local parseTable=mw.loadData("Module:組字/ParseTable")
  4. local function buildTree(str, index)
  5. -- 遞歸下降
  6. index=index or 1
  7. str=str or ""
  8. if mw.ustring.len(str) < index then return nil, index end
  9. local chr=mw.ustring.sub(str, index, index)
  10. local data={}
  11. data.head=chr
  12. if parseTable[chr] then
  13. data.body={}
  14. local nexti=index+1
  15. for i=1,parseTable[chr].length do
  16. data.body[i], nexti=buildTree(str, nexti)
  17. if not data.body[i] then break end
  18. end
  19. return data, nexti
  20. else
  21. return data, index+1
  22. end
  23. end
  24. local function setSize(tree, x, y, width, height)
  25. -- 計算尺寸
  26. if type(tree)~="table" then return end
  27. tree.x=x
  28. tree.y=y
  29. tree.width=width
  30. tree.height=height
  31. if not tree.body then return end
  32. local szt=parseTable[tree.head]
  33. if not szt then return end
  34. for i=1,szt.length do
  35. setSize(tree.body[i], szt.x[i]*width+x, szt.y[i]*height+y, szt.width[i]*width, szt.height[i]*height)
  36. end
  37. end
  38. local function plainize(tree)
  39. local arr={}
  40. if type(tree)~="table" then return end
  41. if tree.body then
  42. for i=1,#(tree.body) do
  43. local sarr=plainize(tree.body[i])
  44. for k,v in ipairs(sarr) do
  45. table.insert(arr, v)
  46. end
  47. end
  48. elseif not parseTable[tree.head] then
  49. table.insert(arr, {
  50. text=tree.head,
  51. width=tree.width,
  52. height=tree.height,
  53. left=tree.x,
  54. top=tree.y,
  55. })
  56. end
  57. return arr
  58. end
  59. local function make_text(text, unit, gw, gh)
  60. local ts=mw.html.create("span")
  61. if type(text)~="table" then return ts end
  62. ts:css("display", "inline-block")
  63. ts:css("position", "absolute")
  64. ts:css("top", tostring(text.top) .. unit)
  65. ts:css("left", tostring(text.left) .. unit)
  66. ts:css("width", tostring(gw) .. unit)
  67. ts:css("height", tostring(gh) .. unit)
  68. ts:css("transform", "scale(" .. tostring(text.width/gw) .. "," .. tostring(text.height/gh) .. ")" )
  69. ts:css("transform-origin", "0% 0%")
  70. ts:addClass("combination-text")
  71. ts:wikitext(text.text)
  72. return ts
  73. end
  74. local function make_frame(texts, width, height, unit)
  75. if type(texts)~="table" then return end
  76. local text=mw.html.create("span")
  77. text:css("display", "inline-block")
  78. --text:css("overflow", "hidden")
  79. text:css("position", "relative")
  80. text:css("width", tostring(width) .. unit)
  81. text:css("height", tostring(height) .. unit)
  82. --text:css("top", tostring(height*5/6) .. unit)
  83. --text:css("left", tostring(width/12) .. unit)
  84. text:css("font-size", tostring((height+width)/2) .. unit)
  85. --local place=mw.html.create("span")
  86. --place:css("display", "inline-block")
  87. --place:css("position", "absolute")
  88. --place:css("z-index", "-1")
  89. --text:node(place)
  90. for k,v in ipairs(texts) do
  91. text:node(make_text(v, unit, width, height))
  92. end
  93. return text
  94. end
  95. local function emit_html(tree, unit)
  96. local arr=plainize(tree)
  97. return tostring(make_frame(arr, tree.width, tree.height, unit))
  98. end
  99. local str=frame.args[1]
  100. if not str then return end
  101. local size=tonumber(frame.args[2]) or 24
  102. local unit=frame.args[3] or "px"
  103. local chrdata=buildTree(str, 1)
  104. setSize(chrdata, 0, 0, size, size)
  105. --do return mw.dumpObject(plainize(chrdata)) end
  106. return emit_html(chrdata, unit)
  107. end
  108. return p
此頁面最後編輯於 2020年2月26日 (週三) 17:33。
搜尋萌娘百科 (按"/"快速搜尋)
有新的未讀公告

阅读更多:模塊:組字(http://mzh.moegirl.tw/%E6%A8%A1%E5%A1%8A%3A%E7%B5%84%E5%AD%97 )
本文引自萌娘百科(http://mzh.moegirl.tw ),文字内容默认使用《知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆》协议。