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

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

glyph-module

glyph-block Letter-Latin-Upper-D : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Shared-Shapes : LetterBarOverlay LeftHook

	define [DArcShapeT sink df top right offset sw fRound smooth] : glyph-proc
		local mockWidth : right + (df.width - df.rightSB)
		local maxXSmoothWidth : mockWidth - df.leftSB * 2 - [HSwToV sw]
		local dsmooth : fallback smooth : clamp (sw * 1.5) (top / 2 - TINY) ((top / CAP) * ArchDepth * 1.1)
		local bsmooth : clamp (sw * 1.5) maxXSmoothWidth ((mockWidth / Width) * ArchDepth * 1.1)
		local barchDepthRd : clamp (sw * 1.5) maxXSmoothWidth ((mockWidth / Width) * ArchDepth * 1.25)

		local ada : ArchDepthAOf dsmooth mockWidth
		local adb : ArchDepthBOf dsmooth mockWidth

		if (fRound) : include : sink
			widths.rhs sw
			[if offset corner flat] (df.leftSB - O) (top - offset) [heading Rightward]
			curl [arch.adjust-x.top (right - barchDepthRd)] (top - offset)
			archv
			g4 (right - OX - offset) [YSmoothMidR top 0 ada adb]
			arcvh
			flat [arch.adjust-x.bot (right - barchDepthRd)] offset
			[if offset corner curl] (df.leftSB - O) offset [heading Leftward]
		: else : include : sink
			widths.rhs sw
			[if offset corner flat] (df.leftSB - O) (top - offset) [heading Rightward]
			curl [arch.adjust-x.top (right - bsmooth)] (top - offset)
			archv
			flatside.rd (right - offset) 0 top ada adb
			arcvh
			flat [arch.adjust-x.bot (right - bsmooth)] offset
			[if offset corner curl] (df.leftSB - O) offset [heading Leftward]

	define [DShape df top right fRound smooth slabTop slabBot] : glyph-proc
		include : VBar.l df.leftSB 0 top df.mvs
		include : DArcShapeT dispiro df top right 0 df.mvs fRound smooth
		if slabTop : include : tagged 'serifLT' : HSerif.lt df.leftSB top SideJut
		if slabBot : include : tagged 'serifLB' : HSerif.lb df.leftSB 0 SideJut

	define DConfig : SuffixCfg.weave
		object # body
			standard    false
			moreRounded true
		object # serifs
			serifless         { false false }
			unilateralSerifed { true  false }
			bilateralSerifed  { true  true  }

	foreach { suffix { fRound {fSlabTop fSlabBot} } } [Object.entries DConfig] : do
		local fMotion : fSlabTop && !fSlabBot

		create-glyph "D.\(suffix)" : glyph-proc
			local df : include : DivFrame 1
			include : df.markSet.capital
			include : DShape df CAP df.rightSB fRound nothing fSlabTop fSlabBot

		if (!fMotion) : create-glyph "Dhookleft.\(suffix)" : glyph-proc
			include [refer-glyph "D.\(suffix)"] AS_BASE ALSO_METRICS
			eject-contour 'serifLT'
			include : LeftHook SB CAP

		if fRound : create-glyph "smcpD.\(suffix)" : glyph-proc
			local df : include : DivFrame 1
			include : df.markSet.e
			include : DShape df XH df.rightSB fRound ArchDepth fSlabTop fSlabBot

		create-glyph "romanFiveThousand.\(suffix)" : glyph-proc
			local df : include : DivFrame 1 3
			include : df.markSet.capital
			include : DShape df CAP df.rightSB fRound nothing fSlabTop fSlabBot
			include : with-transform [ApparentTranslate 0 (CAP * 0.2)]
				DShape df (CAP * 0.6) (df.middle + [HSwToV : 0.5 * df.mvs]) fRound

		create-glyph "romanFiftyThousand.\(suffix)" : glyph-proc
			local df : include : DivFrame 1 4
			include : df.markSet.capital
			local innerDist : df.rightSB - df.leftSB - [HSwToV : 4 * df.mvs]
			local arcX1 : df.leftSB + innerDist * (1 / 3) + [HSwToV : 2 * df.mvs]
			local arcX2 : df.leftSB + innerDist * (2 / 3) + [HSwToV : 3 * df.mvs]
			local heightGap : df.mvs + (CAP - df.mvs * 6) / 6
			include : DShape df CAP df.rightSB fRound nothing fSlabTop fSlabBot
			include : with-transform [ApparentTranslate 0 heightGap]
				DShape df (CAP - 2 * heightGap) arcX2 fRound
			include : with-transform [ApparentTranslate 0 (2 * heightGap)]
				DShape df (CAP - 4 * heightGap) arcX1 fRound

	select-variant 'D' 'D'
	select-variant 'romanFiveThousand' 0x2181 (follow -- 'D')
	select-variant 'romanFiftyThousand' 0x2187 (follow -- 'D')
	link-reduced-variant 'D/sansSerif' 'D' MathSansSerif
	derive-glyphs 'Eth' 0xD0 'D' : lambda [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		include : LetterBarOverlay.l.in SB Stroke (CAP - Stroke)
			space -- { 0 (RightSB - [HSwToV Stroke]) }
	alias 'DStroke' 0x110 'Eth'
	alias 'arficanD' 0x189 'Eth'
	select-variant 'Dhookleft' 0x18A

	derive-composites 'Dbar' 0xA7C7 'D'
		HBar.m (SB - OX) (RightSB + OX) (CAP / 2) OverlayStroke

	select-variant 'smcpD' 0x1D05
	derive-glyphs 'smcpEth' 0x1D06 'smcpD' : lambda [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		include : LetterBarOverlay.l.in SB Stroke (XH - Stroke)
			space -- { 0 (RightSB - [HSwToV Stroke]) }

	glyph-block-import Letter-Blackboard : BBS BBD
	create-glyph 'mathbb/D' 0x1D53B : glyph-proc
		define df : include : DivFrame 1
		include : df.markSet.capital
		include : VBar.l df.leftSB 0 CAP BBS
		include : DArcShapeT dispiro df CAP df.rightSB 0 BBS false
		include : intersection
			DArcShapeT spiro-outline df CAP df.rightSB 1 BBS false
			union
				VBar.l (df.leftSB + BBD) 0 CAP BBS
				VBar.r (df.rightSB - BBD - OX) 0 CAP BBS
