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

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

glyph-module

glyph-block Mark-Overlay : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives

	glyph-block-import Mark-Shared-Metrics : markExtend MarkStroke markStress markFine
	glyph-block-import Mark-Shared-Metrics : asciiMarkZoomX asciiMarkZoomY
	glyph-block-import Mark-Shared-Metrics : markMiddle markDotsRadius
	glyph-block-import Mark-Above : aboveMarkTop aboveMarkBot aboveMarkMid TildeShape RingShape
	glyph-block-import Mark-Below : belowMarkBot belowMarkTop belowMarkMid

	local tildeHeight : 0.75 * (aboveMarkTop - aboveMarkBot)
	local tildeHalfWidth : markExtend + [HSwToV : 0.4 * Stroke]

	do "Overlay on stems"
		create-glyph 'tildeOver' 0x334 : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' markMiddle (XH / 2) markMiddle (XH / 2)
			include : TildeShape
				ttop      --  (XH / 2 + tildeHeight / 2)
				tbot      --  (XH / 2 - tildeHeight / 2)
				leftEnd   --  (markMiddle - tildeHalfWidth)
				rightEnd  --  (markMiddle + tildeHalfWidth)
				hs        --  ([AdviceStroke 4] / 2)

		create-glyph 'dblTildeOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' markMiddle (XH / 2) markMiddle (XH / 2)
			local ttop : XH / 2 + tildeHeight / 2
			local tbot : XH / 2 - tildeHeight / 2

			define [addTilde] : TildeShape
				ttop      --  ttop
				tbot      --  tbot
				leftEnd   --  (markMiddle - tildeHalfWidth)
				rightEnd  --  (markMiddle + tildeHalfWidth)
				hs        --  ([AdviceStroke 4.5] / 2)

			local shift : (ttop - tbot) * 1.2
			include : addTilde
			include : ApparentTranslate 0 shift

			include : addTilde
			include : ApparentTranslate 0 (shift * (-0.5))

		create-glyph 'asciiTilde.symbolMid' : glyph-proc
			include : TildeShape
				ttop      --  (SymbolMid + tildeHeight * 0.5 * asciiMarkZoomY)
				tbot      --  (SymbolMid - tildeHeight * 0.5 * asciiMarkZoomY)
				leftEnd   --  SB
				rightEnd  --  RightSB
				hs        --  (OperatorStroke / 2)

		create-glyph 'tildeOverOnExension' : glyph-proc
			set-width 0
			set-mark-anchor 'overlayOnExtension' markMiddle (XH / 2) markMiddle (XH / 2)
			include : TildeShape
				ttop      --  (XH / 2 + tildeHeight / 3)
				tbot      --  (XH / 2 - tildeHeight / 3)
				leftEnd   --  (markMiddle - tildeHalfWidth)
				rightEnd  --  (markMiddle + tildeHalfWidth)
				hs        --  ([AdviceStroke 4] / 2)

		create-glyph 'invLazySOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			local sw : AdviceStroke 4
			local gap : Math.max (Width * 0.1) : AdviceStroke 6
			local xStart : [HSwToV HalfStroke] + gap
			local r : Math.max (0.75 * markExtend) sw
			include : dispiro
				widths.center sw
				straight.right.start (+xStart) (2 * (+r)) [heading Rightward]
				archv
				g2.down.mid ((+xStart) + (+r)) (+r) [heading Downward]
				arcvh
				flat (+xStart) 0 [heading Leftward]
				curl (-xStart) 0 [heading Leftward]
				archv
				g2.down.mid ((-xStart) + (-r)) (-r) [heading Downward]
				arcvh
				straight.right.end (-xStart) (2 * (-r)) [heading Rightward]

		create-glyph 'barOver' 0x335 : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' markMiddle (XH / 2) markMiddle (XH / 2)
			include : HOverlayBar (markMiddle - LongJut * 0.75) (markMiddle + LongJut * 0.75) (XH / 2)

		create-glyph 'dblBarOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' markMiddle (XH / 2) markMiddle (XH / 2)
			local gap : 0.2 * CAP
			include : HOverlayBar (markMiddle - LongJut * 0.75) (markMiddle + LongJut * 0.75) (XH / 2 + 0.5 * gap)
			include : HOverlayBar (markMiddle - LongJut * 0.75) (markMiddle + LongJut * 0.75) (XH / 2 - 0.5 * gap)

		create-glyph 'shortSlashOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' markMiddle (XH / 2) markMiddle (XH / 2)
			include : FlatSlashShape markMiddle (XH / 2) (0.5 * OverlayStroke)

		create-glyph 'antiSlashOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' markMiddle (XH / 2) markMiddle (XH / 2)
			include : FlatSlashShape markMiddle (XH / 2) (0.5 * OverlayStroke) (+0.75) (-0.3)

		create-glyph 'ringOver' 0x20D8 : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0
			include : RingShape 0 0 tildeHalfWidth

		create-glyph 'aleutSlashOver' : glyph-proc
			set-width 0
			set-mark-anchor 'armOverlay' 0 0 0 0
			include : dispiro
				widths.center OverlayStroke
				flat (-markExtend) (+markExtend)
				curl (+markExtend) (-markExtend)

		create-glyph 'legSlashOver' : glyph-proc
			set-width 0
			set-mark-anchor 'legOverlay' 0 0 0 0
			include : dispiro
				widths.center OverlayStroke
				flat (-markExtend) (-markExtend)
				curl (+markExtend) (+markExtend)

	do "Strikes on entire letter on blank (bowl)"
		create-glyph 'hStrike' : glyph-proc
			set-width 0
			set-mark-anchor 'strike' markMiddle (XH / 2) markMiddle (XH / 2)
			include : HOverlayBar ([mix 0 SB 0.5] - Width) ([mix Width RightSB 0.5] - Width) (XH / 2)

		create-glyph 'strikeThrough' 0x0336 : glyph-proc
			set-width 0
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : HOverlayBar (0 - Width) (0) (XH / 2)

		create-glyph 'oblStrike' : glyph-proc
			set-width 0
			set-mark-anchor 'strike' markMiddle (XH / 2) markMiddle (XH / 2)
			include : HOverlayObliqueBar ([mix 0 SB 0.5] - Width) ([mix Width RightSB 0.5] - Width) (XH / 2)

		create-glyph 'tildeStrike' : glyph-proc
			set-width 0
			set-mark-anchor 'strike' markMiddle (XH / 2) markMiddle (XH / 2)
			include : TildeShape
				ttop      --  (XH / 2 + tildeHeight / 2)
				tbot      --  (XH / 2 - tildeHeight / 2)
				leftEnd   --  (markMiddle - Width / 2)
				rightEnd  --  (markMiddle + Width / 2)
				hs        --  (OverlayStroke / 2)

		create-glyph 'tildeStrike2' : glyph-proc
			set-width 0
			set-mark-anchor 'strike' markMiddle (XH / 2) markMiddle (XH / 2)
			include : TildeShape
				ttop      --  (XH / 2 + tildeHeight / 2)
				tbot      --  (XH / 2 - tildeHeight / 2)
				leftEnd   --  (markMiddle - Width / 2 + SB)
				rightEnd  --  (markMiddle + Width / 2 - SB)
				hs        --  (OverlayStroke / 2)

		create-glyph 'tildeStrikeDivMM' : glyph-proc
			set-width 0
			set-mark-anchor 'strike' markMiddle (XH / 2) markMiddle (XH / 2)
			include : TildeShape
				ttop      --  (XH / 2 + tildeHeight / 2)
				tbot      --  (XH / 2 - tildeHeight / 2)
				leftEnd   --  (markMiddle - Width / 2 * para.advanceScaleMM)
				rightEnd  --  (markMiddle + Width / 2 * para.advanceScaleMM)
				hs        --  (OverlayStroke / 2)

	do "Slashes"
		local fine : AdviceStroke 6
		local fineDbl : AdviceStroke 7
		create-glyph 'shortSlash' 0x337 : glyph-proc
			set-width 0
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat (SB      - O * 3 + 0.5 * fine - Width) (XH * (-0.1)) [widths.center fine]
				curl (RightSB + O * 3 - 0.5 * fine - Width) (XH * (+1.1))

		create-glyph 'longSlash' 0x338 : glyph-proc
			set-width 0
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat (SB      - O * 3 + 0.5 * fine - Width) (XH * 0.5 - CAP * 0.6) [widths.center fine]
				curl (RightSB + O * 3 - 0.5 * fine - Width) (XH * 0.5 + CAP * 0.6)

		create-glyph 'longVStrokeOver' 0x20D2 : glyph-proc
			set-width 0
			set-mark-anchor 'slash' markMiddle (XH * 0.5) markMiddle (XH * 0.5)
			include : dispiro
				flat markMiddle (XH * 0.5 - CAP * 0.6) [widths.center fine]
				curl markMiddle (XH * 0.5 + CAP * 0.6)

		create-glyph 'shortVStrokeOver' 0x20D3 : glyph-proc
			set-width 0
			set-mark-anchor 'slash' markMiddle (XH * 0.5) markMiddle (XH * 0.5)
			include : dispiro
				flat markMiddle (XH * (-0.1)) [widths.center fine]
				curl markMiddle (XH * (+1.1))

		create-glyph 'revLongSlash' 0x20E5 : glyph-proc
			set-width 0
			set-mark-anchor 'slash' markMiddle (XH * 0.5) markMiddle (XH * 0.5)
			include : dispiro
				flat (RightSB + O * 3 - 0.5 * fine - Width) (XH * 0.5 - CAP * 0.6) [widths.center fine]
				curl (SB      - O * 3 + 0.5 * fine - Width) (XH * 0.5 + CAP * 0.6)

		create-glyph 'dblLongVStrokeOver' 0x20E6 : glyph-proc
			set-width 0
			local dy : CAP * 0.6
			local gap : Math.max fineDbl (Width * 0.1)
			set-mark-anchor 'slash' markMiddle (XH * 0.5) markMiddle (XH * 0.5)
			include : dispiro
				flat (markMiddle - gap) (XH * 0.5 - dy) [widths.center fineDbl]
				curl (markMiddle - gap) (XH * 0.5 + dy)
			include : dispiro
				flat (markMiddle + gap) (XH * 0.5 - dy) [widths.center fineDbl]
				curl (markMiddle + gap) (XH * 0.5 + dy)

		create-glyph 'dblLongSlash' 0x20EB : glyph-proc
			set-width 0
			local l : SB      - O * 3 + 0.5 * fineDbl - Width
			local r : RightSB + O * 3 - 0.5 * fineDbl - Width
			local dy : CAP * 0.6
			local gap : Math.max fineDbl (Width * 0.1)
			set-mark-anchor 'slash' markMiddle (XH * 0.5) markMiddle (XH * 0.5)
			include : dispiro
				flat (l - gap) (XH * 0.5 - dy) [widths.center fineDbl]
				curl (r - gap) (XH * 0.5 + dy)
			include : dispiro
				flat (l + gap) (XH * 0.5 - dy) [widths.center fineDbl]
				curl (r + gap) (XH * 0.5 + dy)

		create-glyph 'wideSlash' : glyph-proc
			local df : DivFrame para.advanceScaleMM 3
			set-width 0
			local l : markMiddle - (df.rightSB - df.leftSB) / 2 + O * 3 + 0.5 * fine
			local r : markMiddle + (df.rightSB - df.leftSB) / 2 - O * 3 - 0.5 * fine
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat l (XH * (-0.1)) [widths.center fine]
				curl r (XH * (+1.1))

	do "Arrow overlays"
		local fine : AdviceStroke 6
		create-glyph 'arrSlashOver' : glyph-proc
			set-width 0
			local dx : Width * 0.1
			local dy : Width * 0.4
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat (Middle - dx - Width) (XH / 2 - dy) [widths.center fine]
				curl (Middle + dx - Width) (XH / 2 + dy)

		create-glyph 'arrVStrokeOver' : glyph-proc
			set-width 0
			local dx : Width * 0
			local dy : Width * 0.3
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat (Middle - dx - Width) (XH / 2 - dy) [widths.center fine]
				curl (Middle + dx - Width) (XH / 2 + dy)

		create-glyph 'dblArrVStrokeOver' : glyph-proc
			set-width 0
			local dx : Width * 0
			local dy : Width * 0.4
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat (Middle - dx - Width) (XH / 2 - dy) [widths.center fine]
				curl (Middle + dx - Width) (XH / 2 + dy)

		create-glyph 'arrDblVStrokeOver' : glyph-proc
			set-width 0
			local dx : Width * 0
			local dy : Width * 0.3
			local gap : Math.max fine (Width * 0.1)
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat (Middle - dx - Width - gap) (XH / 2 - dy) [widths.center fine]
				curl (Middle + dx - Width - gap) (XH / 2 + dy)
			include : dispiro
				flat (Middle - dx - Width + gap) (XH / 2 - dy) [widths.center fine]
				curl (Middle + dx - Width + gap) (XH / 2 + dy)

		create-glyph 'arrHStrokeOver' : glyph-proc
			set-width 0
			local dx : Width * 0.3
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat (Middle - Width - dx) (XH / 2) [widths.center fine]
				curl (Middle - Width + dx) (XH / 2)

		create-glyph 'arrDblHStrokeOver' : glyph-proc
			set-width 0
			local dx : Width * 0.3
			local gap : Math.max fine (Width * 0.1)
			set-mark-anchor 'slash' markMiddle (XH / 2) markMiddle (XH / 2)
			include : dispiro
				flat (Middle - Width - dx) (XH / 2 - gap) [widths.center fine]
				curl (Middle - Width + dx) (XH / 2 - gap)
			include : dispiro
				flat (Middle - Width - dx) (XH / 2 + gap) [widths.center fine]
				curl (Middle - Width + dx) (XH / 2 + gap)

		define [ArrowRingOverlay kr c] :  glyph-proc
			set-width 0
			local r : Width * kr
			set-mark-anchor 'slash' 0 0 0 0
			include : RingStrokeAt 0 0 r [AdviceStroke c]

		define [ArrowRingMaskOverlay kr c] :  glyph-proc
			set-width 0
			local r : Width * kr
			set-mark-anchor 'slash' 0 0 0 0
			include : RingAt 0 0 r [AdviceStroke c]

		create-glyph 'arrRingOver.NWID'   : ArrowRingOverlay 0.25 5
		create-glyph 'arrRingOver.WWID'   : ArrowRingOverlay 0.35 4.5
		create-glyph 'arrRingOverLR.NWID' : ArrowRingOverlay 0.2  6
		create-glyph 'arrRingOverLR.WWID' : ArrowRingOverlay 0.35 4.5
		create-glyph 'arrRingOverMask.NWID'   : ArrowRingMaskOverlay 0.25 5
		create-glyph 'arrRingOverMask.WWID'   : ArrowRingMaskOverlay 0.35 4.5
		create-glyph 'arrRingOverLRMask.NWID' : ArrowRingMaskOverlay 0.2  6
		create-glyph 'arrRingOverLRMask.WWID' : ArrowRingMaskOverlay 0.35 4.5

	do "Half-XH marks for spacing modifiers"
		create-glyph 'leftTackOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			include : VBar.m (+markExtend) (XH * (-0.25)) (XH * (+0.25)) MarkStroke
			include : HBar.m (-markExtend) (+markExtend) 0 MarkStroke

		create-glyph 'rightTackOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			include : VBar.m (-markExtend) (XH * (-0.25)) (XH * (+0.25)) MarkStroke
			include : HBar.m (-markExtend) (+markExtend) 0 MarkStroke

		create-glyph 'upTackOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			local ext : Math.min markExtend (belowMarkTop - belowMarkBot)
			include : VBar.m 0 (-ext) (+ext) (markFine * 2)
			include : HBar.b (-markExtend) (+markExtend) (-ext) (markFine * 2)

		create-glyph 'downTackOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			local ext : Math.min markExtend (belowMarkTop - belowMarkBot)
			include : VBar.m 0 (-ext) (+ext) (markFine * 2)
			include : HBar.t (-markExtend) (+markExtend) (+ext) (markFine * 2)

		create-glyph 'plusOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			local ext : Math.min markExtend (belowMarkTop - belowMarkBot)
			include : VBar.m 0 (-ext) (+ext) (markFine * 2)
			include : HBar.m (-markExtend) (+markExtend) 0 (markFine * 2)

		create-glyph 'minusOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			local ext : 0.5 * [Math.min markExtend (belowMarkTop - belowMarkBot)]
			include : HBar.m (-markExtend) (+markExtend) 0 (markFine * 2)

		create-glyph 'equalOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			local ext : Math.min markExtend (belowMarkTop - belowMarkBot)
			local sw : Math.min (2 * markFine) ((2 / 3) * ext)
			include : HBar.t (-markExtend) (+markExtend) (+ext) sw
			include : HBar.b (-markExtend) (+markExtend) (-ext) sw

		create-glyph 'rightHalfCircleOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			include : with-transform [ApparentTranslate (-markMiddle) (-aboveMarkMid)]
				refer-glyph 'rightHalfCircleAbove'

		create-glyph 'leftHalfCircleOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			include : with-transform [ApparentTranslate (-markMiddle) (-aboveMarkMid)]
				refer-glyph 'leftHalfCircleAbove'

		create-glyph 'cyrlInvKavykaOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			include : with-transform [ApparentTranslate (-markMiddle) (-aboveMarkMid)]
				refer-glyph 'cyrlInvKavykaAbove'

		create-glyph 'cyrlKavykaOver' : glyph-proc
			set-width 0
			set-mark-anchor 'overlay' 0 0 0 0

			include : with-transform [ApparentTranslate (-markMiddle) (-aboveMarkMid)]
				refer-glyph 'cyrlKavykaAbove'

		foreach { suffix { DrawAt kdr } } [Object.entries DotVariants] : do
			create-glyph "cyrlKavykaWithDotOver.\(suffix)" : glyph-proc
				set-width 0
				set-mark-anchor 'overlay' 0 0 0 0

				include : with-transform [ApparentTranslate (-markMiddle) (-aboveMarkMid)]
					refer-glyph "cyrlKavykaWithDotAbove.\(suffix)"

		select-variant 'cyrlKavykaWithDotOver' (follow -- 'diacriticDot')

	do "Inner dots"
		glyph-block-export InnerDot
		define [InnerDot x y kHeight fRound kdr _space _adws] : begin
			local space : fallback _space : InnerDot.spaceOfDf [DivFrame 1]
			local adws : fallback _adws 4
			local halfDotWidth  : kdr * [Math.max (DotRadius / 8) : Math.min DotRadius (space / adws)]
			local halfDotHeight : kdr * [Math.max (DotRadius / 8) : Math.min DotRadius (halfDotWidth * kHeight)]

			if fRound
			: then : return : OShapeOutline.NoOvershoot
				y + halfDotHeight
				y - halfDotHeight
				x - halfDotWidth
				x + halfDotWidth
				Stroke * halfDotWidth * 2 / Width
				ArchDepthAOf halfDotWidth (halfDotWidth * 2)
				ArchDepthBOf halfDotWidth (halfDotWidth * 2)
			: else : return : Rect
				y + halfDotHeight
				y - halfDotHeight
				x - halfDotWidth
				x + halfDotWidth

		set [InnerDot.spaceOfDf df] : df.rightSB - df.leftSB - 2 * OX - [HSwToV : 2 * df.mvs]

		define [InnerDot2 kHeight fRound kdr] : glyph-proc
			local space : InnerDot.spaceOfDf [DivFrame 1]
			local adws : StrokeWidthBlend 7 5
			local halfDotWidth : kdr * [Math.min DotRadius (space / adws)]
			local kHeight2 : adws / 4 * kHeight
			local offset : (0.5 * space - 2 * halfDotWidth) / 3 + halfDotWidth
			include : InnerDot (-offset) 0 kHeight2 fRound kdr space adws
			include : InnerDot (+offset) 0 kHeight2 fRound kdr space adws

		foreach { suffix { DrawAt kdr } } [Object.entries DotVariants] : do
			create-glyph "innerDot.\(suffix)" : glyph-proc
				set-width 0
				set-mark-anchor 'slash' 0 0 0 0
				include : InnerDot 0 0 2 (DrawAt === DotAt) kdr
			create-glyph "innerDotSmall.\(suffix)" : glyph-proc
				set-width 0
				set-mark-anchor 'slash' 0 0 0 0
				include : InnerDot 0 0 1.5 (DrawAt === DotAt) kdr

			create-glyph "innerDoubleDot.\(suffix)" : glyph-proc
				set-width 0
				set-mark-anchor 'slash' 0 0 0 0
				include : InnerDot2 2 (DrawAt === DotAt) kdr
			create-glyph "innerDoubleDotSmall.\(suffix)" : glyph-proc
				set-width 0
				set-mark-anchor 'slash' 0 0 0 0
				include : InnerDot2 1.5 (DrawAt === DotAt) kdr

		select-variant "innerDot" (follow -- 'diacriticDot')
		select-variant "innerDotSmall" (follow -- 'diacriticDot')
		select-variant "innerDoubleDot" (follow -- 'diacriticDot')
		select-variant "innerDoubleDotSmall" (follow -- 'diacriticDot')

		# Dot for L
		WithDotVariants 'LDotDot' null : function [DrawAt kdr overshoot] : glyph-proc
			set-width 0
			include : DrawAt 0 0 (DotRadius * kdr - overshoot)
			set-mark-anchor 'cvDecompose' 0 0
