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

import [mix clamp fallback SuffixCfg] from "@iosevka/util"
import [MathSansSerif] from "@iosevka/glyph/relation"

glyph-module

glyph-block Letter-Latin-Lower-D : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Shared-Metrics : markExtend
	glyph-block-import Mark-Adjustment : LeaningAnchor
	glyph-block-import Letter-Shared : CreateCommaCaronComposition
	glyph-block-import Letter-Shared-Shapes : OBarRight RightwardTailedBar CurlyTail
	glyph-block-import Letter-Shared-Shapes : LetterBarOverlay SerifFrame UpwardHookShape
	glyph-block-import Letter-Shared-Shapes : PalatalHook RetroflexHook TopHook

	define [ToothedBody df yTop] : union
		OBarRight.shape
			left  -- df.leftSB
			right -- df.rightSB
			sw    -- df.mvs
			ada   -- df.smallArchDepthA
			adb   -- df.smallArchDepthB
		tagged 'rightBar' : VBar.r df.rightSB 0 yTop

	define [ToothlessCornerBody df yTop] : union
		OBarRight.toothless
			left   -- df.leftSB
			right  -- df.rightSB
			sw     -- df.mvs
			rise   -- DToothlessRise
			mBlend -- DMBlend
			ada    -- df.smallArchDepthA
			adb    -- df.smallArchDepthB
		tagged 'rightBar' : VBar.r df.rightSB DToothlessRise yTop

	define [ToothlessCornerHBBBody df yTop] : union
		OBarRight.toothless
			left   -- df.leftSB
			right  -- df.rightSB
			sw     -- df.mvs
			rise   -- DToothlessRise
			mBlend -- DMBlend
			ada    -- df.smallArchDepthA
			adb    -- df.smallArchDepthB
		tagged 'rightBar' : VBar.r df.rightSB 0 yTop

	define [ToothlessRoundedBody df yTop] : OBarRight.rounded
		left      -- df.leftSB
		right     -- df.rightSB
		sw        -- df.mvs
		yTerminal -- yTop
		ada       -- df.smallArchDepthA
		adb       -- df.smallArchDepthB

	define [TailedBody df yTop] : union
		OBarRight.shape
			left  -- df.leftSB
			right -- df.rightSB
			sw    -- df.mvs
			ada   -- df.smallArchDepthA
			adb   -- df.smallArchDepthB
		RightwardTailedBar df.rightSB 0 yTop

	define [TopSerif df yTop] : tagged 'serifRT'
		HSerif.lt (df.rightSB - [HSwToV Stroke]) yTop SideJut

	define [BaseSerif df yTop] : tagged 'serifRB'
		HSerif.rb df.rightSB 0 SideJut

	define DConfig : SuffixCfg.weave
		object # body
			toothed                   ToothedBody
			toothlessCorner           ToothlessCornerBody
			toothlessCornerHBB        ToothlessCornerHBBBody
			toothlessRounded          ToothlessRoundedBody
			tailed                    TailedBody

		function [body] : object # serifs
			serifless                { null     null      }
			topSerifed               { TopSerif null      }
			bottomSerifed            { null     BaseSerif }
			serifed                  { TopSerif BaseSerif }

	foreach { suffix { Body {topSerif bottomSerif} } } [Object.entries DConfig] : do
		local yOverlay : mix XH (Ascender - [if topSerif Stroke 0]) 0.5

		do
			create-glyph "d.\(suffix)" : make [DivFrame 1]
			create-glyph "dCaronBase.\(suffix)" : make [DivFrame 0.95]
		: where : [make df] : glyph-proc
			include : df.markSet.b
			include : Body df Ascender
			if topSerif    : include : topSerif df Ascender
			if bottomSerif : include : bottomSerif df Ascender
			include : LeaningAnchor.Above.VBar.r df.rightSB
			set-base-anchor 'overlayOnExtension' (df.rightSB - [HSwToV HalfStroke]) yOverlay

		create-glyph "dStroke.\(suffix)" : glyph-proc
			local df : DivFrame 1
			include [refer-glyph "d.\(suffix)"] AS_BASE ALSO_METRICS
			include : LetterBarOverlay.r.in df.rightSB XH (Ascender - [if topSerif Stroke 0])

		create-glyph "latn/de.\(suffix)" : glyph-proc
			local df : DivFrame 1
			include : df.markSet.b
			include : Body df Ascender
			local xLeft : mix df.rightSB df.leftSB 0.9
			local xRight : df.rightSB + O
			include : HBar.t xLeft xRight Ascender
			if topSerif : begin
				include : VSerif.dl xLeft Ascender [Math.min VJut : 0.8 * (Ascender - XH)]
				if [not para.isItalic] : include : HSerif.rt df.rightSB Ascender SideJut
			if bottomSerif : include : bottomSerif df Ascender
			include : LeaningAnchor.Above.VBar.m [mix xLeft xRight 0.5]

		if [not topSerif] : create-glyph "dHookTop.\(suffix)" : glyph-proc
			local df : DivFrame 1
			include : df.markSet.b
			include : Body df (Ascender - Hook - HalfStroke)
			include : TopHook.toRight.rBarInner df.rightSB (XH / 2) Ascender
			if bottomSerif : include : bottomSerif df Ascender
			include : LeaningAnchor.Above.VBar.r df.rightSB

		if [not bottomSerif] : create-glyph "cyrl/djeKomi.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleMM 3
			include : df.markSet.b

			local dfHalf : df.slice 3 2
			include : Body dfHalf Ascender
			eject-contour 'rightBar'

			include : with-transform [ApparentTranslate (df.width - dfHalf.width) 0] : UpwardHookShape
				left   -- dfHalf.leftSB
				right  -- dfHalf.rightSB
				ybegin -- Ascender
				yend   -- (XH / 2 + HalfStroke)
				ada    -- (SmallArchDepthA * 0.6 * df.adws)
				adb    -- (SmallArchDepthB * 0.6 * df.adws)
				sw     -- df.mvs

			if topSerif : include : topSerif dfHalf Ascender
			if SLAB : begin
				local sf2 : [SerifFrame.fromDf df (XH / 2 + HalfStroke) 0].slice 1 2
				include sf2.rt.full

	select-variant 'd' 'd'
	link-reduced-variant 'd/sansSerif' 'd' MathSansSerif
	link-reduced-variant 'd/phoneticLeft' 'd'
	select-variant 'dCaronBase' (follow -- 'd')

	select-variant 'dStroke' 0x111 (follow -- 'd')
	select-variant 'latn/de' 0x18C (follow -- 'd')
	select-variant 'dHookTop' 0x257

	alias 'cyrl/deKomi' 0x501 'd'
	select-variant 'cyrl/djeKomi' 0x503

	link-reduced-variant 'd/descBase' 'd'
	derive-composites 'dPalatalHook' 0x1D81 'd/descBase' : PalatalHook.r
		xLink -- RightSB
		x -- (RightSB + SideJut)
		y -- 0

	link-reduced-variant 'd/hookBottomBase' 'd'
	derive-glyphs 'dHookBottom' 0x256 "d/hookBottomBase" : function [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		include : RetroflexHook.rExt RightSB 0
		include : LeaningAnchor.Below.VBar.r RightSB

	link-reduced-variant 'dHookTop/hookBottomBase' 'dHookTop'
	derive-glyphs 'dHookTopBottom' 0x1D91 "dHookTop/hookBottomBase" : function [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		include : RetroflexHook.rExt RightSB 0
		include : LeaningAnchor.Below.VBar.r RightSB

	CreateCommaCaronComposition 'dCaron' 0x10F 'dCaronBase' (Width - [HSwToV QuarterStroke])

	derive-multi-part-glyphs 'dDot' 0x1E0B {'d' 'dotAbove'} : lambda [srcs gr] : glyph-proc
		local { base mark } srcs
		include : refer-glyph mark
		include : Translate (Width - [HSwToV HalfStroke]) 0
		include [refer-glyph base] AS_BASE
		include : LeaningAnchor.Above.VBar.r Middle

	derive-composites 'dBar' 0xA7C8 'd'
		HBar.m (SB - OX) (RightSB + OX) (XH * 0.5) OverlayStroke

	define DCurlyTailConfig : object
		toothedSerifless  { false }
		toothedTopSerifed { true  }

	foreach { suffix { fSerif } } [Object.entries DCurlyTailConfig] : do
		create-glyph "dCurlyTail.\(suffix)" : glyph-proc
			include : MarkSet.b

			local fine : AdviceStroke 4
			local rinner : clamp (Width * 0.065) (XH * 0.05) (fine * 0.35)
			local sw : AdviceStroke 2.75
			local m : Math.min RightSB (Width - rinner * 2 - fine - OX)

			include : LeaningAnchor.Above.VBar.r m
			include : OBarRight.shape (left -- SB) (right -- m) (sw -- sw)
			include : dispiro
				widths.lhs sw
				flat (m - [HSwToV sw]) Ascender [heading Downward]
				curl (m - [HSwToV sw]) (rinner * 2 + fine)
				CurlyTail.n fine 0 (m + rinner * 2 + fine)
					x2 -- ([mix SB m 0.5] - [HSwToV : 0.75 * fine])
					y2 -- (0.37 * Descender)
					swBefore -- sw
					terminalSlopeAdj -- 0.25
			if fSerif : include : HSerif.lt (m - [HSwToV sw]) Ascender SideJut

	select-variant 'dCurlyTail' 0x221

	glyph-block-import Letter-Blackboard : BBS BBD BBBarRight
	create-glyph 'mathbb/d' 0x1D555 : glyph-proc
		include : MarkSet.b
		include : BBBarRight RightSB 0 Ascender
		include : intersection
			OShapeOutline.NoOvershoot XH 0 SB (RightSB - BBD - [HSwToV BBS])
			VBar.l (SB + BBD + OX) 0 XH BBS
		include : OBarRight.shape
			right -- (RightSB - BBD)
			sw    -- BBS
