Share

PaperClips

Tracker: Feature Requests

5 Justified text - ID: 1771448
Last Update: Comment added ( qualidafial )

With new RowPrint class, i had to print justified text.
Can you implement it?


Michele ( mikydv ) - 2007-08-10 07:31

5

Closed

Wont Fix

Nobody/Anonymous

None

1.1.0

Public


Comments ( 8 )

Date: 2008-10-09 17:35
Sender: qualidafialProject AdminAccepting Donations

Sourceforge has removed shell access to project administrators, and as a
result I am unable to perform essential administration tasks (including
bringing the web site back online). Therefore I have decided to migrate
the PaperClips project to Google Code Hosting:
http://code.google.com/p/swt-paperclips/

Accordingly I have closed this issue on sourceforge and copied the
conversation to a new issue at:
http://code.google.com/p/swt-paperclips/issues/detail?id=8


Date: 2007-11-30 16:18
Sender: mikydv


Yes, I have still interest in this; but I haven't found a simple
solution.
My idea is to introduce the concept of paragraph, and justify all lines of
the paragraph except the last one.


Date: 2007-11-30 16:03
Sender: qualidafialProject AdminAccepting Donations


It's been a while since I commented on the challenges with justified text,
and nobody has responded. Is there still interest in this?

It's not likely I'll have time to work on this any time soon.


Date: 2007-10-17 17:39
Sender: qualidafialProject AdminAccepting Donations


My goal in this is to implement justified text in such a way that both
TextPrint and StyledTextPrint are using the same mechanism. I do not want
to duplicate work. Especially since I'm using TextPrint as the underlying
implementation of StyledTextPrint (which is actually just a glorified
layout component when you look into the guts).

Let me lay out a scenario. Suppose you have a StyledTextPrint, and on one
particular line you have the following text:
-------------------------------------------------------------------
this is normal text *bold* *text* REALLY BIG FONT
-------------------------------------------------------------------

When the text is justified, you want it to look like this:
-------------------------------------------------------------------
this is normal text *bold* *text* REALLY BIG FONT
-------------------------------------------------------------------

Because spaces are wider in REALLY BIG FONT, you want the extra space to
be more than the other fonts so that the spaces stay proportionate to the
respective font sizes.

So StyledTextPrint goes about getting PrintPieces for each bit of text on
the line:
* "this is normal text " yields a PrintPiece, and the size of the piece is
exactly the size of the text, including the trailing space.
* "*bold* *text* " yields another PrintPiece, and it too is exactly the
size of the bold text, including trailing space.
* "REALLY BIG FONT" yields a third PrintPiece, at exactly the size of the
text.

So now StyledTextPrint spaces these PrintPieces so they fill the whole
line:
-------------------------------------------------------------------
this is normal text *bold* *text* REALLY BIG FONT
-------------------------------------------------------------------

Each set of words is still clumped together at the regular spacing. We
need to distribute the extra spacing not only between print pieces but
between words in those individual print pieces. And the spacing should
scale proportionately with the font.

Let's try adding a setJustified() property to TextPrint. When the flag is
set, the text will take up all the space it is given and justify the words
appropriately:
-------------------------------------------------------------------
this is normal text
*bold* *text*
REALLY BIG FONT
-------------------------------------------------------------------
Whoops. We made each chunk of text greedy, and they each took up a whole
line.

Let's change the code so that all but the last line of text is justified:
-------------------------------------------------------------------
this is normal text *bold* *text* REALLY BIG FONT
-------------------------------------------------------------------
That didn't work. A single-line is automatically considered the "last
line", so we're back where we started.

What we need is to lay out text as normal, and then have a way of telling
those PrintPieces how much extra space they should have. Let's create an
interface for conveying that information:

interface JustifiablePrintPiece extends PrintPiece {
public int getJustifyWeight();
public void widenBy(int width);
}

Going back to the example, suppose we find out we have 300 extra pixels.
Now let's look at the justify weight of each piece:
* "this is normal text " has 4 spaces, so we assign a weight of 4.
* "*bold* *text* " has two spaces, so we assign a weight a 2.
* "REALLY BIG FONT" has 2 spaces, but at a larger font size, so let's
assign a weight of 4.

The total weight is 10, therefore we will give 300 * 4 / 10 = 120 pixels
to the first chunk, 300 * 2 / 10 = 60 to the next chunk, and 300 * 4 / 10 =
120 to the last chunk.
-------------------------------------------------------------------
this is normal text *bold* *text* REALLY BIG FONT
-------------------------------------------------------------------

We've given out the extra space, but each chunk is still considered the
"last line" so they are not expanding to fill the extra space. So we need
a way to tell the print piece to grab that extra space. Let's add a method
to JustifiablePrintPiece:

public void setForceJustify(boolean force);

Here's where I've hit a snag. It is not always clear when I should force
justify. At this point in the execution all I know is that I have a
PrintIterator that will give me PrintPieces. I can see whether the
PrintPiece implements JustifiablePrintPiece, but not whether it *should* be
justified. If a piece is the last chunk from a TextPrint but there is more
text right after it, should I force justify them both?

Any helps sorting through this problem would be greatly appreciated. When
you get down into the details it's much more complicated than it seems.
The fact that PaperClips is designed around the principle of composition
only makes it more ambiguous to decide what the appropriate behavior should
be.

Matthew


Date: 2007-09-07 20:18
Sender: k-rueffer


Hey,

I just thpught about justified text a little. There are a couple of simple
algorithms or implementing it. Monday in a week I'll be able to spend some
tme on this.
I'll need it too :D


Date: 2007-08-28 08:02
Sender: mikydv


I have to print styled text from a SWT StyledText object. StyledText class
justifies text based on the horizontal alignment of the line: it adds extra
whitespace between words excluding the last line, which is horizontally
aligned based on the choice made for the entire line. You can run
Snippet213.java from SWT to see an example.

I use this code to horizontally align a RowPrint; I expect to use the same
code adding a boolean parameter "justify".

private static RowPrint addRowPrint(RowPrint parentRowPrint, int align)
{
RowPrint rowPrint = new RowPrint();
parentRowPrint.append(new AlignPrint(rowPrint, align, SWT.DEFAULT));

//TODO: styledText.getLineJustify(index)

return rowPrint;
}

Perhaps, better approach is to add "justified" property to AlignPrint
class.


Date: 2007-08-28 08:00
Sender: mikydv


I have to print styled text from a SWT StyledText object. StyledText class
justifies text based on the horizontal alignment of the line: it adds extra
whitespace between words excluding the last line, which is horizontally
aligned based on the choice made for the entire line. You can run
Snippet213.java from SWT to see an example.

I use this code to horizontally align a RowPrint; I expect to use the same
code adding a boolean parameter "justify".

private static RowPrint addRowPrint(RowPrint parentRowPrint, int align)
{
RowPrint rowPrint = new RowPrint();
parentRowPrint.append(new AlignPrint(rowPrint, align, SWT.DEFAULT));

//TODO: styledText.getLineJustify(index)

return rowPrint;
}

Perhaps, better approach is to add "justified" property to AlignPrint
class.


Date: 2007-08-28 06:39
Sender: qualidafialProject AdminAccepting Donations


A question about the expected behavior of justified text:

There are at several possible ways of justifying text:
* Add extra whitespace between words, excluding the last line. This is
the behavior of MS Word.
* Add extra whitespace between words, including the last line. This is
the behavior of many printed publications.
* Add extra whitespace between every character, excluding the last line.
Some books do this, like the Bible.
* Stretch glyphs horizontally to fill the line. I've seen some magazines
do this.

As you can see, there are many approaches to justified text and I'm not
sure how to approach this problem. At the end of the day, we might have to
implement a pluggable approach to text justification in order to keep
everybody happy. However if you could provide some insight into which
approach you were expecting,that might help me get a jumpstart in the right
direction.


Attached File

No Files Currently Attached

Changes ( 4 )

Field Old Value Date By
status_id Open 2008-10-09 17:35 qualidafial
resolution_id None 2008-10-09 17:35 qualidafial
close_date - 2008-10-09 17:35 qualidafial
artifact_group_id None 2008-09-10 04:18 qualidafial