Ascent, Descent, and Leading
Ascent, descent, and leading are properties of fonts. The ascent
of a font is the distance from the tops of the tallest glyphs to the
baseline.
The descent of a font is the distance from the baseline to the bottom
of the lowest descenders on the glyphs.
The leading is the recommended vertical distance from the bottom of
the descenders to the top of the next line in a multiline setting.
A TextLayout object has methods for returning information
about the ascent, descent, and leading of the font. If the
text string uses more than one font, the ascent and descent
are computed as the maximum values for the fonts used in the
TextLayout object.
TextLayout.getAscent
TextLayout.getDescent
TextLayout.getLeading
Bounds
The bounding box encloses the text in the TextLayout
object. It includes all the visible glyphs and the caret
boundaries, some of which might hang over the origin or
origin plus advance.
A TextLayout object has methods to return its bounds and the
black box bounds for a range of characters.
The bounding box returned by TextLayout.getBounds is relative
to the origin of the TextLayout, and not to any particular
screen position.
The black box bounds is the union of the bounding boxes of all the glyphs
corresponding to the characters between a starting point and an ending point.
In bidirectional text, this path might be disjoint either on the display or in
the
source text. See the Selection Highlighting
section in Lesson 4 for information on how a range of characters in
bidirectional text is not always contiguous.
TextLayout.getBounds
TextLayout.getBlackBoxBounds
Justified Text
TextLayout.getCharacterLevel is not implemented yet so
you cannot justify a paragraph of text so all lines are equal in length.
However, you can right, center, or left justify
text by drawing the TextLayout object in a different
location computed by calling TextLayout.getVisibleAdvance.
The LineBreakSample.java code
shows how to left-align left-to-right text and right-align right-to-left
text. Here is how to align the other possibilities.
To right-align a left-to-right layout: Draw at
(rightMargin - layout.getVisibleAdvance()).
To center a left-to-right layout: Draw at
(leftMargin + rightMargin - layout.getVisibleAdvance()) / 2.
To left-align a right-to-left layout: Draw at
(leftMargin + (layout.getVisibleAdvance() - layout.getAdvance)).
To center a right-toleft layout: Draw at
(leftMargin + rightMargin + layout.getAdvance ()) / 2 - layout.getAdvance().
Character Level
TextLayout.getCharacterLevel returns the bidirectional level of the
character as computed by the bidirectional algorithm. This is primarily used to
determine if the character in question is a left-to-right or a right-to-left character
(which depends on context and style information, and is not a simple character
property). The level (0-16 inclusive) is returned so clients with particular user
interface needs (for instance, clients building user interfaces
that show nested bidirectional levels in some way) have access to the full
information.