Page 1 of 1

Invalid floating point operation

Posted: Thu Nov 03, 2011 7:13 pm
by rafakwolf
I get this error when i use following methods.

Code: Select all

function GetNewStyle(rvs: TRVStyle; BaseStyleNo: Integer; FontName: string; Color: TColor;
  Style: TFontStyles; Size: Integer): Integer;
begin
  Result := rvs.TextStyles.FindStyleWithColor(BaseStyleNo, Color, clNone);
  Result := rvs.TextStyles.FindStyleWithFontStyle(BaseStyleNo, Style, []);
  Result := rvs.TextStyles.FindStyleWithFontSize(BaseStyleNo, Size);
  Result := rvs.TextStyles.FindStyleWithFontName(BaseStyleNo,FontName);

  if (Result < 0) then
  begin
    rvs.TextStyles.Add;
    Result := rvs.TextStyles.Count-1;
    rvs.TextStyles[Result].Assign(rvs.TextStyles[BaseStyleNo]);
    rvs.TextStyles[Result].Standard := False;
    rvs.TextStyles[Result].Color := Color;
    rvs.TextStyles[Result].BackColor := clNone;
    rvs.TextStyles[Result].Style := Style;
    rvs.TextStyles[Result].Size := Size;
    rvs.TextStyles[Result].FontName := FontName;
  end;
end;

function GetNewPara(rvs: TRVStyle; BaseParaNo: Integer; Align: TRVAlignment): Integer;
begin
  Result := rvs.ParaStyles.FindStyleWithAlignment(BaseParaNo, Align);

  if (Result < 0) then
  begin
    rvs.ParaStyles.Add;
    Result := rvs.ParaStyles.Count-1;
    rvs.ParaStyles[Result].Assign(rvs.ParaStyles[BaseParaNo]);
    rvs.ParaStyles[Result].Standard := False;
    rvs.ParaStyles[Result].Alignment := Align;
  end;
end;

procedure AddRichText(RichEd: TppRichText; Texto: String; FontName: string;
  Style: TFontStyles; Size: Integer; Align: TAlignRich; Parent : TWinControl);
var
  mem : TMemoryStream;
  rve : TRichViewEdit;
  rvs : TRVStyle;
  lAlign: TRVAlignment;
begin
  rve := TRichViewEdit.Create(Parent);
  rvs := TRVStyle.Create(Parent);

  mem := TMemoryStream.Create;

  try
    rve.Parent := Parent;
    rve.Style := rvs;

    case Align of
       alrLeft    : lAlign := rvaLeft;
       alrCenter  : lAlign := rvaCenter;
       alrRight   : lAlign := rvaRight;
       alrJustify : lAlign := rvaJustify;
    end;

    rve.Clear;
    rve.CurTextStyleNo := GetNewStyle(rvs,0,FontName,clBlack,Style,Size);
    rve.CurParaStyleNo := GetNewPara(rvs,0,lAlign);
    rve.InsertText(Texto);

    rve.SelectAll;
    rve.FormatTail;

    mem.Clear;
    rve.SaveRTFToStream(mem,False);
    mem.Position := 0;

    RichEd.Text := '';
    RichEd.LoadFromRTFStream(mem);
  finally
    mem.Free;
    rve.Free;
    rvs.Free;
  end;
end;
call this way

Code: Select all

      AddRichText(MyppRichView,
        'MY TEXT MY TEXT MY TEXT MY TEXT',
        'Times New Roman',[],12,alrJustify, TheParentObject);
something wrong ?

thanks.

Posted: Thu Nov 03, 2011 7:22 pm
by Sergey Tkachenko
Please send me a sample project

Posted: Thu Nov 03, 2011 7:59 pm
by rafakwolf
i can´t...because this problem dont happen all the time

sometimes happen, sometimes don´t happen...

exists another way for do that ?

thanks.

sorry for my english! :)

Posted: Thu Nov 03, 2011 8:58 pm
by Sergey Tkachenko
This code overcomplicated. If you create a new TRichViewEdit, you do not need to use searching for styles. You can simple add the necessary styles and use them.

GetNewStyle is wrong: for searching for a style with many properties, you should use FindSuchStyle. However, it should not lead to error.

InsertText requires formatted document, so you must call Format after Clear. This is a serious bug, but I doubt that it leads to floating point error...

I'd write the code like this, see below. RTF saving requires formatting only if document contains tables, so I did not use editing methods or formatting.

Code: Select all

procedure AddTextStyle(rvs: TRVStyle; const AFontName: string; AColor: TColor; 
  AStyle: TFontStyles; ASize: Integer); 
begin 
  with rvs.TextStyles.Add do begin
    Color := AColor; 
    Style := AStyle; 
    Size := ASize; 
    FontName := AFontName; 
  end; 
end; 

procedure AddParaStyle(rvs: TRVStyle; AAlignment: TRVAlignment); 
begin 
  with rvs.ParaStyles.Add do begin
    Alignment := AAlignment; 
  end; 
end; 

procedure AddRichText(RichEd: TppRichText; Texto: String; FontName: string; 
  Style: TFontStyles; Size: Integer; Align: TAlignRich); 
var 
  mem : TMemoryStream; 
  rv : TRichView; 
  rvs : TRVStyle; 
  lAlign: TRVAlignment; 
begin 
  rv := TRichView.Create(nil); 
  rvs := TRVStyle.Create(rv); 

  mem := TMemoryStream.Create; 

  try 
    rv.Style := rvs; 

    case Align of 
       alrLeft    : lAlign := rvaLeft; 
       alrCenter  : lAlign := rvaCenter; 
       alrRight   : lAlign := rvaRight; 
       alrJustify : lAlign := rvaJustify; 
    end; 
    
    rvs.TextStyles.Clear;
    rvs.ParaStyles.Clear;
    AddTextStyle(rvs, FontName,clBlack,Style,Size); 
    AddParaStyle(rvs,0,lAlign); 
    rv.AddTextNL(Texto,0,0,0);
    rve.SaveRTFToStream(mem,False); 
    me.Position := 0; 
    RichEd.Text := ''; 
    RichEd.LoadFromRTFStream(mem); 
  finally 
    mem.Free; 
    rv.Free; 
  end; 
end;

Posted: Thu Nov 03, 2011 8:59 pm
by Sergey Tkachenko
Wait! You just need to add text to ppRichView?
You can simple add necessary styles to RichEd.RichView.Style and then call RichEd.AddTextNL, without all these difficulties.

Posted: Fri Nov 04, 2011 10:22 am
by rafakwolf
Sergey Tkachenko wrote:Wait! You just need to add text to ppRichView?
You can simple add necessary styles to RichEd.RichView.Style and then call RichEd.AddTextNL, without all these difficulties.
exactly!

I will try...and post results later ...

thanks!

Posted: Fri Nov 04, 2011 10:42 am
by rafakwolf
for what i need the RichViewEd ?

can i just use the ppRichView ?

this way

Code: Select all

 rvs  := TRVStyle.Create(nil);

  RichView.RichView.AddTextNL(Texto,
    GetNewStyle(rvs,0,FontName,clBlack,Style,Size),
    GetNewPara(rvs,0,lAlign),0);
thanks.

Posted: Tue Nov 08, 2011 7:11 pm
by Sergey Tkachenko
Use ppRichView.RichView.Style instead of rvs.