Full justify of last line in WPF TextFormatter

Viewed 229

I'm making a WPF text-editor using TextFormatter. I need to justify the last line in each paragraph.

Something like that:

I need to justify the last line in each
paragraph I need to justify the last li
ne in each  paragraph I need to justify 
the  last line in each paragraph I need 
to    justify    the   last   line   in

how to make this happen?

Thanks in advance!!

1 Answers

In your TextSource implementation, in GetTextRun function, at the end of each paragraph return an TextEmbeddedObject object with very wide width, and with height 0.

This way you will force the last line to be justified.

class MyTextEmbeddedObject : TextEmbeddedObject
{
    public override LineBreakCondition BreakBefore => LineBreakCondition.BreakAlways;

    public override LineBreakCondition BreakAfter => LineBreakCondition.BreakRestrained;

    public override bool HasFixedSize => true;

    public override CharacterBufferReference CharacterBufferReference => throw new NotImplementedException();

    public override int Length => 1;

    public override TextRunProperties Properties => GenericTextParagraphProperties._defaultTextRunProperties;

    public override Rect ComputeBoundingBox(bool rightToLeft, bool sideways)
    {
        throw new NotImplementedException();
    }

    public override void Draw(DrawingContext drawingContext, Point origin, bool rightToLeft, bool sideways)
    {
        throw new NotImplementedException();
    }

    public override TextEmbeddedObjectMetrics Format(double remainingParagraphWidth)
    {
        return new TextEmbeddedObjectMetrics(10000 /* very wide width */, 0, 0);
    }
}
Related