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

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

glyph-module

glyph-block Letter-Latin-Lower-A : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Shared-Metrics : markHalfStroke
	glyph-block-import Letter-Shared : CreateAccentedComposition CreateMultiAccentedComposition CreateTurnedLetter
	glyph-block-import Letter-Shared-Shapes : SerifFrame OBarLeft OBarRight ArcStartSerif
	glyph-block-import Letter-Shared-Shapes : RightwardTailedBar RetroflexHook
	glyph-block-import Letter-Shared-Shapes : InwardSlabArcStart

	glyph-block-export DoubleStorey
	define DoubleStorey : namespace
		define [ADoubleStoreyStroke df] : AdviceStroke2 2 3 XH df.adws
		define [ADoubleStoreySmoothA df] : begin
			local sw : ADoubleStoreyStroke df
			return : df.archDepthAOf (ArchDepth * [StrokeWidthBlend 0.9 0.81 sw]) sw
		define [ADoubleStoreySmoothB df] : begin
			local sw : ADoubleStoreyStroke df
			return : df.archDepthBOf (ArchDepth * [StrokeWidthBlend 0.9 0.81 sw]) sw

		define [ADoubleStoreyHookAndBarT sink df hookStyle y0 stroke] : begin
			local isMask : sink == spiro-outline
			return : sink
				widths.rhs stroke
				match hookStyle
					1 : list
						g4 df.leftSB (XH - AHook)
						hookstart XH (sw -- stroke)
					2 : InwardSlabArcStart.LtrRhs df.leftSB XH stroke
				flat df.rightSB (XH - [ADoubleStoreySmoothB df])
				[if isMask corner curl] df.rightSB y0 [heading Downward]
				if [not isMask] {} {[corner df.leftSB y0]}

		export : define [HookAndBar df hookStyle y0 _stroke] : glyph-proc
			if ((hookStyle != 1) && (hookStyle != 2)) : throw : new Error "Invalid hookStyle"
			local sw : fallback _stroke [ADoubleStoreyStroke df]
			include : ADoubleStoreyHookAndBarT dispiro df hookStyle y0 sw
			if (hookStyle == 2) : include : ArcStartSerif.L df.leftSB (XH - DToothlessRise) sw AHook

		export : define [HookAndBarMask df hookStyle y0 _stroke] : begin
			local sw : fallback _stroke [ADoubleStoreyStroke df]
			return : ADoubleStoreyHookAndBarT spiro-outline df hookStyle y0 sw

		define [ADoubleStoreyArcT df sink kind rise stroke] : glyph-proc
			local isMask : sink == spiro-outline
			local bartop : XH * OverlayPos * 1.02 + stroke * 0.425
			local bowlArcY1 : YSmoothMidL 0 bartop SmallArchDepthA SmallArchDepthB
			local bowlArcY2 : YSmoothMidR 0 bartop SmallArchDepthA SmallArchDepthB
			local leftSlopeS : 0.1 * (df.width / HalfUPM)
			local leftSlope  : leftSlopeS - TanSlope
			local barSmooth : mix df.leftSB df.rightSB 0.55
			include : sink
				widths.lhs stroke
				[if isMask corner flat] (df.rightSB + O) bartop [heading Leftward]
				curl barSmooth bartop
				archv
				g4 (df.leftSB + OX) (bowlArcY1 - [HSwToV Stroke] * leftSlope) [heading {.x HVContrast .y leftSlope}]
				match kind
					0 : list
						arch.lhs 0 (sw -- stroke) (swAfter -- ShoulderFine)
						straight.up.end   (df.rightSB - [HSwToV stroke] + [HSwToV ShoulderFine]) (SmallArchDepthB * 0.9) [widths.lhs ShoulderFine]
						if [not isMask] {} {[ corner (df.rightSB - [HSwToV stroke] + [HSwToV ShoulderFine]) bartop ]}
					1 : list
						arch.lhs 0 (sw -- stroke) (blendPost -- {})
						g4  df.rightSB rise
					2 : list
						arch.lhs 0 (sw -- stroke)
						flat df.rightSB bowlArcY2
						curl df.rightSB ([Math.max bowlArcY2 rise] + stroke * TanSlope + 1) [heading Upward]

		export : define [Arc df kind rise _sw] : ADoubleStoreyArcT df dispiro kind rise
			fallback _sw [ADoubleStoreyStroke df]
		export : define [ArcMask df kind rise _sw] : ADoubleStoreyArcT df spiro-outline kind rise
			fallback _sw [ADoubleStoreyStroke df]

		export : define [Serifless df hookStyle sw] : union
			HookAndBar df hookStyle 0 sw
			Arc        df 0 nothing sw
		export : define [Serifed df hookStyle sw] : union
			Serifless  df hookStyle sw
			begin [SerifFrame.fromDf df XH 0].rb.outer
		export : define [Tailed df hookStyle sw] : union
			HookAndBar df hookStyle (XH - [ADoubleStoreySmoothB df] + O) sw
			Arc        df 0 nothing sw
			RightwardTailedBar df.rightSB 0 (XH - [ADoubleStoreySmoothB df]) (sw -- [fallback sw : ADoubleStoreyStroke df])
		export : define [ToothlessCorner df hookStyle sw] : union
			HookAndBar df hookStyle DToothlessRise sw
			Arc        df 1 DToothlessRise sw
		export : define [ToothlessRounded df hookStyle sw] : union
			HookAndBar df hookStyle [ADoubleStoreySmoothA df] sw
			Arc        df 2 [ADoubleStoreySmoothA df] sw

		export : define [GetMask shapeFn df sw] : match shapeFn
			[Just ToothlessCorner]  : ArcMask df 1 DToothlessRise sw
			[Just ToothlessRounded] : ArcMask df 2 [ADoubleStoreySmoothA df] sw
			__                      : ArcMask df 0 nothing sw

	glyph-block-export DoubleStoreyConfig
	define DoubleStoreyConfig : object
		doubleStoreySerifless                         { DoubleStorey.Serifless         RightSB            1 }
		doubleStoreySerifed                           { DoubleStorey.Serifed          (RightSB + SideJut) 1 }
		doubleStoreyTailed                            { DoubleStorey.Tailed           (RightSB + SideJut) 1 }
		doubleStoreyToothlessCorner                   { DoubleStorey.ToothlessCorner   nothing            1 }
		doubleStoreyToothlessRounded                  { DoubleStorey.ToothlessRounded  nothing            1 }
		doubleStoreyHookInwardSerifedSerifless        { DoubleStorey.Serifless         RightSB            2 }
		doubleStoreyHookInwardSerifedSerifed          { DoubleStorey.Serifed          (RightSB + SideJut) 2 }
		doubleStoreyHookInwardSerifedTailed           { DoubleStorey.Tailed           (RightSB + SideJut) 2 }
		doubleStoreyHookInwardSerifedToothlessCorner  { DoubleStorey.ToothlessCorner   nothing            2 }
		doubleStoreyHookInwardSerifedToothlessRounded { DoubleStorey.ToothlessRounded  nothing            2 }

	foreach { suffix { body xTrailing hookStyle } } [Object.entries DoubleStoreyConfig] : do
		create-glyph "a.\(suffix)" : glyph-proc
			local df : include : DivFrame 1
			include : df.markSet.e
			if xTrailing : set-base-anchor 'trailing' xTrailing 0
			include : body df hookStyle
		create-glyph "aRetroflexHook.\(suffix)" : glyph-proc
			include [refer-glyph "a.\(suffix)"] AS_BASE ALSO_METRICS
			include : RetroflexHook.rSideJut RightSB 0 (yOverflow -- Stroke)

	glyph-block-import Letter-Blackboard : BBS BBD
	create-glyph 'mathbb/a' 0x1D552 : glyph-proc
		local df : include : DivFrame 1
		include : df.markSet.e

		include : DoubleStorey.HookAndBar df 1 0 BBS
		include : intersection
			DoubleStorey.HookAndBarMask df 1 0
			VBar.r (RightSB - BBD) 0 XH BBS
		include : difference
			union
				DoubleStorey.Arc df 0 0 BBS
				intersection
					DoubleStorey.ArcMask df 0 0 BBS
					VBar.l (SB + OX + BBD) 0 XH BBS
			Rect XH 0 (RightSB - BBD) Width
		include : HBar.b (RightSB - BBD) RightSB 0 BBS

	# Greek Lower Alpha
	create-glyph 'a.crossing' : glyph-proc
		include : MarkSet.e

		local middle : mix SB RightSB 0.42
		local fine   : AdviceStroke 3.25
		local k1 0.25
		local k2 0.5
		local k3 0.35
		include : dispiro
			widths.rhs fine
			g4   (RightSB - OX * 1.75) XH [heading Downward]
			bezControls k1 k2 k3 1 6
			arch.rhs.centerAt.rtl.b middle 0
			archv
			flat (SB + OX * 2) SmallArchDepthB
			curl (SB + OX * 2) (XH - SmallArchDepthA)
			arcvh
			arch.rhs.centerAt.ltr.t middle XH
			bezControls (1 - k3) 0 (1 - k1) (1 - k2) 6
			g4   (RightSB - OX * 2) 0 [widths.heading 0 fine Downward]

		set-base-anchor 'overlay' (middle - OX) (XH * OverlayPos)

	glyph-block-export SingleStorey
	define SingleStorey : namespace
		define [EaredBodyImpl df top sw] : OBarRight.shape
			top   -- top
			left  -- df.leftSB
			right -- df.rightSB
			sw    -- sw
			fine  -- (ShoulderFine * (sw / Stroke))
			ada   -- [df.archDepthAOf SmallArchDepth sw]
			adb   -- [df.archDepthBOf SmallArchDepth sw]

		export : define [FullBarBody df top bar _sw] : glyph-proc
			local sw : fallback _sw df.mvs
			include : EaredBodyImpl df top sw
			include : bar df top sw
		export : define [TopCutBody df top bar _sw] : glyph-proc
			local sw : fallback _sw df.mvs
			include : EaredBodyImpl df top sw
			include : bar df (top - sw / 2) sw
			include : spiro-outline
				corner  df.rightSB                 top
				corner  df.rightSB                (top - sw / 2)
				corner (df.rightSB - [HSwToV sw]) (top - sw / 2)
		export : define [EarlessCornerBody df top bar _sw] : glyph-proc
			local sw : fallback _sw df.mvs
			include : OBarLeft.toothless
				top   -- top
				left  -- df.leftSB
				right -- df.rightSB
				sw    -- sw
				fine  -- (ShoulderFine * (sw / Stroke))
				ada   -- [df.archDepthAOf SmallArchDepth sw]
				adb   -- [df.archDepthBOf SmallArchDepth sw]
				rise  -- DToothlessRise
				mBlend -- DMBlend
			include : FlipAround df.middle (top / 2)
			include : bar df (top - DToothlessRise) sw
		export : define [EarlessRoundedBody df top bar _sw] : glyph-proc
			local sw : fallback _sw df.mvs
			local ada : df.archDepthAOf SmallArchDepth sw
			local adb : df.archDepthBOf SmallArchDepth sw
			include : OBarLeft.rounded
				top   -- top
				left  -- df.leftSB
				right -- df.rightSB
				sw    -- sw
				fine  -- (ShoulderFine * (sw / Stroke))
				ada   -- ada
				adb   -- adb
				yTerminal -- (top - ada)
			include : FlipAround df.middle (top / 2)
			include : bar df (top - adb) sw

		export : define [SeriflessBar df top sw] : glyph-proc
			set-base-anchor 'trailing' df.rightSB 0
			include : VBar.r df.rightSB 0 top sw
		export : define [TopSerifedBar df top sw] : glyph-proc
			include : SeriflessBar df top sw
			local sf : SerifFrame.fromDf df top 0 (swSerif -- sw)
			include sf.rt.outer
		export : define [BottomSerifedBar df top sw] : glyph-proc
			include : SeriflessBar df top sw
			local sf : SerifFrame.fromDf df top 0 (swSerif -- sw)
			include sf.rb.outer
		export : define [DoubleSerifedBar df top sw] : glyph-proc
			include : SeriflessBar df top sw
			local sf : SerifFrame.fromDf df top 0 (swSerif -- sw)
			include : composite-proc sf.rt.outer sf.rb.outer
		export : define [TailedBar df top sw] : glyph-proc
			set-base-anchor 'trailing' (df.rightSB + SideJut) 0
			include : RightwardTailedBar df.rightSB 0 top sw
		export : define [TailedSerifedBar df top sw] : glyph-proc
			include : TailedBar df top sw
			local sf : SerifFrame.fromDf df top 0 (swSerif -- sw)
			include sf.rt.outer

	glyph-block-export SingleStoreyConfig
	define SingleStoreyConfig : object
		singleStoreySerifless               { SingleStorey.FullBarBody        SingleStorey.SeriflessBar     }
		singleStoreyEarlessCornerSerifless  { SingleStorey.EarlessCornerBody  SingleStorey.SeriflessBar     }
		singleStoreyEarlessRoundedSerifless { SingleStorey.EarlessRoundedBody SingleStorey.SeriflessBar     }
		singleStoreyTopCutSerifless         { SingleStorey.TopCutBody         SingleStorey.SeriflessBar     }

		singleStoreyTopSerifed              { SingleStorey.FullBarBody        SingleStorey.TopSerifedBar    }

		singleStoreySerifed                 { SingleStorey.FullBarBody        SingleStorey.BottomSerifedBar }
		singleStoreyEarlessCornerSerifed    { SingleStorey.EarlessCornerBody  SingleStorey.BottomSerifedBar }
		singleStoreyEarlessRoundedSerifed   { SingleStorey.EarlessRoundedBody SingleStorey.BottomSerifedBar }
		singleStoreyTopCutSerifed           { SingleStorey.TopCutBody         SingleStorey.BottomSerifedBar }

		singleStoreyDoubleSerifed           { SingleStorey.FullBarBody        SingleStorey.DoubleSerifedBar }

		singleStoreyTailed                  { SingleStorey.FullBarBody        SingleStorey.TailedBar        }
		singleStoreyEarlessCornerTailed     { SingleStorey.EarlessCornerBody  SingleStorey.TailedBar        }
		singleStoreyEarlessRoundedTailed    { SingleStorey.EarlessRoundedBody SingleStorey.TailedBar        }
		singleStoreyTopCutTailed            { SingleStorey.TopCutBody         SingleStorey.TailedBar        }

		singleStoreyTailedSerifed           { SingleStorey.FullBarBody        SingleStorey.TailedSerifedBar }

	foreach { suffix { body bar } } [Object.entries SingleStoreyConfig] : do
		create-glyph "a.\(suffix)" : glyph-proc
			local df : include : DivFrame 1
			include : df.markSet.e
			include : body df XH bar
		create-glyph "AScript.\(suffix)" : glyph-proc
			local df : include : DivFrame 1
			include : df.markSet.capital
			include : body df CAP bar
		create-glyph "aRetroflexHook.\(suffix)" : glyph-proc
			include [refer-glyph "a.\(suffix)"] AS_BASE ALSO_METRICS
			include : RetroflexHook.rSideJut RightSB 0 (yOverflow -- Stroke)

	select-variant 'a' 'a'
	link-reduced-variant 'a/sansSerif' 'a' MathSansSerif
	link-reduced-variant 'a/doubleStorey' 'a'
	select-variant 'cyrl/a' 0x430 (shapeFrom -- 'a')

	CreateTurnedLetter 'turna' 0x250 'a/doubleStorey' HalfAdvance (XH / 2)

	select-variant 'aRetroflexHook' 0x1D8F
	link-reduced-variant 'aRetroflexHook/doubleStorey' 'aRetroflexHook'

	select-variant 'grek/alpha' 0x3B1 (shapeFrom -- 'a')
	link-reduced-variant 'grek/alpha/sansSerif' 'grek/alpha' MathSansSerif (shapeFrom -- 'a')

	select-variant 'aScript' 0x251  (follow -- [conditional-follow SLAB 'a/singleStorey/autoSerifed/slab' 'a/singleStorey/autoSerifed/sans']) (shapeFrom -- 'a')
	select-variant 'AScript' 0x2C6D (follow -- [conditional-follow SLAB 'a/singleStorey/autoSerifed/slab' 'a/singleStorey/autoSerifed/sans'])

	select-variant 'aScriptRetroflexHook' 0x1D90 (follow -- [conditional-follow SLAB 'aRetroflexHook/singleStorey/autoSerifed/slab' 'aRetroflexHook/singleStorey/autoSerifed/sans']) (shapeFrom -- 'aRetroflexHook')

	CreateTurnedLetter 'turnaScript' 0x252  'aScript' HalfAdvance (XH / 2)
	CreateTurnedLetter 'turnAScript' 0x2C70 'AScript' HalfAdvance (CAP / 2)

	CreateAccentedComposition 'aGrave'     0xE0   'a' 'graveAbove'
	CreateAccentedComposition 'aAcute'     0xE1   'a' 'acuteAbove'
	CreateAccentedComposition 'aCircum'    0xE2   'a' 'circumflexAbove'
	CreateAccentedComposition 'aTilde'     0xE3   'a' 'tildeAbove'
	CreateAccentedComposition 'aDieresis'  0xE4   'a' 'dieresisAbove'
	CreateAccentedComposition 'aRing'      0xE5   'a' 'ringAbove'
	CreateAccentedComposition 'aMacron'    0x101  'a' 'macronAbove'
	CreateAccentedComposition 'aBreve'     0x103  'a' 'breveAbove'
	CreateAccentedComposition 'aOgonek'    0x105  'a' 'ogonekBelow'
	CreateAccentedComposition 'aCaron'     0x1CE  'a' 'caronAbove'
	CreateAccentedComposition 'aDblGrave'  0x201  'a' 'doubleGraveAbove'
	CreateAccentedComposition 'aInvBreve'  0x203  'a' 'archAbove'
	CreateAccentedComposition 'aDot'       0x227  'a' 'dotAbove'
	CreateAccentedComposition 'aBelowring' 0x1E01 'a' 'ringBelow'
	CreateAccentedComposition 'aBelowdot'  0x1EA1 'a' 'dotBelow'
	CreateAccentedComposition 'aHook'      0x1EA3 'a' 'hookAbove'
	CreateAccentedComposition 'aRightHalfRing' 0x1E9A 'a' 'rightHalfCircleTR'

	CreateMultiAccentedComposition 'aDieresisMacron' 0x1DF  'a' { 'dieresisAbove'   'macronAbove' }
	CreateMultiAccentedComposition 'aDotMacron'      0x1E1  'a' { 'dotAbove'        'macronAbove' }
	CreateMultiAccentedComposition 'aRingAcute'      0x1FB  'a' { 'ringAbove'       'acuteAbove'  }
	CreateMultiAccentedComposition 'aCircumAcute'    0x1EA5 'a' { 'circumflexAbove' 'acuteAbove' }
	CreateMultiAccentedComposition 'aCircumGrave'    0x1EA7 'a' { 'circumflexAbove' 'graveAbove' }
	CreateMultiAccentedComposition 'aCircumHook'     0x1EA9 'a' { 'circumflexAbove' 'hookAbove'  }
	CreateMultiAccentedComposition 'aCircumTilde'    0x1EAB 'a' { 'circumflexAbove' 'tildeAbove' }
	CreateMultiAccentedComposition 'aCircumBelowdot' 0x1EAD 'a' { 'circumflexAbove' 'dotBelow'   }
	CreateMultiAccentedComposition 'aBreveAcute'     0x1EAF 'a' { 'breveAbove'      'acuteAbove' }
	CreateMultiAccentedComposition 'aBreveGrave'     0x1EB1 'a' { 'breveAbove'      'graveAbove' }
	CreateMultiAccentedComposition 'aBreveHook'      0x1EB3 'a' { 'breveAbove'      'hookAbove'  }
	CreateMultiAccentedComposition 'aBreveTilde'     0x1EB5 'a' { 'breveAbove'      'tildeAbove' }
	CreateMultiAccentedComposition 'aBreveBelowdot'  0x1EB7 'a' { 'breveAbove'      'dotBelow'   }

	CreateAccentedComposition 'aGrave/doubleStorey'     null 'a/doubleStorey' 'graveAbove'
	CreateAccentedComposition 'aAcute/doubleStorey'     null 'a/doubleStorey' 'acuteAbove'
	CreateAccentedComposition 'aCircum/doubleStorey'    null 'a/doubleStorey' 'circumflexAbove'
	CreateAccentedComposition 'aTilde/doubleStorey'     null 'a/doubleStorey' 'tildeAbove'
	CreateAccentedComposition 'aDieresis/doubleStorey'  null 'a/doubleStorey' 'dieresisAbove'
	CreateAccentedComposition 'aRing/doubleStorey'      null 'a/doubleStorey' 'ringAbove'
	CreateAccentedComposition 'aMacron/doubleStorey'    null 'a/doubleStorey' 'macronAbove'
	CreateAccentedComposition 'aBreve/doubleStorey'     null 'a/doubleStorey' 'breveAbove'
	CreateAccentedComposition 'aOgonek/doubleStorey'    null 'a/doubleStorey' 'ogonekBelow'
	CreateAccentedComposition 'aCaron/doubleStorey'     null 'a/doubleStorey' 'caronAbove'
	CreateAccentedComposition 'aDblGrave/doubleStorey'  null 'a/doubleStorey' 'doubleGraveAbove'
	CreateAccentedComposition 'aInvBreve/doubleStorey'  null 'a/doubleStorey' 'archAbove'
	CreateAccentedComposition 'aDot/doubleStorey'       null 'a/doubleStorey' 'dotAbove'
	CreateAccentedComposition 'aBelowring/doubleStorey' null 'a/doubleStorey' 'ringBelow'
	CreateAccentedComposition 'aBelowdot/doubleStorey'  null 'a/doubleStorey' 'dotBelow'
	CreateAccentedComposition 'aHook/doubleStorey'      null 'a/doubleStorey' 'hookAbove'
	CreateAccentedComposition 'aRightHalfRing/doubleStorey' null 'a/doubleStorey' 'rightHalfCircleAbove'

	CreateMultiAccentedComposition 'aDieresisMacron/doubleStorey' null 'a/doubleStorey' { 'dieresisAbove'   'macronAbove' }
	CreateMultiAccentedComposition 'aDotMacron/doubleStorey'      null 'a/doubleStorey' { 'dotAbove'        'macronAbove' }
	CreateMultiAccentedComposition 'aRingAcute/doubleStorey'      null 'a/doubleStorey' { 'ringAbove'       'acuteAbove'  }
	CreateMultiAccentedComposition 'aCircumAcute/doubleStorey'    null 'a/doubleStorey' { 'circumflexAbove' 'acuteAbove' }
	CreateMultiAccentedComposition 'aCircumGrave/doubleStorey'    null 'a/doubleStorey' { 'circumflexAbove' 'graveAbove' }
	CreateMultiAccentedComposition 'aCircumHook/doubleStorey'     null 'a/doubleStorey' { 'circumflexAbove' 'hookAbove'  }
	CreateMultiAccentedComposition 'aCircumTilde/doubleStorey'    null 'a/doubleStorey' { 'circumflexAbove' 'tildeAbove' }
	CreateMultiAccentedComposition 'aCircumBelowdot/doubleStorey' null 'a/doubleStorey' { 'circumflexAbove' 'dotBelow'   }
	CreateMultiAccentedComposition 'aBreveAcute/doubleStorey'     null 'a/doubleStorey' { 'breveAbove'      'acuteAbove' }
	CreateMultiAccentedComposition 'aBreveGrave/doubleStorey'     null 'a/doubleStorey' { 'breveAbove'      'graveAbove' }
	CreateMultiAccentedComposition 'aBreveHook/doubleStorey'      null 'a/doubleStorey' { 'breveAbove'      'hookAbove'  }
	CreateMultiAccentedComposition 'aBreveTilde/doubleStorey'     null 'a/doubleStorey' { 'breveAbove'      'tildeAbove' }
	CreateMultiAccentedComposition 'aBreveBelowdot/doubleStorey'  null 'a/doubleStorey' { 'breveAbove'      'dotBelow'   }

	CreateAccentedComposition 'aSbRsbUnderlineBelow' null 'a' 'sbRsbUnderlineBelow'
