$$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-F : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Adjustment : LeaningAnchor
	glyph-block-import Letter-Shared : CreateTurnedLetter
	glyph-block-import Letter-Shared-Shapes : LetterBarOverlay PalatalHook

	glyph-block-export xMidBarShrink
	define [xMidBarShrink serifV] : [Math.max HalfStroke ((RightSB - SB) * 0.15)] + [if serifV QuarterStroke 0]

	define [yMidBarImpl y] : fallback y DesignParameters.upperEBarPos

	glyph-block-export yMidBar
	define [yMidBar top y] : top * [yMidBarImpl y]

	glyph-block-export EFVJutLength
	define [EFVJutLength top pyBar stroke] : begin
		local jutTop : Math.min VJut
			top - [mix (top - stroke) ([yMidBar top pyBar] + stroke / 2) 0.5]
		local jutBot : Math.min VJut
			mix stroke ([yMidBar top pyBar] - stroke / 2) 0.5
		local jutMid : 0.5 * [mix jutBot jutTop : yMidBarImpl pyBar]
		return { jutTop jutBot jutMid }

	define xFBarLeft : SB * 1.5
	define xFBarRight : Width - SB * 1.5

	define [FShape] : 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 (xFBarLeft) 0 top stroke
		include : HBar.t (xFBarLeft - O) RightSB top stroke
		include : tagged 'crossBar' : HBar.m (xFBarLeft - O) xMidRight yBar stroke

		if serifLT : include : HSerif.lt xFBarLeft top SideJut
		if serifLB : begin
			include : tagged 'serifBottom' : HSerif.lb xFBarLeft 0 SideJut
			include : tagged 'serifBottom' : HSerif.rb (xFBarLeft + [HSwToV HalfStroke]) 0 MidJutSide
		if serifV : include : VSerif.dr RightSB top jutTop
		if serifM : begin
			local fine : VJutStroke * stroke / Stroke
			include : VBar.r xMidRight (yBar - jutMid) (yBar + jutMid) fine

	define FConfig : 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 FConfig] : do
		create-glyph "F.\(suffix)" : glyph-proc
			include : MarkSet.capital
			include : LeaningAnchor.Below.VBar.l xFBarLeft [AdviceStroke2 2 3 CAP]
			include : FShape CAP (serifLT -- lt) (serifLB -- lb) (serifV -- v) (serifM -- m)

		if [not m] : create-glyph "currency/frenchFrancSign.\(suffix)" : glyph-proc
			include [refer-glyph "F.\(suffix)"] AS_BASE ALSO_METRICS

			eject-contour 'crossBar'
			include : LetterBarOverlay.l.in
				x   -- xFBarLeft
				bot -- 0
				top -- CAP
				py  -- 0.375
			include : LetterBarOverlay.l.in
				x   -- xFBarLeft
				bot -- 0
				top -- (CAP - Stroke)
				py  -- 0.625

		create-glyph "FBar.\(suffix)" : glyph-proc
			include [refer-glyph "F.\(suffix)"] AS_BASE ALSO_METRICS
			include : LetterBarOverlay.l.in
				x   -- xFBarLeft
				bot -- [yMidBar CAP] + HalfStroke
				top -- CAP - Stroke
				space -- { 0 (RightSB - [if v [HSwToV VJutStroke] 0]) }

		create-glyph "smcpF.\(suffix)" : glyph-proc
			include : MarkSet.e
			include : LeaningAnchor.Below.VBar.l xFBarLeft [AdviceStroke2 2 3 XH]
			include : FShape XH (serifLT -- lt) (serifLB -- lb) (serifV -- v) (serifM -- m)

	select-variant 'F' 'F'
	link-reduced-variant 'F/sansSerif' 'F' MathSansSerif
	select-variant 'smcpF' 0xA730 (follow -- 'F')
	select-variant 'FBar' 0xA798 (follow -- 'F')
	select-variant 'currency/frenchFrancSign' 0x20A3

	alias 'cyrl/Ghayn.BSH' null 'F'
	alias 'cyrl/ghayn.BSH' null 'smcpF'

	glyph-block-import Letter-Blackboard : BBS BBD
	create-glyph 'mathbb/F' 0x1D53D : glyph-proc
		include : MarkSet.capital
		include : union
			VBar.l (xFBarLeft + BBD) 0 CAP BBS
			HBar.b xFBarLeft (xFBarLeft + BBD) 0 BBS
			difference
				FShape CAP (stroke -- BBS)
				Rect (CAP - BBS) BBS (xFBarLeft + [HSwToV BBS]) (xFBarLeft + BBD)

	CreateTurnedLetter 'turnF'     0x2132 'F'     HalfAdvance (CAP / 2)
	CreateTurnedLetter 'turnSmcpF' 0x214E 'smcpF' HalfAdvance (XH  / 2)

	derive-glyphs 'Fltail' 0x191 'F' : lambda [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		eject-contour 'serifBottom'
		include : PalatalHook.lExt xFBarLeft 0
