math-dim.lua /size: 19 Kb    last modification: 2023-12-21 09:44
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 top_accent 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",                               "axis_height" }, },
25    accent_base_height    = { default                     = { "AccentBaseHeight",                         "x_height" }, },
26    fraction_del_size     = { default                     = { "FractionDelimiterSize",                    "delim2" },
27                              cramped_display_style       = { "FractionDelimiterDisplayStyleSize",        "delim1" },
28                              display_style               = { "FractionDelimiterDisplayStyleSize",        "delim1" }, },
29    fraction_denom_down   = { default                     = { "FractionDenominatorShiftDown",             "denom2" },
30                              cramped_display_style       = { "FractionDenominatorDisplayStyleShiftDown", "denom1" },
31                              display_style               = { "FractionDenominatorDisplayStyleShiftDown", "denom1" }, },
32    fraction_denom_vgap   = { default                     = { "FractionDenominatorGapMin",                "default_rule_thickness" },
33                              cramped_display_style       = { "FractionDenominatorDisplayStyleGapMin",    "3*default_rule_thickness" },
34                              display_style               = { "FractionDenominatorDisplayStyleGapMin",    "3*default_rule_thickness" }, },
35    fraction_num_up       = { default                     = { "FractionNumeratorShiftUp",                 "num2" },
36                              cramped_display_style       = { "FractionNumeratorDisplayStyleShiftUp",     "num1" },
37                              display_style               = { "FractionNumeratorDisplayStyleShiftUp",     "num1" }, },
38    fraction_num_vgap     = { default                     = { "FractionNumeratorGapMin",                  "default_rule_thickness" },
39                              cramped_display_style       = { "FractionNumeratorDisplayStyleGapMin",      "3*default_rule_thickness" },
40                              display_style               = { "FractionNumeratorDisplayStyleGapMin",      "3*default_rule_thickness" }, },
41    skewed_fraction_hgap  = { default                     = { "SkewedFractionHorizontalGap",              "math_quad/2" },
42                              cramped_display_style       = { "SkewedFractionHorizontalGap",              "math_quad/2" },
43                              display_style               = { "SkewedFractionHorizontalGap",              "math_quad/2" }, },
44    skewed_fraction_vgap  = { default                     = { "SkewedFractionVerticalGap",                "x_height" },
45                              cramped_display_style       = { "SkewedFractionVerticalGap",                "x_height" },
46                              display_style               = { "SkewedFractionVerticalGap",                "x_height" }, },
47    fraction_rule         = { default                     = { "FractionRuleThickness",                    "default_rule_thickness" }, },
48    limit_above_bgap      = { default                     = { "UpperLimitBaselineRiseMin",                "big_op_spacing3" }, },
49    limit_above_vgap      = { default                     = { "UpperLimitGapMin",                         "big_op_spacing1" }, },
50    limit_above_kern      = { default                     = { "0",                                        "big_op_spacing5" }, },
51    limit_below_bgap      = { default                     = { "LowerLimitBaselineDropMin",                "big_op_spacing4" }, },
52    limit_below_vgap      = { default                     = { "LowerLimitGapMin",                         "big_op_spacing2" }, },
53    limit_below_kern      = { default                     = { "0",                                        "big_op_spacing5" }, },
54    math_operator_size    = { default                     = { "DisplayOperatorMinHeight",                 "math_x_height*3" }, }, -- 2
55    overbar_kern          = { default                     = { "OverbarExtraAscender",                     "default_rule_thickness" }, },
56    overbar_rule          = { default                     = { "OverbarRuleThickness",                     "default_rule_thickness" }, },
57    overbar_vgap          = { default                     = { "OverbarVerticalGap",                       "3*default_rule_thickness" }, },
58    quad                  = { default                     = { "font_size(f)",                             "math_quad" }, },
59    radical_kern          = { default                     = { "RadicalExtraAscender",                     "default_rule_thickness" }, },
60    radical_rule          = { default                     = { "RadicalRuleThickness",                     "default_rule_thickness" }, },
61                           -- default                     = { "surd_height(f)",                           "default_rule_thickness" },
62    radical_vgap          = { default                     = { "RadicalVerticalGap",                       "default_rule_thickness+(abs(default_rule_thickness)/4)" },
63                              display_style               = { "RadicalDisplayStyleVerticalGap",           "default_rule_thickness+(abs(math_x_height)/4)" }, },
64    space_after_script    = { default                     = { "SpaceAfterScript",                         "script_space" }, },
65    space_before_script   = { default                     = { "SpaceAfterScript",                         "script_space" }, },
66    stack_denom_down      = { default                     = { "StackBottomShiftDown",                     "denom2" },
67                              cramped_display_style       = { "StackBottomDisplayStyleShiftDown",         "denom1" },
68                              display_style               = { "StackBottomDisplayStyleShiftDown",         "denom1" }, },
69    stack_num_up          = { default                     = { "StackTopShiftUp",                          "num3" },
70                              cramped_display_style       = { "StackTopDisplayStyleShiftUp",              "num1" },
71                              display_style               = { "StackTopDisplayStyleShiftUp",              "num1" }, },
72    stack_vgap            = { default                     = { "StackGapMin",                              "3*default_rule_thickness" },
73                              cramped_display_style       = { "StackDisplayStyleGapMin",                  "7*default_rule_thickness" },
74                              display_style               = { "StackDisplayStyleGapMin",                  "7*default_rule_thickness" }, },
75    sub_shift_down        = { default                     = { "SubscriptShiftDown",                       "sub1" }, },
76    sub_shift_drop        = { default                     = { "SubscriptBaselineDropMin",                 "sub_drop" }, },
77    sub_sup_shift_down    = { default                     = { "SubscriptShiftDown",                       "sub2" }, },
78    sub_top_max           = { default                     = { "SubscriptTopMax",                          "abs(math_x_height*4)/5" }, },
79    subsup_vgap           = { default                     = { "SubSuperscriptGapMin",                     "4*default_rule_thickness" }, },
80    sup_bottom_min        = { default                     = { "SuperscriptBottomMin",                     "abs(math_x_height)/4" }, },
81    sup_shift_drop        = { default                     = { "SuperscriptBaselineDropMax",               "sup_drop" }, },
82    sup_shift_up          = { cramped_display_style       = { "SuperscriptShiftUpCramped",                "sup3" },
83                              cramped_script_script_style = { "SuperscriptShiftUpCramped",                "sup3" },
84                              cramped_script_style        = { "SuperscriptShiftUpCramped",                "sup3" },
85                              cramped_text_style          = { "SuperscriptShiftUpCramped",                "sup3" },
86                              display_style               = { "SuperscriptShiftUp",                       "sup1" },
87                              script_script_style         = { "SuperscriptShiftUp",                       "sup2" },
88                              script_style                = { "SuperscriptShiftUp",                       "sup2" },
89                              text_style                  = { "SuperscriptShiftUp",                       "sup2" }, },
90    sup_sub_bottom_max    = { default                     = { "SuperscriptBottomMaxWithSubscript",        "abs(math_x_height*4)/5" }, },
91    underbar_kern         = { default                     = { "UnderbarExtraDescender",                   "0" }, },
92    underbar_rule         = { default                     = { "UnderbarRuleThickness",                    "default_rule_thickness" }, },
93    underbar_vgap         = { default                     = { "UnderbarVerticalGap",                      "3*default_rule_thickness" }, },
94    connector_overlap_min = { default                     = { "MinConnectorOverlap",                      "0.25*default_rule_thickness" }, },
95    over_delimiter_vgap   = { default                     = { "StretchStackGapBelowMin",                  "big_op_spacing1" }, },
96    over_delimiter_bgap   = { default                     = { "StretchStackTopShiftUp",                   "big_op_spacing3" }, },
97    under_delimiter_vgap  = { default                     = { "StretchStackGapAboveMin",                  "big_op_spacing2" }, },
98    under_delimiter_bgap  = { default                     = { "StretchStackBottomShiftDown",              "big_op_spacing4" }, },
99    radical_degree_before = { default                     = { "RadicalKernBeforeDegree",                  "(5/18)*quad" }, },
100    radical_degree_after  = { default                     = { "RadicalKernAfterDegree",                   "(-10/18)*quad" }, },
101    radical_degree_raise  = { default                     = { "RadicalDegreeBottomRaisePercent",          "60" }, },
102    no_limit_sub_factor   = { default                     = { "NoLimitSubFactor",                         "0" }, },
103    no_limit_sup_factor   = { default                     = { "NoLimitSupFactor",                         "0" }, },
104}
105
106local styles = {
107    'cramped_display_style',
108    'cramped_script_script_style',
109    'cramped_script_style',
110    'cramped_text_style',
111    'display_style',
112    'script_script_style',
113    'script_style',
114    'text_style',
115}
116
117for k, v in next, defaults do
118    for _, s in next, styles do
119        if not v[s] then
120            v[s] = v.default
121        end
122    end
123end
124
125-- we cannot use a metatable because we do a copy (takes a bit more work)
126--
127-- local mt = { }  setmetatable(defaults,mt)
128--
129-- mt.__index = function(t,s)
130--     return t.default or t.text_style or 0
131-- end
132
133function mathematics.dimensions(dimens)  -- beware, dimens get spoiled
134    if dimens.SpaceAfterScript then
135        return table.fastcopy(dimens), { }
136    elseif dimens.AxisHeight or dimens.axis_height then
137        local t = { }
138        local math_x_height = dimens.x_height or 10*65536
139        local math_quad = dimens.quad or 10*65536
140        local default_rule_thickness = dimens.FractionDenominatorGapMin or dimens.default_rule_thickness or 0.4*65536
141        dimens["0"] = 0
142        dimens["60"] = 60
143        dimens["0.25*default_rule_thickness"] = default_rule_thickness / 4
144        dimens["3*default_rule_thickness"] = 3 * default_rule_thickness
145        dimens["4*default_rule_thickness"] = 4 * default_rule_thickness
146        dimens["7*default_rule_thickness"] = 7 * default_rule_thickness
147        dimens["(5/18)*quad"] = (math_quad * 5) / 18
148        dimens["(-10/18)*quad"] = - (math_quad * 10) / 18
149        dimens["math_x_height*3"] = math_x_height * 3 -- needs checking
150        dimens["abs(math_x_height*4)/5"] = abs(math_x_height * 4) / 5
151        dimens["default_rule_thickness+(abs(default_rule_thickness)/4)"] = default_rule_thickness+(abs(default_rule_thickness) / 4)
152        dimens["default_rule_thickness+(abs(math_x_height)/4)"] = default_rule_thickness+(abs(math_x_height) / 4)
153        dimens["abs(math_x_height)/4"] = abs(math_x_height) / 4
154        dimens["abs(math_x_height*4)/5"] = abs(math_x_height * 4) / 5
155        dimens["<not set>"] = false
156        dimens["script_space"] = false -- at macro level
157        for variable, styles in next, defaults do
158            local tt = { }
159            for style, default in next, styles do
160                local one   = default[1]
161                local two   = default[2]
162                local value = dimens[one]
163                if value then
164                    tt[style] = value
165                else
166                    value = dimens[two]
167                    if value == false then
168                        tt[style] = nil
169                    else
170                        tt[style] = value or 0
171                    end
172                end
173            end
174            t[variable] = tt
175        end
176        local d = {
177            AccentBaseHeight                            = t . accent_base_height    . text_style,
178            AxisHeight                                  = t . axis                  . text_style,
179         -- DelimitedSubFormulaMinHeight
180            DisplayOperatorMinHeight                    = t . math_operator_size    . text_style, -- no longer let tex decide (weird values)
181         -- FlattenedAccentBaseHeight
182            FractionDenominatorDisplayStyleGapMin       = t . fraction_denom_vgap   . display_style,
183            FractionDenominatorDisplayStyleShiftDown    = t . fraction_denom_down   . display_style,
184            FractionDenominatorGapMin                   = t . fraction_denom_vgap   . text_style,
185            FractionDenominatorShiftDown                = t . fraction_denom_down   . text_style,
186            FractionNumeratorDisplayStyleGapMin         = t . fraction_num_vgap     . display_style,
187            FractionNumeratorDisplayStyleShiftUp        = t . fraction_num_up       . display_style,
188            FractionNumeratorGapMin                     = t . fraction_num_vgap     . text_style,
189            FractionNumeratorShiftUp                    = t . fraction_num_up       . text_style,
190            FractionRuleThickness                       = t . fraction_rule         . text_style,
191            FractionDelimiterSize                       = t . fraction_del_size     . text_style,
192            FractionDelimiterDisplayStyleSize           = t . fraction_del_size     . display_style,
193            LowerLimitBaselineDropMin                   = t . limit_below_bgap      . text_style,
194            LowerLimitGapMin                            = t . limit_below_vgap      . text_style,
195         -- MathLeading
196            MinConnectorOverlap                         = t . connector_overlap_min . text_style,
197            OverbarExtraAscender                        = t . overbar_kern          . text_style,
198            OverbarRuleThickness                        = t . overbar_rule          . text_style,
199            OverbarVerticalGap                          = t . overbar_vgap          . text_style,
200            RadicalDisplayStyleVerticalGap              = t . radical_vgap          . display_style,
201            RadicalExtraAscender                        = t . radical_kern          . text_style,
202            RadicalRuleThickness                        = t . radical_rule          . text_style,
203            RadicalVerticalGap                          = t . radical_vgap          . text_style,
204            RadicalKernBeforeDegree                     = t . radical_degree_before . display_style,
205            RadicalKernAfterDegree                      = t . radical_degree_after  . display_style,
206            RadicalDegreeBottomRaisePercent             = t . radical_degree_raise  . display_style,
207         -- ScriptPercentScaleDown
208         -- ScriptScriptPercentScaleDown
209         -- SkewedFractionHorizontalGap
210         -- SkewedFractionVerticalGap
211            SpaceAfterScript                            = t . space_after_script    . text_style,
212            StackBottomDisplayStyleShiftDown            = t . stack_denom_down      . display_style,
213            StackBottomShiftDown                        = t . stack_denom_down      . text_style,
214            StackDisplayStyleGapMin                     = t . stack_vgap            . display_style,
215            StackGapMin                                 = t . stack_vgap            . text_style,
216            StackTopDisplayStyleShiftUp                 = t . stack_num_up          . display_style,
217            StackTopShiftUp                             = t . stack_num_up          . text_style,
218            StretchStackGapBelowMin                     = t . over_delimiter_vgap   . text_style,
219            StretchStackTopShiftUp                      = t . over_delimiter_bgap   . text_style,
220            StretchStackGapAboveMin                     = t . under_delimiter_vgap  . text_style,
221            StretchStackBottomShiftDown                 = t . under_delimiter_bgap  . text_style,
222            SubSuperscriptGapMin                        = t . subsup_vgap           . text_style,
223            SubscriptBaselineDropMin                    = t . sub_shift_drop        . text_style,
224            SubscriptShiftDown                          = t . sub_shift_down        . text_style,
225            SubscriptShiftDownWithSuperscript           = t . sub_sup_shift_down    . text_style,
226            SubscriptTopMax                             = t . sub_top_max           . text_style,
227            SuperscriptBaselineDropMax                  = t . sup_shift_drop        . text_style,
228            SuperscriptBottomMaxWithSubscript           = t . sup_sub_bottom_max    . text_style,
229            SuperscriptBottomMin                        = t . sup_bottom_min        . text_style,
230            SuperscriptShiftUp                          = t . sup_shift_up          . text_style,
231            SuperscriptShiftUpCramped                   = t . sup_shift_up          . cramped_text_style,
232            UnderbarExtraDescender                      = t . underbar_kern         . text_style,
233            UnderbarRuleThickness                       = t . underbar_rule         . text_style,
234            UnderbarVerticalGap                         = t . underbar_vgap         . text_style,
235            UpperLimitBaselineRiseMin                   = t . limit_above_bgap      . text_style,
236            UpperLimitGapMin                            = t . limit_above_vgap      . text_style,
237        }
238
239        -- too fragile for tx/px ... even the same values give different results
240        d.DisplayOperatorMinHeight = nil
241        --
242        d.AccentBaseHeight = 0 -- here? still?
243        return d, t -- t only for diagnostics
244    else
245        return { }, { }
246    end
247end
248
249