Module:Halberstadt SVG: Difference between revisions
From XenReference
mNo edit summary |
mNo edit summary |
||
| Line 129: | Line 129: | ||
-- SVG assembly | -- SVG assembly | ||
local | -- Has to use mw.html.create('htmltag') HtmlBuilder object so MediaWiki knows it's trusted HTML and avoids escaping a plain string | ||
local svg = mw.html.create('htmltag') | |||
svg | |||
:attr('tagname', 'svg') | |||
:attr('xmlns', 'http://www.w3.org/2000/svg') | |||
:attr('width', svgWidth) | |||
:attr('height', svgHeight) | |||
-- White keys | -- White keys | ||
for _, pc in ipairs(whiteKeys) do | for _, pc in ipairs(whiteKeys) do | ||
local x = xPos[pc] | local x = xPos[pc] | ||
svg:tag('htmltag') | |||
:attr('tagname', 'rect') | |||
:attr('x', x) | |||
:attr('y', 0) | |||
) | :attr('width', wW) | ||
:attr('height', hW) | |||
:css('fill', '#' .. colors[1]) | |||
:css('stroke', '#000') | |||
:done() | |||
if labels[pc] then | if labels[pc] then | ||
svg:tag('htmltag') | |||
:attr('tagname', 'text') | |||
:attr('x', x + wW / 2) | |||
:attr('y', hW - 5) | |||
:attr('font-size', fsW) | |||
:attr('text-anchor', 'middle') | |||
:css('fill', '#' .. colors[2]) | |||
:wikitext(table.concat(labels[pc], "/")) | |||
:done() | |||
end | end | ||
end | end | ||
| Line 166: | Line 171: | ||
local fill = colors[colorIndex] or colors[3] | local fill = colors[colorIndex] or colors[3] | ||
local textCol = colors[colorIndex + 1] or colors[4] | local textCol = colors[colorIndex + 1] or colors[4] | ||
svg:tag('htmltag') | |||
:attr('tagname', 'rect') | |||
:attr('x', x) | |||
:attr('y', y) | |||
) | :attr('width', wB) | ||
:attr('height', hB) | |||
:css('fill', '#' .. fill) | |||
:css('stroke', '#000') | |||
:done() | |||
svg:tag('htmltag') | |||
:attr('tagname', 'text') | |||
:attr('x', x + wB / 2) | |||
:attr('y', y + hB - 5) | |||
:attr('font-size', fsB) | |||
:attr('text-anchor', 'middle') | |||
:css('fill', '#' .. textCol) | |||
:wikitext(table.concat(labels[pc], "/")) | |||
:done() | |||
end | end | ||
end | end | ||
return svg | |||
return | |||
end | end | ||
return p | return p | ||
Revision as of 17:43, 8 February 2026
Documentation for this module may be created at Module:Halberstadt SVG/doc
--[[
MediaWiki Lua module to generate diagrams of microtonal Halberstadt keyboards using the html <svg> element. If there is more than one "black" key in between two "white" keys, stack the higher ones above the lower ones vertically. Arguments with a 19edo example:
nominals = {"F", "C", "G", "D", "A", "E", "B"} -- natural notes, the "white" keys of the keyboard
period = 19 -- number of keys total
generator = 11 -- number of keys from one nominal to the next, putting each on key number n*generator % period
startIndex = 5 -- index of the nominal on the leftmost key, in this case A
duplicateFirstKey = true -- whether to add an additional key at the right side of the keyboard with the same note as the first key
accidentals = {"b", "#"}
accidentalValues = {-1, 1} -- number of keys that each accidental alters the pitch by
nominalOverride = false -- whether to ignore note names with an accidental if they land on the same key as a nominal; if false, show both names generated
keyDimensions = {20, 80, 10, 40, 20, 60, 10} -- all measured in pixels: white key width, white key height, white key font size, vertical offset of black keys, black key width, black key height, black key font size
keyColors = {"fff", "000", "333", "fff", "666", "fff"} -- "white" key color, "white" key font color, "black" key color, "black" key font color, subsequent values may be used to define the color and font color of any "black" keys above the first row
--]]
local p = {}
-- Convert comma-separated values into Lua table, trim whitespace
local function splitCSV(str)
if not str or str == "" then return {} end
local t = {}
for s in mw.text.gsplit(str, "%s*,%s*") do
table.insert(t, s)
end
return t
end
-- Modulo that handles negative numbers correctly
local function mod(a, b)
return ((a % b) + b) % b
end
-- Core renderer
function p.render(frame)
local args = frame.args
-- Musical parameters
local nominals = splitCSV(args.nominals)
local period = tonumber(args.period)
local generator = tonumber(args.generator)
local startIndex = tonumber(args.startIndex) or 1
local duplicateFirstKey = args.duplicateFirstKey == "yes"
local accidentals = splitCSV(args.accidentals)
local accidentalValues = {}
for _, v in ipairs(splitCSV(args.accidentalValues)) do
table.insert(accidentalValues, tonumber(v))
end
local nominalOverride = args.nominalOverride == "yes"
-- Geometry
local dims = {}
for _, v in ipairs(splitCSV(args.keyDimensions or "20,80,10,40,20,60,10")) do
table.insert(dims, tonumber(v))
end
local wW, hW, fsW, blkOffset, wB, hB, fsB =
dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7]
-- Colors
local colors = splitCSV(args.keyColors or "fff,000,333,fff,666,fff")
-- Compute pitch classes for nominals
local nominalKeys = {}
for i, name in ipairs(nominals) do
local idx = mod((i - startIndex) * generator, period)
nominalKeys[idx] = nominalKeys[idx] or {}
table.insert(nominalKeys[idx], name)
end
-- Generate all labels per pitch class
local labels = {}
for pc, names in pairs(nominalKeys) do
labels[pc] = labels[pc] or {}
for _, n in ipairs(names) do
table.insert(labels[pc], n)
end
end
for pc, names in pairs(nominalKeys) do
for i, acc in ipairs(accidentals) do
local delta = accidentalValues[i]
local target = mod(pc + delta, period)
if not nominalOverride or not nominalKeys[target] then
labels[target] = labels[target] or {}
for _, n in ipairs(names) do
table.insert(labels[target], n .. acc)
end
end
end
end
-- Determine white keys (nominals only)
local whiteKeys = {}
for pc in pairs(nominalKeys) do
table.insert(whiteKeys, pc)
end
table.sort(whiteKeys)
if duplicateFirstKey then
table.insert(whiteKeys, whiteKeys[1] + period)
end
-- Map pitch class -> x-position
local xPos = {}
for i, pc in ipairs(whiteKeys) do
xPos[pc] = (i - 1) * wW
end
local svgWidth = #whiteKeys * wW
local svgHeight = hW + hB + blkOffset
-- Collect black keys between whites
local blackKeys = {}
for pc, lbls in pairs(labels) do
if not nominalKeys[pc] then
local leftWhite
for i = #whiteKeys, 1, -1 do
if whiteKeys[i] < pc then
leftWhite = whiteKeys[i]
break
end
end
if leftWhite then
blackKeys[leftWhite] = blackKeys[leftWhite] or {}
table.insert(blackKeys[leftWhite], pc)
end
end
end
-- SVG assembly
-- Has to use mw.html.create('htmltag') HtmlBuilder object so MediaWiki knows it's trusted HTML and avoids escaping a plain string
local svg = mw.html.create('htmltag')
svg
:attr('tagname', 'svg')
:attr('xmlns', 'http://www.w3.org/2000/svg')
:attr('width', svgWidth)
:attr('height', svgHeight)
-- White keys
for _, pc in ipairs(whiteKeys) do
local x = xPos[pc]
svg:tag('htmltag')
:attr('tagname', 'rect')
:attr('x', x)
:attr('y', 0)
:attr('width', wW)
:attr('height', hW)
:css('fill', '#' .. colors[1])
:css('stroke', '#000')
:done()
if labels[pc] then
svg:tag('htmltag')
:attr('tagname', 'text')
:attr('x', x + wW / 2)
:attr('y', hW - 5)
:attr('font-size', fsW)
:attr('text-anchor', 'middle')
:css('fill', '#' .. colors[2])
:wikitext(table.concat(labels[pc], "/"))
:done()
end
end
-- Black keys (stacked)
for leftWhite, pcs in pairs(blackKeys) do
table.sort(pcs)
for i, pc in ipairs(pcs) do
local x = xPos[leftWhite] + wW - wB / 2
local y = blkOffset - (i - 1) * (hB + 2)
local colorIndex = 3 + (i - 1) * 2
local fill = colors[colorIndex] or colors[3]
local textCol = colors[colorIndex + 1] or colors[4]
svg:tag('htmltag')
:attr('tagname', 'rect')
:attr('x', x)
:attr('y', y)
:attr('width', wB)
:attr('height', hB)
:css('fill', '#' .. fill)
:css('stroke', '#000')
:done()
svg:tag('htmltag')
:attr('tagname', 'text')
:attr('x', x + wB / 2)
:attr('y', y + hB - 5)
:attr('font-size', fsB)
:attr('text-anchor', 'middle')
:css('fill', '#' .. textCol)
:wikitext(table.concat(labels[pc], "/"))
:done()
end
end
return svg
end
return p
