$$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 [ISeriflessShape df top bot jut] : glyph-proc
		include : VBar.m df.middle bot top

	define [ISerifShapeImpl df top bot jut] : glyph-proc
		include : VBar.m df.middle bot top
		include : HSerif.mb df.middle bot jut
		include : HSerif.mt df.middle top jut
		set-base-anchor 'trailing' (df.middle + jut) bot

	define [ISerifShape df top bot jut] : ISerifShapeImpl df top bot (jut * df.adws)

	define UpperIConfig : object
		'straight'     { para.advanceScaleI  ISeriflessShape LongJut      }
		'serifless'    { para.advanceScaleII ISeriflessShape LongJut      }
		'serifed'      { para.advanceScaleI  ISerifShape     LongJut      }
		'shortSerifed' { para.advanceScaleI  ISerifShape     MidJutCenter }

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

		create-glyph "grek/Iota.\(suffix)" : glyph-proc
			include [refer-glyph "I.\(suffix)"] AS_BASE ALSO_METRICS
			include : SetGrekUpperTonos 0

		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 : Body df (Width - SB) 0 jut
				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 : Body df top 0 jut

	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 : ISerifShapeImpl df XH 0 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
