-- lx-bundle:true

-- This file is part of Lx, created by Liqube <www.liqube.com>
-- Copyright Liqube Audio. All rights reserved.
-- Lx Interim License. See LICENSE for details.

--- Accent and rule definitions for mathematical expressions
-- @module plugins.mdpreview.math.accents

--[[
The current accents table conflates different layout operations. Use metadata.
]]

local ACCENTS = {
	-- Fixed-width accents
	hat = {
		glyph = "ˆ",
		position = "over",
		stretchy = false,
	},

	check = {
		glyph = "ˇ",
		position = "over",
		stretchy = false,
	},

	acute = {
		glyph = "´",
		position = "over",
		stretchy = false,
	},

	grave = {
		glyph = "ˋ",
		position = "over",
		stretchy = false,
	},

	dot = {
		glyph = "˙",
		dots = 1,
		position = "over",
		stretchy = false,
	},

	ddot = {
		glyph = "¨",
		dots = 2,
		position = "over",
		stretchy = false,
	},

	dddot = {
		glyph = "⃛",
		dots = 3,
		position = "over",
		stretchy = false,
	},

	ddddot = {
		glyph = "⃜",
		dots = 4,
		position = "over",
		stretchy = false,
	},

	breve = {
		glyph = "˘",
		position = "over",
		stretchy = false,
	},

	bar = {
		glyph = "¯",
		bar = true,
		position = "over",
		stretchy = false,
	},

	mathring = {
		glyph = "˚",
		position = "over",
		stretchy = false,
	},

	H = {
		glyph = "˝",
		position = "over",
		stretchy = false,
	},

	vec = {
		glyph = "⃗",
		position = "over",
		stretchy = false,
	},

	tilde = {
		glyph = "˜",
		position = "over",
		stretchy = false,
	},

	-- Stretchy accents
	widehat = {
		glyph = "̂",
		position = "over",
		stretchy = true,
	},

	widetilde = {
		glyph = "̃",
		position = "over",
		stretchy = true,
	},

	overrightarrow = {
		glyph = "→",
		position = "over",
		stretchy = true,
	},

	overleftarrow = {
		glyph = "←",
		position = "over",
		stretchy = true,
	},

	overleftrightarrow = {
		glyph = "↔",
		position = "over",
		stretchy = true,
	},

	underrightarrow = {
		glyph = "→",
		position = "under",
		stretchy = true,
	},

	underleftarrow = {
		glyph = "←",
		position = "under",
		stretchy = true,
	},

	underleftrightarrow = {
		glyph = "↔",
		position = "under",
		stretchy = true,
	},

	overbrace = {
		glyph = "⏞",
		position = "over",
		stretchy = true,
		attach_scripts = true,
	},

	underbrace = {
		glyph = "⏟",
		position = "under",
		stretchy = true,
		attach_scripts = true,
	},

	overbracket = {
		position = "over",
		rule = true,
		bracket = true,
		attach_scripts = true,
	},

	underbracket = {
		position = "under",
		rule = true,
		bracket = true,
		attach_scripts = true,
	},

	-- Group marks use font's matching stretchy parenthesis pieces
	overgroup = {
		glyph = "⏜",
		position = "over",
		stretchy = true,
	},

	undergroup = {
		glyph = "⏝",
		position = "under",
		stretchy = true,
	},

	-- Harpoons share same horizontal stretching path as arrows
	overleftharpoon = {
		glyph = "↼",
		position = "over",
		stretchy = true,
	},

	overrightharpoon = {
		glyph = "⇀",
		position = "over",
		stretchy = true,
	},
}

-- KaTeX aliases keep same drawing data instead of duplicating it
ACCENTS.Overrightarrow = ACCENTS.overrightarrow
ACCENTS.r = ACCENTS.mathring
-- TeX control symbols use same shapes as their named accent forms
ACCENTS["'"] = ACCENTS.acute
ACCENTS["`"] = ACCENTS.grave
ACCENTS["="] = ACCENTS.bar
ACCENTS["~"] = ACCENTS.tilde
ACCENTS["."] = ACCENTS.dot
ACCENTS['"'] = ACCENTS.ddot
ACCENTS.v = ACCENTS.check
ACCENTS["^"] = ACCENTS.hat
ACCENTS.u = ACCENTS.breve
-- Combining caron owns math font's wide check variants
ACCENTS.widecheck = {
	glyph = "̌",
	position = "over",
	stretchy = true,
}
ACCENTS.utilde = {
	glyph = ACCENTS.widetilde.glyph,
	position = "under",
	stretchy = true,
}

--[[
Treat these separately from accents:
]]

local LINES = {
	overline = {
		position = "over",
	},

	underline = {
		position = "under",
	},

	overlinesegment = {
		position = "over",
		segment = true,
	},

	underlinesegment = {
		position = "under",
		segment = true,
	},
}

-- Underbar is text-sized spelling of the same lower rule
LINES.underbar = LINES.underline

--[[
\overline{x} should draw a rule spanning the expression. It should not place the Unicode macron ¯ above it.

Similarly:

\bar{x} is a short accent.
\overline{x+y} is an extensible rule.
\vec{x} is usually a short vector accent.
\overrightarrow{AB} is extensible.
\underline{x} is a rule, not the _ character.

For reliable rendering, combining marks such as ̂ and ⃗ should be replaced by the accent glyph and attachment data from the font’s OpenType MATH table when available.
]]

return {
	accents = ACCENTS,
	lines = LINES,
}
