Recent

Author Topic: Any RichMemo example projects?  (Read 2394 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Any RichMemo example projects?
« on: July 01, 2023, 07:26:45 am »
Are there any within Lazarus installations? I can't find any.
« Last Edit: July 01, 2023, 11:56:03 am by egsuh »

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #1 on: July 01, 2023, 07:34:41 am »
I found this...  It's funny that I was googling and searching Lazarus directory about for an hour and found notihng, but as soon as I posted this query then following example popped up.


https://github.com/vrybant/richdemo

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #2 on: July 01, 2023, 07:59:31 am »
Now, have anybody done transforming the content of RichMemo into HTML? It means font styles into HTML tags like <font color=red><u>... </u></font>, etc.

paweld

  • Hero Member
  • *****
  • Posts: 1434
Re: Any RichMemo example projects?
« Reply #3 on: July 01, 2023, 08:09:34 am »
Best regards / Pozdrawiam
paweld

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #4 on: July 01, 2023, 08:40:36 am »
Quote
@wp share an example: https://www.lazarusforum.de/viewtopic.php?p=135708#p135708

Wunderbar !

I don't have to try to do something myself^^

BTW, why a command window is shown when I run this program?  I'm running on Window 11.

paweld

  • Hero Member
  • *****
  • Posts: 1434
Re: Any RichMemo example projects?
« Reply #5 on: July 01, 2023, 09:48:50 am »
check "Win32 gui application" in "Project Options":   
Menu Project > Project Options... > Compiler options > Config and Target
Best regards / Pozdrawiam
paweld

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #6 on: July 01, 2023, 10:37:31 am »
Thank you very much in many ways !!!

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #7 on: July 01, 2023, 12:13:40 pm »
While testing his program, I found the charset is not converted correctly.  So I delved into wp's program a little further.

I think that conversion from Ansi code to UTF-8 is done at character base, based on the following codes. And I'm afraid this causes some problem for multi-byte characters like Korean. CP949 expresses a Korean Character with two bytes but mostly in 3 bytes in UTF8.  I think converting the whole chunk of text at once, not char by char, may solve the problem. Am I wrong?

Code: Pascal  [Select][+][-]
  1. procedure TRtf2HtmlConverter.DoCharSet;
  2. begin
  3.   case FParser.rtfMinor of
  4.     rtfMacCharSet:
  5.       FCodePage := encodingCPMac;
  6.     rtfAnsiCharSet:
  7.       // FCodePage := encodingCP1252;
  8.       FCodePage := encodingANSI;   // <== changed here
  9.     rtfPCCharSet:
  10.       FCodePage := encodingCP437;
  11.     rtfPcaCharSet:
  12.       FCodePage := encodingCP850;
  13.     otherwise
  14.       FCodePage := encodingUTF8;        // Changed here too, but this does not have any effect.
  15.       // FCodePage := encodingCP949;
  16.       // there is also a \ansicpgN, but it is not supported by RTFPars
  17.   end;
  18. end;
  19.  

Our Windows system uses CP949, not CP1252. But it should not matter as long as I use encodingANSI, as the system will interpret it as CP949.

But when I put this, English characters are displayed correctly, but not Korean characters --- they disappear (Setting FCodepage := encodingCP949 doesn't work). 

So, looking for possible reason, I found followings.


Code: Pascal  [Select][+][-]
  1. procedure TRtf2HtmlConverter.DoText;
  2. var
  3.   c: char;
  4.   s: string;
  5. begin
  6.   if (FCurrText = '') then
  7.   begin
  8.     // This is the very first character -- we must write the HTML header.
  9.     if FOutput.Count = 0 then
  10.       WriteHeader;
  11.  
  12.     if FSpanOpen then
  13.       CloseSpan;
  14.   end;
  15.  
  16.   c := chr(FParser.RTFMajor);
  17.   if c = #0 then   // last character
  18.   begin
  19.     WriteFooter;
  20.     exit;
  21.   end;
  22.  
  23.   if c > #127 then
  24.   begin
  25.     s := ConvertEncoding(c, FCodePage, encodingUTF8);   // <== I think this matters.
  26.     FCurrText := FCurrText + s;
  27.   end
  28.   else
  29.     FCurrText := FCurrText + c;
  30.  
  31.   if FParDelayed then
  32.   begin
  33.     WritePar(FParAttrib);
  34.     FParDelayed := False;
  35.     FParOpen := True;
  36.   end;
  37.  
  38.   if FSpanDelayed then
  39.   begin
  40.     WriteSpan(FCurrCharAttrib);
  41.     FSpanDelayed := False;
  42.     FSpanOpen := True;
  43.   end;
  44. end;
  45.  

I'm not sure I can modify this unit myself (unless this is prohibited), but please comment on the reason I suggested at least.


Other approach that works is as following.

First, I enforce utf8 from the beginning.

Code: Pascal  [Select][+][-]
  1. procedure TRtf2HtmlConverter.DoCharSet;
  2. begin
  3.   case FParser.rtfMinor of
  4.      rtfAnsiCharSet:
  5.           FCodePage := encodingUTF8;   // <== set utf8 here
  6.      ....
  7.   end;  // case
  8. end;  // procedure
  9.  

And the created HTML file does not appear correctly, but it contains characters. The content appears correctly in Notepad, but not on webbrowsers -- broken characters.

So, I open the HTML file in NotePad, save it again but with encoding of UTF8, not Ansi. Then webbrowsers show the contents correctly. 


wp

  • Hero Member
  • *****
  • Posts: 12909
Re: Any RichMemo example projects?
« Reply #8 on: July 01, 2023, 01:08:33 pm »
You are right, egsuh; my code in fact is very far from perfect. I have an improved version in my disk, but finally gave up to complete it because I should spend more time on how to understand the hierarchy of all the sections in the rtf. But basically the idea is correct: Using the TRtfParser from fpc's RTFPars unit you can go through the rtf document and, based on the rtfClass, rtfMajor, rtfMinor and rtfParam values provided by the parser you can decide how to convert this into html.

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #9 on: July 01, 2023, 01:19:25 pm »
@wp,
Thank you very much, wp. I'll try to modify your source as far as I can, and I might email you if there are any question.

wp

  • Hero Member
  • *****
  • Posts: 12909
Re: Any RichMemo example projects?
« Reply #10 on: July 01, 2023, 02:10:38 pm »
If you want to work on it, I probably should send you my current version to avoid that your are fixing issues that I already fixed. The file "converter_lib" in the attachment contains the unit for the rtf-to-html conversion, and "converter" is a simple command-line test application. Note that "converter_lib" contains also an extended version of fpc'c rtfpars which defines some additional rtf keys. Without it, you will not be able to use "converter_lib".

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #11 on: July 01, 2023, 02:27:21 pm »
wp,

Really appreciate. I’ll look around it.
Quick solution would be to convert whole HTML text at once, but anyway I’ll look into your additions.

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #12 on: July 12, 2023, 09:34:27 am »
@wp,

FYI,

I tested with your new files --  uRtf2Html.pas, RTFPars.pp and RTFPars.inc.

I made modifications of uRtf2Html.pas file on three procedures as following,  where I notified as BRIO did something.
This works fine. Thank you again for your efforts.

Code: Pascal  [Select][+][-]
  1. procedure TRtf2HtmlConverter.ConvertToHtml(AStream: TStream; ATitle: String);
  2. begin
  3.   // ... ......
  4.   FOutput.Clear;
  5.   // .........
  6.  
  7.   FOutput.Text := ConvertEncoding(FOutput.Text, FCodePage, encodingUTF8);  // BRIO added
  8.   FOutput.SaveToStream(AStream);
  9. end;
  10.  
  11.  
  12. procedure TRtf2HtmlConverter.DoCharSet;
  13. begin
  14.   case FParser.rtfMinor of
  15.     // ............
  16.     rtfAnsiCharSet:
  17.       FCodePage := encodingAnsi; // BRIO modified. instead of encodingCP1252;
  18.     // ...........
  19.     otherwise
  20.       FCodePage := encodingAnsi; // BRIO modified. instead of  encodingCP1252;
  21.   end;
  22. end;
  23.  
  24. procedure TRtf2HtmlConverter.DoText;
  25. const
  26.   DATA_BLOCK_SIZE = 1024;
  27. var
  28.   c: Char;
  29.   s: String;
  30. begin
  31.  
  32.    // ............................
  33.  
  34.   // Get the character...
  35.   c := chr(FParser.RTFMajor);
  36.   if c = #0 then   // last character
  37.     exit;
  38.  
  39.   // ... and append it to current text
  40.   if FActiveDestination = rtfPict then
  41.   begin
  42.      // ..............................
  43.   end else
  44.   begin
  45.     { // --------------- BRIO commented out --------
  46.     if c > #127 then
  47.     begin
  48.       s := ConvertEncoding(c, FCodePage, encodingUTF8);
  49.       FCurrText := FCurrText + s;
  50.     end else   // -- up to here }
  51.       FCurrText := FCurrText + c;
  52.   end;
  53. end;  

BTW, you are saying "an extended version of fpc'c rtfpars".  Is this used by any package of Lazarus default installation? I cannnot find the file within my Lazarus installation.

wp

  • Hero Member
  • *****
  • Posts: 12909
Re: Any RichMemo example projects?
« Reply #13 on: July 12, 2023, 12:07:28 pm »
Can you send me a file that did not convert correctly with my original code? I had tried the attached rtf file which converted correctly.

BTW, you are saying "an extended version of fpc'c rtfpars".  Is this used by any package of Lazarus default installation? I cannnot find the file within my Lazarus installation.
The original file is in the fpc folder packages/fcl-base/src which, in a Lazarus release installation, is in (Lazarus)/fpc/<version, e.g. 3.2.2>/source. But note that the FPC release versions do not contain the additions of some rtf keys that I had to make. Now that my patch https://gitlab.com/freepascal.org/fpc/source/-/issues/40336 has been accepted they are contained in FPC/main, at least.

egsuh

  • Hero Member
  • *****
  • Posts: 1623
Re: Any RichMemo example projects?
« Reply #14 on: July 12, 2023, 03:01:12 pm »
Here's the file not converted to HTML correctly.
Korean characters use CP949.  AFAIK, Codepage ANSI will interpreted as CP949 in Korean Windows.

 

TinyPortal © 2005-2018