参考 《让Fastreport3.x支持中文PDF的输出》一文,确实可以支持中文。但是发现两个问题:
1、只能在Adobe Reader下打开,在Foxit Reader下空白。
2、用Adobe Acrobat或者Adobe Reader打开时提示rebuild,关闭时提示save
研究了一下Fastreport3.14和3.07的源代码,发现关于字体这部分重新调整了。在3.07下,每个type0的字体有三个obj表示,而在3.14下只用两个obj(把FontDescriptor放进实际字体中去了)。3.14中源代码中有个小bug,漏掉了实际字体obj的ref(原frxPDFfile.pas的985行和986行之间)。Foxit Reader应该是严格按照ref去找obj的,找不到obj,所以显示不出来。Adobe Reader应该不是按ref找的,能够正常显示汉字,但是它会重新ref,所以提示rebuild。
修改后的TfrxPDFFont.SaveToStream替换原来的即可。
镶入字体还不行。有时间再说。3.15的说明中有说增强了PDF导出的功能,期待中。
procedure TfrxPDFFont.SaveToStream(Stream: TStream);
var
s: string;
b: TBitmap;
pm: ^OUTLINETEXTMETRIC;
FontName: string;
i: Cardinal;
pfont: PChar;
FirstChar, LastChar: Integer;
MemStream: TMemoryStream;
MemStream1: TMemoryStream;
pwidths: PABC;
Charset: TFontCharSet;
// support DBCS font name encoding
function EncodeFontName(AFontName: string): string;
var
s: string;
Index, Len: Integer;
begin
// Add Begin by ijia 2004.12.20
// 修正在简体系统下繁体字体名的问题
// 只提供 MingLiU, PMingLiU --> 细明体, 新细明体的修正
s:=UpperCase(AFontName);
if Copy(s, 1, 7)='MINGLIU' then
AFontName:='细明体';
if Copy(s, 1, 8)='PMINGLIU' then
AFontName:='新细明体';
// Add end
s := '';
Len := Length(AFontName);
Index := 0;
while Index < Len do
begin
Index := Index + 1;
if Byte(AFontName[Index]) > $7F then
s := s + '#' + IntToHex(Byte(AFontName[Index]), 2)
else
s := s + AFontname[Index];
end;
Result := s;
end;
begin
inherited SaveToStream(Stream);
b := TBitmap.Create;
b.Canvas.Font.Assign(Font);
b.Canvas.Font.PixelsPerInch := 96;
b.Canvas.Font.Size := 750;
i := GetOutlineTextMetrics(b.Canvas.Handle, 0, nil);
GetMem(pm, i);
try
try
GetOutlineTextMetrics(b.Canvas.Handle, i, pm);
FirstChar := Ord(pm.otmTextMetrics.tmFirstChar);
LastChar := Ord(pm.otmTextMetrics.tmLastChar);
FontName := StringReplace(Font.Name, ' ', '#20', [rfReplaceAll]);
s := '';
if fsBold in Font.Style then
s := s + 'Bold';
if fsItalic in Font.Style then
s := s + 'Italic';
if s <> '' then
FontName := FontName + ',' + s;
Charset := pm.otmTextMetrics.tmCharSet;
FontName := EncodeFontName(FontName);
Parent.XRefAdd(Stream);
WriteLn(Stream, IntToStr(Index + Parent.FStartFonts) + ' 0 obj');
WriteLn(Stream, '<<');
WriteLn(Stream, '/Type /Font');
WriteLn(Stream, '/Name /F' + IntToStr(Index - 1));
WriteLn(Stream, '/BaseFont /' + FontName);
// Add by ijia 2004.12.20
//if Charset <> CHINESEBIG5_CHARSET then
if not (Charset in [CHINESEBIG5_CHARSET, GB2312_CHARSET]) then
WriteLn(Stream, '/Subtype /TrueType')
else
WriteLn(Stream, '/Subtype /Type0');
case Charset of
SYMBOL_CHARSET, ANSI_CHARSET:
WriteLn(Stream, '/Encoding /WinAnsiEncoding');
RUSSIAN_CHARSET: {1251}
begin
WriteLn(Stream, '/Encoding <
>');
end;
EASTEUROPE_CHARSET: {1250}
begin
WriteLn(Stream, '/Encoding <
>');
end;
GREEK_CHARSET: {1253}
begin
WriteLn(Stream, '/Encoding <
>');
end;
TURKISH_CHARSET: {1254}
begin
WriteLn(Stream, '/Encoding <
>');
end;
HEBREW_CHARSET: {1255}
begin
WriteLn(Stream, '/Encoding <
>');
end;
ARABIC_CHARSET:
begin
WriteLn(Stream, '/Encoding <
>');
end;
VIETNAMESE_CHARSET:
begin
WriteLn(Stream, '/Encoding <
>');
end;
CHINESEBIG5_CHARSET: {136}
begin
WriteLn(Stream, '/DescendantFonts [' + IntToStr(Index + 1 + Parent.FStartFonts) + ' 0 R]');
WriteLn(Stream, '/Encoding /ETenms-B5-H');
WriteLn(Stream, '>>');
WriteLn(Stream, 'endobj');
// Add by MagicDog
Parent.XRefAdd(Stream);
WriteLn(Stream, IntToStr(Index + 1 + Parent.FStartFonts) + ' 0 obj');
WriteLn(Stream, '<<');
WriteLn(Stream, '/Type /Font');
WriteLn(Stream, '/Subtype');
WriteLn(Stream, '/CIDFontType2');
WriteLn(Stream, '/BaseFont /' + EncodeFontName(FontName));
WriteLn(Stream, '/WinCharSet 136');
Write(Stream, '/FontDescriptor ');
WriteLn(Stream, '<<');
WriteLn(Stream, '/Type /FontDescriptor');
if Parent.FEmbedded then
WriteLn(Stream, '/FontFile2 ' + IntToStr(Index + 2 + Parent.FStartFonts) + ' 0 R');
WriteLn(Stream, '/FontName /' + EncodeFontName(FontName));
WriteLn(Stream, '/Flags 7');
WriteLn(Stream, '/FontBBox [' + IntToStr(pm^.otmrcFontBox.Left) + ' ' + IntToStr(pm^.otmrcFontBox.Bottom) + ' ' + IntToStr(pm^.otmrcFontBox.Right) + ' ' + IntToStr(pm^.otmrcFontBox.Top) + ' ]');
WriteLn(Stream, '/Style << /Panose <010502020300000000000000> >>');
WriteLn(Stream, '/Ascent ' + IntToStr(pm^.otmAscent));
WriteLn(Stream, '/Descent ' + IntToStr(pm^.otmDescent));
WriteLn(Stream, '/CapHeight ' + IntToStr(pm^.otmTextMetrics.tmHeight));
WriteLn(Stream, '/StemV ' + IntToStr(50 + Round(sqr(pm^.otmTextMetrics.tmWeight / 65))));
WriteLn(Stream, '/ItalicAngle ' + IntToStr(pm^.otmItalicAngle));
WriteLn(Stream, '>>');
WriteLn(Stream, '/CIDSystemInfo');
WriteLn(Stream, '<<');
WriteLn(Stream, '/Registry(Adobe)');
WriteLn(Stream, '/Ordering(CNS1)');
WriteLn(Stream, '/Supplement 0');
WriteLn(Stream, '>>');
WriteLn(Stream, '/DW 1000');
WriteLn(Stream, '/W [1 95 500]');
WriteLn(Stream, '>>');
WriteLn(Stream, 'endobj');
end;
// Add begin by ijia 2004.12.20
GB2312_CHARSET: {134}
begin
WriteLn(Stream, '/DescendantFonts [' + IntToStr(Index + 1 + Parent.FStartFonts) + ' 0 R]');
WriteLn(Stream, '/Encoding /GB-EUC-H');
WriteLn(Stream, '>>');
WriteLn(Stream, 'endobj');
// Add by MagicDog
Parent.XRefAdd(Stream);
WriteLn(Stream, IntToStr(Index + 1 + Parent.FStartFonts) + ' 0 obj');
WriteLn(Stream, '<<');
WriteLn(Stream, '/Type /Font');
WriteLn(Stream, '/Subtype');
WriteLn(Stream, '/CIDFontType2');
WriteLn(Stream, '/BaseFont /' + EncodeFontName(FontName));
WriteLn(Stream, '/WinCharSet 134');
Write(Stream, '/FontDescriptor ');
WriteLn(Stream, '<<');
WriteLn(Stream, '/Type /FontDescriptor');
if Parent.FEmbedded then
WriteLn(Stream, '/FontFile2 ' + IntToStr(Index + 2 + Parent.FStartFonts) + ' 0 R');
WriteLn(Stream, '/FontName /' + EncodeFontName(FontName));
WriteLn(Stream, '/Flags 6');
WriteLn(Stream, '/FontBBox [' + IntToStr(pm^.otmrcFontBox.Left) + ' ' + IntToStr(pm^.otmrcFontBox.Bottom) + ' ' + IntToStr(pm^.otmrcFontBox.Right) + ' ' + IntToStr(pm^.otmrcFontBox.Top) + ' ]');
WriteLn(Stream, '/Style << /Panose <010502020400000000000000> >>');
WriteLn(Stream, '/Ascent ' + IntToStr(pm^.otmAscent));
WriteLn(Stream, '/Descent ' + IntToStr(pm^.otmDescent));
WriteLn(Stream, '/CapHeight ' + IntToStr(pm^.otmTextMetrics.tmHeight));
WriteLn(Stream, '/StemV ' + IntToStr(50 + Round(sqr(pm^.otmTextMetrics.tmWeight / 65))));
WriteLn(Stream, '/ItalicAngle ' + IntToStr(pm^.otmItalicAngle));
WriteLn(Stream, '>>');
WriteLn(Stream, '/CIDSystemInfo');
WriteLn(Stream, '<<');
WriteLn(Stream, '/Registry(Adobe)');
WriteLn(Stream, '/Ordering(GB1)');
WriteLn(Stream, '/Supplement 2');
WriteLn(Stream, '>>');
WriteLn(Stream, '/DW 1000');
WriteLn(Stream, '/W [ 1 95 500 814 939 500 7712 [ 500 ] 7716 [ 500 ] ]');
WriteLn(Stream, '>>');
WriteLn(Stream, 'endobj');
end;
// Add end
end;
// Add by ijia 2004.12.20
//if Charset <> CHINESEBIG5_CHARSET then
if not (Charset in [CHINESEBIG5_CHARSET, GB2312_CHARSET]) then
begin
WriteLn(Stream, '/FontDescriptor ' + IntToStr(Index + 1 + Parent.FStartFonts) + ' 0 R');
WriteLn(Stream, '/FirstChar ' + IntToStr(FirstChar));
WriteLn(Stream, '/LastChar ' + IntToStr(LastChar));
GetMem(pwidths, SizeOf(ABCArray));
Write(Stream, '/Widths [');
GetCharABCWidths(b.Canvas.Handle, FirstChar, LastChar, pwidths^);
for i := 0 to (LastChar - FirstChar) do
Write(Stream, IntToStr(pwidths^[i].abcA + Integer(pwidths^[i].abcB) + pwidths^[i].abcC) + ' ');
WriteLn(Stream, ']');
FreeMem(pwidths);
WriteLn(Stream, '>>');
WriteLn(Stream, 'endobj');
Parent.XRefAdd(Stream);
WriteLn(Stream, IntToStr(Index + 1 + Parent.FStartFonts) + ' 0 obj');
WriteLn(Stream, '<<');
WriteLn(Stream, '/Type /FontDescriptor');
WriteLn(Stream, '/FontName /' + FontName);
WriteLn(Stream, '/Flags 32');
WriteLn(Stream, '/FontBBox [' + IntToStr(pm^.otmrcFontBox.Left) + ' ' + IntToStr(pm^.otmrcFontBox.Bottom) + ' ' + IntToStr(pm^.otmrcFontBox.Right) + ' ' + IntToStr(pm^.otmrcFontBox.Top) + ' ]');
WriteLn(Stream, '/ItalicAngle ' + IntToStr(pm^.otmItalicAngle));
WriteLn(Stream, '/Ascent ' + IntToStr(pm^.otmAscent));
WriteLn(Stream, '/Descent ' + IntToStr(pm^.otmDescent));
WriteLn(Stream, '/Leading ' + IntToStr(pm^.otmTextMetrics.tmInternalLeading)); //NEW
WriteLn(Stream, '/CapHeight ' + IntToStr(pm^.otmTextMetrics.tmHeight));
WriteLn(Stream, '/XHeight ' + IntToStr(pm^.otmsXHeight)); //NEW
WriteLn(Stream, '/StemV ' + IntToStr(50 + Round(sqr(pm^.otmTextMetrics.tmWeight / 65))));
WriteLn(Stream, '/AvgWidth ' + IntToStr(pm^.otmTextMetrics.tmAveCharWidth)); //NEW
WriteLn(Stream, '/MaxWidth ' + IntToStr(pm^.otmTextMetrics.tmMaxCharWidth)); //NEW
WriteLn(Stream, '/MissingWidth ' + IntToStr(pm^.otmTextMetrics.tmAveCharWidth)); //NEW
if Parent.FEmbedded then
WriteLn(Stream, '/FontFile2 ' + IntToStr(Index + 2 + Parent.FStartFonts) + ' 0 R');
WriteLn(Stream, '>>');
WriteLn(Stream, 'endobj');
end;
if Parent.FEmbedded then
begin
Parent.XRefAdd(Stream);
WriteLn(Stream, IntToStr(Index + 2 + Parent.FStartFonts) + ' 0 obj');
i := GetFontData(b.Canvas.Handle, 0, 0, nil, 1);
GetMem(pfont, i);
i := GetFontData(b.Canvas.Handle, 0, 0, pfont, i);
MemStream := TMemoryStream.Create;
MemStream.Write(pfont^, i);
MemStream1 := TMemoryStream.Create;
frxDeflateStream(MemStream, MemStream1, gzMax);
WriteLn(Stream, '<< /Length ' + IntToStr(MemStream1.Size) + ' /Filter /FlateDecode /Length1 ' + IntToStr(MemStream.Size) + ' >>');
WriteLn(Stream, 'stream');
Stream.CopyFrom(MemStream1, 0);
MemStream1.Free;
MemStream.Free;
FreeMem(pfont);
WriteLn(Stream, '');
WriteLn(Stream, 'endstream');
WriteLn(Stream, 'endobj');
end;
except
end;
finally
FreeMem(pm);
b.Free;
end;
end;