From a1274b51b287ec4da00fcca785c86152976dc0b8 Mon Sep 17 00:00:00 2001 From: Joseph Finney Date: Sat, 14 Jan 2023 13:18:45 -0600 Subject: [PATCH] Respect multi line Word Borders when breaking into words Fixes #244 --- Text-Grab/Views/GrabFrame.xaml.cs | 66 ++++++++++++++++++------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/Text-Grab/Views/GrabFrame.xaml.cs b/Text-Grab/Views/GrabFrame.xaml.cs index 81705f5b..6e747df3 100644 --- a/Text-Grab/Views/GrabFrame.xaml.cs +++ b/Text-Grab/Views/GrabFrame.xaml.cs @@ -235,6 +235,7 @@ public void WordChanged() public void MergeSelectedWordBorders() { + RectanglesCanvas.ContextMenu.IsOpen = false; FreezeGrabFrame(); List selectedWordBorders = wordBorders.Where(w => w.IsSelected).OrderBy(o => o.Left).ToList(); @@ -325,17 +326,15 @@ public void StartWordBorderMoveResize(WordBorder wordBorder, Side sideEnum) public void BreakWordBorderIntoWords(WordBorder wordBorder) { - List listOfWords = wordBorder.Word.Split().ToList(); + // List listOfWords = wordBorder.Word.Split().ToList(); + ICollection wordLines = wordBorder.Word.Split(Environment.NewLine); const double widthScaleAdjstFactor = 1.5; double top = wordBorder.Top; double left = wordBorder.Left; - int numberOfLines = 1 + wordBorder.Word.Count(c => c == '\n'); + int numberOfLines = wordLines.Count; double wordHeight = wordBorder.Height / numberOfLines; - double wordFractionWidth = wordBorder.Width / listOfWords.Count; - double lineWidth = (double)GetWidthOfString(wordBorder.Word, (int)wordFractionWidth, (int)wordHeight); - double diffBetweenWordAndBorder = (wordBorder.Width - (lineWidth / widthScaleAdjstFactor)) / listOfWords.Count; DeleteThisWordBorder(wordBorder); UndoRedo.StartTransaction(); @@ -347,33 +346,44 @@ public void BreakWordBorderIntoWords(WordBorder wordBorder) GrabFrameCanvas = RectanglesCanvas }); - foreach (string word in listOfWords) + int lineItterator = 0; + foreach (string line in wordLines) { - double wordWidth = (double)GetWidthOfString(word, (int)wordFractionWidth, (int)wordHeight) / widthScaleAdjstFactor; - // wordWidth += 8; // this is to account for the 8px left border thickness - WordBorder wordBorderBox = new() - { - Width = wordWidth, - Height = wordHeight, - Word = word, - OwnerGrabFrame = this, - Top = top, - Left = left, - MatchingBackground = wordBorder.MatchingBackground, - }; + double lineWidth = GetWidthOfString(line, (int)wordBorder.Width, (int)wordHeight); + ICollection lineWords = line.Split(); - wordBorders.Add(wordBorderBox); - _ = RectanglesCanvas.Children.Add(wordBorderBox); + double wordFractionWidth = lineWidth / lineWords.Count; + // double diffBetweenWordAndBorder = (wordBorder.Width - (lineWidth / widthScaleAdjstFactor)) / lineWords.Count; - UndoRedo.InsertUndoRedoOperation(UndoRedoOperation.AddWordBorder, - new GrabFrameOperationArgs() + foreach (string word in lineWords) + { + double wordWidth = (double)GetWidthOfString(word, (int)wordFractionWidth, (int)wordHeight) / widthScaleAdjstFactor; + WordBorder wordBorderBox = new() { - WordBorder = wordBorderBox, - WordBorders = wordBorders, - GrabFrameCanvas = RectanglesCanvas - }); - - left += wordWidth + diffBetweenWordAndBorder; + Width = wordWidth, + Height = wordHeight, + Word = word, + OwnerGrabFrame = this, + Top = top + (lineItterator * wordHeight), + Left = left, + MatchingBackground = wordBorder.MatchingBackground, + }; + + wordBorders.Add(wordBorderBox); + _ = RectanglesCanvas.Children.Add(wordBorderBox); + + UndoRedo.InsertUndoRedoOperation(UndoRedoOperation.AddWordBorder, + new GrabFrameOperationArgs() + { + WordBorder = wordBorderBox, + WordBorders = wordBorders, + GrabFrameCanvas = RectanglesCanvas + }); + + left += wordWidth; // + diffBetweenWordAndBorder; + } + lineItterator++; + left = wordBorder.Left; } UndoRedo.EndTransaction(); }