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

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

glyph-module

glyph-block Digits-One : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Digits-Shared : OnumHeight OnumMarks CodeLnum CodeOnum

	define Shape : namespace
		export : define [Line top balance pTopSerif] : VBar.m (Middle + balance) 0 top

		export : define [Serifed top balance pTopSerif] : glyph-proc
			define topSW : AdviceStroke 3.5
			include : VBar.m (Middle + balance) 0 top
			include : dispiro
				flat (Middle - [HSwToV HalfStroke] + balance) top [widths.lhs topSW]
				curl (Middle - [HSwToV : Stroke / 8] - HookX * 1.25 + balance) (top - Stroke / 8 - Hook * pTopSerif * (top / CAP))

		export : define [FlatSerifed top balance pTopSerif] : glyph-proc
			define topSW : AdviceStroke 3.5
			include : VBar.m (Middle + balance) 0 top
			include : HSerif.lt (Middle + balance - [HSwToV HalfStroke]) (top - Stroke / 8 - Hook * pTopSerif * (top / CAP)) (LongJut - [HSwToV HalfStroke])

		export : define [TopCut top balance pTopSerif] : glyph-proc
			local sl : Middle + balance - [HSwToV HalfStroke]
			local sr : Middle + balance + [HSwToV HalfStroke]
			include : spiro-outline
				corner sr 0
				corner sl 0
				corner sl (top - (sr - sl) * pTopSerif)
				corner sr top

		export : define [Base] : HSerif.mb Middle 0 LongJut

	define OneConfig : object
		line               { Shape.Line         null        0.75   0 }
		noBase             { Shape.Serifed      null        0.75   1 }
		base               { Shape.Serifed      Shape.Base  0.75   1 }
		noBaseLongTopSerif { Shape.Serifed      null        1.875  1 }
		baseLongTopSerif   { Shape.Serifed      Shape.Base  1.875  1 }
		noBaseFlatTopSerif { Shape.FlatSerifed  null        0.75   1 }
		baseFlatTopSerif   { Shape.FlatSerifed  Shape.Base  0.75   1 }
		noBaseTopCut       { Shape.TopCut       null        0.75   0 }
		baseTopCut         { Shape.TopCut       Shape.Base  0.75   0 }

	foreach {suffix {Shape Base pTS pBalance}} [Object.entries OneConfig] : do
		local balance : pBalance * [if Base (OneBalance / 4) OneBalance]

		create-glyph "one.lnum.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : Shape CAP balance pTS
			if Base : include [Base]

		create-glyph "one.onum.\(suffix)" : glyph-proc
			include : OnumMarks.e
			include : Shape OnumHeight balance (pTS * 5 / 6)
			if Base : include [Base]

	select-variant 'one.lnum' [CodeLnum '1'] (follow -- 'one')
	select-variant 'one.onum' [CodeOnum '1'] (follow -- 'one')

	link-reduced-variant 'one/sansSerif.lnum' 'one.lnum' MathSansSerif (follow -- 'one/sansSerif')
	link-reduced-variant 'one/sansSerif.onum' 'one.onum' MathSansSerif (follow -- 'one/sansSerif')

	glyph-block-import Letter-Blackboard : BBS BBD BBBarCenter
	create-glyph 'mathbb/one' 0x1D7D9 : glyph-proc
		define barCenter : Middle + OneBalance / 2
		define xSerifTerminal : barCenter - [HSwToV : BBD / 8] - HookX * 1.25
		define ySerifTerminal : CAP - BBD / 8 - Hook * 0.75
		include : MarkSet.capital
		include : intersection
			BBBarCenter barCenter 0 CAP
			spiro-outline
				corner (barCenter + BBD / 2) CAP
				corner Width CAP
				corner Width 0
				corner 0     0
				corner xSerifTerminal ySerifTerminal
		include : dispiro
			widths.lhs BBS
			corner (barCenter + BBD / 2) CAP
			corner xSerifTerminal ySerifTerminal
