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

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

glyph-module

glyph-block Letter-Latin-Upper-E : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Shared-Metrics : markHalfStroke
	glyph-block-import Letter-Shared : CreateAccentedComposition SetGrekUpperTonos CreateTurnedLetter
	glyph-block-import Letter-Latin-Upper-F : xMidBarShrink yMidBar EFVJutLength

	define kSB 1
	define xEBarLeft     : SB * kSB - O
	define xRevEBarRight : Width - SB * kSB + O

	define [EShape] : with-params [top pyBar serifLT serifLB serifV serifM [stroke : AdviceStroke2 2 3 top]] : glyph-proc
		local xMidRight : RightSB - [xMidBarShrink serifV]
		local yBar : yMidBar top pyBar
		local { jutTop jutBot jutMid } : EFVJutLength top pyBar stroke

		include : VBar.l (xEBarLeft) 0 top stroke
		include : HBar.t (xEBarLeft - O) RightSB top stroke
		include : HBar.m (xEBarLeft - O) xMidRight yBar stroke
		include : HBar.b (xEBarLeft - O) RightSB 0 stroke

		if serifLT : include : HSerif.lt xEBarLeft top SideJut
		if serifLB : include : HSerif.lb xEBarLeft 0 SideJut
		if serifV : begin
			include : VSerif.dr RightSB top jutTop
			include : VSerif.ur RightSB 0 jutBot
		if serifM : begin
			local fine : VJutStroke * stroke / Stroke
			include : VBar.r xMidRight (yBar - jutMid) (yBar + jutMid) fine

	glyph-block-export RevEShape
	define [RevEShape] : with-params [top pyBar serifRT serifRB serifV serifM [stroke : AdviceStroke2 2 3 top]] : glyph-proc
		local xMidLeft : SB + [xMidBarShrink serifV]
		local yBar : yMidBar top pyBar
		local { jutTop jutBot } : EFVJutLength top pyBar stroke

		include : VBar.r (xRevEBarRight) 0 top stroke
		include : HBar.t SB (xRevEBarRight + O) top stroke
		include : HBar.m xMidLeft (xRevEBarRight + O) yBar stroke
		include : HBar.b SB (xRevEBarRight + O) 0 stroke

		if serifRT : include : HSerif.rt xRevEBarRight top SideJut
		if serifRB : include : HSerif.rb xRevEBarRight 0 SideJut
		if serifV : begin
			include : VSerif.dl SB top jutTop
			include : VSerif.ul SB 0 jutBot
		if serifM : begin
			local fine : VJutStroke * stroke / Stroke
			include : VBar.l xMidLeft (yBar - 0.5 * jutBot) (yBar + 0.5 * jutBot) fine

	define EConfig : object
		serifless      { false false false false }
		topLeftSerifed { true  false false false }
		serifed        { true  true  true  false }
		serifedCapped  { true  true  true  true  }

	foreach { suffix { lt lb v m } } [Object.entries EConfig] : do
		create-glyph "E.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : EShape CAP (serifLT -- lt) (serifLB -- lb) (serifV -- v) (serifM -- m)
			set-base-anchor 'trailing' RightSB 0

		create-glyph "grek/Epsilon.\(suffix)" : glyph-proc
			include [refer-glyph "E.\(suffix)"] AS_BASE ALSO_METRICS
			include : SetGrekUpperTonos [if lt (-SideJut) 0]

		create-glyph "smcpE.\(suffix)" : glyph-proc
			include : MarkSet.e
			include : EShape XH (serifLT -- lt) (serifLB -- lb) (serifV -- v) (serifM -- m)
			set-base-anchor 'trailing' (RightSB - markHalfStroke) 0

		create-glyph "revE.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : RevEShape CAP (serifRT -- lt) (serifRB -- lb) (serifV -- v) (serifM -- m)

	select-variant 'E' 'E'
	link-reduced-variant 'E/sansSerif' 'E' MathSansSerif
	select-variant 'grek/Epsilon' 0x395 (follow -- 'E')
	link-reduced-variant 'grek/Epsilon/sansSerif' 'grek/Epsilon' MathSansSerif (follow -- 'E/sansSerif')

	alias 'cyrl/Ie' 0x415 'E'

	select-variant 'revE'   0x18E  (follow -- 'E')
	select-variant 'exists' 0x2203 (follow -- 'E/sansSerif') (shapeFrom -- 'revE')
	CreateAccentedComposition 'notExists' 0x2204 'exists' 'longSlash'

	select-variant 'smcpE' 0x1D07 (follow -- 'E')
	CreateTurnedLetter 'turnSmcpE' 0x2C7B 'smcpE' HalfAdvance (XH / 2)

	glyph-block-import Letter-Blackboard : BBS BBD
	create-glyph 'mathbb/E' 0x1D53C : glyph-proc
		include : MarkSet.capital
		include : union
			difference
				EShape CAP (serif -- false) (stroke -- BBS)
				Rect (CAP - BBS) BBS (xEBarLeft + [HSwToV BBS]) (xEBarLeft + BBD)
			VBar.l (xEBarLeft + BBD) 0 CAP BBS
