module

CrImage::Font::Face

Face is a font face. Its glyphs are often derived from a font file, such as "Comic_Sans_MS.ttf", but a face has a specific size, style, weight and hinting. For example, the 12pt and 18pt versions of Comic Sans are two different faces, even if derived from the same font file.

A Face is not safe for concurrent use by multiple fibers, as its methods may re-use implementation-specific caches and mask image buffers.

To create a Face, look to other modules that implement specific font file formats.

Instance methods

ascent

Returns the ascent (distance from baseline to top) in pixels.

Source
descent

Returns the descent (distance from baseline to bottom) in pixels.

Source
glyph(dot : Math::Fixed::Point26_6, r : Char) : Tuple(CrImage::Rectangle, CrImage::Image, CrImage::Point, Math::Fixed::Int26_6, Bool)

glyph returns the Draw.draw_mask parameters (dr, mask, maskp) to draw r's glyph at the sub-pixel destination location dot, and that glyph's advance width.

It returns !ok if the face does not contain a glyph for r.

The contents of the mask image returned by one glyph call may change after the next glyph call. Callers that want to cache the mask must make a copy.

Source
glyph_advance(r : Char) : Tuple(Math::Fixed::Int26_6, Bool)

glyph_advance returns the advance width of r's glyph.

It returns !ok if the face does not contain a glyph for r.

Source
glyph_bounds(r : Char) : Tuple(Math::Fixed::Rectangle26_6, Math::Fixed::Int26_6, Bool)

glyph_bounds returns the bounding box of r's glyph, drawn at a dot equal to the origin, and that glyph's advance width.

It returns !ok if the face does not contain a glyph for r.

The glyph's ascent and descent equal -bounds.min.y and +bounds.max.y. A visual depiction of what these metrics are is at https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyph_metrics_2x.png

Source
glyph_by_index(dot : Math::Fixed::Point26_6, glyph_index : Int32) : Tuple(CrImage::Rectangle, CrImage::Image, CrImage::Point, Math::Fixed::Int26_6, Bool)

Render a glyph by its index.

Source
glyph_index(r : Char) : Int32

Convert a character to its glyph index. Returns 0 if the character is not in the font.

Source
has_vertical_metrics?

Returns true if this face has vertical metrics (for vertical text layout).

Source
kern(r0 : Char, r1 : Char) : Math::Fixed::Int26_6

kern returns the horizontal adjustment for the kerning pair (r0, r1). A positive kern means to move the glyphs further apart.

Source
kern_by_index(glyph0 : Int32, glyph1 : Int32) : Math::Fixed::Int26_6

Kerning by glyph index.

Source
line_height

Returns the line height for this face in pixels.

This is the recommended vertical distance between baselines of consecutive lines of text.

Example:

line_height = face.line_height
y = 50
lines.each do |line|
  drawer.draw_text(line, 10, y)
  y += line_height
end
Source
lookup_ligature(glyphs : Array(UInt16), start_index : Int32 = 0) : Tuple(UInt16, Int32)

Lookup ligature substitution for a glyph sequence. Returns {ligature_glyph, glyphs_consumed} or {0, 0} if no ligature.

Source
measure(text : String) : Int32

Measures the width of a string in pixels.

This is a convenience method that returns the advance width of the text as an integer pixel value.

Parameters:

  • text : The string to measure

Returns: Width in pixels

Example:

width = face.measure("Hello, World!")
puts "Text is #{width} pixels wide"
Source
metrics

metrics returns the metrics for this Face.

Source
supports_ligatures?

Returns true if this face supports ligature substitution. Default implementation returns false.

Source
text_bounds(text : String) : CrImage::Rectangle

Returns the bounding rectangle for rendered text.

The rectangle represents the visual bounds of the text, with the origin at (0, 0). The min.y will typically be negative (above baseline) and max.y positive (below baseline).

Parameters:

  • text : The string to measure

Returns: Rectangle with text bounds in pixels

Example:

bounds = face.text_bounds("Hello")
puts "Width: #{bounds.width}, Height: #{bounds.height}"
Source
text_size(text : String) : Tuple(Int32, Int32)

Returns both width and height of text as a tuple.

This is a convenience method for quickly getting text dimensions.

Parameters:

  • text : The string to measure

Returns: Tuple of {width, height} in pixels

Example:

width, height = face.text_size("Hello")
Source
vertical_advance(r : Char) : Tuple(Math::Fixed::Int26_6, Bool)

Get vertical advance for a character (used for vertical text layout). Returns {advance, ok} where ok is false if glyph not found.

Source
vertical_advance_by_index(glyph_index : Int32) : Math::Fixed::Int26_6

Get vertical advance by glyph index.

Source