gpt4 book ai didi

delphi - 使用Canvas.TextOut有什么含义?

转载 作者:行者123 更新时间:2023-12-03 14:34:54 30 4
gpt4 key购买 nike

介绍

我的问题来自过去几天来我一直在处理的一个相当有趣的问题。我最近问了一个有关Writing a custom property inspector - How to handle inplace editor focus when validating values?的问题

自那以来,我的控件取得了一些不错的进步,例如在中间添加了一个分隔符以分隔Name和Value行,并且重要的是,该分隔符可用于调整两列的大小。

这是我的问题开始的地方,在调整分隔符大小的同时可以看到就地编辑器,这会导致我的控件稍微变慢。因此,我进一步更改了代码,以仅在未调整分隔符大小的情况下显示就地编辑器。因此,基本上,我使用Canvas.TextOut将我的值绘制为字符串,如果选择了一行,则上面显示了Inplace编辑器。如果调整分隔符的大小,则内部编辑器将隐藏,一旦调整大小操作完成,内部编辑器将再次变为可见。

虽然这解决了我提到的缓慢问题,但我遇到了一个新问题,即就地编辑器(基本上是TEdit)中的文本与我使用Canvas.TextOut绘制的文本略有不同

例子1

区别是非常细微的,但是如果您看起来足够近,则可以看到它:



图1 Canvas.TextOut

enter image description here

图2 DrawText

您可能需要使用屏幕放大镜看得更近一些,但是使用SomeText行时,它更加引人注目,因为SomeText之间的间距以及TeText之间的间距略有不同。

例子2

一个更好的示例也许是将Canvas.TextOutDrawText与就地编辑器(TEdit)文本进行比较:

enter image description here

图3比较

如您所见,这里的区别更加明显。使用True时,字符串Canvas.TextOut显然显示出文本字符之间的间距要大得多,因为DrawTextinplace editor完全相同地呈现文本。

当我使用Canvas.TextOut时,在调整检查器分隔符的大小以及显示和隐藏就地编辑器之间,我遇到各种可怕的文本不匹配的情况。如果我没有尝试过并尝试过其他的文本绘制方法,我想我不会意识到其中的区别并找到解决方案。重要的是要知道在将文本绘制到 Canvas 上时,我使用的字体设置与为原位编辑器定义的字体完全相同。

现在,我使用的是DrawText而不是Canvas.TextOut,一切都可以与就地编辑器以及我希望的方式一致地工作。



我的问题是什么使Canvas.TextOut呈现文本与DrawText如此不同?从我的示例并处理我当前的问题,很明显Canvas.TextOut不会以与具有相同Font设置的TEdit相同的方式呈现文本,但是DrawText似乎以正确的方式呈现文本。

这使我对Canvas.TextOut的使用提出疑问,如果它不能正确呈现文本,我是否应该始终使用DrawText代替?

测试演示

您可以使用以下代码自行测试:

type
TForm1 = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
FFont: TFont;
FRect: TRect;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
FFont := TFont.Create;
FFont.Color := clNavy;
FFont.Name := 'Segoe UI';
FFont.Size := 9;
FFont.Style := [];
FRect := Rect(10, 30, 100, 100);

Canvas.Font.Assign(FFont);
Edit1.Font.Assign(FFont);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FFont.Free;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
Canvas.TextOut(10, 10, 'Canvas.TextOut: [True]');
DrawText(Canvas.Handle, PChar('DrawText: [True]'), Length('DrawText: [True]'), FRect, DT_LEFT);
end;

通过在全新的VCL项目上运行以上内容,我得到的结果如下:

enter image description here

图4测试演示

再次注意使用 True时字符串 Canvas.TextOut中的间距,从我的 Angular 来看,它与 DrawTextTEdit绘制文本的方式明显不同。

下图与图4相同,但放大了400%

enter image description here

图5测试演示放大了400%

T中的 eText之间以及在 T中的 rTrue之间可以看到明显的差异。

enter image description here

图6“文字”一词​​在指导原则下放大了400%

您可以看到 Te之间的字距紧调比 DrawText(使用 Canvas.TextOut)的 ExtTextOut更近一个像素。

enter image description here

图7单词 True在指导下放大了700%

您会看到 Tr之间的字距调整比 DrawText(使用 Canvas.TextOut)和 ExtTextOut和Inplace编辑器(TEdit)距更近一个像素。

我测试了几种不同的字体,这是我的发现:

好:

Arial, Cambria, Candara, Comic Sans MS, Consolas, Courier, Courier New, Fixedsys, Georgia, Lucida Console, Lucida Sans Unicode, Microsoft Sans Serif, Tahoma, Terminal and Times New Roman.



坏:

Calibri, Corbel, Myriad Pro, Segoe UI, Trebuchet MS and Verdana.



好的字体是那些看起来像 DrawText一样呈现文本的字体,Inpace编辑器(TEdit)控件也使用 Canvas.TextOut进行渲染。不好的结果表明 Canvas.TextOut呈现的文本与其他方法略有不同。

尽管我不太确定,但这里可能有一些线索,但是我还是添加了它,以防万一。

最佳答案

观察到的差异是由于使用了不同的WinAPI文本呈现功能及其行为。具体字符kerning

In typography, kerning (less commonly mortising) is the process of adjusting the spacing between characters in a proportional font, usually to achieve a visually pleasing result. Kerning adjusts the space between individual letter forms, while tracking (letter-spacing) adjusts spacing uniformly over a range of characters.


  • DrawText

  • The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth).


  • ExtTextOut(由Canvas.TextOut使用)
  • ExtTextOut声明:
    BOOL ExtTextOut(
    _In_ HDC hdc,
    _In_ int X,
    _In_ int Y,
    _In_ UINT fuOptions,
    _In_ const RECT *lprc,
    _In_ LPCTSTR lpString,
    _In_ UINT cbCount,
    _In_ const INT *lpDx
    );

    If the lpDx parameter is NULL, the ExtTextOut function uses the default spacing between characters. The character-cell origins and the contents of the array pointed to by the lpDx parameter are specified in logical units. A character-cell origin is defined as the upper-left corner of the character cell.



    基本上, DrawText将自动绘制格式化的文本,包括调整字符之间的间距(字距调整),而 ExtTextOut默认情况下将使用字符之间的默认间距(无字距调整)。如果要调整字符之间的间距,则必须计算并提供字距调整数组( lpDx)参数。

    这些差异在某些字符组合中尤其明显,例如 T和视觉上适合 T的小写字母,或 AV适合 VA。不同的字体也具有不同的默认字距,这就是为什么某些字体使用这两种功能在外观上呈现相同效果的原因,而有些字体则没有。字距调整还取决于字体大小。例如,在 AV处用 Arial呈现的字符 9 pt将对两个函数具有相同的输出,而在 Arial处的 12 pt将导致不同的输出。

    下图中的第一行使用 ExtTextOut进行无字距调整,第二行使用 DrawText进行自动字距调整。

    enter image description here

    关于delphi - 使用Canvas.TextOut有什么含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31968771/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com