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

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

glyph-module

glyph-block Symbol-Arrow-Composite : for-width-kinds WideWidth1
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Symbol-Arrow-Shared : ArrowDims ArrowT MkArrowT

	define dims : ArrowDims MosaicWidth MosaicWidthScalar
	define MkArrow : MkArrowT dims
	define Arrow : ArrowT dims

	define {
		.arrowWidth arrowWidth
		.arrowTop arrowTop
		.arrowBot arrowBot
		.arrowMidX arrowMidX
		.arrowSB arrowSB
		.arrowRSB arrowRSB

		.arrowSw arrowSw
		.halfArrowSw halfArrowSw
		.arrowHeadSize arrowHeadSize
		.compactHeadSize compactHeadSize
		.fine fine
		.terminal terminal
	} dims

	define [ThinArrowShape x1 y1 x2 y2 size _halfSw] : glyph-proc
		local halfSw : fallback _halfSw halfArrowSw
		include : Arrow.Head.shape x1 y1 x2 y2 size
		include : Arrow.Bar.shape x1 y1 x2 y2 (0.5 * [mix fine terminal 0.5])

	# Thin arrows for composition
	MkArrow.single ThinArrowShape [MangleName 'thinArrowLeft']  null arrowRSB SymbolMid arrowSB SymbolMid
	# MkArrow.single ThinArrowShape [MangleName 'thinArrowUp']    null arrowMidX arrowBot arrowMidX arrowTop
	MkArrow.single ThinArrowShape [MangleName 'thinArrowRight'] null arrowSB SymbolMid arrowRSB SymbolMid
	# MkArrow.single ThinArrowShape [MangleName 'thinArrowDown']  null arrowMidX arrowTop arrowMidX arrowBot
	MkArrow.double ThinArrowShape ThinArrowShape [MangleName 'thinArrowLeftRight'] null arrowSB SymbolMid arrowRSB SymbolMid

	do "Arrow with Stroke"
		create-glyph [MangleName 'uni219A'] [MangleUnicode 0x219A] : composite-proc
			refer-glyph : MangleName 'arrowLeft'
			refer-glyph 'arrSlashOver'
		create-glyph [MangleName 'uni219B'] [MangleUnicode 0x219B] : composite-proc
			refer-glyph : MangleName 'arrowRight'
			refer-glyph 'arrSlashOver'
		create-glyph [MangleName 'uni21AE'] [MangleUnicode 0x21AE] : composite-proc
			refer-glyph : MangleName 'arrowLeftRight'
			refer-glyph 'arrSlashOver'
		create-glyph [MangleName 'uni21F7'] [MangleUnicode 0x21F7] : composite-proc
			refer-glyph : MangleName 'arrowLeft'
			refer-glyph 'arrVStrokeOver'
		create-glyph [MangleName 'uni21F8'] [MangleUnicode 0x21F8] : composite-proc
			refer-glyph : MangleName 'arrowRight'
			refer-glyph 'arrVStrokeOver'
		create-glyph [MangleName 'uni21F9'] [MangleUnicode 0x21F9] : composite-proc
			refer-glyph : MangleName 'arrowLeftRight'
			refer-glyph 'arrVStrokeOver'
		create-glyph [MangleName 'uni21FA'] [MangleUnicode 0x21FA] : composite-proc
			refer-glyph : MangleName 'arrowLeft'
			refer-glyph 'arrDblVStrokeOver'
		create-glyph [MangleName 'uni21FB'] [MangleUnicode 0x21FB] : composite-proc
			refer-glyph : MangleName 'arrowRight'
			refer-glyph 'arrDblVStrokeOver'
		create-glyph [MangleName 'uni21FC'] [MangleUnicode 0x21FC] : composite-proc
			refer-glyph : MangleName 'arrowLeftRight'
			refer-glyph 'arrDblVStrokeOver'
		create-glyph [MangleName 'uni21CD'] [MangleUnicode 0x21CD] : composite-proc
			refer-glyph : MangleName 'dblArrowLeft'
			refer-glyph 'arrSlashOver'
		create-glyph [MangleName 'uni21CE'] [MangleUnicode 0x21CE] : composite-proc
			refer-glyph : MangleName 'dblArrowLeftRight'
			refer-glyph 'arrSlashOver'
		create-glyph [MangleName 'uni21CF'] [MangleUnicode 0x21CF] : composite-proc
			refer-glyph : MangleName 'dblArrowRight'
			refer-glyph 'arrSlashOver'

		create-glyph [MangleName 'uni2908'] [MangleUnicode 0x2908] : composite-proc
			refer-glyph : MangleName 'arrowDown'
			refer-glyph 'arrHStrokeOver'
		create-glyph [MangleName 'uni2909'] [MangleUnicode 0x2909] : composite-proc
			refer-glyph : MangleName 'arrowUp'
			refer-glyph 'arrHStrokeOver'
		create-glyph [MangleName 'uni21DF'] [MangleUnicode 0x21DF] : composite-proc
			refer-glyph : MangleName 'arrowDown'
			refer-glyph 'arrDblHStrokeOver'
		create-glyph [MangleName 'uni21DE'] [MangleUnicode 0x21DE] : composite-proc
			refer-glyph : MangleName 'arrowUp'
			refer-glyph 'arrDblHStrokeOver'

		create-glyph [MangleName 'uni2902'] [MangleUnicode 0x2902] : composite-proc
			refer-glyph : MangleName 'dblArrowLeft'
			refer-glyph 'dblArrVStrokeOver'
		create-glyph [MangleName 'uni2903'] [MangleUnicode 0x2903] : composite-proc
			refer-glyph : MangleName 'dblArrowRight'
			refer-glyph 'dblArrVStrokeOver'
		create-glyph [MangleName 'uni2904'] [MangleUnicode 0x2904] : composite-proc
			refer-glyph : MangleName 'dblArrowLeftRight'
			refer-glyph 'dblArrVStrokeOver'

		create-glyph [MangleName 'uni2B7A'] [MangleUnicode 0x2B7A] : composite-proc
			refer-glyph : MangleName 'trigArrowLeft'
			refer-glyph 'arrDblVStrokeOver'
		create-glyph [MangleName 'uni2B7B'] [MangleUnicode 0x2B7B] : composite-proc
			refer-glyph : MangleName 'trigArrowUp'
			refer-glyph 'arrDblHStrokeOver'
		create-glyph [MangleName 'uni2B7C'] [MangleUnicode 0x2B7C] : composite-proc
			refer-glyph : MangleName 'trigArrowRight'
			refer-glyph 'arrDblVStrokeOver'
		create-glyph [MangleName 'uni2B7D'] [MangleUnicode 0x2B7D] : composite-proc
			refer-glyph : MangleName 'trigArrowDown'
			refer-glyph 'arrDblHStrokeOver'

	do "Arrow with ring"
		define [RingOverlayShape base baseThin overlay overlayMask] : composite-proc
			with-transform [Translate 0 0] : composite-proc
				refer-glyph : MangleName base
				clear-geometry
				refer-glyph : MangleName overlay
			difference
				refer-glyph : MangleName base
				with-transform [Translate 0 0] : composite-proc
					refer-glyph : MangleName base
					clear-geometry
					refer-glyph : MangleName overlayMask
			intersection
				refer-glyph : MangleName baseThin
				with-transform [Translate 0 0] : composite-proc
					refer-glyph : MangleName base
					clear-geometry
					refer-glyph : MangleName overlayMask

		create-glyph [MangleName 'uni21F4'] [MangleUnicode 0x21F4]
			RingOverlayShape 'arrowRight' 'thinArrowRight' 'arrRingOverLR' 'arrRingOverLRMask'
		create-glyph [MangleName 'uni2B30'] [MangleUnicode 0x2B30]
			RingOverlayShape 'arrowLeft' 'thinArrowLeft' 'arrRingOverLR' 'arrRingOverLRMask'
		create-glyph [MangleName 'uni2948'] [MangleUnicode 0x2948]
			RingOverlayShape 'arrowLeftRight' 'thinArrowLeftRight' 'arrRingOverLR' 'arrRingOverLRMask'

	do "Branched Arrows"
		local branchInset : 0.5 * MosaicWidth - 1.5 * arrowHeadSize
		create-glyph [MangleName 'arrowLeftFromDown'] [MangleUnicode 0x1F8C0]  : composite-proc
			refer-glyph : MangleName 'arrowDown'
			intersection [MaskLeft  arrowMidX]
				with-transform [ApparentTranslate   branchInset  0] : refer-glyph : MangleName 'arrowLeft'
		create-glyph [MangleName 'arrowRightFromDown'] [MangleUnicode 0x1F8C1] : composite-proc
			refer-glyph : MangleName 'arrowDown'
			intersection [MaskRight arrowMidX]
				with-transform [ApparentTranslate (-branchInset) 0] : refer-glyph : MangleName 'arrowRight'
