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

Take word wrapping into account when adjusting row height #2048

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

machekku
Copy link

@machekku machekku commented Apr 3, 2023

Hello,
This is my initial attempt to add text wrapping support when auto adjusting row height (#934).

Please note: This pull request is not yet intended to be merged. It serves as a base to discuss the solution. Once architectural concerns are resolved, I will add unit tests and polish the core algorithm itself.

Here's outline of my solution:

  1. When text wrapping is enabled, Excel will break long lines so that text is not wider then the cell.
  2. Lines can be wrapped on white space characters.
  3. Content width is calculated in XLRow.CalculateMinRowHeight() method as Column Width - Pixel Padding.
  4. Cell content height is calculated in XLRow.GetContentHeight(). As noted above, let's not focus on the algorithm itself this time yet. General idea is:
  • scan glyphs and accumulate them into clusters (a cluster is either a word-like object or a group of white space characters),
  • control line width and introduce line breaks when needed, moving current word into next line.
  1. To find line break opportunities, XLRow.GetContentHeight() method needs to know which glyphs are white space.
  2. GlyphBox class is extended to include IsWhiteSpace property, which is set via constructor.
  3. DefaultGraphicEngine.GetGlyphBox() determines if a glyph box represents white space by checking if any of grapheme code points is a white space code point. (I am really not sure if this assumption is correct)

Please let me know if this is the right approach.

@jahav
Copy link
Member

jahav commented Apr 5, 2023

Hi, the structure looks good to me.

I am not sure about adding a flag to the GlyphBox. Maybe it would be better to create a new structure (e.g. equivalent of internal readonly record struct Grapheme(GlyphBox Box, bool IsSpace) and return that from XLCell). Graphic engine should be kept mostly as a very thin wrapper and shouldn't deal with fonts/images and this is an information for Excel-specific layout.

I haven't realized that we would need to know if a glyph box was for space and I am pretty sure there will be other other layout related things that will need to be changed and I don't want to break public API (IXLGraphicEngine+GlyphBox) every time layout will have to be fixed.

As for 8., I would just check that grapheme is only 1 codepoint long and it is a space (only space - I have tried to what Excel does with other whitespaces, e.g. En Quad U+2000 and it behaves like a normal character). Multi-codepoint graphemes are used only for emoji as far as I know (e.g. see 43 https://unicode.org/emoji/charts/full-emoji-list.html).

The important thing here is scope, because there there are four places that must work in sync:

  • row - adjust to content - wrapped
  • row - adjust to content - non-wrapped
  • column - adjust to content - wrapped
  • column - adjust to content - non-wrapped

One place can't behave differently than others for the feature (in this case non-vertical non-rotated text).

This is a breaking change for a feature used in pretty much every worksheet that sets a text with a newline and uses adjust to content.

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

2 participants