How to get the "lines" and "line count"

  • Hi,

    "line" migh be wrong word for the "thing" that I am searching for, but I don't know the exactly term. So don't hesitate to correct the title.

    Imaging the following paragraph:

    This is a very long
    sentence that is auto
    wrapped for because
    of its length.

    The paragraph has only one sentence which is auto wrapped because of it's length. There is no line break within the sentence.

    I would like to get the "line count" (4 in this example) and the text of every line.
    In addition, I would like to know the height of each line (height of the largest character).

    I just tried
    - par.LineCount -> returns 0
    - par. LineAllCount -> returns 0
    - par.Lines -> is empty
    - par.LineHeight

    It looks like that "line" is a sub sequence which ends with a "soft line break" (like "\line" in RTF).

    BTW: Is there a const for a "soft line break"? If not, how to add a "soft line break" to a paragraph?

    Thanks in advance :)
    /Steven

    • Offizieller Beitrag

    Hi,

    WPRichText.ActiveParagraph.LineCount would be the correct way to Count the lines in the current paragraph. But the text must be reformatted. Emmideately after Insertion it is not formatted, it will be formatted when the application is idle, or ReformatAll was called.

    There is also a par.GetLineCount to count in childparagraphs, too.

    This code reads the second line:

    ShowMessage(WPRichText1.ActiveParagraph.GetSubText(
    WPRichText1.ActiveParagraph.LineOffset(1),
    WPRichText1.ActiveParagraph.LineLength(1)
    ));

  • Hi again,

    It's working now. I just forgot to call "ReformatAll" after one style change.

    There remains a further question: How to get the FontSize of the highest character in a line? If this would be an expensivie operation, how to get the height of the whole paragraph in pixel (independently of the resolution, zoom)?

    I tried "paragraph.LineHeight(line)" but the result seems to be related to the resolution, zoom or something else.

    Basically, I would like to create an empty paragraph (in another "TWPRichText") which has the same height like the first one.

    My idea:

    Code
    par_copy := (par_original.CreateCopyList(False, par_original));
    par_copy.Clear;
    
    
    RichText1.AttrHelper.Clear;
    RichText1.AttrHelper.SetFontSize(HEIGHT_OF_par_original);
    
    
    par_copy.SetText(' ', RichText1.AttrHelper.CharAttr);
    // add par_copy

    Einmal editiert, zuletzt von Steven (30. Juli 2013 um 08:56)

  • Zitat von wpsupport

    LineHeight depends on the Resolution which is Memo.CurrentYPixelsPerInch - usually that is 600. It does not depend on zoom.

    You can convert that in pt, using MulDiv( val, 72, Memo.CurrentYPixelsPerInch)

    If you need Pixels, use ScreenResolution - see FormatOptions. ("Height in Pixels" always depends on Resolution)

    Hi again,

    Thank you for the answer. However, I am at a loss how to do it.
    Why do you multiply by 72? What is 72? Is it "Screen.PixelsPerInch" (should be 96)?

    I need the pixels. Could you please tell me how to get it?
    I just found "wpfAlwaysFormatWithScreenRes" in "FormatOptions" but the result is always the same if I set it to true.

    Thanks in advance :)
    /Steven

    • Offizieller Beitrag

    If you need Pixels I assume You Need Screen Pixels. You need to multiply with Screen.PixelsPerInch.

    The 72 would give You Points (pt) - a general unit for text height which can be used in SetFontSize.

    It is usually difficult to give help, when the Problem is not described, just the programming idea.

    I don't really understand the example code - why do you copy a paragraph if you later clear it.

    I would suggest to just create it in the Destination and use ACopy(source) to copy the indent and other paragraph attributes.

  • Sorry, I forgot the background information:
    We want to implement a text comparing tool using "TWPRichText". I implemented a frame with two "TWPRichText" next to each other (let's call them "Text_A" and "Text_B").
    The compare lib compares the paragraphs of "Text_A" with "Text_B".
    When the compare lib finds an added paragraph in "Text_B" an empty paragraph has to be added to "Text_A". The new paragraph needs to have the same height as "Text_B" but without text in it. This is necessary to make sure that the following paragraphs are at the same height.

    My idea is to:
    - create an empty paragraph in "Text_A"
    - calculate the total height of the paragraph added in "Text_B"
    - add an empty line (or maybe a ' ' if an empty line is not possible) to the new paragraph
    - set the font size of the added line to the calculated total height

    I tried:
    "MulDiv(par_original.LineHeight(i), 72, Memo.CurrentYPixelsPerInch)"
    but it returns 18 if the font size is 16 and 13 if the font size is 11.

    • Offizieller Beitrag

    Hi,

    if an empty paragraph needs to have the same height as a source paragraph You do not need par.LineHight but par.Height - the total height with all of its lines.

    If you use some IDs to synchronize two views, you can use the even BeforeInitailizeParagrapg to adjust he height dynamically.

    You can assign a fixed LineHeight to he empty paragraph, use WPAT_SpaceBetween with a negative value

    • Offizieller Beitrag

    A little demo,requires a button and two TWPRichText