Using CallForSelectedText to change font attributes and blank lines

  • I use CallForSelectedText with the TWPCharAttrCallback to allow me to optionally change fonts and sizes of certain characters. I also want the font and size to change on blank paragraphs but it isn't working for that since there aren't any characters.

    For example, if all my text is 12pt but a few empty paragraphs are in 11pt and I use the TWPCharAttrCallback to change all chars to 10pt, it works except the blank paragraphs are still set to 11pt. If you start typing on those blank lines, the text is in the font and size of that paragraph.

    I know that if I had the cursor on that line and change it via WPRichText1.CurrAttr.Size, it works because it references the Cursor.WritingTextAttr (if nothing is selected), but how do I do that in CallForSelectedText in which case it would change everything but as I mentioned, I conditionally change certain chars? I thought about looking for blank paragraphs in the TWPParCallback, but that is where I am stuck when I do find an empty par... how to change the blank paragraph's font, size and style?

    Hopefully an easy answer, thanks.

    • Offizieller Beitrag

    As you probably know characters use a index value (CharAttr) which references to the character attribute table in RTFProps.

    This means that you only have to assign the index value to assign multiple characters to make them use all the same attributes.

    On the other hand also paragraphs may have attributes. The one with the low numbers (up to 15) are the characer attributes. They are assigned with par.ASet. The character attributes however have precedence, so if a paragraph uses an attribute also the character use, you will not see the value of the paragraph.

    In you case you need a character attribute set for an empty paragraph which should be applied to characters as soon as they are typed in. The index value for this new characters is stored in TParagraph.LoadedCharAttr. So you need to assign the calculated index value there. (the name is historic, since RTF uses something like this when loading empty paragraphs)

    To calculate the index values you can use the AttrHelper object.

    Code
    var cha: TWPStoredCharAttrInterface;
    var HeadlineA : Cardinal;
    
    cha := RTFMemo.HeaderFooter.RTFProps.AttrHelper;
    cha.Clear;
    cha.SetFontName('Times New Roman');
    cha.SetFontSize(12);
    HeadlineA := cha.CharAttr;

    Also see the demo unit WPCreateDemoText.pas.