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

import [mix fallback SuffixCfg] from "@iosevka/util"


glyph-module

glyph-block Letter-Latin-Ezh : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Shared-Shapes : CurlyTail SerifedArcEnd PalatalHook RetroflexHook

	define [pArc ada adb] : adb / (ada + adb)

	define [StdTerminalShape df top bot yMidBar sw hook ada adb] : list
		g4 (df.rightSB - OX) [mix yMidBar bot : pArc ada adb]
		hookend bot
		g4 df.leftSB (bot + hook)

	define [HooklessTerminalShape p] : function [df top bot yMidBar sw hook ada adb] : list
		g4.down.mid  (df.rightSB - OX) [mix yMidBar bot p] [heading Downward]

	define [RetroflexConnectionTerminal df top bot yMidBar sw hook ada adb] : list
		g4 (df.rightSB - OX) [mix yMidBar bot : pArc ada adb]
		SerifedArcEnd.RtlRhs df.leftSB bot sw hook

	define [CurlyTailTerminalShape df top bot yMidBar sw hook ada adb] : begin
		local fine : df.adviceStroke2 3.5 5 (top - bot)
		return : list
			g4 (df.rightSB - OX) [mix yMidBar bot : pArc ada adb]
			CurlyTail.n fine bot df.leftSB df.width (bot + 0.5 * fine)
				yLoopTop -- ([mix yMidBar bot 0.5] + 0.25 * fine)
				swBefore -- sw

	define [ConventionalStart df top bot ezhLeft ezhRight yMidBar sw hook] : glyph-proc
		include : HBar.t df.leftSB ezhRight top sw
		include : dispiro
			corner ezhRight (top - sw) [widths.rhs : VSwToH sw]
			corner ezhLeft  yMidBar    [widths.lhs : VSwToH sw]

	define [CurisveStart df top bot ezhLeft ezhRight yMidBar sw hook] : glyph-proc
		define hookTerminalWidth : sw * ([AdviceStroke 3.5] / Stroke)
		define xDiagWidth : 1 * sw
		define yFootHeight : [Math.max (0.15 * (top - bot)) (sw * 0.625)] + 0.4 * sw
		define yHookDepth : hook + sw * 0.25
		define yHookStraightDepth : Math.min (yHookDepth - sw * 1.1) (yHookDepth / 3 - sw / 4)
		define xHookDepth : Math.max (0.25 * (df.rightSB - df.leftSB)) (hookTerminalWidth * 1.5)
		define xMockTailDepth : Math.max (0.375 * (df.rightSB - df.leftSB)) (hookTerminalWidth * 1.375)
		define kTop 0.625
		define kBot 0.625
		define yTailDepth : hook * 0.5

		include : tagged 'strokeTop' : intersection
			spiro-outline
				corner (-df.width) bot
				corner (-df.width) (2 * top)
				corner (ezhRight - xDiagWidth + TINY) (2 * top)
				corner (ezhRight - xDiagWidth + TINY) (top - yFootHeight)
				corner (ezhLeft + xDiagWidth) yMidBar
				corner (ezhLeft + xDiagWidth) bot
			dispiro
				flat (df.leftSB + OX) (top - yHookDepth) [widths.rhs.heading hookTerminalWidth Upward]
				curl (df.leftSB + OX) (top - yHookDepth + yHookStraightDepth) [heading Upward]
				arcvh
				g2.right.mid (df.leftSB + xHookDepth) (top - O) [widths.rhs.heading sw Rightward]
				flat [mix (df.leftSB + xMockTailDepth) ezhRight kTop] (top - kTop * yTailDepth)
				curl [mix (df.leftSB + xMockTailDepth) ezhRight 4]    (top - 4    * yTailDepth)

		include : VBar.r ezhRight top (top - yFootHeight) [VSwToH xDiagWidth]
		include : dispiro
			corner ezhRight (top - yFootHeight) [widths.rhs : VSwToH sw]
			corner ezhLeft  yMidBar             [widths.lhs : VSwToH sw]

	glyph-block-export EzhShape
	define flex-params [EzhShape] : glyph-proc
		local-parameter : df
		local-parameter : top
		local-parameter : bot
		local-parameter : pLeft         -- 0.2
		local-parameter : pRight        -- 0.925
		local-parameter : terminalShape -- StdTerminalShape
		local-parameter : isCursive     -- false
		local-parameter : isSerifed     -- SLAB
		local-parameter : sw            -- Stroke
		local-parameter : hook          -- Hook
		local-parameter : ada           -- SmallArchDepthA
		local-parameter : adb           -- SmallArchDepthB

		local yMidBar : [mix bot top : if isCursive 0.5 0.55] + 0.5 * sw
		local ezhLeft  : mix df.leftSB df.rightSB pLeft
		local ezhRight : mix df.leftSB df.rightSB pRight

		include : union
			if isCursive
				CurisveStart      df top bot ezhLeft ezhRight yMidBar sw hook
				ConventionalStart df top bot ezhLeft ezhRight yMidBar sw hook
			dispiro
				widths.rhs sw
				flat ezhLeft yMidBar [heading Rightward]
				curl [arch.adjust-x.top df.middle] yMidBar
				archv
				terminalShape df top bot yMidBar sw hook ada adb

		if isSerifed : include : VSerif.dl df.leftSB top VJut (sw / Stroke * VJutStroke)

		return : object yMidBar

	define EzhConfig : object
		straightSerifless  { false false }
		straightTopSerifed { false true  }
		cursive            { true  false }

	foreach { suffix { isCursive isSerifed } } [pairs-of EzhConfig] : do
		create-glyph "Ezh.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : EzhShape [DivFrame 1] CAP 0
				isCursive -- isCursive
				isSerifed -- isSerifed
				hook      -- Hook
				ada       -- ArchDepthA
				adb       -- ArchDepthB

		create-glyph "smcpEzh.\(suffix)" : glyph-proc
			include : MarkSet.e
			include : EzhShape [DivFrame 1] XH 0
				isCursive -- isCursive
				isSerifed -- isSerifed
				hook      -- Hook
				ada       -- ArchDepthA
				adb       -- ArchDepthB

		create-glyph "ezh.\(suffix)" : glyph-proc
			include : MarkSet.p
			include : EzhShape [DivFrame 1] XH Descender
				isCursive -- isCursive
				isSerifed -- isSerifed
				hook      -- Hook
				ada       -- SmallArchDepthA
				adb       -- SmallArchDepthB

		create-glyph "ezhTail.\(suffix)" : glyph-proc
			include : MarkSet.p
			local b : mix Descender XH 0.25
			local [object yMidBar] : include : EzhShape [DivFrame 1] XH b
				isCursive -- isCursive
				isSerifed -- isSerifed
				ada       -- SmallArchDepthA
				adb       -- SmallArchDepthB
				terminalShape -- [HooklessTerminalShape 0.5]
			local y : mix yMidBar b 0.5
			include : dispiro
				widths.rhs
				g4.down.mid (RightSB - OX) y [heading Downward]
				arcvh
				flat [mix SB RightSB 0.45] b
				curl [mix SB RightSB 0.4]  b
				archv
				g4 (SB + [HSwToV Stroke]) [mix (Descender + Stroke) b 0.5]
				arcvh
				flat [mix SB RightSB 0.4] (Descender + Stroke)
				curl RightSB (Descender + Stroke)

		create-glyph "ezhCurlyTail.\(suffix)" : glyph-proc
			include : MarkSet.p
			include : EzhShape [DivFrame 1] XH Descender
				isCursive -- isCursive
				isSerifed -- isSerifed
				hook      -- Hook
				ada       -- SmallArchDepthA
				adb       -- SmallArchDepthB
				terminalShape -- CurlyTailTerminalShape

		create-glyph "ezhRetroflexHook.\(suffix)" : glyph-proc
			include : MarkSet.e
			include : EzhShape [DivFrame 1] XH 0
				isCursive -- isCursive
				isSerifed -- isSerifed
				hook      -- SHook
				ada       -- SmallArchDepthA
				adb       -- SmallArchDepthB
				terminalShape -- RetroflexConnectionTerminal
			include : RetroflexHook.l
				x -- SB
				y -- 0
				yAttach -- SHook

		create-glyph "ezhPalatalHook.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleM 1
			include : df.markSet.p
			local dfSub : DivFrame (0.75 * para.advanceScaleM) 2
			local [object yMidBar] : include : EzhShape dfSub XH Descender
				isCursive -- isCursive
				isSerifed -- isSerifed
				sw        -- dfSub.mvs
				hook      -- Hook
				ada       -- dfSub.smallArchDepthA
				adb       -- dfSub.smallArchDepthB
			local y : [mix yMidBar Descender : pArc dfSub.smallArchDepthA dfSub.smallArchDepthB] - 0.5 * dfSub.mvs
			include : PalatalHook.r
				x       -- df.rightSB
				y       -- y
				xLink   -- dfSub.rightSB
				refSw   -- dfSub.mvs
				maskOut -- [intersection [MaskBelow y] [MaskLeft dfSub.rightSB]]

		if [not isSerifed] : begin
			create-glyph "ezh/phoneticRight.\(suffix)" : glyph-proc
				include : MarkSet.p
				include : EzhShape [DivFrame 1] XH Descender
					pLeft     -- (4/15)
					isCursive -- isCursive
					isSerifed -- isSerifed
					hook      -- Hook
					ada       -- SmallArchDepthA
					adb       -- SmallArchDepthB

			create-glyph "ezhRetroflexHook/phoneticRight.\(suffix)" : glyph-proc
				include : MarkSet.p
				local b : Math.min (O - SHook) ((-LongVJut) + QuarterStroke)
				include : EzhShape [DivFrame 1] XH b
					pLeft     -- (4/15)
					isCursive -- isCursive
					isSerifed -- isSerifed
					hook      -- SHook
					ada       -- SmallArchDepthA
					adb       -- SmallArchDepthB
					terminalShape -- RetroflexConnectionTerminal
				include : RetroflexHook.l
					x -- SB
					y -- b
					yAttach -- (b + SHook)

			create-glyph "ezhPalatalHook/phoneticRight.\(suffix)" : glyph-proc
				include : MarkSet.p
				local [object yMidBar] : include : EzhShape [DivFrame 1] XH Descender
					pLeft     -- (4/15)
					isCursive -- isCursive
					isSerifed -- isSerifed
					hook      -- Hook
					ada       -- SmallArchDepthA
					adb       -- SmallArchDepthB
				local y : [mix yMidBar Descender : pArc SmallArchDepthA SmallArchDepthB] - HalfStroke
				include : PalatalHook.r
					x       -- [mix SB RightSB (4/3)]
					y       -- y
					xLink   -- RightSB
					refSw   -- [AdviceStroke 3]
					maskOut -- [intersection [MaskBelow y] [MaskLeft RightSB]]

	select-variant 'Ezh' 0x1B7
	select-variant 'smcpEzh' 0x1D23 (follow -- 'Ezh')
	select-variant 'ezh' 0x292
	select-variant 'ezhTail' 0x1BA (follow -- 'ezh')
	select-variant 'ezhCurlyTail' 0x293 (follow -- 'ezh')
	select-variant 'ezhRetroflexHook' 0x1D9A (follow -- 'ezh')
	select-variant 'ezhPalatalHook' 0x1DF18 (follow -- 'ezh')
	select-variant 'ezh/phoneticRight'
	select-variant 'ezhRetroflexHook/phoneticRight' (follow -- 'ezh/phoneticRight')
	select-variant 'ezhPalatalHook/phoneticRight' (follow -- 'ezh/phoneticRight')

	alias 'cyrl/abk/Dze' 0x4E0 'Ezh'
	alias 'cyrl/abk/dze' 0x4E1 'ezh'

	# Variants for Ezh don't make sense for Lyogh.
	create-glyph 'lyogh.serifless' : glyph-proc
		include : MarkSet.bp
		include : EzhShape [DivFrame 1] XH Descender
			pLeft     -- 0.4
			isSerifed -- false
			hook      -- Hook
			ada       -- SmallArchDepthA
			adb       -- SmallArchDepthB
		include : VBar.l SB (XH * 0.1) Ascender

	create-glyph 'lyogh.hooky' : glyph-proc
		include [refer-glyph 'lyogh.serifless'] AS_BASE ALSO_METRICS
		include : HSerif.lt SB Ascender SideJut

	select-variant 'lyogh' 0x26E

	create-glyph 'lyoghRTail.serifless' : glyph-proc
		include : MarkSet.b
		include : EzhShape [DivFrame 1] XH 0
			pLeft     -- 0.4
			isSerifed -- false
			hook      -- SHook
			ada       -- SmallArchDepthA
			adb       -- SmallArchDepthB
			terminalShape -- RetroflexConnectionTerminal
		include : VBar.l SB 0 Ascender
		include : RetroflexHook.lExt SB 0

	create-glyph 'lyoghRTail.hooky' : glyph-proc
		include [refer-glyph 'lyoghRTail.serifless'] AS_BASE ALSO_METRICS
		include : HSerif.lt SB Ascender SideJut

	select-variant 'lyoghRTail' 0x1DF05 (follow -- 'lyogh')


	# Used by ampersand only.
	# Current reversed Ezh is generated using an auto-build.
	glyph-block-export RevEzhShape
	define flex-params [RevEzhShape] : glyph-proc
		local-parameter : top
		local-parameter : bot
		local-parameter : pLeft     -- 0.075
		local-parameter : pRight    -- 0.8
		local-parameter : hookless  -- false
		local-parameter : ada       -- SmallArchDepthA
		local-parameter : adb       -- SmallArchDepthB
		local-parameter : diagCoeff -- 1.2
		local-parameter : pyBar     -- 0.6

		local cor : HSwToV diagCoeff
		local yMidBar : RevEzhShape.yMidBar top bot pyBar
		local ezhRight : mix SB RightSB pRight
		local ezhLeft  : mix SB RightSB pLeft

		include : HBar.t ezhLeft RightSB top
		include : dispiro
			corner ezhLeft (top - Stroke) [widths.lhs : VSwToH Stroke]
			corner ezhRight yMidBar       [widths.rhs : VSwToH Stroke]

		include : dispiro
			widths.lhs
			flat ezhRight yMidBar [heading Leftward]
			curl [arch.adjust-x.bot Middle] yMidBar
			archv
			if hookless
			: then : list
				g4.down.mid (SB + OX) [RevEzhShape.yLoopLeft top bot pyBar ada adb]
			: else : list
				g4 (SB + OX) [RevEzhShape.yLoopLeft top bot pyBar ada adb]
				hookend bot
				g4 RightSB (bot + Hook * ((top - bot) / CAP))
		if SLAB : begin
			include : VSerif.dr RightSB top VJut
	set RevEzhShape.yMidBar : lambda [top bot pyBar] : mix bot top pyBar
	set RevEzhShape.yLoopLeft : lambda [top bot pyBar ada adb]
		mix [RevEzhShape.yMidBar top bot pyBar] bot (ada / (ada + adb))
