以公制单位准确计算文本宽度

发布于 2025-01-06 17:20:08 字数 572 浏览 6 评论 0原文

我想计算给定字符串的精确文本宽度(以公制单位表示)。我的伪代码如下所示:

Bitmap.Canvas.Assign(Font);
PixelWidth := Bitmap.Canvas.TextWidth(Font)
MetricWidth := PtToMM * (PixelWidth * 72.0 / GetScreenDPI);

PtToMM 是一个定义为 0.352777778 的常量。对于某些字体和字体大小来说,这非常准确,但对于其他字体和字体大小来说,它要么太小,要么太大。我尝试了很多其他可能性,例如 GetCharWidth32GetCharABCWidths,也使用映射模式 MM_LOMETRIC 但我就是无法让它工作。这个问题一直困扰着我,所以请任何人帮忙并告诉我哪里错了。非常感谢!

编辑 我检查了一个字符串:公制宽度计算为 4.17 厘米,实际打印输出的宽度(在纸上测量)为 4.4 厘米(字体 Times New Roman,尺寸 12)。

I want to compute the exact text width in metric units of a given string. My pseudocode looks like this:

Bitmap.Canvas.Assign(Font);
PixelWidth := Bitmap.Canvas.TextWidth(Font)
MetricWidth := PtToMM * (PixelWidth * 72.0 / GetScreenDPI);

PtToMM is a constant that is defined as 0.352777778. This is pretty accurate for some fonts and font sizes but for others it is either too small or too large. I experimented a lot witht other possiblities such as GetCharWidth32 and GetCharABCWidths, also with mapping mode MM_LOMETRIC but I just can't get it to work. This problem is haunting me, so please can anyone help and show me where I'm wrong. Thank you very much!

EDIT I checked for one string: metric width is computed as 4.17 cm, the width on the actual printout (measured on paper) is 4.4 cm (font Times New Roman, size 12).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

献世佛 2025-01-13 17:20:08

我没有对其进行广泛的测试,但它似乎给出了正确的结果。结果是文本的宽度(以千分之一毫米为单位)。此函数不支持自动换行和其他特殊注意事项。

另请注意,使用打印机时,不正确的打印套准可能会“拉伸”文本。

另请注意,对于打印机,将打印分辨率从 300 ppp 更改为 1200 ppp 也会改变结果。

uses
  ConvUtils, stdConvs ;

function CalcRequiredTextWidth(aDC : HDC; aFont : TFont; const asText: string): Double;
var vCanvas : TCanvas;
    iPixelsWidth : Integer;
    dInchWidth : Double;
    iFontSize : Integer;
begin
  vCanvas := TCanvas.Create;
  try
    vCanvas.Handle := aDC;

    vCanvas.Font.Assign(aFont);
    iFontSize := vCanvas.Font.Size;
    vCanvas.Font.PixelsPerInch := GetDeviceCaps(aDC, LOGPIXELSY);
    vCanvas.Font.Size := iFontSize;

    iPixelsWidth := vCanvas.TextExtent(asText).cx;

    dInchWidth := iPixelsWidth / GetDeviceCaps(vCanvas.Handle, LOGPIXELSX);

    Result := Convert(dInchWidth, duInches, duMicrons);

  finally
    vCanvas.Free;
  end;
end;

I didn't test it extensively, but it seems to be giving the proper results. The result is the width of the text in thousandth of milimeter. This function doesn't support wordwrapping and other special considerations.

Also, note that when working with printer, improper Print Registration can "stretch" the text.

Also, note that for printer, changing, for example, the print resolution from 300 ppp to 1200 ppp will also change the result.

uses
  ConvUtils, stdConvs ;

function CalcRequiredTextWidth(aDC : HDC; aFont : TFont; const asText: string): Double;
var vCanvas : TCanvas;
    iPixelsWidth : Integer;
    dInchWidth : Double;
    iFontSize : Integer;
begin
  vCanvas := TCanvas.Create;
  try
    vCanvas.Handle := aDC;

    vCanvas.Font.Assign(aFont);
    iFontSize := vCanvas.Font.Size;
    vCanvas.Font.PixelsPerInch := GetDeviceCaps(aDC, LOGPIXELSY);
    vCanvas.Font.Size := iFontSize;

    iPixelsWidth := vCanvas.TextExtent(asText).cx;

    dInchWidth := iPixelsWidth / GetDeviceCaps(vCanvas.Handle, LOGPIXELSX);

    Result := Convert(dInchWidth, duInches, duMicrons);

  finally
    vCanvas.Free;
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文