%% This is part of the OpTeX project, see http://petr.olsak.net/optex

\_codedecl \normalmath {Math fonts CM + AMS preloaded <2022-12-01>} % preloaded in format

  \_doc ------------------------------
  We have two math macros \`\normalmath` for the normal shape of all math
  symbols and \`\boldmath` for the bold shape of all math symbols. The second one
  can be used in bold titles, for example.
  These macros load all fonts from all given math font families.
  \_cod ------------------------------

\_def\_normalmath{%
  \_loadmathfamily 0 cmr  % CM Roman
  \_loadmathfamily 1 cmmi % CM Math Italic
  \_loadmathfamily 2 cmsy % CM Standard symbols
  \_loadmathfamily 3 cmex % CM extra symbols
  \_loadmathfamily 4 msam % AMS symbols A
  \_loadmathfamily 5 msbm % AMS symbols B
  \_loadmathfamily 6 rsfs % script
  \_loadmathfamily 7 eufm % fractur
  \_loadmathfamily 8 bfsans % sans serif bold
  \_loadmathfamily 9 bisans % sans serif bold slanted (for vectors)
%  \_setmathfamily 10 \_tentt
%  \_setmathfamily 11 \_tenit
  \_setmathdimens
}
\_def\_boldmath{%
  \_loadmathfamily 0 cmbx  % CM Roman Bold Extended
  \_loadmathfamily 1 cmmib % CM Math Italic Bold
  \_loadmathfamily 2 cmbsy % CM Standard symbols Bold
  \_loadmathfamily 3 cmexb % CM extra symbols Bold
  \_loadmathfamily 4 msam  % AMS symbols A (bold not available?)
  \_loadmathfamily 5 msbm  % AMS symbols B (bold not available?)
  \_loadmathfamily 6 rsfs  % script (bold not available?)
  \_loadmathfamily 7 eufb  % fractur bold
  \_loadmathfamily 8 bbfsans % sans serif extra bold
  \_loadmathfamily 9 bbisans % sans serif extra bold slanted (for vectors)
%  \_setmathfamily 10 \_tentt
%  \_setmathfamily 11 \_tenbi
  \_setmathdimens
}
\_def \normalmath {\_normalmath}  \_def\boldmath {\_boldmath}

   \_doc -----------------------------
   The classical math family selectors
   \`\mit`, \^`\cal`, \^`\bbchar`, \^`\frak` and \^`\script`
   are defined here.
   The \^`\rm`, \^`\bf`, \^`\it`, \^`\bi` and \^`\tt` does two things:
   they are variant selectors for text fonts and math family selectors for math fonts.
   The idea was adapted from plain \TeX/.\nl
   These macros are redefined when `unimat-codes.opm` is loaded,
   see the section~\ref[unimath-codes].
   \_cod -----------------------------

\_chardef\_bffam = 8
\_chardef\_bifam = 9
%\_chardef\_ttfam = 10
%\_chardef\_itfam = 11

\_protected\_def \_marm {\_fam0 }
\_protected\_def \_mabf {\_fam\_bffam}
\_protected\_def \_mait {\_fam1 }
\_protected\_def \_mabi {\_fam\_bifam}
\_protected\_def \_matt {}

\_protected\_def \_mit    {\_fam1 }
\_protected\_def \_cal    {\_fam2 }
\_protected\_def \_bbchar {\_fam5 }  % double stroked letters
\_protected\_def \_frak   {\_fam7 }  % fraktur
\_protected\_def \_script {\_fam6 }  % more extensive script than \cal

\_public \mit \cal \bbchar \frak \script ;

   \_doc -----------------------------
   The optical sizes of Computer Modern fonts, AMS, and other fonts
   are declared here.
   \_cod -----------------------------

%% CM math fonts, optical sizes:

\_regtfm cmmi 0 cmmi5 5.5 cmmi6 6.5 cmmi7 7.5 cmmi8 8.5 cmmi9 9.5
                cmmi10 11.1 cmmi12 *
\_regtfm cmmib 0 cmmib5 5.5 cmmib6 6.5 cmmib7 7.5 cmmib8 8.5 cmmib9 9.5 cmmib10 *
\_regtfm cmtex 0 cstex8 8.5 cstex9 9.5 cstex10 *
\_regtfm cmsy 0 cmsy5 5.5 cmsy6 6.5 cmsy7 7.5 cmsy8 8.5 cmsy9 9.5 cmsy10 *
\_regtfm cmbsy 0 cmbsy5 5.5 cmbsy6 6.5 cmbsy7 7.5 cmbsy8 8.5 cmbsy9 9.5 cmbsy10 *
\_regtfm cmex 0 cmex7 7.5 cmex8 8.5 cmex9 9.5 cmex10 *
\_regtfm cmexb 0 cmexb10 *

\_regtfm cmr  0 cmr5 5.5 cmr6 6.5 cmr7 7.5 cmr8 8.5 cmr9 9.5
                cmr10 11.1 cmr12 15 cmr17 *
\_regtfm cmbx 0 cmbx5 5.5 cmbx6 6.5 cmbx7 7.5 cmbx8 8.5 cmbx9 9.5
                cmbx10 11.1 cmbx12 *
\_regtfm cmti 0 cmti7 7.5 cmti8 8.5 cmti9 9.5 cmti10 11.1 cmti12 *
\_regtfm cmtt 0 cmtt8 8.5 cmtt9 9.5 cmtt10 11.1 cmtt12 *

%% AMS math fonts, optical sizes:

\_regtfm msam 0 msam5 5.5 msam6 6.5 msam7 7.5 msam8 8.5 msam9 9.5 msam10 *
\_regtfm msbm 0 msbm5 5.5 msbm6 6.5 msbm7 7.5 msbm8 8.5 msbm9 9.5 msbm10 *

%% fraktur, rsfs, optical sizes:

\_regtfm eufm 0 eufm5 6 eufm7 8.5 eufm10 *
\_regtfm eufb 0 eufb5 6 eufb7 8.5 eufb10 *
\_regtfm rsfs 0 rsfs5 6 rsfs7 8.5 rsfs10 *

%% bf and bi sansserif math alternatives:

\_regtfm bfsans 0 ecsx0500 5.5 ecsx0600 6.5 ecsx0700 7.5 ecsx0800
             8.5 ecsx0900 9.5 ecsx1000 11.1 ecsx1200 *
\_regtfm bisans 0 ecso0500 5.5 ecso0600 6.5 ecso0700 7.5 ecso0800
             8.5 ecso0900 9.5 ecso1000 11.1 ecso1200 *
\_regtfm bbfsans 0 ecsx0500 5.5 ecsx0600 6.5 ecsx0700 7.5 ecsx0800
             8.5 ecsx0900 9.5 ecsx1000 11.1 ecsx1200 *
\_regtfm bbisans 0 ecso0500 5.5 ecso0600 6.5 ecso0700 7.5 ecso0800
             8.5 ecso0900 9.5 ecso1000 11.1 ecso1200 *

   \_doc -----------------------------
   \`\_loadmathfamily` `<number> <font>` loads one math family, i.\,e.\
   the triple of fonts in the text size, script size and script-script size.
   The `<font>` is `<font-id>` used in the `\_regtfm` parameter or
   the real TFM name. The family is saved as `\fam<number>`.
   \nl
   \`\_setmathfamily` `<number> \<font-switch>` loads one math family
   like `\_loadmathfamily` does it. But the second parameter is a
   `\<font-switch>` declared previously by the `\font` primitive.
   \nl
   The `<number>` is saved by \^`\_loadmathfamily`, \^`\_setmathfamily`
   to the \`\_mfam`.
   \nl
   The font family is loaded at \`\_sizemtext`, \`\_sizemscript` and
   \`\_sizemsscript` sizes. These sizes are set by the
   \`\setmathsizes` `[<text-size>/<script-size>/<scriptscript-size>]` macro.
   These parameters are given in the \`\ptmunit` unit, it is set to
   1\`\ptunit` and it is set to 1\,pt by  default.
   \nl
   \`\_mfactor` sets scaling factor for given math fonts family
   related to text font size. It does the setting `\_ptmunit=<factor>\_ptunit` where
   the <factor> is defined by `\sdef{_mfactor:<family>}{<factor>}`.
   For eample, you can set `\sdef{_mfactor:1}{0.95}` if you found that
   this scaling of math family 1 gives better visual compatibility
   with used text fonts. If not declared then scaling factor is~1.
   \_cod -----------------------------

\_def\_loadmathfamily {\_afterassignment\_loadmathfamilyA \_chardef\_mfam}
\_def\_loadmathfamilyA #1 {\_mfactor
  \_edef\_optsizesave{\_the\_optsize}%
  \_optsize=\_sizemtext    \_font\_mF \_optfn{#1} at\_optsize \_textfont\_mfam=\_mF
  \_optsize=\_sizemscript  \_font\_mF \_optfn{#1} at\_optsize \_scriptfont\_mfam=\_mF
  \_optsize=\_sizemsscript \_font\_mF \_optfn{#1} at\_optsize \_scriptscriptfont\_mfam=\_mF
  \_optsize=\_optsizesave
}
\_def\_setmathfamily {\_afterassignment\_setmathfamilyA \_chardef\_mfam}
\_def\_setmathfamilyA #1{\_mfactor \_let\_mF=#1%
  \_edef\_optsizesave{\_the\_optsize}%
  \_optsize=\_sizemtext    \_fontlet#1#1at\_optsize \_textfont\_mfam=#1%
  \_optsize=\_sizemscript  \_fontlet#1#1at\_optsize \_scriptfont\_mfam=#1%
  \_optsize=\_sizemsscript \_fontlet#1#1at\_optsize \_scriptscriptfont\_mfam=#1%
  \_optsize=\_optsizesave \_let#1=\_mF
}
\_def\_setmathsizes[#1/#2/#3]{\_ptmunit=\_ptunit
   \_def\_sizemtext{#1\_ptmunit}\_def\_sizemscript{#2\_ptmunit}%
   \_def\_sizemsscript{#3\_ptmunit}%
}
\_def\_mfactor{\_ptmunit=\_trycs{_mfactor:\_the\_mfam}{}\_ptunit}

\_newdimen\_ptunit   \_ptunit=1pt
\_newdimen\_ptmunit  \_ptmunit=1\_ptunit

\_public \setmathsizes \ptunit ;

   \_doc -----------------------------
   \`\_setmathparam``<luatex-param> {<factor>}` sets
   <luatex-param> (like `\Umathspaceafterscript`) to values dependent
   on `1em` of textfont, scriptfont, scriptscriptfont. The <factor> is
   scaling factor of mentioned `1em`.
   \_cod -----------------------------

\_def\_setmathparam#1#2{%
   #1\_displaystyle             =#2\_fontdimen6\_textfont1
   #1\_crampeddisplaystyle      =#2\_fontdimen6\_textfont1
   #1\_textstyle                =#2\_fontdimen6\_textfont1
   #1\_crampedtextstyle         =#2\_fontdimen6\_textfont1
   #1\_scriptstyle              =#2\_fontdimen6\_scriptfont1
   #1\_crampedscriptstyle       =#2\_fontdimen6\_scriptfont1
   #1\_scriptscriptstyle        =#2\_fontdimen6\_scriptscriptfont1
   #1\_crampedscriptscriptstyle =#2\_fontdimen6\_scriptscriptfont1
}

   \_doc -----------------------------
   The \`\_setmathdimens` macro is used in \^`\normalmath` or \^`\boldmath`
   macros. It makes math dimensions dependent on the font size (plain \TeX/ sets
   them only for 10\,pt typesetting). The `\skewchar` of some math families are
   set here too.\nl
   \^`\_setmathparam \Umathspaceafterscript` is used instead `\scriptspace`
   setting because \LuaTeX/ ingnores `\scriptspace` in most cases. There is
   small difference from classical \TeX/: we set \"scaled"
   `\Umathspaceafterscript` dependent on textstyle, scriptstyle, etc.\ sizes.
   The \`\_scriptspacefactor` is set to 0.05 which gives the same result
   as Plain \TeX/ `\scriptspace=0.5pt` at 10\,pt font size.
   \_cod -----------------------------

\_def\_setmathdimens{% PlainTeX sets these dimens for 10pt size only:
  \_delimitershortfall=0.5\_fontdimen6\_textfont3
  \_nulldelimiterspace=0.12\_fontdimen6\_textfont3
  \_setmathparam\_Umathspaceafterscript \_scriptspacefactor
  \_skewchar\_textfont1=127 \_skewchar\_scriptfont1=127
  \_skewchar\_scriptscriptfont1=127
  \_skewchar\_textfont2=48  \_skewchar\_scriptfont2=48
  \_skewchar\_scriptscriptfont2=48
  \_skewchar\_textfont6=127 \_skewchar\_scriptfont6=127
  \_skewchar\_scriptscriptfont6=127
}
\_def\_scriptspacefactor{.05}

   \_doc -----------------------------
   Finally, we preload a math fonts collection in [10/7/5] sizes
   when the format is generated. This is done when
   `\_suppressfontnotfounderror=1` because we need not errors when the format is
   generated. Maybe there are not all fonts in the \TeX/ distribution
   installed.
   \_cod -----------------------------

\_suppressfontnotfounderror=1
\_setmathsizes[10/7/5]
\_ifx\fontspreload\_relax \_else \_normalmath \_fi
\_suppressfontnotfounderror=0


\_endcode %---------------------------------------------------

The Computer Modern and AMS fonts are preloaded here in classical math-fam
concept, where each math family includes three fonts with max 256 characters
(typically 128 characters).

On the other hand, when \^`\fontfam` macro is used in the document then text
font family and appropriate math family is loaded with Unicode fonts, i.e.
Unicode-math is used. It re-defines all settings given here.

The general rule of usage the math fonts in different sizes in \OpTeX/ says: set three
sizes by the macro
\^`\setmathsizes` `[<text-size>/<script-size>/<scriptscript-size>]` and then
load all math fonts in given sizes by \^`\normalmath` or \^`\boldmath` macros.
For example
\begtt
\setmathsizes[12/8.4/6]\normalmath ... math typesetting at 12 pt is ready.
\endtt

\_endinput

2022-12-01  reading number by \_loadmathfamily, \_setmathfamily more robust
2022-11-16: \_corrmsize removed, \_mfactor introduced
2022-09-04: \crampedstyles setings added.
2022-07-22: \_setmathparam\_Umathspaceafterscript added.
2022-02-22: \rm, \it, etc moved to fonts-select, \_marm, \_mait etc. introduced
2020-05-06: \itfam, \ttfam removed, (Unicode math preferred)
2020-04-14: \skewchar\fam6 added
