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

Conversation

xworld21
Copy link
Contributor

But also, help! This started as an easy fix, and ended up touching alignment code. I think it is correct, but I'd rather have some comments now, before spending more time on it. I really hope I am right, because it improves the TikZ output in a small but quite substantial way.

The aim is to align foreign objects properly inside SVGs.

  1. Flip svg:foreignObject around its center, instead of the center of an imaginary line of height 16.6px (the hardcoded global value of \baselineskip). Compare with equivalent code in Post::SVG::convertNode. I have concluded that \baselineskip was just a mistake.

  2. Add a CSS flex wrapper to svg:foreignObject, so that the foreignObject box and its content share the same center, instead of the content being anchored north east, which looks bad in most situations.

(1) and (2) make normal TikZ figures (with no halign) perfect, modulo font metric errors. For instance,
image
becomes
image
Small difference, but the equal sign is where it should be. I have played with other figures and they all look better this way.

  1. (commits 3 + 4) Now TikZ-cd is even worse than before. The root cause: Core::Alignment hardcodes the depth to 0. That should rather be the depth of the last line. With the depth available, the heuristic code can be removed altogether. I seemingly get perfect alignment in TikZ-cd.

Example: before
image
after
image
which is much closer to the PDF
image

@dginev dginev requested a review from brucemiller January 15, 2025 19:49
@dginev
Copy link
Collaborator

dginev commented Jan 15, 2025

Very exciting work! @brucemiller please help :>

@xworld21 xworld21 force-pushed the realign-foreignobject-2 branch from df24b87 to b6fcdc4 Compare February 1, 2025 19:21
Comment on lines +342 to +345
$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)");
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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants