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

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


glyph-module

glyph-block Letter-Latin-Eth : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives

	define fine ShoulderFine

	define yMid : Ascender * 0.6
	define yMid2 : YSmoothMidR yMid 0 SmallArchDepthA SmallArchDepthB
	define yMid3 : mix yMid yMid2 0.875

	define [ArcStartKnots _flatY1 _flatY2] : list
		widths.lhs fine
		straight.up.mid (RightSB - OX - [HSwToV : Stroke - fine]) yMid2
		arch.lhs yMid (swBefore -- fine)
		g4 (SB + OX) [YSmoothMidL yMid 0 SmallArchDepthA SmallArchDepthB]
		arch.lhs 0
		flat (RightSB - OX) [fallback _flatY1 yMid2]
		curl (RightSB - OX) [fallback _flatY2 yMid3]

	create-glyph 'dInsular.curly-bar' : glyph-proc
		include : MarkSet.b
		include : dispiro
			ArcStartKnots
			quadControls 0 0.8
			g4 (SB + Stroke * 1.1) Ascender [widths.lhs]

	create-glyph 'dInsular.straight-bar' : glyph-proc
		include : MarkSet.b
		local yMockBarStart : yMid2 + Stroke * 0.3
		local xTerminal : [mix SB RightSB 0.4] + [HSwToV HalfStroke]
		local pStraightBarStart : 0.75 - (Stroke / Ascender)
		include : intersection [MaskBelow Ascender] : dispiro
			ArcStartKnots
			flat [mix Width xTerminal pStraightBarStart] [mix yMockBarStart CAP pStraightBarStart] [widths.lhs]
			curl [mix Width xTerminal 2]                 [mix yMockBarStart CAP 2]

	create-glyph 'dInsular.closed-contour' : glyph-proc
		include : MarkSet.b
		include : dispiro
			ArcStartKnots SmallArchDepthA (Ascender - SmallArchDepthB)
			hookend Ascender
			g4 (SB + OX) (Ascender - Hook)

	select-variant 'dInsular' 0xA77A (follow -- 'eth')
	select-variant 'partial'  0x2202 (shapeFrom -- 'dInsular')

	derive-composites 'eth' 0xF0 'dInsular' : dispiro
		widths.center OverlayStroke
		flat [mix SB RightSB 0.1]  [mix XH Ascender (-0.05)]
		curl [mix SB RightSB 0.95] [mix XH Ascender 0.35]

	create-glyph 'DInsular' 0xA779 : glyph-proc
		include : MarkSet.capital
		local df : DivFrame 1
		local fine : mix ShoulderFine Stroke 0.125
		local cofine : Stroke - fine
		local top : CAP - O

		include : union
			dispiro
				flat (df.middle + Stroke * TanSlope - O) (top - cofine) [widths.lhs fine]
				curl [arch.adjust-x.top df.middle (sw -- fine)] (top - cofine)
				archv
				flat df.leftSB (CAP - ArchDepthA) [widths.lhs Stroke]
				curl df.leftSB ArchDepthB
				arch.lhs 0
				flat df.rightSB ArchDepthA
				curl df.rightSB (CAP - ArchDepthB)
				arcvh
				straight.left.end [arch.adjust-x.top df.middle] top
			dispiro
				flat [arch.adjust-x.top df.middle] top [widths.lhs : AdviceStroke 3]
				curl [mix 0 df.leftSB 0.5] top
