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

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

glyph-module

glyph-block Digits-Six : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Digits-Shared : CodeLnum CodeOnum

	glyph-block-export OpenContourSixShape
	define [OpenContourSixShape charTop] : glyph-proc
		local oTop : charTop * 0.55 + HalfStroke
		local ymiddlea : (oTop - SmallArchDepthA + SmallArchDepthB) / 2
		local ymiddleb : (oTop - SmallArchDepthB + SmallArchDepthA) / 2
		include : dispiro
			widths.rhs (ShoulderFine - TINY)
			g4 (SB + OX + [HSwToV (Stroke - ShoulderFine)]) ymiddlea
			arch.rhs oTop (swBefore -- (ShoulderFine - TINY))
			g4.down.mid (RightSB - OX) ymiddleb
			arch.rhs 0
			g4.up.mid (SB + OX) ymiddlea
			quadControls 0 0.85
			g4 ([mix SB RightSB 0.85] - [HSwToV QuarterStroke]) (charTop - O) [widths.rhs]

	glyph-block-export ClosedContourSixShape
	define [ClosedContourSixShape top] : glyph-proc
		local oTop : Math.max (2 * SmallArchDepth + TINY) (top * 0.55 + HalfStroke)
		include : dispiro
			widths.rhs ShoulderFine
			straight.up.start (SB + OX + [HSwToV (Stroke - ShoulderFine)]) (oTop - SmallArchDepthA)
			arch.rhs oTop (swBefore -- ShoulderFine)
			flat (RightSB - OX) (oTop - SmallArchDepthB)
			curl (RightSB - OX) SmallArchDepthA
			arch.rhs 0
			flat (SB + OX) SmallArchDepthB
			curl (SB + OX) (top - SmallArchDepthA)
			hookend top
			g4 (RightSB - OX) (top - Hook)

	glyph-block-export StraightBarSixShape
	define [StraightBarSixShape] : with-params [charTop [sw Stroke] [bbd 0]] : glyph-proc
		local oTop : charTop * 0.55 + sw / 2
		local ymiddlea : (oTop - SmallArchDepthA + SmallArchDepthB) / 2
		local ymiddleb : (oTop - SmallArchDepthB + SmallArchDepthA) / 2

		local xMockBarStart   0
		local yMockBarStart : ymiddlea + sw * 0.3
		local xTerminal0 : [mix SB RightSB 0.6] - [HSwToV : 0.5 * sw]
		local kDiagBbd : DiagCorDs (charTop - yMockBarStart) (xTerminal0 - xMockBarStart) bbd
		local xTerminal : xTerminal0 + (kDiagBbd * bbd / 2)
		local pStraightBarStart : 0.75 - (sw / charTop)
		include : intersection
			Rect charTop (-charTop) (-Width) (2 * Width)
			dispiro
				widths.rhs (ShoulderFine - TINY)
				g4.up.mid (SB + bbd + OX + [HSwToV (sw - ShoulderFine)]) ymiddlea
				arch.rhs oTop (sw -- sw) (swBefore -- (ShoulderFine - TINY))
				g4.down.mid (RightSB - OX) ymiddleb
				arch.rhs 0 (sw -- sw)
				g4.up.mid (SB + bbd + OX) ymiddlea
				flat [mix (xMockBarStart + bbd) xTerminal pStraightBarStart] [mix yMockBarStart charTop pStraightBarStart] [widths.rhs sw]
				curl [mix (xMockBarStart + bbd) xTerminal 2]                 [mix yMockBarStart charTop 2]

		if bbd : include : HBar.t (xTerminal - kDiagBbd * bbd) xTerminal charTop sw
		if bbd : include : intersection
			Rect charTop (-charTop) (-Width) (2 * Width)
			dispiro
				arch.rhs.centerAt.rtl.b (Middle + bbd / 2) 0 (sw -- sw)
				archv
				g4.up.mid (SB + OX) ymiddlea
				flat [mix xMockBarStart (xTerminal - kDiagBbd * bbd) pStraightBarStart] [mix yMockBarStart charTop pStraightBarStart]
				curl [mix xMockBarStart (xTerminal - kDiagBbd * bbd) 2]                 [mix yMockBarStart charTop 2]

	create-glyph 'six.lnum.openContour' : glyph-proc
		include : MarkSet.capital
		include : OpenContourSixShape CAP

	create-glyph 'six.lnum.closedContour' : glyph-proc
		include : MarkSet.capital
		include : ClosedContourSixShape CAP

	create-glyph 'six.lnum.straightBar' : glyph-proc
		include : MarkSet.capital
		include : StraightBarSixShape CAP

	select-variant 'six.lnum' [CodeLnum '6'] (follow -- 'six')
	alias          'six.onum' [CodeOnum '6'] 'six.lnum'

	glyph-block-import Letter-Blackboard : BBS BBD
	create-glyph 'mathbb/six' 0x1D7DE : glyph-proc
		include : MarkSet.capital
		include : StraightBarSixShape CAP (sw -- BBS) (bbd -- BBD)
