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

import [mix linreg clamp fallback] from "@iosevka/util"
import [Point] from "@iosevka/geometry/point"
import [MathSansSerif] from "@iosevka/glyph/relation"
import [DesignParameters] from "../../meta/aesthetics.mjs"

glyph-module

glyph-block Letter-Greek-Upper-Gamma: begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Shared-Shapes : CyrDescender MidHook LegShape
	glyph-block-import Letter-Shared-Shapes : LetterBarOverlay PalatalHook
	glyph-block-import Letter-Blackboard : BBS BBD BBBarLeft
	glyph-block-import Letter-Latin-Upper-F : xMidBarShrink
	glyph-block-import Mark-Adjustment : ExtendAboveBaseAnchors LeaningAnchor

	define SLAB-NONE   0
	define SLAB-TR     1
	define SLAB-BOTTOM 2
	define SLAB-ALL    3

	define GammaBarLeftX : SB * 1.5
	define GammaRightX : RightSB - 0.75 * OX
	define [GammaShape top bot slabType] : glyph-proc
		include : LeaningAnchor.Below.VBar.l GammaBarLeftX
		include : tagged 'strokeV' : VBar.l GammaBarLeftX bot top
		include : tagged 'strokeH' : HBar.t (GammaBarLeftX - O) GammaRightX top
		match slabType
			[Just SLAB-ALL] : begin
				include : HSerif.lt GammaBarLeftX top SideJut
				include : tagged 'serifLB' : HSerif.lb (GammaBarLeftX + [HSwToV HalfStroke]) bot Jut
				include : tagged 'serifLB' : HSerif.rb (GammaBarLeftX + [HSwToV HalfStroke]) bot MidJutSide
				include : tagged 'serifRT' : VSerif.dr GammaRightX top VJut
			[Just SLAB-BOTTOM] : begin
				include : tagged 'serifLB' : HSerif.lb (GammaBarLeftX + [HSwToV HalfStroke]) bot Jut
				include : tagged 'serifLB' : HSerif.rb (GammaBarLeftX + [HSwToV HalfStroke]) bot MidJutSide
			[Just SLAB-TR] : begin
				include : tagged 'serifRT' : VSerif.dr GammaRightX top VJut

	define GammaConfig : object
		serifless       { SLAB-NONE   false }
		topRightSerifed { SLAB-TR     true  }
		bottomSerifed   { SLAB-BOTTOM false }
		serifed         { SLAB-ALL    true  }

	foreach { suffix { slabType doST } } [Object.entries GammaConfig] : do
		create-glyph "grek/Gamma.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : GammaShape CAP 0 slabType

		create-glyph "grek/Digamma.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : GammaShape CAP 0 slabType
			local yBar : CAP * DesignParameters.upperEBarPos
			include : HBar.m GammaBarLeftX (RightSB - [xMidBarShrink doST]) yBar
			if doST : include : tagged 'serifRM'
				VSerif.dr (RightSB - [xMidBarShrink doST]) (yBar + HalfStroke) [mix Stroke VJut 0.5]

		create-glyph "cyrl/GheDescender.\(suffix)" : glyph-proc
			include [refer-glyph "grek/Gamma.\(suffix)"] AS_BASE ALSO_METRICS
			include : CyrDescender.rSideJut
				x   -- (GammaBarLeftX + [HSwToV Stroke])
				y   -- 0
				jut -- MidJutSide

		create-glyph "cyrl/GheDHook.\(suffix)" : glyph-proc
			include [refer-glyph "grek/Gamma.\(suffix)"] AS_BASE ALSO_METRICS
			include : PalatalHook.rSideJut
				x   -- (GammaBarLeftX + [HSwToV Stroke])
				y   -- 0
				jut -- MidJutSide

		create-glyph "cyrl/ghe.upright.\(suffix)" : glyph-proc
			include : MarkSet.e
			include : GammaShape XH 0 slabType

		create-glyph "cyrl/gheDescender.upright.\(suffix)" : glyph-proc
			include [refer-glyph "cyrl/ghe.upright.\(suffix)"] AS_BASE ALSO_METRICS
			include : CyrDescender.rSideJut
				x   -- (GammaBarLeftX + [HSwToV Stroke])
				y   -- 0
				jut -- MidJutSide

		create-glyph "cyrl/gheDHook.upright.\(suffix)" : glyph-proc
			include [refer-glyph "cyrl/ghe.upright.\(suffix)"] AS_BASE ALSO_METRICS
			include : PalatalHook.rSideJut
				x   -- (GammaBarLeftX + [HSwToV Stroke])
				y   -- 0
				jut -- MidJutSide

		create-glyph "cyrl/Ge.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : ExtendAboveBaseAnchors (CAP + LongVJut - QuarterStroke)
			include : GammaShape CAP 0 slabType
			eject-contour 'serifRT'
			include : VBar.r GammaRightX CAP (CAP + LongVJut - QuarterStroke) VJutStroke

		create-glyph "cyrl/ge.upright.\(suffix)" : glyph-proc
			include : MarkSet.e
			include : ExtendAboveBaseAnchors (XH + LongVJut - QuarterStroke)
			include : GammaShape XH 0 slabType
			eject-contour 'serifRT'
			include : VBar.r GammaRightX XH (XH + LongVJut - QuarterStroke) VJutStroke

		create-glyph "cyrl/ge.italic.\(suffix)" : glyph-proc
			include [refer-glyph "cyrl/ge.upright.\(suffix)"] AS_BASE ALSO_METRICS
			eject-contour 'serifLB'
			eject-contour 'strokeV'
			include : LegShape
				ztop -- [Point.fromXY Point.Type.Corner GammaBarLeftX XH]
				zbot -- [Point.fromXY Point.Type.Corner [mix SB 0 : if doST 1 0.75] 0]
				xb   -- GammaBarLeftX

		create-glyph "cyrl/GheMidHook.\(suffix)" : glyph-proc
			include [refer-glyph "grek/Gamma.\(suffix)"] AS_BASE ALSO_METRICS
			include : MarkSet.capDesc
			include : MidHook.general
				left   -- (GammaBarLeftX + [HSwToV Stroke])
				right  -- RightSB
				top    -- (CAP * HBarPos + QuarterStroke)
				ada    -- ArchDepthA
				adb    -- ArchDepthB

		create-glyph "cyrl/gheMidHook.\(suffix)" : glyph-proc
			include [refer-glyph "cyrl/ghe.upright.\(suffix)"] AS_BASE ALSO_METRICS
			include : MarkSet.p
			include : MidHook.general
				left   -- (GammaBarLeftX + [HSwToV Stroke])
				right  -- RightSB
				top    -- (XH * HBarPos + QuarterStroke)
				ada    -- (ArchDepthA * (HBarPos ** 0.3))
				adb    -- (ArchDepthB * (HBarPos ** 0.3))
			if para.isItalic : eject-contour 'serifLB'

	select-variant 'grek/Gamma' 0x393
	link-reduced-variant 'grek/Gamma/sansSerif' 'grek/Gamma' MathSansSerif
	select-variant 'cyrl/Ghe' 0x413 (shapeFrom -- 'grek/Gamma')
	select-variant 'cyrl/GheDescender' 0x4F6 (follow -- 'cyrl/Ghe')
	select-variant 'cyrl/GheDHook' (follow -- 'cyrl/Ghe')
	select-variant 'cyrl/Ge' 0x490
	select-variant 'cyrl/GheMidHook' 0x494 (follow -- 'cyrl/Ghe')

	select-variant 'cyrl/ghe.upright'
	select-variant 'cyrl/gheDescender.upright' (follow -- 'cyrl/ghe.upright')
	select-variant 'cyrl/gheDHook.upright' (follow -- 'cyrl/ghe.upright')
	select-variant 'cyrl/ge.upright' (follow -- 'cyrl/ge')
	select-variant 'cyrl/ge.italic' (follow -- 'cyrl/ge')
	select-variant 'cyrl/gheMidHook' 0x495 (follow -- 'cyrl/ghe.upright')
	select-variant 'grek/smcpGamma' 0x1D26 (shapeFrom -- 'cyrl/ghe.upright') (follow -- 'grek/Gamma')

	select-variant 'grek/Digamma' 0x3DC (follow -- 'grek/Gamma')

	create-glyph 'grek/digamma' 0x3DD : glyph-proc
		include : MarkSet.p
		include : GammaShape XH Descender SLAB-NONE
		local yBar : mix 0 XH DesignParameters.upperEBarPos
		include : HBar.m GammaBarLeftX (RightSB - [xMidBarShrink SLAB]) yBar
		if SLAB : include : tagged 'serifRM'
			VSerif.dr (RightSB - [xMidBarShrink SLAB]) (yBar + HalfStroke) [mix Stroke VJut 0.5]

	define [GhaynOverlayBar top] : LetterBarOverlay.l GammaBarLeftX (top * (1 - OverlayPos))

	derive-composites 'cyrl/Ghayn' 0x492 'cyrl/Ghe' [GhaynOverlayBar CAP]
	derive-composites 'cyrl/ghayn.upright' null 'cyrl/ghe.upright' [GhaynOverlayBar XH]

	derive-composites 'cyrl/GheStrokeHook' 0x4FA 'cyrl/GheDHook' [GhaynOverlayBar CAP]
	derive-composites 'cyrl/gheStrokeHook.upright' null 'cyrl/gheDHook.upright' [GhaynOverlayBar XH]

	create-glyph 'mathbb/grek/Gamma' 0x213E : glyph-proc
		include : MarkSet.capital
		include : BBBarLeft GammaBarLeftX 0 CAP
		include : HBar.t GammaBarLeftX GammaRightX CAP BBS
