Recent

Author Topic: Pasting from RTF Text into a stream  (Read 497 times)

zxandris

  • Full Member
  • ***
  • Posts: 109
Pasting from RTF Text into a stream
« on: March 05, 2024, 11:17:11 am »
I'm generally trying to paste RTF format convert that into other markup and then paste.  I need to get that RTF format (Which I assume HasFormat can help me with) But I honestly have no idea how to do it, then assign that to the stream I can then use.  Has anyone a snippet of code that could help me there.  I looked into the source of RichEdit Memo, but honestly that seems to inherit all over the place and I couldn't find the root of the paste code. (Probably to my lacking there).

Any help would be most appreciated.

zxandris

  • Full Member
  • ***
  • Posts: 109
Re: Pasting from RTF Text into a stream
« Reply #1 on: March 05, 2024, 11:38:45 am »
Okay, fiddling with what I could find I've made the following

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.ToolButton285Click(Sender: TObject);
  2.   {$ifdef windows}
  3.   const ClipboardFormat_RTF = 'Rich Text Format';
  4.   {$endif}
  5.   {$ifdef Linux}
  6.   const ClipboardFormat_RTF = 'text/richtext';
  7.   {$endif}
  8.   {$ifdef darwin}
  9.   const ClipboardFormat_RTF = 'text/rtf';
  10.   {$endif}
  11. var
  12.    RTFAvail : Boolean;
  13.    strm : TMemoryStream;
  14.    LST : TSTringList;
  15.    conv : TLeanHTML;
  16.    HTMSTr : STring;
  17.    formatID : integer;
  18. begin
  19.      FormatID := RegisterClipboardFormat(PChar('Rich Text Format'));
  20.      if Clipboard.HasFormat(FormatID) then
  21.         RTFAvail := True;
  22.      if RTFAvail then
  23.      begin
  24.          strm := TMemoryStream.create;
  25.          try
  26.             clipboard.GetFormat(FormatID, strm);
  27.             htmStr := strm.ToString;
  28.             htmStr := RemoveBadRTF(htmStr);
  29.             lst := TStringList.create;
  30.             try
  31.                lst.text := htmStr;
  32.                strm.clear;
  33.                lst.SaveToStream(strm);
  34.             finally
  35.                    lst.free;
  36.             end;
  37.             strm.Position := 0;
  38.             conv := TLeanHTML.Create;
  39.             try
  40.                conv.FOptions.PClass:='Para';
  41.                HTMStr := Conv.ConvertToString(Strm, ExtractFilePath('ME'));
  42.             finally
  43.                    conv.free;
  44.             end;
  45.             htmStr := StringReplaceAll(htmStr, '<b>', '[B]');
  46.             htmStr := StringReplaceAll(htmStr, '</b>', '[/B]');
  47.             htmStr := StringReplaceAll(htmStr, '<I>', '[I]');
  48.             htmStr := StringReplaceAll(htmStr, '</i>', '[/I]');
  49.             htmStr := StringReplaceAll(htmStr, '<U>', '[U]');
  50.             htmStr := StringReplaceAll(htmStr, '</U>', '[/U]');
  51.             htmStr := StringReplaceAll(htmStr, '<BR>', '');
  52.             htmStr := StringReplaceAll(htmSTR, '* * *', '[BREAK]');
  53.             HTMStr := StripHTML(htmStr);
  54.             htmStr := StringReplaceAll(htmStr, '&gt;', '>');
  55.             htmStr := StringReplaceAll(htmStr, '&lt;', '<');
  56.             htmStr := StringReplaceAll(htmStr, '&nbsp;', '');
  57.          finally
  58.                 strm.free;
  59.          end;
  60.          rtfEditor.SelTexT := htmStr;
  61.      end;
  62. end;
  63.  

It doens't work, in fact it seems to paste nothing.  Now that's all very cirticulous by converting to html then my odd little tags.  But I'll fix that later, the big ticket is that it should automatically replace some 'issues' and compatibillity.  But for right now I would like to get the RTF clipboard contents into first a string, into a stream to run my odd little converter on.

Would anyone please be willing to help me there?


Thanks,


CJ

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Pasting from RTF Text into a stream
« Reply #2 on: March 05, 2024, 11:41:56 am »
Hmm, the FCL does have an RTF parser, SRC/fpc-3.2.2/packages/fcl-base/src/rtfpars.pp

That might be a good place to start looking.  It looks like it is used in LazReport so you might, if very lucky, find some sample code. Writing a parser of your own from scratch might be a substantial undertaking !

Unless you are working with a small subset of RTF ....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

zxandris

  • Full Member
  • ***
  • Posts: 109
Re: Pasting from RTF Text into a stream
« Reply #3 on: March 05, 2024, 12:09:20 pm »
Hmm, the FCL does have an RTF parser, SRC/fpc-3.2.2/packages/fcl-base/src/rtfpars.pp

That might be a good place to start looking.  It looks like it is used in LazReport so you might, if very lucky, find some sample code. Writing a parser of your own from scratch might be a substantial undertaking !

Unless you are working with a small subset of RTF ....

Davo

That's useful info thanks, I'll look into that, oddly I am using a tiny sub-set, and have a...sorta solve, but it actually makes very rubbish html then i convert that to what I want.  I'll cut out the middle man soon.  Right now it works lol.

Generally I started a journey today to replace an annoying dash that was about immune as a cockroach to being destroyed.  But I got there in the end.  I've managed to gain the rtf, and convert it simplistically, ffor now, and remove that darn dash! :)  But thanks for your reply.  I will look into that parser and do something to improve the one I've got.  Oddly I'm not especially working in rtf, even though I'm using an rtf controll, so the simplistic mark up I use isn't so difficult to work with.  But rtf...yeah...not so easy :)

Thanks

CJ

 

TinyPortal © 2005-2018