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

import [mix] from "@iosevka/util"

glyph-module

glyph-block Letter-Latin-Insular-G : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Shared : CreateTurnedLetter

	define [InsularGShape top bot] : glyph-proc
		local fine : AdviceStroke 4

		local pl : SmallArchDepthB / (SmallArchDepthA + SmallArchDepthB)
		local pr : SmallArchDepthA / (SmallArchDepthA + SmallArchDepthB)
		local fallArcShape : list {0.25 0.1} {0.5 0.21} {0.75 0.38}

		local xFirstTurn    : [mix SB RightSB 0.3] + [if SLAB 0.75 0.5] * [HSwToV Stroke]
		local xIntersection : [mix xFirstTurn RightSB 0.3] - [HSwToV HalfStroke]

		local yIntersection : [mix bot top 0.55] - QuarterStroke
		local yFirstTurn    : [mix yIntersection top 0.32] - 0.2 * Stroke
		local yBottomRingLeft  : [mix bot yIntersection (0.875 * pl)] + QuarterStroke
		local yBottomRingRight : [mix bot yIntersection (0.875 * pr)] + QuarterStroke
		local yPhantomBowlEnd  : mix bot yIntersection 1.2

		include : HBar.t SB RightSB top

		include : dispiro
			g4 RightSB (top - Stroke) [widths.rhs fine]
			bezControls 0.7 0.35 1 0.6 12
			g4.down.mid xFirstTurn yFirstTurn [widths.rhs.heading Stroke {.x HVContrast .y (+0.1)}]
			g2 xIntersection yIntersection [widths.center Stroke]
			alsoThruThem fallArcShape
			g4.down.mid (RightSB - O) yBottomRingRight [widths.rhs]
			arcvh
			arch.rhs.centerAt.rtl.b Middle bot

		include : difference
			dispiro
				arch.rhs.centerAt.rtl.b Middle bot
				archv
				g4.up.mid (SB + O) yBottomRingLeft
				g4 (Width - xIntersection) yIntersection [widths.center fine]
				g4 RightSB yPhantomBowlEnd
			spiro-outline
				corner xFirstTurn top
				g4.down.mid xFirstTurn yFirstTurn
				g2 xIntersection yIntersection
				alsoThruThem fallArcShape
				g4.down.mid (RightSB - O) yBottomRingRight
				corner (RightSB - O) bot
				corner (2 * Width) bot
				corner (2 * Width) top

		if SLAB : include : VSerif.dl SB top VJut

	create-glyph 'gInsular' 0x1D79 : glyph-proc
		include : MarkSet.p
		include : InsularGShape XH Descender

	create-glyph 'GInsular' 0xA77D : glyph-proc
		include : MarkSet.capital
		include : InsularGShape CAP 0

	CreateTurnedLetter 'GInsularTurned' 0xA77E 'GInsular' HalfAdvance (CAP / 2)
	CreateTurnedLetter 'gInsularTurned' 0xA77F 'gInsular' HalfAdvance [mix Descender XH 0.5]
