private void EndLine(LineData currentLine, StringBuilder currentLineText) { currentLine.Text = currentLineText.ToString().Trim(); if (!string.IsNullOrWhiteSpace(currentLine.Text)) { currentLine.TextSize = PInvoked.MeasureString(graphics, currentLine.Text, font, settings.LetterSpacing); Lines.Add(currentLine); } }
public void SetText() { IEnumerable <string> words = settings.Text.Replace(Environment.NewLine, Constants.Space + Environment.NewLine + Constants.Space).Split(' '); StringBuilder currentLineText = new StringBuilder(); int y = 0; LineData currentLine = StartNewLine(ref y); //until all words are placed or y is outside scaledBounds foreach (string word in words) { if (y > scaledBounds.Bottom + lineIncrement) { break; } if (word.Equals(Environment.NewLine)) { EndLine(currentLine, currentLineText); //start new line currentLineText.Clear(); currentLine = StartNewLine(ref y); continue; } Size textSize = PInvoked.MeasureString(graphics, (currentLineText + word).Trim(), font, settings.LetterSpacing); if (textSize.Width > currentLine.LineBounds.Width) { EndLine(currentLine, currentLineText); //start new line currentLineText.Clear(); currentLine = StartNewLine(ref y); currentLineText.Append(word + Constants.Space); } else { currentLineText.Append(word + Constants.Space); } } if (currentLine != null && currentLineText.Length > 0) { EndLine(currentLine, currentLineText); } }
private void Justify(LineData line, Graphics lineGraphics) { var lineTextWithoutSpaces = line.Text.Replace(Constants.Space, string.Empty); var lineSizeWithoutSpaces = PInvoked.MeasureString(lineGraphics, lineTextWithoutSpaces, font, settings.LetterSpacing); var spaceWidth = (line.TextSize.Width - lineSizeWithoutSpaces.Width) / Math.Max((line.Text.Length - lineTextWithoutSpaces.Length), 1); if (spaceWidth > font.Size * 3) { PInvoked.TextOut(lineGraphics, line.Text, 0, 0, font, settings.LetterSpacing); } else { var x = 0; foreach (string word in line.Text.Split(Constants.SpaceChar)) { var wordSize = PInvoked.MeasureString(lineGraphics, word, font, settings.LetterSpacing); PInvoked.TextOut(lineGraphics, word, x, 0, font, settings.LetterSpacing); x += wordSize.Width + spaceWidth; } } }