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

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

glyph-module

glyph-block Letter-Latin-Lower-H : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Shared-Metrics : markExtend
	glyph-block-import Mark-Above : aboveMarkMid
	glyph-block-import Mark-Below : belowMarkMid
	glyph-block-import Mark-Adjustment : LeaningAnchor
	glyph-block-import Letter-Shared-Shapes : RightwardTailedBar nShoulder CyrDescender
	glyph-block-import Letter-Shared-Shapes : TopHook PalatalHook EngHook RetroflexHook

	define [SmallHSerifs tailed hookTop] : glyph-proc : begin
		include : SmallHLeftSerifs tailed hookTop
		if [not tailed] : include : tagged 'serifRB' : HSerif.mb (RightSB - [HSwToV HalfStroke]) 0 Jut

	define [SmallHLeftSerifs tailed hookTop] : glyph-proc : begin
		include : SmallHTopLeftSerifs tailed hookTop
		include : tagged 'serifLB' : HSerif.mb (SB + [HSwToV HalfStroke]) 0 Jut

	define [SmallHMotionSerifs tailed hookTop] : glyph-proc
		include : SmallHTopLeftSerifs tailed hookTop
		if [not tailed] : include : tagged 'serifRB' : HSerif.rb RightSB 0 SideJut

	define [SmallHTopLeftSerifs tailed hookTop] : glyph-proc
		if [not hookTop] : include : tagged 'serifLT' : HSerif.lt SB Ascender SideJut

	define [HBar.mOverlay fHasTopSerif] : glyph-proc
		local barSpaceTop : Ascender - [if fHasTopSerif Stroke 0]
		local s : Math.min OverlayStroke (0.625 * (barSpaceTop - XH))
		local xOverlayStart : mix SB 0 0.7
		local xOverlayEnd : mix SB RightSB 0.5
		local yOverlay : [mix XH barSpaceTop 0.45] - s / 2
		include : dispiro
			widths.lhs s
			corner xOverlayStart yOverlay
			corner (SB + [HSwToV HalfStroke]) yOverlay
		include : dispiro
			widths.lhs OverlayStroke
			corner (SB + [HSwToV HalfStroke]) yOverlay
			corner xOverlayEnd yOverlay

	define HConfig : SuffixCfg.weave
		object # tail
			straight false
			tailed   true
		object # serifs
			serifless      { no-shape            false }
			serifed        { SmallHSerifs        true  }
			motionSerifed  { SmallHMotionSerifs  true  }
			topLeftSerifed { SmallHTopLeftSerifs true  }

	foreach { suffix { fTailed {Serifs fHasTopSerif} } } [Object.entries HConfig] : do
		create-glyph "h.\(suffix)" : glyph-proc
			include : MarkSet.b
			include : LeaningAnchor.Above.VBar.l SB
			include : VBar.l SB 0 Ascender
			include : nShoulder.shape
				left -- (SB + [HSwToV Stroke])
				right -- RightSB
				bottom -- [if fTailed (XH - SmallArchDepthB + O) 0]
			if fTailed : include : RightwardTailedBar RightSB 0 (XH - SmallArchDepthB)
			include : Serifs fTailed false

		create-glyph "hStroke.\(suffix)" : glyph-proc
			include [refer-glyph "h.\(suffix)"] AS_BASE ALSO_METRICS
			include : HBar.mOverlay fHasTopSerif

		create-glyph "hHookTop.\(suffix)" : glyph-proc
			include : MarkSet.b
			include : LeaningAnchor.Above.VBar.l SB
			include : TopHook.toRight.lBarInner SB 0 Ascender
			include : nShoulder.shape
				left -- (SB + [HSwToV Stroke])
				right -- RightSB
				bottom -- [if fTailed (XH - SmallArchDepthB + O) 0]
			if fTailed : include : RightwardTailedBar RightSB 0 (XH - SmallArchDepthB)
			include : Serifs fTailed true

		if [not fTailed] : begin
			create-glyph "heng.\(suffix)" : glyph-proc
				include : MarkSet.bp
				include : LeaningAnchor.Above.VBar.l SB
				include : VBar.l SB 0 Ascender
				include : nShoulder.shape
					left -- (SB + [HSwToV Stroke])
					right -- RightSB
					bottom -- 0
				include : Serifs true false
				include : EngHook RightSB 0 Descender

			create-glyph "hengStroke.\(suffix)" : glyph-proc
				include [refer-glyph "heng.\(suffix)"] AS_BASE ALSO_METRICS
				include : HBar.mOverlay fHasTopSerif

			create-glyph "hengHookTop.\(suffix)" : glyph-proc
				include : MarkSet.bp
				include : LeaningAnchor.Above.VBar.l SB
				include : TopHook.toRight.lBarInner SB 0 Ascender
				include : nShoulder.shape
					left -- (SB + [HSwToV Stroke])
					right -- RightSB
					bottom -- 0
				include : Serifs true true
				include : EngHook RightSB 0 Descender

	select-variant 'h' 'h'
	link-reduced-variant 'h/descBase' 'h'
	link-reduced-variant 'h/sansSerif' 'h' MathSansSerif

	select-variant 'hStroke' 0x127 (follow -- 'h')

	select-variant 'hHookTop' 0x266

	select-variant 'heng' 0xA727
	select-variant 'cyrl/dje' 0x452 (follow -- 'heng') (shapeFrom -- 'hengStroke')
	select-variant 'hengHookTop' 0x267

	alias 'cyrl/shha' 0x4BB 'h'
	alias 'cyrl/tshe' 0x45B 'hStroke'
	alias 'cyrl/hwe'  0xA695 'hHookTop'

	derive-composites 'hPalatalHook' 0xA795 'h/descBase' [PalatalHook.rSideJut RightSB 0]
	derive-composites 'hDescender' 0x2C68 'h/descBase' [CyrDescender.rSideJut RightSB 0]
	alias 'cyrl/shhaDescender' 0x527 'hDescender'

	derive-multi-part-glyphs 'hDot' 0x1E23 { 'h' 'dotAbove' } : lambda [srcs gr] : glyph-proc
		local { base mark } srcs
		include : refer-glyph mark
		include : Translate (Width + [HSwToV HalfStroke]) 0
		include [refer-glyph base] AS_BASE ALSO_METRICS
		include : LeaningAnchor.Above.VBar.l Middle

	derive-glyphs 'hCedilla' 0x1E29 'h' : lambda [src gr] : glyph-proc
		local shift : Width + SB - Middle + [HSwToV HalfStroke]
		include [refer-glyph 'cedillaBelow']
		include : ApparentTranslate shift 0
		include [refer-glyph src]
		include : MarkSet.bp
		set-base-anchor 'belowBraceL' ((-Middle) + shift - 0.5 * markExtend) belowMarkMid
		set-base-anchor 'belowBraceR' ((-Middle) + shift + 0.5 * markExtend) belowMarkMid

	glyph-block-import Letter-Blackboard : BBS BBD BBBarLeft
	create-glyph 'mathbb/h' 0x1D559 : glyph-proc
		local df : include : DivFrame 1 2
		include : df.markSet.e

		local bbs BBS
		local bbd BBD
		include : BBBarLeft df.leftSB 0 Ascender
		include : union
			HBar.b (df.rightSB - bbd) df.rightSB 0 bbs
			nShoulder.shape
				stroke -- bbs
				left -- (df.leftSB + bbd + [HSwToV bbs])
				right -- df.rightSB
				fine -- ShoulderFine
			intersection
				VBar.r (df.rightSB - bbd) 0 XH bbs
				nShoulder.mask
					stroke -- bbs
					left -- (df.leftSB + bbd + [HSwToV bbs] + 1)
					right -- (df.rightSB - 1)
					top -- (XH - 1)
					fine -- ShoulderFine
