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

Cache is_justifiable #2399

Merged
merged 7 commits into from
Oct 17, 2023
Merged

Cache is_justifiable #2399

merged 7 commits into from
Oct 17, 2023

Conversation

Dherse
Copy link
Sponsor Collaborator

@Dherse Dherse commented Oct 16, 2023

See #2393

@Dherse
Copy link
Sponsor Collaborator Author

Dherse commented Oct 16, 2023

@Enter-tainer I am going to leave until late this afternoon (Brussels time), I have just pushed all of the changes I have done, I think this should fix the cache by updating it in the right places, but apparently I must have missed one, do you think you could have a look at it so that I can push it when I get back? Please 😄

@Enter-tainer
Copy link
Contributor

I will take a closer look when I leave work. (aka ~2 or 3h later) 👍

@Enter-tainer
Copy link
Contributor

i'm thinking of changing fields in shapeglyph to private and use setters to access x_advance and stretchability 🤔 this can avoid problem like this

@Enter-tainer
Copy link
Contributor

image

seems that cjk punctuation adjustment is broken, let me see how to fix it

@Dherse
Copy link
Sponsor Collaborator Author

Dherse commented Oct 16, 2023

seems that cjk punctuation adjustment is broken, let me see how to fix it

I had a look at it, and I did see that some of the spacing was wrong, but since I know nothing about CJK (sorry about that, might one day try and learn Japanese or Chinese) I didn't know where to look for. Thanks a lot @Enter-tainer :D

@Enter-tainer
Copy link
Contributor

Enter-tainer commented Oct 17, 2023

Overall this PR is great but there is something I concern. As all field of ShapedGlyph is public, there is a chance people change x_advance or something but forget to update is_justifiable. I don't know if there is some good rust pattern to handle this.

From another view, after adding is_justifiable, fields in this struct no longer vary independently but has an invariant. Therefore I'd prefer change fields of this struct to private and use getters to get access to them, and only allow mutate these fields through certain setters and accessors -- is_justifiable is maintained in them. But this sounds like a lot of code change 🤔. cc @laurmaedje what do you think?

@Dherse
Copy link
Sponsor Collaborator Author

Dherse commented Oct 17, 2023

I think that's a valid concern and a needed change

@laurmaedje
Copy link
Member

A lot of the shaping code is in the same module, so as long as we don't move ShapedGlyph to another file making it private wouldn't actually prevent wrong mutations, right?

@Enter-tainer
Copy link
Contributor

A lot of the shaping code is in the same module, so as long as we don't move ShapedGlyph to another file making it private wouldn't actually prevent wrong mutations, right?

Oh i've written too much C++ these days and forgot about that. You are right. So make it private might not work. How do rust people solve this problem?

@laurmaedje
Copy link
Member

Sometimes by putting it into a module, sometimes by just not using it wrongly. Depends. For public API, I would also enforce correct usage, but for internal use I think it's okay to enforce some variants manually. There's tons of different ways to break the shaping code that the compiler won't catch.

@laurmaedje laurmaedje merged commit 77b8467 into typst:main Oct 17, 2023
3 checks passed
@laurmaedje
Copy link
Member

Thanks!

}

/// Updates the justifiability of the glyph.
fn update_justifiable(&mut self) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the late comment after the PR merged. But the update_justifiable function is not needed. Though the width of a glyph may change due to some CJK adjustments, whether the glyph is justifiable will never change.

Copy link
Sponsor Collaborator Author

Choose a reason for hiding this comment

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

You're absolutely right, the reason why I added the update_justifiable was because I assumed it changes (since it relies on the width) but it does not, I'll open up a follow up PR, Thanks a lot!

Comment on lines +1086 to +1090
fn is_justifiable(
c: char,
script: Script,
x_advance: Em,
stretchability: (Em, Em),
Copy link
Contributor

@peng1999 peng1999 Oct 18, 2023

Choose a reason for hiding this comment

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

The x_advance and stretchability arguments are used as heuristics to distinguish between English and Chinese quotation marks. It can be combined to one argument width, which should be x_advance + stretchability.

More information that can put into code comments: The quotations in two language is unfortunately share the same code point in Unicode (U+2018, U+2019, U+201C, U+201D), but quotation marks in Chinese is fullwidth (1em) while in English is usually not. This heuristic will have false positive for monospace English fonts, but it will not hurt much because monospace texts usually need not justifying.

@Dherse Dherse deleted the is_justifiable_cache branch November 2, 2023 12:22
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.

None yet

5 participants