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

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

glyph-module

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

	glyph-block-import Mark-Shared-Metrics : markExtend markHalfStroke markStress markFine
	glyph-block-import Mark-Shared-Metrics : markMiddle markDotsRadius
	glyph-block-import Mark-Above : aboveMarkTop aboveMarkBot aboveMarkMid aboveMarkStack
	glyph-block-import Mark-Above : commaOvershoot commaAboveRadius
	glyph-block-import Mark-Below : belowMarkBot belowMarkTop belowMarkMid

	define [GreekLRDoubleMark id unicode gnLeft gnRight] : begin
		local k : -Width * 0.15
		derive-multi-part-glyphs id null { gnLeft gnRight } : function [srcs gr] : glyph-proc
			local { leftPart rightPart } srcs
			set-width 0

			include : refer-glyph leftPart
			include : Translate (2 * k) 0
			include : refer-glyph rightPart
			include : Translate (0 - k) 0

			set-mark-anchor 'above' markMiddle XH markMiddle aboveMarkStack
			set-base-anchor 'aboveBraceL' (markMiddle - 0.75 * markExtend) aboveMarkMid
			set-base-anchor 'aboveBraceR' (markMiddle + 0.75 * markExtend) aboveMarkMid

		derive-glyphs "\(id)GrekUpperTonos" null id : function [src gr] : glyph-proc
			set-width 0
			include [refer-glyph src]
			set-mark-anchor 'grekUpperTonos' (markMiddle - k) XH
			set-base-anchor 'aboveBraceL' (markMiddle - 0.75 * markExtend) aboveMarkMid
			set-base-anchor 'aboveBraceR' (markMiddle + 0.75 * markExtend) aboveMarkMid

		if unicode : derive-glyphs ('spaced' + id) unicode id : function [src gr] : glyph-proc
			include [refer-glyph 'markBaseSpace'] AS_BASE
			set-width [query-glyph 'markBaseSpace'].advanceWidth
			include [refer-glyph src]

	GreekLRDoubleMark 'psiliVaria' 0x1FCD 'commaAbove'    'variaAbove'
	GreekLRDoubleMark 'psiliOxia'  0x1FCE 'commaAbove'    'oxiaAbove'
	GreekLRDoubleMark 'dasiaVaria' 0x1FDD 'revCommaAbove' 'variaAbove'
	GreekLRDoubleMark 'dasiaOxia'  0x1FDE 'revCommaAbove' 'oxiaAbove'

	local yc : aboveMarkTop - commaAboveRadius + DotRadius * 0.3 + commaOvershoot

	derive-glyphs 'psiliPerispomeni' 0xEF01 'commaAbove' : function [src gr] : glyph-proc
		set-width 0
		include : refer-glyph src
		local radius : Math.max (markExtend - commaAboveRadius) (commaAboveRadius * 1.25)
		include : dispiro
			widths.lhs : Math.min (markHalfStroke * 2) ([AdviceStroke 6.5] * 1.25)
			g4.up.start (markMiddle - radius) yc [heading Upward]
			arcvh
			g4.right.mid markMiddle (yc + radius)
			archv
			g4.down.end (markMiddle + radius) yc [heading Downward]

		set-mark-anchor 'above' markMiddle XH markMiddle (aboveMarkStack + markHalfStroke * 2)
		set-base-anchor 'aboveBraceL' (markMiddle - 0.5 * markExtend) (aboveMarkMid + markHalfStroke)
		set-base-anchor 'aboveBraceR' (markMiddle + 0.5 * markExtend) (aboveMarkMid + markHalfStroke)
		ScheduleLeaningMark.set currentGlyph

	derive-glyphs 'psiliPerispomeniGrekUpperTonos' null 'psiliPerispomeni' : function [src gr] : glyph-proc
		set-width 0
		include : refer-glyph src
		set-mark-anchor 'grekUpperTonos' markMiddle XH
		set-base-anchor 'aboveBraceL' (markMiddle - 0.5 * markExtend) (aboveMarkMid + markHalfStroke)
		set-base-anchor 'aboveBraceR' (markMiddle + 0.5 * markExtend) (aboveMarkMid + markHalfStroke)

	derive-glyphs 'spaced_psiliPerispomeni' 0x1FCF 'psiliPerispomeni' : function [src gr] : glyph-proc
		include [refer-glyph 'markBaseSpace'] AS_BASE
		set-width [query-glyph 'markBaseSpace'].advanceWidth
		include : refer-glyph src

	derive-glyphs 'dasiaPerispomeni' 0xEF02 'revCommaAbove' : function [src gr] : glyph-proc
		set-width 0
		include : refer-glyph src
		local radius : Math.max (markExtend - commaAboveRadius) (commaAboveRadius * 1.25)
		include : dispiro
			widths.lhs : Math.min (markHalfStroke * 2) ([AdviceStroke 6.5] * 1.25)
			g4.up.start (markMiddle - radius) yc [heading Upward]
			arcvh
			g4.right.mid markMiddle (yc + radius)
			archv
			g4.down.end (markMiddle + radius) yc [heading Downward]

		set-mark-anchor 'above' markMiddle XH markMiddle (aboveMarkStack + markHalfStroke * 2)
		set-base-anchor 'aboveBraceL' (markMiddle - 0.5 * markExtend) (aboveMarkMid + markHalfStroke)
		set-base-anchor 'aboveBraceR' (markMiddle + 0.5 * markExtend) (aboveMarkMid + markHalfStroke)
		ScheduleLeaningMark.set currentGlyph

	derive-glyphs 'dasiaPerispomeniGrekUpperTonos' null 'dasiaPerispomeni' : function [src gr] : glyph-proc
		set-width 0
		include : refer-glyph src
		set-mark-anchor 'grekUpperTonos' markMiddle XH
		set-base-anchor 'aboveBraceL' (markMiddle - 0.5 * markExtend) (aboveMarkMid + markHalfStroke)
		set-base-anchor 'aboveBraceR' (markMiddle + 0.5 * markExtend) (aboveMarkMid + markHalfStroke)

	derive-glyphs 'spaced_dasiaPerispomeni'  0x1FDF 'dasiaPerispomeni' : function [src gr] : glyph-proc
		include [refer-glyph 'markBaseSpace'] AS_BASE
		set-width [query-glyph 'markBaseSpace'].advanceWidth
		include : refer-glyph src

	derive-glyphs 'cyrlPsiliPokrytieAbove' null 'commaAbove' : function [src gr] : glyph-proc
		set-width 0
		include : refer-glyph src
		local radius : Math.max (markExtend - commaAboveRadius) (commaAboveRadius * 1.25)
		local sw : Math.min (markHalfStroke * 2) ([AdviceStroke 6.5] * 1.25)
		include : dispiro
			widths.lhs sw
			g4.up.start (markMiddle - radius) yc [heading Upward]
			arcvh
			g2.right.mid markMiddle (yc + radius)
			alsoThru.g2 0.5 0.5
			g2.right.end (markMiddle + [Math.max radius (markExtend * 2)]) yc [heading Rightward]

		set-mark-anchor 'above' markMiddle XH markMiddle (aboveMarkStack + markHalfStroke * 2)
		set-base-anchor 'aboveBraceL' (markMiddle - markExtend) (aboveMarkMid + markHalfStroke)
		set-base-anchor 'aboveBraceR' (markMiddle + markExtend) (aboveMarkMid + markHalfStroke)
