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

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

glyph-module

glyph-block Letter-Cyrillic-Yu : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Cyrillic-Iotified-A : Iotified

	define SLAB-ALL        1
	define SLAB-LOWER      2
	define SLAB-BULGARIAN  3

	define SLAB-NONE		0
	define SLAB-FULL		1
	define SLAB-OUTWARD		2

	define [CyrYuShape df slabType top xtop ada adb] : glyph-proc
		local gap : 0.375 * (df.width - 2 * df.leftSB - 2.5 * df.mvs) - [HSwToV : 0.25 * df.mvs]
		local divSub : (df.width - gap - df.mvs) / Width
		local subDf : DivFrame divSub 2

		local shift : Width * (df.adws - divSub)
		include : with-transform [ApparentTranslate shift 0]
			OShape top 0 subDf.leftSB subDf.rightSB df.mvs (ada * 0.7 * df.adws) (adb * 0.7 * df.adws)

		include : if (slabType === SLAB-BULGARIAN)
			Iotified.ascender df xtop (shift + subDf.leftSB + [HSwToV : 0.5 * df.mvs]) (top / 2)
			Iotified.full     df xtop (shift + subDf.leftSB + [HSwToV : 0.5 * df.mvs]) (top / 2)
				fCapital -- (slabType === SLAB-ALL)

	define [CyrRevYuShape df slabType top xtop ada adb fTail] : glyph-proc
		local gap : 0.375 * (df.width - 2 * df.leftSB - 2.5 * df.mvs) - [HSwToV : 0.25 * df.mvs]
		local divSub : (df.width - gap - df.mvs) / Width
		local subDf : DivFrame divSub 2

		include : OShape top 0 subDf.leftSB subDf.rightSB df.mvs (ada * 0.7 * df.adws) (adb * 0.7 * df.adws)

		local useItalicShape : slabType !== SLAB-ALL && para.isItalic
		local slabTop : if useItalicShape SLAB-NONE SLAB-FULL
		local slabBottom : if useItalicShape SLAB-OUTWARD SLAB-FULL

		include : Iotified.RevShape df xtop
			hBarLeft   -- (subDf.rightSB - [HSwToV : 0.5 * df.mvs])
			hBarY      -- (top / 2)
			slabTop    -- slabTop
			slabBottom -- slabBottom
			fTail      -- fTail

	create-glyph 'cyrl/Yu' 0x42E : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		include : df.markSet.capital
		include : CyrYuShape df SLAB-ALL CAP CAP ArchDepthA ArchDepthB

	create-glyph 'cyrl/yu' 0x44E : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		include : df.markSet.e
		include : CyrYuShape df SLAB-LOWER XH XH SmallArchDepthA SmallArchDepthB

	create-glyph 'cyrl/yu.BGR' : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		include : df.markSet.b
		include : CyrYuShape df SLAB-BULGARIAN XH Ascender SmallArchDepthA SmallArchDepthB

	define YuRevConfig : object
		straight  false
		tailed    true

	foreach { suffix fTail } [Object.entries YuRevConfig] : do
		if [not fTail] : create-glyph "cyrl/YuRev.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleM 3
			include : df.markSet.capital
			include : CyrRevYuShape df SLAB-ALL CAP CAP ArchDepthA ArchDepthB fTail
		create-glyph "cyrl/yuRev.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleM 3
			include : df.markSet.e
			include : CyrRevYuShape df SLAB-LOWER XH XH SmallArchDepthA SmallArchDepthB fTail

	select-variant 'cyrl/YuRev' 0xA654
	select-variant 'cyrl/yuRev' 0xA655
