local common  = fonts.goodies.load("common-math.lfg")
local presets = common.mathematics.tweaks.presets

return {
    name = "ebgaramond",
    version = "1.00",
    comment = "Goodies that complement ebgaramond.",
    author = "Hans Hagen & Mikael Sundqvist",
    copyright = "ConTeXt development team",
    mathematics = {
        parameters = {
            FractionRuleThickness             =  60,
            OverbarRuleThickness              =  60,
            UnderbarRuleThickness             =  60,
            AccentTopShiftUp                  = -25, -- was 075
            FlattenedAccentBaseHeight         = 620, -- 420 in font (maybe some misunderstanding in the font regarding accents and heights)
            FlattenedAccentTopShiftUp         = -25, -- was 075
            AccentBaseDepth                   =  75,
            DelimiterPercent                  =  85,
            DelimiterShortfall                = 400,
            DelimiterDisplayPercent           =  90,
            DelimiterDisplayShortfall         = 400,
            SubscriptShiftDown                = 200, -- 250 in font
            SuperscriptShiftUp                = 400, -- 430 in font
            SubscriptShiftDownWithSuperscript = "1.25*SubscriptShiftDown", -- trial and error
            SubSuperscriptGapMin              = 200, -- 250 in font
            PrimeShiftUp                      = "1.2*SuperscriptShiftUp",
            PrimeShiftUpCramped               = "1.2*SuperscriptShiftUp",
            PrimeBaselineDropMax              = "0.5*SuperscriptBaselineDropMax",
            RadicalDisplayStyleVerticalGap = "RadicalVerticalGap", --150, 65
        },
        tweaks = {
            aftercopying = {
                {
                    tweak    = "fontname",
                    expected = "Garamond-Math",
                    abort    = true,
                },
                {
                    tweak    = "version",
                    expected = "Version 2019-08-16",
                },
                {
                    tweak   = "fixprimes",
                    factor  = 0.73,
                },
                {
                    tweak = "addmirrors",
                },
                {
                    tweak = "addcomposites",
                },
                presets.scripttocalligraphic { },
                presets.rsfstoscript         { rscale = 0.95  },
                presets.rsfsuprighttoscript  { rscale = 0.95  },
                presets.moderntocalligraphic { rscale = 0.95  },
                presets.eulertocalligraphic  { rscale = 0.95  },
                presets.xitsarabic           { rscale = 0.80  },
                presets.fallbacks            { },
                presets.moveitalics          { correct = true },
                presets.moveitalics          { correct = true, letters = true },
                presets.moveintegrals        { factor  = 1.5  },
                presets.wipeitalics          { },
                {
                    tweak = "checkaccents",
                },
                {
                    tweak = "simplifykerns",
                },
                {
                    tweak = "topanchors",
                    list  = {
                        [0x1D453] = 0.88, -- italic f
                    },
                },
                {
                    tweak = "dimensions",
                    list  = {
                        [0x1D453] = {  width = 1.1 }, -- italic f
                    },
                },
                {
                    tweak = "kerns",
                    list  = {
                        [0x002F]                = { topleft  = -0.2,  bottomright = -0.2  },
                        ["0x7D.parts.top"]      = { topright = -0.15,                     }, -- right brace top
                        ["0x7D.parts.bottom"]   = {                   bottomright = -0.15 }, -- right brace bottom
                        ["0x7D.variants.*"]     = { topright = -0.05, bottomright = -0.05 }, -- right brace variants
                        ["0x29.parts.top"]      = { topright = -0.25,                     }, -- right parenthesis top
                        ["0x29.parts.bottom"]   = {                   bottomright = -0.25 }, -- right parenthesis bottom
                        ["0x29.variants.*"]     = { topright = -0.2,  bottomright = -0.2  }, -- right parenthesis variants
                        ["0x221A.parts.top"]    = { topright =  0.2,                      }, -- right radical top
                        ["0x221A.parts.bottom"] = {                   bottomright =  0.2  }, -- right radical bottom
                        ["0x221A.variants.*"]   = { topright =  0.2,  bottomright =  0.2  }, -- right radical variants                        -- angle
                        [0x27E9]                = { topright = -0.1,  bottomright = -0.1  },
                        ["0x27E9.variants.*"]   = { topright = -0.2,  bottomright = -0.2  },
                        [0x27EB]                = { topright = -0.1,  bottomright = -0.1  },
                        ["0x27EB.variants.*"]   = { topright = -0.2,  bottomright = -0.2  },

                        [0x00051]               = { bottomright = 0.25 }, -- Upright Q
                        [0x0211A]               = { bottomright = 0.2  }, -- Blackboard bold Q
                        [0x1D410]               = { bottomright = 0.2  }, -- Bold Q
                        [0x1D478]               = { bottomright = 0.05 }, -- Bold Q
                        [0x1D5B0]               = { bottomright = 0.05 }, -- Sans Q
                    },
                },
                {
                    -- to be considered
                    -- tweak = "kernpairs",
                    list = {
                        [mathematics.tweaks.subsets.gjqy] = {
                            [mathematics.tweaks.subsets.f] = -.1,
                            [0x1D70C]                      = -.1, -- italic \rho
                        },
                        [mathematics.tweaks.subsets.bhklt] = {
                            [mathematics.tweaks.subsets.f] = -.25,
                            [0x1D70C]                      = -.25, -- italic \rho
                        },
                        [mathematics.tweaks.subsets.d] = {
                            [mathematics.tweaks.subsets.f] = -.25,
                            [0x1D70C]                      = -.25, -- italic \rho
                        },
                        [mathematics.tweaks.subsets.f] = {
                            [mathematics.tweaks.subsets.f] = -.25,
                            [0x1D70C]                      = -.25, -- italic \rho
                            [mathematics.tweaks.subsets.gjqy]  = -.25,
                            [mathematics.tweaks.subsets.bhklt] = -.1,
                            [mathematics.tweaks.subsets.d] = -.25,
                            [mathematics.tweaks.subsets.i] = -.25,
                            [mathematics.tweaks.subsets.mw] = -.25,
                            [mathematics.tweaks.subsets.p] = -.25,
                            [mathematics.tweaks.subsets.acenorsuvxz] = -.25,
                            [mathematics.tweaks.subsets.dotless] = -.25,
                        },
                        [mathematics.tweaks.subsets.i] = {
                            [mathematics.tweaks.subsets.f] = -.25,
                            [0x1D70C]                      = -.25, -- italic \rho
                        },
                        [mathematics.tweaks.subsets.mw] = {
                            [mathematics.tweaks.subsets.f] = -.25,
                            [0x1D70C]                      = -.25, -- italic \rho
                        },
                        [mathematics.tweaks.subsets.p] = {
                            [mathematics.tweaks.subsets.f] = -.25,
                            [0x1D70C]                      = -.25, -- italic \rho
                        },
                        [mathematics.tweaks.subsets.acenorsuvxz] = {
                            [mathematics.tweaks.subsets.f] = -.25,
                            [0x1D70C]                      = -.25, -- italic \rho
                        },
                        [mathematics.tweaks.subsets.dotless] = {
                            [mathematics.tweaks.subsets.f] = -.25,
                            [0x1D70C]                      = -.25, -- italic \rho
                        },
                    },
                },
                -- Accents are a mess. We migrate the extensibles from the combiners to the base accent
                -- and then need to tweak the width (which is auto set because it was zero with a large
                -- accent anchor offset). First we copy and fix.
                {
                    tweak = "extendaccents",
                },
                {
                    tweak = "flattenaccents",
                },
                {
                    tweak = "radicaldegreeanchors",
                    list  = {
                        ["0x221A.parts.bottom"] = { location = "left", hfactor  = .0, vfactor  = 1.0 },
                    }
                },
                -- First we set the dimensions of the initial accent which started out as zero but we want
                -- a proper width. None in ebgaramond.
                -- Then we deal with all offsets and heights in one go. So we treat the initial accent
                -- as well as the variants here. None in ebgaramond
                -- We now copy these to the not wide slots so that we can set these to stretch as well,
                -- if only because it is less confusing and more consistent.
                {
                    tweak = "copyaccents",
                },
                {
                    tweak = "checkspacing",
                },
                {
                    tweak = "addscripts",
                },
                {
                    tweak = "accentdimensions",
                },
                {
                    tweak = "addrules",
                },
                {
                    tweak    = "replacerules",
                    minus = {
                        leftoffset  = .075,
                        rightoffset = .075,
                    },
                    fraction = {
                        height  = .2,
                        yoffset = .8,
                    },
                    radical = {
                        height      = .2,
                        yoffset     = .66,
                        leftoverlap = .020,
                        leftoffset  = .075, -- todo
                        rightoffset = .075, -- todo
                        yscale      = .84,
                    },
                    stacker = {
                        height  = .2,
                        yoffset = .8,
                    },
                },
                {
                    tweak   = "addbars",
                    advance = 0.6,
                },
                {
                    tweak = "addactuarian",
                },
                {
                    tweak = "addequals",
                },
                {
                    tweak = "addfourier",
                    variant = 2,
                },
                {
                    tweak = "addprimed",
                },
                -- this is the mkiv section
                {
                    tweak   = "emulatelmtx",
                    feature = "emulatelmtx",
                    comment = "this is for mkiv",
                },
                {
                    tweak = "diagnose",
                },
            },
        },
        alternates = {
            partial        = { feature = 'ss02', value = 1, comment = "Curved partial" },
            semibold       = { feature = 'ss04', value = 1, comment = "Semibold" },
            extrabold      = { feature = 'ss05', value = 1, comment = "Extrabold" },
            hbar           = { feature = 'ss06', value = 1, comment = "Horizontal bar for h-bar" },
            integral       = { feature = 'ss07', value = 1, comment = "A more slanted integral sign" },
            tilde          = { feature = 'ss09', value = 1, comment = "A tilde variant" },
            outbendingh    = { feature = 'ss10', value = 1, comment = "Out-bending h" },
            largeoperators = { feature = 'ss11', value = 1, comment = "Larger operators" },
        },
        bigslots = {
            1, 3, 5, 7
        },
    },
}
