您正在处理 UTF“代理对”:
使用 UTF-16,从 $D800-DBFF 到 $DC00-DCFF 的值范围用于指定所谓的代理对。
使用这些代理对,我们可以映射 10000 美元或更高的 Unicode 代码点(在 10000 美元到 10FFFD 的范围内)。
这是通过从值中减去 $10000 来完成的,留下一个介于 0 到 $FFFFD 之间的值,可以用 20 位表示。这 20 位分为两对,每对 10 位,分别添加到 $D800 中。$DC00 双。
因此,对于 Unicode 代码点 $1D11E,UTF-16 代理对的计算如下:首先减去 $10000,剩下 $D11E,即 00001101000100011110 为 20 位,分为 $34 和 $11E。$34 加到 $D800 中,$11E 加到 $DC00 中,结果是最重要的替代品为 $D834,而最不重要的替代品为 $DD1E。
[请注意,Unicode 标准不会为 Unicode 代码点 $D800 到 $DFFD 分配有效字符(以避免 UTF-16 出现问题),因此各个代理字符本身永远不会映射到实际字符本身(但应始终成对使用)。]
另见http://en.wikipedia.org/wiki/UTF-16
要正确显示代理对字符,您需要包含它们的字体。例如,Windows 字体 Code2001, Euterpe, Free Serif, Musica, Quivira, Symbola (http://www.alanwood.net/unicode/) 支持 U+1D100 – U+1D1FF (119040–119295) 范围内的音乐符号fontsbyrange.html#u1d100)
您需要在系统上下载并安装 Musica 字体(以前称为 Musical Symbols)才能使该示例正常工作。下载位置 eg http://users.teilar.gr/~g1951d/
[Win7下安装:在ttf文件上右击选择'install']
[测试页面:http://www.alanwood.net/unicode/音乐符号.html]
这是我使用上述代码的示例 Delphi XE2 测试代码(您有 D2007,但这可能会让您上路)。
unit uSurrogatePairs;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TFrmSurrogatePairs = class(TForm)
MmoCharacter: TMemo;
Mmo: TMemo;
procedure FormShow(Sender: TObject);
private
procedure Log(S: String);
public
{ Public declarations }
end;
var
FrmSurrogatePairs: TFrmSurrogatePairs;
implementation
{$R *.dfm}
type
TDanishString = type ansistring(1252);
procedure TFrmSurrogatePairs.FormShow(Sender: TObject);
// Code adapted from http://compaspascal.blogspot.nl/2008/10/delphi-2009-strings-explained-by.html
var
UTF16Str : string;
UTF8Str : utf8string;
DanishStr: TDanishString;
L : Integer;
begin
{ TODO -oJan -cShouldHave : Test if Musica font is installed }
UTF16Str:=#$1D160;
MmoCharacter.Text := UTF16Str;
L := length(UTF16Str);
Assert (L=2);
Log('Assigned: UTF16Str := #$1D160');
Log(' This is a musical note (000011101000101100000),');
log(' see http://unicode.org/charts/PDF/U1D100.pdf');
Log('Length(UTF16Str)=2');
Log(' This character occupies 2 positions in UTF-16');
Assert (UTF16Str[1]=#$D834); // 110110 0000110100 First half of the symbol
Assert (UTF16Str[2]=#$DD60); // 110111 0101100000 Second half of the symbol
Log('UTF16Str[1]=#$D834');
Log('UTF16Str[2]=#$DD60');
UTF8Str := utf8string(UTF16Str);
MmoCharacter.Lines.Add(String(UTF8Str));
Log('');
Log('Assigned: UTF8Str := UTF16Str');
Log(' This is the second line (char) in the left memo');
L := Length(UTF8Str);
Assert (L=4);
Log('Length(UTF8Str)=4');
Log(' This character occupies 4 positions in UTF-8, each 1 byte');
Assert (UTF8Str[1]=#$F0); // 11110 000
Assert (UTF8Str[2]=#$9D); // 10 011101
Assert (UTF8Str[3]=#$85); // 10 000101
Assert (UTF8Str[4]=#$A0); // 10 100000
DanishStr:=UTF16Str;
Assert (DanishStr='??'); // Note how Windows incorrectly converts to two letters!
Assert (length(DanishStr)=2);
DanishStr:=UTF8Str;
Assert (DanishStr='??'); // Note how Windows incorrectly converts to two letters!
Assert (length(DanishStr)=2);
end;
procedure TFrmSurrogatePairs.Log(S: String);
begin
Mmo.Lines.Add(S);
end;
end.
和 DFM:
object FrmSurrogatePairs: TFrmSurrogatePairs
Left = 0
Top = 0
Caption = 'Surrogate pairs'
ClientHeight = 273
ClientWidth = 600
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object MmoCharacter: TMemo
AlignWithMargins = True
Left = 3
Top = 3
Width = 134
Height = 267
Align = alLeft
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -107
Font.Name = 'Musica'
Font.Style = []
ParentFont = False
ReadOnly = True
TabOrder = 0
end
object Mmo: TMemo
AlignWithMargins = True
Left = 143
Top = 3
Width = 454
Height = 267
Align = alClient
Lines.Strings = (
'')
ReadOnly = True
TabOrder = 1
end
end