$$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-I : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Letter-Shared : SetGrekUpperTonos CreateAccentedComposition
	glyph-block-import Mark-Adjustment : ExtendAboveBaseAnchors

	define [IShape df top] : glyph-proc
		include : VBar.m df.middle 0 top

	define ISerifs : namespace
		export : define [Impl df top jut] : glyph-proc
			include : HSerif.mb df.middle 0   jut
			include : HSerif.mt df.middle top jut
			set-base-anchor 'trailing' (df.middle + jut) 0

		export : define [None  df top] : no-shape
		export : define [Long  df top] : Impl df top : Math.max MidJutCenter (LongJut * df.adws)
		export : define [Short df top] : Impl df top : Math.max Jut     (MidJutCenter * df.adws)

	define UpperIConfig : object
		'straight'     { para.advanceScaleI  ISerifs.None  }
		'serifless'    { para.advanceScaleII ISerifs.None  }
		'serifed'      { para.advanceScaleI  ISerifs.Long  }
		'shortSerifed' { para.advanceScaleII ISerifs.Short }

	foreach { suffix { adws Serifs } } [Object.entries UpperIConfig] : do
		create-glyph "I.\(suffix)" : glyph-proc
			local df : include : DivFrame adws
			include : df.markSet.capital
			include : IShape df CAP
			include : Serifs df CAP

		create-glyph "grek/Iota.\(suffix)" : glyph-proc
			local df : DivFrame adws
			include [refer-glyph "I.\(suffix)"] AS_BASE ALSO_METRICS
			include : SetGrekUpperTonos [Math.min 0 : df.middle - [if (Serifs != no-shape) Jut : HSwToV HalfStroke] - SB]

		create-glyph "ISideways.\(suffix)" : glyph-proc
			local df : DivFrame (XH / Width) 2 (XH * 0.1 / SB)
			include : PointingTo Width XH Width 0 : function [] : glyph-proc
				include : IShape df RightSB
				include : Serifs df RightSB
				include : ApparentTranslate 0 (0.5 * SB)

		create-glyph "ILonga.\(suffix)" : glyph-proc
			local df : include : DivFrame adws
			include : df.markSet.capital
			local top : CAP + (Ascender - XH)
			include : ExtendAboveBaseAnchors top
			include : IShape df top
			include : Serifs df top

	select-variant 'I' 'I'
	link-reduced-variant 'I/sansSerif' 'I' MathSansSerif
	select-variant 'grek/Iota' 0x399 (follow -- 'I')
	link-reduced-variant 'grek/Iota/sansSerif' 'grek/Iota' MathSansSerif (follow -- 'I/sansSerif')
	alias 'cyrl/IUkrainian' 0x406 'I'
	CreateAccentedComposition 'cyrl/Yi' 0x407 'I' 'dieresisAbove'
	alias 'cyrl/Palochka' 0x4C0 'I'

	select-variant 'ISideways' 0xA7F7 (follow -- 'I')
	select-variant 'ILonga' 0xA7FE (follow -- 'I')

	CreateAccentedComposition 'IAcute' 0xCD 'I' 'acuteAbove'

	create-glyph 'smcpI' 0x26A : glyph-proc
		local df : include : DivFrame para.advanceScaleI
		include : df.markSet.e
		include : IShape df XH
		include : ISerifs.Impl df XH MidJutCenter

	CreateAccentedComposition 'smcpIBarOver' 0x1D7B 'smcpI' 'barOver'

	glyph-block-import Letter-Blackboard : BBS BBD BBBarCenter
	create-glyph 'mathbb/I' 0x1D540 : glyph-proc
		include : MarkSet.capital
		include : BBBarCenter Middle 0 CAP
		include : HBar.t (Middle - BBD / 2 - Jut) (Middle + BBD / 2 + Jut) CAP BBS
		include : HBar.b (Middle - BBD / 2 - Jut) (Middle + BBD / 2 + Jut) 0   BBS
