### Punctuation symbols
$$include '../../meta/macros.ptl'

import [mix linreg clamp fallback] from "@iosevka/util"
import [Box] from "@iosevka/geometry/box"
import [DependentSelector] from "@iosevka/glyph/relation"

glyph-module

glyph-block Symbol-Punctuation-Quotes-And-Primes : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Symbol-Punctuation-Small
	glyph-block-import Letter-Shared : CreateDependentComposite

	local quoteTop (CAP * 1.05)
	local quoteBottom (XH * 0.85)

	local yCurlyQuotes [mix quoteBottom quoteTop 0.5]
	local commaLow (Descender * 0.9)
	local ratio : Math.min 1 : (quoteTop - quoteBottom) / (PeriodSize - commaLow)

	derive-glyphs 'lowSingleQuote' 0x201A 'comma' : function [src gr] : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		include : refer-glyph src
		include : FlipAround df.middle 0 ratio ratio

	derive-glyphs 'revLowSingleQuote' null 'revComma' : function [src gr] : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		include : refer-glyph src
		include : FlipAround df.middle 0 ratio ratio

	derive-glyphs 'openSingleQuote' 0x2018 'lowSingleQuote' : function [src gr] : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		include : refer-glyph src
		include : FlipAround df.middle [mix [mix PeriodSize commaLow 0.5] yCurlyQuotes 0.5]

	derive-glyphs 'closeSingleQuote' 0x2019 'openSingleQuote' : function [src gr] : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		include : refer-glyph src
		include : FlipAround df.middle yCurlyQuotes

	derive-glyphs 'revSingleQuote' 0x201B 'revComma' : function [src gr] : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		include : refer-glyph src
		include : FlipAround df.middle 0 ratio ratio
		include : FlipAround df.middle [mix [mix PeriodSize commaLow 0.5] yCurlyQuotes 0.5]
		include : FlipAround df.middle yCurlyQuotes

	define [DoubleQuoteProc gn] : glyph-proc
		local dfSingle : DivFrame para.advanceScaleF
		local dist : Width * 0.225
		include : refer-glyph gn
		include : Translate (-dist * 2) 0
		include : refer-glyph gn
		include : Translate (dist + Middle - dfSingle.middle) 0

	derive-glyphs 'lowDoubleQuote'    0x201E 'lowSingleQuote'    DoubleQuoteProc
	derive-glyphs 'openDoubleQuote'   0x201C 'openSingleQuote'   DoubleQuoteProc
	derive-glyphs 'closeDoubleQuote'  0x201D 'closeSingleQuote'  DoubleQuoteProc
	derive-glyphs 'revDoubleQuote'    0x201F 'revSingleQuote'    DoubleQuoteProc
	derive-glyphs 'revLowDoubleQuote' 0x2E42 'revLowSingleQuote' DoubleQuoteProc

	alias 'mdfTurncomma'        0x2BB 'openSingleQuote'
	alias 'mdfApostrophe'       0x2BC 'closeSingleQuote'
	alias 'mdfRevComma'         0x2BD 'revSingleQuote'
	alias 'mdfDoubleApostrophe' 0x2EE 'closeDoubleQuote'

	# Primes
	create-glyph 'prime' 0x2032 : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		local wide : Width * 0.1
		include : dispiro
			widths.center (Stroke * 1.2)
			flat (df.middle + wide) quoteTop [heading Downward]
			curl (df.middle - wide) quoteBottom [widths.heading HalfStroke HalfStroke Downward]

	create-glyph 'doubleprime' 0x2033 : glyph-proc
		local dfSingle : DivFrame para.advanceScaleF
		local d : 0.25 * (RightSB - SB)
		include : refer-glyph "prime"
		include : Translate (-d * 2) 0
		include : refer-glyph "prime"
		include : Translate (d + Middle - dfSingle.middle) 0

	create-glyph 'tripleprime' 0x2034 : glyph-proc
		local df : include : DivFrame para.advanceScaleM
		local wide : Width * 0.1
		local d : df.width * 0.3
		local fine : 0.5 * [AdviceStroke 3.5]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle + wide) quoteTop [heading Downward]
			curl (df.middle - wide) quoteBottom [widths.heading fine fine Downward]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle - d + wide) quoteTop [heading Downward]
			curl (df.middle - d - wide) quoteBottom [widths.heading fine fine Downward]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle + d + wide) quoteTop [heading Downward]
			curl (df.middle + d - wide) quoteBottom [widths.heading fine fine Downward]

	create-glyph 'quadPrime' 0x2057 : glyph-proc
		local df : include : DivFrame para.advanceScaleM
		local wide : Width * 0.1
		local fine : 0.5 * [AdviceStroke 4]
		local d : df.width * 0.15 - fine / 6
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle - d + wide) quoteTop [heading Downward]
			curl (df.middle - d - wide) quoteBottom [widths.heading fine fine Downward]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle + d + wide) quoteTop [heading Downward]
			curl (df.middle + d - wide) quoteBottom [widths.heading fine fine Downward]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle - 3 * d + wide) quoteTop [heading Downward]
			curl (df.middle - 3 * d - wide) quoteBottom [widths.heading fine fine Downward]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle + 3 * d + wide) quoteTop [heading Downward]
			curl (df.middle + 3 * d - wide) quoteBottom [widths.heading fine fine Downward]

	create-glyph 'revprime' 0x2035 : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		local wide : -Width * 0.1
		include : dispiro
			widths.center (Stroke * 1.2)
			flat (df.middle + wide) quoteTop [heading Downward]
			curl (df.middle - wide) quoteBottom [widths.heading HalfStroke HalfStroke Downward]

	create-glyph 'revdoubleprime' 0x2036 : glyph-proc
		local dfSingle : DivFrame para.advanceScaleF
		local d : 0.25 * (RightSB - SB)
		include : refer-glyph "revprime"
		include : Translate (-d * 2) 0
		include : refer-glyph "revprime"
		include : Translate (d + Middle - dfSingle.middle) 0

	create-glyph 'revtripleprime' 0x2037 : glyph-proc
		local df : include : DivFrame para.advanceScaleM
		local wide : -Width * 0.1
		local d : df.width * 0.3
		local fine : 0.5 * [AdviceStroke 3.5]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle + wide) quoteTop [heading Downward]
			curl (df.middle - wide) quoteBottom [widths.heading fine fine Downward]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle - d + wide) quoteTop [heading Downward]
			curl (df.middle - d - wide) quoteBottom [widths.heading fine fine Downward]
		include : dispiro
			widths.center (fine * 2 * 1.2)
			flat (df.middle + d + wide) quoteTop [heading Downward]
			curl (df.middle + d - wide) quoteBottom [widths.heading fine fine Downward]

	alias 'mdfPrime' 0x2B9 'prime'
	alias 'grek/numberSign' 0x374 'prime'
	alias 'mdfDoublePrime' 0x2BA 'doubleprime'

	derive-composites 'grek/lowNumberSign' 0x375 'grek/numberSign'
		ApparentTranslate 0 ([mix [mix [mix PeriodSize commaLow 0.5] yCurlyQuotes 0.5] quoteBottom (-1)] - quoteTop)

	create-glyph 'doubleSuspensionMark' 0x2E44 : composite-proc
		refer-glyph 'grek/numberSign'
		refer-glyph 'grek/lowNumberSign'

	define [StraightQuoteShape df t b] : dispiro
		widths.center (Stroke * 1.1)
		g4.down.start df.middle t [heading Downward]
		g4 df.middle b [widths.center.heading (Stroke * 0.95) Downward]

	# ASCII quotes
	create-glyph 'asciiSingleQuote/body/straight' : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		include : StraightQuoteShape df quoteTop quoteBottom

	create-glyph 'Saltillo' 0xA78B : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		include : df.markSet.capital
		include : StraightQuoteShape df CAP (0.625 * XH)

	create-glyph 'saltillo' 0xA78C : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		include : df.markSet.b
		include : StraightQuoteShape df Ascender XH

	create-glyph 'asciiGrave/selector.straight' : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		DependentSelector.set currentGlyph 'straight'
	create-glyph 'asciiGrave/selector.raisedInverseComma' : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		DependentSelector.set currentGlyph 'raisedInverseComma'
	create-glyph 'asciiGrave/selector.raisedTurnComma' : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		DependentSelector.set currentGlyph 'raisedTurnComma'

	create-glyph 'asciiSingleQuote/selector.straight' : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		DependentSelector.set currentGlyph 'straight'
	create-glyph 'asciiSingleQuote/selector.raisedComma' : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		DependentSelector.set currentGlyph 'raisedComma'

	select-variant 'asciiSingleQuote/selector' (follow -- 'asciiSingleQuote')
	select-variant 'asciiGrave/selector' (follow -- 'asciiGrave')

	CreateDependentComposite 'asciiGrave' 0x60 'asciiGrave/selector' : object
		'straight'            'asciiGrave/body/straight'
		'raisedInverseComma'  'revSingleQuote'
		'raisedTurnComma'     'openSingleQuote'

	CreateDependentComposite 'asciiSingleQuote' 0x27 'asciiSingleQuote/selector' : object
		'straight'            'asciiSingleQuote/body/straight'
		'raisedComma'         'closeSingleQuote'

	alias 'mdfStress' 0x2C8 'asciiSingleQuote/body/straight'
	turned 'mdfSecondaryStress' 0x2CC 'asciiSingleQuote/body/straight' [DivFrame para.advanceScaleF].middle (XH / 2)

	create-glyph 'asciiDoubleQuote' 0x22 : glyph-proc
		local dfSingle : DivFrame para.advanceScaleF
		local dist : 0.2 * Width
		include : refer-glyph "asciiSingleQuote/body/straight"
		include : Translate (-dist * 2) 0
		include : refer-glyph "asciiSingleQuote/body/straight"
		include : Translate (dist + Middle - dfSingle.middle) 0

	# Half-circle modifiers, use quote dimensions
	create-glyph 'modifierLetterLeftHalfRing' 0x2BF : glyph-proc
		local height : quoteTop - quoteBottom
		include : dispiro
			g4.left.start (Middle + height / 4) (quoteTop - O) [widths.lhs.heading [AdviceStroke 3] Leftward]
			archv
			g4.down.mid (Middle - height / 4 + O) [mix quoteBottom quoteTop 0.5] [widths.lhs.heading [AdviceStroke 3.5] Downward]
			arcvh
			g4.right.end (Middle + height / 4) (quoteBottom + O) [widths.lhs.heading [AdviceStroke 3] Rightward]

	turned 'modifierLetterRightHalfRing' 0x2BE 'modifierLetterLeftHalfRing' Middle [mix quoteBottom quoteTop 0.5]

	derive-composites 'hypodiastole' 0x2E12 'modifierLetterRightHalfRing'
		ApparentTranslate 0 ([mix [mix [mix PeriodSize commaLow 0.5] yCurlyQuotes 0.5] quoteBottom (-1)] - quoteTop)

	# Armenian exclamatory
	create-glyph 'armn/exclam' 0x55C : glyph-proc
		local df : include : DivFrame para.advanceScaleF
		local height : quoteTop - quoteBottom
		local xLeft  : df.middle - height / 2.5
		local xRight : df.middle + height / 2.5
		local sw : AdviceStroke 3
		include : dispiro
			g4.up.start xLeft  quoteBottom [widths.rhs.heading sw Upward]
			g2 [mix xLeft xRight 0.5] [mix quoteBottom quoteTop 0.5] [widths.center sw]
			g4.up.end xRight quoteTop [widths.lhs.heading sw Upward]

	create-glyph "armn/questionMark" 0x055E : glyph-proc
		local df : include : DivFrame para.advanceScaleF

		local height : quoteTop - quoteBottom
		local xLeft  : df.middle - height / 2
		local xRight : df.middle + height / 2
		local sw : AdviceStroke 4

		local frame : new Box quoteTop [mix quoteBottom quoteTop 0.2] xLeft xRight
		include : dispiro
			g4.up.start frame.left frame.bot [widths.rhs sw]
			arch.rhs frame.top 0.55
			g4.down.mid frame.right ([frame.yp 0.55] - 0.5 * sw) [widths.rhs sw]
			arcvh
			g4.left.end [frame.xp 0.6] frame.bot [heading Leftward]
