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

Some code organization changes in ShapeLine::layout #170

Merged
merged 3 commits into from
Aug 28, 2023

Conversation

Imberflur
Copy link
Contributor

@Imberflur Imberflur commented Aug 15, 2023

I was reading through this code (to get a better understanding of how alignment is applied and potential avenues for reusing layout work done for width measurement when performing the final layout) and noticed a few things that could be easily de-duplicated or adjusted to improve clarity (IMO).

  • Some variables declared at the top of ShapeLine::layout weren't used until the second phase which is much farther down, so I moved them down to be at the start of that phase.
  • Replaced the push_line bool with checking layout_lines.is_empty() which is effectively equivalent.
  • Some variables defined outside the loop over visual_lines were reset with each loop iteration, so I moved their definitions down into the loop.
  • Added a separate justification_expansion instead of reusing the alignment_correction variable (which just shifts the line in all other modes, but was used to hold how much each space should be expanded when justifying). This is more clear to the reader and avoids several later checks of whether the alignment is Align::Justified in spots where alignment_correction is used. (I may have gone overboard with the other changes but I think at least this one is pretty useful)
  • I was able to factor out the common code during iterating visual line ranges for LTR/RTL text into a process_range closure as well as combine several cases of iterating over glyphs during this.
  • Removed unnecessary mem::swap(&mut glyphs, &mut glyphs_swap); (glyphs is not used later and can just be directly moved out of)

None of these changes should affect the behavior (If I didn't make any mistakes!).

@jackpot51
Copy link
Member

This now conflicts after #175, please rebase.

…bles declarations down closer to where they are used, move variables that are reset every loop down to be declared in the loop, replace Vec::new + mem::swap with mem::take
* max_ascent and max_descent declarations moved into loop since they are
  reset each iteration and the one spot where they are used outside the
  loop for pushing an empty line is if all items are empty (so they
  would always be 0.0 there).
* For `Align::Justified`, instead of repurposing `alignment_correction`
  variable for expanding blank spaces, there is a new
  `justification_expansion` variable. This helps clarify the code.
* Common code for processing ranges factored out section where ranges
  are iterated in opposite orders for RTL vs LTR.
* We don't need to use `take_mut` on `glyphs` since the variable is not
  used afterwards (i.e. we can just  move out of `glyphs`).
* Fix bug where `scratch.scripts` was being used for logging info
  instead of `scripts`.
…replaced used of push_line bool with checking is layout_lines is empty
@Imberflur
Copy link
Contributor Author

Rebased. Also fixed a small bug where &scratch.scripts was being logged instead of &scripts.

@jackpot51 jackpot51 merged commit 37e8f00 into pop-os:main Aug 28, 2023
2 checks passed
@Imberflur Imberflur deleted the refactor branch August 28, 2023 20:28
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.

2 participants