User Tools

Site Tools


k25:k25.1.8:start

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:

  • The two functions MMToPoints(Value As Float) and PointsToMM(Value As Float) are also used in this example.
  • The difference of around 1.2 millimetres between the sum of Ascent and Descent and the value for the height is interesting. This is because the height is calculated from the sum of Ascent, Descent and 'External Leading'.
  • 'External Leading' stands for the (outdated) term 'shoot-through' and determines the minimum distance between two print lines.
  • If you print out the generated PDF file and draw the horizontal, parallel lines shown in → Figure 25.1.8.1 for the word 'anger' and measure the differences between them, you will obtain the values in the above overview with sufficient accuracy.
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k25/k25.1.8/start.txt · Last modified: 20.02.2024 by emma

Page Tools