math-dim.lmt /size: 20 Kb    last modification: 2024-01-16 10:22
1if not modules then modules = { } end modules ['math-dim'] = {
2    version   = 1.001,
3    comment   = "companion to math-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- The radical_rule value is also used as a trigger. In luatex the accent
10-- placement happens either the opentype way (using topaccent cum suis) or the
11-- traditional way. In order to determine what method to use the \Umathradicalrule
12-- setting is consulted to determine what method to use. This is more efficient
13-- than analyzing the (potentially spread over multiple families) situation. For
14-- this reason we need to set the radical_rule here. It used to be "<unset>" in
15-- which case the engine takes the rulethickness. In c-speak:
16--
17-- int compat_mode = (radical_rule(cur_style) == undefined_math_parameter) ;
18--
19-- In the meantime things have been updated and we now have two code paths.
20
21local abs, next = math.abs, next
22
23local defaults = {
24    axis                         = { default                     = { "AxisHeight",                               "axisheight" }, },
25    accent_base_height           = { default                     = { "AccentBaseHeight",                         "xheight" }, },
26    flattened_accent_base_height = { default                     = { "FlattenedAccentBaseHeight",                "xheight" }, },
27    fraction_del_size            = { default                     = { "FractionDelimiterSize",                    "delim2" },
28                                     cramped_display_style       = { "FractionDelimiterDisplayStyleSize",        "delim1" },
29                                     display_style               = { "FractionDelimiterDisplayStyleSize",        "delim1" }, },
30    fraction_denom_down          = { default                     = { "FractionDenominatorShiftDown",             "denom2" },
31                                     cramped_display_style       = { "FractionDenominatorDisplayStyleShiftDown", "denom1" },
32                                     display_style               = { "FractionDenominatorDisplayStyleShiftDown", "denom1" }, },
33    fraction_denom_vgap          = { default                     = { "FractionDenominatorGapMin",                "defaultrulethickness" },
34                                     cramped_display_style       = { "FractionDenominatorDisplayStyleGapMin",    "3*defaultrulethickness" },
35                                     display_style               = { "FractionDenominatorDisplayStyleGapMin",    "3*defaultrulethickness" }, },
36    fraction_num_up              = { default                     = { "FractionNumeratorShiftUp",                 "num2" },
37                                     cramped_display_style       = { "FractionNumeratorDisplayStyleShiftUp",     "num1" },
38                                     display_style               = { "FractionNumeratorDisplayStyleShiftUp",     "num1" }, },
39    fraction_num_vgap            = { default                     = { "FractionNumeratorGapMin",                  "defaultrulethickness" },
40                                     cramped_display_style       = { "FractionNumeratorDisplayStyleGapMin",      "3*defaultrulethickness" },
41                                     display_style               = { "FractionNumeratorDisplayStyleGapMin",      "3*defaultrulethickness" }, },
42    skewed_fraction_hgap         = { default                     = { "SkewedFractionHorizontalGap",              "mathquad/2" },
43                                     cramped_display_style       = { "SkewedFractionHorizontalGap",              "mathquad/2" },
44                                     display_style               = { "SkewedFractionHorizontalGap",              "mathquad/2" }, },
45    skewed_fraction_vgap         = { default                     = { "SkewedFractionVerticalGap",                "xheight" },
46                                     cramped_display_style       = { "SkewedFractionVerticalGap",                "xheight" },
47                                     display_style               = { "SkewedFractionVerticalGap",                "xheight" }, },
48    fraction_rule                = { default                     = { "FractionRuleThickness",                    "defaultrulethickness" }, },
49    limit_above_bgap             = { default                     = { "UpperLimitBaselineRiseMin",                "bigopspacing3" }, },
50    limit_above_vgap             = { default                     = { "UpperLimitGapMin",                         "bigopspacing1" }, },
51    limit_above_kern             = { default                     = { "0",                                        "bigopspacing5" }, },
52    limit_below_bgap             = { default                     = { "LowerLimitBaselineDropMin",                "bigopspacing4" }, },
53    limit_below_vgap             = { default                     = { "LowerLimitGapMin",                         "bigopspacing2" }, },
54    limit_below_kern             = { default                     = { "0",                                        "bigopspacing5" }, },
55    math_operator_size           = { default                     = { "DisplayOperatorMinHeight",                 "mathxheight*3" }, }, -- 2
56    overbar_kern                 = { default                     = { "OverbarExtraAscender",                     "defaultrulethickness" }, },
57    overbar_rule                 = { default                     = { "OverbarRuleThickness",                     "defaultrulethickness" }, },
58    overbar_vgap                 = { default                     = { "OverbarVerticalGap",                       "3*defaultrulethickness" }, },
59    quad                         = { default                     = { "fontsize(f)",                              "mathquad" }, },
60    radical_kern                 = { default                     = { "RadicalExtraAscender",                     "defaultrulethickness" }, },
61    radical_rule                 = { default                     = { "RadicalRuleThickness",                     "defaultrulethickness" }, },
62                                  -- default                     = { "surdheight(f)",                            "defaultrulethickness" },
63    radical_vgap                 = { default                     = { "RadicalVerticalGap",                       "defaultrulethickness+(abs(defaultrulethickness)/4)" },
64                                     display_style               = { "RadicalDisplayStyleVerticalGap",           "defaultrulethickness+(abs(mathxheight)/4)" }, },
65    space_after_script           = { default                     = { "SpaceAfterScript",                         "scriptspace" }, },
66    space_before_script          = { default                     = { "SpaceAfterScript",                         "scriptspace" }, },
67    stack_denom_down             = { default                     = { "StackBottomShiftDown",                     "denom2" },
68                                     cramped_display_style       = { "StackBottomDisplayStyleShiftDown",         "denom1" },
69                                     display_style               = { "StackBottomDisplayStyleShiftDown",         "denom1" }, },
70    stack_num_up                 = { default                     = { "StackTopShiftUp",                          "num3" },
71                                     cramped_display_style       = { "StackTopDisplayStyleShiftUp",              "num1" },
72                                     display_style               = { "StackTopDisplayStyleShiftUp",              "num1" }, },
73    stack_vgap                   = { default                     = { "StackGapMin",                              "3*defaultrulethickness" },
74                                     cramped_display_style       = { "StackDisplayStyleGapMin",                  "7*defaultrulethickness" },
75                                     display_style               = { "StackDisplayStyleGapMin",                  "7*defaultrulethickness" }, },
76    sub_shift_down               = { default                     = { "SubscriptShiftDown",                       "sub1" }, },
77    sub_shift_drop               = { default                     = { "SubscriptBaselineDropMin",                 "subdrop" }, },
78    sub_sup_shift_down           = { default                     = { "SubscriptShiftDown",                       "sub2" }, },
79    sub_top_max                  = { default                     = { "SubscriptTopMax",                          "abs(mathxheight*4)/5" }, },
80    subsup_vgap                  = { default                     = { "SubSuperscriptGapMin",                     "4*defaultrulethickness" }, },
81    sup_bottom_min               = { default                     = { "SuperscriptBottomMin",                     "abs(mathxheight)/4" }, },
82    sup_shift_drop               = { default                     = { "SuperscriptBaselineDropMax",               "supdrop" }, },
83    sup_shift_up                 = { cramped_display_style       = { "SuperscriptShiftUpCramped",                "sup3" },
84                                     cramped_script_script_style = { "SuperscriptShiftUpCramped",                "sup3" },
85                                     cramped_script_style        = { "SuperscriptShiftUpCramped",                "sup3" },
86                                     cramped_text_style          = { "SuperscriptShiftUpCramped",                "sup3" },
87                                     display_style               = { "SuperscriptShiftUp",                       "sup1" },
88                                     script_script_style         = { "SuperscriptShiftUp",                       "sup2" },
89                                     script_style                = { "SuperscriptShiftUp",                       "sup2" },
90                                     text_style                  = { "SuperscriptShiftUp",                       "sup2" }, },
91    sup_sub_bottom_max           = { default                     = { "SuperscriptBottomMaxWithSubscript",        "abs(mathxheight*4)/5" }, },
92    underbar_kern                = { default                     = { "UnderbarExtraDescender",                   "0" }, },
93    underbar_rule                = { default                     = { "UnderbarRuleThickness",                    "defaultrulethickness" }, },
94    underbar_vgap                = { default                     = { "UnderbarVerticalGap",                      "3*defaultrulethickness" }, },
95    connector_overlap_min        = { default                     = { "MinConnectorOverlap",                      "0.25*defaultrulethickness" }, },
96    over_delimiter_vgap          = { default                     = { "StretchStackGapBelowMin",                  "bigopspacing1" }, },
97    over_delimiter_bgap          = { default                     = { "StretchStackTopShiftUp",                   "bigopspacing3" }, },
98    under_delimiter_vgap         = { default                     = { "StretchStackGapAboveMin",                  "bigopspacing2" }, },
99    under_delimiter_bgap         = { default                     = { "StretchStackBottomShiftDown",              "bigopspacing4" }, },
100    radical_degree_before        = { default                     = { "RadicalKernBeforeDegree",                  "(5/18)*quad" }, },
101    radical_degree_after         = { default                     = { "RadicalKernAfterDegree",                   "(-10/18)*quad" }, },
102    radical_degree_raise         = { default                     = { "RadicalDegreeBottomRaisePercent",          "60" }, },
103    no_limit_sub_factor          = { default                     = { "NoLimitSubFactor",                         "0" }, },
104    no_limit_sup_factor          = { default                     = { "NoLimitSupFactor",                         "0" }, },
105}
106
107local styles = {
108    'display_style',
109    'cramped_display_style',
110    'text_style',
111    'cramped_text_style',
112    'script_style',
113    'cramped_script_style',
114    'script_script_style',
115    'cramped_script_script_style',
116}
117
118for k, v in next, defaults do
119    for _, s in next, styles do
120        if not v[s] then
121            v[s] = v.default
122        end
123    end
124end
125
126-- we cannot use a metatable because we do a copy (takes a bit more work)
127--
128-- local mt = { }  setmetatable(defaults,mt)
129--
130-- mt.__index = function(t,s)
131--     return t.default or t.text_style or 0
132-- end
133--
134-- This will all go away in \LMTX\ because it makes no sense to support old fonts any longer,
135-- even when we assemble them one really needs to think about proper values. Okay, there is
136-- some historic value in here.
137
138function mathematics.dimensions(dimens)  -- beware, dimens get spoiled
139    if dimens.SpaceAfterScript then
140        return table.fastcopy(dimens), { }
141    elseif dimens.AxisHeight or dimens.axisheight then
142        local t = { }
143        local mathxheight = dimens.xheight or 10*65536
144        local mathquad = dimens.quad or 10*65536
145        local defaultrulethickness = dimens.FractionDenominatorGapMin or dimens.defaultrulethickness or 0.4*65536
146        dimens["0"] = 0
147        dimens["60"] = 60
148        dimens["0.25*defaultrulethickness"] = defaultrulethickness / 4
149        dimens["3*defaultrulethickness"] = 3 * defaultrulethickness
150        dimens["4*defaultrulethickness"] = 4 * defaultrulethickness
151        dimens["7*defaultrulethickness"] = 7 * defaultrulethickness
152        dimens["(5/18)*quad"] = (mathquad * 5) / 18
153        dimens["(-10/18)*quad"] = - (mathquad * 10) / 18
154        dimens["mathxheight*3"] = mathxheight * 3 -- needs checking
155        dimens["abs(mathxheight*4)/5"] = abs(mathxheight * 4) / 5
156        dimens["defaultrulethickness+(abs(defaultrulethickness)/4)"] = defaultrulethickness+(abs(defaultrulethickness) / 4)
157        dimens["defaultrulethickness+(abs(mathxheight)/4)"] = defaultrulethickness+(abs(mathxheight) / 4)
158        dimens["abs(mathxheight)/4"] = abs(mathxheight) / 4
159        dimens["abs(mathxheight*4)/5"] = abs(mathxheight * 4) / 5
160        dimens["<not set>"] = false
161        dimens["scriptspace"] = false -- at macro level
162        for variable, styles in next, defaults do
163            local tt = { }
164            for style, default in next, styles do
165                local one   = default[1]
166                local two   = default[2]
167                local value = dimens[one]
168                if value then
169                    tt[style] = value
170                else
171                    value = dimens[two]
172                    if value == false then
173                        tt[style] = nil
174                    else
175                        tt[style] = value or 0
176                    end
177                end
178            end
179            t[variable] = tt
180        end
181        local d = {
182            AccentBaseHeight                            = t . accent_base_height           . text_style,
183            FlattenedAccentBaseHeight                   = t . flattened_accent_base_height . text_style,
184            AxisHeight                                  = t . axis                         . text_style,
185         -- DelimitedSubFormulaMinHeight
186            DisplayOperatorMinHeight                    = t . math_operator_size           . text_style, -- no longer let tex decide (weird values)
187            FractionDenominatorDisplayStyleGapMin       = t . fraction_denom_vgap          . display_style,
188            FractionDenominatorDisplayStyleShiftDown    = t . fraction_denom_down          . display_style,
189            FractionDenominatorGapMin                   = t . fraction_denom_vgap          . text_style,
190            FractionDenominatorShiftDown                = t . fraction_denom_down          . text_style,
191            FractionNumeratorDisplayStyleGapMin         = t . fraction_num_vgap            . display_style,
192            FractionNumeratorDisplayStyleShiftUp        = t . fraction_num_up              . display_style,
193            FractionNumeratorGapMin                     = t . fraction_num_vgap            . text_style,
194            FractionNumeratorShiftUp                    = t . fraction_num_up              . text_style,
195            FractionRuleThickness                       = t . fraction_rule                . text_style,
196            FractionDelimiterSize                       = t . fraction_del_size            . text_style,
197            FractionDelimiterDisplayStyleSize           = t . fraction_del_size            . display_style,
198            LowerLimitBaselineDropMin                   = t . limit_below_bgap             . text_style,
199            LowerLimitGapMin                            = t . limit_below_vgap             . text_style,
200         -- MathLeading
201            MinConnectorOverlap                         = t . connector_overlap_min        . text_style,
202            OverbarExtraAscender                        = t . overbar_kern                 . text_style,
203            OverbarRuleThickness                        = t . overbar_rule                 . text_style,
204            OverbarVerticalGap                          = t . overbar_vgap                 . text_style,
205            RadicalDisplayStyleVerticalGap              = t . radical_vgap                 . display_style,
206            RadicalExtraAscender                        = t . radical_kern                 . text_style,
207            RadicalRuleThickness                        = t . radical_rule                 . text_style,
208            RadicalVerticalGap                          = t . radical_vgap                 . text_style,
209            RadicalKernBeforeDegree                     = t . radical_degree_before        . display_style,
210            RadicalKernAfterDegree                      = t . radical_degree_after         . display_style,
211            RadicalDegreeBottomRaisePercent             = t . radical_degree_raise         . display_style,
212         -- ScriptPercentScaleDown
213         -- ScriptScriptPercentScaleDown
214            SkewedFractionHorizontalGap                 = t . skewed_fraction_hgap         . default,
215            SkewedFractionVerticalGap                   = t . skewed_fraction_vgap         . default,
216            SpaceAfterScript                            = t . space_after_script           . text_style,
217            StackBottomDisplayStyleShiftDown            = t . stack_denom_down             . display_style,
218            StackBottomShiftDown                        = t . stack_denom_down             . text_style,
219            StackDisplayStyleGapMin                     = t . stack_vgap                   . display_style,
220            StackGapMin                                 = t . stack_vgap                   . text_style,
221            StackTopDisplayStyleShiftUp                 = t . stack_num_up                 . display_style,
222            StackTopShiftUp                             = t . stack_num_up                 . text_style,
223            StretchStackGapBelowMin                     = t . over_delimiter_vgap          . text_style,
224            StretchStackTopShiftUp                      = t . over_delimiter_bgap          . text_style,
225            StretchStackGapAboveMin                     = t . under_delimiter_vgap         . text_style,
226            StretchStackBottomShiftDown                 = t . under_delimiter_bgap         . text_style,
227            SubSuperscriptGapMin                        = t . subsup_vgap                  . text_style,
228            SubscriptBaselineDropMin                    = t . sub_shift_drop               . text_style,
229            SubscriptShiftDown                          = t . sub_shift_down               . text_style,
230            SubscriptShiftDownWithSuperscript           = t . sub_sup_shift_down           . text_style,
231            SubscriptTopMax                             = t . sub_top_max                  . text_style,
232            SuperscriptBaselineDropMax                  = t . sup_shift_drop               . text_style,
233            SuperscriptBottomMaxWithSubscript           = t . sup_sub_bottom_max           . text_style,
234            SuperscriptBottomMin                        = t . sup_bottom_min               . text_style,
235            SuperscriptShiftUp                          = t . sup_shift_up                 . text_style,
236            SuperscriptShiftUpCramped                   = t . sup_shift_up                 . cramped_text_style,
237            UnderbarExtraDescender                      = t . underbar_kern                . text_style,
238            UnderbarRuleThickness                       = t . underbar_rule                . text_style,
239            UnderbarVerticalGap                         = t . underbar_vgap                . text_style,
240            UpperLimitBaselineRiseMin                   = t . limit_above_bgap             . text_style,
241            UpperLimitGapMin                            = t . limit_above_vgap             . text_style,
242        }
243        -- too fragile for tx/px ... even the same values give different results
244     -- d.DisplayOperatorMinHeight = nil
245     -- d.AccentBaseHeight         = 0 -- here? still? or maybe undedined i.e. maxdimen
246        return d, t -- t only for diagnostics
247    else
248        return { }, { }
249    end
250end
251
252