Skip to content

Add support for cliping content in block and box - #431

Merged
laurmaedje merged 8 commits into
typst:mainfrom
birktj:clip-block-box
Apr 1, 2023
Merged

laurmaedje merged 8 commits into
typst:mainfrom
birktj:clip-block-box

Conversation

@birktj

@birktj birktj commented Mar 29, 2023

Copy link
Copy Markdown
Contributor

This PR tries to fix #429 by adding a clip argument to block and box. From my very limited it seems to work fine, however I am not entirely updated on the internals of these elements work and also how the frame element works so there might be some cases that are not handled in the best way, like:

  • Should this argument consider inset?
  • Should clipping happen inside or outside of the border?
  • What should happen with blocks that break across pages?

@laurmaedje

Copy link
Copy Markdown
Member

The reason why this doesn't exist yet is because the text and SVG clipping in the renderer doesn't work correctly, yet.

@birktj

birktj commented Mar 29, 2023

Copy link
Copy Markdown
Contributor Author

I assume that is the image renderer then? Both text and svg seems to work fine for me in the pdf renderer (where I assume you use native pdf clipping constructs?)

@laurmaedje

Copy link
Copy Markdown
Member

Exactly.

@birktj

birktj commented Mar 29, 2023

Copy link
Copy Markdown
Contributor Author

Okay, I am looking at the image rendering code now. It doesn't seem unreasonably difficult (but with a few small challenges) to add clipping there. I will try to see if I am able to implement something during the day.

@laurmaedje

Copy link
Copy Markdown
Member

One Problem is that resvg doesn't take a clip mask and another is that tiny-skia's clip mask data is private and we need it to correctly render clipped glyphs.

@birktj

birktj commented Mar 29, 2023

Copy link
Copy Markdown
Contributor Author

Too me it looks like PixmapMut::draw_pixmap would probably be the way to go. And then render the svgs and glyphs onto a separate pixmap first. I don't know if that will cause other issues or performance problems, but I can give it a try and we can see how well it works

@laurmaedje

Copy link
Copy Markdown
Member

Rendering normal glyphs onto a pixmap first has a significant performance overhead, unfortunately. It might also cause problems with transformations (which SVG images already have).

@birktj

birktj commented Mar 30, 2023

Copy link
Copy Markdown
Contributor Author

So I have implemented what I talked about now. The outline rendering code should have almost identical results to the previous code, except for some (very small) differences in alpha blending. For svg glyphs I had a lot of problems determining a good pixmap size. I ended on a solution where I use the same heuristic as in the outline rendering to see if the transform is very complicated and in that case render to a full sized pixmap. If not then I render to a pixmap with width and height of 3*ppem and place the glyph in the middle hoping that it will never overflow. This might need some reconsidering.

In order for tests to pass I needed to change the following line to > 3, because of small differences of alpha blending of glyph outlines:

.any(|(&a, &b)| a.abs_diff(b) > 2)

Unfortunately the performance regression was much worse than I would have expected, the following is only rendering outlines to pixmaps (compared to main branch):

bench_render
  Instructions:            17968013 (+48.08550%)
  L1 Accesses:             24223514 (+48.97363%)
  L2 Accesses:                75319 (+944.5015%)
  RAM Accesses:               24481 (+1.623080%)
  Estimated Cycles:        25456944 (+48.52814%)

However on the other hand there are a lot fewer L2 accesses when rendering svg glyphs to pixmaps, probably because it we render to small buffer rather than the whole page. This is just rendering svg glyphs to a pixmap compared to the main branch:

bench_render
  Instructions:            12146741 (+0.107296%)
  L1 Accesses:             16277443 (+0.103428%)
  L2 Accesses:                 3286 (-53.88070%)
  RAM Accesses:               24095 (+0.020756%)
  Estimated Cycles:        17137198 (-0.012848%)

This is both changes compared to the main branch:

bench_render
  Instructions:            17964047 (+48.05059%)
  L1 Accesses:             24239316 (+49.06755%)
  L2 Accesses:                51145 (+617.8246%)
  RAM Accesses:               24492 (+1.668742%)
  Estimated Cycles:        25352261 (+47.91802%)

For my case I am more or less only interested in PDF rendering and clipping is very important for me so I would prefer adding this change even if it reduces performance in the image renderer. But of course you decide what tradeoffs you consider more important. In the long term it is of course always possible to try to get a patch into tiny-skia that would expose the internals of ClipMask allowing for more performant glyph outline rendering.

@laurmaedje

laurmaedje commented Mar 30, 2023 •

Copy link
Copy Markdown
Member

The problem is that the image renderer is used by our web app. (Long-term we want to build a GPU-accelerated one, but we haven't yet.) As rendering is already relatively slow, a 50% slowdown is just too much. The slowdown mostly comes from rendering normal glyphs, I assume. How about using the old code if there is no clip mask and the new one only if there is one? Or just trying to get a patch into tiny-skia, that would probably be the best option. RazrFalcon is pretty responsive in my experience, also with version bumps.

@birktj

birktj commented Mar 30, 2023 •

Copy link
Copy Markdown
Contributor Author

Checking for a clip mask seems like a good start. Presumably for current documents the clipmask will mostly be unset so you won't really notice any difference.

This is when performance when checking for clipmask compared to main, unsurprisingly the numbers look similar as for the version that just clips svg glyphs, probably the clipmask is never set in the benchmark:

bench_render
  Instructions:            12125022 (-0.071701%)
  L1 Accesses:             16256410 (-0.025922%)
  L2 Accesses:                 4745 (-33.40351%)
  RAM Accesses:               24110 (+0.083022%)
  Estimated Cycles:        17123985 (-0.089939%)

Could this be an acceptable tradeoff? For now it shouldn't affect performance too much and it is still possible to have a longer-term plan of adding a patch to tiny-skia in order to improve performance for the cases when a clipmask is set.

Of course this PR needs a bit more work before it can be considered usable. The svg glyph rendering needs some more scrutiny and we also need to add some tests that include clipping.

@laurmaedje

Copy link
Copy Markdown
Member

This is a good tradeoff, I think. I'm excited to see clipping support!

@birktj

birktj commented Mar 30, 2023

Copy link
Copy Markdown
Contributor Author

I think I have improved the svg glyph rendering a bit now. All the tests seem to pass, except for text/font.typ where one pixel on the camel is quite different from the reference (~5%, so not too bad). I don't really understand why this is happening (it seems to go away if I change the pixmap size a bit, maybe indicating some sampling/floating point issues?) however it probably doesn't matter too much.

The following is the result of compare -fuzz 2% -metric mae tests/png/text/font.png tests/ref/text/font.png diff.png

diff

I have also added a simple test for clipping. From those tests we can see that the results when adding a stroke to the frames are not perfect, preferably the content would clip to the inside of the stroke or at least the stroke would be applied over the content.
image

@laurmaedje

Copy link
Copy Markdown
Member

Generally, it would be nice if strokes weren't always center strokes. Maybe inside would be a better default. But that's a separate issue. This is very nice!

@laurmaedje

Copy link
Copy Markdown
Member

I don't think the stroke issue is blocking. Tell me if you think this is ready to be merged.

@birktj

birktj commented Mar 30, 2023 •

Copy link
Copy Markdown
Contributor Author

I just did a tiny commit, but now I think it should be ready for merging now if you think everything looks good.

@laurmaedje
laurmaedje merged commit ed79ecb into typst:main Apr 1, 2023
@birktj
birktj deleted the clip-block-box branch April 1, 2023 14:05
@laurmaedje laurmaedje mentioned this pull request Jul 17, 2023
5 tasks done
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
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.

Support clipping content

2 participants