###### Letter-like symbols

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

import [Point] from "@iosevka/geometry/point"
import [Box] from "@iosevka/geometry/box"
import [Transform] from "@iosevka/geometry/transform"
import [mix linreg clamp fallback] from "@iosevka/util"
import [DesignParameters] from "../meta/aesthetics.mjs"

glyph-module

glyph-block Symbol-Currency : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Shared-Shapes : LetterBarOverlay LetterObliqueBarOverlay SerifFrame

	create-glyph 'currency' 0xA4 : glyph-proc
		local sw : AdviceStroke 3.5
		local radius : (RightSB - SB) / 2
		include : OShape (SymbolMid + radius * 0.9) (SymbolMid - radius * 0.9) (Middle - radius * 0.9) (Middle + radius * 0.9) sw
		foreach j [range 0 4] : begin
			local angle : Math.PI * (j / 2 + 0.25)
			include : dispiro
				widths.center sw
				flat (Middle + radius * 0.87 * [Math.cos angle]) (SymbolMid + radius * 0.87 * [Math.sin angle])
				curl (Middle + radius * Math.SQRT2 * [Math.cos angle]) (SymbolMid + radius * Math.SQRT2 * [Math.sin angle])

	define SterlingConfig : object
		serifless     { 0 }
		motionSerifed { 1 }

	foreach { suffix { serifGrade } } [Object.entries SterlingConfig] : do
		define xBarLeft : [mix SB RightSB 0.2] - Stroke * 0.1
		define [BaseShape] : glyph-proc
			local hookEndX : Math.max (xBarLeft + [HSwToV : 2.25 * Stroke]) [mix SB RightSB 0.95]
			include : union
				dispiro
					g4 hookEndX (CAP - Hook * 0.85) [widths.lhs]
					hookstart CAP
					flat xBarLeft [CAP - ArchDepthA * 0.75]
					curl xBarLeft [mix Stroke CAP 0.3]
					g4   SB Stroke
				HBar.b SB RightSB 0
			if serifGrade : include : VSerif.ur RightSB 0 VJut

		create-glyph "currency/sterlingSign.\(suffix)" : glyph-proc
			set-width Width
			include : BaseShape
			include : LetterBarOverlay.l xBarLeft (CAP * 0.5)

		create-glyph "currency/liraSymbolSign.\(suffix)" : glyph-proc
			set-width Width
			include : BaseShape
			include : LetterBarOverlay.l xBarLeft (CAP * 0.37)
			include : LetterBarOverlay.l xBarLeft (CAP * 0.60 - OverlayStroke * 0.25)

	select-variant 'currency/sterlingSign'   0xA3
	select-variant 'currency/liraSymbolSign' 0x20A4 (follow -- 'currency/sterlingSign')

	define TurkLiraConfig : object
		serifless { 0 }
		serifed   { 1 }

	foreach { suffix { serifGrade } } [Object.entries TurkLiraConfig] : do
		create-glyph "currency/turkishLiraSign.\(suffix)" : glyph-proc
			set-width Width
			include : MarkSet.capital
			local xBarLeft : SB * 1.5
			local xRight : RightSB - 0.75 * OX
			include : union
				dispiro
					widths.lhs Stroke
					flat xBarLeft 0 [heading Rightward]
					curl [mix xBarLeft xRight 0.5] 0
					archv
					straight.up.end (xRight - OXHook) Hook [heading Upward]
				VBar.l xBarLeft 0 CAP
			include : LetterObliqueBarOverlay.l xBarLeft (CAP * 0.37)
			include : LetterObliqueBarOverlay.l xBarLeft (CAP * 0.60 - OverlayStroke * 0.25)
			if serifGrade : begin
				include : HSerif.lb (xBarLeft + [HSwToV HalfStroke]) 0   Jut
				include : HSerif.lt (xBarLeft + [HSwToV HalfStroke]) CAP Jut
				include : HSerif.rt (xBarLeft + [HSwToV HalfStroke]) CAP MidJutSide

	select-variant 'currency/turkishLiraSign' 0x20BA

	create-glyph 'currency/sheqelSign' 0x20AA : glyph-proc
		local df : include : DivFrame [mix 1 para.advanceScaleMM 0.5] 4
		include : df.markSet.e

		define pX 0.7
		define pY 0.8

		local vswPlusGap : (df.rightSB - df.leftSB - [HSwToV df.mvs]) / 3
		local barShrink : Math.min (XH * 0.3) (vswPlusGap - [HSwToV df.mvs] + df.mvs * 1.5)
		include : VBar.l df.leftSB 0 XH df.mvs
		include : VBar.l (df.leftSB + vswPlusGap) 0 (XH - barShrink) df.mvs
		include : lift-@ : dispiro
			widths.rhs df.mvs
			flat df.leftSB XH [heading Rightward]
			curl [Math.min (@right - [HSwToV : Stroke * 1.5]) [mix df.leftSB @right pX]] XH
			archv
			flat @right [Math.min (XH - Stroke * 1.5) [mix 0 XH pY]]
			curl (@right = df.rightSB - vswPlusGap) barShrink [heading Downward]
		include : lift-@ : dispiro
			widths.lhs df.mvs
			flat (@left = df.leftSB + vswPlusGap) 0 [heading Rightward]
			curl [Math.min (df.rightSB - [HSwToV : Stroke * 1.5]) [mix @left df.rightSB pX]] 0
			archv
			flat df.rightSB [Math.max (Stroke * 1.5) [mix XH 0 pY]]
			curl df.rightSB XH [heading Upward]

	create-glyph 'currency/manatSign' 0x20BC : glyph-proc
		local df : include : DivFrame para.advanceScaleT 3

		include : UShape df XH 0 df.mvs df.smallArchDepthA df.smallArchDepthB
		include : FlipAround df.middle (XH / 2)

		include : VBar.m df.middle [if SLAB df.mvs 0] (XH + LongVJut - QuarterStroke) [Math.min VJutStroke df.mvs]

		if SLAB : begin
			local sf : SerifFrame.fromDf df XH 0 (swSerif -- df.mvs)
			include : composite-proc sf.lb.full sf.rb.full

	create-glyph 'currency/lariSign' 0x20BE : glyph-proc
		local df : include : DivFrame para.advanceScaleM 3
		local gap : df.rightSB - df.leftSB - [HSwToV : 2 * df.mvs]
		local fine : Math.min OverlayStroke
			AdviceStrokeInSpace gap HVContrast 3 1 1
		local offset : (gap - [HSwToV : 2 * fine]) / 6
		local barTop : CAP + (Ascender - XH) / 2
		local barBot : Math.max
			CAP * 0.5
			[mix CAP barTop (-2)] - df.mvs

		include : HBar.b df.leftSB df.rightSB 0 df.mvs
		include : dispiro
			widths.lhs df.mvs
			g4 (df.rightSB - OX) (CAP - Hook)
			hookstart CAP (sw -- df.mvs)
			flatside.ld df.leftSB 0 CAP df.archDepthA df.archDepthB
			arcvh
			flat df.middle  (df.mvs - df.shoulderFine) [widths.lhs df.shoulderFine]
			curl df.rightSB (df.mvs - df.shoulderFine) [heading Rightward]
		include : VBar.r (df.middle - offset) barBot barTop fine
		include : VBar.l (df.middle + offset) barBot barTop fine

glyph-block Symbol-Currency-Letter-Derived : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Shared-Shapes : LetterObliqueBarOverlay

	derive-composites 'currency/colonSign' 0x20A1 'C' 'dblLongSlash'
	derive-composites 'currency/millSign'  0x20A5 'm' 'shortSlash'

	create-glyph 'currency/overlay/NS' : glyph-proc
		define sw : Math.min OverlayStroke : AdviceStroke2 2 4 CAP
		define gap : Math.max (CAP / 8) (sw / 2)
		include : HBar.b [mix 0 SB 0.5] [mix Width RightSB 0.5] (CAP / 2 + gap / 2) sw
		include : HBar.t [mix 0 SB 0.5] [mix Width RightSB 0.5] (CAP / 2 - gap / 2) sw

	derive-composites 'currency/nairaSign' 0x20A6 'currency/nairaSignBase' 'currency/overlay/NS'
	derive-composites 'currency/hryvniaSign' 0x20B4 'revS' 'currency/overlay/NS'

	derive-composites 'currency/dongSign' 0x20AB 'dStroke' 'sbRsbUnderlineBelow'
	derive-composites 'currency/kipSign' 0x20AD 'K' 'hStrike'

	create-glyph 'currency/tugrikOverride' : glyph-proc
		set-width 0
		set-mark-anchor 'overlay' Middle (CAP * 0.5)
		include : LetterObliqueBarOverlay.m.in
			x   -- Middle
			bot -- 0
			top -- CAP
			py  -- 0.35
		include : LetterObliqueBarOverlay.m.in
			x   -- Middle
			bot -- 0
			top -- (CAP - Stroke)
			py  -- 0.65
	derive-composites 'currency/tugrikSign' 0x20AE 'T' 'currency/tugrikOverride'

	derive-composites 'currency/guaraniSign' 0x20B2 'G' 'longVStrokeOver'
	derive-composites 'currency/cediSign' 0x20B5 'C' 'longVStrokeOver'

	derive-composites 'currency/somSign' 0x20C0 'smcpC' 'sbRsbUnderlineBelow'

glyph-block Symbol-Letter : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Latin-X : XStrand
	glyph-block-import Letter-Shared-Shapes : FlatHookDepth
	glyph-block-import Mark-Above : aboveMarkMid

	alias 'eulerConst' 0x2107 'latn/Epsilon'
	alias 'scruple'    0x2108 'cyrl/E'

	alias 'Ohm' 0x2126 'grek/Omega'
	alias 'Mho' 0x2127 'latn/Upsilon'

	alias 'letterLike/kelvinSign' 0x212A 'K'

	for-width-kinds WideWidth1 : begin
		create-glyph [MangleName 'estimated'] [MangleUnicode 0x212E] : glyph-proc
			set-width MosaicWidth

			local cx 500
			local cy 450
			local r1 (846 / 2)
			local yGapSize 263
			local sw 183
			local r2 85
			local r3 13
			local swCenter 25

			# Scale and transform
			local desiredHeight : if FMosaicWide CAP XH
			local s : Math.min (desiredHeight / (2 * cy)) ((MosaicWidth - SB) / (2 * cx))
			local glyphCenter : currentGlyph.gizmo.applyXY (MosaicWidth / 2) (desiredHeight / 2)
			local tfm : Transform.Combine
				Transform.Translate (-cx) (-cy)
				Transform.Scale s s
				Transform.Translate glyphCenter.x glyphCenter.y

			# Center of inner round corner, as well as the limit X for the inner circle edge
			local xLargeRc : sw + r2
			local yLargeRc : cy - [Math.sqrt : (r1 - r2) * (r1 - r2) - (cx - sw - r2) * (cx - sw - r2)]
			local xInnerCircleLimit : cx - (cx - xLargeRc) * (r1 / (r1 - r2))

			include : ForceUpright
			include : difference [Ellipse.Outline.At cx cy cx cy]
				# Inner arcs
				intersection [Circle.Outline.At cx cy r1] : union
					Rect (2 * cy) (2 * cy - yLargeRc) xInnerCircleLimit (2 * cx - xInnerCircleLimit)
					Rect yLargeRc (-1) xInnerCircleLimit cx
					Rect (cy - yGapSize) (-1) (cx - 1) (2 * cx)

				# Large round corners
				Circle.Outline.At xLargeRc yLargeRc r2
				Circle.Outline.At xLargeRc (2 * cy - yLargeRc) r2
				Circle.Outline.At (2 * cx - xLargeRc) (2 * cy - yLargeRc) r2

				# Small round corners
				Circle.Outline.At (sw + r3) (cy + swCenter + r3) r3
				Circle.Outline.At (2 * cx - sw - r3) (cy + swCenter + r3) r3
				Circle.Outline.At (sw + r3) (cy - r3) r3

				# Top inner opening
				Rect (2 * cy - yLargeRc) (cy + swCenter + r3) sw (2 * cx - sw)
				Rect (cy + swCenter + r3 + 1) (cy + swCenter) (sw + r3) (2 * cx - sw - r3)

				# Bottom-left opening
				Rect (cy - r3) yLargeRc sw cx
				Rect cy (cy - r3 - 1) (sw + r3) cx

				# Bottom-right opening
				Rect cy (cy - yGapSize) (cx - 1) (2 * cx)

			include tfm

	create-glyph 'mathAleph' 0x2135 : glyph-proc
		include : MarkSet.capital
		define p 0.3
		include : XStrand true false SB CAP RightSB 0 0.1 0.4 0.28
		include : intersection
			dispiro
				widths.rhs
				g4.down.start RightSB CAP
				g4 [mix SB RightSB p] 0
			spiro-outline
				corner SB CAP
				corner RightSB CAP
				corner RightSB 0
		include : intersection
			dispiro
				widths.rhs
				g4.up.start SB 0
				g4 [mix SB RightSB (1 - p)] CAP
			spiro-outline
				corner SB 0
				corner RightSB 0
				corner SB CAP

	create-glyph 'mathBeth' 0x2136 : glyph-proc
		include : MarkSet.capital
		define pX 0.7
		define pY 0.8
		define turnX : mix SB RightSB pY
		include : dispiro
			widths.rhs
			flat SB CAP
			curl [Math.min (turnX - [HSwToV : 1.5 * Stroke]) [mix SB turnX pX]] CAP
			archv
			flat turnX [Math.min (CAP - Stroke * 1.5) [mix 0 CAP pY]]
			curl turnX 0
		include : HBar.b SB RightSB 0

	create-glyph 'mathGimel' 0x2137 : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		local hd : FlatHookDepth df

		local xStart : mix df.leftSB df.rightSB 0.1
		local xVertBar : [mix df.leftSB df.rightSB 0.625] + [HSwToV : 0.375 * Stroke]
		local yBranch : mix 0 CAP 0.5

		include : df.markSet.capital

		include : intersection [MaskAbove 0] : dispiro [widths.rhs]
			flat xStart CAP
			curl [Math.max (xStart + TINY) (xVertBar - hd.x)] CAP
			archv
			flat xVertBar (CAP - hd.y)
			curl xVertBar yBranch
			flat df.rightSB 0
			curl [mix xVertBar df.rightSB 2] [mix yBranch 0 2]

		include : dispiro [widths.rhs]
			g4.down.start xVertBar yBranch
			g4 (df.leftSB + [HSwToV : 0.2 * Stroke]) O

	create-glyph 'mathDalet' 0x2138 : glyph-proc
		include : MarkSet.capital
		include : HBar.t SB RightSB CAP
		include : VBar.r [mix SB RightSB 0.75] 0 CAP

	create-glyph 'informationSource' 0x2139 : glyph-proc
		include : MarkSet.b
		local sw : clamp para.canonicalStrokeWidthMin para.canonicalStrokeWidthMax : UnicodeWeightGrade 900
		include : VBar.m Middle 0 XH sw
		include : HSerif.lt (Middle - [HSwToV : 0.5 * sw]) XH (LongJut / 2)
		include : HSerif.lb (Middle - [HSwToV : 0.5 * sw]) 0  (LongJut / 2)
		include : HSerif.rb (Middle + [HSwToV : 0.5 * sw]) 0  (LongJut / 2)
		include : DotAt Middle aboveMarkMid (DotRadius * sw / Stroke)

	turned 'turnAmpersand' 0x214B 'ampersand' Middle (CAP / 2)

	do "Tironian Et"
		define [TironianEtTopBarShape y] : intersection
			Rect (y - O) 0 SB RightSB
			dispiro
				widths.rhs
				g4 (SB + O) (y - O)
				g4 Middle  (y + O)
				g4 RightSB (y - O)

		create-glyph 'tironianEt' 0x204A : glyph-proc
			include : TironianEtTopBarShape XH
			include : intersection
				Rect (XH - O - Stroke) (-Stroke) SB RightSB
				dispiro
					widths.rhs
					flat RightSB (XH - O - Stroke)
					curl (Middle + [HSwToV : 0.125 * Stroke]) O

		create-glyph 'TironianEt' 0x2E52 : glyph-proc
			include : TironianEtTopBarShape CAP
			include : dispiro
				widths.rhs
				flat RightSB (CAP - HalfStroke) [heading Downward]
				curl RightSB ArchDepthB
				hookend 0
				g4 SB Hook

	do "Roman Numerals"
		create-glyph 'romanFiftyEarlyForm' 0x2186 : glyph-proc
			include : MarkSet.capital
			include : VBar.m Middle 0 CAP
			include : dispiro
				flat (Middle - [HSwToV HalfStroke]) 0 [widths.rhs : AdviceStroke 3]
				curl (SB + O) [mix 0 CAP 0.375]
			include : dispiro
				flat (Middle + [HSwToV HalfStroke]) 0 [widths.lhs : AdviceStroke 3]
				curl (RightSB - O) [mix 0 CAP 0.375]
			if SLAB : include : HSerif.mt Middle CAP MidJutCenter

glyph-block Symbol-Cyrl-Thousands : begin
	glyph-block-import CommonShapes
	create-glyph 'cyrlThousandsSign' 0x482 : glyph-proc
		define fine : AdviceStroke 3
		include : ExtLineCenter (-0.1) fine SB Descender RightSB XH
		include : ExtLineCenter (-0.1) fine [mix SB RightSB (+0.1)] [mix Descender XH 0.8] [mix SB RightSB (+1.1)] [mix Descender XH 0.5]
		include : ExtLineCenter (-0.1) fine [mix SB RightSB (-0.1)] [mix Descender XH 0.5] [mix SB RightSB (+0.9)] [mix Descender XH 0.2]

glyph-block Symbol-Letter-Phonetic : begin
	glyph-block-import CommonShapes
	glyph-block-import Mark-Above : BreveShape InvBreveShape
	glyph-block-import Mark-Shared-Metrics : markExtend markHalfStroke

	create-glyph 'modifierArchBreve' 0xAB5B : glyph-proc
		include : MarkSet.e
		local archHeight : XH * 0.45

		include : dispiro
			widths.rhs
			g4.up.start (SB + OX) 0 [heading Upward]
			arch.rhs archHeight
			g4.down.end (RightSB - OX) 0 [heading Downward]

		include : dispiro
			widths.lhs
			g4.down.start (SB + OX) XH [heading Downward]
			arch.lhs (XH - archHeight)
			g4.up.end (RightSB - OX) XH [heading Upward]

	create-glyph 'cyrlInvLowKavykaWithKavyka' 0x2E46 : glyph-proc
		local df : include : DivFrame para.advanceScaleSp
		include : df.markSet.e

		include : BreveShape
			xMiddle -- df.middle
			width   -- (3 * markExtend)
			top     -- XH
			bottom  -- (XH - AccentHeight)
			hs      -- markHalfStroke

		include : InvBreveShape
			xMiddle -- df.middle
			width   -- (3 * markExtend)
			top     -- AccentHeight
			bottom  -- 0
			hs      -- markHalfStroke
