Module:calligraphy

From Wiktionary, the free dictionary
Jump to navigation Jump to search
This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

This module implements {{calligraphy}}.


local m_str_utils = require("Module:string utilities")

local codepoint = m_str_utils.codepoint
local u = m_str_utils.char
local ugsub = m_str_utils.gsub

local export = {}

local code_points = {
	pattern = {
		Latn_capital = "[A-Z]",
		Latn_small = "[a-z]",
		Grek_capital = "[Α-Ω]",
		Grek_small = "[α-ω]",
		digit = "[0-9]"
	},
	normal = {
		Latn = 65,
		Grek = 913,
		digit = 48
	},
	b = {
		Latn = 119808,
		Grek = 120488,
		digit = 120782
	},
	i = {
		Latn = 119860,
		Grek = 120546,
		digit = 48
	},
	bi = {
		Latn = 119912,
		Grek = 120604,
		digit = 48
	},
	s = {
		Latn = 119964,
		Grek = 913,
		digit = 48
	},
	bs = {
		Latn = 120016,
		Grek = 913,
		digit = 48
	},
	f = {
		Latn = 120068,
		Grek = 913,
		digit = 48
	},
	ds = {
		Latn = 120120,
		Grek = 913,
		digit = 120792
	},
	bf = {
		Latn = 120172,
		Grek = 913,
		digit = 48
	},
	ss = {
		Latn = 120224,
		Grek = 913,
		digit = 120802
	},
	ssb = {
		Latn = 120276,
		Grek = 120662,
		digit = 120812
	},
	ssi = {
		Latn = 120328,
		Grek = 913,
		digit = 48
	},
	ssbi = {
		Latn = 120380,
		Grek = 120720,
		digit = 48
	},
	m = {
		Latn = 120432,
		Grek = 913,
		digit = 120822
	},
}

local fixes = { -- some characters are in the BMP, separated from their peers
	["i"] = {
		["𝑕"]="ℎ"
	},
	["s"] = {
		["𝒝"]="ℬ", ["𝒠"]="ℰ", ["𝒡"]="ℱ", ["𝒣"]="ℋ", ["𝒤"]="ℐ", ["𝒧"]="ℒ", ["𝒨"]="ℳ", ["𝒭"]="ℛ", ["𝒺"]="ℯ", ["𝒼"]="ℊ", ["𝓄"]="ℴ"
	},
	["f"] = {
		["𝔆"]="ℭ", ["𝔋"]="ℌ", ["𝔌"]="ℑ", ["𝔕"]="ℜ", ["𝔝"]="ℨ"
	},
	["ds"] = {
		["𝔺"]="ℂ", ["𝔿"]="ℍ", ["𝕅"]="ℕ", ["𝕇"]="ℙ", ["𝕈"]="ℚ", ["𝕉"]="ℝ", ["𝕑"]="ℤ"
	}
};

function export.show(frame)
	local mode = frame:getParent().args[1]
	local text = frame:getParent().args[2]
	text = ugsub(text, code_points.pattern.Latn_capital, function(a)
		return u(codepoint(a) - code_points.normal.Latn + code_points[mode].Latn)
	end)
	text = ugsub(text, code_points.pattern.Latn_small, function(a)
		return u(codepoint(a) - code_points.normal.Latn + code_points[mode].Latn - 6)
	end)
	text = ugsub(text, code_points.pattern.Grek_capital, function(a)
		return u(codepoint(a) - code_points.normal.Grek + code_points[mode].Grek)
	end)
	text = ugsub(text, code_points.pattern.Grek_small, function(a)
		return u(codepoint(a) - code_points.normal.Grek + code_points[mode].Grek - 6)
	end)
	text = ugsub(text, code_points.pattern.digit, function(a)
		return u(codepoint(a) - code_points.normal.digit + code_points[mode].digit)
	end)
	if mode == "i" or mode == "s" or mode == "f" or mode == "ds" then
		text = ugsub(text, ".", fixes[mode])
	end
	return text
end

return export