我目前正在使用libharu库,以便在C ++程序中呈现一些pdf。
我不知道是否可能知道用特定字体绘制特定文本所需的大小。
该HPDF_Page_TextRect绘制方法会返回一个HPDF_PAGE_INSUFFICIENT_SPACE
消息,如果文本不能在所提供的矩形放进去,但我想知道是否有计算,其中文本将适合一个特定的字体最小尺寸的一种方式。
在临时,谢谢。
我发现了一种解决方法,其中包括在缩小页面宽度的同时将页面外部绘制文本,直到绘制失败。它一点也不枯燥,但是可以。
HPDF_STATUS drawString_2(HPDF_Page pdfPage, CString texte, COLORREF textColor, HPDF_Font font, HPDF_REAL fontSize, HPDF_REAL left, HPDF_REAL right, HPDF_REAL top,
HPDF_REAL bottom, HPDF_TextAlignment alignment, HPDF_REAL textRise = 0, HPDF_TextRenderingMode renderingMode = HPDF_FILL);
HPDF_REAL calculateMaxWidthForTextInRect2(HPDF_Page pdfPage, CString texte, HPDF_REAL left, HPDF_REAL right, HPDF_REAL top, HPDF_REAL bottom, HPDF_Font font, HPDF_REAL fontSize);
HPDF_STATUS drawString_2(HPDF_Page pdfPage, CString texte, COLORREF textColor, HPDF_Font font, HPDF_REAL fontSize, HPDF_REAL left, HPDF_REAL right, HPDF_REAL top,
HPDF_REAL bottom, HPDF_TextAlignment alignment, HPDF_REAL textRise, HPDF_TextRenderingMode renderingMode)
{
#ifdef DEBUG
assert(font != NULL);
#else
if (font == NULL)
return 1;
#endif
HPDF_Page_SetFontAndSize(pdfPage, font, fontSize);
HPDF_Page_SetTextRise(pdfPage, textRise);
HPDF_Page_SetTextRenderingMode(pdfPage, renderingMode);
HPDF_Page_SetRGBFill(pdfPage, GetRValue(textColor) / 255.f, GetGValue(textColor) / 255.f, GetBValue(textColor) / 255.f);
HPDF_Page_BeginText(pdfPage);
HPDF_STATUS returnValue = HPDF_Page_TextRect(pdfPage, left, top, right, bottom, (LPCSTR)texte, alignment, NULL);
HPDF_Page_EndText(pdfPage);
return returnValue;
}
HPDF_REAL calculateMaxWidthForTextInRect2(HPDF_Page pdfPage, CString texte, HPDF_REAL left, HPDF_REAL right, HPDF_REAL top, HPDF_REAL bottom, HPDF_Font font, HPDF_REAL fontSize)
{
// Let's draw outside the page size, so the text, even invisible, will not be selectionnable and copyable
HPDF_REAL leftTmp = -(right - left);
HPDF_REAL rightTmp = 0;
HPDF_REAL topTmp = -(bottom - top);
HPDF_REAL bottomTmp = 0;
HPDF_Page_SetTextRenderingMode(pdfPage, HPDF_INVISIBLE);
// Let's reduce horizontally the rect until the drawing fails
while (drawString_2(pdfPage, texte, RGB(0, 0, 0), font, fontSize, leftTmp, rightTmp, topTmp, bottomTmp, HPDF_TALIGN_CENTER, 0, HPDF_INVISIBLE) == HPDF_OK)
{
leftTmp ++;
rightTmp --;
}
// If no drawing has be performed, the text cannot be rendered in the provided rect
if (rightTmp == 0)
return -1;
// Let's decrement the shifting in order to get the latest correct value
leftTmp --;
rightTmp ++;
return (rightTmp - leftTmp);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句