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

Missing glyphs and inccorect breaks when setting MaxLines on TextBlock containing line breaks #65

Closed
handerss-spotfire opened this issue Jan 16, 2023 · 2 comments · Fixed by #68

Comments

@handerss-spotfire
Copy link
Contributor

Setting the MaxLines property on a TextBlock that contains text with line breaks results in missing glyphs being rendered at the line break character positions.

Repro:

var tb = new TextBlock();
tb.AddText("Hello\n\nWorld!", new Style());
tb.MaxLines = 1;

Result:
maxlines

@handerss-spotfire
Copy link
Contributor Author

Maybe it enough to always line break at a line break character if that occurs before the max width of the text block?

E.g:

diff --git a/Topten.RichTextKit/TextBlock.cs b/Topten.RichTextKit/TextBlock.cs
index 1b29bda..9403b80 100644
--- a/Topten.RichTextKit/TextBlock.cs
+++ b/Topten.RichTextKit/TextBlock.cs
@@ -1475,10 +1475,15 @@ namespace Topten.RichTextKit
                     fr = _fontRuns[frIndex];
                     var room = _maxWidthResolved - fr.XCoord;
                     frSplitIndex = frIndex;
-                    codePointIndexSplit = fr.FindBreakPosition(room, frSplitIndex == frIndexStartOfLine);
-                    codePointIndexWrap = codePointIndexSplit;
-                    while (codePointIndexWrap < _codePoints.Length && UnicodeClasses.LineBreakClass(_codePoints[codePointIndexWrap]) == LineBreakClass.SP)
-                        codePointIndexWrap++;
+                    var breakPosition = fr.FindBreakPosition(room, frSplitIndex == frIndexStartOfLine);
+
+                    if (breakPosition < codePointIndexSplit)
+                    {
+                        codePointIndexSplit = breakPosition;
+                        codePointIndexWrap = codePointIndexSplit;
+                        while (codePointIndexWrap < _codePoints.Length && UnicodeClasses.LineBreakClass(_codePoints[codePointIndexWrap]) == LineBreakClass.SP)
+                            codePointIndexWrap++;
+                    }
                 }
 
                 // Split it

@handerss-spotfire
Copy link
Contributor Author

The patch above is a bit too naive since codePointIndexSplit can be negative if no non-character breaks were found. I have modified it for our use-case such that it differentiates between required text breaks and others, and that it prefers to break at characters if that happens later than a break position (leads to behavior similar to StringTrimming.EllipsisCharacter in GDI+).

I'll submit a PR if testing is successful.

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 a pull request may close this issue.

1 participant