Table of Contents

25.1.8 CairoFontExtents

The Font property in the Cairo class sets the font or reads the font with which text is drawn - with the properties Bold, Extents, Italic, Matrix, Name, Size, Slant and Weight of the virtual class .Cairo.Font.

B0

Figure 25.1.8.1: Properties of the virtual class .Cairo.Font

The values 'Internal Leading' and 'External Leading' are font dimensions of the typographic metric - but not properties of the virtual class .Cairo.Font.

25.1.8.1 Properties

The CairoFontExtents class stores metric information for a font with a specified font in the Extents property of the .Cairo.Font virtual class and has these properties of the Float data type:

PropertyDescription
AscentSpecifies the distance at which the font extends above the base line. Note that this is not always exactly the same as the maximum extension of all font characters.
DescentSpecifies the distance at which the font lies below the base line. This value is positive for typical fonts that occupy sections below the baseline. Note that this is not always exactly the same as the maximum extension of all glyphs in the font.
HeightSpecifies the vertical distance between the baselines of successive lines of text with the given font. The distance is greater than the sum of Ascent and Descent.
MaxXAdvanceReturns the maximum distance in the X direction by which the origin is advanced for each glyph in the font.
MaxYAdvanceReturns the maximum distance in the y-direction by which the origin is shifted for each glyph in the font. This value is zero for fonts with a horizontal layout.

Table 25.1.8.1.1 : Properties of the virtual class .Cairo.Font

25.1.8.2 Example

The example demonstrates the use of all properties of the CairoFontExtents class and the reading of the values for these properties:

Private Sub GeneratePDF()
 
  Dim PDFSurface As CairoPdfSurface
  Dim fXOffset As Float = 20
  Dim fYOffset As Float = 20
 
  PDFSurface = New CairoPdfSurface(sPfadPDFDatei, 210, 297) ' → A4-Format
 
  Cairo.Begin(PDFSurface)    
    Cairo.Matrix = Cairo.Matrix.Translate(MMToPoints(fXOffset), MMToPoints(fYOffset))
    Cairo.Matrix = Cairo.Matrix.Scale(1, 1) ' Zoom-Faktor = 1    
 
    Cairo.AntiAlias = False
    Line(MMToPoints(0), MMToPoints(0), MMToPoints(0), MMToPoints(297 - fYOffset), 0.1, [1, 1], Color.Red)
    Line(MMToPoints(0), MMToPoints(0), MMToPoints(210 - fXOffset), MMToPoints(0), 0.1, [1, 1], Color.Red)
    Cairo.Stroke
 
'-- TEXT
    Cairo.Source = Cairo.ColorPattern(Color.DarkBlue) 
    Cairo.Font.Name = "Arial"
    Cairo.Font.Size = 100
    Cairo.Font.Bold = True
 
    Print "FONT"
    Print "--------------------------------"
    Print "Ascent = ", PointsToMM(Cairo.Font.Extents.Ascent)
    Print "Descent = ", PointsToMM(Cairo.Font.Extents.Descent)
    Print "Height = ", PointsToMM(Cairo.Font.Extents.Height)
    Print "H = ", PointsToMM(Cairo.Font.Extents.Ascent) + PointsToMM(Cairo.Font.Extents.Descent)
    Print "MaxXAdvance = ", PointsToMM(Cairo.Font.Extents.MaxXAdvance) 
    Print "MaxYAdvance = ", PointsToMM(Cairo.Font.Extents.MaxYAdvance)
 
    Cairo.MoveTo(MMToPoints(0), MMToPoints(50))
    Cairo.DrawText("Ärger")
  Cairo.End()
 
  PDFSurface.Finish()
  Desktop.Open(sPfadPDFDatei)
 
End
 
Private Function MMToPoints(Value As Float) As Float
  Return Value * 2.83527
End
 
Private Function PointsToMM(Value As Float) As Float
  Return Value * 0.3527
End
 
Private Sub Line(Xa As Float, Ya As Float, Xe As Float, Ye As Float, fWidth As Float, Optional fDash As Float[], Optional cColor As Integer)
 
'-- Coloured line from point A to point E - A(xa|ya), E(xe|ye) - in millimetres!
    Cairo.MoveTo(MMToPoints(Xa), MMToPoints(Ya))
    Cairo.LineTo(MMToPoints(Xe), MMToPoints(Ye))
    Cairo.Source = Cairo.ColorPattern(cColor)
    Cairo.LineWidth = fWidth
    Cairo.Dash = fDash
    Cairo.Stroke()
    Cairo.Dash = Null
 
End

The above source code snippet produces the following output (rounded and with the unit millimetre) in the console of the IDE

FONT
--------------------------------
Ascent      =  31,93 mm
Descent     =  7,48 mm
Height      =  40,56 mm
H           =  39,40 mm
Height - H  =  1,16 mm
MaxXAdvance =  70,54 mm
MaxYAdvance =  0 mm

Comment: