-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!

11
  • The properties of the TRichEdit includes Font and a property of the Font is Charset. Try with EASTEUROPE_CHARSET or RUSSIAN_CHARSET Commented Sep 14, 2022 at 13:22
  • @TomBrunberg It didn't fix my problem. Commented Sep 14, 2022 at 13:26
  • 2
    This code isn't what I would describe as a minimal reproducible example but perhaps somebody can decode it Commented Sep 14, 2022 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. Commented Sep 14, 2022 at 14:42
  • 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. Commented Sep 14, 2022 at 17:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.