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

import [mix linreg clamp fallback SuffixCfg] from "@iosevka/util"


glyph-module

glyph-block Letter-Latin-Gha : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Shared-Shapes : SerifFrame RightwardTailedBar
	glyph-block-import Letter-Latin-Lower-Q : RDiagTailedBar
	glyph-block-import Mark-Adjustment : LeaningAnchor

	define TERMINAL-NORMAL  0
	define TERMINAL-TAILED  1
	define TERMINAL-DIAG    2

	define [GhaShape df terminal top bot _ada _adb slab] : glyph-proc
		local gap : 0.375 * (df.width - 2 * df.leftSB - 2.5 * df.mvs) - [HSwToV : 0.25 * df.mvs]
		local divSub : (df.width - gap - df.mvs) / Width
		local subDf : DivFrame divSub 2

		local ada : _ada * 0.7 * df.adws
		local adb : _adb * 0.7 * df.adws
		include : OShape top 0 subDf.leftSB subDf.rightSB df.mvs ada adb
		include : match terminal
			[Just TERMINAL-NORMAL] : VBar.r             (df.rightSB - O) bot top df.mvs
			[Just TERMINAL-TAILED] : RightwardTailedBar (df.rightSB - O) bot top df.mvs
			[Just TERMINAL-DIAG]   : RDiagTailedBar     (df.rightSB - O) bot top df.mvs
		include : dispiro
			widths.lhs df.mvs
			flat (subDf.rightSB - [HSwToV : 0.5 * df.mvs])        (top - adb) [heading Rightward]
			curl (subDf.rightSB - [HSwToV : 0.5 * df.mvs] + TINY) (top - adb) [heading Rightward]
			alsoThru 0.5 0.15
			g4   (df.rightSB - O - [HSwToV df.mvs]) top [widths 0 df.mvs]

		if slab : begin
			local sf : SerifFrame.fromDf df top bot
			include sf.rb.full

	define GhaConfig : object
		straightSerifless       { TERMINAL-NORMAL false }
		straightBottomSerifed   { TERMINAL-NORMAL true  }
		tailedSerifless         { TERMINAL-TAILED false }
		diagonalTailedSerifless { TERMINAL-DIAG   false }

	foreach { suffix { terminal doSerif } } [Object.entries GhaConfig] : do
		create-glyph "Gha.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleM 3
			include : df.markSet.capDesc
			include : GhaShape df terminal CAP Descender ArchDepthA ArchDepthB doSerif
			include : LeaningAnchor.Below.VBar.r (df.rightSB - O)

		create-glyph "gha.\(suffix)" : glyph-proc
			local df : include : DivFrame para.advanceScaleM 3
			include : df.markSet.p
			include : GhaShape df terminal XH Descender SmallArchDepthA SmallArchDepthB doSerif
			include : LeaningAnchor.Below.VBar.r (df.rightSB - O)

	select-variant 'Gha' 0x1A2 (follow -- 'gha')
	select-variant 'gha' 0x1A3
