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

import [mix linreg clamp fallback] from '../support/utils'
import [designParameters] from '../meta/aesthetics'

###### NUMBERS
export : define [apply] : begin
	glyph-module-entry commonShapes

	sketch # zero.unslashed
		include markset.capital
		include : OShape CAP 0 SB RIGHTSB
		save 'zero.unslashed'

	sketch # zero.slashed
		include markset.capital
		include glyphs.'zero.unslashed'
		local fine : 0.5 * OVERLAYSTROKE
		include : dispiro
			flat (SB + STROKE / 2) (CAP * (1 - 0.77)) [widths fine fine]
			curl (RIGHTSB - STROKE / 2) (CAP * 0.77)
		save 'zero.slashed'

	sketch # zero.dotted
		include markset.capital
		include glyphs.'zero.unslashed'
		local radius : Math.min DOTRADIUS ((RIGHTSB - SB - STROKE * 2) / 4)
		include : Ring (CAPMIDDLE + radius) (CAPMIDDLE - radius) (MIDDLE + radius) (MIDDLE - radius)
		save 'zero.dotted'

	select-variant 'zero' '0' 'slashed' {
		.cv13 'zero.slashed'
		.cv14 'zero.dotted'
		.cv15 'zero.unslashed'
	}

	sketch # one
		include markset.capital
		include : VBar (MIDDLE + ONEBALANCE) 0 CAP
		include : dispiro
			flat (MIDDLE - HALFSTROKE * HVCONTRAST + ONEBALANCE) CAP [widths STROKE 0]
			curl (MIDDLE - HOOKX * 1.25 + ONEBALANCE) (CAP - HOOK * 0.75)

		if SLAB : begin
			include : CenterBottomSerif (MIDDLE + ONEBALANCE) 0 LONGJUT
		save 'one' '1'

	sketch # two
		include markset.capital
		define smooth : [adviceGlottalStopSmooth CAP 1] * 0.95
		include : dispiro
			widths.rhs
			g4 SB (CAP - HOOK)
			hookstart (CAP - O)
			g4 (RIGHTSB - OX / 2) (CAP - smooth)
			alsothru 0.5 ([linreg 18 0.425 120 0.4 STROKE] * [linreg 500 1 700 0.95 WIDTH]) [widths.center]
			flat SB 1 [widths.heading STROKE 0 DOWNWARD]
			curl SB 0 [heading DOWNWARD]

		include : HBarBottom  (SB + HALFSTROKE) RIGHTSB 0
		if SLAB : begin
			include : UpwardRightSerif RIGHTSB 0 VJUT
		save 'two' '2'

	sketch # three
		include markset.capital
		local barcenter : CAP * 0.53
		local threeRadius : (CAPMIDDLE + HALFSTROKE - SMOOTH) * 1.25
		local fine : STROKE * CTHIN

		include : dispiro
			widths.rhs
			g4 (SB - O) (CAP - HOOK)
			hookstart CAPO
			g4 (RIGHTSB) (CAP - (SMOOTHB * (CAP - barcenter) / CAPMIDDLE))
			flat (RIGHTSB - threeRadius + 0.01) (barcenter - (fine - HALFSTROKE)) [widths.heading 0 fine LEFTWARD]
			curl (RIGHTSB - threeRadius) (barcenter - (fine - HALFSTROKE)) [heading LEFTWARD]
		include : dispiro
			widths.lhs
			g4 (SB + O) HOOK
			hookstart O
			g4 (RIGHTSB - O * 2) (SMOOTHA * barcenter / CAPMIDDLE)
			flat (RIGHTSB - threeRadius + 0.01) (barcenter + (fine - HALFSTROKE)) [widths.heading fine 0 LEFTWARD]
			curl (RIGHTSB - threeRadius) (barcenter + (fine - HALFSTROKE)) [heading LEFTWARD]

		save 'three' '3'

	sketch # four
		include markset.capital
		local bar (CAP * 0.4)
		local vert : [mix SB RIGHTSB 0.825] - HVCONTRAST * STROKE
		local p 0.25
		local fine (STROKE * 0.9)

		include : HBarTop (SB + OX) RIGHTSB bar
		include : VBarLeft vert 0 CAP
		local slope : (CAP - bar) / (vert - fine * p * HVCONTRAST - (SB + OX))
		include : dispiro
			g4   (SB + OX) bar [widths 0 fine]
			g4   (vert - fine * p * HVCONTRAST) CAP [widths.heading 0 [Math.hypot fine (fine / slope)] UPWARD]
		if SLAB : begin
			include : CenterBottomSerif (vert + HALFSTROKE * HVCONTRAST) 0 JUT
		save 'four' '4'

	define [FiveShape top bp] : glyph-construction
		local t1 : top * bp * 0.8
		local t2 : top * bp
		local ycurly : mix 0 t2 (SMOOTHA / (SMOOTHA + SMOOTHB))
		local xleft : mix SB RIGHTSB 0.025
		local xright : [mix RIGHTSB SB 0.05] - OXE

		include : dispiro
			widths.rhs
			g4 (xleft - OXHOOK) (t2 - AHOOK * top / CAP)
			hookstart (t2 - O)
			g4 (RIGHTSB - OX) ycurly
			hookend O
			g4 SB HOOK
		include : VBarLeft xleft (t2 - AHOOK * top / CAP) top
		include : HBarTop xleft xright top
		if SLAB : begin
			include : DownwardRightSerif xright top VJUT

	sketch # five
		include markset.capital
		include : FiveShape CAP FIVEBARPOS
		save 'five' '5'

	sketch # zhuangToneFive
		include markset.capital
		include : FiveShape CAP FIVEBARPOS
		save 'zhuangToneFive' 0x1BC

	sketch # zhuangtonefive
		include markset.e
		include : FiveShape XH FIVEBARPOS
		save 'zhuangtonefive' 0x1BD

	sketch # six
		include markset.capital
		local top (CAP * 0.6)
		local ymiddlea : (top - SMALLSMOOTHA + SMALLSMOOTHB) / 2
		local ymiddleb : (top - SMALLSMOOTHB + SMALLSMOOTHA) / 2
		include : dispiro
			widths.rhs
			g4.up.start (SB + OX + STROKE * 0.5 * HVCONTRAST) ymiddlea [widths.heading 0 (STROKE * 0.5) UPWARD]
			arcvh
			g4 (MIDDLE - CORRECTION_OMIDS) (top - O) [widths.rhs]
			archv
			g4.down.mid (RIGHTSB - OX) ymiddleb
			arcvh
			g4 (MIDDLE + CORRECTION_OMIDS) (O)
			archv
			g4.up.mid (SB + OX) ymiddlea
			quadcontrols 0 0.8
			g4 ([mix SB RIGHTSB 0.85] - HALFSTROKE * HVCONTRAST) CAP
		save 'six' '6'

	sketch # seven
		include markset.capital
		include : HBarTop (SB + OXE) RIGHTSB CAP
		local x : mix SB RIGHTSB 0.28
		local cor : [Math.hypot 1 ((RIGHTSB - x - HALFSTROKE) / (CAP - STROKE))] * HVCONTRAST
		start-from (x - STROKE * cor / 2) 0
		line-to (x + STROKE * cor / 2) 0
		line-to RIGHTSB (CAP - STROKE)
		line-to (RIGHTSB - STROKE * cor) (CAP - STROKE)
		reverse-last
		if SLAB : begin
			include : DownwardLeftSerif (SB + OXE) CAP VJUT
		save 'seven' '7'

	local pr : linreg 18 0.85 120 0.975 STROKE

	sketch # eight
		include markset.capital
		local p 0.96
		local l (SB + OX)
		local r (RIGHTSB - OX)

		include : dispiro
			widths.rhs
			g4.right.mid (MIDDLE - CORRECTION_OMIDS) (CAP - O)
			archv
			g4 [mix l r p] (CAP - SMOOTHB * pr * p)
			alsothru 0.5 0.5 [widths.center]
			g4 (l) (SMOOTHB * pr) [widths.lhs]
			arcvh
			g4.right.mid (MIDDLE + CORRECTION_OMIDS) (O)
			archv
			g4 (r) (SMOOTHA * pr) [widths.lhs]
			alsothru 0.5 0.5 [widths.center]
			g4 [mix r l p] (CAP - SMOOTHA * pr * p) [widths.rhs]
			arcvh
			close
		save 'eight' '8'

	# There is an "eight without lower contour" shape used for /propto
	sketch # rotetedpropto
		local p 0.96
		local py 0.4
		local l (SB + OX)
		local r (RIGHTSB - OX)
		include : dispiro
			widths.lhs
			straight.up.start r 0 [heading UPWARD]
			alsothru 0.5 (1 - py) [widths (STROKE * py) (STROKE * (1 - py))]
			g4 [mix r l p] (CAP - SMOOTHA * p * pr) [widths.rhs]
			arcvh
			g4 (MIDDLE - CORRECTION_OMIDS) (CAP - O)
			archv
			g4 [mix l r p] (CAP - SMOOTHB * p * pr)
			alsothru 0.5 py [widths (STROKE * py) (STROKE * (1 - py))]
			straight.down.end l 0 [widths.heading STROKE 0 DOWNWARD]
		save 'rotetedpropto'

	sketch # nine
		include markset.capital
		set-width WIDTH
		local u CAP
		local d : Math.min (u - SMALLSMOOTHA - SMALLSMOOTHB - 0.1) (CAP * 0.425 - HALFSTROKE)
		local l (SB + OX)
		local r (RIGHTSB - OX)
		local ymiddlea : mix u d 0.5
		include : dispiro
			widths.rhs (STROKE * CTHINB)
			flat (r - STROKE * (1 - CTHINB) * HVCONTRAST) ymiddlea
			curl (r - STROKE * (1 - CTHINB) * HVCONTRAST) (d + SMALLSMOOTHA)
			arcvh
			g4   ([mix l r 0.5] + CORRECTION_OMIDS) (d + O) [widths 0 STROKE]
			archv
			flat l (d + SMALLSMOOTHB)
			curl l (u - SMALLSMOOTHA)
			arcvh
			g4   ([mix l r 0.5] - CORRECTION_OMIDS) (u - O)
			archv
			flat r ymiddlea
			curl r (CAP * 0.35)
			hookend O
			g4 SB HOOK
		save 'nine' '9'

	sketch # ten
		include markset.capital
		set-width : WIDTH * 2
		include glyphs.one
		apply-transform : Translate (-WIDTH) 0
		include glyphs.zero
		apply-transform : Translate WIDTH 0
		save 'ten'
