Peter Graf
Peter Graf

Reputation: 23

Cannot see UTF8-characters in Delphi 2010 Memo

I read an UTF8-File, made with Winword, into a Tmemo, using the code below (tried all 2 methods). The file contains IPA pronunciation characters. For these characters, I see only squares. I tried different versions of tmemo.font.charset, but it did not help.

What can I do?

Peter

// OD is an TOpenDialog

procedure TForm1.Load1Click(Sender: TObject);

{
var fileH: textFile;
    newLine: RawByteString;

begin
   if od.execute (self.Handle) then begin
      assignFile(fileH,od.filename);
      reset(fileH);
      while not eof(fileH) do begin
        readln(fileH,newLine);
        Memo1.lines.Add(UTF8toString(newLine));
      end;
      closeFile(fileH);
   end;
end;
}


var
  FileStream: tFileStream;
  Preamble: TBytes;
  memStream: TMemoryStream;
begin
  if od.Execute then
  begin
    FileStream := TFileStream.Create(od.FileName,fmOpenRead or fmShareDenyWrite);
    MemStream := TMemoryStream.Create;

    Preamble := TEncoding.UTF8.GetPreamble;
    memStream.Write(Preamble[0],length(Preamble));
    memStream.CopyFrom(FileStream,FileStream.Size);
    memStream.Seek(0,soFromBeginning);

    memo1.Lines.LoadFromStream(memStream);

    showmessage(SysErrorMessage(GetLastError));

    FileStream.Free;
    memStream.Free;
  end;
end;

Upvotes: 1

Views: 5898

Answers (2)

David Heffernan
David Heffernan

Reputation: 613442

For these characters, I see only squares.

The squares indicate that the font does not contain glyphs for those characters. You'll need to switch to a font that does. Assuming that your file has been properly encoded and that you are reading in the code points that you intend to.

You can pass TEncoding.UTF8 to the LoadFromFile method to avoid having to add a BOM to the content. Finally, don't call GetLastError unless the Win32 documentation says it has meaning. Where you call it, there is no reason to believe that the value has any meaning.

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 597941

First, you are doing too much work. Your code can be simplified to this:

procedure TForm1.Load1Click(Sender: TObject);
begin
  if od.Execute then
    memo1.Lines.LoadFromFile(od.FileName, TEncoding.UTF8);
end;

Second, as David said, you need to use a font that supports the Unicode characters/glyphs that are stored in the file. It is not enough to set the Font.Charset, you have to set the Font.Name to a compatible font. Look at the fonts that loursonwinny mentioned.

Upvotes: 5

Related Questions