How to get depth and other lengths of a font?

There is no way for TeX to know the exact size of the glyph, TeX only knows the box around them, so the small blue line at the f in the second row can't be determined by TeX alone. The other lengths are determinable, you can measure the size of any box you can typeset (the following is in plain TeX syntax):

\setbox0\hbox{x}
height: \the\ht0\par
depth: \the\dp0\par
width: \the\wd0

With this you can determine the first blue line as:

\setbox0\hbox{yf}
distance between boxes: \the\dimexpr\baselineskip-\ht0-\dp0\relax

The blue lines above and below the x in the second row:

\setbox0\hbox{x}
\setbox1\hbox{yf}
distance above the x: \the\dimexpr\ht1-\ht0\relax\par
distance below the x: \the\dimexpr\dp1-\dp0\relax
\bye

enter image description here


e-tex allows you to get the font dimens of each charcter without needing to box it, this shows x and y of the current font

\documentclass{article}

\begin{document}

\typeout{x}
\showthe\fontcharht\font`x
\showthe\fontchardp\font`x
\showthe\fontcharwd\font`x


\typeout{y}
\showthe\fontcharht\font`y
\showthe\fontchardp\font`y
\showthe\fontcharwd\font`y
\end{document}

making a log:

x
> 4.30554pt.
l.8 \showthe\fontcharht\font`x

? 
> 0.0pt.
l.9 \showthe\fontchardp\font`x

? 
> 5.2778pt.
l.10 \showthe\fontcharwd\font`x

? 
y
> 4.30554pt.
l.14 \showthe\fontcharht\font`y

? 
> 1.94444pt.
l.15 \showthe\fontchardp\font`y

? 
> 5.2778pt.
l.16 \showthe\fontcharwd\font`y

? 

The two distances marked with blue lines depend on the items in both lines, except the blue line next to “f”, which obviously only depends on the letter.

So you can't “know” them in general.

TeX tries to achieve constant distance b between consecutive lines (the value is stored in the parameter \baselineskip). In order to achieve this goal, TeX measures a line (let's call it “line T”) and the next (call it “line B”, by determining the maximum depth d (how much a character extends below the baseline) in line T and the maximum height in line B.

Then TeX computes bdh. If this turns out to be positive, then TeX will insert a glob of vertical glue of this amount between the two lines.

Let's see an example:

\documentclass{article}

\begin{document}

\typeout{depth of y: \the\fontchardp\font`y}

\typeout{height of f: \the\fontcharht\font`h}

\hbox{xyfM}\hbox{xyf}

\showoutput

\end{document}

The relevant part of the log file is

depth of y: 1.94444pt
height of f: 6.94444pt
[...]
...\hbox(6.94444+1.94444)x22.77786
....\OT1/cmr/m/n/10 x
....\OT1/cmr/m/n/10 y
....\OT1/cmr/m/n/10 f
....\OT1/cmr/m/n/10 M
...\glue(\baselineskip) 3.11111
...\hbox(6.94444+1.94444)x13.61118
....\OT1/cmr/m/n/10 x
....\OT1/cmr/m/n/10 y
....\OT1/cmr/m/n/10 f

As you see, the depth of y is reflected in the depth of line T, namely \hbox(6.94444+1.94444)x22.77786; the height of f corresponds to the height of line B, namely \hbox(6.94444+1.94444)x13.61118.

If we try 1.94444+6.94444+3.11111 we get 11.99999, which to TeX is the same as 12pt; actually \typeout{\the\dimexpr1.94444pt+6.94444pt+3.11111pt} would print 12.0pt on the console.

However, if bdh is less than the value stored in \lineskiplimit (default is 0pt), instead of the computed glue, TeX inserts a fixed amount using the value stored in \lineskip (default 1pt), so that lines never overlap with each other (in the standard setup). By using suitable values of \lineskiplimit and \lineskip one can actually get lines that superimpose to each other, never filling a page (this is exploited in the \ooalign macro).

The usage of \hbox might seem unrealistic, but actually TeX makes \hboxes after splitting a paragraph into lines.

The amount by which a glyph extends outside its bounding box is unknown to TeX and irrelevant for its computations, which only use the bounding box, characterized by height, depth and width.

XeTeX has a primitive to do such measurements, namely \XeTeXglyphbounds; look for it in the XeTeX reference (texdoc xetex).