Newspaper Style Justification

The current justification for text editing (as of Proclaim 3.4.0.0063 on 12/19/2022)distributes both font size and word distance. I request a style of justification that operates on word distance only, leaving the font whatever size was indicated in the font size setting. An example of the feature is attached. Thank you for your consideration and assistance.
4
4 votes

Submitted · Last Updated

Comments

  • Christopher Moore
    Christopher Moore Member Posts: 2
    In terms of functionality, it would look something like this assuming C#
    using System;
    using System.Collections.Generic;
    using System.Text;


    namespace TextJustification
    {
        class Program
        {
            static void Main(string[] args)
            {
                string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor velit vel malesuada bibendum. Sed euismod, sapien vel bibendum auctor, lectus magna laoreet magna, et congue est velit vel urna. In hac habitasse platea dictumst. Nullam gravida libero a sapien luctus, id viverra ligula commodo.";
                int width = 40;


                Console.WriteLine(JustifyText(text, width));
            }


            static string JustifyText(string text, int width)
            {
                List words = new List(text.Split(' '));
                List lines = new List();
                StringBuilder line = new StringBuilder();


                foreach (string word in words)
                {
                    if (line.Length + word.Length + 1 <= width)
                    {
                        line.Append(word + " ");
                    }
                    else
                    {
                        lines.Add(line.ToString());
                        line.Clear();
                        line.Append(word + " ");
                    }
                }


                if (line.Length > 0)
                {
                    lines.Add(line.ToString());
                }


                StringBuilder result = new StringBuilder();
                foreach (string l in lines)
                {
                    result.AppendLine(JustifyLine(l, width));
                }


                return result.ToString();
            }


            static string JustifyLine(string line, int width)
            {
                List words = new List(line.Split(' '));


                if (words.Count == 1)
                {
                    return line.PadRight(width);
                }


                int totalSpaces = width - line.Length;
                int spaceCount = words.Count - 1;
                int spacesPerWord = totalSpaces / spaceCount;
                int extraSpaces = totalSpaces % spaceCount;


                StringBuilder result = new StringBuilder();
                for (int i = 0; i < words.Count; i++)
                {
                    result.Append(words[i]);
                    if (i < words.Count - 1)
                    {
                        result.Append(' ', spacesPerWord + (extraSpaces > 0 ? 1 : 0));
                        extraSpaces--;
                    }
                }


                return result.ToString();
            }
        }
    }