Så här hämtar du teckensnittsmått
Klassen FontFamily innehåller följande metoder som hämtar olika mått för en viss kombination av familj/stil:
GetEmHeight(Typsnittsstil)
GetCellAscent(Teckensnittsstil)
GetCellDescent(FontStyle)
GetLineSpacing(FontStyle)
De värden som returneras av dessa metoder finns i teckensnittsdesignenheter, så de är oberoende av storleken och enheterna för ett visst Font objekt.
Följande bild visar de olika måtten:
Exempel
I följande exempel visas måtten för den vanliga stilen i teckensnittsfamiljen Arial. Koden skapar också ett Font -objekt (baserat på Arial-familjen) med storlek 16 bildpunkter och visar måtten (i bildpunkter) för just det Font objektet.
Följande bild visar utdata från exempelkoden:
Observera de två första raderna med utdata i föregående bild. Objektet Font returnerar en storlek på 16 och FontFamily-objektet returnerar en em-höjd på 2 048. Dessa två tal (16 och 2 048) är nyckeln till att konvertera mellan teckensnittsdesignenheter och enheterna (i det här fallet bildpunkter) för det Font objektet.
Du kan till exempel konvertera övergången från designenheterna till bildpunkter på följande sätt:
Följande kod placerar texten lodrätt genom att ange Y datamedlem i ett PointF objekt. Y-koordinaten ökas med font.Height
för varje ny textrad. Egenskapen Height för ett Font-objekt returnerar radavståndet (i bildpunkter) för just det Font objektet. I det här exemplet är talet som returneras av Height 19. Observera att detta är samma som talet (avrundat upp till ett heltal) som erhålls genom att konvertera måttet för radavstånd till bildpunkter.
Observera att em-höjden (även kallad storlek eller em-storlek) inte är summan av uppstigningen och nedstigningen. Summan av uppstigningen och nedstigningen kallas cellhöjden. Cellhöjden minus den interna ledningen är lika med em-höjden. Cellhöjden plus den externa ledningen är lika med radavståndet.
string infoString = ""; // enough space for one line of output
int ascent; // font family ascent in design units
float ascentPixel; // ascent converted to pixels
int descent; // font family descent in design units
float descentPixel; // descent converted to pixels
int lineSpacing; // font family line spacing in design units
float lineSpacingPixel; // line spacing converted to pixels
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
fontFamily,
16, FontStyle.Regular,
GraphicsUnit.Pixel);
PointF pointF = new PointF(10, 10);
SolidBrush solidBrush = new SolidBrush(Color.Black);
// Display the font size in pixels.
infoString = "font.Size returns " + font.Size + ".";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
// Move down one line.
pointF.Y += font.Height;
// Display the font family em height in design units.
infoString = "fontFamily.GetEmHeight() returns " +
fontFamily.GetEmHeight(FontStyle.Regular) + ".";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
// Move down two lines.
pointF.Y += 2 * font.Height;
// Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(FontStyle.Regular);
// 14.484375 = 16.0 * 1854 / 2048
ascentPixel =
font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The ascent is " + ascent + " design units, " + ascentPixel +
" pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
// Move down one line.
pointF.Y += font.Height;
// Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(FontStyle.Regular);
// 3.390625 = 16.0 * 434 / 2048
descentPixel =
font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The descent is " + descent + " design units, " +
descentPixel + " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
// Move down one line.
pointF.Y += font.Height;
// Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);
// 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel =
font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The line spacing is " + lineSpacing + " design units, " +
lineSpacingPixel + " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);
Dim infoString As String = "" ' enough space for one line of output
Dim ascent As Integer ' font family ascent in design units
Dim ascentPixel As Single ' ascent converted to pixels
Dim descent As Integer ' font family descent in design units
Dim descentPixel As Single ' descent converted to pixels
Dim lineSpacing As Integer ' font family line spacing in design units
Dim lineSpacingPixel As Single ' line spacing converted to pixels
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
fontFamily, _
16, _
FontStyle.Regular, _
GraphicsUnit.Pixel)
Dim pointF As New PointF(10, 10)
Dim solidBrush As New SolidBrush(Color.Black)
' Display the font size in pixels.
infoString = "font.Size returns " & font.Size.ToString() & "."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
' Move down one line.
pointF.Y += font.Height
' Display the font family em height in design units.
infoString = "fontFamily.GetEmHeight() returns " & _
fontFamily.GetEmHeight(FontStyle.Regular) & "."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
' Move down two lines.
pointF.Y += 2 * font.Height
' Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(FontStyle.Regular)
' 14.484375 = 16.0 * 1854 / 2048
ascentPixel = _
font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The ascent is " & ascent & " design units, " & ascentPixel _
& " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
' Move down one line.
pointF.Y += font.Height
' Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(FontStyle.Regular)
' 3.390625 = 16.0 * 434 / 2048
descentPixel = _
font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The descent is " & descent & " design units, " & _
descentPixel & " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
' Move down one line.
pointF.Y += font.Height
' Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular)
' 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel = _
font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular)
infoString = "The line spacing is " & lineSpacing & " design units, " & _
lineSpacingPixel & " pixels."
e.Graphics.DrawString(infoString, font, solidBrush, pointF)
Kompilera koden
Föregående exempel är utformat för användning med Windows Forms och kräver PaintEventArgse
, som är en parameter för PaintEventHandler.
Se även
.NET Desktop feedback