Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full justify of last line in WPF TextFormatter

Tags:

c#

.net

wpf

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!!

like image 325
google dev Avatar asked Sep 13 '18 14:09

google dev


People also ask

How to read just the last line of a text file?

In other words, we can’t give you a script that reads just the last line of a text file. But here’s a script that will make it look like you read just the last line of a text file: Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objFile = objFSO.OpenTextFile(“C:\Scripts\Test.txt”, ForReading)

Does justify with last line aligned left work?

If you create a new document, put some text in it (which should appear as Align left by default) and then click the Control Bar button for Justify with Last Line Aligned Left, does it work? Fixed by manually deleting all hard returns in pasted paragrahp.

How does the text formatter create lines of text?

As soon as an instance of the text formatter is created, the line creation process is started by calling the FormatLine method. TextFormatter calls back to the text source to retrieve the text and formatting parameters for the runs of text that form a line. In the following example, the process of formatting a text store is shown.

What is WPF text formatting?

At the most advanced level, WPF provides an extensible text formatting engine to control every aspect of text presentation, such as text store management, text run formatting management, and embedded object management. This topic provides an introduction to WPF text formatting.


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);
    }
}
like image 122
codeDom Avatar answered Oct 06 '22 01:10

codeDom