-2

I have a formatted text on a wordpad file(rtf). I'm trying to open it on a richedit on a delphi form. The problem is that the string is in cyrillic(Bulgarian) and it's saved with weird hieroglyphs or whatever those are "Âëåçå ïîòðåáèòåë". Is there a way to transfer/translate the hieroglyphs to the richedit, so they can appear as proper text?

This function I use to check if the file is empty so I can then enter the first rtf tag, or remove the closing tag, so I can add more text in there without breaking the file

function FileIsEmpty(const FileName: String): Boolean;
var
  fad: TWin32FileAttributeData;
begin
  Result := GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @fad) and
            (fad.nFileSizeLow = 0) and (fad.nFileSizeHigh = 0);
end;

This is the code I use to format the text and also give it to the file:

procedure FormatLogAndAddToFile(richEditLog : TRichEdit; richEditTextColor : TRichEdit);

var
  i : integer;
  s, c, finalText : string;
  sString : TStringList;

begin
  with frmMain do
  begin
    sString := TStringList.Create;
    sString.LoadFromFile('C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf');
    if Pos('{\rtf}', sString.Strings[0]) <> 0 then
    begin
      sString.Delete(0);
    end
    else
    begin
      sString.Delete(sString.Count - 1);
    end;
    sString.SaveToFile('C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf');
    sString.free;

    AssignFile(logFile, 'C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf');
    Append(logFile);

    if FileIsEmpty('C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf') = True then
    begin
      WriteLn(logFile, '{\rtf\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}}');
    end;

    for i := 0 to richEditLog.Lines.Count do
    begin
      s := richEditLog.Lines[i];
      c := richEditTextColor.Lines[i];

      if c = 'blue' then
      begin
        finalText := '{\colortbl ;\red0\green128\blue255;\red255\green0\blue0;}' +
                       '\viewkind4\uc1 \pard\sa200\sl276\slmult1\cf1\f0\fs32\lang9 ' + s + '\cf2\par';
      end
      else if c = 'red' then
      begin
        finalText := '{\colortbl ;\red255\green0\blue0;}' +
                       '\viewkind4\uc1 \pard\sa200\sl276\slmult1\cf1\f0\fs32\lang9 ' + s + '\par';
      end
      else if c = 'green' then
      begin
        finalText := '{\colortbl ;\red0\green128\blue128;\red255\green0\blue0;}' +
                       '\viewkind4\uc1 \pard\sa200\sl276\slmult1\cf1\f0\fs32\lang9 ' + s + '\cf2\par';
      end;

      WriteLn(logFile, finalText);
    end;

    WriteLn(logFile, '}');
    CloseFile(logFile);
  end;
end;

This is the code I use to add the log lines to the file. I also have little bit of code that checks if the file has lines with a date that is entered on a TDateEdit, so I can only get log from the date I've entered.

procedure OpenLogInRichEdit(dateFilter : Boolean; searchDate : tDate);

var
  sTime : string;
  dateExists : Boolean;
  I : integer;
  
begin
  with frmMain do
  begin
    dateExists := false;
    frmLogSearch.tLogRichEdit.Clear;
    frmLogSearch.tLogRichEdit.Lines.LoadFromFile('C:\Users\lyuben\Desktop\Lyuben Airport Delphi\Log File\TestFormating.rtf');

    sTime := DateTimeToStr(searchDate); 

    if dateFilter then
    begin
      for I := 0 to frmLogSearch.tLogRichEdit.Lines.Count do
      begin
        if Pos(sTime, frmLogSearch.tLogRichEdit.Lines[i]) <> 0 then
        begin
          frmLogSearch.tLogRichEdit.Lines.Delete(i);
          dateExists := True;
        end;
      end;

      if dateExists = false then
      begin
        ShowMessage('No log from this day!');
      end;
    end;
  end;
end;

This is how I add the text to the richedits I use later for the procedure FormatLogAndAddToFile.

dateTimeNow := Now;

  logText.Lines.Add('<' + DateTimeToStr(dateTimeNow) + '> Изтрита е поръчка');
  logTextColor.Lines.Add('red');

And this is how I eventually call the procedures. First the procedure to get the formatted log to the richedits

OpenLogInRichEdit(tcxCheckBoxDate.Checked, tcxDate.Date);

And this is the procedure to format the text and give it to the file

LogFileUse.FormatLogAndAddToFile(logText, logTextColor);

Thanks to the comments I've managed to make it work. I've changed the code above. Instead of having 'fcharset0' as a tag, I now have 'fcharset1' and I also changed 'lang9' to 'lang1026' and now I save it properly to the file and it opens perfectly!

Ineffable21
  • 153
  • 1
  • 13
  • The properties of the `TRichEdit` includes `Font` and a property of the `Font` is `Charset`. Try with `EASTEUROPE_CHARSET` or `RUSSIAN_CHARSET` – Tom Brunberg Sep 14 '22 at 13:22
  • @TomBrunberg It didn't fix my problem. – Ineffable21 Sep 14 '22 at 13:26
  • Not at all or partly? Do you see the texts correctly in Wordpad or any other sw? Is the file from this millennium or from the previous.? – Tom Brunberg Sep 14 '22 at 13:28
  • @TomBrunberg There is no change whatsoever. In fact I was using russian_charset anyway. I'm pretty sure the problem comes from the wordpad itself. When I open it it shows the weird text. I can type normal letter in it and it appears as Bulgarian letters, there's no problem with that. The problem I think comes from me formatting the text with delphi, which I do by entering the rtf tags myself, which seems to be the only way to format with wordpad. Then the text appears formatted but with weird letters, aka hieroglyphs. When I open it with notepad the text is in normal letters but with the tags. – Ineffable21 Sep 14 '22 at 13:34
  • Please provide [mcve] – David Heffernan Sep 14 '22 at 13:36
  • 1
    This code isn't what I would describe as a [mcve] but perhaps somebody can decode it – David Heffernan Sep 14 '22 at 13:58
  • 1
    One thing I still have not worked out, is why you are hacking around with raw RTF. What would be perhaps awesome would be if you could provide an RTF file that doesn't display as you hope. Make it as small as possible. Should be possible to load it into a rich edit control with a one liner and see the issue. Also, if you actually simplify things you might be able to debug it yourself. – David Heffernan Sep 14 '22 at 14:42
  • What is lang9 in the RTF supposed to mean? Bulgarian language is supposed to be set as \lang1026 AFAIK – dwrbudr Sep 14 '22 at 17:03
  • 2
    Which Delphi version are you using? Why is this even important? Well the first line that you write into your RTF file is `{\rtf\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}}` which defines that all the text stored in RTF file is of ANSI string format. But default string format in modern Delphi versions (Delhi 2009 and newer) is Unicode string. So if you are using modern Delphi you should change your string types from `string` to `AnsiString`. – SilverWarior Sep 14 '22 at 17:15
  • @dwrbudr Changing my \lang09 tag to \lang1026 doesn't seem to work. I also tried to write some text in bulgarian in wordpad and then opened it in notepad to get the different strings and although there were more tags, the \lang9 tag was still there. Then I copied those tags and put them in my code but that only led to them losing their formatting. – Ineffable21 Sep 15 '22 at 06:54
  • @Ineffable21: Change fcharset0 to fcharset1 – dwrbudr Sep 15 '22 at 12:01

1 Answers1

0

If all this scary code is here only to add colored lines to the file, than you should use TRichEdit.SelAttributes with friends: Colorful text in the same line in TRichEdit This way TRichEdit will be able to correctly handle encoding. And if you need some fancy file header or footer, that you do not want to create from code, than you can create empty rtf-file with required header/footer, and use it as a template.

Torbins
  • 2,111
  • 14
  • 16