2014-09-04 54 views
0

我使用下面的代码(尝试了所有2种方法),将使用Winword制作的UTF8文件读入Tmemo。该文件包含IPA发音字符。对于这些角色,我只看到正方形。我尝试了tmemo.font.charset的不同版本,但它没有帮助。在Delphi 2010中无法看到UTF8字符备注

我该怎么办?

彼得

// 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; 
+0

你确定你使用的字体包含这些字符吗? – FileVoyager 2014-09-04 15:16:06

+0

on http://ipa.typeit.org/他们推荐以下字体:Segoe UI,Cambria,Calibri,Ari​​al,Times New Roman,Tahoma或Lucida Sans Unicode(不完整) – FileVoyager 2014-09-04 15:18:37

+0

通过“Winword”,我认为你的意思是“ Word for Windows“(通常被称为”Word“),Word不会创建文本文件,除非您使用”另存为“和更改文件类型专门告诉它,所以很可能您所看到的方块是非文本字符,你是否像记事本一样检查了文件,看它是否可读? – 2014-09-04 15:26:54

回答

1

对于这些角色,我只看到广场。

正方形表示字体不包含这些字符的字形。你需要切换到一个字体。假设您的文件已被正确编码,并且您正在阅读您打算使用的代码点。

您可以将TEncoding.UTF8传递给LoadFromFile方法以避免必须向内容添加BOM。最后,除非Win32文档声明它有意义,否则不要致电GetLastError。在你称之为的地方,没有理由相信价值有任何意义。

2

首先,你正在做的工作太多。您的代码可以简化为这样:

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

其次,正如大卫说,你需要使用支持Unicode字符/指存储在文件中的字形字体。仅设置Font.Charset是不够的,您必须将Font.Name设置为兼容的字体。看看loursonwinny提到的字体。