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

import [mix linreg clamp fallback] from "@iosevka/util"
import [DesignParameters] from "../../meta/aesthetics.mjs"

glyph-module

glyph-block Letter-Greek-Lower-Sigma-Final : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives

	define [SigmaFinalEndingKnots] : list
		curl (SB + OX) SmallArchDepthB
		arcvh
		g4 [arch.adjust-x.bot Middle] 0
		alsoThru 0.5 0.1353
		g4.down.mid (RightSB - [HSwToV Stroke] + OX) [mix Descender Stroke : StrokeWidthBlend 0.5 0.49] [heading Downward]
		arcvh
		flat (RightSB - HookX + OX) (Descender + Stroke)
		curl [Math.min (RightSB - [HSwToV : 1.5 * Stroke]) (RightSB - HookX + OX - 1)] (Descender + Stroke)

	create-glyph 'grek/sigma/final' 0x3C2 : glyph-proc
		include : MarkSet.p
		include : dispiro
			widths.lhs
			g4 RightSB (XH - Hook)
			hookstart XH
			flat (SB + OX) (XH - SmallArchDepthA)
			SigmaFinalEndingKnots

	create-glyph 'grek/stigma' 0x3DB : glyph-proc
		include : MarkSet.p
		define fine : AdviceStroke 3
		include : dispiro
			widths.lhs fine
			straight.down.start (RightSB - [HSwToV fine]) (XH + 0.25 * Hook) [heading Downward]
			g2 [mix [arch.adjust-x.top Middle] (RightSB - [HSwToV fine]) 0.5] (XH - O - Hook * [StrokeWidthBlend 0.05 0.025]) [widths.lhs.heading Stroke Leftward]
			g4 [arch.adjust-x.top Middle] (XH - O)
			archv 16
			flat (SB + OX) (XH - SmallArchDepthA)
			SigmaFinalEndingKnots

	create-glyph 'grek/Stigma' 0x3DA : glyph-proc
		include : MarkSet.capital
		define yAttach : [if SLAB Stroke 0] + XH * 0.2
		include : difference
			dispiro
				widths.lhs
				g4 RightSB (CAP - Hook)
				hookstart CAP
				flatside.ld SB yAttach CAP ArchDepthA ArchDepthB
				arcvh
				g4 [arch.adjust-x.bot Middle] (yAttach + O)
				archv
				g4 [mix Middle RightSB 2] (yAttach + Hook * 1.5) [heading Upward]
			Rect (yAttach + Hook * 1.5 - O) Descender (Middle + [HSwToV HalfStroke]) VERY-FAR
		include : VBar.m Middle 0 (yAttach + HalfStroke)
		if SLAB : include : HSerif.mb Middle 0 MidJutCenter
