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

import [mix fallback SuffixCfg] from "@iosevka/util"
import [MathSansSerif] from "@iosevka/glyph/relation"

glyph-module

glyph-block Letter-Latin-Lower-P : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Mark-Adjustment : ExtendAboveBaseAnchors LeaningAnchor
	glyph-block-import Letter-Shared-Shapes : OBarLeft LetterBarOverlay
	glyph-block-import Letter-Shared-Shapes : TopHook PalatalHook SerifFrame


	define [SmallThornEaredBody] : glyph-proc
		include : tagged 'bowl' : OBarLeft.shape
		include : tagged 'stemLeft' : VBar.l SB Descender Ascender

	define [EaredBody] : with-params [[left SB] [right RightSB] [sw Stroke]] : glyph-proc
		include : tagged 'bowl' : OBarLeft.shape
			left  -- left
			right -- right
			sw    -- sw
			fine  -- (ShoulderFine * sw / Stroke)
		include : tagged 'stemLeft' : VBar.l left Descender XH sw

	define [EarlessCornerBody] : with-params [[left SB] [right RightSB] [sw Stroke]] : glyph-proc
		include : tagged 'bowl' : OBarLeft.toothlessTop (rise -- DToothlessRise) (mBlend -- DMBlend)
			left  -- left
			right -- right
			sw    -- sw
			fine  -- (ShoulderFine * sw / Stroke)
		include : tagged 'stemLeft' : VBar.l left Descender (XH - DToothlessRise) sw

	define [EarlessRoundedBody] : with-params [[left SB] [right RightSB] [sw Stroke]] : glyph-proc
		include : tagged 'bowl' : OBarLeft.roundedTop (yTerminal -- Descender)
			left  -- left
			right -- right
			sw    -- sw
			fine  -- (ShoulderFine * sw / Stroke)

	define [SmallPSerifLT df top _sw] : return [SerifFrame.fromDf df top Descender (swRef -- _sw)].lt.outer
	define [SmallPSerifLB df top _sw] : return [SerifFrame.fromDf df top Descender (swRef -- _sw)].lb.fullSide

	define [FullSerifs  df top _sw] : composite-proc [SmallPSerifLT df top _sw] [SmallPSerifLB df top _sw]
	define [MotionSerif df top _sw] : SmallPSerifLT df top _sw
	define [BottomSerif df top _sw] : SmallPSerifLB df top _sw

	glyph-block-export : PConfig
	define PConfig : SuffixCfg.weave
		object # body
			eared                EaredBody
			earlessCorner        EarlessCornerBody
			earlessRounded       EarlessRoundedBody
		object # serifs
			serifless           { no-shape     false false }
			motionSerifed       { MotionSerif  true  false }
			bottomSerifed       { BottomSerif  false true  }
			serifed             { FullSerifs   true  true  }

	foreach { suffix { Body {Serifs doTS doBS} }} [Object.entries PConfig] : do
		local yOverlay : mix 0 (Descender + [if doBS Stroke 0]) 0.5

		create-glyph "p.\(suffix)" : glyph-proc
			include : MarkSet.p
			include : Body
			include : Serifs [DivFrame 1] XH
			include : LeaningAnchor.Below.VBar.l SB
			set-base-anchor 'overlayOnExtension' (SB + [HSwToV HalfStroke]) yOverlay
			set-base-anchor 'strike' Middle (XH / 2)

		create-glyph "pStrokeBottom.\(suffix)" : glyph-proc
			include [refer-glyph "p.\(suffix)"] AS_BASE ALSO_METRICS
			include : LetterBarOverlay.l.in
				x   -- SB
				bot -- (Descender + [if doBS Stroke 0])
				top -- 0

		create-glyph "thorn.\(suffix)" : glyph-proc
			include : MarkSet.bp
			include : SmallThornEaredBody
			include : Serifs [DivFrame 1] Ascender
			include : LeaningAnchor.Above.VBar.l SB
			include : LeaningAnchor.Below.VBar.l SB
			set-base-anchor 'overlayOnExtension' (SB + [HSwToV HalfStroke]) yOverlay
			set-base-anchor 'strike' Middle (XH / 2)

		create-glyph "thornStroke.\(suffix)" : glyph-proc
			include [refer-glyph "thorn.\(suffix)"] AS_BASE ALSO_METRICS
			include : LetterBarOverlay.l.in SB XH (Ascender - [if doTS Stroke 0])

		create-glyph "thornStrokeBottom.\(suffix)" : glyph-proc
			include [refer-glyph "thorn.\(suffix)"] AS_BASE ALSO_METRICS
			include : LetterBarOverlay.l.in
				x   -- SB
				bot -- (Descender + [if doBS Stroke 0])
				top -- 0

		create-glyph "pPalatalHook.\(suffix)" : glyph-proc
			include [refer-glyph "p.\(suffix)"] AS_BASE ALSO_METRICS

			local tinyGap : Math.max (Width / 32) : AdviceStroke 24
			local xMask : Math.max
				SB + [HSwToV Stroke] + 2 * tinyGap
				SB + [HSwToV HalfStroke] + MidJutSide + tinyGap

			include : PalatalHook.r
				x -- [mix [arch.adjust-x.bot Middle] RightSB 0.75]
				y -- 0
				yAttach -- [mix O SmallArchDepthA : archv.yFromX 0.75]
				maskOut -- [intersection [MaskBelow 0] [MaskLeft xMask]]

	select-variant 'p' 'p'
	link-reduced-variant 'p/sansSerif' 'p' MathSansSerif
	link-reduced-variant 'p/hookTopBase' 'p'
	select-variant 'cyrl/er' 0x440 (shapeFrom -- 'p')
	select-variant 'pPalatalHook' 0x1D88 (follow -- 'p')

	alias 'grek/rho' 0x3C1 'p.earlessRoundedSerifless'

	select-variant 'pStrokeBottom' 0xA751 (follow -- 'p')

	alias 'grek/rhoStroke' 0x3FC 'pStrokeBottom.earlessRoundedSerifless'

	select-variant 'thorn' 0xFE
	alias 'grek/sho' 0x3F8 'thorn.earedSerifless'

	select-variant 'thornStroke' 0xA765 (follow -- 'thorn')
	select-variant 'thornStrokeBottom' 0xA767 (follow -- 'thorn')

	derive-glyphs 'cyrl/erTick' 0x48F 'cyrl/er' : lambda [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		include : dispiro
			widths.center [AdviceStroke 4]
			flat [mix SB RightSB 0.5]   (HalfStroke + XH * 0.25)
			curl [mix SB RightSB 0.875] (HalfStroke - XH * 0.25)

	derive-glyphs 'pHookTop' 0x1A5 "p/hookTopBase" : function [src gr] : glyph-proc
		include [refer-glyph src] AS_BASE ALSO_METRICS
		eject-contour 'serifLT'
		eject-contour 'stemLeft'
		include : TopHook.toRight.lBarOuter SB Descender XH
		include : LeaningAnchor.Above.VBar.l SB

	glyph-block-import Letter-Blackboard : BBS BBD BBBarLeft
	create-glyph 'mathbb/p' 0x1D561 : glyph-proc
		include : MarkSet.p
		include : BBBarLeft SB Descender XH
		include : intersection
			OShapeOutline.NoOvershoot XH 0 (SB + BBD + [HSwToV BBS]) RightSB
			VBar.r (RightSB - BBD - OX) 0 XH BBS
		include : OBarLeft.shape
			left -- (SB + BBD)
			sw   -- BBS
