Skip to content

Commit

Permalink
Merge pull request #2174 from Omikhleia/fix-math-primes-asterisk
Browse files Browse the repository at this point in the history
Fix math primes and asterisk
  • Loading branch information
alerque authored Nov 23, 2024
2 parents f324a15 + b2ca8b4 commit 6bc618c
Show file tree
Hide file tree
Showing 17 changed files with 705 additions and 181 deletions.
11 changes: 11 additions & 0 deletions packages/math/base-elements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,17 @@ end

function elements.text:shape ()
self.font.size = self.font.size * self:getScaleDown()
if isScriptMode(self.mode) then
local scriptFeature = SILE.settings:get("math.font.script.feature")
if scriptFeature then
self.font.features = ("+%s=1"):format(scriptFeature)
end
elseif isScriptScriptMode(self.mode) then
local scriptFeature = SILE.settings:get("math.font.script.feature")
if scriptFeature then
self.font.features = ("+%s=2"):format(scriptFeature)
end
end
local face = SILE.font.cache(self.font, SILE.shaper.getFace)
local mathMetrics = self:getMathMetrics()
local glyphs = SILE.shaper:shapeToken(self.text, self.font)
Expand Down
10 changes: 10 additions & 0 deletions packages/math/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ function package.declareSettings (_)
type = "integer",
default = 400,
})
SILE.settings:declare({
parameter = "math.font.script.feature",
type = "string or nil",
default = "ssty",
help = "OpenType feature for the math script-style alternates (esp. primes), theoretically ssty",
})
SILE.settings:declare({
parameter = "math.font.filename",
type = "string",
Expand Down Expand Up @@ -138,6 +144,10 @@ By default, this package uses Libertinus Math, so it will fail if Libertinus Mat
Another font may be specified via the setting \autodoc:setting{math.font.family}.
If required, you can set the font style and weight via \autodoc:setting{math.font.style} and \autodoc:setting{math.font.weight}.
The font size can be set via \autodoc:setting{math.font.size}.
The \autodoc:setting{math.font.script.feature} setting can be used to specify OpenType features for the math font, which are applied to the smaller script styles.
It defaults to \code{ssty} (script style alternates), notably to ensure that some symbols such as the prime, double prime, etc. are displayed correctly.
The default setting applies to Libertinus Math and well-designed math fonts, but some fonts may require different features.
(The STIX Two Math font has a stylitic set \code{ss04} from primes only, but also supports, according to its documentation, \code{ssty}, which provides other optical adjustments.)
\paragraph{MathML}
The first way to typeset math formulas is to enter them in the MathML format.
Expand Down
45 changes: 39 additions & 6 deletions packages/math/texlike.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,49 @@ local mathGrammar = function (_ENV)
return pl.utils.unpack(t)
end

-- TeX uses the regular asterisk (* = U+002A) in superscripts or subscript:
-- The TeXbook exercice 18.32 (p. 179, 330) for instance.
-- Fonts usually have the asterisk raised too high, so using the Unicode
-- asterisk operator U+2217 looks better (= \ast in TeX).
local astop = P"*" / luautf8.char(0x2217)
-- TeX interprets apostrophes as primes in math mode:
-- The TeXbook p. 130 expands ' to ^\prime commands and repeats the \prime
-- for multiple apostrophes.
-- The TeXbook p. 134: "Then there is the character ', which we know is used
-- as an abbreviation for \prime superscripts."
-- (So we are really sure superscript primes are really the intended meaning.)
-- Here we use the Unicode characters for primes, but the intent is the same.
local primes = (
P"''''" / luautf8.char(0x2057) + -- quadruple prime
P"'''" / luautf8.char(0x2034) + -- triple prime
P"''" / luautf8.char(0x2033) + -- double prime
P"'" / luautf8.char(0x2032) -- prime
) / function (s)
return { id="atom", s }
end
local primes_sup = (
primes * _ * P"^" * _ * element_no_infix / function (p, e)
-- Combine the prime with the superscript in the same mathlist
if e.id == "mathlist" then
table.insert(e, 1, p)
return e
end
return { id="mathlist", p, e }
end
+ primes -- or standalone primes
)

START "math"
math = V"mathlist" * EOF"Unexpected character at end of math code"
mathlist = (comment + (WS * _) + element)^0
supsub = element_no_infix * _ * P"^" * _ * element_no_infix * _ *
P"_" * _ * element_no_infix
subsup = element_no_infix * _ * P"_" * _ * element_no_infix * _ *
P"^" * _ * element_no_infix
sup = element_no_infix * _ * P"^" * _ * element_no_infix
supsub = element_no_infix * _ * primes_sup * _ * P"_" * _ * element_no_infix +
element_no_infix * _ * P"^" * _ * element_no_infix * _ * P"_" * _ * element_no_infix
subsup = element_no_infix * _ * P"_" * _ * element_no_infix * primes_sup +
element_no_infix * _ * P"_" * _ * element_no_infix * _ * P"^" * _ * element_no_infix
sup = element_no_infix * _ * primes_sup +
element_no_infix * _ * P"^" * _ * element_no_infix
sub = element_no_infix * _ * P"_" * _ * element_no_infix
atom = natural + C(utf8code - S"\\{}%^_&") +
atom = natural + astop + C(utf8code - S"\\{}%^_&'") +
(P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end
text = (
P"\\text" *
Expand Down
4 changes: 2 additions & 2 deletions tests/bug-1495-inline-math-layout.expected
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 196.6642
My 37.4447
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 198.4962
T 3421 w=4.5776 (nil)
Expand Down Expand Up @@ -101,7 +101,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 199.7501
My 49.5701
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 201.5821
T 3413 w=3.5152 (nil)
Expand Down
16 changes: 8 additions & 8 deletions tests/feat-math-display-numbered.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 129.6547
My 48.7418
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 131.4867
T 3421 w=4.5776 (nil)
Expand Down Expand Up @@ -49,7 +49,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 129.6547
My 108.3223
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 131.4867
T 3421 w=4.5776 (nil)
Expand Down Expand Up @@ -88,7 +88,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 129.6547
My 167.9027
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 131.4867
T 3421 w=4.5776 (nil)
Expand Down Expand Up @@ -133,7 +133,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 129.6547
My 227.4832
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 131.4867
T 3421 w=4.5776 (nil)
Expand Down Expand Up @@ -176,7 +176,7 @@ My 285.3548
T 2765 w=4.9200 (nil)
Mx 104.6983
My 287.4548
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 17 w=3.7200 (nil)
Mx 114.8483
My 285.3548
Expand All @@ -189,7 +189,7 @@ My 300.1048
T 2765 w=4.9200 (nil)
Mx 104.6855
My 302.2048
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2758 w=3.7328 (nil)
Mx 114.8483
My 300.1048
Expand All @@ -203,7 +203,7 @@ Mx 140.6728
T 2765 w=4.9200 (nil)
Mx 145.2328
My 302.2048
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2758 w=3.7328 (nil)
Mx 148.9656
T 2088 w=5.1840 (nil)
Expand Down Expand Up @@ -246,7 +246,7 @@ Mx 146.0615
T 2750 w=3.8100 (nil)
Mx 149.8715
My 341.0405
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 151.7035
T 3421 w=4.5776 (nil)
Expand Down
8 changes: 4 additions & 4 deletions tests/feat-math-display-unnumbered.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 135.7213
My 47.6655
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 137.5533
T 3421 w=4.5776 (nil)
Expand Down Expand Up @@ -43,7 +43,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 135.7213
My 105.2660
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 137.5533
T 3421 w=4.5776 (nil)
Expand Down Expand Up @@ -76,7 +76,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 135.7213
My 162.8665
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 137.5533
T 3421 w=4.5776 (nil)
Expand Down Expand Up @@ -115,7 +115,7 @@ Set font Libertinus Math;10;400;Regular;normal;;;LTR
T 2750 w=3.8100 (nil)
Mx 135.7213
My 220.4670
Set font Libertinus Math;8;400;Regular;normal;;;LTR
Set font Libertinus Math;8;400;Regular;normal;+ssty=1;;LTR
T 2753 w=1.8320 (nil)
Mx 137.5533
T 3421 w=4.5776 (nil)
Expand Down
Loading

0 comments on commit 6bc618c

Please sign in to comment.