Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Make kerns add hint in math, spacing in plain text; implement \unkern, \lastkern

* Improve converting a Dimension to space chars; Make \hskip add hints to math; implement \unskip, \lastskip

* Add \unskip,\unkern etc tests to test case

* Updates test cases for changed spacing conversion

* Slightly better choice of space chars, updating tests

* Whooops missed a teset
  • Loading branch information
brucemiller authored Sep 29, 2024
1 parent 2bef09e commit 2233d87
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 173 deletions.
54 changes: 37 additions & 17 deletions lib/LaTeXML/Engine/TeX_Glue.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ use LaTeXML::Package;
# \vskip c inserts vertical glue in a vertical list.
# \unskip c removes a glue item from the current list.

# a candidate for use by \hskip, \hspace, etc... ?
our @spaces = ( # Spaces to fake spacing, with width in ems
[0.100, "\x{200A}"], # Hair space (thinner than thin space)
[0.167, "\x{2006}"], # six-per-em
[0.200, "\x{2009}"], # five-per-em, thin space
[0.250, "\x{2005}"], # four-per-em, mid space
[0.333, "\x{2004}"], # three-per-em, thick space
[0.500, "\x{2002}"], # en-quad, "nut"
[1.000, "\x{2003}"], # em-quad, "mutton"
);

# String of spacing chars with width roughly equivalent to $dimen
sub DimensionToSpaces {
my ($dimen) = @_;
my $fs = LookupValue('font')->getSize; # 1 em
my $pt = $dimen->ptValue;
my $ems = $pt / $fs;
if ($ems < 0.01) { return; }
elsif ($ems < 0.17) { return "\x{2006}"; } # 6/em
elsif ($ems < 0.30) { return "\x{2005}"; } # 4/em
elsif ($ems < 0.40) { return "\x{2004}"; } # 3/em (same as nbsp?)
else {
my $n = int(($ems + 0.3) / 0.333); # 10pts per space...?
return (UTF(0xA0) x $n); } }
my $ems = $dimen->ptValue / $fs;
my $s = '';
for (my $i = $#spaces ; ($i >= 0) && ($ems > 0) ; $i--) {
my $w = $spaces[$i][0];
if ($ems + 0.01 > $w) {
my $n = int(($ems + 0.01) / $w);
$ems -= $n * $w; $s .= $spaces[$i][1] x $n; } }
return $s; }

# \hskip handled similarly to \kern
# \hskip can be ignored in certain situations...
Expand All @@ -54,13 +63,14 @@ DefConstructor('\hskip Glue', sub {
} }
elsif (inSVG()) {
Warn('unexpected', 'kern', $_[0], "Lost hskip in SVG " . ToString($length)); }

elsif ($props{isMath}) {
$document->insertElement('ltx:XMHint', undef, width => $length); }
else {
# $document->openText(DimensionToSpaces($length), $props{font}); } },
$document->absorb(DimensionToSpaces($length)); } },
properties => sub {
my ($stomach, $length) = @_;
(width => $length, isSpace => 1); });
(width => $length, isSpace => 1, isSkip => 1); });

# If this is the right solution...
# then we also should put the desired spacing on a style attribute?!?!?!
Expand All @@ -73,14 +83,19 @@ DefConstructor('\vskip Glue', sub {
elsif ($document->isOpenable('ltx:break')) {
$document->insertElement('ltx:break'); } }
return; },
properties => sub { (height => $_[1], isSpace => 1, isVerticalSpace => 1, isBreak => 1); });
properties => sub { (height => $_[1], isSpace => 1,, isSkip => 1, isVerticalSpace => 1, isBreak => 1); });

## Worrisome, but...
# Remove skip, if last on LIST
DefPrimitiveI('\unskip', undef, sub {
my ($stomach) = @_;
my @c = ();
my $box;
while (($box = $LaTeXML::LIST[-1]) && IsEmpty($box)) {
# Scan past any Comment boxes
while (($box = $LaTeXML::LIST[-1]) && (ref $box eq 'LaTeXML::Core::Comment')) {
push(@c, pop(@LaTeXML::LIST)); }
if ($box && $box->getProperty('isSkip')) {
pop(@LaTeXML::LIST); }
push(@LaTeXML::LIST, @c);
return; });

#======================================================================
Expand Down Expand Up @@ -121,8 +136,13 @@ DefPrimitiveI('\vfilneg', undef, undef);
# Lastskip
#----------------------------------------------------------------------
# \lastskip iq is 0.0 pt or the last glue or muglue on the current list.

DefRegister('\lastskip' => Glue(0), readonly => 1);
DefRegister('\lastskip' => Dimension(0), readonly => 1, getter => sub {
my $box;
for (my $i = $#LaTeXML::LIST ; $i > 0 ; $i--) {
my $box = $LaTeXML::LIST[$i];
last if !$box || !$box->getProperty('isSkip');
return $box->getProperty('width'); }
return Dimension(0); });

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1;
36 changes: 32 additions & 4 deletions lib/LaTeXML/Engine/TeX_Kern.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use LaTeXML::Package;
# Completely HACK version for the moment
# Note that \kern should add vertical spacing in vertical modes!
DefConstructor('\kern Dimension', sub {
my ($document, $length) = @_;
my ($document, $length, %props) = @_;
my $parent = $document->getNode;
if ($document->getNodeQName($parent) eq 'svg:g') {
if (my $x = $length->pxValue) {
Expand All @@ -40,9 +40,37 @@ DefConstructor('\kern Dimension', sub {
} }
elsif (inSVG()) {
Warn('unexpected', 'kern', $_[0], "Lost kern in SVG " . ToString($length)); }
});
DefPrimitiveI('\unkern', undef, undef);
DefRegister('\lastkern' => Dimension(0), readonly => 1);
elsif ($props{isMath}) {
$document->insertElement('ltx:XMHint', undef, width => $length); }
else {
# Add space to document?
$document->absorb(DimensionToSpaces($length)); }
},
properties => sub {
my ($stomach, $length) = @_;
(width => $length, isSpace => 1, isKern => 1); });

# Remove kern, if last on LIST
DefPrimitiveI('\unkern', undef, sub {
my ($stomach) = @_;
my @c = ();
my $box;
# Scan past any Comment boxes
while (($box = $LaTeXML::LIST[-1]) && (ref $box eq 'LaTeXML::Core::Comment')) {
push(@c, pop(@LaTeXML::LIST)); }
if ($box && $box->getProperty('isKern')) {
pop(@LaTeXML::LIST); }
push(@LaTeXML::LIST, @c);
return; });

# Get kern, if last on LIST
DefRegister('\lastkern' => Dimension(0), readonly => 1, getter => sub {
my $box;
for (my $i = $#LaTeXML::LIST ; $i > 0 ; $i--) {
my $box = $LaTeXML::LIST[$i];
last if !$box || !$box->getProperty('isKern');
return $box->getProperty('width'); }
return Dimension(0); });

#======================================================================
# Moving Vertically
Expand Down
16 changes: 8 additions & 8 deletions t/alignment/algx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<listingline xml:id="algx1.l2"><tags>
<tag><text fontsize="80%">2:</text></tag>
<tag role="refnum">2</tag>
</tags>     <text font="bold">return</text> 7
</tags>  <text font="bold">return</text> 7
</listingline>
<listingline xml:id="algx1.l3"><tags>
<tag><text fontsize="80%">3:</text></tag>
Expand Down Expand Up @@ -75,7 +75,7 @@
<listingline xml:id="alg1.l2"><tags>
<tag><text fontsize="80%">2:</text></tag>
<tag role="refnum">2</tag>
</tags>     <Math mode="inline" tex="r\leftarrow a\bmod b" text="r leftarrow modulo@(a, b)" xml:id="alg1.l2.m1">
</tags>  <Math mode="inline" tex="r\leftarrow a\bmod b" text="r leftarrow modulo@(a, b)" xml:id="alg1.l2.m1">
<XMath>
<XMApp>
<XMTok name="leftarrow" role="ARROW">←</XMTok>
Expand All @@ -92,7 +92,7 @@
<listingline xml:id="alg1.l3"><tags>
<tag><text fontsize="80%">3:</text></tag>
<tag role="refnum">3</tag>
</tags>     <text font="bold">while</text> <Math mode="inline" tex="r\not=0" text="r not-equals 0" xml:id="alg1.l3.m1">
</tags>  <text font="bold">while</text> <Math mode="inline" tex="r\not=0" text="r not-equals 0" xml:id="alg1.l3.m1">
<XMath>
<XMApp>
<XMTok meaning="not-equals" name="not-=" role="RELOP">≠</XMTok>
Expand All @@ -109,7 +109,7 @@
<listingline xml:id="alg1.l4"><tags>
<tag><text fontsize="80%">4:</text></tag>
<tag role="refnum">4</tag>
</tags>         <Math mode="inline" tex="a\leftarrow b" text="a leftarrow b" xml:id="alg1.l4.m1">
</tags>   <Math mode="inline" tex="a\leftarrow b" text="a leftarrow b" xml:id="alg1.l4.m1">
<XMath>
<XMApp>
<XMTok name="leftarrow" role="ARROW">←</XMTok>
Expand All @@ -122,7 +122,7 @@
<listingline xml:id="alg1.l5"><tags>
<tag><text fontsize="80%">5:</text></tag>
<tag role="refnum">5</tag>
</tags>         <Math mode="inline" tex="b\leftarrow r" text="b leftarrow r" xml:id="alg1.l5.m1">
</tags>   <Math mode="inline" tex="b\leftarrow r" text="b leftarrow r" xml:id="alg1.l5.m1">
<XMath>
<XMApp>
<XMTok name="leftarrow" role="ARROW">←</XMTok>
Expand All @@ -135,7 +135,7 @@
<listingline xml:id="alg1.l6"><tags>
<tag><text fontsize="80%">6:</text></tag>
<tag role="refnum">6</tag>
</tags>         <Math mode="inline" tex="r\leftarrow a\bmod b" text="r leftarrow modulo@(a, b)" xml:id="alg1.l6.m1">
</tags>   <Math mode="inline" tex="r\leftarrow a\bmod b" text="r leftarrow modulo@(a, b)" xml:id="alg1.l6.m1">
<XMath>
<XMApp>
<XMTok name="leftarrow" role="ARROW">←</XMTok>
Expand All @@ -152,12 +152,12 @@
<listingline labels="LABEL:euclidendwhile" xml:id="alg1.l7"><tags>
<tag><text fontsize="80%">7:</text></tag>
<tag role="refnum">7</tag>
</tags>     <text font="bold">end</text> <text font="bold">while</text>
</tags>  <text font="bold">end</text> <text font="bold">while</text>
</listingline>
<listingline xml:id="alg1.l8"><tags>
<tag><text fontsize="80%">8:</text></tag>
<tag role="refnum">8</tag>
</tags>     <text font="bold">return</text> <Math mode="inline" tex="b" text="b" xml:id="alg1.l8.m1">
</tags>  <text font="bold">return</text> <Math mode="inline" tex="b" text="b" xml:id="alg1.l8.m1">
<XMath>
<XMTok font="italic" role="UNKNOWN">b</XMTok>
</XMath>
Expand Down
4 changes: 2 additions & 2 deletions t/alignment/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
<tabular vattach="middle">
<tbody>
<tr>
<td align="left">a   –</td>
<td align="left">a   –</td>
<td align="left">b</td>
</tr>
<tr>
<td align="left">c   –</td>
<td align="left">c   –</td>
<td align="left">d</td>
</tr>
</tbody>
Expand Down
10 changes: 5 additions & 5 deletions t/alignment/halign.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<tr>
<td align="left" class="ltx_nopad_r">[a]</td>
<td align="left" class="ltx_nopad_r">(b)</td>
<td align="left" class="ltx_nopad_r">      {c}</td>
<td align="left" class="ltx_nopad_r">  {c}</td>
</tr>
</tabular>
</para>
Expand All @@ -67,8 +67,8 @@
<tr>
<td align="left" class="ltx_nopad_r">[a]</td>
<td align="left" class="ltx_nopad_r">(b)</td>
<td align="left" class="ltx_nopad_r">                              {c}</td>
<td align="left" class="ltx_nopad_r">                              [d]</td>
<td align="left" class="ltx_nopad_r">          {c}</td>
<td align="left" class="ltx_nopad_r">          [d]</td>
</tr>
</tabular>
</para>
Expand All @@ -77,8 +77,8 @@
<tr>
<td align="left" class="ltx_nopad_r">[a]</td>
<td align="left" class="ltx_nopad_r">(b)</td>
<td align="left" class="ltx_nopad_r">                              {c}</td>
<td align="left" class="ltx_nopad_r">                              [d]</td>
<td align="left" class="ltx_nopad_r">          {c}</td>
<td align="left" class="ltx_nopad_r">          [d]</td>
</tr>
</tabular>
</para>
Expand Down
Loading

0 comments on commit 2233d87

Please sign in to comment.