Thread: Formula for justifying text?

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    Question Formula for justifying text?

    Hey does any one know of a (preferably simple) formula for justifying text into a column? Or maybe someone can fix mine?

    I have to write a prog in C to do this, I have the program all written out, but my output isn't as I would like it...

    I calculate the number of spaces that should be between each word in a line using this:

    spaces = ( m - c ) / (w - 1)

    where:
    m = maximum number of characters in line (e.g. column width)
    c = the number of characters in the line I want to print (basically a combination of the lengths of all the words in the line)
    w = number of words in the line

    I think a lot of my problem may lie in that this fraction wouldn't always be coming out to a whole number and you can print say .3 of a space...
    Last edited by nine-hundred; 09-02-2007 at 12:50 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you pick a variable pitch font, then yes you need something far more sophisticated than that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Well, for simplicity's sake, let's assume a monospace type of font

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I don't see how you came up with ( ( m - c ) / (w - 1) ). Try somthing more like ( ( m / 2 ) - ( c / 2) ).

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nine-hundred View Post
    I think a lot of my problem may lie in that this fraction wouldn't always be coming out to a whole number and you can print say .3 of a space...
    Suppose you need to print 4.5 spaces between each word. That's 9 spaces between every two words. So if you alternate between 4 and 5 spaces, every other word, you insert, on average, 4.5 spaces per word.

    You can generalize this in the following way:

    Code:
    double total_error = 0.0;
    double nspace = (m - c) / (w - 1); /* Ideal number of spaces per word (not an integer) */
    double space_error = nspace - floor(nspace); /* Fractional part of nspace */
    
    for(each word)
    {
        if(total_error >= 1.0)
        {
            /* Error exceeds a space, so subtract a full unit of error and generate an extra
             * space */
            output_spaces(1);
            total_error -= 1.0;
        }
        output(word);
        output_spaces(floor(nspace));
        total_error += space_error;
    }
    Where output_spaces() is a function that outputs the given number of spaces.

    This is closely related to a few other algorithms, like Bresenham line drawing, or box-filter image scaling.

    Also, it can be implemented entirely with integer math, but that's a trick I keep to myself.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Hmm.. I can try it, but why the division by two?

    how I got mine was something like this (sorry I wasn't more clear):

    (m - c) will be the number of unoccupied spaces on the line (e.g. if the words don't take up exactly all the space on the line but there is not enough room for another full word)

    and then I divide this number by the number of words I have minus one (because I won't be placing any spaces after the last word, as the last character on the line should not be whitespace in the justification.

    then I would print each word doing something like: printf("%*s",spaces, word);
    except for the last one, of course.

    It made sense to me...but obviously it doesn't seem to be doing it as I thought it would...

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Yarin View Post
    I don't see how you came up with ( ( m - c ) / (w - 1) ). Try somthing more like ( ( m / 2 ) - ( c / 2) ).
    Makes sense to me. m - c is the number of space characters needed to make the line exactly "m" characters long. These spaces need to be divided into w - 1 chunks, since there are w - 1 spaces in between w words.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Hah, oh, brewbuck posted while I was writing up my post.

    Alright, thank you, brewbuck, I'll try that one


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting Text from a Text file
    By Daved in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2006, 09:47 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  4. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 04:33 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM