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

import [mix linreg clamp fallback] from "@iosevka/util"
import [maskBit] from "@iosevka/util/mask-bit"

glyph-module

glyph-block Symbol-Mosaic-Teletext : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Symbol-Geometric-Shared : GeometricDim

	define [MosaicTile t b l r w h id gap] : glyph-proc
		local column : id % w
		local row : Math.floor (id / w)
		define cw : (r - l) / w
		define ch : (t - b) / h
		include : spiro-outline
			corner (l + column * cw + gap)       (t - row * ch - gap)
			corner (l + (column + 1) * cw - gap) (t - row * ch - gap)
			corner (l + (column + 1) * cw - gap) (t - (row + 1) * ch + gap)
			corner (l + column * cw + gap)       (t - (row + 1) * ch + gap)

	define [MosaicPattern t b l r w h bits] : glyph-proc
		if (bits.length > 0) : include : union
			MosaicTile t b l r w h bits.0 0
			MosaicPattern t b l r w h : bits.slice 1

	for-width-kinds WideWidth4
		define top     fontMetrics.os2.sTypoAscender
		define bottom  fontMetrics.os2.sTypoDescender
		define left    0
		define right   MosaicWidth

		define gap : 0.05 * (top - bottom)

		define Geom : GeometricDim MosaicUnitWidth MosaicWidth

		define [BlockMosaicPattern w h bits] : MosaicPattern top bottom left right w h bits
		define [BlockMosaicTile w h id gap] : MosaicTile top bottom left right w h id gap

		define [BlockMosaic22 unicode pattern] : begin
			create-glyph [MangleName : NameUni unicode] [MangleUnicode unicode] : glyph-proc
				set-width MosaicWidth
				include : ForceUpright
				include : BlockMosaicPattern 2 2 : [pattern.split ''].map : x => x - 1

		define [BlockMosaic23 unicode pattern] : begin
			create-glyph [MangleName : NameUni unicode] [MangleUnicode unicode] : glyph-proc
				set-width MosaicWidth
				include : ForceUpright
				include : BlockMosaicPattern 2 3 : [pattern.split ''].map : x => x - 1

		define [BlockMosaic24 unicode pattern] : begin
			create-glyph [MangleName : NameUni unicode] [MangleUnicode unicode] : glyph-proc
				set-width MosaicWidth
				include : ForceUpright
				include : BlockMosaicPattern 2 4 : [pattern.split ''].map : x => x - 1

		define [BlockMosaic unicode w h pattern] : begin
			create-glyph [MangleName : NameUni unicode] [MangleUnicode unicode] : glyph-proc
				set-width MosaicWidth
				include : ForceUpright
				include : BlockMosaicPattern w h pattern

		define [MakeGappedBlockMosaic w h su] : foreach [offset : range 1 : 2 ** (w * h)] : begin
			create-glyph [MangleName : NameUni (su + offset)] [MangleUnicode (su + offset)] : glyph-proc
				set-width MosaicWidth
				include : ForceUpright
				foreach [bit : range 0 (w * h)] : if [maskBit offset bit]
					include : BlockMosaicTile w h bit gap

		MakeGappedBlockMosaic 2 2 0x1CC20
		MakeGappedBlockMosaic 2 3 0x1CE50

		BlockMosaic22 0x2596 '3'
		BlockMosaic22 0x2597 '4'
		BlockMosaic22 0x2598 '1'
		BlockMosaic22 0x2599 '134'
		BlockMosaic22 0x259A '14'
		BlockMosaic22 0x259B '123'
		BlockMosaic22 0x259C '124'
		BlockMosaic22 0x259D '2'
		BlockMosaic22 0x259E '23'
		BlockMosaic22 0x259F '234'

		BlockMosaic23 0x1FB00 '1'
		BlockMosaic23 0x1FB01 '2'
		BlockMosaic23 0x1FB02 '12'
		BlockMosaic23 0x1FB03 '3'
		BlockMosaic23 0x1FB04 '13'
		BlockMosaic23 0x1FB05 '23'
		BlockMosaic23 0x1FB06 '123'
		BlockMosaic23 0x1FB07 '4'
		BlockMosaic23 0x1FB08 '14'
		BlockMosaic23 0x1FB09 '24'
		BlockMosaic23 0x1FB0A '124'
		BlockMosaic23 0x1FB0B '34'
		BlockMosaic23 0x1FB0C '134'
		BlockMosaic23 0x1FB0D '234'
		BlockMosaic23 0x1FB0E '1234'
		BlockMosaic23 0x1FB0F '5'
		BlockMosaic23 0x1FB10 '15'
		BlockMosaic23 0x1FB11 '25'
		BlockMosaic23 0x1FB12 '125'
		BlockMosaic23 0x1FB13 '35'
		BlockMosaic23 0x1FB14 '235'
		BlockMosaic23 0x1FB15 '1235'
		BlockMosaic23 0x1FB16 '45'
		BlockMosaic23 0x1FB17 '145'
		BlockMosaic23 0x1FB18 '245'
		BlockMosaic23 0x1FB19 '1245'
		BlockMosaic23 0x1FB1A '345'
		BlockMosaic23 0x1FB1B '1345'
		BlockMosaic23 0x1FB1C '2345'
		BlockMosaic23 0x1FB1D '12345'
		BlockMosaic23 0x1FB1E '6'
		BlockMosaic23 0x1FB1F '16'
		BlockMosaic23 0x1FB20 '26'
		BlockMosaic23 0x1FB21 '126'
		BlockMosaic23 0x1FB22 '36'
		BlockMosaic23 0x1FB23 '136'
		BlockMosaic23 0x1FB24 '236'
		BlockMosaic23 0x1FB25 '1236'
		BlockMosaic23 0x1FB26 '46'
		BlockMosaic23 0x1FB27 '146'
		BlockMosaic23 0x1FB28 '1246'
		BlockMosaic23 0x1FB29 '346'
		BlockMosaic23 0x1FB2A '1346'
		BlockMosaic23 0x1FB2B '2346'
		BlockMosaic23 0x1FB2C '12346'
		BlockMosaic23 0x1FB2D '56'
		BlockMosaic23 0x1FB2E '156'
		BlockMosaic23 0x1FB2F '256'
		BlockMosaic23 0x1FB30 '1256'
		BlockMosaic23 0x1FB31 '356'
		BlockMosaic23 0x1FB32 '1356'
		BlockMosaic23 0x1FB33 '2356'
		BlockMosaic23 0x1FB34 '12356'
		BlockMosaic23 0x1FB35 '456'
		BlockMosaic23 0x1FB36 '1456'
		BlockMosaic23 0x1FB37 '2456'
		BlockMosaic23 0x1FB38 '12456'
		BlockMosaic23 0x1FB39 '3456'
		BlockMosaic23 0x1FB3A '13456'
		BlockMosaic23 0x1FB3B '23456'

		BlockMosaic24 0x1CD00 '3'
		BlockMosaic24 0x1CD01 '23'
		BlockMosaic24 0x1CD02 '123'
		BlockMosaic24 0x1CD03 '4'
		BlockMosaic24 0x1CD04 '14'
		BlockMosaic24 0x1CD05 '124'
		BlockMosaic24 0x1CD06 '34'
		BlockMosaic24 0x1CD07 '134'
		BlockMosaic24 0x1CD08 '234'
		BlockMosaic24 0x1CD09 '5'
		BlockMosaic24 0x1CD0A '15'
		BlockMosaic24 0x1CD0B '25'
		BlockMosaic24 0x1CD0C '125'
		BlockMosaic24 0x1CD0D '135'
		BlockMosaic24 0x1CD0E '235'
		BlockMosaic24 0x1CD0F '1235'
		BlockMosaic24 0x1CD10 '45'
		BlockMosaic24 0x1CD11 '145'
		BlockMosaic24 0x1CD12 '245'
		BlockMosaic24 0x1CD13 '1245'
		BlockMosaic24 0x1CD14 '345'
		BlockMosaic24 0x1CD15 '1345'
		BlockMosaic24 0x1CD16 '2345'
		BlockMosaic24 0x1CD17 '12345'
		BlockMosaic24 0x1CD18 '6'
		BlockMosaic24 0x1CD19 '16'
		BlockMosaic24 0x1CD1A '26'
		BlockMosaic24 0x1CD1B '126'
		BlockMosaic24 0x1CD1C '36'
		BlockMosaic24 0x1CD1D '136'
		BlockMosaic24 0x1CD1E '236'
		BlockMosaic24 0x1CD1F '1236'
		BlockMosaic24 0x1CD20 '146'
		BlockMosaic24 0x1CD21 '246'
		BlockMosaic24 0x1CD22 '1246'
		BlockMosaic24 0x1CD23 '346'
		BlockMosaic24 0x1CD24 '1346'
		BlockMosaic24 0x1CD25 '2346'
		BlockMosaic24 0x1CD26 '12346'
		BlockMosaic24 0x1CD27 '56'
		BlockMosaic24 0x1CD28 '156'
		BlockMosaic24 0x1CD29 '256'
		BlockMosaic24 0x1CD2A '1256'
		BlockMosaic24 0x1CD2B '356'
		BlockMosaic24 0x1CD2C '1356'
		BlockMosaic24 0x1CD2D '2356'
		BlockMosaic24 0x1CD2E '12356'
		BlockMosaic24 0x1CD2F '456'
		BlockMosaic24 0x1CD30 '1456'
		BlockMosaic24 0x1CD31 '2456'
		BlockMosaic24 0x1CD32 '12456'
		BlockMosaic24 0x1CD33 '3456'
		BlockMosaic24 0x1CD34 '13456'
		BlockMosaic24 0x1CD35 '23456'
		BlockMosaic24 0x1CD36 '17'
		BlockMosaic24 0x1CD37 '27'
		BlockMosaic24 0x1CD38 '127'
		BlockMosaic24 0x1CD39 '37'
		BlockMosaic24 0x1CD3A '137'
		BlockMosaic24 0x1CD3B '237'
		BlockMosaic24 0x1CD3C '1237'
		BlockMosaic24 0x1CD3D '47'
		BlockMosaic24 0x1CD3E '147'
		BlockMosaic24 0x1CD3F '247'
		BlockMosaic24 0x1CD40 '1247'
		BlockMosaic24 0x1CD41 '347'
		BlockMosaic24 0x1CD42 '1347'
		BlockMosaic24 0x1CD43 '2347'
		BlockMosaic24 0x1CD44 '12347'
		BlockMosaic24 0x1CD45 '157'
		BlockMosaic24 0x1CD46 '257'
		BlockMosaic24 0x1CD47 '1257'
		BlockMosaic24 0x1CD48 '357'
		BlockMosaic24 0x1CD49 '2357'
		BlockMosaic24 0x1CD4A '12357'
		BlockMosaic24 0x1CD4B '457'
		BlockMosaic24 0x1CD4C '1457'
		BlockMosaic24 0x1CD4D '12457'
		BlockMosaic24 0x1CD4E '3457'
		BlockMosaic24 0x1CD4F '13457'
		BlockMosaic24 0x1CD50 '23457'
		BlockMosaic24 0x1CD51 '67'
		BlockMosaic24 0x1CD52 '167'
		BlockMosaic24 0x1CD53 '267'
		BlockMosaic24 0x1CD54 '1267'
		BlockMosaic24 0x1CD55 '367'
		BlockMosaic24 0x1CD56 '1367'
		BlockMosaic24 0x1CD57 '2367'
		BlockMosaic24 0x1CD58 '12367'
		BlockMosaic24 0x1CD59 '467'
		BlockMosaic24 0x1CD5A '1467'
		BlockMosaic24 0x1CD5B '2467'
		BlockMosaic24 0x1CD5C '12467'
		BlockMosaic24 0x1CD5D '3467'
		BlockMosaic24 0x1CD5E '13467'
		BlockMosaic24 0x1CD5F '23467'
		BlockMosaic24 0x1CD60 '123467'
		BlockMosaic24 0x1CD61 '567'
		BlockMosaic24 0x1CD62 '1567'
		BlockMosaic24 0x1CD63 '2567'
		BlockMosaic24 0x1CD64 '12567'
		BlockMosaic24 0x1CD65 '3567'
		BlockMosaic24 0x1CD66 '13567'
		BlockMosaic24 0x1CD67 '23567'
		BlockMosaic24 0x1CD68 '123567'
		BlockMosaic24 0x1CD69 '4567'
		BlockMosaic24 0x1CD6A '14567'
		BlockMosaic24 0x1CD6B '24567'
		BlockMosaic24 0x1CD6C '124567'
		BlockMosaic24 0x1CD6D '34567'
		BlockMosaic24 0x1CD6E '134567'
		BlockMosaic24 0x1CD6F '234567'
		BlockMosaic24 0x1CD70 '1234567'
		BlockMosaic24 0x1CD71 '18'
		BlockMosaic24 0x1CD72 '28'
		BlockMosaic24 0x1CD73 '128'
		BlockMosaic24 0x1CD74 '38'
		BlockMosaic24 0x1CD75 '138'
		BlockMosaic24 0x1CD76 '238'
		BlockMosaic24 0x1CD77 '1238'
		BlockMosaic24 0x1CD78 '48'
		BlockMosaic24 0x1CD79 '148'
		BlockMosaic24 0x1CD7A '248'
		BlockMosaic24 0x1CD7B '1248'
		BlockMosaic24 0x1CD7C '348'
		BlockMosaic24 0x1CD7D '1348'
		BlockMosaic24 0x1CD7E '2348'
		BlockMosaic24 0x1CD7F '12348'
		BlockMosaic24 0x1CD80 '58'
		BlockMosaic24 0x1CD81 '158'
		BlockMosaic24 0x1CD82 '258'
		BlockMosaic24 0x1CD83 '1258'
		BlockMosaic24 0x1CD84 '358'
		BlockMosaic24 0x1CD85 '1358'
		BlockMosaic24 0x1CD86 '2358'
		BlockMosaic24 0x1CD87 '12358'
		BlockMosaic24 0x1CD88 '458'
		BlockMosaic24 0x1CD89 '1458'
		BlockMosaic24 0x1CD8A '2458'
		BlockMosaic24 0x1CD8B '12458'
		BlockMosaic24 0x1CD8C '3458'
		BlockMosaic24 0x1CD8D '13458'
		BlockMosaic24 0x1CD8E '23458'
		BlockMosaic24 0x1CD8F '123458'
		BlockMosaic24 0x1CD90 '168'
		BlockMosaic24 0x1CD91 '268'
		BlockMosaic24 0x1CD92 '1268'
		BlockMosaic24 0x1CD93 '368'
		BlockMosaic24 0x1CD94 '2368'
		BlockMosaic24 0x1CD95 '12368'
		BlockMosaic24 0x1CD96 '468'
		BlockMosaic24 0x1CD97 '1468'
		BlockMosaic24 0x1CD98 '12468'
		BlockMosaic24 0x1CD99 '3468'
		BlockMosaic24 0x1CD9A '13468'
		BlockMosaic24 0x1CD9B '23468'
		BlockMosaic24 0x1CD9C '568'
		BlockMosaic24 0x1CD9D '1568'
		BlockMosaic24 0x1CD9E '2568'
		BlockMosaic24 0x1CD9F '12568'
		BlockMosaic24 0x1CDA0 '3568'
		BlockMosaic24 0x1CDA1 '13568'
		BlockMosaic24 0x1CDA2 '23568'
		BlockMosaic24 0x1CDA3 '123568'
		BlockMosaic24 0x1CDA4 '4568'
		BlockMosaic24 0x1CDA5 '14568'
		BlockMosaic24 0x1CDA6 '24568'
		BlockMosaic24 0x1CDA7 '124568'
		BlockMosaic24 0x1CDA8 '34568'
		BlockMosaic24 0x1CDA9 '134568'
		BlockMosaic24 0x1CDAA '234568'
		BlockMosaic24 0x1CDAB '1234568'
		BlockMosaic24 0x1CDAC '178'
		BlockMosaic24 0x1CDAD '278'
		BlockMosaic24 0x1CDAE '1278'
		BlockMosaic24 0x1CDAF '378'
		BlockMosaic24 0x1CDB0 '1378'
		BlockMosaic24 0x1CDB1 '2378'
		BlockMosaic24 0x1CDB2 '12378'
		BlockMosaic24 0x1CDB3 '478'
		BlockMosaic24 0x1CDB4 '1478'
		BlockMosaic24 0x1CDB5 '2478'
		BlockMosaic24 0x1CDB6 '12478'
		BlockMosaic24 0x1CDB7 '3478'
		BlockMosaic24 0x1CDB8 '13478'
		BlockMosaic24 0x1CDB9 '23478'
		BlockMosaic24 0x1CDBA '123478'
		BlockMosaic24 0x1CDBB '578'
		BlockMosaic24 0x1CDBC '1578'
		BlockMosaic24 0x1CDBD '2578'
		BlockMosaic24 0x1CDBE '12578'
		BlockMosaic24 0x1CDBF '3578'
		BlockMosaic24 0x1CDC0 '13578'
		BlockMosaic24 0x1CDC1 '23578'
		BlockMosaic24 0x1CDC2 '123578'
		BlockMosaic24 0x1CDC3 '4578'
		BlockMosaic24 0x1CDC4 '14578'
		BlockMosaic24 0x1CDC5 '24578'
		BlockMosaic24 0x1CDC6 '124578'
		BlockMosaic24 0x1CDC7 '34578'
		BlockMosaic24 0x1CDC8 '134578'
		BlockMosaic24 0x1CDC9 '234578'
		BlockMosaic24 0x1CDCA '1234578'
		BlockMosaic24 0x1CDCB '678'
		BlockMosaic24 0x1CDCC '1678'
		BlockMosaic24 0x1CDCD '2678'
		BlockMosaic24 0x1CDCE '12678'
		BlockMosaic24 0x1CDCF '3678'
		BlockMosaic24 0x1CDD0 '13678'
		BlockMosaic24 0x1CDD1 '23678'
		BlockMosaic24 0x1CDD2 '123678'
		BlockMosaic24 0x1CDD3 '4678'
		BlockMosaic24 0x1CDD4 '14678'
		BlockMosaic24 0x1CDD5 '24678'
		BlockMosaic24 0x1CDD6 '124678'
		BlockMosaic24 0x1CDD7 '34678'
		BlockMosaic24 0x1CDD8 '134678'
		BlockMosaic24 0x1CDD9 '234678'
		BlockMosaic24 0x1CDDA '1234678'
		BlockMosaic24 0x1CDDB '15678'
		BlockMosaic24 0x1CDDC '25678'
		BlockMosaic24 0x1CDDD '125678'
		BlockMosaic24 0x1CDDE '35678'
		BlockMosaic24 0x1CDDF '235678'
		BlockMosaic24 0x1CDE0 '1235678'
		BlockMosaic24 0x1CDE1 '45678'
		BlockMosaic24 0x1CDE2 '145678'
		BlockMosaic24 0x1CDE3 '1245678'
		BlockMosaic24 0x1CDE4 '1345678'
		BlockMosaic24 0x1CDE5 '2345678'

		BlockMosaic 0x2429 4 8 {0 2 5 7 8 10 13 15 16 18 21 23 24 26 29 31}
		BlockMosaic 0x1FB95 4 4 {0 2 5 7 8 10 13 15}
		BlockMosaic 0x1FB96 4 4 {1 3 4 6 9 11 12 14}
		BlockMosaic 0x1FB97 4 4 {4 5 6 7 12 13 14 15}

		create-glyph [MangleName 'symbolForDeleteSquareCheckerForm'] [MangleUnicode 0x2427] : glyph-proc
			set-width MosaicWidth
			include : MosaicPattern Geom.Top Geom.Bot Geom.Left Geom.Right 5 5 {0 2 4 6 8 10 12 14 16 18 20 22 24}

		create-glyph [MangleName 'symbolForDeleteRectCheckerForm'] [MangleUnicode 0x2428] : glyph-proc
			set-width MosaicWidth
			include : MosaicPattern (Geom.MidY + Geom.Size * 1.4) (Geom.MidY - Geom.Size * 1.4) Geom.Left Geom.Right 5 7 {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33}

		create-glyph [MangleName 'hellschreiberPause'] [MangleUnicode 0x2BFF] : glyph-proc
			set-width MosaicWidth
			include : MosaicPattern [Math.min (Geom.MidY + Geom.Size * 2) top] [Math.max (Geom.MidY - Geom.Size * 2) bottom] Geom.Left Geom.Right 5 10 {0 1 2 3 4 5 6 7 8 9 12 17 22 23 24 27 28 29 32 37 40 41 42 43 44 45 46 47 48 49}
