typst / typst

A new markup-based typesetting system that is powerful and easy to learn.
https://typst.app
Apache License 2.0
32.63k stars 872 forks source link

Thin space between tightly placed shapes #3432

Open Mc-Zen opened 6 months ago

Mc-Zen commented 6 months ago

Description

Consider the following code in which we display a series of rectangles using rect in the first line and an alike series of rectangles created with polygon.


// Simulate `rect` with a polygon
#let polygon-rect(width: 20pt, height: 20pt, stroke: auto, fill: auto) = {
  polygon((0pt, 0pt), (width, 0pt), (width, height), (0pt, height), (0pt, 0pt), fill: fill, stroke: stroke)
}
#let len = 1cm

#for i in range(5) {
  place(dx: len * i, rect(width: len, height: len, fill: red, stroke: none))
}

#v(len * 2)
#for i in range(5) {
  place(dx: len * i, polygon-rect(width: len, height: len, fill: red, stroke: none))
}

2

With polygon, a slight space appears between the tightly set rectangles.

Note I have not discovered this on PDFs, it only seems to happen with PNG export. With SVGs, the story is different: here, both with rect and polygon, a slight space is left between consecutive rectangles.

This may not seem as a great issue in itself but is indeed very annoying for many tasks such as general layout design, heatmaps, and more.

A (not very nice) workaround would be to add a very thin stroke. But how thin should the stroke be?

(Why use polygon instead of rect? The latter does not accept negative dimensions, see also #3265, this can be overcome with polygon).

Reproduction URL

No response

Operating system

No response

Typst version

Enter-tainer commented 6 months ago

https://github.com/typst/typst/issues/3216#issuecomment-1897653191

it's reader's problem and wont appear in printer

Enivex commented 6 months ago

https://github.com/typst/typst/issues/3216#issuecomment-1897653191

it's reader's problem and wont appear in printer

In this case it's png export, so not a reader problem.

It's a tricky issue though. Presumably due to float rounding.

Enter-tainer commented 6 months ago

#3216 (comment) it's reader's problem and wont appear in printer

In this case it's png export, so not a reader problem.

It's a tricky issue though. Presumably due to float rounding.

Maybe. But note that the same thin lines appears in svg and pdf export when using certain readers.

Enter-tainer commented 3 months ago

One trick is that you can draw it multiple times and the thin line will disappear.

#for i in range(5) {
  for _ in range(5) {
    place(dx: len * i, polygon-rect(width: len, height: len, fill: red, stroke: none))
  }
}

image https://discord.com/channels/1054443721975922748/1088371919725793360/1244280894227742781

Enter-tainer commented 3 months ago

This is related to anti alias rendering and is known as Conflation artifacts. For more please see https://github.com/linebender/vello/issues/49