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

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

glyph-module

glyph-block Symbol-Math-Large-Operators : for-width-kinds WideWidth1
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Shared-Symbol-Shapes : TriangleShape
	glyph-block-import Letter-Latin-Long-S : LongSShape
	glyph-block-import Letter-Greek-Upper-Sigma : SigmaShape
	glyph-block-import Letter-Greek-Pi : PiShape
	glyph-block-import Letter-Blackboard : BBS BBD

	define MosaicTop    fontMetrics.os2.sTypoAscender
	define MosaicBottom fontMetrics.os2.sTypoDescender
	define MosaicHeight : MosaicTop - MosaicBottom

	define advanceScaleLargeOperators : Math.max para.advanceScaleM (MosaicWidth / Width)
	define df : DivFrame advanceScaleLargeOperators 0 [if FMosaicWide advanceScaleLargeOperators 1]

	create-glyph [MangleName 'sum'] [MangleUnicode 0x2211] : glyph-proc
		set-width df.width
		include : SigmaShape df BgOpTop BgOpBot OperatorStroke

	create-glyph [MangleName 'sumUpper'] [MangleUnicode 0x23B2] : glyph-proc
		set-width df.width
		include : intersection
			MaskAbove MosaicBottom
			SigmaShape df BgOpTop (BgOpBot - MosaicHeight * 2) OperatorStroke (pCenter -- 0.75)

	create-glyph [MangleName 'sumLower'] [MangleUnicode 0x23B3] : glyph-proc
		set-width df.width
		include : intersection
			MaskBelow MosaicTop
			SigmaShape df (BgOpTop + MosaicHeight * 2) BgOpBot OperatorStroke (pCenter -- 0.75)

	create-glyph [MangleName 'mathbb/sum'] [MangleUnicode 0x2140] : glyph-proc
		set-width df.width
		local kDiag : DiagCorDs (BgOpTop - BgOpBot) (df.rightSB - df.leftSB) BBD
		include : SigmaShape df BgOpTop BgOpBot BBS (noSerif -- true)
		include : SigmaShape df BgOpTop BgOpBot BBS (noSerif -- true) (offsetLeft -- kDiag * BBD)

	create-glyph [MangleName 'product'] [MangleUnicode 0x220F] : glyph-proc
		set-width df.width
		include : PiShape df BgOpTop BgOpBot (shrinkRate -- 0) (fine -- OperatorStroke) (doSerif -- SLAB)

	turned [MangleName 'coproduct'] [MangleUnicode 0x2210] [MangleName 'product'] df.middle SymbolMid

	create-glyph [MangleName 'Vee'] [MangleUnicode 0x22C1] : glyph-proc
		set-width df.width
		include : dispiro
			widths.center OperatorStroke
			flat df.leftSB BgOpTop
			curl df.middle BgOpBot [heading Downward]
		include : dispiro
			widths.center OperatorStroke
			flat df.rightSB BgOpTop
			curl df.middle BgOpBot [heading Downward]

	turned [MangleName 'Wedge'] [MangleUnicode 0x22C0] [MangleName 'Vee'] df.middle SymbolMid

	create-glyph [MangleName 'Cup'] [MangleUnicode 0x22C3] : glyph-proc
		set-width df.width
		include : UShape df BgOpTop BgOpBot
			stroke -- OperatorStroke
			ada -- [ArchDepthAOf (ArchDepth * [Math.sqrt df.adws]) (df.width)]
			adb -- [ArchDepthBOf (ArchDepth * [Math.sqrt df.adws]) (df.width)]

	turned [MangleName 'Cap'] [MangleUnicode 0x22C2] [MangleName 'Cup'] df.middle SymbolMid

	create-glyph [MangleName 'Top'] [MangleUnicode 0x27D9] : glyph-proc
		set-width df.width
		include : HBar.t df.leftSB df.rightSB BgTkTop OperatorStroke
		include : VBar.m df.middle BgTkTop BgTkBot OperatorStroke

	turned [MangleName 'Bot'] [MangleUnicode 0x27D8] [MangleName 'Top'] df.middle SymbolMid

	# These operators are always wide. In force-monospace variants they will be deleted
	if (MosaicWidthScalar > 1 && !para.forceMonospace) : do
		define kLongTackSB : [DivFrame MosaicWidthScalar 4].leftSB / [DivFrame MosaicWidthScalar 2].leftSB

		define l : mix 0 SB kLongTackSB
		define r : MosaicWidth - l

		create-glyph 'longVDash' 0x27DD : glyph-proc
			set-width MosaicWidth
			include : HBar.m l r SymbolMid OperatorStroke
			include : VBar.l l TackTop TackBot OperatorStroke

		turned 'longDashV' 0x27DE 'longVDash' (MosaicWidth / 2) SymbolMid

	create-glyph [MangleName 'SquareCap'] [MangleUnicode 0x2A05] : glyph-proc
		set-width df.width
		include : PiShape df BgOpTop BgOpBot (shrinkRate -- 0) (fine -- OperatorStroke) (doSerif -- false)

	turned [MangleName 'SquareCup'] [MangleUnicode 0x2A06] [MangleName 'SquareCap'] df.middle SymbolMid

	create-glyph [MangleName 'Multiply'] [MangleUnicode 0x2A09] : glyph-proc
		set-width df.width
		include : dispiro
			widths.center OperatorStroke
			corner df.leftSB  BgOpTop
			corner df.rightSB BgOpBot
		include : dispiro
			widths.center OperatorStroke
			corner df.leftSB  BgOpBot
			corner df.rightSB BgOpTop

	create-glyph [MangleName 'Bowtie'] [MangleUnicode 0x2A1D] : glyph-proc
		set-width df.width
		define swBowtie : Math.min OperatorStroke : AdviceStroke 4.25 MosaicWidthScalar
		define swBowtieHv : Math.min OperatorStroke : AdviceStroke 4.75 MosaicWidthScalar
		include : union
			intersection
				MaskRight df.leftSB
				MaskLeft df.rightSB
				union
					dispiro
						widths.center swBowtie
						corner df.leftSB  BgOpTop
						corner df.rightSB BgOpBot
					dispiro
						widths.center swBowtie
						corner df.leftSB  BgOpBot
						corner df.rightSB BgOpTop
			intersection
				spiro-outline
					corner df.leftSB  BgOpBot
					corner df.rightSB BgOpTop
					corner df.rightSB BgOpBot
					corner df.leftSB  BgOpTop
				union
					VBar.l df.leftSB  BgOpBot BgOpTop swBowtieHv
					VBar.r df.rightSB BgOpBot BgOpTop swBowtieHv

	create-glyph [MangleName 'WhiteTriangleLeft'] [MangleUnicode 0x2A1E] : glyph-proc
		set-width df.width
		include : TriangleShape df.leftSB SymbolMid df.rightSB BgOpTop df.rightSB BgOpBot OperatorStroke
