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

import [mix fallback] from "@iosevka/util"

glyph-module

glyph-block LetterLike-Fraktur-Upper-MNP : begin
	glyph-block-import Common-Derivatives
	glyph-block-import CommonShapes
	glyph-block-import LetterLike-Fraktur-Common : UpperDf S M F T fraktur-stroke change-pen
	glyph-block-import LetterLike-Fraktur-Common : DecoSizeX DecoSizeY SlopeA SlopeB CutMaskLeft
	glyph-block-import LetterLike-Fraktur-Common : Wave FHook PHexTop PHexBot

	define [deriveBoxes box] : begin
		local body : box.padLeft (+1.50 * DecoSizeX)
		local deco : box.padLeft (-0.25 * DecoSizeX)
		return { body deco }

	define [LeftStroke box profile] : glyph-proc
		local { body deco } : deriveBoxes box

		local [knots] : lift-@ : list
			g2.ru.start @xLD                          ([deco.yp 0.625] - 2 * F.thick) [change-pen F]
			~~~ [Wave.vc Wave.LTDecoSize]
			g2.ru.mid  (@xLD = deco.left)             (@yT - DecoSizeY) [change-pen profile]
			arch.rhs (sw -- profile.thick) (blendPre -- null) (@yT = deco.top) 0.4
			flat       (@xLB = body.left)             (@yT - ArchDepthA)
			curl        @xLB                          (@yB + ArchDepthB)
			corner     (@xB = @xLD + 0.5 * DecoSizeX) (@yB = deco.bot)
			corner     (@xB - DecoSizeX)              (@yB + DecoSizeY)

		local s : include : fraktur-stroke profile [knots]

		return : object
			maskLeft : function [] : fraktur-stroke CutMaskLeft [knots]


	create-glyph 'frak/M' 0x1D510 : glyph-proc
		local df : include : UpperDf para.advanceScaleM 3
		include : df.markSet.capital

		local box : M.box CAP 0 df.leftSB df.rightSB
		local { body deco } : deriveBoxes box

		local leftStroke : include : LeftStroke box M

		local rightStroke : include : lift-@ : fraktur-stroke M
			corner (@xR + DecoSizeX)                       (@yR + DecoSizeY)
			corner (@xR = body.right + 0.25 * DecoSizeX)   (@yR = body.bottom)
			~~~ [straight.up.mid (@xR - Wave.DepthX)       [mix @yR @yRT 0.5]]
			corner (@xRT = @xR + 0.25 * DecoSizeX)         (@yRT = [body.yp 0.75] + S.thick)
			~~~ [curl (@xRT - TINY)                        (@yRT - TINY * SlopeB)]
			g2     (@xRT - 0.75 * Wave.DepthX)             [mix @yRT @yT 0.5]
			arch.rhs (sw -- M.thick) (blendPre -- {})      (@yT = body.top)
			corner (body.xMid - 0.25 * Wave.DepthX)        (@yMT = @yT - 0.5 * Wave.DepthY)
			~~~ [arch.rhs (sw -- M.thick) (blendPre -- {}) (blendPost -- {}) @yT]
			corner  body.left                               @yMT

		local middleStroke : include : lift-@ : fraktur-stroke M
			corner (@xMB - DecoSizeX)                      (@yB + DecoSizeY)
			corner (@xMB = deco.xMid)                      (@yB = deco.bot)
			flat    @xMT                                   [mix @yB @yMTS 0.5]
			flat    @xMT                                   (@yMTS = [body.yp 0.75] + S.thick)
			curl   (@xMT = body.xMid - 0.25 * Wave.DepthX) (body.top - 0.5 * Wave.DepthY)

	create-glyph 'frak/N' 0x1D511 : glyph-proc
		local df : include : UpperDf 1
		include : df.markSet.capital

		local box : S.box CAP 0 df.leftSB df.rightSB
		local { body deco } : deriveBoxes box

		local leftStroke : include : LeftStroke box S

		local rightStroke : include : lift-@ : difference
			fraktur-stroke S
				corner (@xR + DecoSizeX)                     (@yR + DecoSizeY)
				corner (@xR = body.right + 0.25 * DecoSizeX) (@yR = body.bottom)
				~~~ [straight.up.mid (@xR - Wave.DepthX)     [mix @yR @yRT 0.5]]
				corner  @xR                                  (@yRT = [body.yp 0.75] + S.thick)
				~~~ [curl (@xR - TINY)                       (@yRT - TINY * SlopeB)]
				g2     (@xR - Wave.DepthX)                   [mix @yRT @yT 0.5]
				arch.rhs (sw -- S.thick) (blendPre -- {})    (@yT = body.top)
				corner (body.left - S.thick)                 (@yT - ArchDepthA)
			leftStroke.maskLeft
