$$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-L : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Shared-Metrics : markMiddle
	glyph-block-import Mark-Above : aboveMarkMid
	glyph-block-import Mark-Adjustment : LeaningAnchor
	glyph-block-import Letter-Blackboard : BBS BBD
	glyph-block-import Letter-Shared : CreateTurnedLetter
	glyph-block-import Letter-Shared-Shapes : BeltOverlay LetterBarOverlay

	define [LBarLeftX df] : df.leftSB * 1.5
	define [LRightX   df] : df.rightSB - 0.75 * OX
	define [LShape df top sgr swv] : glyph-proc
		include : VBar.l [LBarLeftX df] 0 top swv
		include : HBar.b ([LBarLeftX df] - O) [LRightX df] 0
		if (sgr > 1) : begin
			include : HSerif.lb ([LBarLeftX df] + [HSwToV : 0.5 * swv]) 0   Jut
			include : HSerif.lt ([LBarLeftX df] + [HSwToV : 0.5 * swv]) top Jut
			include : HSerif.rt ([LBarLeftX df] + [HSwToV : 0.5 * swv]) top MidJutSide
		if (sgr > 0) : include : VSerif.ur [LRightX df] 0 VJut

	define LConfig : object
		serifless     { 0 }
		motionSerifed { 1 }
		serifed       { 2 }

	foreach { suffix { serifGrade } } [Object.entries LConfig] : do
		local df : DivFrame 1
		create-glyph "L.\(suffix)" : glyph-proc
			set-width df.width
			include : df.markSet.capital
			include : LeaningAnchor.Above.VBar.l [LBarLeftX df]
			include : LShape df CAP serifGrade Stroke
			set-base-anchor 'cvDecompose'
				[mix df.leftSB df.rightSB 0.65] + [HSwToV QuarterStroke]
				mix Stroke CAP 0.5

		create-glyph "smcpL.\(suffix)" : glyph-proc
			set-width df.width
			include : df.markSet.e
			include : LeaningAnchor.Above.VBar.l [LBarLeftX df]
			include : LShape df XH serifGrade Stroke

		create-glyph "LLWelsh.\(suffix)" : glyph-proc
			set-width df.width
			include : df.markSet.capital

			local sw : AdviceStroke 4
			local gap : Math.max (Width / 8) [AdviceStroke 8]

			include : LShape df CAP serifGrade sw
			include : intersection
				MaskLeft RightSB
				MaskAbove sw
				with-transform [ApparentTranslate ([HSwToV sw] + gap) 0]
					LShape df CAP serifGrade sw

		create-glyph "LHighBar.\(suffix)" : glyph-proc
			include [refer-glyph "L.\(suffix)"] AS_BASE ALSO_METRICS
			include : LetterBarOverlay.l.in [LBarLeftX : DivFrame 1] XH (CAP - [if (serifGrade > 1) Stroke 0])

	select-variant 'L' 'L'
	link-reduced-variant 'L/sansSerif' 'L' MathSansSerif
	select-variant 'smcpL' 0x29F (follow -- 'L')
	select-variant 'LLWelsh' 0x1EFA (follow -- 'L')
	select-variant 'LHighBar' 0xA748 (follow -- 'L')

	CreateTurnedLetter 'turnL' 0xA780 'L' HalfAdvance (CAP / 2)
	CreateTurnedLetter 'turnL/sansSerif' 0x2142 'L/sansSerif' HalfAdvance (CAP / 2)

	create-glyph 'mathbb/L' 0x1D543 : glyph-proc
		local df : include : DivFrame 1
		include : df.markSet.capital
		include : VBar.l [LBarLeftX df] 0 CAP BBS
		include : HBar.b ([LBarLeftX df] - O) [LRightX df] 0 BBS
		include : VBar.l ([LBarLeftX df] + BBD) 0 CAP BBS
		include : HBar.t [LBarLeftX df] ([LBarLeftX df] + BBD) CAP BBS

	derive-multi-part-glyphs 'Lcaron' 0x13D { 'L' 'commaAbove' } : lambda [srcs gr] : glyph-proc
		local { base mark } srcs
		include : with-transform [Translate (Width + (RightSB - SB) * 0.375) 0]
			refer-glyph mark
		include [refer-glyph base] AS_BASE ALSO_METRICS

	define [LSlashOverlay top] : begin
		local fine : 0.5 * OverlayStroke
		local middle : mix Stroke top 0.5
		return : dispiro
			flat [mix 0 SB 0.5] (middle - LongJut * 0.4) [widths.center : 2 * fine]
			curl (1.6 * LongJut + [mix 0 SB 0.5]) (middle + LongJut * 0.4)

	derive-glyphs 'LSlash' 0x141 'L' : lambda [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		include : LSlashOverlay CAP

	derive-glyphs 'smcpLSlash' 0x1D0C 'smcpL' : lambda [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		include : LSlashOverlay XH

	derive-glyphs 'LTildeOver' 0x2C62 'L' : lambda [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		include : new-glyph : glyph-proc
			include : refer-glyph "tildeOver"
			include : ApparentTranslate ([LBarLeftX : DivFrame 1] + [HSwToV Stroke] / 2 - markMiddle) (CAP * 0.525 - XH / 2)

	create-glyph 'LBarOverlay'
		LetterBarOverlay.l.in [LBarLeftX : DivFrame 1] 0 CAP
	create-glyph 'LDoubleBarOverlay' : composite-proc
		LetterBarOverlay.l.in [LBarLeftX : DivFrame 1] 0 CAP (py -- 0.6)
		LetterBarOverlay.l.in [LBarLeftX : DivFrame 1] 0 CAP (py -- 0.4)

	create-glyph 'LBeltOverlay'
		BeltOverlay CAP ([LBarLeftX : DivFrame 1] + [HSwToV HalfStroke])
	create-glyph 'smcpLBeltOverlay'
		BeltOverlay XH  ([LBarLeftX : DivFrame 1] + [HSwToV HalfStroke])

	derive-composites 'LBar' 0x23D 'L' 'LBarOverlay'
	derive-composites 'LDoubleBar' 0x2C60 'L' 'LDoubleBarOverlay'
	derive-composites 'LBelt' 0xA7AD 'L' 'LBeltOverlay'
	derive-composites 'smcpLBelt' 0x1DF04 'smcpL' 'smcpLBeltOverlay'
	derive-composites 'LDot' 0x13F 'L' 'LDotDot'
