-- Well, here we have yet another math font that does things differently: a huge prime in the ssty1
-- slot (not an alternate but a single) and a small one in the normal location. Actually, only primes
-- have an ssty feature. Now, because we have some magic with primes in ConTeXt we cannot use some
-- fix-feature or just disable ssty, but we have to revert to a runtime fix. Lucky me that I didn't
-- remove that hardly needed 'tweaks' mechanism yet.


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

return {
    name = "libertinus-math",
    version = "1.00",
    comment = "Goodies that complement libertinus.",
    author = "Hans Hagen & Mikael Sundqvist",
    copyright = "ConTeXt development team",
    mathematics = {
        parameters = {
            FractionRuleThickness          =   50,
            OverbarRuleThickness           =   50,
            UnderbarRuleThickness          =   50,
            AccentTopShiftUp               =  -35,
            FlattenedAccentTopShiftUp      =  -35,
            AccentBaseDepth                =   10,
            DelimiterPercent               =   85,
            DelimiterShortfall             =  400,
            DelimiterDisplayPercent        =   90,
            DelimiterDisplayShortfall      =  400,
            PrimeShiftUp                   = "1.2*SuperscriptShiftUp",
            PrimeShiftUpCramped            = "1.2*SuperscriptShiftUp",
            -- RadicalDisplayStyleVerticalGap    = "RadicalVerticalGap", --170, 90
            RadicalDisplayStyleVerticalGap = 50,-- 170
            RadicalVerticalGap             = 50,-- 90
        },
        tweaks = {
            aftercopying = {
                {
                    tweak    = "fontname",
                    expected = "LibertinusMath-Regular",
                    abort    = true,
                },
                {
                    tweak    = "version",
                    expected = "Version 7.040",
                },
                {
                    tweak = "addmirrors",
                },
                -- We add extensiblesd when they are not there.
                {
                    tweak = "fixintegrals",
                },
-- {
--     tweak = "dimensions",
--     list  = {
--         [0x302] = { width = 0 }, --hat
--         [0x303] = { width = 0 }, --tilde
--     },
-- },
                presets.scripttocalligraphic { },
                presets.rsfstoscript         { rscale  = 0.97 },
                presets.rsfsuprighttoscript  { rscale  = 0.97 },
                presets.moderntocalligraphic { rscale  = 0.97 },
                presets.eulertocalligraphic  { rscale  = 0.97 },
                presets.xitsarabic           { rscale  = 0.85 },
                presets.fallbacks            { },
                presets.moveitalics          { correct = true },
                presets.moveitalics          { correct = true, letters = true },
                presets.moveintegrals        { factor = 1.5 },
                presets.wipeitalics          { },
                {
                    tweak = "checkaccents",
                },
                {
                    tweak = "simplifykerns",
                },
                {
                    tweak = "kerns",
                    list  = {
                        [0x002F]                = { topleft  = -0.15, bottomright = -0.15 },
                        ["0x7D.parts.top"]      = { topright = -0.2,                      }, -- right brace top
                        ["0x7D.parts.bottom"]   = {                   bottomright = -0.2  }, -- right brace bottom
                        ["0x29.parts.top"]      = { topright = -0.15,                     }, -- right parenthesis top
                        ["0x29.parts.bottom"]   = {                   bottomright = -0.15 }, -- right parenthesis bottom
                        ["0x29.variants.*"]     = { topright = -0.15, bottomright = -0.15 }, -- 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
                        [0x27E9]                = { topright = -0.1,  bottomright = -0.1  }, -- angle
                        ["0x27E9.variants.*"]   = { topright = -0.2,  bottomright = -0.2  },
                        [0x27EB]                = { topright = -0.1,  bottomright = -0.1  },
                        ["0x27EB.variants.*"]   = { topright = -0.2,  bottomright = -0.2  },
                        --
                        [0x00393] = { bottomright = -0.30, }, -- upright Gamma
                    },
                },
                -- 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 = "dimensions",
                    list  = {
--                         [0x302] = { width = 0 }, --hat
--                         [0x303] = { width = 0 }, --tilde
                        -- The middle pice is shifted compared to the larger pieces.
                        -- And the integral stick out down left.
                        -- We fix the width and shift.
                        [0x2320] = { width = 1.2, xoffset = 0.191 },
                        [0x23AE] = { width = 1.2, }, --  bug
                        [0x2321] = { width = 1.2, xoffset = 0.191 },
                    },
                },
                {
                    tweak = "extendaccents",
                },
                {
                    tweak = "flattenaccents",
                },
                {
                    tweak = "fixaccents",
                },
                -- First we set the dimensions of the initial accent which started out as zero but we want
                -- a proper width.
                -- {
                --     tweak = "dimensions",
                --     list  = {
                --         [0x00302] = { width = 1.8, anchor = 1.40, xoffset =  .20 }, -- widehat
                --         [0x00303] = { width = 2.0, anchor = 1.50, xoffset =  .30 }, -- widetilde
                --         [0x00306] = { width = 1.6, anchor = 1.30, xoffset =  .15 }, -- widebreve
                --         [0x0030C] = { width = 1.3, anchor = 1.15, xoffset = .075 }, -- widecheck
                --     },
                -- },
                -- 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 libertinus.
                -- 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 = "dimensions",
                    list  = {
                        [0x305] = { height = .8, yoffset = -.2  }, -- widehat
                    },
                },
                {
                    tweak = "copyaccents",
                },
                -- So far for the accents.
                {
                    tweak   = "radicaldegreeanchors",
                    list = {
                        [0x221A]                = { location = "left", hfactor  = .0, vfactor  = .625 },
                        ["0x221A.variants.*"]   = { location = "left", hfactor  = .0, vfactor  = .5 },
                        ["0x221A.variants.1"]   = { location = "left", hfactor  = .0, vfactor  = .55 },
                        ["0x221A.parts.bottom"] = { location = "left", hfactor  = .0, vfactor  = 1.15 },
                    }
                },
                {
                    tweak   = "fixprimes",
                    factor  = 0.92,
                    fake    = 0.75,
                },
                {
                    tweak = "checkspacing",
                },
                {
                    tweak = "addscripts",
                },
                {
                    tweak = "accentdimensions",
                },
             -- {
             --     tweak = "addrules",
             -- },
                {
                    tweak   = "addbars",
                    advance = 0.5,
                },
                {
                    tweak    = "replacerules",
                    minus    = {
                        height      = .176,
                        yoffset     = .825,
                        leftoffset  = .065,
                        rightoffset = .065,
                    },
                    fraction = {
                        height  = .176,
                        yoffset = .825,
                    },
                    radical  = {
                        height      = .140,
                        yoffset     = .6575,
                        leftoffset  = .075,
                        rightoffset = .075,
                        yscale      = .8,
                    },
                    stacker  = {
                        height  = .176,
                        yoffset = .825,
                    },
                },
                {
                    tweak = "addactuarian",
                },
                {
                    tweak = "addequals",
                },
                {
                    tweak = "addfourier",
                    variant = 1,
                },
                {
                    tweak = "addprimed",
                },
                {
                    -- needed for integrals (bad axis) and (less prominent) fences
                    tweak = "setoptions",
                    set   = { "ignorekerndimensions" }
                },
                -- this is the mkiv section
                {
                    tweak   = "emulatelmtx",
                    feature = "emulatelmtx",
                    comment = "this is for mkiv",
                },
            },
        },
        bigslots = {
            1, 3, 5, 7
        },
    },
}
