### Punctuation symbols
$$include '../../meta/macros.ptl'

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

glyph-module

glyph-block Symbol-Other-Phonetic : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives

	local triangleSize : Math.min (PeriodRadius) (XH / 6)
	local toneMarkLeft : mix SB RightSB 0.1
	local toneMarkRight : Width - toneMarkLeft

	define [dblToneMarkLeft df] : mix df.leftSB df.rightSB 0.1
	define [dblToneMarkRight df] : mix df.leftSB df.rightSB 0.9
	define [dblToneMarkMiddle df] : mix [dblToneMarkLeft df] [dblToneMarkRight df] 0.5

	create-glyph 'triangleColon' 0x2D0 : glyph-proc
		include : MarkSet.e
		include : union
			spiro-outline
				corner (Middle - triangleSize * 1.35) XH
				corner Middle (XH - triangleSize * 2.75)
				corner (Middle + triangleSize * 1.35) XH
			spiro-outline
				corner (Middle - triangleSize * 1.35) 0
				corner Middle (0 + triangleSize * 2.75)
				corner (Middle + triangleSize * 1.35) 0

	create-glyph 'halfTriangleColon' 0x2D1 : glyph-proc
		include : MarkSet.e
		include : spiro-outline
			corner (Middle - triangleSize * 1.35) XH
			corner Middle (XH - triangleSize * 2.75)
			corner (Middle + triangleSize * 1.35) XH

	define [yOfTone tone]        : mix (OperatorStroke / 2) (CAP - OperatorStroke / 2) (tone / 4)
	define [yOfToneNeutral tone] : mix DotRadius (CAP - DotRadius) (tone / 4)
	define [yOfToneDepart a b p] : mix (CAP * a + OperatorStroke / 2) (CAP * b - OperatorStroke / 2) p

	foreach tone [range 4 downtill 0] : begin
		create-glyph ('tone' + tone) (0x2E5 + 4 - tone) : glyph-proc
			include : VBar.r toneMarkRight 0 CAP OperatorStroke
			include : HBar.m toneMarkLeft (toneMarkRight - TINY) [yOfTone tone] OperatorStroke
		create-glyph ('toneSandhi' + tone) (0xA712 + 4 - tone) : glyph-proc
			include : VBar.l toneMarkLeft 0 CAP OperatorStroke
			include : HBar.m (toneMarkLeft + TINY) toneMarkRight [yOfTone tone] OperatorStroke
		create-glyph ('toneNeutral' + tone) (0xA708 + 4 - tone) : glyph-proc
			include : VBar.r toneMarkRight 0 CAP OperatorStroke
			include : DotAt (toneMarkLeft + DotRadius) [yOfToneNeutral tone] DotRadius
		create-glyph ('toneSandhiNeutral' + tone) (0xA70D + 4 - tone) : glyph-proc
			include : VBar.l toneMarkLeft 0 CAP OperatorStroke
			include : DotAt (toneMarkRight - DotRadius) [yOfToneNeutral tone] DotRadius

	# Connected tones
	foreach toneStart [range 4 downtill 0] : foreach toneEnd [range 4 downtill 0] : begin
		create-glyph ('toneStart' + toneStart + toneEnd) : glyph-proc
			Joining.set currentGlyph Joining.Classes.Right
			include : intersection
				Rect CAP 0 Middle Width
				dispiro
					widths.center OperatorStroke
					corner (Width / 2) [yOfTone toneStart]
					corner (Width + Width / 2) [yOfTone toneEnd]
		create-glyph ('toneSandhiStart' + toneStart + toneEnd) : glyph-proc
			Joining.set currentGlyph Joining.Classes.Right
			include : intersection
				Rect CAP 0 Middle Width
				dispiro
					widths.center OperatorStroke
					corner (0 - Width / 2) [yOfTone : mix toneEnd toneStart 2]
					corner (Width + Width / 2) [yOfTone toneEnd]
			include : VBar.m Middle 0 CAP OperatorStroke

	foreach toneStart [range 4 downtill 0] : foreach toneEnd [range 4 downtill 0] : begin
		create-glyph ('toneEnd' + toneStart + toneEnd) : glyph-proc
			Joining.set currentGlyph Joining.Classes.Left
			include : intersection
				Rect CAP 0 0 Middle
				dispiro
					widths.center OperatorStroke
					corner (0 - Width / 2)         [yOfTone toneStart]
					corner (Width + Width / 2)     [yOfTone : mix toneStart toneEnd 2]
			include : VBar.m Middle 0 CAP OperatorStroke
		create-glyph ('toneSandhiEnd' + toneStart + toneEnd) : glyph-proc
			Joining.set currentGlyph Joining.Classes.Left
			include : intersection
				Rect CAP 0 0 Width
				dispiro
					widths.center OperatorStroke
					corner (0 - Width / 2) [yOfTone toneStart]
					corner (Width / 2) [yOfTone toneEnd]

	foreach toneStart [range 4 downtill 0] : foreach toneMid [range 4 downtill 0] : foreach toneEnd [range 4 downtill 0] : begin
		create-glyph ('toneMid' + toneStart + toneMid + toneEnd) : glyph-proc
			Joining.set currentGlyph Joining.Classes.Mid
			include : intersection
				Rect CAP 0 0 Width
				union
					intersection
						dispiro
							widths.center OperatorStroke
							corner (0 - Width / 2)     [yOfTone toneStart]
							corner (Width + Width / 2) [yOfTone : mix toneStart toneMid 2]
						dispiro
							widths.center OperatorStroke
							corner (0 - Width / 2)     [yOfTone : mix toneEnd toneMid 2]
							corner (Width + Width / 2) [yOfTone toneEnd]
					dispiro
						widths.center OperatorStroke
						corner (0 - Width / 2) [yOfTone toneStart]
						corner (Width / 2)     [yOfTone toneMid]
					dispiro
						widths.center OperatorStroke
						corner (Width / 2)         [yOfTone toneMid]
						corner (Width + Width / 2) [yOfTone toneEnd]
		create-glyph ('toneSandhiMid' + toneStart + toneMid + toneEnd) : glyph-proc
			include [refer-glyph ('toneMid' + toneStart + toneMid + toneEnd)] AS_BASE ALSO_METRICS
			Joining.set currentGlyph Joining.Classes.Mid

	# Departing tones
	local DepartingToneConfig : list
		list 'departingToneYin'  0x2EA  0    0.5  0
		list 'departingToneYang' 0x2EB  0    0.5  0.5
		list 'beginHighTone'     0x2F9  0.6  1    1.0
		list 'beginLowTone'      0x2FB  0    0.4  0
	foreach { name code a b pos } [items-of DepartingToneConfig] : begin
		create-glyph name code : glyph-proc
			include : VBar.l toneMarkLeft (CAP * a) (CAP * b) OperatorStroke
			include : HBar.m (toneMarkLeft + TINY) toneMarkRight [yOfToneDepart a b pos] OperatorStroke

	local EnteringToneConfig : list
		list 'endHighTone'       0x2FA  0.6  1    1.0
		list 'endLowTone'        0x2FC  0    0.4  0
	foreach { name code a b pos } [items-of EnteringToneConfig] : begin
		create-glyph name code : glyph-proc
			include : VBar.r toneMarkRight (CAP * a) (CAP * b) OperatorStroke
			include : HBar.m toneMarkLeft (toneMarkRight - TINY) [yOfToneDepart a b pos] OperatorStroke

	local DblToneConfig : list
		list 'stressAndHighTone'     0xA720  0.6  1    1.0
		list 'stressAndLowTone'      0xA721  0    0.4  0
	foreach { name code a b pos } [items-of DblToneConfig] : begin
		create-glyph name code : glyph-proc
			local df : include : DivFrame 1 3.75
			local fine : OperatorStroke * df.mvs / Stroke
			include : VBar.l [dblToneMarkLeft df] (CAP * a) (CAP * b) fine
			include : VBar.m [dblToneMarkMiddle df] (CAP * a) (CAP * b) fine
			include : HBar.m ([dblToneMarkLeft df] + TINY) [dblToneMarkRight df] [yOfToneDepart a b pos] OperatorStroke
