Skip to content

Add stroke for text - #2970

Merged
laurmaedje merged 7 commits into
typst:mainfrom
Enter-tainer:mgt/text-stroke
Dec 19, 2023
Merged

laurmaedje merged 7 commits into
typst:mainfrom
Enter-tainer:mgt/text-stroke

Conversation

@Enter-tainer

@Enter-tainer Enter-tainer commented Dec 15, 2023 •

Copy link
Copy Markdown
Contributor

fix #2474 and related to #394

This PR add a stroke field to text and allow add strokes for outline glyphs. The paint order is stroke then fill so stokes won't cover fill

What's missing:

  • stroke + gradient/pattern. I don't know if it should be covered in this PR or not. Honestly I'm not pretty familiar with that This has been fixed by @Dherse !
  • stroke in pdf. It seems harder than i expected. perhaps we need to actually draw a path. Call for help from pdf experts! It works but the rendering order is fill-stroke so the strokes will cover fills. fill-stroke will be impled in following PRs

Credit: this pr is based on @rapiz1's branch

@Enter-tainer

Enter-tainer commented Dec 15, 2023 •

Copy link
Copy Markdown
Contributor Author

@flaribbit The image you posted in #394 (comment) shows that a text and have stroke in pdf. Do you know how I can add it? I tried to do it in 61d5f58 but it doesn't produce desired result I've did it.

Question: Which order should we choose? stroke-fill or fill-stroke? pdf seems only support the fill-stroke one. For the fake bold usage I think both of them are OK. But for word art mentioned in #2474 I guess the stroke-fill one is needed.

@Enter-tainer
Enter-tainer force-pushed the mgt/text-stroke branch 2 times, most recently from 31a3482 to eaed2df Compare December 15, 2023 06:37
@Enter-tainer
Enter-tainer marked this pull request as ready for review December 15, 2023 06:39
@flaribbit

flaribbit commented Dec 15, 2023 •

Copy link
Copy Markdown

image

f7c3470db16d3122f1a69cff4a78f15c

I tested it. Microsoft Word also used fill-stroke. There is no stroke-fill mode. For stroked text in #2474, drawing it twice may be a good idea.

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

Microsoft Word also used fill-stroke.

yes. there is no stroke-fill mode in pdf so if we insist to do that we need to make our own one.

@Myriad-Dreamin

Copy link
Copy Markdown
Contributor

It should be great to have gradient stroke on text, but from perspective of its mentioned primary usage, used for formal documents, it is not a urgent feature imo.

I can assert it is not that difficult to have gradient stroke in SVG/PNG export, since we have finished gradient stroke in geometric shape elements. But whether we could have that in PDF export may need judgement from the author of gradient PRs, pinging @Dherse.

Also should this PR detect gradient stroke and let them fallback to sensible color (such as black), @Enter-tainer?

@Myriad-Dreamin

Copy link
Copy Markdown
Contributor

@Enter-tainer Btw, we may already have them in SVG export (and may really work), since you are clever and using write_stroke 😸.

@Dherse

Dherse commented Dec 15, 2023

Copy link
Copy Markdown
Contributor

As far as gradient on test strokes, if you rely on the set stroke functionality in the current pdf export code then you should get gradients for free so to speak

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

As far as gradient on test strokes, if you rely on the set stroke functionality in the current pdf export code then you should get gradients for free so to speak

It seems that to we need to pass the bbox to set_stoke to get correct gradient stroke. Should I calculate the bbox of each glyph and do that?

@PgBiel

PgBiel commented Dec 15, 2023

Copy link
Copy Markdown
Contributor

Wow, this is looking awesome!

I wonder: could there be any way to prevent the overlapping strokes on the bottommost example below? Or should we maybe leave that to a future PR and merge this, when ready, as an MVP?

the bottommost example has overlapping strokes

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

We may first render all strokes and then all fills. But the svg rendered by chrome also has these overlaps so I think this behaviour is somehow "desired"

@PgBiel

PgBiel commented Dec 15, 2023 •

Copy link
Copy Markdown
Contributor

We may first render all strokes and then all fills. But the svg rendered by chrome also has these overlaps so I think this behaviour is somehow "desired"

Interestingly enough, it seems that this behavior can be configured: https://stackoverflow.com/a/64067791

Maybe we should have some similar setting for Typst? (At least it seems like it could be some text property?)

(Although it seems that, to not depend on CSS, you'd have to use SVG 2 features, according to a reply to the linked answer...)

Edit: https://caniuse.com/mdn-svg_attributes_presentation_paint-order seems like support was introduced only very recently to major browsers (and isn't supported by iOS Safari)...

Though it does seem to make a particularly considerable difference!

image

Comment thread crates/typst-svg/src/lib.rs Outdated
Size::new(Abs::pt(width), Abs::pt(height)),
self.text_paint_transform(state, &text.fill),
);
self.xml.write_attribute("paint-order", "stroke"); // make sure the stroke is rendered on top of the fill

@PgBiel PgBiel Dec 15, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, it seems that we're already using paint-order. So Chrome is probably rendering "wrongly" because it's a very recent addition, so maybe it's just a matter of using a recent Chrome version (see https://caniuse.com/mdn-svg_attributes_presentation_paint-order )

Perhaps it could still be configurable through the text Typst element, though 🤔

@Enter-tainer Enter-tainer Dec 15, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i dont know. i'm using arch and i think my chrome is relatively new.

the image you posted seems only mentioned the order between stroke and fill but not the paint order of each glyphs. the stroke in that image is also not that "huge". i guess there is no one-key-solution for this problem.

for me i think such a huge stroke sounds like an edge case for me and i prefer not handling it in this pr. but i'm open for people who have better ideas.

@PgBiel PgBiel Dec 15, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the image you posted seems only mentioned the order between stroke and fill but not the paint order of each glyphs.

Hmmm... Good point. My thoughts regarding this parameter are, then, moot.

We'd need to find a way to draw the stroke for a group of glyphs at a time (e.g. a word). I'm not sure how feasible that is...

Maybe it's better to just leave it to another PR, honestly.
Still, thanks for the effort, the current additions are already awesome 🙂

@Enter-tainer Enter-tainer changed the title feat: add stroke to text Add stroke to text Dec 18, 2023
@Enter-tainer Enter-tainer changed the title Add stroke to text Add stroke for text Dec 18, 2023
Comment thread crates/typst-pdf/src/page.rs Resolved
Comment thread crates/typst-pdf/src/page.rs Resolved
Comment thread crates/typst-render/src/lib.rs Resolved
Comment thread crates/typst-render/src/lib.rs Resolved
Comment thread crates/typst-render/src/lib.rs Resolved
// Flip vertically because font design coordinate
// system is Y-up.
let ts = ts.pre_scale(scale, -scale);
if let Some(FixedStroke {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we perhaps need a to_sk_stroke?

@Enter-tainer Enter-tainer Dec 18, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We may have one but it may be not as clean as other to_sk_xxx due to the extra / scale. I perfer not adding one.

Comment thread crates/typst-svg/src/lib.rs Outdated
Size::new(Abs::pt(width), Abs::pt(height)),
self.text_paint_transform(state, &text.fill),
);
self.xml.write_attribute("paint-order", "stroke"); // make sure the stroke is rendered on top of the fill

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
self.xml.write_attribute("paint-order", "stroke"); // make sure the stroke is rendered on top of the fill
// Make sure the stroke is rendered on top of the fill.
self.xml.write_attribute("paint-order", "stroke");

Please put comments on their own line above the code they document, start them with a capital letter, and end them with a period.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

From my reading of MDN paint-order: stroke draws the stroke first and then the fill. This means the stroke would be rendered below the fill, wouldn't it?

That would be different behaviour from PDF export, which is always fill-stroke.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. That's the problem. Which one should we choose? For fake bold usage, the stroke and the fill has the same color so the order donest really matter. But for use cases in #2474, I think the stroke-fill order should be better.

I think we can use the stroke-fill order, and emulate this in pdf by writing stroked glyphs twice. First time stroke only, and second time fill only.

@laurmaedje laurmaedje Dec 18, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We definitely need to do it in a way to is consistent for all outputs. Either we just settle on the fill-stroke order for now or we write twice in PDF. I am fine with both ways.

@Enter-tainer Enter-tainer Dec 18, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I did some try to write them twice and found it not as easy as I expected. I prefer left it to following prs, and the currently behavior is fill-stroke for all targets

Comment thread crates/typst/src/text/mod.rs Outdated
#[default(Smart::Auto)]
#[ghost]
#[resolve]
pub stroke: Smart<Stroke>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please but this directly below fill and add some documentation.

Also, why is this Smart<Stroke> rather than Option<Stroke>?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay I will change it to Option then. I dont really know the difference of them and I see Smart is used in other places so I copy them

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Option: When absent, the thing is not there, not visible, not existing.
Smart: When absent, some smart default behaviour is picked. E.g. Smart<Linebreaks> which picks optimized linebreaks if and only if justification is enabled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to Option. Also am i using the correct attributes? I don't really understant when should I add ghost or resolve

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  • default can be omitted for None, I have removed it before merging
  • ghost means that the field doesn't exist on the element, only in the styles. it's mostly used on the text element because it's special
  • resolve turns Lengths in the Stroke into Abs, it's correct here

Comment thread tests/typ/text/stroke.typ Resolved
Comment thread crates/typst-render/src/lib.rs Resolved
@Dherse

Dherse commented Dec 18, 2023

Copy link
Copy Markdown
Contributor

As far as gradient on test strokes, if you rely on the set stroke functionality in the current pdf export code then you should get gradients for free so to speak

It seems that to we need to pass the bbox to set_stoke to get correct gradient stroke. Should I calculate the bbox of each glyph and do that?

Sorry for not answering sooner, on text you don't need the bbox because per-glyph gradients on text aren't supported :)

Comment thread crates/typst-svg/src/lib.rs Resolved
@Dherse

Dherse commented Dec 18, 2023

Copy link
Copy Markdown
Contributor

image

Note that the different targets render slightly differently!

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

Note that the different targets render slightly differently!

Now the pdf/svg/png looks the same. I've changed to paint order to fill-stroke for all targets

image

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

Things left for future PRs:

  1. the stroke-fill painting order
  2. overlap between consecutive glyphs when a large stroke is presented.

- Links cannot appear in the first line because it is also used as a plain-text description.
- None is the default by default
@laurmaedje
laurmaedje merged commit 81ff34d into typst:main Dec 19, 2023
@laurmaedje

Copy link
Copy Markdown
Member

Thank you!

@logistic-bot

Copy link
Copy Markdown

For people looking to use stroke-fill paint order right now, I made this show rule which seems to work for me:

#show regex(".+"): it => box(
  width: auto,
  height: auto,
  {
    place(
      scale(
        x: 100%,
        y: 100%,
        it
      )
    )
    scale(
      x: 100%,
      y: 100%,
      text(
        stroke: 0pt,
        it
      )
    )
  }
)

(for some reason, setting the stroke to none still seems to render the stroke)

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

setting the stroke to none still seems to render the stroke

Does this happens for normal texts? Because by default the stroke is none.

@logistic-bot

Copy link
Copy Markdown

Here is a test document that shows this behaviour:

#set text(size: 20pt, stroke: 1pt + red, fill: blue)
#lorem(10) 1pt + red

#set text(stroke: none)
#lorem(10) none

#set text(stroke: none + black)
#lorem(10) none + black

#set text(stroke: 0pt)
#lorem(10) 0pt

compiled with typst 0.10.0 (ce0c387)

After further testing, it seems like the behaviour is not consistent between multiple compile targets, or viewers.

example with png export
example with png export

example with pdf export

pdf in zathura + mupdf
pdf in zathura

pdf in firefox
pdf in firefox

@Enter-tainer

Copy link
Copy Markdown
Contributor Author

okay i will take a look at the none+black case

@Enter-tainer
Enter-tainer deleted the mgt/text-stroke branch December 20, 2023 15:16
@Enter-tainer

Copy link
Copy Markdown
Contributor Author

I think the problem is I didn't handle 0pt stroke correctly in pdf export, svg and png are fine. I will fix that in #3013. Thank you for testing this!

@logistic-bot

Copy link
Copy Markdown

No problem, thanks for your work :)

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.

Stroked text

7 participants