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

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

glyph-module

glyph-block Letter-Cyrillic-Dche : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Adjustment : LeaningAnchor ExtendBelowBaseAnchors
	glyph-block-import Letter-Cyrillic-Che : CyrCheShape ItalicConfig BODY SERIFS
	glyph-block-import Letter-Cyrillic-De : CyrDeBottom BottomExtension CyrDeItalicShapeT

	create-glyph 'cyrl/Dche' 0x52C : glyph-proc
		include : MarkSet.capital
		include : ExtendBelowBaseAnchors BottomExtension
		include : CyrCheShape [DivFrame 1] CAP [if SLAB 0.45 0.4] BODY.STRAIGHT
			if SLAB SERIFS.TOP SERIFS.NONE
		include : CyrDeBottom SB RightSB

		local fine : AdviceStroke 3.5
		include : dispiro
			flat SB Stroke [widths.rhs fine]
			curl (RightSB - [HSwToV Stroke]) (CAP - [if SLAB Stroke 0])

	create-glyph 'cyrl/dche.upright' : glyph-proc
		include : MarkSet.e
		include : ExtendBelowBaseAnchors BottomExtension
		include : CyrCheShape [DivFrame 1] XH [if SLAB 0.45 0.4] BODY.STRAIGHT
			if SLAB SERIFS.TOP SERIFS.NONE
		include : CyrDeBottom SB RightSB

		local fine : AdviceStroke 3.5
		include : dispiro
			flat SB Stroke [widths.rhs fine]
			curl (RightSB - [HSwToV Stroke]) (XH - [if SLAB Stroke 0])

	foreach { suffix { body slabUpright slabUprightBGR slabItalic } } [pairs-of ItalicConfig] : do
		create-glyph "cyrl/dche.italic.\(suffix)" : glyph-proc
			define df : include : DivFrame para.advanceScaleM 3
			include : df.markSet.e

			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 : CyrDeItalicShapeT dispiro subDf df.mvs
			include : difference
				CyrCheShape df XH 0.5 body [if SLAB slabItalic SERIFS.NONE] (sw -- df.mvs)
				MaskLeft : subDf.rightSB - [HSwToV : 0.5 * df.mvs]

	select-variant 'cyrl/dche.italic' (follow -- 'cyrl/che')
