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

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

glyph-module

glyph-block Symbol-Math-Circled : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Symbol-Math-Frame : MathEnclosureSw
	glyph-block-import Symbol-Arrow-Shared : ArrowHead ArrowBar

	define [MakeMathCircled u inner] : begin
		derive-glyphs "mathCircled{\(inner)}" u inner : function [src gr]
			composite-proc
				refer-glyph 'mathO'
				intersection [refer-glyph 'mathOOutline'] [refer-glyph src]

	MakeMathCircled 0x2295 'innerPlus'
	MakeMathCircled 0x2296 'innerMinus'
	MakeMathCircled 0x2297 'innerMultiply'
	MakeMathCircled 0x2298 'innerMultiplyStroke1'
	MakeMathCircled 0x2299 'mathCDotInner'
	MakeMathCircled 0x229A 'whiteSmallCircle.NWID'
	MakeMathCircled 0x229B 'opAsterisk'
	MakeMathCircled 0x29B6 'innerVerticalBar'
	MakeMathCircled 0x29B8 'innerMultiplyStroke2'
	MakeMathCircled 0x29BA 'innerIsraeliPlus'
	MakeMathCircled 0x29BC 'rotatedInnerDivide'
	MakeMathCircled 0x29BE 'whiteVerySmallCircle.NWID'
	MakeMathCircled 0x29BF 'blackVerySmallCircle.NWID'
	MakeMathCircled 0x2A38 'innerDivide'

	define [MakeMathCircledAllowingOverflow u inner] : begin
		derive-glyphs "mathCircledAllowingOverflow{\(inner)}" u inner : function [src gr]
			composite-proc [refer-glyph 'mathO'] [refer-glyph src]

	MakeMathCircledAllowingOverflow 0x2300 'innerMultiplyStroke1'
	MakeMathCircledAllowingOverflow 0x29BB 'innerMultiply'

	define [MakeMathLeftHalfCircled u inner] : begin
		derive-glyphs "mathLeftHalfCircled{\(inner)}" u inner : function [src gr]
			composite-proc
				refer-glyph 'mathOlefthalf'
				intersection [refer-glyph 'mathOOutline'] [refer-glyph src]

	define [MakeMathRightHalfCircled u inner] : begin
		derive-glyphs "mathRightHalfCircled{\(inner)}" u inner : function [src gr]
			composite-proc
				refer-glyph 'mathOrighthalf'
				intersection [refer-glyph 'mathOOutline'] [refer-glyph src]

	MakeMathLeftHalfCircled  0x2A2D 'innerPlus'
	MakeMathRightHalfCircled 0x2A2E 'innerPlus'
	MakeMathLeftHalfCircled  0x2A34 'innerMultiply'
	MakeMathRightHalfCircled 0x2A35 'innerMultiply'

	create-glyph 0x2A36 : composite-proc [refer-glyph "mathCircled{innerMultiply}"] [MarkSet.plus] [refer-glyph 'circumflexAbove'] [clear-anchors]

	for-width-kinds WideWidth1
		define [MakeMathBigCircled u inner] : begin
			derive-glyphs [MangleName "mathBigCircled{\(inner)}"] [MangleUnicode u] [MangleName inner] : function [src gr] : composite-proc
				refer-glyph : MangleName 'mathOBig'
				intersection
					refer-glyph : MangleName 'mathOOutlineBig'
					refer-glyph src

		MakeMathBigCircled 0x2A00 'mathCDotBig'
		MakeMathBigCircled 0x2A01 'innerPlusBig'
		MakeMathBigCircled 0x2A02 'innerMultiplyBig'

	create-glyph 0x229D : composite-proc [refer-glyph 'mathO'] : intersection
		refer-glyph 'mathOOutline'
		HBar.m (SB + MathEnclosureSw) (RightSB - MathEnclosureSw) SymbolMid MathEnclosureSw

	local eqS : Math.min MathEnclosureSw ((RightSB - SB) / 8)
	local eqD : Math.max eqS ((RightSB - SB) / 6)
	create-glyph 0x229C : composite-proc [refer-glyph 'mathO'] : intersection
		refer-glyph 'mathOOutline'
		union
			HBar.m (SB + eqS) (RightSB - eqS) (SymbolMid + eqD) eqS
			HBar.m (SB + eqS) (RightSB - eqS) (SymbolMid - eqD) eqS

	create-glyph 0x29B5 : composite-proc [refer-glyph 'mathO'] : HBar.m [mix Middle SB Math.SQRT2] [mix Middle RightSB Math.SQRT2] SymbolMid MathEnclosureSw

	create-glyph 0x29B7 : composite-proc [refer-glyph 'mathO'] : intersection
		refer-glyph 'mathOOutline'
		union
			VBar.m (Middle + eqD) (PlusBot + eqS) (PlusTop - eqS) eqS
			VBar.m (Middle - eqD) (PlusBot + eqS) (PlusTop - eqS) eqS

	create-glyph 0x29B9 : composite-proc [refer-glyph 'mathO'] : intersection
		refer-glyph 'mathOOutline'
		union
			VBar.m Middle PlusTop (SymbolMid - eqD) eqS
			HBar.m (SB + eqS) (RightSB - eqS) (SymbolMid - eqD) eqS

	local arrowTop : mix SymbolMid BgOpTop 1.1
	local arrowBot : mix SymbolMid BgOpBot 1.1
	create-glyph 0x29BD : composite-proc [refer-glyph 'mathO'] : union
		ArrowHead Middle arrowBot Middle arrowTop ((RightSB - SB) / 2)
		ArrowBar  Middle arrowBot Middle arrowTop (MathEnclosureSw / 2)
