summaryrefslogtreecommitdiff
path: root/macros/optex/base/math-unicode.opm
diff options
context:
space:
mode:
Diffstat (limited to 'macros/optex/base/math-unicode.opm')
-rw-r--r--macros/optex/base/math-unicode.opm266
1 files changed, 266 insertions, 0 deletions
diff --git a/macros/optex/base/math-unicode.opm b/macros/optex/base/math-unicode.opm
new file mode 100644
index 0000000000..96f9983380
--- /dev/null
+++ b/macros/optex/base/math-unicode.opm
@@ -0,0 +1,266 @@
+%% This is part of the OpTeX project, see http://petr.olsak.net/optex
+
+\_codedecl \loadmath {Unicode Math fonts <2021-03-09>} % preloaded in format
+
+ \_doc -----------------------------
+ \`\loadmath` `{<Unicode-math font>}` loads the given font. It does:
+ \begitems
+ * define \`\_unimathfont` as `<Unicode-math font>`,
+ * redefine `\normalmath` and `\boldmath` macros to their Unicode counterparts,
+ * load the `\_unimathfont` by `\normalmath`,
+ * print information about the loaded font on the terminal,
+ * redefine all encoding dependent setting by `\input unimath-codes.opm`,
+ * protect new loading by setting \`\_ifmathloading` to false.
+ \enditems
+ \`\noloadmath` disallows Unicode-math loading by \`\_mathloadingfalse`.\nl
+ \`\doloadmath` allows Unicode-math loading by \`\_mathloadingtrue`.
+ \_cod -----------------------------
+
+\_newifi \_ifmathloading \_mathloadingtrue
+
+\_def\_noloadmath{\_mathloadingfalse}
+\_def\_doloadmath{\_mathloadingtrue}
+
+\_def\_loadmath#1{%
+ \_ifmathloading
+ \_initunifonts
+ \_isfont{#1}\_iffalse
+ \_opwarning{Math font "#1" not found, skipped...}%
+ \_else
+ \_def\_unimathfont{#1}%
+ \_let\_normalmath = \_normalunimath \_let\_boldmath = \_boldunimath
+ \_normalmath
+ \_wterm {MATH-FONT: "#1" -- unicode math prepared.}%
+ \_ifx\_ncharrmA\_undefined \_opinput {unimath-codes.opm}\_fi
+ \_mathloadingfalse
+ \_fi\_fi}
+
+\_public \loadmath \noloadmath \doloadmath ;
+
+ \_doc -----------------------------
+ \`\loadboldmath` `{<bold-font>} \to {<normal-font>}`
+ defines \`\_unimathboldfont` as `<bold-font>` only if `\_unimathfont` is
+ defined as `<normal-font>`. It is used when \^`\boldmath` macro is run.
+ When no `\_unimathboldfont` is defined then the `\boldmath` macro
+ use \"fake bold" generated by `embolden` \LuaTeX/ font feature.
+ \_cod -----------------------------
+
+\_def\_loadboldmath#1#2\to #3{%
+ \_def\_tmp{#3}\_ifx\_unimathfont\_tmp % do work only if #3 is loaded as normal Math
+ \_isfont{#1}\_iffalse
+ \_opwarning{Bold-Math font "#1" not found, skipped...}
+ \_else
+ \_def\_unimathboldfont{#1}%
+ \_wterm {MATH-FONT: "#1" -- unicode math bold prepared.}%
+ \_fi\_fi}
+
+\_public \loadboldmath ;
+
+ \_doc -----------------------------
+ The Unicode version of the \^`\normalmath` and \^`\boldmath` macros
+ are defined here as \`\_normalunimath` and \`\_boldunimath` macros.
+ They are using \`\_setunimathdimens` in a similar sense as
+ \^`\_setmathdimens`.
+ \nl
+ You can combine more fonts if you register them to another
+ math families (5, 6, 7, etc.) in the \^`\normalmath` macro.
+ \nl
+ The default value of \^`\_normalunimath` shows a combination of base Unicode-math
+ font with 8bit Math font at family 4. See definition of `\script` macro where
+ `\fam4` is used.
+ \_cod -----------------------------
+
+\_def\_normalunimath{%
+ \_loadumathfamily 1 {\_unimathfont}{} % Base font
+ \_loadmathfamily 4 rsfs % script
+ \_setunimathdimens
+}%
+\_def\_boldunimath{%
+ \_ifx\_unimathboldfont \_undefined
+ \_loadumathfamily 1 {\_unimathfont}{embolden=1.7;} % Base faked bold
+ \_else
+ \_loadumathfamily 1 {\_unimathboldfont}{} % Base real bold font
+ \_fi
+ \_loadmathfamily 4 rsfs % script
+ \_setunimathdimens
+}%
+\_def\_setunimathdimens{% PlainTeX sets these dimens for 10pt size only:
+ \_delimitershortfall=0.5\_fontdimen6\_textfont3
+ \_nulldelimiterspace=0.12\_fontdimen6\_textfont3
+ \_scriptspace=0.05\_fontdimen6\_textfont3
+ \_begingroup % correction for \choose
+ \_everymath{}\_global\_setbox0=\_hbox{$\_fam1\_displaystyle{0\_atop0}$}\_endgroup
+ \_Umathfractiondelsize\_displaystyle = \_dimexpr(\_ht0-\_Umathaxis\_displaystyle)*2\_relax
+}
+
+ \_doc -----------------------------
+ If you try the example above about
+ \~`\loadboldmath``{[xitsmath-bold]} \to {[xitsmath-regular]}`
+ then you can find a bug in XITSMath-Bold font: the symbols for norm
+ $\|x\|$ are missing. So, we have to define `\_boldmath` macro manually.
+ The missing symbol is loaded from family 5 as no-bold variant in our example:
+ \begtt
+ \loadmath{[xitsmath-regular]}
+ \def\_boldmath{%
+ \_loadumathfamily 1 {[xitsmath-bold]}{} % Base font
+ \_loadmathfamily 4 rsfs % script
+ \_loadumathfamily 5 {[xitsmath-regular]}{}
+ \_def\|{\_Udelimiter 0 5 "02016 }% % norm delimiter from family 5
+ \_setmathdimens
+ }
+ \endtt
+
+ \`\_loadumathfamily` `<number> {<font>}{<font features>}`
+ loads the given Unicode-math fonts in three sizes given by the
+ \^`\setmathsizes` macro and sets it as the math family `<number>`.
+ The `<font features>` are added to the default
+ \`\_mfontfeatures` and to the size-dependent features `+ssty=0`
+ if script size is asked or `+ssty=1` if scriptscriptsize is asked.
+ If the math family 1 is loaded then the family 2 and 3 are set by the same
+ font because \TeX/ needs to read dimension information about generating
+ math formulae from these three math families. All information needed by
+ \TeX/ is collected in single Unicode-math font.\nl
+ The \^`\_corrmsize` `<factor><space>` can be used just before
+ `\_loadumathfamily`, see section~\ref[math-preload] for more information.\nl
+ The \`\_textmff`, \`\_scriptmff` and \`\_sscriptmff` are additional font
+ features for text, script and sscript sizes respectively. They are
+ locally re-defined in \^`\mathbox` macro.
+ \_cod -----------------------------
+
+\_def\_umathname#1#2{"#1:\_mfontfeatures#2"}
+\_def\_mfontfeatures{mode=base;script=math;}
+
+\_def\_loadumathfamily #1 #2#3 {%
+ \_edef\_optsizesave{\_the\_optsize}%
+ \_optsize=\_sizemtext \_font\_mF=\_umathname{#2}{\_textmff #3} at\_optsize
+ \_textfont#1=\_mF \_ifnum#1=1 \_textfont2=\_mF \_textfont3=\_mF \_fi
+ \_optsize=\_sizemscript \_font\_mF=\_umathname{#2}{\_scriptmff #3} at\_optsize
+ \_scriptfont#1=\_mF \_ifnum#1=1 \_scriptfont2=\_mF \_scriptfont3=\_mF \_fi
+ \_optsize=\_sizemsscript \_font\_mF=\_umathname{#2}{\_sscriptmff #3} at\_optsize
+ \_scriptscriptfont#1=\_mF \_ifnum#1=1 \_scriptscriptfont2=\_mF \_scriptscriptfont3=\_mF \_fi
+ \_optsize=\_optsizesave \_ptmunit=\_ptunit
+}
+\_def\_textmff{} \_def\_scriptmff{+ssty=0;} \_def\_sscriptmff{+ssty=1;}
+
+ \_doc -----------------------------
+ Unicode math font includes all typical math alphabets together, user needs not to
+ load more \TeX/ math families. These math alphabets are encoded by
+ different parts of Unicode table. We need auxiliary macros for setting
+ mathcodes by selected math alphabet.
+ \nl
+ \`\_umathrange` `{<from->-<to>}<class><family>\<first>` sets `\Umathcode`s
+ of the characters in the interval `<from>-<to>` to `\<first>`,
+ `\<first>+1`, `\<first>+2`
+ etc., but \`\_umathcharholes` are skipped
+ (`\_umathcharholes` are parts of the Unicode table not designed for math
+ alphabets but they cause that the math alphabets are
+ not continuously spread out in the table; I mean that the
+ designers were under the influence of drugs when they created
+ this part of the Unicode table).
+ The `<from>-<to>` clause includes normal letters like `A-Z`.
+ \nl
+ \`\_umahrangegreek` `\<first>` is the same as
+ `\_umathrange {<alpha>-<omega>}\<first>`.
+ \nl
+ \`\_umahrangeGREEK` `\<first>` is the same as
+ `\_umathrange {<Alpha>-<Omega>}\<first>`.
+ \nl
+ \`\_greekdef` `<control sequences> \_relax` defines each control sequence
+ as a normal character with codes `\_umathnumB`, `\_umathnumB+1`,
+ `\_umathnumB+2` etc. It is used for redefinig the contol sequences for
+ math Greek `\alpha`, `\beta`, `\gamma` etc.
+ \_cod -----------------------------
+
+\_newcount\_umathnumA \_newcount\_umathnumB
+
+\_def\_umathcorr#1#2{\_ea#1\_ea{\_the#2}}
+\_def\_umathprepare#1{\_def\_umathscanholes##1[#1]##2##3\_relax{##2}}
+\_def\_umathvalue#1{\_ea\_umathscanholes\_umathcharholes[#1]{#1}\_relax}
+
+\_def\_umathcharholes{% holes in math alphabets:
+ [119893]{"210E}[119965]{"212C}[119968]{"2130}[119969]{"2131}%
+ [119971]{"210B}[119972]{"2110}[119975]{"2112}[119976]{"2133}[119981]{"211B}%
+ [119994]{"212F}[119996]{"210A}[120004]{"2134}%
+ [120070]{"212D}[120075]{"210C}[120076]{"2111}[120085]{"211C}[120093]{"2128}%
+ [120122]{"2102}[120127]{"210D}[120133]{"2115}[120135]{"2119}
+ [120136]{"211A}[120137]{"211D}[120145]{"2124}%
+}
+\_def\_umathrange#1#2#3#4{\_umathnumB=#4\_def\_tmp{#2 #3 }\_umathrangeA#1}
+\_def\_umathrangeA#1-#2{\_umathnumA=`#1\_relax
+ \_loop
+ \_umathcorr\_umathprepare\_umathnumB
+ \_Umathcode \_umathnumA = \_tmp \_umathcorr\_umathvalue{\_umathnumB}
+ \_ifnum\_umathnumA<`#2\_relax
+ \_advance\_umathnumA by1 \_advance\_umathnumB by1
+ \_repeat
+}
+\_def\_umathrangeGREEK{\_umathrange{^^^^0391-^^^^03a9}}
+\_def\_umathrangegreek{\_umathrange{^^^^03b1-^^^^03d6}}
+\_def\_greekdef#1{\_ifx#1\_relax \_else
+ \_begingroup \_lccode`X=\_umathnumB \_lowercase{\_endgroup \_def#1{X}}%
+ \_advance\_umathnumB by 1
+ \_ea\_greekdef \_fi
+}
+
+\_endcode
+
+
+The \^`\loadmath` `{<Unicode-math font>}` macro loads math fonts and
+redefines all default math-codes using `\input unimath-codes.opm`.
+If Unicode-math font is loaded then \^`\_mathloadingfalse`
+is set, so the new Unicode-math font isn't loaded until \^`\doloadmath` is used.
+
+\^`\loadboldmath` `{<bold-font>} \to {<normal-font>}` loads bold variant only
+if `<normal-font>` was sucessully loaded by the previous `\loadmath`. For example:
+
+\begtt
+\loadmath {[xitsmath-regular]}
+\loadboldmath {[xitsmath-bold]} \to {[xitsmath-regular]}
+\endtt
+There are very few Unicode-math fonts with full \^`\boldmath` support.
+I know only XITSMath-Bold and KpMath-Bold. If \^`\loadboldmath` is not used
+then \"faked bold" created from \^`\normalmath` is used by default.
+
+The \^`\loadmath` macro was succesfully tested on:
+
+\begtt
+\loadmath{[XITSMath-Regular]} ... XITS MATH
+\loadmath{[latinmodern-math]} ... Latin Modern Math
+\loadmath{[texgyretermes-math]} ... TeXGyre Termes Math
+\loadmath{[texgyrebonum-math]} ... TeXGyre Bonum Math
+\loadmath{[texgyrepagella-math]} ... TeXGyre Pagella Math
+\loadmath{[texgyreschola-math]} ... TeXGyre Schola Math
+\loadmath{[texgyredejavu-math]} ... TeXGyre DeJaVu Math
+\loadmath{[LibertinusMath-Regular]} ... Libertinus Math
+\loadmath{[FiraMath-Regular]} ... Fira Math
+\loadmath{[Asana-Math]} ... Asana Math
+\loadmath{[KpMath-Regular]} ... KP fonts Math
+\endtt
+
+\secc Unicode-math macros preloaded in the format
+
+\printdoc math-unicode.opm
+
+
+\secc[unimath-codes] Macros and codes set when \code{\\loadmatfont} is processed
+
+The file `unimath-codes.opm` is loaded when the \^`\loadmath` is used. The
+macros here redefines globally all encoding dependent settings declared in
+the section~\ref[math-macros].
+
+\printdoc unimath-codes.opm
+\printdoctail unimath-codes.opm
+
+\secc Printing all Unicode math slots in used math font
+
+\printdoctail print-unimath.opm
+\printdoc print-unimath.opm
+
+\_endinput
+
+2021-03-09 \_setunimathdimes: \_begin/end/group instead {}, bug fixed
+2021-02-15 \_textmff, \_scriptmff and \_sscriptmff introduced
+2021-02-15 \_expandafter -> \_ea
+2020-06-07 \Umathfractiondelsize (for \choose brackets) corrected, see https://tug.org/pipermail/luatex/2020-June/007365.html
+2020-04-15 \_setmathdimens -> \_setuniathdimens
+2020-02-25 implemented