Module:Halberstadt SVG: Difference between revisions

From XenReference
m Undo revision 3443 by Inthar (talk)
Tag: Undo
mNo edit summary
Line 10: Line 10:
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
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
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
keyColors = {"000", "fff", "000", "333", "fff", "666", "fff"} -- outline color, "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
--]]
--]]


Line 46: Line 46:
table.insert(accidentalValues, tonumber(v))
table.insert(accidentalValues, tonumber(v))
end
end
local nominalOverride = args.nominalOverride == "yes"
local nominalOverride = args.nominalOverride == "yes"
Line 54: Line 53:
table.insert(dims, tonumber(v))
table.insert(dims, tonumber(v))
end
end
local wW, hW, fsW, blkOffset, wB, hB, fsB =
local wWhite, hWhite, fsWhite, offsetBlack, wBlack, hBlack, fsBlack =
dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7]
dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7]


-- Colors
-- Colors
local colors = splitCSV(args.keyColors or "fff,000,333,fff,666,fff")
local colors = splitCSV(args.keyColors or "000,fff,000,333,fff,666,fff")
-- Compute pitch classes for nominals
-- Compute pitch classes for nominals
Line 104: Line 103:
local xPos = {}
local xPos = {}
for i, pc in ipairs(whiteKeys) do
for i, pc in ipairs(whiteKeys) do
xPos[pc] = (i - 1) * wW
xPos[pc] = (i - 1) * wWhite
end
end
local svgWidth = #whiteKeys * wW
local svgWidth = #whiteKeys * wWhite
local svgHeight = hW + hB + blkOffset
local svgHeight = hWhite + hBlack + offsetBlack
-- Collect black keys between whites
-- Collect black keys between whites
Line 141: Line 140:
x = x,
x = x,
y = 0,
y = 0,
width = wW,
width = wWhite,
height = hW,
height = hWhite,
fill = "#" .. colors[1],
fill = '#' .. colors[2],
stroke = "#000"
stroke = colors[1]
}
}
}
}
Line 155: Line 154:
args = {
args = {
tagname = 'text',
tagname = 'text',
x = x + wW / 2,
x = x + wWhite / 2,
y = hW - 5,
y = hWhite - 5,
["text-anchor"] = "middle",
['text-anchor'] = 'middle',
["font-size"] = fsW,
['font-size'] = fsWhite,
fill = "#" .. colors[2]
fill = '#' .. colors[3]
}
}
}
}
Line 170: Line 169:
table.sort(pcs)
table.sort(pcs)
for i, pc in ipairs(pcs) do
for i, pc in ipairs(pcs) do
local x = xPos[leftWhite] + wW - wB / 2
local x = xPos[leftWhite] + wWhite - wBlack/2
local y = blkOffset - (i - 1) * (hB + 2)
local y = offsetBlack - (i-1)*(hBlack+2)
local colorIndex = 3 + (i - 1) * 2
local fill = colors[(i-1)*2 + 4] or colors[4]
local fill = colors[colorIndex] or colors[3]
local textCol = colors[(i-1)*2 + 5] or colors[5]
local textCol = colors[colorIndex + 1] or colors[4]
table.insert(out,
table.insert(out,
frame:extensionTag{
frame:extensionTag{
Line 182: Line 180:
x = x,
x = x,
y = y,
y = y,
width = wB,
width = wBlack,
height = hB,
height = hBlack,
fill = "#" .. fill,
fill = '#' .. fill,
stroke = "#000"
stroke = colors[1]
}
}
}
}
Line 195: Line 193:
args = {
args = {
tagname = 'text',
tagname = 'text',
x = x + wB / 2,
x = x + wBlack / 2,
y = y + hB - 5,
y = y + hBlack - 5,
["text-anchor"] = "middle",
['text-anchor'] = 'middle',
["font-size"] = fsB,
['font-size'] = fsBlack,
fill = "#" .. textCol
fill = '#' .. textCol
}
}
}
}
Line 210: Line 208:
args = {
args = {
tagname = 'svg',
tagname = 'svg',
xmlns = 'http://www.w3.org/2000/svg',
xmlns = 'http://wWhitew.w3.org/2000/svg',
width = svgWidth,
width = svgWidth,
height = svgHeight
height = svgHeight

Revision as of 18:53, 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 = {"000", "fff", "000", "333", "fff", "666", "fff"} -- outline color, "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 wWhite, hWhite, fsWhite, offsetBlack, wBlack, hBlack, fsBlack =
		dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7]

	-- Colors
	local colors = splitCSV(args.keyColors or "000,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) * wWhite
	end
	
	local svgWidth = #whiteKeys * wWhite
	local svgHeight = hWhite + hBlack + offsetBlack
	
	-- 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
	local out = {}
	
	-- White keys
	for _, pc in ipairs(whiteKeys) do
		local x = xPos[pc]
		table.insert(out,
			frame:extensionTag{
				name = "rect",
				args = {
					name = 'htmltag',
					x = x,
					y = 0,
					width = wWhite,
					height = hWhite,
					fill = '#' .. colors[2],
					stroke = colors[1]
				}
			}
		)
		if labels[pc] then
			table.insert(out,
				frame:extensionTag{
					name = 'htmltag',
					content = table.concat(labels[pc], "/"),
					args = {
						tagname = 'text',
						x = x + wWhite / 2,
						y = hWhite - 5,
						['text-anchor'] = 'middle',
						['font-size'] = fsWhite,
						fill = '#' .. colors[3]
					}
				}
			)
		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] + wWhite - wBlack/2
			local y = offsetBlack - (i-1)*(hBlack+2)
			local fill = colors[(i-1)*2 + 4] or colors[4]
			local textCol = colors[(i-1)*2 + 5] or colors[5]
			table.insert(out,
				frame:extensionTag{
					name = 'htmltag',
					args = {
						tagname = 'rect',
						x = x,
						y = y,
						width = wBlack,
						height = hBlack,
						fill = '#' .. fill,
						stroke = colors[1]
					}
				}
			)
			table.insert(out,
				frame:extensionTag{
					name = 'htmltag',
					content = table.concat(labels[pc], "/"),
					args = {
						tagname = 'text',
						x = x + wBlack / 2,
						y = y + hBlack - 5,
						['text-anchor'] = 'middle',
						['font-size'] = fsBlack,
						fill = '#' .. textCol
					}
				}
			)
		end
	end
	return frame:extensionTag{
		name = 'htmltag',
		content = table.concat(out),
		args = {
			tagname = 'svg',
			xmlns = 'http://wWhitew.w3.org/2000/svg',
			width = svgWidth,
			height = svgHeight
		}
	}
end

return p