Repository navigation
Add stroke for text - #2970
Add stroke for text#2970
Conversation
e4af280 to
61d5f58
Compare
|
Question: Which order should we choose? stroke-fill or fill-stroke? pdf seems only support the |
31a3482 to
eaed2df
Compare
|
I tested it. Microsoft Word also used |
yes. there is no stroke-fill mode in pdf so if we insist to do that we need to make our own one. |
|
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? |
|
@Enter-tainer Btw, we may already have them in SVG export (and may really work), since you are clever and using |
|
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? |
|
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 (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! |
| 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 |
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙂
| // Flip vertically because font design coordinate | ||
| // system is Y-up. | ||
| let ts = ts.pre_scale(scale, -scale); | ||
| if let Some(FixedStroke { |
There was a problem hiding this comment.
Do we perhaps need a to_sk_stroke?
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| #[default(Smart::Auto)] | ||
| #[ghost] | ||
| #[resolve] | ||
| pub stroke: Smart<Stroke>, |
There was a problem hiding this comment.
Please but this directly below fill and add some documentation.
Also, why is this Smart<Stroke> rather than Option<Stroke>?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Changed to Option. Also am i using the correct attributes? I don't really understant when should I add ghost or resolve
There was a problem hiding this comment.
- 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
Sorry for not answering sooner, on text you don't need the bbox because per-glyph gradients on text aren't supported :) |
2eb43df to
0b28939
Compare
0b28939 to
25076d0
Compare
|
Things left for future PRs:
|
- Links cannot appear in the first line because it is also used as a plain-text description. - None is the default by default
|
Thank you! |
|
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) |
Does this happens for normal texts? Because by default the stroke is none. |
|
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) 0ptcompiled with typst 0.10.0 (ce0c387) After further testing, it seems like the behaviour is not consistent between multiple compile targets, or viewers. |
|
okay i will take a look at the none+black case |
|
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! |
|
No problem, thanks for your work :) |









fix #2474 and related to #394
This PR add a
strokefield totextand allow add strokes for outline glyphs. The paint order isstrokethenfillso stokes won't coverfillWhat's missing:
I don't know if it should be covered in this PR or not. Honestly I'm not pretty familiar with thatThis has been fixed by @Dherse !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 PRsCredit: this pr is based on @rapiz1's branch