简体   繁体   English

GetTextExtentPoint32() 中的设备句柄

[英]device handle in GetTextExtentPoint32()

I want to use GetTextExtentPoint32 to compute the size of the string in pixels.我想使用GetTextExtentPoint32来计算字符串的像素大小。 GetTextExtentPoint32 has the follwoing syntax: GetTextExtentPoint32具有以下语法:

BOOL GetTextExtentPoint32(
  _In_  HDC     hdc,
  _In_  LPCTSTR lpString,
  _In_  int     c,
  _Out_ LPSIZE  lpSize
);

where hdc [in] is the handle to the device context.其中hdc [in]是设备上下文的句柄。

QUESTIONS:问题:

I am using the following code to load fonts in libharu我正在使用以下代码在libharu 中加载字体

    //-----UTF8 Encoding
    HPDF_UseUTFEncodings(pdf);
    const char *fontname = HPDF_LoadTTFontFromFile(pdf, "FreeSans.ttf", HPDF_TRUE);
    HPDF_Font font = HPDF_GetFont(pdf, fontname, "UTF-8");

How to I pass/compute the device handle to GetTextExtentPoint32() ?如何将设备句柄传递/计算到GetTextExtentPoint32()

UPDATE: I am using Win-7 on a monitor of resolution (1920x1080).更新:我在分辨率 (1920x1080) 的显示器上使用 Win-7。 My aim is to generate a PDF report using Libharu libraries.我的目标是使用Libharu库生成 PDF 报告。 I want to compute the length of a string in pixels and for that purpose, I want to use GetTextExtentPoint32()我想以像素为单位计算字符串的长度,为此,我想使用GetTextExtentPoint32()

So to get the width of a string of text in your PDF with libharu, use HPDF_Font_TextWidth() .因此,要使用HPDF_Font_TextWidth() PDF 中文本字符串的宽度,请使用HPDF_Font_TextWidth() This returns a HPDF_TextWidth structure;这将返回一个HPDF_TextWidth结构; use its width field.使用它的width字段。 The width will be measured in either 1/1000th of an em or 1000 ems , I'm not sure which (using whatever sense of "em" TrueType uses for the em square), which I'm assuming PDF uses natively for measurements.宽度将以em 的 1/1000th1000 ems进行测量,我不确定哪个(使用 TrueType 用于 em 方块的“em”的任何意义),我假设 PDF 本身用于测量。 You'd have to check the rest of HPDF's documentation.您必须检查其余的 HPDF 文档。

I'm not sure if this takes kerning into consideration or not.我不确定这是否考虑了字距调整。

Confirmation: the source of HPDF's general font handling, the source of HDPF's TrueType font handling, Apple's TrueType font reference.确认:HPDF的通用字体处理的源码,HDPF的TrueType字体处理的源码,苹果的TrueType字体参考。 The exact calculation is准确的计算是

width of one glyph = advance_width(units) * 1000 / units_per_em一个字形的宽度 = Advance_width(units) * 1000 / units_per_em

Windows GDI works with pixels. Windows GDI 使用像素。 Haru PDF works with points (72 points/inch). Haru PDF 使用点(72 点/英寸)。 There is no direct equivalent to GetTextExtentPoint32() in Haru PDF. Haru PDF 中没有直接等效于GetTextExtentPoint32()

If you want to duplicate your GDI results in PDF, you will need to:如果您想以 PDF 格式复制 GDI 结果,您需要:

[1] Call GetTextExtentPoint32() to get the text size in GDI pixels. [1] 调用GetTextExtentPoint32()获取以 GDI 像素为单位的文本大小。

[2] Divide the width in pixels by the Device Context resolution (pixels/inch), then multiply by 72 to get points. [2] 以像素为单位的宽度除以设备上下文分辨率(像素/英寸),然后乘以 72 得到点数。 Call GetDeviceCaps(hdc, LOGPIXELSX) to get the pixels/inch.调用GetDeviceCaps(hdc, LOGPIXELSX)以获取像素/英寸。 Call this result fTargetWidth .将此结果fTargetWidth This is how wide you expect your string to be.这是您期望字符串的宽度。

[3] When writing the PDF, call HPDF_Font_TextWidth() to get the string's width as seen from Haru. [3] 在编写 PDF 时,调用HPDF_Font_TextWidth()以获取从 Haru 看到的字符串的宽度。 If the PDF width is greater than GDI's "points" width, tighten the spacing a tad, subtracting the excess over the range of characters:如果 PDF 宽度大于 GDI 的“点”宽度,请稍微收紧间距,减去超出字符范围的部分:

// how long is this string?
HPDF_TextWidth htw = HPDF_Font_TextWidth(pdfFont, (HPDF_BYTE *)string, len);
// convert to actual points
fWidth = (htw.width * fontPoints) / 1000.f;
// does it overflow?
if(fWidth > fTargetWidth && htw.numchars > 0)
{   excess = fWidth - fTargetWidth;    // by how much
    charSpace = -excess / htw.numchars;  // note negative
    // tighten up the spacing
    HPDF_Page_SetCharSpace(page, charSpace);
}

HPDF_Page_TextOut (page, x, y, string);
HPDF_Page_SetCharSpace(page, 0.f);      // reset

For some reason, HPDF_Page_SetWordSpace() doesn't seem to have any effect.出于某种原因, HPDF_Page_SetWordSpace()似乎没有任何效果。

How come the same font, at the same size, has such a difference in width?同样的字体,同样的大小,宽度怎么会有这么大的差别呢? So much for WYSIWYG :o)所见即所得 :o)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM