$$include '../meta/macros.ptl'

import [mix linreg clamp fallback] from "@iosevka/util"
import [TieMark TieGlyph] from "@iosevka/glyph/relation"

glyph-module

glyph-block Mark-Tie : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives

	glyph-block-import Mark-Shared-Metrics : markExtend markHalfStroke markStress markFine
	glyph-block-import Mark-Shared-Metrics : markMiddle markDotsRadius
	glyph-block-import Mark-Above : aboveMarkTop aboveMarkBot aboveMarkMid aboveMarkStack
	glyph-block-import Mark-Above : TildeShape CaretCaronMidSw CaretCaronTerminalSw
	glyph-block-import Mark-Below : belowMarkBot belowMarkTop belowMarkMid

	define aboveTieBase   : aboveMarkStack - AccentClearance
	define aboveTieTop    : mix XH aboveMarkTop 2
	define aboveTieBottom : aboveTieTop - (aboveMarkTop - aboveMarkBot) * 0.875
	define aboveTieMid    : mix aboveTieBottom aboveTieTop 0.5

	define belowTieBase     Descender
	define belowTieBottom : belowTieBase - (aboveTieTop - aboveTieBase)
	define belowTieTop    : belowTieBase - (aboveTieBottom - aboveTieBase)
	define belowTieMid    : mix belowTieBottom belowTieTop 0.5

	local tieLeft [mix 0 markMiddle 1.5]
	local tieRight (-tieLeft)

	define [TieAboveAnchors] : glyph-proc
		set-mark-anchor 'tieAbove' 0 aboveTieBase 0 aboveTieTop
		set-base-anchor 'aboveBraceL' tieLeft aboveTieMid
		set-base-anchor 'aboveBraceR' tieRight aboveTieMid

	define [TieBelowAnchors] : glyph-proc
		set-mark-anchor 'tieBelow' 0 belowTieBase 0 belowTieBottom
		set-base-anchor 'belowBraceL' tieLeft belowTieMid
		set-base-anchor 'belowBraceR' tieRight belowTieMid

	create-glyph 'tieAbove' 0x361 : glyph-proc
		set-width 0
		include : TieAboveAnchors

		TieGlyph.set currentGlyph
		include : dispiro
			widths.center (markHalfStroke * 2)
			g4 tieLeft aboveTieBottom
			quadControls  0.5 1
			g4.right.mid  0 aboveTieTop [heading Rightward]
			quadControls  0.5 0
			g4 tieRight aboveTieBottom

	create-glyph 'turnTieAbove' 0x35D : glyph-proc
		set-width 0
		include : TieAboveAnchors

		TieGlyph.set currentGlyph
		include : dispiro
			widths.center (markHalfStroke * 2)
			g4 tieLeft aboveTieTop
			quadControls  0.5 1
			g4.right.mid  0 aboveTieBottom [heading Rightward]
			quadControls  0.5 0
			g4 tieRight aboveTieTop

	create-glyph 'tieBelow' 0x35C : glyph-proc
		set-width 0
		include : TieBelowAnchors
		TieGlyph.set currentGlyph
		include [refer-glyph 'tieAbove']
		include : FlipAround 0 [mix belowTieBase aboveTieBase 0.5]

	create-glyph 'turnTieBelow' 0x1DFC : glyph-proc
		set-width 0
		include : TieBelowAnchors
		TieGlyph.set currentGlyph
		include [refer-glyph 'turnTieAbove']
		include : FlipAround 0 [mix belowTieBase aboveTieBase 0.5]

	create-glyph 'overlineTieAbove' 0x35E : glyph-proc
		set-width 0
		include : TieAboveAnchors

		TieGlyph.set currentGlyph
		include : dispiro
			widths.center (markHalfStroke * 2)
			flat tieLeft aboveTieMid
			curl tieRight aboveTieMid

	create-glyph 'underlineTieBelow' 0x35F : glyph-proc
		set-width 0
		include : TieBelowAnchors
		TieGlyph.set currentGlyph
		include [refer-glyph 'overlineTieAbove']
		include : FlipAround 0 [mix belowTieBase aboveTieBase 0.5]

	create-glyph 'arrowTieBelow' 0x362 : glyph-proc
		set-width 0
		include : TieBelowAnchors

		TieGlyph.set currentGlyph
		include : HBar.m tieLeft tieRight belowTieMid (markFine * 2)
		include : new-glyph : glyph-proc
			local top : belowTieMid + (belowMarkTop - belowMarkBot) / 2 * 1.5
			local bot : belowTieMid - (belowMarkTop - belowMarkBot) / 2 * 1.5
			local left : tieRight - markExtend * 1.5
			local right tieRight
			local exp : Math.sqrt : 1 + (top - bot) / (2 * (right - left)) * (top - bot) / (2 * (right - left))
			include : dispiro
				widths.center : markFine * 2
				flat left top
				curl right [mix top bot 0.5] [widths.heading (markFine * exp) (markFine * exp) Downward]
			include : dispiro
				widths.center : markFine * 2
				flat left bot
				curl right [mix top bot 0.5] [widths.heading (markFine * exp) (markFine * exp) Upward]

	create-glyph 'tildeTieAbove' 0x360 : glyph-proc
		set-width 0
		include : TieAboveAnchors
		TieGlyph.set currentGlyph
		include : TildeShape
			ttop -- aboveTieTop
			tbot -- aboveTieBottom
			leftEnd -- tieLeft
			rightEnd -- tieRight
			hs -- markHalfStroke

	create-glyph 'circumflexTieAbove' 0x1DCD : glyph-proc
		set-width 0
		include : TieAboveAnchors

		TieGlyph.set currentGlyph
		local leftHalf : include : dispiro
			flat tieLeft aboveTieBottom [widths.center CaretCaronTerminalSw]
			curl 0 aboveTieTop [widths.center CaretCaronMidSw]
		local rightHalf : include : dispiro
			flat tieRight aboveTieBottom [widths.center CaretCaronTerminalSw]
			curl 0 aboveTieTop [widths.center CaretCaronMidSw]

		local a leftHalf.lhsKnots.1
		local b rightHalf.rhsKnots.1
		local c leftHalf.rhsKnots.1
		local d rightHalf.lhsKnots.1

		include : spiro-outline
			corner a.x a.y
			corner b.x b.y
			corner [Math.max c.x d.x] d.y
			corner [Math.min c.x d.x] c.y
