我的应用程序允许用户在画布上创建文本对象。该对象可以保存到项目文件中以便以后加载。
为了在各种平台上加载后对象看起来相同,我将文本对象实现为路径。这也允许用户使用下载的字体,然后在没有该字体的不同设备上打开项目文件 - 而不会改变文本的外观。
路径是使用创建TTextLayout.ConvertToPath
和绘制的TCanvas.FillPath
。这适用于大多数字体,但对其他字体有问题。
下图显示了使用 Bahnschrift 字体的结果(顶部)。底部显示了使用 MS Paint 的外观。这种字体似乎有相交的路径,我认为问题在于 FillPath 正在使用另一种填充模式,这似乎不是一个可以改变的选项。
我还通过创建文本并将其转换为路径,在 Inkscape 中测试了与 SVG 相同的字体,但它绘制正确。Delphi 和 Inkscape 创建的路径数据基本相同(t 由 2 个相互交叉的封闭区域组成),因此它们的绘制方式必须不同。
任何人都可以建议解决这个问题吗?
这是代码
procedure TMainForm.Button1Click(Sender: TObject);
Var
LPath : TPathData;
LLayout : TTextLayout;
begin
LLayout := TTextLayoutManager.DefaultTextLayout.Create;
LLayout.Text := 'test';
LLayout.Font.Family := 'Bahnschrift';
LPath := TPathData.Create;
LLayout.ConvertToPath(LPath);
// Draw the path to a bitmap
SavePathBitmap(LPath, 'Path_test');
LLayout.Free;
LPath.Free;
end;
procedure TMainForm.SavePathBitmap(APath : TPathData ; AFileName : String);
var
bmp : TBitmap;
rect : TRectF;
begin
APath.Scale(10, 10); // Enlarge
rect := APath.GetBounds;
APath.Translate(-rect.Left + 5, -rect.Top + 5); // offset onto bitmap
bmp := TBitmap.Create(Trunc(rect.Width)+10, Trunc(rect.Height)+10);
with bmp.Canvas do if BeginScene then begin
Clear(TAlphaColorRec.White);
Fill.Color := TAlphaColorRec.Black;
FillPath(APath, 1);
EndScene;
bmp.SaveToFile(AFileName + '.png');
end;
bmp.Free;
end;