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

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

glyph-module

glyph-block Letter-Cyrillic-De : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Adjustment : ExtendBelowBaseAnchors
	glyph-block-import Letter-Cyrillic-Ze : CyrZe ZeConfig

	glyph-block-export BottomExtension
	local BottomExtension : (-LongVJut) + QuarterStroke

	glyph-block-export CyrDeBottom
	define [CyrDeBottom left right _sw _desc] : glyph-proc
		local descenderOverflow : if SLAB SideJut ((right - left) * 0.075)
		local xTopLeft : mix left right : StrokeWidthBlend 0.15 0.1
		local xTopRight : mix left right : StrokeWidthBlend 0.95 0.96
		local swOuter : fallback _sw Stroke
		local desc : fallback _desc BottomExtension

		include : HBar.b xTopLeft xTopRight 0 swOuter
		include : tagged 'footL' : union
			HBar.b (left - descenderOverflow) xTopLeft 0 swOuter
			VBar.l (left - descenderOverflow) desc 0.1 swOuter
		include : tagged 'footR' : union
			HBar.b xTopRight (right + descenderOverflow) 0 swOuter
			VBar.r (right + descenderOverflow) desc 0.1 swOuter

	glyph-block-export CyrDeShape
	define [CyrDeShape top left right _sw _desc fLower] : glyph-proc
		local descenderOverflow : if SLAB SideJut ((right - left) * 0.075)
		local xTopLeft : mix left right : StrokeWidthBlend 0.15 0.1
		local xTopRight : mix left right : StrokeWidthBlend 0.95 0.96
		local swOuter : fallback _sw Stroke
		local swInner : swOuter * [AdviceStroke 2.75] / Stroke
		local desc : fallback _desc BottomExtension

		include : CyrDeBottom left right _sw _desc

		include : VBar.r xTopRight 0 top swInner
		include : dispiro
			widths.lhs swInner
			flat xTopLeft top
			curl xTopLeft [mix 0 top 0.625]
			g4   left  swOuter

		include : if SLAB
			then : dispiro
				widths.rhs swOuter
				flat (xTopLeft - descenderOverflow) top
				if (fLower && para.isItalic)
					then : list : curl xTopRight top [heading Rightward]
					else : list : curl (xTopRight + descenderOverflow) top
			else : HBar.t xTopLeft xTopRight top swOuter

		return : object desc xTopLeft xTopRight

	define [CyrSoftDeShape df top _sw _desc fLower vSlab] : glyph-proc
		local descenderOverflow : if SLAB SideJut ((df.rightSB - df.leftSB) * 0.075)
		local sw : fallback _sw Stroke
		local xm : if SLAB
			[mix df.leftSB df.rightSB 0.625] + [HSwToV : 0.25 * sw]
			mix df.leftSB df.rightSB : if (df.adws > 1) (2 / 3) (3 / 4)
		local xTopRight : mix df.leftSB xm : StrokeWidthBlend 0.95 0.96
		local xTopBarRightEnd : mix df.width df.rightSB : if vSlab 0.25 0.375

		include : CyrDeShape top df.leftSB xm sw _desc fLower

		include : if (SLAB && !(fLower && para.isItalic))
			then : HBar.t (xTopRight + descenderOverflow) xTopBarRightEnd top sw
			else : HBar.t xTopRight xTopBarRightEnd top sw

		if vSlab : include : VSerif.dr xTopBarRightEnd top VJut (VJutStroke * sw / Stroke)

	create-glyph 'cyrl/De' 0x414 : glyph-proc
		include : MarkSet.capital
		include : ExtendBelowBaseAnchors BottomExtension
		include : CyrDeShape CAP SB RightSB

	create-glyph 'cyrl/de.upright' : glyph-proc
		include : MarkSet.e
		include : ExtendBelowBaseAnchors BottomExtension
		include : CyrDeShape XH SB RightSB

	create-glyph 'cyrl/deLongLeg' 0x1C81 : glyph-proc
		include : MarkSet.p
		include : CyrDeShape XH SB RightSB nothing Descender true

	create-glyph 'cyrl/DeSoft.serifless' : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		include : df.markSet.capital
		include : ExtendBelowBaseAnchors BottomExtension
		include : CyrSoftDeShape df CAP df.mvs nothing false false

	create-glyph 'cyrl/DeSoft.topRightSerifed' : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		include : df.markSet.capital
		include : ExtendBelowBaseAnchors BottomExtension
		include : CyrSoftDeShape df CAP df.mvs nothing false true

	select-variant 'cyrl/DeSoft' 0xA662 (follow -- 'cyrl/EnGhe/GhePart')

	create-glyph 'cyrl/deSoft.serifless' : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		include : df.markSet.e
		include : ExtendBelowBaseAnchors BottomExtension
		include : CyrSoftDeShape df XH df.mvs nothing true false

	create-glyph 'cyrl/deSoft.topRightSerifed' : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		include : df.markSet.e
		include : ExtendBelowBaseAnchors BottomExtension
		include : CyrSoftDeShape df XH df.mvs nothing true true

	select-variant 'cyrl/deSoft' 0xA663 (follow -- 'cyrl/enghe/ghePart')

	foreach { suffix { st sb }} [Object.entries ZeConfig] : do
		define [DzzeDescendershape de] : begin
			local sr : [mix RightSB Width 0.5] - O * 2
			local sl : mix sr de.xTopRight 2
			local sw : AdviceStroke 2.5 ((sr - sl + 2 * SB) / Width)
			local shapeBot : de.desc - 0.5 * sw
			local hook : Hook * (0 - shapeBot) / CAP
			local ze : CyrZe 3 sb sw shapeBot
				left   -- sl
				right  -- sr
				hook   -- hook
				stroke -- sw
				xo     -- (0.5 * O)
				op     -- 0.5
			return : union [ze.Shape] [ze.AutoEndSerifL]

		create-glyph "cyrl/Dzze.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : ExtendBelowBaseAnchors BottomExtension
			local de : include : CyrDeShape CAP SB RightSB
			eject-contour 'footR'
			include : DzzeDescendershape de

		create-glyph "cyrl/dzze.upright.\(suffix)" : glyph-proc
			include : MarkSet.e
			include : ExtendBelowBaseAnchors BottomExtension
			local de : include : CyrDeShape XH SB RightSB
			eject-contour 'footR'
			include : DzzeDescendershape de

	## Italic
	glyph-block-export CyrDeItalicShapeT
	define [CyrDeItalicShapeT sink df _sw] : begin
		local left  df.leftSB
		local right df.rightSB
		local middle : mix left right 0.5
		local sw : fallback _sw df.mvs

		local yRingTop : Math.min (XH + O) (XH - QuarterStroke)
		local ada : df.archDepthAOf : SmallArchDepth * yRingTop / XH
		local adb : df.archDepthBOf : SmallArchDepth * yRingTop / XH

		return : sink
			widths.lhs ShoulderFine
			straight.up.start (right - OX - [HSwToV : sw - ShoulderFine]) (yRingTop - adb)
			arch.lhs yRingTop (sw -- sw) (swBefore -- ShoulderFine)
			flat (left + OX) (yRingTop - ada)
			curl (left + OX) adb
			arch.lhs 0 (sw -- sw)
			flat (right - OX) ada
			curl (right - OX) (yRingTop - adb)
			quadControls 0 0.8
			g4 (left + sw * 1.1) Ascender

	create-glyph 'cyrl/de.italic' : glyph-proc
		local df : include : DivFrame 1
		include : df.markSet.b
		include : CyrDeItalicShapeT dispiro df

	foreach { suffix { st sb }} [Object.entries ZeConfig] : do
		create-glyph "cyrl/dzze.italic.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleMM 3
			include : df.markSet.bp

			local dfLeft : df.slice 3 2 OX
			include : CyrDeItalicShapeT dispiro dfLeft

			local xZeLeft : dfLeft.leftSB + df.width - dfLeft.width + OX
			local xZeRight : dfLeft.rightSB + df.width - dfLeft.width - OX
			local ze : CyrZe 1 sb XH Descender
				left     -- xZeLeft
				right    -- xZeRight
				blend    -- 0.7
				hook     -- Hook
				overflow -- 0
				stroke   -- df.mvs
			include : union [ze.Shape] [ze.AutoEndSerifL]

	select-variant 'cyrl/Dzze' 0xA688  (follow -- 'cyrl/ZeBottomSerifOnly')
	select-variant 'cyrl/dzze.upright' (follow -- 'cyrl/zeBottomSerifOnly')
	select-variant 'cyrl/dzze.italic'  (follow -- 'cyrl/zeBottomSerifOnly')
