Condividi tramite


Recupero dei criteri di misurazione dei caratteri

Nella classe FontFamily sono disponibili i metodi elencati di seguito per il recupero di vari criteri di misurazione per una particolare combinazione di gruppo e stile:

  • GetEmHeight(FontStyle)
  • GetCellAscent(FontStyle)
  • GetCellDescent(FontStyle)
  • GetLineSpacing(FontStyle)

I numeri restituiti da tali metodi sono espressi in unità di progettazione caratteri e perciò sono indipendenti dalla dimensione e dalle unità di un oggetto Font specifico.

Nell'illustrazione che segue sono mostrati i vari criteri di misurazione.

Nell'esempio che segue sono visualizzati i criteri di misurazione per lo stile normale del gruppo di caratteri Arial. Viene inoltre creato un oggetto Font basato sul gruppo Arial con dimensioni 16 pixel e vengono visualizzati i criteri di misurazione, espressi in pixel, per quello specificato oggetto Font.

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 + "."
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)
[C#]
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);

Nell'illustrazione che segue si mostra l'output del codice precedente.

Si notino le prime due linee dell'output nell'illustrazione precedente. L'oggetto Font restituisce una dimensione pari a 16, mentre l'oggetto FontFamily restituisce un'altezza em pari a 2,048. Questi due numeri (16 e 2,048) sono la chiave delle conversione tra le unità di progettazione caratteri e le unità (in questo caso pixel) dell'oggetto Font.

È possibile ad esempio convertire l'inclinazione da unità di progettazione in pixel nel modo che segue:

Con il codice riportato in precedenze il testo viene collocato in verticale tramite l'impostazione del membro di dati Y di un oggetto PointF. La coordinata y viene aumentata di font.Height per ogni nuova riga di testo. La proprietà Height di un oggetto Font restituisce l'interlinea, espressa in pixel, per quel particolare oggetto Font. Nell'esempio il numero restituito da Height è 19. Si tratta dello stesso numero, arrotondato a un intero, ottenuto tramite la conversione della misurazione dell'interlinea in pixel.

L'altezza em, detta anche dimensione o dimensione em, non corrisponde alla somma della parte ascendente e della parte discendente. La somma della parte ascendente e della parte discendente è invece uguale all'altezza della cella. La differenza tra l'altezza della cella e la spalla superiore è uguale all'altezza em. La somma dell'altezza della cella e della spalla inferiore è uguale all'interlinea.