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

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

glyph-module

glyph-block Letter-Latin-Lower-J : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Adjustment : LeaningAnchor ExtendAboveBaseAnchors
	glyph-block-import Letter-Shared : CreateAccentedComposition
	glyph-block-import Letter-Shared-Shapes : CurlyTail FlatHookDepth LetterBarOverlay
	glyph-block-import Letter-Shared-Shapes : DiagTail TopHook
	glyph-block-import Letter-Blackboard : BBS BBD

	glyph-block-export FlatHookDotlessJShape
	define [FlatHookDotlessJShape] : with-params [df dfHook top crossLeft barCenter serif] : glyph-proc
		local bar : barCenter + [HSwToV HalfStroke]
		local hd : FlatHookDepth dfHook
		local terminal : Math.min
			bar - hd.x - [Math.max (Stroke / 3) (df.width / 6)]
			crossLeft - OX * 2

		if serif : include : HSerif.lt barCenter top (LongJut * df.adws)
		include : dispiro
			widths.lhs
			flat terminal Descender
			curl (bar - hd.x) Descender
			archv.superness DesignParameters.tightHookSuperness
			flat bar (Descender + hd.y)
			curl bar top [heading Upward]
		set-base-anchor 'above' barCenter top
		set-base-anchor 'trailing' (bar - hd.x) Descender
		set-base-anchor 'overlay' barCenter (top / 2)

	define XMiddle : namespace
		export : define [StraightSerifless df] : begin df.middle
		export : define [StraightSerifed df]   : df.middle + [IBalance2 df]
		export : define [BentHook df]          : df.middle + JBalance
		export : define [FlatHookSerifless df] : df.middle + 0.25 * JBalance * df.adws * [mix 1 df.adws 2]
		export : define [FlatHookSerifed df]   : df.middle + JBalance * df.adws

	define Body : namespace
		export : define [BentHook df top xMiddle] : glyph-proc
			local hookx : Math.min (xMiddle - (Width * 0.5) - [HSwToV HalfStroke] + OXHook) (xMiddle - [HSwToV : 1.5 * Stroke] + OXHook)
			set-base-anchor "below" [mix hookx xMiddle 0.5] Descender
			include : dispiro
				widths.rhs
				flat (xMiddle + [HSwToV HalfStroke]) top [heading Downward]
				curl (xMiddle + [HSwToV HalfStroke]) (Descender + ArchDepthA)
				hookend Descender
				g4 hookx (Descender + JHook)

		export : define [Straight df top xMiddle] : glyph-proc
			include : VBar.m xMiddle Descender top
			set-base-anchor "below" xMiddle Descender
			set-base-anchor "trailing" (xMiddle + [HSwToV HalfStroke]) Descender

		export : define [FlatHook df top xMiddle] : glyph-proc
			local dfHook : DivFrame para.advanceScaleI
			local crossLeft : df.middle - (dfHook.middle - [mix 0 dfHook.leftSB : mix 1 dfHook.adws 2])
			include : FlatHookDotlessJShape df dfHook top
				crossLeft -- crossLeft
				barCenter -- xMiddle
				serif -- false

		export : define [DiagonalTailed df top xMiddle] : glyph-proc
			local dfHook : DivFrame para.advanceScaleI
			include : dispiro
				widths.center
				flat xMiddle top [heading Downward]
				DiagTail.L xMiddle Descender [DiagTail.StdDepth dfHook Stroke] Stroke
			set-base-anchor "trailing" (xMiddle - [HSwToV HalfStroke]) Descender

	define Serifs : namespace
		export : define [None  df top xMiddle] : no-shape
		export : define [Long  df top xMiddle] : HSerif.lt xMiddle top (LongJut * df.adws)
		export : define [Short df top xMiddle] : HSerif.lt xMiddle top Jut

	define Marks : namespace
		export : define [Serifless df top xMiddle] : glyph-proc
			set-base-anchor 'above'   xMiddle  top
			set-base-anchor 'overlay' xMiddle (top / 2)

		export : define [Serifed df top xMiddle] : glyph-proc
			include : Serifless df top xMiddle
			include : LeaningAnchor.Above.At [mix df.middle xMiddle (7/8)]

	define Div : namespace
		export : define BentHook          1
		export : define StraightSerifless para.advanceScaleII
		export : define StraightSerifed   para.advanceScaleI
		export : define FlatHookSerifless para.advanceScaleII
		export : define FlatHookSerifed   para.advanceScaleI

	define JConfig : object
		'bentHookSerifless'          { "BentHook"        Serifs.None   XMiddle.BentHook           Marks.Serifless  Div.BentHook          }
		'bentHookShortSerifed'       { "BentHook"        Serifs.Short  XMiddle.BentHook           Marks.Serifless  Div.BentHook          }
		'bentHookSerifed'            { "BentHook"        Serifs.Long   XMiddle.BentHook           Marks.Serifed    Div.BentHook          }
		'straightSerifless'          { "Straight"        Serifs.None   XMiddle.StraightSerifless  Marks.Serifless  Div.StraightSerifless }
		'straightShortSerifed'       { "Straight"        Serifs.Short  XMiddle.StraightSerifless  Marks.Serifless  Div.StraightSerifless }
		'straightSerifed'            { "Straight"        Serifs.Long   XMiddle.StraightSerifed    Marks.Serifed    Div.StraightSerifed   }
		'flatHookSerifless'          { "FlatHook"        Serifs.None   XMiddle.FlatHookSerifless  Marks.Serifless  Div.FlatHookSerifless }
		'flatHookShortSerifed'       { "FlatHook"        Serifs.Short  XMiddle.FlatHookSerifless  Marks.Serifless  Div.FlatHookSerifless }
		'flatHookSerifed'            { "FlatHook"        Serifs.Long   XMiddle.FlatHookSerifed    Marks.Serifed    Div.FlatHookSerifed   }
		'diagonalTailedSerifless'    { "DiagonalTailed"  Serifs.None   XMiddle.FlatHookSerifless  Marks.Serifless  Div.FlatHookSerifless }
		'diagonalTailedShortSerifed' { "DiagonalTailed"  Serifs.Short  XMiddle.FlatHookSerifless  Marks.Serifless  Div.FlatHookSerifless }
		'diagonalTailedSerifed'      { "DiagonalTailed"  Serifs.Long   XMiddle.FlatHookSerifed    Marks.Serifed    Div.FlatHookSerifed   }

	foreach { suffix { shapeId Serif xMiddleT Marks adws } } [Object.entries JConfig] : do
		local df : DivFrame adws
		local xMiddle : xMiddleT df

		create-glyph "dotlessj.\(suffix)" : glyph-proc
			set-width df.width
			include : df.markSet.p
			include : Body.(shapeId) df XH xMiddle
			include : tagged 'serifLT' : Serif df XH xMiddle
			include : Marks df XH xMiddle

		create-glyph "dotlessjBar.\(suffix)" : glyph-proc
			include [refer-glyph "dotlessj.\(suffix)"] AS_BASE ALSO_METRICS
			include : LetterBarOverlay.m xMiddle (XH / 2) (space -- {0  df.width})

		create-glyph "dotlessjBarHookTop.\(suffix)" : glyph-proc
			include [refer-glyph "dotlessjBar.\(suffix)"] AS_BASE ALSO_METRICS
			include : TopHook.toRight.mBarOuter xMiddle (XH + O) XH

	select-variant 'dotlessj' 0x237

	link-reduced-variant 'dotlessj/sansSerif' 'dotlessj' MathSansSerif
	CreateAccentedComposition 'j'           'j'  'dotlessj'           'tittleAbove'
	CreateAccentedComposition 'j/sansSerif' null 'dotlessj/sansSerif' 'tittleAbove'
	link-reduced-variant 'j/sansSerif' 'j' MathSansSerif

	select-variant 'dotlessjBar'        0x25F (follow -- 'dotlessj')
	select-variant 'dotlessjBarHookTop' 0x284 (follow -- 'dotlessjHookTop')
	CreateAccentedComposition 'jBar'    0x249 'dotlessjBar' 'tittleAbove'

	alias 'grek/yot' 0x3F3 'j'
	alias 'cyrl/je'  0x458 'j'

	create-glyph 'dotlessjCurlyTail.bentHookSerifless' : glyph-proc
		include : MarkSet.p
		set-base-anchor 'above' (Middle + JBalance) XH
		local fine : AdviceStroke 3.5
		local rinner : LongJut / 2 - fine / 2
		local m1 : Middle + [HSwToV HalfStroke] + JBalance
		include : dispiro
			widths.rhs
			flat m1 XH [heading Downward]
			curl m1 (Descender + fine + rinner * 2)
			CurlyTail.n fine Descender (m1 - LongJut - [HSwToV Stroke])
				x2 -- (RightSB + [HSwToV : 0.5 * fine])
				y2 -- (Descender + 0.5 * fine)

	create-glyph 'dotlessjCurlyTail.bentHookSerifed' : glyph-proc
		include [refer-glyph 'dotlessjCurlyTail.bentHookSerifless'] AS_BASE ALSO_METRICS
		include : HSerif.lt (Middle + JBalance) XH LongJut

	select-variant 'dotlessjCurlyTail'
	CreateAccentedComposition 'jCurlyTail' 0x29D 'dotlessjCurlyTail' 'tittleAbove'

	CreateAccentedComposition 'jAcute' null 'dotlessj' 'acuteAbove'

	create-glyph 'mathbb/dotlessj' : glyph-proc
		include : MarkSet.p
		local center : Middle + JBalance + BBD / 2
		set-base-anchor 'above'   (center - [HSwToV BBS] / 2 - BBD / 2)  XH
		set-base-anchor 'overlay' (center - [HSwToV BBS] / 2 - BBD / 2) (XH / 2)
		local hookx : center - (Width * 0.5) - [HSwToV BBD] + OXHook
		local turn : arch.adjust-x.bot [mix center hookx 0.5]
		include : dispiro
			widths.rhs BBS
			flat center XH [heading Downward]
			curl center (Descender + ArchDepthA)
			hookend Descender (sw -- BBS)
			g4 hookx (Descender + JHook)
		include : HBar.t (center - [HSwToV BBS] / 2 - BBD - Jut) center XH BBS
		include : intersection
			VBar.r (center - BBD) Descender XH BBS
			spiro-outline
				corner center XH [heading Downward]
				curl center (Descender + ArchDepthA)
				hookend Descender (sw -- BBS)
				g4 hookx (Descender + JHook)
				corner hookx XH

	create-glyph 'mathbb/j' 0x1D55B : glyph-proc
		include [refer-glyph 'mathbb/dotlessj'] AS_BASE ALSO_METRICS
		include [refer-glyph 'mathbb/dotAbove']
		Dotless.set currentGlyph 'mathbb/dotlessj'
