取得字型計量
FontFamily類別提供下列方法來擷取特定系列/樣式組合的各種計量:
- FontFamily::GetEmHeight (FontStyle)
- FontFamily::GetCellAscent (FontStyle)
- FontFamily::GetCellDescent (FontStyle)
- FontFamily::GetLineSpacing (FontStyle)
這些方法傳回的數位是以字型設計單位表示,因此它們與特定 Font 物件的大小和單位無關。
下圖顯示上升、下降和行距。
下列範例會顯示 Arial 字型系列一般樣式的計量。 此程式碼也會根據大小為 16 圖元的 Arial 系列) 建立 Font 物件 (,並顯示該特定 Font 物件的圖元) (計量。
#define INFO_STRING_SIZE 100 // one line of output including null terminator
WCHAR infoString[INFO_STRING_SIZE] = L"";
UINT ascent; // font family ascent in design units
REAL ascentPixel; // ascent converted to pixels
UINT descent; // font family descent in design units
REAL descentPixel; // descent converted to pixels
UINT lineSpacing; // font family line spacing in design units
REAL lineSpacingPixel; // line spacing converted to pixels
FontFamily fontFamily(L"Arial");
Font font(&fontFamily, 16, FontStyleRegular, UnitPixel);
PointF pointF(10.0f, 10.0f);
SolidBrush solidBrush(Color(255, 0, 0, 0));
// Display the font size in pixels.
StringCchPrintf(
infoString,
INFO_STRING_SIZE,
L"font.GetSize() returns %f.", font.GetSize());
graphics.DrawString(
infoString, -1, &font, pointF, &solidBrush);
// Move down one line.
pointF.Y += font.GetHeight(0.0);
// Display the font family em height in design units.
StringCchPrintf(
infoString,
INFO_STRING_SIZE,
L"fontFamily.GetEmHeight() returns %d.",
fontFamily.GetEmHeight(FontStyleRegular));
graphics.DrawString(infoString, -1, &font, pointF, &solidBrush);
// Move down two lines.
pointF.Y += 2.0f * font.GetHeight(0.0f);
// Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(FontStyleRegular);
// 14.484375 = 16.0 * 1854 / 2048
ascentPixel =
font.GetSize() * ascent / fontFamily.GetEmHeight(FontStyleRegular);
StringCchPrintf(
infoString,
INFO_STRING_SIZE,
L"The ascent is %d design units, %f pixels.",
ascent,
ascentPixel);
graphics.DrawString(infoString, -1, &font, pointF, &solidBrush);
// Move down one line.
pointF.Y += font.GetHeight(0.0f);
// Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(FontStyleRegular);
// 3.390625 = 16.0 * 434 / 2048
descentPixel =
font.GetSize() * descent / fontFamily.GetEmHeight(FontStyleRegular);
StringCchPrintf(
infoString,
INFO_STRING_SIZE,
L"The descent is %d design units, %f pixels.",
descent,
descentPixel);
graphics.DrawString(infoString, -1, &font, pointF, &solidBrush);
// Move down one line.
pointF.Y += font.GetHeight(0.0f);
// Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(FontStyleRegular);
// 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel =
font.GetSize() * lineSpacing / fontFamily.GetEmHeight(FontStyleRegular);
StringCchPrintf(
infoString,
INFO_STRING_SIZE,
L"The line spacing is %d design units, %f pixels.",
lineSpacing,
lineSpacingPixel);
graphics.DrawString(infoString, -1, &font, pointF, &solidBrush);
下圖顯示上述程式碼的輸出。
請注意上圖中的前兩行輸出。 Font物件會傳回 16 的大小,而 FontFamily物件會傳回 2,048 的 em 高度。 這兩個數字 (16 和 2,048) 是轉換字型設計單位與字型 (單位之間的索引鍵,在此案例中為 Font 物件的圖元) 。
例如,您可以將象心從設計單位轉換為圖元,如下所示:
上述程式碼會藉由設定PointF物件的Y資料成員,垂直定位文字。 每個新文字行都會增加 font.GetHeight(0.0f)
Y 座標。
Font物件的Font::GetHeight方法會傳回該特定Font物件的行距) 圖元 (。 在此範例中, Font::GetHeight 傳回的數位是 18.398438。 請注意,這與將行距度量轉換成圖元所取得的數位相同。