Skip to content

Commit

Permalink
Have \hskip revert back to a common command, when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
brucemiller committed Nov 29, 2024
1 parent 20ecf5a commit 328bd8e
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/LaTeXML/Engine/TeX_Glue.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ use LaTeXML::Package;
# \vskip c inserts vertical glue in a vertical list.
# \unskip c removes a glue item from the current list.

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"
our @spaces = ( # Spaces to fake spacing, with width in ems, along with the common command
[0.100, "\x{200A}"], # Hair space (thinner than thin space)
[0.167, "\x{2006}"], # six-per-em
[0.200, "\x{2009}", T_CS('\thinspace')], # five-per-em, thin space
[0.250, "\x{2005}", T_CS('\>')], # four-per-em, mid space
[0.333, "\x{2004}", T_CS('\;')], # three-per-em, thick space
[0.500, "\x{2002}", T_CS('\enspace')], # en-quad, "nut"
[1.000, "\x{2003}", T_CS('\quad')], # em-quad, "mutton"
[2.000, "\x{2003}\x{2003}", T_CS('\qquad')],
);

# String of spacing chars with width roughly equivalent to $dimen
sub DimensionToSpaces {
my ($dimen) = @_;
my $fs = LookupValue('font')->getSize; # 1 em
my $fs = LookupValue('font')->getSize; # 1 em
my $ems = $dimen->ptValue / $fs;
my $s = '';
for (my $i = $#spaces ; ($i >= 0) && ($ems > 0) ; $i--) {
Expand All @@ -49,6 +50,16 @@ sub DimensionToSpaces {
$ems -= $n * $w; $s .= $spaces[$i][1] x $n; } }
return $s; }

# Revert a skip to it's (common) command, falling back to a generic \hskip <dim>
sub revertSkip {
my ($command, $dimen) = @_;
my $fs = LookupValue('font')->getSize; # 1 em
my $ems = $dimen->ptValue / $fs;
for (my $i = 0 ; ($i <= $#spaces) ; $i++) {
next if ($ems > $spaces[$i][0] + 0.01) || !$spaces[$i][2];
return $spaces[$i][2] if $ems < $spaces[$i][0] + 0.01; }
return Invocation($command, $dimen); }

# \hskip handled similarly to \kern
# \hskip can be ignored in certain situations...
DefConstructor('\hskip Glue', sub {
Expand All @@ -68,6 +79,7 @@ DefConstructor('\hskip Glue', sub {
else {
# $document->openText(DimensionToSpaces($length), $props{font}); } },
$document->absorb(DimensionToSpaces($length)); } },
reversion => sub { revertSkip(T_CS('\hskip'), $_[1]); },
properties => sub {
my ($stomach, $length) = @_;
(width => $length, isSpace => 1, isSkip => 1); });
Expand Down

0 comments on commit 328bd8e

Please sign in to comment.