Strange behavior re: font size & alignment

  • A user found the following bug in a system i wrote. This is written in Delphi 2007 using v5.40 of WPTools.

    In a nutshell, the editor is very simplified. A single line of text can only be one font size, etc, so the commands in general apply to the "current line".

    I sent the font size for a line with the following code:

    WPRichText1.TextCursor.SelectLine;
    WPRichText1.TextCursor.SelectedTextAttr.SetFontSize(fs);
    WPRichText1.HideSelection;

    I then set the justification like this:

    case _Tag of
    0:
    WPRichTExt1.CurrAttr.Alignment := paralLeft;
    1:
    WPRichTExt1.CurrAttr.Alignment := paralCenter;
    2:
    WPRichTExt1.CurrAttr.Alignment := paralRight;
    end;

    The issue that came up seems to arise ONLY when working with the last line in the editor.

    For example, they set a font size for the last line, then center-justify that line. What happens is it is center justified, but the font size reverts to the previous size.

    Why would this happen? Something to do with line enders?

    Thanks,

    alan

  • Hi Julian -

    Thanks for the suggestions. One followup question:

    If i want to set the font size for a TParagraph, is this code correct?

    WPRichText1.ActiveParagraph.ASet(WPAT_CharFontSize,fs);

    If i do this, the attribute is set, but the font size is NOT changed in the editor. Is there a better way to do it to set the font size for an entire paragraph?

    Also, by using the TParagraph API, is it just as easy, or easier, to do full or partial bolding of a paragraph? Or would i have to revert to the character-based attribute approach for that?

    Thanks,

    Alan

  • Thanks again Julian,

    And I have another question...

    Again, on the TParagraph API.

    Lets say i have a paragraph with one sentance, and 6 words. Is it possible, via only TParagraph methods, to bold the first 3 words? OR would this require using character methods?

    Currently I'm accomplishing this via the TextCursor.SetSelPosLen() call and teh TextCursor.SelectedTextAttr.SetFontStyle call.

    Thanks,

    alan

    • Offizieller Beitrag
    Zitat von aolson

    Again, on the TParagraph API.

    Lets say i have a paragraph with one sentance, and 6 words. Is it possible, via only TParagraph methods, to bold the first 3 words? OR would this require using character methods?

    There is a difference if You need to assign a fixed attribute (i.e. Arial, Bold) to the part of the text, or if you want to add the bold attribute. The first is explained in file WPCreateDemoText.pas (demo Task\CreateTable), here, the last " for i:=0 to l-1 do.." I add the style.

    The most difficult part here is to locate those 3 words, so WPTools 6 will add a GetWordOffset method.

    • Offizieller Beitrag

    Hi,

    WPRichText1.AttrHelper.CharAttr calculates a new character attribute index (into the attribute cache)

    The value is a 32 bit value, but only 24 are used. The 8 bits at the high side are used for status flags, such as spellcheck markers.

    The calculation

    (par.CharAttr[i] and $FF000000) or new_value

    assigns the new value and preserves the existing status flags.

    Julian

  • I"m making progress, but just ran into one problem.

    I have the code working to partially bold a line via te TParagraph methods. Thanks.

    I also have the code working to set the font size for a TParagraph.

    But, if I change the font size for a paragraph AFTER partially bolding a line, the bolding attribute is gone after the point size change. This is obviously not how i want this to work.

    do i have to go thorugh by character and do something similar to the bolding code to retain this when the point size is changed for a paragraph? Remember, i'm not doing any selection. Each paragraph has to have the same point size throughout it.

    alan

    • Offizieller Beitrag
    Zitat

    But, if I change the font size for a paragraph AFTER partially bolding a line, the bolding attribute is gone after the point size change. This is obviously not how i want this to work.

    Then You need to set the paragraph font size differently - similar code as setting bold

    Code
    for i:=0 to par.CharCount-1 do 
     begin 
       WPRichText1.AttrHelper.CharAttr := par.CharAttr[i]; 
       WPRichText1.AttrHelper.SetFontSize(11); 
       par.CharAttr[i] := (par.CharAttr[i] and $FF000000) or  WPRichText1.AttrHelper.CharAttr; 
     end;

    As I pointed out above, CharAttrClear will delete the character attributes - and so the "bold" markers, too. It sets all CharAttr[] to 0.

    Julian