Skip to content

Commit

Permalink
Respect multi line Word Borders when breaking into words
Browse files Browse the repository at this point in the history
Fixes #244
  • Loading branch information
TheJoeFin committed Jan 14, 2023
1 parent 597609f commit a1274b5
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions Text-Grab/Views/GrabFrame.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public void WordChanged()

public void MergeSelectedWordBorders()
{
RectanglesCanvas.ContextMenu.IsOpen = false;
FreezeGrabFrame();

List<WordBorder> selectedWordBorders = wordBorders.Where(w => w.IsSelected).OrderBy(o => o.Left).ToList();
Expand Down Expand Up @@ -325,17 +326,15 @@ public void StartWordBorderMoveResize(WordBorder wordBorder, Side sideEnum)

public void BreakWordBorderIntoWords(WordBorder wordBorder)
{
List<string> listOfWords = wordBorder.Word.Split().ToList();
// List<string> listOfWords = wordBorder.Word.Split().ToList();
ICollection<string> 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();
Expand All @@ -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<string> 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();
}
Expand Down

0 comments on commit a1274b5

Please sign in to comment.