Read/Write CSS

<< Click to Display Table of Contents >>

Navigation:  Programming > Change text attributes in code > Styles (CSS) >

Read/Write CSS

With WPTools 8 the TWPTextStyle object, which is also the ancestor of TParagraph (see Datastructures) has the methods AGet_CSS and ASet_CSS to read and write a CSS compatible style definition. The name and parentheses are not used in this case.

 

Also the style collection has an easy way to load and save cascading style sheets, GetCSS and SetCSS. Al those methods are implemented in unit WPIOCSS which has to be linked in unless it isn't already by unit WPIO.

 

Please note, when you changed styles by directly accessing the TWPTextStyle or the style collection you need to call ReformatAll(true, true) to format and display the text. Since a TWPStyleScroller is also not informed about the change, you need to call UpdateStyleList.

 

Example:

 

procedure TForm1.GetSingleStyleCSS(Sender: TObject);

begin

  if WPStyleScroller1.CurrentStyle<>nil then

     CSSMemo.Text := WPStyleScroller1.CurrentStyle.AGet_CSS(true, false, true, true, false);

end;

 

procedure TForm1.SetSingleStyle(Sender: TObject);

begin

if WPStyleScroller1.CurrentStyle<>nil then

begin

    WPStyleScroller1.CurrentStyle.ASet_CSS(CSSMemo.Text);

    // We need to update format and screen

    WPRichText1.ReformatAll(true,true);

    WPRichText2.ReformatAll(true,true);

    WPStyleScroller1.ReformatAll(true,true);

end;

end;

 

procedure TForm1.GetStylesheetCSS(Sender: TObject);

begin

 CSSMemo.Text := WPRTFProps1.ParStyles.GetCSS;

end;

 

procedure TForm1.SetStylesheetCSS(Sender: TObject);

begin

   WPRTFProps1.ParStyles.SetCSS(CSSMemo.Text);

  // We need to update format and screen

   WPRichText1.ReformatAll(true,true);

   WPRichText2.ReformatAll(true,true);

   WPStyleScroller1.UpdateStyleList;

end;

 

clip0212

 

The TWPRichText implements some methods to load and save CSS. (Internally the methods of ParStyles are called.)

 

This method saves paragraph styles to a CSS style sheet.

   function SaveCSSheet: string;

 

This method saves paragraph styles to a CSS style sheet file.

   procedure SaveCSSFile(CSSFileName: string);

 

The following TWPRichText methods also call ReformatAll:

 

This method loads paragraph styles from a CSS style sheet file.

   function LoadCSSFile(CSSFileName: string; Merge : Boolean = false): Integer;

 

This method loads paragraph styles from a CSS style sheet.

   function LoadCSSheet(StyleString: string; Merge : Boolean = false): Integer;

 

Important:

 

WPTools also supports the proprietary WPCSS - this format is similar to CSS but optimized for performance. It can hold all text features WPTools supports. You can get and set WPCSS in styles, paragraphs, attribute interfaces etc. It is also use the WPT format to define the attributes.

 

It is incompatible to the standard CSS though and can only be used in WPTools or TextDynamic.

 

Example WPCSS description of a style sheet,

which was eead by CSSMemo.Text :=WPRTFProps1.ParStyles.GetWPCSS:

 

BODY=CharFont:'Verdana';CharFontSize:1100;

H1=CharFont:'Verdana';CharFontSize:1300;CharStyleMask:5;CharStyleON:5;

H2=CharFont:'Verdana';CharFontSize:1100;CharStyleMask:1;CharStyleON:1;

H3=CharFont:'Verdana';CharFontSize:1100;CharStyleMask:4;CharStyleON:4;

 

The names represent the WPAT_... property IDs used for the respective properties. (Please see reference for supported WPAT_ codes.)

 

To read a WPCSS string of a style use

 

function TWPTextStyle.AGetWPSS(

     Names,

     CharAttr,

     TabAttr: Boolean;

     OnlyUsePTag: Boolean = FALSE;

     Abbreviated: Boolean = FALSE): AnsiString;

 

To write it use

 

procedure TWPTextStyle.ASetWPSS(

    const WPCSSString: AnsiString;

     Merge: Boolean = false;

     Abbreviated: Boolean = FALSE);

 

If OnlyUsePTag is set to TRUE no style names are written. You should not use this feature if you want to store the created string. It will be invalid the next time the text is created.

 

If Abbreviated is TRUE the short form for the property names will be used.

 

Save and restore style sheet in WPCSS format

 

// Save:

  Memo1.Text := WPRichText1.ParStyles.GetWPCSS;

//or

  WPRichText1.ParStyles.SaveToFile('c:\stylesheet.wpcss');

// Load:

  WPRichText1.ParStyles.SetWPCSS(Memo1.Text);

  WPRichText1.ReformatAll(true,true);

//or

  WPRichText1.ParStyles.LoadFromFile('c:\stylesheet.wpcss');

  WPRichText1.ReformatAll(true,true);