Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realign foreignObject to its rightful position #2488

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/LaTeXML/Core/Alignment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ sub beAbsorbed {
&{ $$self{openContainer} }($document, ($attr ? %$attr : ()),
cwidth => $$self{cwidth}, cheight => $$self{cheight}, cdepth => $$self{cdepth},
rowheights => $$self{rowheights},
rowdepths => $$self{rowdepths},
columnwidths => $$self{columnwidths},
);
foreach my $row (@rows) {
Expand Down Expand Up @@ -513,6 +514,7 @@ sub showSize {
sub normalize_sum_sizes {
my ($self) = @_;
my @rowheights = ();
my @rowdepths = ();
my @colwidths = ();
my @colrights = ();
my @collefts = ();
Expand Down Expand Up @@ -564,6 +566,7 @@ sub normalize_sum_sizes {
$$row{tpadding} = Dimension($rowt);
$$row{bpadding} = Dimension($rowb);
# NOTE: Should be storing column widths to; individually, as well as per-column!
push(@rowdepths, $rowd);
push(@rowheights, $rowh + $rowd); } # somehow our heights are way too short????
## Now compute the positions
my @rowpos = ();
Expand All @@ -588,8 +591,10 @@ sub normalize_sum_sizes {
$$self{cdepth} = Dimension(0);
@colwidths = map { Dimension($_); } @colwidths;
@rowheights = map { Dimension($_); } @rowheights;
@rowdepths = map { Dimension($_); } @rowdepths;
$$self{columnwidths} = [@colwidths];
$$self{rowheights} = [@rowheights];
$$self{rowdepths} = [@rowdepths];

for (my $i = 0 ; $i < scalar(@rowheights) ; $i++) {
my $row = $rows[$i];
Expand Down
11 changes: 6 additions & 5 deletions lib/LaTeXML/Engine/TeX_Box.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,12 @@ Tag('svg:foreignObject', autoOpen => 1, autoClose => 1,
# Make sure we get a size, in case autoOpen'd
if ($whatsit) {
my ($w, $h, $d) = $whatsit->getSize;
my $y = $STATE->lookupDefinition(T_CS('\baselineskip'))->valueOf->pxValue;
my $ht = $h->add($d);
$node->setAttribute(width => $w->pxValue) unless $node->hasAttribute('width');
$node->setAttribute(height => $ht->pxValue) unless $node->hasAttribute('height');
$node->setAttribute(transform => "matrix(1 0 0 -1 0 $y)");
my $ht = $node->getAttribute('height') // $h->add($d)->pxValue;
$h = $h->pxValue;
$node->setAttribute(style => "line-height:${h}px");
$node->setAttribute(width => $w->pxValue) unless $node->hasAttribute('width');
$node->setAttribute(height => $ht) unless $node->hasAttribute('height');
$node->setAttribute(transform => "matrix(1 0 0 -1 0 $h)");
Comment on lines +342 to +345
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This took a few tries. Shifting by height only mimics better the behaviour of the depth, but it's not enough on its own. The additional line-height ensures that boxes with same horizontal coordinate and font size get the same baseline (this is noticeable when an arrow has multiple labels in tikzcd).

$node->setAttribute(overflow => 'visible'); } });

# This attempts to be a generalize vbox construction;
Expand Down
12 changes: 6 additions & 6 deletions lib/LaTeXML/Package/pgfsys-latexml.def.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ sub tikzAlignmentBindings {

# Note that pgf *seems* to have set the coordinate system to the center(?) (w/ \pgfsys@transformcm)
# Additionally, there's a svg:g outside the alignment (w/ ltx_svg_fog)
# The following tweaks (heuristically) the transform to position the alignment object
# The following tweaks the transform to position the alignment object
#
# Coordinates are currently pgf: y moves UP
# Coordinates of alignment are : y moves DOWN
Expand All @@ -940,14 +940,14 @@ sub openTikzAlignment {
my $h = ($props{cheight} && $props{cheight}->pxValue) || 0;
my $d = ($props{cdepth} && $props{cdepth}->pxValue) || 0;
my @rowheights = ($props{rowheights} ? @{ $props{rowheights} } : (Dimension(0)));
my @rowdepths = ($props{rowdepths} ? @{ $props{rowdepths} } : (Dimension(0)));
my @colwidths = ($props{columnwidths} ? @{ $props{columnwidths} } : (Dimension(0)));
my $x = 0;
# This SHOULD be just $h+$d, but the shift is necessary to align
# w/additional material outside the \halign ( arrows & such)
my $y = ($h + $d) - $rowheights[-1]->pxValue / 2; # HEURISTIC adjustment! half of LAST row!?
my $y = ($h + $d) - $rowdepths[-1]->pxValue; # set baseline to baseline of last row
Debug("TIKZ Alignment size $w x $h + $d"
. " rows: " . join(',', map { $_->pxValue; } @rowheights)
. " cols: " . join(',', map { $_->pxValue; } @colwidths)
. " rows: " . join(',', map { $_->pxValue; } @rowheights)
. " cols: " . join(',', map { $_->pxValue; } @colwidths)
. " depths: " . join(',', map { $_->pxValue; } @rowdepths)
. " => alignment adjustment = $x, $y") if $LaTeXML::DEBUG{pgf};
my $array = $doc->openElement($tag, class => 'ltx_tikzmatrix',
_scopebegin => 1,
Expand Down
2 changes: 2 additions & 0 deletions lib/LaTeXML/resources/CSS/LaTeXML.css
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ span.ltx_framed { display:inline-block; text-indent:0; } /* avoid padding/
.ltx_svg_fog foreignObject > p { margin:0; padding:0; display:block; }
/*.ltx_svg_fog foreignObject > p { margin:0; padding:0; display:block; white-space:nowrap; }*/

.ltx_foreignobject_container { display:flex; width:100%; height:100%; align-items:end; justify-items:center; justify-content:center; }

/*======================================================================
Low-level Basics */
/* Note that LaTeX(ML)'s font model doesn't map quite exactly to CSS's */
Expand Down
21 changes: 17 additions & 4 deletions lib/LaTeXML/resources/XSLT/LaTeXML-picture-xhtml.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,23 @@
</xsl:choose>
</xsl:for-each>
<xsl:choose>
<!-- If foreignObject in a DIFFERENT namespace, copy as foreign markup -->
<xsl:when test="local-name()='foreignObject'
and not(namespace-uri(child::*) = $SVG_NAMESPACE)">
<xsl:apply-templates mode='copy-foreign'/>
<xsl:when test="local-name()='foreignObject'">
<!-- center the content within foreignObject (note that display:flex does not work on foreignObject so we use two wrappers) -->
<xsl:element name="div" namespace="{$html_ns}">
<xsl:attribute name="class">ltx_foreignobject_container</xsl:attribute>
<xsl:element name="div" namespace="{$html_ns}">
<xsl:attribute name="class">ltx_foreignobject_content</xsl:attribute>
<!-- If foreignObject in a DIFFERENT namespace, copy as foreign markup -->
<xsl:choose>
<xsl:when test="not(namespace-uri(child::*) = $SVG_NAMESPACE)">
<xsl:apply-templates mode='copy-foreign'/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
Expand Down
Loading